使用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\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])
使用C语言扩展Python3的更多相关文章
- C语言扩展Python模块
1. 先创建一个PythonDemo.cpp文件: //c/c++中调用python脚本,配置步骤参见上一篇:C/C++与python交互 \ C/C++中调用python文件. #include ...
- Azure Table storage 之改进DynamicTableEntity类为其添加动态语言扩展
在之前的一篇文章中提到,storage类库中包含一个可以用来动态获取Azure table storage 表结构的类-DynamicTableEntity. 我们可以通过这个类,我们无需为每一个表提 ...
- javascript语言扩展:可迭代对象(3)
除了前2篇文章中描述的可迭代对象以外,在js语言扩展中的生成器对象,也可以作为可迭代对象. 这里用到一个新的关键字yield,该关键字在函数内部使用,用法和return类似,返回函数中的一个值:yie ...
- R语言扩展包dplyr——数据清洗和整理
R语言扩展包dplyr——数据清洗和整理 标签: 数据R语言数据清洗数据整理 2015-01-22 18:04 7357人阅读 评论(0) 收藏 举报 分类: R Programming(11) ...
- C#高级编程9-第12章 动态语言扩展
C#高级编程9-第12章 动态语言扩展 dynamic t = new ExpandoObject(); t.Abc = "abc"; t.Value = ; Console.Wr ...
- Haskell语言学习笔记(88)语言扩展(1)
ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...
- javascript语言扩展:可迭代对象(1)
在ECMAScript中我们知道可以通过for in语句进行对象属性的遍历,当然这些属性不包括继承而来的属性: var ary = [1,2,3,"aa",4]; for(i in ...
- R语言扩展包dplyr笔记
引言 2014年刚到, 就在 Feedly 订阅里看到 RStudio Blog 介绍 dplyr 包已发布 (Introducing dplyr), 此包将原本 plyr 包中的 ddply() 等 ...
- 使用C语言扩展Python提供性能
python底层是用c写的,c本身是一个非常底层的语言,所以它做某些事情的效率肯定会比上层语言高一些. 比如有些自动化测试用的python库,会对系统的UI进行一些捕获,点击之类的操作,这必然要用到c ...
随机推荐
- 团体程序设计天梯赛-练习集-L1-039. 古风排版
L1-039. 古风排版 中国的古人写文字,是从右向左竖向排版的.本题就请你编写程序,把一段文字按古风排版. 输入格式: 输入在第一行给出一个正整数N(<100),是每一列的字符数.第二行给出一 ...
- esp32使iOS 获取蓝牙外设的Mac地址
最近在做一个需要上下位机的项目,我负责的任务下位机,使用的主控芯片是esp32.这个项目中有一项是需要手机扫描二维码然后连接作为esp32的蓝牙.二维码中包含了mac地址信息,在手机扫描周围设备的时候 ...
- 多文件编程(day13)
多文件编程时一个文件里可以包含多个函数, 一个函数只能属于一个文件 多文件编程的步骤 .把所有函数分散在多个不同的源文件里 (主函数通常单独占一个文件) .为每个源文件编写一个配对的以.h作为 扩展名 ...
- 继续聊WPF——Thumb控件
这个控件,真不好介绍,MSDN上也是草草几句,反正就是可以让用户拖动的玩意儿,但是,你会发现,当你在该控件上拖动时,它没有反响,也就是说这个东西默认不做任何操作的,它是赖在那里什么都不干,除非你去踢上 ...
- 1.git的安装和配置
从git官网下一个git安装包. 点击git.exe安装程序,点击[next] 点击[next] 根据自己的需要可以把需要安装软件全选上,点击[next] 由于这是个人使用所以选择第二 ...
- sqlalchemy带条件查询相关应用
sqlalchemy带条件查询 filter_by与filter filter_by 与filter的区别: 1. filter_by只能取值= filter可以==,!=,>=,<=等多 ...
- 【习题 4-2 Uva201】Squares
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 注意那个星号的数量... 然后V x y的话,是从(y,x)向(y+1,x)连线. H x y才是从(x,y)向(x,y+1)连线 ...
- 合并目录中的txt文件
txt 文件 合并 并 按章节 分隔 # -*- coding: utf8 -*- import os result_name = 'result.txt' def resplit_txt_in_fo ...
- find结合rm删除大量文件
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://navarro.blogbus.com/logs/31502374.html 例:删除/home/raven下,包括子目录 ...
- 【ACM】hdu_1234_开门人和关门人_201307300845
开门人和关门人Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...