`
ideage
  • 浏览: 319018 次
社区版块
存档分类
最新评论

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的好处是类库丰富,几乎要什么有什么。

 

分享到:
评论
6 楼 tomqyp 2009-06-17  
试了下,在phobo版中也使用pyd自己实现的minNumArgs,现在通过编译了。

看来D1里面问题也不少啊。
5 楼 tomqyp 2009-06-17  
谢谢~

这么说你是在tango下编译的喽。
我看了下pyd的tango版是自己实现的minNumArgs,虽然没有phobos的那么优雅,但是逻辑简单不容易受bug影响,可惜pyd依赖老版tango中一些已经移除的函数。
4 楼 ideage 2009-06-17  
我试验了,编译不了.

我好像没有用到std.bind.
3 楼 tomqyp 2009-06-17  
1.16、1.20、1.30我都试过都通过不了,等下试试1.31
你有没有改过std.bind呢?
之前没有注意过std.bind,细看了一下感觉应该是dmd的问题。
能不能帮忙看看这段代码你能通过编译吗?

import std.bind;

bool foo(int a, int b)
{
    return a < b;
}
void main()
{
    int ma = minNumArgs!(foo);
}
2 楼 ideage 2009-06-16  
pyd要用低版本的DMD,我用1.031.
1 楼 tomqyp 2009-06-16  
能不能介绍下pyd的使用。
我自己试了试,pyd中引用的std.bind总是不能通过编译。

相关推荐

Global site tag (gtag.js) - Google Analytics