Extensions for Python
来源:互联网 发布:注册表卸载软件 编辑:程序博客网 时间:2024/06/03 20:51
备注:这里是我为培训写的PPT,网上资料较少,特此分享出来。
What is python extension?
The extension of python is that provided by other computer language such as C/C++.
Creating extensions for python involves three main steps:
1. Creating application code
2. Wrapping code with boilerplates
3. Compilation and testing
1. Creating application code
To call c function in python, we need program codes inherit corresponding wrap’s interface,and then Complier them as a python’s module.
2. Wrapping code with boilerplates
1). Include Python header file
#include “python.h”/< python.h >
2). Add PyObject* Module_func() Python wrappers for each module function
PyObject* method(PyObject* self, PyObject* args);
3). Add PyMethodDef ModuleMethods[] array/table for each module function
4). Add void initModule() module initialize function
3.Complier And Test
Python was default built with visual studio 2003. if you compile in dos, available compiler as follows:
1). create the setup.py file
from distutils.core import setup, Extension
MOD = ‘ModuleName‘
setup(name=MOD, ext_modules=[ Extension(MOD, sources=[‘C++fileName'])])
2). run “setup.py build” in commandline
3).copy the pyd file from “../Python25/build/lib.win32-2.5” folder to” ../Python25/DLLs” folder then we can import the extension in python.
4).Import And Test
A simple example for python extensions.
setp1: open Lcc-win32,choose file/new/file, create file type as follows:
Click ok button.
Create hello.c script, save this file.
#include <python.h>
#include <string.h>
static PyObject *message(PyObject *self, PyObject *args)
{
char *fromPython, result[64];
if (!PyArg_Parse(args, "(s)", &fromPython))
return NULL;
else
{
strcpy(result, "Hello, ");
strcat(result, fromPython);
return Py_BuildValue("s", result);
}
}
static PyMethodDef
helloMethods[] =
{
{"message", message, 1},
{NULL, NULL},
};
void inithello()
{
Py_InitModule("hello", helloMethods);
}
2.Copy hello.c file to python root directory. For example: “C:/Python25”
3. Create a setup.py file in “C:/Python25”.
from distutils.core import setup, Extension
MOD = 'hello'
setup(name=MOD, ext_modules=[
Extension(MOD, sources=['hello.c'])])
4. Open your windows shell window,entry into your python root, then input this command as follows:
5).copy the pyd file from “../Python25/build/lib.win32-2.5” folder to” ../Python25/DLLs” folder then we can import the extension in python.
6).Import And Test
Now, Import successfully!
- Extensions for Python
- Wireless Extensions for Linux
- Markup Extensions for XAML
- Useful extensions for UIView
- IDE for developing firefox extensions
- Native Extensions For Microsoft Silverlight
- App Extensions for iOS 8
- RxAndroid: Reactive Extensions for Android
- 理解 Visual C++ Extensions for ADO
- C library extensions for Windows CE
- Cross-browser NPAPI extensions for plugin scriptability
- One stop shop for BlogEngine 1.3 extensions
- Developing native extensions for Adobe AIR
- Developing native extensions for Adobe AIR
- Using native extensions for Adobe AIR
- Four Best Chrome Extensions for Reading JSON
- DISK - Check for INT 13h Extensions
- 1、Reactive Extensions for .NET(译)
- jsf+spring+hibernate 整合出现的错误集合
- 以此为念
- BADI using Enhancement Framework
- c++学习(0)
- Comegg的下一步开发已经提上个人的开发日程
- Extensions for Python
- Resin中JVM内存设置
- 使用SubVersion+TortoiseSVN多仓库方式进行版本控制
- 山寨版“破解图片防盗链”方法
- The "Double-Checked Locking is Broken" Declaration
- Apache Commons项目简介之Attributes
- fck editor 在java中的应用
- 动态SQL之一 EXEC
- getchar()和EOF总结