python C PyObject
#include"Python.h"
//three ways :
/*
PyObject *MyFunction(PyObject *self, PyObject *args);
PyObject *MyFunctionWithKeywords(PyObject *self,
PyObject *args,
PyObject *kw);
PyObject *MyFunctionWithNoArgs(PyObject *self);
*/ //the return Py_RETURN_NONE
static PyObject* foo_bar(PyObject *self, PyObect *args)
{
//Do something interesting here. }
/*
struct PyMethodDef {
char *ml_name;//function name in python
PyCfunction ml_meth;//the address of function
int ml_flags;// METH_VARARGS METH_KEYWORDS METH_NOARGS
char *ml_doc;
};
*/
static PyMethodDef foo_methods[] = {
{"bar", (PyCFunction)foo_bar, METH_NOARGS, "My first function."},
{NULL, NULL, , NULL}
}; //init function for the python module
PyMODINIT_FUNC initfoo(){//init+moduleName Python解释器规定所有的初始化函数的函数名都必须以init开头,并加上模块的名字
Py_InitModule3("foo", foo_methods, "My first extension module.");
//Py_InitModule("foo", foo_methods, "My first extension module");
} //how to build te module
/* //in unix or linux:
gcc -shared -I/usr/include/python3.1 foo.c -o foo.so
// in windows:
cl /LD /IC:\Python31\include foo.c C:\Python31\libs\python31.lib
=============================================
example2:
#include"Python.h"
//three ways :
/*
PyObject *MyFunction(PyObject *self, PyObject *args);
PyObject *MyFunctionWithKeywords(PyObject *self,
PyObject *args,
PyObject *kw);
PyObject *MyFunctionWithNoArgs(PyObject *self);
*/ //the return Py_RETURN_NONE
static PyObject* foo_bar(PyObject *self, PyObect *args)
{
//Do something interesting here0.
Py_RETURN_NONE; }
static PyObject * foo_baz(PyObject *self, PyObject *args)
{
int i;
dobule d;
char *s;
if(!PyArg_ParseTuple(args, "ids", &i, &d, &s))
{
return NULL;
}
//Do something interesting here.
Py_RETURN_NONE;
}
static PyObject *foo_baz2(PyObject *self, PyObject *args)
{
int i;
double d;
char *s;
int i2 = ;
dobule d2 = 5.0;
char *s2 = "six";
if(!PyArg_ParseTuple(args, "ids|ids", &i, &d, &s, &i2, &d2, &s2))
{
return NULL;
}
//Do something interesting here.
Py_RETURN_NONE;
}
static PyObject *foo_quux(PyObject *self, PyObject *args, PyObject* kw)
{
char *kwlist[] = {"i", "d", "s", NULL};
int i;
double d = 2.0;
char *s = "three";
if(!PyArg_ParseTupleAndKeyWords(args, kw, "i|ds", kwlist, &i, &d, &s))
{
return NULL;
}
//Do something interesting here.
Py_RETURN_NONE;
}
static PyObject * foo_add(PyObject *self, PyObect *args)
{
int a;
int b;
if(!PyArg_ParseTuple(args, "ii", &a, &b))
{
return NULL;
}
return Py_BuildValue("i", a+b);
/*
*python function is:
def add(a,b):
return a + b
*/
}
static PyObject *foo_add_and_subtract(PyObject *self, PyObject *args)
{
int a;
int b;
if(!PyArg_ParseTuple(args, "ii", &a, &b))
{
return NULL;
}
return Py_BuildValue("(ii)", a+b, a-b);
/*
*python function is:
def add_and_subtrace(a, b):
return (a+b, a-b)
*/
}
/*
struct PyMethodDef {
char *ml_name;//function name in python
PyCfunction ml_meth;//the address the function
int ml_flags;// METH_VARARGS METH_KEYWORDS METH_NOARGS
char *ml_doc;
};
*/
static PyMethodDef foo_methods[] = {
{"bar", (PyCFunction)foo_bar, METH_NOARGS, "My first function."},
{"baz", (PyCFunction)foo_baz, METH_VARAGRS, NULL},
{"baz2", (PyCFunction)foo_baz2, METH_VARARGS, NULL},
{"quux", (PyCFunction)foo_quux, METH_VARARGS|METH_KEYWORDS, NULL},
{"add", (PyCFunction)foo_add, METH_VARARGS, NULL},
{"add_and_subtract", (PyCFunction)foo_add_and_subtract, METH_VARAGRS, NULL},
{NULL, NULL, , NULL}
}; //init function for the python module
PyMODINIT_FUNC initfoo(){//init+moduleName Python解释器规定所有的初始化函数的函数名都必须以init开头,并加上模块的名字
Py_InitModule3("foo", foo_methods, "My first extension module.");
//Py_InitModule("foo", foo_methods, "My first extension module");
} //how to build te module
/* //in unix or linux:
gcc -shared -I/usr/include/python3.1 foo.c -o foo.so
// in windows:
cl /LD /IC:\Python31\include foo.c C:\Python31\libs\python31.lib
python C PyObject的更多相关文章
- windows 下 使用codeblocks 实现C语言对python的扩展
本人比较懒就粘一下别人的配置方案了 从这开始到代码 摘自http://blog.csdn.net/yueguanghaidao/article/details/11538433 一直对Python扩展 ...
- 如何在Java中调用Python代码
有时候,我们会碰到这样的问题:与A同学合作写代码,A同学只会写Python,而不会Java, 而你只会写Java并不擅长Python,并且发现难以用Java来重写对方的代码,这时,就不得不想方设法“调 ...
- Python Cookbook(第3版)中文版:15.16 不确定编码格式的C字符串
15.16 不确定编码格式的C字符串¶ 问题¶ 你要在C和Python直接来回转换字符串,但是C中的编码格式并不确定. 例如,可能C中的数据期望是UTF-8,但是并没有强制它必须是. 你想编写代码来以 ...
- [转] C/C++ 调用Python
from : https://cyendra.github.io/2018/07/10/pythoncpp/ 目录 前言 官方文档 环境搭建 编译链接 Demo 解释器 初始化 GIL Object ...
- 在Java中调用Python
写在前面 在微服务架构大行其道的今天,对于将程序进行嵌套调用的做法其实并不可取,甚至显得有些愚蠢.当然,之所以要面对这个问题,或许是因为一些历史原因,或者仅仅是为了简单.恰好我在项目中就遇到了这个问题 ...
- 『Python CoolBook』C扩展库_其六_线程
GIL操作 想让C扩展代码和Python解释器中的其他进程一起正确的执行, 那么你就需要去释放并重新获取全局解释器锁(GIL). 在Python接口封装中去释放并重新获取全局解释器锁(GIL),此时本 ...
- 如何在python中调用C语言代码
1.使用C扩展CPython还为开发者实现了一个有趣的特性,使用Python可以轻松调用C代码 开发者有三种方法可以在自己的Python代码中来调用C编写的函数-ctypes,SWIG,Python/ ...
- 使用c语言调用python小结
近期在做一个漏洞展示平台,攻击实现部分使用python实现.c语言实现部分使用libcli库做一个类似telnet的东东,回调函数run的时候调用python模块. 针对c调用python,做个了小d ...
- Python源码分析(一)
最近想学习下Python的源码,希望写个系列博客,记录的同时督促自己学习. Python源码目录 从Python.org中下载源代码压缩包并解压,我下载的是Python2.7.12,解压后: 对于主要 ...
随机推荐
- 记一次深刻的教训-----将mat数据转化为SequenceFile
深刻的体会就是,“java.lang.NullPointer.Exception”就是空指针异常可能是由于数组部分元素未被初始化引起的. 1)使用jmatio将mat数据转化为SequenceFile ...
- NHibernate官方文档中文版--只读实体类型(Read-only entities)
重点 NHIbernate处理只读对象的方式可能和你在别处看到的不同.不正确的使用方式可能造成不可预料的结果. 当一个实体是只读的时候: NHIbernate不会对实体的简单属性和单向关联数据检查数据 ...
- telnet协议的作用详解,以及telnet端口号介绍
转:http://www.ctowhy.com/382.html Telnet协议,工作在TCP/IP协议栈的“应用层”,telnet是一种使用命令行的远程终端管理的协议,可以远程连接到网络设备上,并 ...
- 获取XIB子视图的两个方法
创建了一个XIB文件 CommentCell.xib,并设置好UIImageView的tag为100,昵称UILabel的tag为101,时间的UILabel的tag为102,并制定cell为Comm ...
- 调试Ajax调用的利器firebug
这几天我在家里调试PCS的Ajax调用时候发现一个问题就是调试手段太少,一般我会在进入ajax调用前加上一段alert输出变量信息. 比如 alert($("#taskid").v ...
- ylbtech-LanguageSamples-Generics(泛型)
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Generics(泛型) 1.A,示例(Sample) 返回顶部 “泛型”示例 (C#) ...
- iOS:扩展UIColor,支持十六进制颜色设置
来自转载:http://my.oschina.net/leejan97/blog/307491 摘要: 可以直接使用十六进制设置控件的颜色,而不必通过除以255.0进行转换 #define UICol ...
- [转载]Delphi Tokyo 10.2.3发布了
转载:http://blog.sina.com.cn/s/blog_44fa172f0102wwwg.html 今早最好的消息,Delphi 10.2.3如期发布,下载地址:http://altd.e ...
- 反向传播BP为什么高效
之前有一篇文章讲了反向传播的原理: 下面这篇文章讲了反向传播为什么高效: https://blog.csdn.net/lujiandong1/article/details/52716726 主要通过 ...
- maven的安装和eclipse的配置以及构建mahout基本项目
maven介绍在此略过 下载地址: 点击打开链接 windows下载xxx-bin.zip文件 linux下载xxx-bin.tar.gz 这里以windows为例 下载完成直接解压到一个目录下 计算 ...