Python嵌入D

所有的嵌入都研究了一个遍。发现嵌入Python挺好,试试。

 

用D写Python的扩展,已经有了pyD,非常好用,效率也很高,http://pyd.dsource.org

 

将Python嵌入D,只能使用Raw的C-API了。这里用到了pyD中的Python接口文件

 

python.d和python25_digitalmars.lib,并且在本机安装了Python2.5

 

1.简单的高层次嵌入:就是执行一个Python的脚本

import python;

void main()
{
	Py_Initialize();

	
	PyRun_SimpleString("from time import time,ctime\n"
            "print('Today is:',ctime(time()))\n");	
	Py_Finalize();
}

 

2.低层次嵌入:修改了一个C的例子,在D中调用Python函数,并返回值。python脚本和调用写在注释了。

 

import python;
import std.stdio,std.string,std.c.string;
//call: t sm mw 2 3
/*
'''sm.py'''
def mw(a,b):
	print 'Will compute',a,'times',b
	c=0
	for i in range(0,a):
		c=c+b
	return c
*/
void main(char[][] argv)
{
	PyObject * pName,pModule,pDict,pFunc;
 
    PyObject *pArgs, pValue;
    int i,argc;
	argc = argv.length;
    if (argc < 3) {
        printf("Usage: call pythonfile funcname [args]\n");
        return 1;
    }
	printf("args:%d\n",argc);
	
    Py_Initialize();
    pName = PyString_FromString( toStringz(argv[1]) );
    /* Error checking of pName left out */

    pModule = PyImport_Import(pName);
    Py_DECREF(pName);

    if (pModule != null) {
        pFunc = PyObject_GetAttrString(pModule, toStringz(argv[2]));
        /* pFunc is a new reference */
        if (pFunc && PyCallable_Check(pFunc)) {
            pArgs = PyTuple_New(argc - 3);
			//set args
            for (i = 0; i < argc - 3; ++i) {
                pValue = PyInt_FromLong(atoi(argv[i + 3]));
                if (!pValue) {
                    Py_DECREF(pArgs);
                    Py_DECREF(pModule);
                    printf("Cannot convert argument\n");
                    return 1;
                }
                /* pValue reference stolen here: */
                PyTuple_SetItem(pArgs, i, pValue);
            }
			//call function
            pValue = PyObject_CallObject(pFunc, pArgs);
            Py_DECREF(pArgs); //release args
            if (pValue != null) {
                printf("Result of call: %ld\n", PyInt_AsLong(pValue));
                Py_DECREF(pValue); //release return
            } 
            else {
                Py_DECREF(pFunc); //
                Py_DECREF(pModule);
                PyErr_Print();
                printf("Call failed/n");
                return 1;
            }
        }
        else {
            if (PyErr_Occurred())
                PyErr_Print();
            printf( "Cannot find function \"%s\"\n", argv[2]);
        }
        Py_XDECREF(pFunc);
        Py_DECREF(pModule);
    }
    else {
        PyErr_Print();
        printf("Failed to load \"%s\"\n", argv[1]);
        return 1;
    }
    Py_Finalize();
	
    return 0;
}

 

关键的是在合适的时机释放Python的变量。自己也写了个简单的封装。比较而言,最简单的还是MiniD的嵌入,其次是Lua,Python的比较复杂。Python的好处是类库丰富,几乎要什么有什么。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值