python c++ extension

在调试PIL的扩展Agg中对python的c扩展感到了好奇,所幸研究了一下python的c扩展。
并自己根据例子写了一下效果还不错,以下代码供大家学习:)
我本人对c不大了解,欢迎大家,分析install 流程 :)

执行结果:
[img]http://dl.iteye.com/upload/picture/pic/81573/f70076d3-8ea1-341b-af6f-cef3c8118389.png[/img]

编写代码spam.cxx


/*
How to use :
import spam
status = spam.system("ls -l")
dome is C++ Extension
*/

#define VERSION "1.2a3"

#if defined(_MSC_VER)
#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>
#endif

#include "Python.h"

#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x01060000
#if PY_VERSION_HEX < 0x02020000 || defined(Py_USING_UNICODE)
/* defining this enables unicode support (default under 1.6a1 and later) */
#define HAVE_UNICODE
#endif
#endif

/*
Function Define and AttributesDefine
*/
static PyObject *CmdError;
static PyObject *
cmd(PyObject *self, PyObject *args)
{
const char *command;
int sts;

/*
int PyArg_ParseTuple(PyObject *args, const char *format, ...)
return true or false | exception
*/
if (!PyArg_ParseTuple(args, "s", &command))
{
PyErr_SetString(CmdError, "System command failed");
return NULL;
}
else{
printf("run command is:");
puts(command); //or use PyObject_Print for debug
}
sts = system(command);
//PyObject* Py_BuildValue(const char *format, ...)
return Py_BuildValue("i", sts);
}


static PyMethodDef CmdMethods[] = {
{"cmd", (PyCFunction) cmd, METH_VARARGS},
{NULL, NULL}
};


/*
Initial Modules
*/

PyMODINIT_FUNC
initspam(void)
{
PyObject *m;
m = Py_InitModule("spam", CmdMethods);
if (m == NULL)
return;
/*
PyObject* PyErr_NewException(char *name, PyObject *base, PyObject *dict)
*/
CmdError = PyErr_NewException("system.error", NULL, NULL);
Py_INCREF(CmdError);
PyModule_AddObject(m, "error", CmdError);
};

extern "C"
DL_EXPORT(void)
initaggdraw(void)
{
/* Initialize the Python interpreter. Required. */
Py_Initialize();

/* Add a static module */
initspam();

PyObject* g = PyDict_New();
PyDict_SetItemString(g, "__builtins__", PyEval_GetBuiltins());
PyRun_String("print 'welcome!'\n", Py_file_input, g, NULL);
}


setup.py

from distutils.core import setup, Extension

moduleSpam = Extension('spam',sources = ['spammodule.cxx'])

setup(name = 'Spam system',
version = '1.0',
description = 'This is a demo package for run system command',
ext_modules = [moduleSpam])



跟踪了一下安装过程,发现setup 查找Setup.local文件:

/*
在 Setup.local 文件中我们可以指定输出
*/
spam spammodule.so




:execve("/usr/bin/python", ["python", "setup.py", "install"], [/* 57 vars */]) = 0
104:stat("/usr/bin/Modules/Setup", 0x7fff47082070) = -1 ENOENT (No such file or directory)
419:stat("/usr/local/lib64/python2.6/site-packages/setuptools-0.6c11-py2.6.egg", {st_mode=S_IFREG|0644, st_size=333581, ...}) = 0
446:open("/usr/local/lib64/python2.6/site-packages/setuptools.pth", O_RDONLY) = 5
450:read(5, "./setuptools-0.6c11-py2.6.egg\n", 8192) = 30
469:stat("/usr/local/lib64/python2.6/site-packages/setuptools-0.6c11-py2.6.egg", {st_mode=S_IFREG|0644, st_size=333581, ...}) = 0
470:open("/usr/local/lib64/python2.6/site-packages/setuptools-0.6c11-py2.6.egg", O_RDONLY) = 4
2738:lstat("/home/bmc/test/ct/Modules/setup.py", {st_mode=S_IFREG|0644, st_size=262, ...}) = 0
2739:stat("setup.py", {st_mode=S_IFREG|0644, st_size=262, ...}) = 0
2740:open("setup.py", O_RDONLY) = 3
2745:read(3, "from distutils.core import setup"..., 240) = 240
2749:stat("setup.py", {st_mode=S_IFREG|0644, st_size=262, ...}) = 0
2750:open("setup.py", O_RDONLY) = 3
2756:read(3, "from distutils.core import setup"..., 4096) = 262
2853:read(4, "\0\0\0error in %s setup command: %s"..., 4096) = 3617
3915:stat("setup.cfg", 0x7fff47083e70) = -1 ENOENT (No such file or directory)
3999:stat("/usr/bin/Modules/Setup.dist", 0x7fff4707c980) = -1 ENOENT (No such file or directory)
4000:stat("/usr/bin/Modules/Setup.local", 0x7fff4707c980) = -1 ENOENT (No such file or directory)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值