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,解压后: 对于主要 ...
随机推荐
- oracle表空间操作语句
1.查看所有表空间及表空间大小: select tablespace_name ,sum(bytes) / 1024 / 1024 as MB from dba_data_files group by ...
- asp.net 视图引擎归类
1. ASPX View Engine 第一个也是我们最熟悉的---aspx,相信做过WebForm开发对Aspx都比较了解: 小示例: <%@ Control Inherits="S ...
- python3发送html格式的邮件
def send_mail(to_list, sub, content, attpath): me = "*******" + "<" + mail_us ...
- t-SNE和LDA PCA的学习
t-SNE 可以看这篇文章: http://bindog.github.io/blog/2016/06/04/from-sne-to-tsne-to-largevis/ LDA可以看这篇文章: htt ...
- x-forwarded-for之深度挖掘
如今利用nginx做负载均衡的实例已经很多了,针对不同的应用场合,还有很多需要注意的地方,本文要说的就是在通过CDN 后到达nginx做负载均衡时请求头中的X-Forwarded-For项到底发生了什 ...
- http://www.oschina.net/code/snippet_12_13918
http://www.oschina.net/code/snippet_12_13918
- new AppiumDriver<>(new URL(url), capabilities) 报错 java.lang.NoSuchMethodError: com.google.common.base.Throwables.throwIfUnchecked(Ljava/lang/Throwable;)V
2017-10-11 17:37:02.102 INFO c.u.a.r.PrepareDriver:41 - appium server url : http://127.0.0.1:4723/wd ...
- selenium以手机模拟器方式打开Google浏览器
使用chrome driver和chrome浏览器并进入chrome的 toggle device mode 模式,就可以很好的模拟手机端,下面直接上代码 public class runtest { ...
- 淘宝Diamond架构分析
转载:http://blog.csdn.net/szwandcj/article/details/51165954 早期的应用都是单体的,配置修改后,只要通过预留的管理界面刷新reload即可.后来, ...
- Mac机装Win7后 启动只见鼠标怎么办
我有一台Mac机,用Bootcamp的方式装了Win7,昨天一按开机键发现只有鼠标没有别的. 当时按热启动无效,把笔记本盖子合上一会再开也无效,按关机键关掉再开也无效(这时是短按). 当时想是不是Ma ...