zoukankan      html  css  js  c++  java
  • 使用C语言扩展Python3

    使用C语言扩展Python3。
    在Python3中正确调用C函数。

    1. 文件demo.c

    #include <Python.h>
     
    // c function
    static 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
    ", age);
        char total[10000];
        memset(total, 0, sizeof(total));
        strcat(total, "strcat() 函数用来连接字符串:");
        strcat(total, "tset");
        PyObject *result = Py_BuildValue("s", total);
        return result;
    }
     
    // method table
    static 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_FUNC
    PyInit_demo(void)
    {
        return PyModule_Create(&demomodule);
    }

    2. hello.py

    import demo
    print("---------------------------")
    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])
  • 相关阅读:
    服务器的Redis连接不上解决方案
    给大家推荐一个很好的自学网站
    简单说下HashMap的实现原理
    LinkedList源码解析
    你要了解的jvm
    单例设计
    百度编辑器删除旧的图片
    Ueditor 单图、多图、视频、附件的上传及在线管理总结
    上传新图片删除旧图片
    webapi发布IIS时出现500.19错误:不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的(overrideModeDefault="Deny")或者是通过包含overrideModeDefault="Deny"....
  • 原文地址:https://www.cnblogs.com/yaoyu126/p/6435912.html
Copyright © 2011-2022 走看看