使用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的更多相关文章

  1. C语言扩展Python模块

    1. 先创建一个PythonDemo.cpp文件: //c/c++中调用python脚本,配置步骤参见上一篇:C/C++与python交互 \  C/C++中调用python文件. #include ...

  2. Azure Table storage 之改进DynamicTableEntity类为其添加动态语言扩展

    在之前的一篇文章中提到,storage类库中包含一个可以用来动态获取Azure table storage 表结构的类-DynamicTableEntity. 我们可以通过这个类,我们无需为每一个表提 ...

  3. javascript语言扩展:可迭代对象(3)

    除了前2篇文章中描述的可迭代对象以外,在js语言扩展中的生成器对象,也可以作为可迭代对象. 这里用到一个新的关键字yield,该关键字在函数内部使用,用法和return类似,返回函数中的一个值:yie ...

  4. R语言扩展包dplyr——数据清洗和整理

    R语言扩展包dplyr——数据清洗和整理 标签: 数据R语言数据清洗数据整理 2015-01-22 18:04 7357人阅读 评论(0) 收藏 举报  分类: R Programming(11)  ...

  5. C#高级编程9-第12章 动态语言扩展

    C#高级编程9-第12章 动态语言扩展 dynamic t = new ExpandoObject(); t.Abc = "abc"; t.Value = ; Console.Wr ...

  6. Haskell语言学习笔记(88)语言扩展(1)

    ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...

  7. javascript语言扩展:可迭代对象(1)

    在ECMAScript中我们知道可以通过for in语句进行对象属性的遍历,当然这些属性不包括继承而来的属性: var ary = [1,2,3,"aa",4]; for(i in ...

  8. R语言扩展包dplyr笔记

    引言 2014年刚到, 就在 Feedly 订阅里看到 RStudio Blog 介绍 dplyr 包已发布 (Introducing dplyr), 此包将原本 plyr 包中的 ddply() 等 ...

  9. 使用C语言扩展Python提供性能

    python底层是用c写的,c本身是一个非常底层的语言,所以它做某些事情的效率肯定会比上层语言高一些. 比如有些自动化测试用的python库,会对系统的UI进行一些捕获,点击之类的操作,这必然要用到c ...

随机推荐

  1. CNN:Windows下编译使用Caffe和Caffe2

    用于检测的CNN分为基于回归网络的方法和基于区域+CNN网络的方法,其中基于回归网络的方法典型为YOLO9000,可以兼容使用VGG-Net框架.其中基于区域+CNN网络方法,大量使用了Caffe作为 ...

  2. 人脸Pose检测:ASM、AAM、CLM总结

    人脸的Pose检测可以使用基于位置约束的特征点的方法.人脸特征点定位的目的是在人脸检测的基础上,进一步确定脸部特征点(眼睛.眉毛.鼻子.嘴巴.脸部外轮廓)的位置.定位算法的基本思路是:人脸的纹理特征和 ...

  3. Java:Java 队列的遍历

    Java队列到底有没有可以遍历的功能呢?暂且试一下吧 参考链接:stl容器遍历测试 1.LinkedList实现简单遍历 for(Iter =LocTimesSerials.size()-1; iSe ...

  4. Callback-回调-回呼

    很早以前看<Delphi 4从入门到精通>有这么一个概念——CallBack.然后在<Delphi 6从入门到精通>看同样的章节,翻译为“回调”,就有一个疑问了,什么是Call ...

  5. 【sqli-labs】 less33 GET- Bypass AddSlashes (GET型绕过addslashes() 函数的宽字节注入)

    和less32一样,对关键字符进行了添加\ 关于addslashes()函数 payload和less32一样 http://192.168.136.128/sqli-labs-master/Less ...

  6. 身份认证防止重放攻击的challenge-response方法

    或者叫询问-应答机制. 基于挑战/应答(Challenge/Response)方式的身份认证系统就是每次认证时认证服务器端都给客户端发送一个不同的"挑战"字串,客户端程序收到这个& ...

  7. SqlLite提高批量插入速度的方法及原因分析

    (1)-SQLite忽略大小写查询大部分数据库在进行字符串比较时,对大小写是不敏感的.但是SQLite却是大小写敏感的.如果想让SQLite忽略大小写,方法如下:方法一:使用大小写转换函数LOWER. ...

  8. RabbitMQ出现服务启动几秒退出问题

    最近在学习rebbitmq, 1.首先安装了otp_win64_20.3, 2.erlang安装完成需要配置erlang环境变量: 这个是新建的 文档是:ERLANG_HOME D:\develop\ ...

  9. html第三节课

    表单 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

  10. 原生js封装ajax,深入理解$.ajax()

    直接上代码 //封装的ajax函数 // 传一个对象,所有要用的参数都在对象中 因为不写对象 实参列表个数太多,所以像jq一样,调用ajax也是把对象当实际参数传进去 // type 请求方式 默认g ...