[Pyrex] C-API implementation in Pyrex 0.9.6

Stefan Behnel stefan_ml at behnel.de
Wed Oct 17 08:58:18 CEST 2007


Stefan Behnel wrote:
> Stefan Behnel wrote:
>>> I forgot to mention that this uses a package local import, so the first lines
>>> of lxml/objectify.pyx look like this:
>>>
>>>     from etree cimport _Document, _Element, ElementBase
>>>     from etree cimport _ElementIterator, ElementClassLookup
>>>     from etree cimport elementFactory, textOf
>>>     cimport etree as cetree
>> I think the problem here is that __Pyx_ImportModule() does not take the
>> current package into account. I'll fix it to first try an import based on the
>> package of the current module and then try a global import.
> 
> I also think that Pyrex should not re-import a module for each type it wants
> to cimport from it, but rather import each required module once, store it in a
> global variable, and reuse it whenever it is accessed by generated code. I'll
> see if I can fix this while I'm at it.

Here is a patch that fixes the imports for me.

Stefan

PS: Note that it requires the fully qualified module name to be extracted from
distutils, which Cython currently does like this (sorry, I don't have a clean
diff for that):

Distutils/build_ext.py:
@@ -167,6 +167,8 @@
         if not pyrex_sources:
             return new_sources

+        module_name = extension.name
+
         for source in pyrex_sources:
             target = pyrex_targets[source]
             source_time = os.stat(source).st_mtime
@@ -184,6 +186,7 @@
                     output_file = target,
                     cplus = cplus,
                     generate_pxi = pyrex_gen_pxi)
-                result = pyrex_compile(source, options=options)
+                result = cython_compile(source, options=options,
+                                        full_module_name=module_name)

         return new_sources

And hands it to the compiler like this:

--- a/Compiler/Main.py  Wed Aug 29 14:54:29 2007 -0700
+++ b/Compiler/Main.py  Mon Jul 30 22:18:23 2007 +0200
@@ -153,7 +153,7 @@ class Context:
         name, _ = os.path.splitext(tail)
         return name

-    def compile(self, source, options = None):
+    def compile(self, source, options = None, full_module_name = None):
         # Compile a Pyrex implementation file in this context
         # and return a CompilationResult.
         if not options:
@@ -161,7 +161,8 @@ class Context:
         result = CompilationResult()
         cwd = os.getcwd()

-        full_module_name, _ = os.path.splitext(source.replace('/', '.'))
+        if full_module_name is None:
+            full_module_name, _ = os.path.splitext(source.replace('/', '.'))

         source = os.path.join(cwd, source)

@@ -263,7 +264,8 @@ class CompilationResult:
         self.extension_file = None


-def compile(source, options = None, c_compile = 0, c_link = 0):
+def compile(source, options = None, c_compile = 0, c_link = 0,
+            full_module_name = None):
     """
     compile(source, options = default_options)

@@ -278,7 +280,7 @@ def compile(source, options = None, c_co
     if c_link:
         options.obj_only = 0
     context = Context(options.include_path)
-    return context.compile(source, options)
+    return context.compile(source, options, full_module_name)

 #------------------------------------------------------------------------
 #
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cython-module-import.patch
Type: text/x-diff
Size: 8409 bytes
Desc: not available
Url : http://lists.copyleft.no/pipermail/pyrex/attachments/20071017/26c38136/attachment.bin 


More information about the Pyrex mailing list