• C扩展Python


    基本想法: 先看中文小介绍,再看英文详细文档。

    1. 参考

    首先参考THIS, IBM的工程师好像出了好多这样的文章啊,而且每次看到时间戳,我都想戳自己- -!

    2. ERROR

    可能遇到错误: fatal error: Python.c: No such file or directory

    a. 参考THIS ;  b. 或许用  sudo apt-get install python2.7-dev,命令就能解决(江湖传闻,只用于ubuntu) ; c. python2.7-dev 是什么?THIS

    3. 参考

    参考《Python core programming》- Chapter 22, 扩展Python - 全部看一遍

    注意到了 strdup() 函数。联想到和 strcpy() 函数 的区别。

    关于 strdup() 的解说 参考 THIS

    (这里,我一开始以为,下面的答案有 memory leak,但是注意返回值char*, 注意在《Python core programming》 中的用法,

    说明,如果你使用 p= strdup(),就需要你自己再次 free(p) , 对我来说,这就是最重要的区别。) 

    4. ERROR

    $ python setup.py install
    
    running install
    running build
    running build_ext
    running install_lib
    copying build/lib.linux-i686-2.7/Extest.so -> /usr/local/lib/python2.7/dist-packages
    error: /usr/local/lib/python2.7/dist-packages/Extest.so: Permission denied 

    注意到,error发生。猜测是ubuntu 读写权限问题。

    $ sudo python setup.py install
    [sudo] password for usr: 
    running install
    running build
    running build_ext
    running install_lib
    copying build/lib.linux-i686-2.7/Extest.so -> /usr/local/lib/python2.7/dist-packages
    running install_egg_info
    Writing /usr/local/lib/python2.7/dist-packages/Extest-0.0.0.egg-info

    问题解决。

    5. 参考

    a. 官方文档

    b. 应该是,中文翻译。面对这个翻译,我真的真的,尼玛很佩服啊!太tmd有用了啊!

    —— THIS,这个货,尼玛啊!是做果壳网的!!?

    c. THIS,虽然不是文档,但是看着还不错。

    说实话,看了一些资料之后,对于 “C扩展Python”这个问题,所有资料都指向官方文档。完全是躲不开的文档。除非你不玩了。那你还能不玩很多事情。

    6. 未解决

    static PyObject *
    spam_system(PyObject *self, PyObject *args) { ...}

    self ??

    The self argument points to the module object for module-level functions; for a method it would point to the object instance.

    7. 未解决

    where is

    An example may be found in the file Demo/embed/demo.c in the Python source distribution.

    where is

    A more substantial example module is included in the Python source distribution as Modules/xxmodule.c. This file may be used as a template or simply read as an example. 

     8. 未解决

    1.2, 1.4, 1.5, 1.6, 1.8需要例子,

    C中调用Python,要十分小心 reference 的使用,很容易导致内存问题。

    为什么所有的 要供给python使用的 封装函数,都要是static?

    9. <Python cookbook> - chapter 17

    -(17.1) Topic: C语言定义了一个新的类型,这个类型Python可以操作。

    a. 在开始17.1之前,先完成 THIS.

    b. PyObject_HEAD -> Py_ssize_t -> PyTypeObject -> PyObject 

    -> staticforward(注意:Python2.2中,定义新类型发生了变化。 Python2.7中,没有staticforward)

    -> statichere(THIS1(ctrl+F: statichere); THIS2; In a word, both staticforward & statichere are just static now.)

    -> PyArg_ParseTuple -> 

    未解决 - ERROR:

    running build
    running build_ext
    building 'elemlist' extension
    creating build
    creating build/temp.linux-i686-2.7
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c elemlist.c -o build/temp.linux-i686-2.7/elemlist.o
    elemlist.c: In function ‘car’:
    elemlist.c:58:31: error: ‘PyObject’ has no member named ‘car’
    elemlist.c: In function ‘cdr’:
    elemlist.c:67:31: error: ‘PyObject’ has no member named ‘cdr’
    elemlist.c: In function ‘setcar’:
    elemlist.c:78:5: error: ‘PyObject’ has no member named ‘car’
    elemlist.c:78:5: error: ‘PyObject’ has no member named ‘car’
    elemlist.c:78:5: error: ‘PyObject’ has no member named ‘car’
    elemlist.c:79:5: error: ‘PyObject’ has no member named ‘car’
    elemlist.c: In function ‘setcdr’:
    elemlist.c:91:5: error: ‘PyObject’ has no member named ‘cdr’
    elemlist.c:91:5: error: ‘PyObject’ has no member named ‘cdr’
    elemlist.c:91:5: error: ‘PyObject’ has no member named ‘cdr’
    elemlist.c:92:5: error: ‘PyObject’ has no member named ‘car’
    elemlist.c: In function ‘initelemlist’:
    elemlist.c:107:15: warning: unused variable ‘m’ [-Wunused-variable]
    elemlist.c: In function ‘cdr’:
    elemlist.c:68:1: warning: control reaches end of non-void function [-Wreturn-type]
    elemlist.c: In function ‘car’:
    elemlist.c:59:1: warning: control reaches end of non-void function [-Wreturn-type]
    error: command 'gcc' failed with exit status 1

    -(17.6) - 处理python数据到C中计算并返回。

    void
    inittotal922(void) {
        (void) Py_InitModule("total922", total922Methods);
    }  
    
    # 函数名规则: initModuleName
    # Py_InitModule(ModuleNmae, 方法数组)
    
    # setup.py
    # ext_modules = [Extension('Modulename', sources = ['sequenceFast_test.c'])]
    # 例: ext_modules = [Extension('total922', sources = ['sequenceFast_test.c'])]

     

  • 相关阅读:
    ffmpeg合并视频+音频
    You-Get和youtube-dl
    【会声会影】初装会声会影时,必要的设置
    【会声会影】视频导出、输出时,如何设置参数
    【会声会影】导入的srt字幕文件,如何快速批量调整字体及大小
    把h264文件快速包装成mp4格式
    Notepad++正则表达式——去掉srt字幕文件的时间轴
    百度地图API的网页使用
    ASPCMS_判断语句if标签的使用
    Grid布局
  • 原文地址:https://www.cnblogs.com/kevin922/p/3196999.html
Copyright © 2020-2023  润新知