设为首页 收藏本站
查看: 1140|回复: 0

[经验分享] python c++ extension

[复制链接]

尚未签到

发表于 2017-4-22 14:16:37 | 显示全部楼层 |阅读模式
在调试PIL的扩展Agg中对python的c扩展感到了好奇,所幸研究了一下python的c扩展。
并自己根据例子写了一下效果还不错,以下代码供大家学习:)
我本人对c不大了解,欢迎大家,分析install 流程 :)
执行结果:
DSC0000.png
编写代码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)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-367868-1-1.html 上篇帖子: 基于Visual C++2010 混合Python编程以及Python简介 下篇帖子: hive+python 入门
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表