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

  1. windows 下 使用codeblocks 实现C语言对python的扩展

    本人比较懒就粘一下别人的配置方案了 从这开始到代码 摘自http://blog.csdn.net/yueguanghaidao/article/details/11538433 一直对Python扩展 ...

  2. 如何在Java中调用Python代码

    有时候,我们会碰到这样的问题:与A同学合作写代码,A同学只会写Python,而不会Java, 而你只会写Java并不擅长Python,并且发现难以用Java来重写对方的代码,这时,就不得不想方设法“调 ...

  3. Python Cookbook(第3版)中文版:15.16 不确定编码格式的C字符串

    15.16 不确定编码格式的C字符串¶ 问题¶ 你要在C和Python直接来回转换字符串,但是C中的编码格式并不确定. 例如,可能C中的数据期望是UTF-8,但是并没有强制它必须是. 你想编写代码来以 ...

  4. [转] C/C++ 调用Python

    from :  https://cyendra.github.io/2018/07/10/pythoncpp/ 目录 前言 官方文档 环境搭建 编译链接 Demo 解释器 初始化 GIL Object ...

  5. 在Java中调用Python

    写在前面 在微服务架构大行其道的今天,对于将程序进行嵌套调用的做法其实并不可取,甚至显得有些愚蠢.当然,之所以要面对这个问题,或许是因为一些历史原因,或者仅仅是为了简单.恰好我在项目中就遇到了这个问题 ...

  6. 『Python CoolBook』C扩展库_其六_线程

    GIL操作 想让C扩展代码和Python解释器中的其他进程一起正确的执行, 那么你就需要去释放并重新获取全局解释器锁(GIL). 在Python接口封装中去释放并重新获取全局解释器锁(GIL),此时本 ...

  7. 如何在python中调用C语言代码

    1.使用C扩展CPython还为开发者实现了一个有趣的特性,使用Python可以轻松调用C代码 开发者有三种方法可以在自己的Python代码中来调用C编写的函数-ctypes,SWIG,Python/ ...

  8. 使用c语言调用python小结

    近期在做一个漏洞展示平台,攻击实现部分使用python实现.c语言实现部分使用libcli库做一个类似telnet的东东,回调函数run的时候调用python模块. 针对c调用python,做个了小d ...

  9. Python源码分析(一)

    最近想学习下Python的源码,希望写个系列博客,记录的同时督促自己学习. Python源码目录 从Python.org中下载源代码压缩包并解压,我下载的是Python2.7.12,解压后: 对于主要 ...

随机推荐

  1. 【计算几何】【凸包】bzoj2829 信用卡凸包

    http://hzwer.com/6330.html #include<cstdio> #include<cmath> #include<algorithm> us ...

  2. Exercise02_09

    import javax.swing.JOptionPane; public class Acceleration { public static void main(String[] args){ ...

  3. ListView(下)自定义适配器

    (一) 1.效果图 2.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <L ...

  4. SSH学习——Spring基础

    1.理解什么是Spring框架? spring是J2EE应用程序框架,是轻量级的IOC和AOP的容器框架,主要是针对javaBean的生命周期进行管理的轻量级容器,可以单独使用,也可以和Struts框 ...

  5. iOS 调H5方法不执行没反应的坑

    调用H5的方法需要给H5传一些参数,参数中包括图片的base64字符串. 错误一: 图片转base64,后面参数不能随便写,正确做法如下 NSData *imageData = UIImageJPEG ...

  6. 【java】在分页查询结果中对最后的结果集List进行操作add()或remove()操作,报错:java.lang.UnsupportedOperationException

    场景: 在分页查询结果中对最后的结果集List进行操作add()或remove()操作,报错:java.lang.UnsupportedOperationException 错误: java.lang ...

  7. 【Node.js】2.开发Node.js选择哪个IDE 开发工具呢

    安装完Node.js之后,就要为它选择一个有利的IDE用于开发. 相比较了多个IDE之后,定位在webstrom和sublime上. 有一个简单的比较: webstorm功能很丰富,前端开发工具的集大 ...

  8. mongodb_服务端安装及连接

    安装环境:Windows7  64位    附件内容: 1.mongodb Windows7 补丁:451413_intl_x64_zip.exe 2.mongodb Windows 安装程序:mon ...

  9. Python - 文本处理模块

    文本处理模块 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27050431 Python的文本处理模块, 使用四种内置库. st ...

  10. Kafka 简单实验二(Python实现简单生产者消费者)

    Apache Kafka 是什么? Kafka 是一个开源的分布式流处理平台,其简化了不同数据系统的集成.流指的是一个数据管道,应用能够通过流不断地接收数据.Kafka 作为流处理系统主要有两个用处: ...