V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2019-07-12 20:13:24 +08:00 · 2289 次点击
    这是一个创建于 1721 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

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

    c 代码编译成动态库,然后让 python 调用? 或者把核心组件用 python 写,用 cython 编译成 pyd(win 系统)或.so(linux),给其它 python 程序使用。
    HelloAmadeus
        3
    HelloAmadeus  
       2019-07-12 22:14:22 +08:00 via iPhone
    多看看官方文档,要区分 borrowed reference 和 new reference 的区别,按照 cpython 的源码规范来,引用计数管理比较简单的
    ysc3839
        4
    ysc3839  
       2019-07-13 02:43:22 +08:00 via Android   ❤️ 1
    如果可以使用 C++ 的话试试 pybind11 ?
    lastright
        5
    lastright  
    OP
       2019-07-13 06:03:13 +08:00
    @anonymous256  也需要跟 list,dict 打交道。如果只跟 str 打交道的话,那直接一步 PyUnicode_UnicodeAsXX 就行了,对吧? 至于 C 和 python 谁调用谁,还得听上面的,我是个小兵。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1223 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 18:05 · PVG 02:05 · LAX 11:05 · JFK 14:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.