使用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\n", age);
char total[];
memset(total, , 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, , 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 */
-, /* 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])

最新文章

  1. 我用了13行代碼開發出来的PHP框架
  2. [CareerCup] 14.2 Try-catch-finally Java中的异常处理
  3. hiho 第116周,最大流最小割定理,求最小割集S,T
  4. mysql常用的命令大全
  5. redhat5安装phantomjs
  6. Mark一下~
  7. python 试题
  8. yarn application ID 增长达到10000后
  9. MySQL--binlog和relay log的生成和删除
  10. 淘宝JAVA中间件Diamond详解之简介&amp;快速使用 管理持久配置的系统
  11. K8S调度之节点亲和性
  12. Lichee (五) sysconfig1.fex 配置系统
  13. springboot获取URL请求参数的多种方式
  14. [Selenium]Grid模式下运行时打印出当前Case在哪台node机器上运行
  15. PHPWAMP自启异常,服务器重启后Apache等服务不会自动重启的原因分析
  16. 5个基于Linux命令行的文件下载和网站浏览工具
  17. commond prompt CD
  18. java 的 &amp;和&amp;&amp;的区别
  19. spring aop 原理学习
  20. Python框架之Django学习笔记(六)

热门文章

  1. VHDL之FSM
  2. 使用replace pioneer批量修改文件名
  3. java 发送http请求
  4. python tips:匿名函数lambda
  5. Markdown 常用语法总结
  6. javascrip this指向问题深入理解
  7. SP1825 FTOUR2 - Free tour II 点分治+启发式合并+未调完
  8. [NOIP模拟赛]b
  9. 使用Selenium爬取网站表格类数据
  10. Problem 17