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

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'])]

C扩展Python的更多相关文章

  1. 使用c/c++扩展python

    用python脚本写应用比较方便,但有时候由于种种原因需要扩展python(比如给程序提供python接口等). 之前一直想整理下,今天终于坐下来把这件事情给做了,这里记录下,也方便我以后查阅. 说明 ...

  2. 用 C 扩展 python

    本文介绍如何用 C 语言来扩展 python.所举的例子是,为 python 添加一个设置字符串到 windows 的剪切板(Clipboard)的功能.我在写以下代码的时候用到的环境是:window ...

  3. 使用C++扩展Python的功能 转自:http://blog.csdn.net/magictong/article/details/8897568#comments

    使用C++扩展Python的功能 环境 VS2005Python2.5.4 Windows7(32位) 简介 长话短说,这里说的扩展Python功能与直接用其它语言写一个动态链接库,然后让Python ...

  4. C语言扩展Python模块

    1. 先创建一个PythonDemo.cpp文件: //c/c++中调用python脚本,配置步骤参见上一篇:C/C++与python交互 \  C/C++中调用python文件. #include ...

  5. 扩展Python模块系列(三)----参数解析与结果封装

    在上一节中,通过一个简单的例子介绍了C语言扩展Python内建模块的整体流程,从本节开始讲开始深入讨论一些细节问题,在细节讨论中从始至终都会涉及[引用计数]的问题.首先讨论C语言封装的Python函数 ...

  6. 扩展Python模块系列(二)----一个简单的例子

    本节使用一个简单的例子引出Python C/C++ API的详细使用方法.针对的是CPython的解释器. 目标:创建一个Python内建模块test,提供一个功能函数distance, 计算空间中两 ...

  7. 扩展Python模块系列(一)----开发环境配置

    本系列将介绍如何用C/C++扩展Python模块,使用C语言编写Python模块,添加到Python中作为一个built-in模块.Python与C之间的交互目前有几种方案: 1. 原生的Python ...

  8. 扩展Python模块系列(四)----引用计数问题的处理

    承接上文,发现在使用Python C/C++ API扩展Python模块时,总要在各种各样的地方考虑到引用计数问题,稍不留神可能会导致扩展的模块存在内存泄漏.引用计数问题是C语言扩展Python模块最 ...

  9. 扩展Python模块系列(五)----异常和错误处理

    在上一节中,讨论了在用C语言扩展Python模块时,应该如何处理无处不在的引用计数问题.重点关注的是在实现一个C Python的函数时,对于一个PyObject对象,何时调用Py_INCREF和Py_ ...

随机推荐

  1. div+css的前端工程师的价值体现在哪些方面?

    个人认为前端工程师正慢慢演变为产品工程师.wap app, 响应性UI等以html5技术为基础的开发将成为前端工程师的主要工作内容,解决产品跨平台跨设备的实现问题.Javascript, HTML, ...

  2. 【BZOJ】【2223】【COCI 2009】PATULJCI

    可持久化线段树 同BZOJ 3524,但是不要像我一样直接贴代码……TAT白白WA了一次,so sad /*********************************************** ...

  3. 【转载】CCombobox使用大全

    一.如何添加/删除Combo Box内容 1. 在Combo Box控件属性的Data标签里面添加,一行表示Combo Box下拉列表中的一行.换行用ctrl+回车. 2. 在程序初始化时动态添加 如 ...

  4. linux vi修改后如何保存

    linux vi修改后如何保存 按ESC键去到命令模式,然后: :w?? 保存文件但不退出vi :w file 将修改另外保存到file中,不退出vi :w! 强制保存,不推出vi :wq 保存文件并 ...

  5. Unity手游:自动寻路Navmesh 跳跃 攀爬 斜坡

    原地址:http://dong2008hong.blog.163.com/blog/static/46968827201403114644210/ 步骤 1.在场景中摆放各种模型,包括地板,斜坡,山体 ...

  6. linux源码阅读笔记 move_to_user_mode()解析

    在linux 0.11版本源代码中,在文件linux/include/asm/system.h中有一个宏定义  move_to_user_mode() 1 #define move_to_user_m ...

  7. package.json 字段全解析 share

    Name 必须字段. 小提示: 不要在name中包含js, node字样: 这个名字最终会是URL的一部分,命令行的参数,目录名,所以不能以点号或下划线开头: 这个名字可能在require()方法中被 ...

  8. hdu 2486/2580 / poj 3922 A simple stone game 博弈论

    思路: 这就是K倍动态减法游戏,可以参考曹钦翔从“k倍动态减法游戏”出发探究一类组合游戏问题的论文. 首先k=1的时候,必败态是2^i,因为我们把数二进制分解后,拿掉最后一个1,那么会导致对方永远也取 ...

  9. BZOJ 3203 sdoi 2013 保护出题人

    由于样例解释很清晰,所以很容易得到以下结论: 1.每一关都是独立的,且僵尸的相对位置不会变 2.每一关的攻击力=Max(sum(i)/dis(i)) 其实sum(i)是僵尸攻击力的前缀和,dis(i) ...

  10. *[topcoder]PalindromicSubstringsDiv2

    http://community.topcoder.com/stat?c=problem_statement&pm=12967 计算一个字符串里Palindrome的数量.我的DP方法需要n^ ...