推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lastright
V2EX  ›  Python

在给 Python 开发一个 C 模块,引用计数全靠眼睛吗?

  •  
  •   lastright · Jul 12, 2019 · 3204 views
    This topic created in 2513 days ago, the information mentioned may be changed or developed.

    Python 新手,看了一下 API,对象转字符串需要分两步走,第一步 PyObject_Str 弄出来一个 PyObject,第二步才能从这个临时的 PyObject 拿到 char *的 C-string.

    但这个中间的对象我又不敢递减计数,还得等这个 C-string 用完了。。

    有点儿麻烦啊,大家平时都这么凭肉眼计数吗。

    5 replies    2019-07-13 06:03:13 +08:00
    bigtan
        1
    bigtan  
       Jul 12, 2019   ❤️ 1
    不要自己直接用 Python 接口写,可以用类似 cython 的东西。
    anonymous256
        2
    anonymous256  
       Jul 12, 2019 via Android
    你的 C 模块要接受 Python 什么样的数据结构? list,dict 这种高级数据结构吗,还是说只是字符串这种简单的。

    c 代码编译成动态库,然后让 python 调用? 或者把核心组件用 python 写,用 cython 编译成 pyd(win 系统)或.so(linux),给其它 python 程序使用。
    HelloAmadeus
        3
    HelloAmadeus  
       Jul 12, 2019 via iPhone
    多看看官方文档,要区分 borrowed reference 和 new reference 的区别,按照 cpython 的源码规范来,引用计数管理比较简单的
    ysc3839
        4
    ysc3839  
       Jul 13, 2019 via Android   ❤️ 1
    如果可以使用 C++ 的话试试 pybind11 ?
    lastright
        5
    lastright  
    OP
       Jul 13, 2019
    @anonymous256  也需要跟 list,dict 打交道。如果只跟 str 打交道的话,那直接一步 PyUnicode_UnicodeAsXX 就行了,对吧? 至于 C 和 python 谁调用谁,还得听上面的,我是个小兵。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2594 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 10:37 · PVG 18:37 · LAX 03:37 · JFK 06:37
    ♥ Do have faith in what you're doing.