Lisandro Dalcin wrote:
Sorry, forgot to attach...

On Thu, Oct 30, 2008 at 11:28 PM, Lisandro Dalcin <[EMAIL PROTECTED]> wrote:
If all you can accept some vile hackery, see the attached tarball ;-) .

Python recognizes a PKGNAME directory as a pakage if '__init__.py' is
there, if not, no way. BUT  if '__init__.so' is also there, it loads
'__init__.so' !!! But then the exported module init function in the
dynlib needs to be named initPKGNAME, and "PKGNAME" needs to be passed
to Py_InitModule()

So, Greg, here you have the rules if you want to implement it ;-).

PS: tried only in Py 2.5, this is surely undocumented, and probably it
is in fact some bugy code in CPython's  'import.c' .

I finally got around to playing with your example, and after working past a couple of issues, I got it to work for my software. For the record, I had to use the following __init__.h file:

---------------------------------------------------------------------
#if SIZEOF_SIZE_T != SIZEOF_INT
#  define Py_InitModule4_64(name,a,b,c,d) Py_InitModule4_64("Crux",a,b,c,d)
#else
#  define Py_InitModule4(name,a,b,c,d) Py_InitModule4("Crux",a,b,c,d)
#endif

#define init__init__(a) initCrux(a)
---------------------------------------------------------------------

After I got this working, I took a look at the generated __init__.c, and realized that there is no simple way to extend the approach to Python 3, since "__init__" is directly embedded in a data structure used for module initialization. The attached patch modifies Cython to handle the __init__ module name specially. This works correctly for my software. Does it look like a general solution?

Thanks,
Jason
Set module name for Spam/__init__.pyx to Spam.

diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py
--- a/Cython/Compiler/Symtab.py
+++ b/Cython/Compiler/Symtab.py
@@ -795,7 +795,12 @@
         self.parent_module = parent_module
         outer_scope = context.find_submodule("__builtin__")
         Scope.__init__(self, name, outer_scope, parent_module)
-        self.module_name = name
+        if name != "__init__":
+            self.module_name = name
+        else:
+            # Treat Spam/__init__.pyx specially, so that when Python loads
+            # Spam/__init__.so, initSpam() is defined.
+            self.module_name = parent_module.module_name
         self.context = context
         self.module_cname = Naming.module_cname
         self.module_dict_cname = Naming.moddict_cname
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to