在调试PIL的扩展Agg中对python的c扩展感到了好奇,所幸研究了一下python的c扩展。
并自己根据例子写了一下效果还不错,以下代码供大家学习:)
我本人对c不大了解,欢迎大家,分析install 流程 :)
执行结果:
编写代码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)
分享到:
相关推荐
One of the features of Python that makes it so powerful is the ability to take existing libraries, written in C or C++, and make them available as Python extension modules. Such extension modules are ...
1,打开 VS 2019,新建C++ Windows 动态链接库工程 Example,加入下列文件,如果Python是64位的则在VS中 Solution platforms 选择 x64 编译成64位的 DLL; Example.h #pragma once #ifndef CPP_EXPORTS #define ...
Python is also suitable as an extension language for customizable applications. Python 的解释器很容易通过C 或C++ (或者其它可以由C来调用的语言)来扩展新的函数和数据结构。因 此Python 也很适于作为定制...
convince someone to give me a biology degree for writing an extension for ArcView GIS (a precursor to A rc GIS , for you Esri fans out there). After finishing that up, I went to work for the Remote ...
Programming Python, 2nd Edition 目录如下,需要下载的朋友不要错过哦~ Programming Python, 2nd Edition By Mark Lutz Publisher : O'Reilly Pub Date : March 2001 ISBN : 0-596-00085-5 Pages : 1256 ...
Without extension, using pure python implementation on CPython runs slowly. Notes ----- Note for msgpack 2.0 support ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ msgpack 2.0 adds two types: *bin* and *ext*. *raw*...
1.打开Unofficial Windows Binaries for Python Extension Packages(http://www.lfd.uci.edu/~gohlke/pythonlibs/),这里面有很多封装好的Python模块的运行环境 2.找到所需要下载的模块文件对应版本进行下载。 如...
One of the features of Python that makes it so powerful is the ability to take existing libraries, written in C or C++, and make them available as Python extension modules. Such extension modules are ...
Swapping One File Extension for Another Throughout a Directory Tree Recipe 2.18. Finding a File Given a Search Path Recipe 2.19. Finding Files Given a Search Path and a Pattern Recipe 2.20. ...
c/c++, lua, python, c#, ruby, java等语言可以通过CLE中间件直接使用其它语言中的类,函数,变量或者模块, 使得现有的功能库或者模块可以很容易的应用到其它编程语言中。开发者可以使用熟悉的语言编写功能库,然后...
This manual documents the API used by C and C++ programmers who want to write extension modules or embed Python. It is a companion to Extending and Embedding the Python Interpreter (in Extending and ...
软件开发设计:PHP、QT、应用软件开发、系统软件开发、移动应用开发、网站开发C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、...
用python操作修改windows注册表,显然要比用C或者C++简单。 主要参考资料:官方文档:https://docs.python.org/zh-cn/3/library/winreg.html 通过python操作注册表主要有两种方式,一种是通过python的内置模块 _...
Python Extension Packages for Windows - Christoph Gohlke http://www.lfd.uci.edu/~gohlke/pythonlibs/ TA-Lib, a wrapper for the TA-LIB Technical Analysis Library. TA_Lib‑0.4.10‑cp27‑cp27m‑win32.whl ...
├── c_extension C/C++扩展Python模块 ├── cdn_utils cdn相关工具 ├── compress 压缩工具 ├── coroutine 协程 ├── database_manager 数据库操作 ├── image_utils 图像处理工具 ├── logger 日志...
It is implemented as a Python extension module (native code) that wraps the popular wxWidgets cross platform GUI library, which is written in C++. Like Python and wxWidgets, wxPython is Open Source ...
Cython is to C/C++ and Python. It allows us to extend and develop bindings to applications in a really intuitive manner so that we are able to reuse code from levels of the software stack. The Cython ...
extension on C++ proven technology (5 of 5 documents) Good references.
error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/ 将文件至python\Scripts目录下,然后执行 pip.exe install .\...
python中使用pip安装扩展包的时候,有时候会遇到如下类似报错: Running setup.py install for mysqlclient … error …(中间报错信息省略) building ‘MySQLdb._mysql’ extension error: Microsoft Visual C++ ...