初学者
crquan · ) · 2006-01-09
<< 爱因斯坦
|
首 页
|
几个常用的字符串操作函数 >>
明年要继续学习Python!
- [
Linux
Python
CPython
]
2005-12-31
又是年末了,想在最后的时间在BLOGBUS里贴一个Linux的TAG
马上速战速决!
终于照样写好了CPython Demo,明年要继续学习Python!
crq@ $ cat capi.c
#include <Python.h>
static PyObject *c_system(PyObject *self, PyObject *args)
{
const char *str;
if (!PyArg_ParseTuple(args, "s:must be a shell command", &str))
return NULL;
int ret = system(str);
return Py_BuildValue("i", ret);
}
static PyMethodDef methods[] = {
{ "system", c_system, METH_VARARGS, "system with all" },
{ },
};
void initcapi(void)
{
Py_InitModule("capi", methods);
}
crq@ $ gcc -Wall -fPIC -O2 -I/usr/include/python2.2 -c capi.c
crq@ $ ld -shared -o capi.so capi.o
crq@ $ python
Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
import capi
>>>
dir(capi)
['__doc__', '__file__', '__name__', 'system']
>>>
capi.system("uptime")
23:22:48 up 5 days, 8:02, 1 user, load average: 0.00, 0.00, 0.00
0
>>>
^D
Tags:
Linux
Python
CPython
crquan
发表于 23:22:35 |
引用 0
|
编辑
评论
原来这里也有人在研究python啊!这个玩意真的是不错
可惜的是函数不能重载,到今天我还觉得遗憾……
crquan 回复 kernel1983 说:
函数重载完全没问题,lib/SocketServer.py 绝对是个很好的例子!
(2006-01-09 15:08:22)
kernel1983
(
kanjia.net/
回复
]
发表评论
姓名
E-mail
地址
原始快照:web.archive.org