⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 00000005.htm

📁 一份很好的linux入门资料
💻 HTM
📖 第 1 页 / 共 3 页
字号:
	PyObject *args;
&nbsp;<BR>{
&nbsp;<BR>
&nbsp;<BR>	if&nbsp;(!PyArg_ParseTuple(args,&nbsp;&quot;&quot;))
&nbsp;<BR>		return&nbsp;NULL;
&nbsp;<BR>	Py_INCREF(Py_None);
&nbsp;<BR>	return&nbsp;Py_None;
&nbsp;<BR>}
&nbsp;<BR>
&nbsp;<BR>static&nbsp;char&nbsp;tortoise_turn__doc__[]&nbsp;=
&nbsp;<BR>&quot;&quot;
&nbsp;<BR>;
&nbsp;<BR>
&nbsp;<BR>static&nbsp;PyObject&nbsp;*
&nbsp;<BR>tortoise_turn(self,&nbsp;args)
&nbsp;<BR>	PyObject&nbsp;*self;	/*&nbsp;Not&nbsp;used&nbsp;*/
&nbsp;<BR>	PyObject&nbsp;*args;
&nbsp;<BR>{
&nbsp;<BR>
&nbsp;<BR>	if&nbsp;(!PyArg_ParseTuple(args,&nbsp;&quot;&quot;))
&nbsp;<BR>		return&nbsp;NULL;
&nbsp;<BR>	Py_INCREF(Py_None);
&nbsp;<BR>	return&nbsp;Py_None;
&nbsp;<BR>}
&nbsp;<BR>
&nbsp;<BR>static&nbsp;char&nbsp;tortoise_move__doc__[]&nbsp;=
&nbsp;<BR>&quot;&quot;
&nbsp;<BR>;
&nbsp;<BR>
&nbsp;<BR>static&nbsp;PyObject&nbsp;*
&nbsp;<BR>tortoise_move(self,&nbsp;args)
&nbsp;<BR>	PyObject&nbsp;*self;	/*&nbsp;Not&nbsp;used&nbsp;*/
&nbsp;<BR>	PyObject&nbsp;*args;
&nbsp;<BR>{
&nbsp;<BR>
&nbsp;<BR>	if&nbsp;(!PyArg_ParseTuple(args,&nbsp;&quot;&quot;))
&nbsp;<BR>		return&nbsp;NULL;
&nbsp;<BR>	Py_INCREF(Py_None);
&nbsp;<BR>	return&nbsp;Py_None;
&nbsp;<BR>}
&nbsp;<BR>
&nbsp;<BR>/*&nbsp;List&nbsp;of&nbsp;methods&nbsp;defined&nbsp;in&nbsp;the&nbsp;module&nbsp;*/
&nbsp;<BR>/*&nbsp;模块中所有方法的列表,这是一个扩展模块的关键数据结构
&nbsp;<BR>&nbsp;&nbsp;&nbsp;例如当你在python里调用tortoise.pendown()方法时,
&nbsp;<BR>&nbsp;&nbsp;&nbsp;python解释器会来查这个表,找到对应的函数tortoise_pendown()
&nbsp;<BR>*/
&nbsp;<BR>
&nbsp;<BR>static&nbsp;struct&nbsp;PyMethodDef&nbsp;tortoise_methods[]&nbsp;=&nbsp;{
&nbsp;<BR>&nbsp;{&quot;reset&quot;,	(PyCFunction)tortoise_reset,	METH_VARARGS,	tortoise_reset__doc__},
&nbsp;<BR>&nbsp;<BR>&nbsp;{&quot;pendown&quot;,	(PyCFunction)tortoise_pendown,	METH_VARARGS,	tortoise_pendown__do&nbsp;<BR>c__},
&nbsp;<BR>&nbsp;{&quot;penup&quot;,	(PyCFunction)tortoise_penup,	METH_VARARGS,	tortoise_penup__doc__},
&nbsp;<BR>&nbsp;<BR>&nbsp;{&quot;turn&quot;,	(PyCFunction)tortoise_turn,	METH_VARARGS,	tortoise_turn__doc__},
&nbsp;<BR>&nbsp;{&quot;move&quot;,	(PyCFunction)tortoise_move,	METH_VARARGS,	tortoise_move__doc__},
&nbsp;<BR>&nbsp;
&nbsp;<BR>&nbsp;{NULL,	&nbsp;(PyCFunction)NULL,&nbsp;0,&nbsp;NULL}		/*&nbsp;sentinel&nbsp;*/
&nbsp;<BR>};
&nbsp;<BR>
&nbsp;<BR>
&nbsp;<BR>/*&nbsp;Initialization&nbsp;function&nbsp;for&nbsp;the&nbsp;module&nbsp;(*must*&nbsp;be&nbsp;called&nbsp;inittortoise)&nbsp;*/
&nbsp;<BR>
&nbsp;<BR>static&nbsp;char&nbsp;tortoise_module_documentation[]&nbsp;=&nbsp;
&nbsp;<BR>&quot;&quot;
&nbsp;<BR>;
&nbsp;<BR>
&nbsp;<BR>/*&nbsp;每个模块都要有这样一个初始化函数,
&nbsp;<BR>&nbsp;&nbsp;&nbsp;在import这个模块的时候python会自动执行这个函数.
&nbsp;<BR>&nbsp;&nbsp;&nbsp;函数命名必须是&nbsp;init&lt;modulename&gt;&nbsp;
&nbsp;<BR>*/
&nbsp;<BR>void
&nbsp;<BR>inittortoise()
&nbsp;<BR>{
&nbsp;<BR>	PyObject&nbsp;*m,&nbsp;*d;
&nbsp;<BR>
&nbsp;<BR>	/*&nbsp;Create&nbsp;the&nbsp;module&nbsp;and&nbsp;add&nbsp;the&nbsp;functions&nbsp;*/
&nbsp;<BR>	/*&nbsp;增加tortoise这个模块&nbsp;*/
&nbsp;<BR>	m&nbsp;=&nbsp;Py_InitModule4(&quot;tortoise&quot;,&nbsp;tortoise_methods,
&nbsp;<BR>		tortoise_module_documentation,
&nbsp;<BR>		(PyObject*)NULL,PYTHON_API_VERSION);
&nbsp;<BR>
&nbsp;<BR>	/*&nbsp;Add&nbsp;some&nbsp;symbolic&nbsp;constants&nbsp;to&nbsp;the&nbsp;module&nbsp;*/
&nbsp;<BR>	/*&nbsp;得到此模块的名字空间&nbsp;*/
&nbsp;<BR>	d&nbsp;=&nbsp;PyModule_GetDict(m);
&nbsp;<BR>	/*&nbsp;添加一个错误对象的名字&nbsp;*/
&nbsp;<BR>	ErrorObject&nbsp;=&nbsp;PyString_FromString(&quot;tortoise.error&quot;);
&nbsp;<BR>	PyDict_SetItemString(d,&nbsp;&quot;error&quot;,&nbsp;ErrorObject);
&nbsp;<BR>
&nbsp;<BR>	/*&nbsp;XXXX&nbsp;Add&nbsp;constants&nbsp;here&nbsp;*/
&nbsp;<BR>	/*&nbsp;在这里可以加入模块的常量&nbsp;*/
&nbsp;<BR>
&nbsp;<BR>	/*&nbsp;Check&nbsp;for&nbsp;errors&nbsp;*/
&nbsp;<BR>	if&nbsp;(PyErr_Occurred())
&nbsp;<BR>		Py_FatalError(&quot;can't&nbsp;initialize&nbsp;module&nbsp;tortoise&quot;);
&nbsp;<BR>}
&nbsp;<BR>
&nbsp;<BR>/*------------------------------------------------------------------------*/
&nbsp;<BR>
&nbsp;<BR>	这个文件其实就可以编译了,只不过没有任何功能罢了,呵呵.我们去Python源程序的&nbsp;<BR>Demo/extend目录,把make_shared拷贝一份叫my_make_shared,然后把my_make_shared里面&nbsp;<BR>的xxmodule都改成我们的tortoisemodule.如果上面那个tortoisemodule.c不在&nbsp;<BR>Tools/modulator目录下,还需要修改第10行的路径指向tortoisemodule.c的目录.最后,再&nbsp;<BR>加上编译X程序需要的包含库.呵呵,说起来挺罗嗦的,看看我的my_make_shared文件吧:
&nbsp;<BR>
&nbsp;<BR>
&nbsp;<BR>#!&nbsp;/bin/sh
&nbsp;<BR>
&nbsp;<BR>#&nbsp;This&nbsp;script&nbsp;tests&nbsp;and&nbsp;demonstrates&nbsp;the&nbsp;mechanism&nbsp;for&nbsp;building&nbsp;a
&nbsp;<BR>#&nbsp;shared&nbsp;library&nbsp;for&nbsp;an&nbsp;additional&nbsp;extension&nbsp;module&nbsp;using&nbsp;the
&nbsp;<BR>#&nbsp;generic&nbsp;Makefile.pre.in&nbsp;from&nbsp;the&nbsp;Misc&nbsp;directory.
&nbsp;<BR>
&nbsp;<BR>./make_clean
&nbsp;<BR>
&nbsp;<BR>cp&nbsp;../../Misc/Makefile.pre.in&nbsp;.
&nbsp;<BR>cp&nbsp;/home/kafa/guile/tortoisemodule.c&nbsp;.
&nbsp;<BR>echo&nbsp;'*shared*'&nbsp;&gt;Setup.in
&nbsp;<BR>echo&nbsp;tortoise&nbsp;tortoisemodule.c&nbsp;-I/usr/X11R6/include&nbsp;-lX11&nbsp;-L/usr/X11R6/lib&nbsp;&nbsp;<BR>-lm&nbsp;&gt;&gt;Setup.in
&nbsp;<BR>
&nbsp;<BR>make&nbsp;-f&nbsp;Makefile.pre.in&nbsp;boot
&nbsp;<BR>make&nbsp;Makefile
&nbsp;<BR>make
&nbsp;<BR>
&nbsp;<BR>######################################################################
&nbsp;<BR>
&nbsp;<BR>	然后我们运行my_make_shared就可以在此目录下生成一个叫tortoisemodule.so的动态联&nbsp;<BR>接库文件,这就是我们需要的python扩展模块.现在我们就可以在此目录下试试这个扩展模&nbsp;<BR>块了,运行命令'python'起动python的交互式解释器,然后执行'import&nbsp;tortoise'导入&nbsp;<BR>tortoise模块.如果没有错误发生的话,现在你可以用'dir(tortoise)'看看tortoise模块&nbsp;<BR>里是不是包含了我们要的那些函数,也可以用'tortoise.pendown()'命令调用一下这些方&nbsp;<BR>法看看有什么结果.
&nbsp;<BR>
&nbsp;<BR>	OK,现在tortoise的基本程序和扩展模块的骨架都已经有了,让我们把它们合在一起,就在&nbsp;<BR>那个tortoisemodule.c上面改吧,先把tortoise1.c里的头文件和定义的宏copy进来,然后&nbsp;<BR>把main()函数里面初始化X和工作窗口的代码放到一个函数里,就叫init_workspace()吧,&nbsp;<BR>然后就是真正有意义的工作--把模块函数加入真正的代码实现我们需要的功能,就以&nbsp;<BR>tortoise_turn()为例:
&nbsp;<BR>
&nbsp;<BR>static&nbsp;PyObject&nbsp;*
&nbsp;<BR>tortoise_turn(self,&nbsp;args)
&nbsp;<BR>	PyObject&nbsp;*self;	/*&nbsp;Not&nbsp;used&nbsp;*/
&nbsp;<BR>	PyObject&nbsp;*args;
&nbsp;<BR>{
&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;定义一个整型变量用来放真正传进来的degrees参数&nbsp;*/
&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;degrees;
&nbsp;<BR>	
&nbsp;<BR>	/*&nbsp;从python对象里提取出一个整型参数付给degrees
&nbsp;<BR>	&nbsp;&nbsp;&nbsp;PyArg_ParseTuple()函数里的&quot;i&quot;表示int类型&nbsp;*/		
&nbsp;<BR>	if&nbsp;(!PyArg_ParseTuple(args,&nbsp;&quot;i&quot;,&nbsp;&amp;degrees))
&nbsp;<BR>		return&nbsp;NULL;
&nbsp;<BR>
&nbsp;<BR>	/*&nbsp;真正的功能代码&nbsp;*/
&nbsp;<BR>	currentDirection&nbsp;+=&nbsp;(double)degrees;
&nbsp;<BR>
&nbsp;<BR>	/*&nbsp;没有错误发生,正常返回&nbsp;*/
&nbsp;<BR>	Py_INCREF(Py_None);
&nbsp;<BR>	return&nbsp;Py_None;
&nbsp;<BR>}
&nbsp;<BR>	
&nbsp;<BR>	很简单吧?呵呵,依样画葫芦修改其它几个函数,这个扩展模块就完成了.我的改过的&nbsp;<BR>tortoisemodule.c文件内容如下:
&nbsp;<BR>
&nbsp;<BR>/*&nbsp;-------------------------------------------------------&nbsp;*/
&nbsp;<BR>/*&nbsp;tortoisemodule.c&nbsp;*/
&nbsp;<BR>
&nbsp;<BR>#include&nbsp;&lt;unistd.h&gt;
&nbsp;<BR>#include&nbsp;&lt;math.h&gt;
&nbsp;<BR>#include&nbsp;&lt;X11/Xlib.h&gt;
&nbsp;<BR>
&nbsp;<BR>#define&nbsp;WINDOW_SIZE&nbsp;500
&nbsp;<BR>#define&nbsp;DEGREES_TO_RADIANS&nbsp;&nbsp;(3.1415926535897932384626433832795029L/180.0)
&nbsp;<BR>
&nbsp;<BR>Display&nbsp;*theDisplay;
&nbsp;<BR>Window&nbsp;theWindow;
&nbsp;<BR>Screen&nbsp;*theScreen;
&nbsp;<BR>GC&nbsp;theGC;
&nbsp;<BR>double&nbsp;currentX;
&nbsp;<BR>double&nbsp;currentY;
&nbsp;<BR>double&nbsp;currentDirection;
&nbsp;<BR>int&nbsp;penDown;
&nbsp;<BR>
&nbsp;<BR>#include&nbsp;&quot;Python.h&quot;
&nbsp;<BR>static&nbsp;PyObject&nbsp;*ErrorObject;
&nbsp;<BR>
&nbsp;<BR>/*&nbsp;-----------------------------------------------------&nbsp;*/
&nbsp;<BR>static&nbsp;void
&nbsp;<BR>init_workspace(void)
&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sp;&nbsp;&nbsp;&nbsp;&nbsp;BlackPixelOfScreen(theScreen),&nbsp;
&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WhitePixelOfScreen(theScreen));
&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;theGC&nbsp;=&nbsp;XCreateGC(theDisplay,&nbsp;theWindow,&nbsp;0L,&nbsp;NULL);
&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;XSetForeground(theDisplay,&nbsp;theGC,&nbsp;BlackPixelOfScreen(theScreen));
&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;XMapWindow(theDisplay,theWindow);&nbsp;&nbsp;
&nbsp;<BR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -