Skip to content

[BUG] namespace packages as in pep 420 cannot be cimported #5335

@tornaria

Description

@tornaria

Describe the bug

When PACKAGE is a namespace package, then both:

cimport PACKAGE

or

from PACKAGE cimport MODULE

fail at compilation, complaining that PACKAGE.pxd can't be found.

Presumably cython has looked for PACKAGE/__init__.* and PACKAGE.* which are not available for a namespace package.

This was discovered as part of sagemath/sage#35322.

Code to reproduce the behaviour:

  1. Patch the pep420 test in master as follows:
--- a/tests/build/cythonize_pep420_namespace.srctree
+++ b/tests/build/cythonize_pep420_namespace.srctree
@@ -31,9 +31,17 @@ cdef class A:
 
 ######## nsp/m2/b.pyx ########
 
+from nsp.m1 cimport a
 from nsp.m1.a cimport A
+from nsp.m3.c cimport d   ### BROKEN
 from nsp.m3.c.d cimport D
 
+cimport nsp.m1
+cimport nsp.m1.a
+cimport nsp.m3
+cimport nsp.m3.c  ### BROKEN
+cimport nsp.m3.c.d
+
 cdef class B(A):
     pass
 

I added several new tests for the different ways one can cimport modules. All of them work ok, except the two I've marked BROKEN which corresponds to the ones mentioned in the description.

  1. Build and run ./runtests.py cythonize_pep420_namespace, to get:
Error compiling Cython file:
------------------------------------------------------------
...

from nsp.m1 cimport a
from nsp.m1.a cimport A
from nsp.m3.c cimport d
^
------------------------------------------------------------

nsp/m2/b.pyx:4:0: 'nsp/m3/c.pxd' not found
  1. Comment out the first line marked BROKEN above and repeat the test to get:
Error compiling Cython file:
------------------------------------------------------------
...
from nsp.m3.c.d cimport D

cimport nsp.m1
cimport nsp.m1.a
cimport nsp.m3
cimport nsp.m3.c
        ^
------------------------------------------------------------

nsp/m2/b.pyx:10:8: 'nsp/m3/c.pxd' not found
  1. Comment out the second line marked BROKEN, now all tests pass.

Expected behaviour

All tests pass, including the two marked BROKEN.

OS

linux

Python version

3.11.2

Cython version

3.0.0b1+master (2ea4f96)

Additional context

In sagemath we still use 0.29.33, with #2946 backported (see https://github.com/sagemath/sage/blob/develop/build/pkgs/cython/patches/4918.patch) so other uses of namespace packages work ok.

Whatever is the solution, it'd be nice to have it backported to 0.29.x.

It would also be wonderful if 0.29.34 could include #2946.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy