使用C語言擴充Python3

來源:互聯網
上載者:User

標籤:void   lag   erp   import   ati   setup.py   package   ems   字元   

使用C語言擴充Python3。
在Python3中正確調用C函數。

1. 檔案demo.c
#include <Python.h> // c functionstatic PyObject *demo_system(PyObject *self, PyObject *args) {    const char *command;    int sts;    if (!PyArg_ParseTuple(args, "s", &command))        return NULL;    sts = system(command);    return PyLong_FromLong(sts);} static PyObject *demo_hello(PyObject *self, PyObject *args) {    PyObject *name, *result;    if (!PyArg_ParseTuple(args, "U:demo_hello", &name))        return NULL;    result = PyUnicode_FromFormat("Hello, %S!", name);    return result;} static PyObject *demo_chinese(PyObject *self, PyObject *args) {    char *name;    int age;    if (!PyArg_ParseTuple(args, "si", &name, &age))         return NULL;    // printf("%d\n", age);    char total[10000];    memset(total, 0, sizeof(total));    strcat(total, "strcat() 函數用來連接字串:");    strcat(total, "tset");    PyObject *result = Py_BuildValue("s", total);    return result;} // method tablestatic PyMethodDef DemoMethods[] = {    {"system", // python method name     demo_system, // matched c function name     METH_VARARGS, /* a flag telling the interpreter the calling                                 convention to be used for the C function. */     "I guess here is description." },      {"hello", demo_hello,  METH_VARARGS, "I guess here is description." },     {"chinese", demo_chinese, METH_VARARGS, NULL },     {NULL, NULL, 0, NULL}        /* Sentinel */}; // The method table must be referenced in the module definition structure.static struct PyModuleDef demomodule = {    PyModuleDef_HEAD_INIT,    "demo",   /* name of module */    NULL, /* module documentation, may be NULL */    -1,       /* size of per-interpreter state of the module,                or -1 if the module keeps state in global variables. */    DemoMethods}; // The initialization function must be named PyInit_name()PyMODINIT_FUNCPyInit_demo(void){    return PyModule_Create(&demomodule);}
2. hello.py
import demoprint("---------------------------")status = demo.system("ls -l")print("---------------------------")hi = demo.hello("Sink")print(hi)print("---------------------------")hi = demo.chinese("Sink", 2014)print(hi)print("---------------------------")
3. setup.py
from distutils.core import setup, Extension module1 = Extension(‘demo‘,                    sources = [‘demo.c‘]) setup (name = ‘Demo hello‘,       version = ‘1.0‘,       description = ‘This is a demo package by Sink‘,       ext_modules = [module1])

 

使用C語言擴充Python3

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.