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,解压后: 对于主要 ...
随机推荐
- 【数论】【暴力】bzoj4052 [Cerc2013]Magical GCD
考虑向一个集合里添加一个数,它们的gcd要么不变,要么变成原gcd的一个约数.因此不同的gcd只有log个. 所以对于每个位置,维护一个表,存储从这个位置向前所有的不同的gcd及其初始位置,然后暴力更 ...
- 【动态规划】【最长上升子序列】【贪心】bzoj1046 [HAOI2007]上升序列
nlogn求出最长上升子序列长度. 对每次询问,贪心地回答.设输入为x.当前数a[i]可能成为答案序列中的第k个,则若 f[i]>=x-k && a[i]>ans[k-1] ...
- 【KM算法】HDU2255-奔小康赚大钱
KM算法的裸体.O(n^4)的模板,实际上在增广路径的时候依然有冗余,可以用bfs优化到O(n^3). #include <iostream> #include <cstdio> ...
- Spark1.4远程调试
1)首先,我们是在使用spark-submit提交作业时,使用 --driver-java-options ”-Xdebug -Xrunjdwp:transport=dt_socket,server= ...
- python之装饰器、生成器、内置函数、JSON
一.装饰器: 装饰器,器在这里的意思是函数,也就是装饰函数.作用是给其他函数添加新功能,它可以不改变原有的函数,原来的函数和原来一模一样,什么都不需要改变,只需要在函数外部加上调用哪个装饰器就可以了, ...
- [CSS]滚动条样式设置
概述 最近项目中需要,将一个页面嵌入在一个webbrower中,这个webrower是定高的,在页面内容超过webbrower高度时,需要以滚动条的形式展现,当时也考虑了使用webbrower的滚动条 ...
- ES6笔记之参数默认值(译)
原文链接:http://dmitrysoshnikov.com/ 原文作者:Dmitry Soshnikov 译者做了少量补充.这样的的文字是译者加的,可以选择忽略. 作者微博:@Bosn 在这个简短 ...
- projecteuler---->problem=11----Largest product in a grid
In the 2020 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 1 ...
- 关于 iOS 证书,你必须了解的知识
收录待用,修改转载已取得腾讯云授权 最新腾讯云技术公开课直播,提问腾讯W3C代表,如何从小白成为技术专家?点击了解活动详情. 作者 |陈泽滨 编辑 | 顾乡 从事iOS开发几年,越来越发现,我们的开发 ...
- [Angular] Control the dependency lookup with @Host, @Self, @SkipSelf and @Optional
Very differently to AngularJS (v1.x), Angular now has a hierarchical dependency injector. That allow ...