[PyKDE] sip modules as part of packages

Andreas Gerstlauer gerstl at ics.uci.edu
Tue Nov 13 06:53:00 GMT 2001


Ok, I found where the problem is... When looking for the module
dictionary, the sipRegisterModule() function assumes that the module
name is "module" whereas it could actually be "package.module".

I created a patch for sip which fixes that by passing the real
module name ("__name__") through an optional argument to
sipInitModule()/sipRegisterModule() for lookup of the corresponding 
dictionary (defaults to hardcoded module name if no argument is
passed). See attachement.

In the patch, I am using the passed module name only for the
lookup in sipRegisterModule(). I don't know what implications
updating the md_name in the sipModule data structure would
have (i.e. what else that is used for). Possibly that should 
get updated, too?

Andreas


> I am having trouble using a sip-generated module as part
> of a Python package. For example, if I would be so inclined
> as to put the "qt.py" and "libqtcmodule.so" files into some
> package subdirectory "tst":
>   % mkdir tst
>   % cp $PYTHONMODULES/qt.py $PYTHONMODULES/libqtcmodule.so tst
>   % touch tst/__init__.py
>   % python
>   Python 2.0 (#10, Feb 13 2001, 13:25:47) 
>   [GCC 2.95.2 19991024 (release)] on sunos5
>   Type "copyright", "credits" or "license" for more information.
>   >>> from tst import qt
>   Traceback (most recent call last):
>     File "<stdin>", line 1, in ?
>     File "/opt/contrib/python-2.0/lib/python2.0/site-packages/qt.py", line 46, 
> in ?
>       libqtc.sipInitModule()
>   SystemError: Unable to find module dictionary for qt
>   >>> 
-------------- next part --------------
Index: sip/gencode.c
===================================================================
RCS file: /public/sip/sip/gencode.c,v
retrieving revision 1.54
diff -u -r1.54 gencode.c
--- sip/gencode.c	2001/11/11 19:05:36	1.54
+++ sip/gencode.c	2001/11/13 05:32:01
@@ -484,7 +484,7 @@
 
 	prcode(fp,
 "\n"
-"lib%sc.sipInitModule()\n"
+"lib%sc.sipInitModule(__name__)\n"
 		,mname);
 
 	/* Generate the pre-Python code blocks. */
@@ -1463,9 +1463,9 @@
 "\n"
 "// Initialise the module.\n"
 "\n"
-"static PyObject *initModule(PyObject *,PyObject *)\n"
+"static PyObject *initModule(PyObject *,PyObject *args)\n"
 "{\n"
-"	if (sipRegisterModule(&sipModule) < 0)\n"
+"	if (sipRegisterModule(&sipModule,args) < 0)\n"
 "		return NULL;\n"
 		);
 
Index: siplib/sip.h
===================================================================
RCS file: /public/sip/siplib/sip.h,v
retrieving revision 1.14
diff -u -r1.14 sip.h
--- siplib/sip.h	2001/09/28 23:11:44	1.14
+++ siplib/sip.h	2001/11/13 05:32:01
@@ -297,7 +297,7 @@
 #define	sipIsMethod(m)		((m) -> mcflags & SIP_MC_ISMETH)
 #define	sipSetIsMethod(m)	((m) -> mcflags |= SIP_MC_ISMETH)
 
-extern SIP_EXTERN int sipRegisterModule Py_PROTO((sipModuleDef *));
+extern SIP_EXTERN int sipRegisterModule Py_PROTO((sipModuleDef *,PyObject *));
 extern SIP_EXTERN int sipRegisterClasses Py_PROTO((sipModuleDef *,int));
 extern SIP_EXTERN void sipSaveMethod Py_PROTO((sipPyMethod *,PyObject *));
 extern SIP_EXTERN void sipCommonCtor Py_PROTO((sipMethodCache *,int));
Index: siplib/siplib.c
===================================================================
RCS file: /public/sip/siplib/siplib.c,v
retrieving revision 1.26
diff -u -r1.26 siplib.c
--- siplib/siplib.c	2001/10/28 10:32:48	1.26
+++ siplib/siplib.c	2001/11/13 05:32:01
@@ -68,10 +68,15 @@
  * Register a module.
  */
 
-int sipRegisterModule(sipModuleDef *sm)
+int sipRegisterModule(sipModuleDef *sm, PyObject* args)
 {
+        char *name = 0;
 	PyObject *dictofmods, *mod;
 
+	if (!PyArg_ParseTuple(args,"|z",&name))
+		return -1;
+        if (!name) name = sm -> md_name;  
+  
 	if ((dictofmods = PyImport_GetModuleDict()) == NULL)
 		return -1;
 
@@ -133,10 +138,10 @@
 
 	/* Get the module dictionary. */
 
-	if ((mod = PyDict_GetItemString(dictofmods,sm -> md_name)) == NULL ||
+	if ((mod = PyDict_GetItemString(dictofmods,name)) == NULL ||
 	    (sm -> md_dict = PyModule_GetDict(mod)) == NULL)
 	{
-		PyErr_Format(PyExc_SystemError,"Unable to find module dictionary for %s",sm -> md_name);
+		PyErr_Format(PyExc_SystemError,"Unable to find module dictionary for %s",name);
 
 		return -1;
 	}


More information about the PyQt mailing list