1

I am trying to build a cython module that uses the bisect module. When compiling with python-2 the import works flawlessly, but when compiled for python-3 I get a strange recursion error. Here is an example test and setup script:

## test_module.pyx
import bisect

def primes(int kmax):
    cdef int n, k, i
    cdef int p[1000]
    result = []
    if kmax > 1000:
        kmax = 1000
    k = 0
    n = 2
    while k < kmax:
        i = 0
        while i < k and n % p[i] != 0:
            i = i + 1
        if i == k:
            p[k] = n
            k = k + 1
            result.append(n)
        n = n + 1
    return result

setup script

## setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy as np

extensions = [
    Extension("test_module", ["test_module.pyx"])
]

setup(
    ext_modules=cythonize(extensions),
    include_dirs = [np.get_include()],
)

build instructions

$ python3 setup.py build_ext --inplace
$ python3
>>> import test_module
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.pyx", line 1, in init sph2sed.test
    import bisect
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 951, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 894, in _find_spec
  File "<frozen importlib._bootstrap_external>", line 1157, in find_spec
  File "<frozen importlib._bootstrap_external>", line 1129, in _get_spec
  File "<frozen importlib._bootstrap_external>", line 1271, in find_spec
  File "<frozen importlib._bootstrap_external>", line 96, in _path_isfile
  File "<frozen importlib._bootstrap_external>", line 88, in _path_is_mode_type
RecursionError: maximum recursion depth exceeded
>>> exit()

I have create a new python3 environment with conda, the list of installed packages is:

$ conda list
# packages in environment at /home/chris/anaconda/envs/sph2sed:
#
ca-certificates           2017.08.26           h1d4fec5_0  
certifi                   2017.11.5        py36hf29ccca_0  
Cython                    0.27.3                    <pip>
decorator                 4.1.2            py36hd076ac8_0  
intel-openmp              2018.0.0             hc7b2577_8  
ipython                   6.2.1            py36h88c514a_1  
ipython_genutils          0.2.0            py36hb52b0d5_0  
jedi                      0.11.0                   py36_2  
libedit                   3.1                  heed3624_0  
libffi                    3.2.1                hd88cf55_4  
libgcc-ng                 7.2.0                h7cc24e2_2  
libstdcxx-ng              7.2.0                h7a57d05_2  
mkl                       2018.0.1             h19d6760_4  
ncurses                   6.0                  h9df7e31_2  
numpy                     1.13.3           py36ha12f23b_0  
openssl                   1.0.2n               hb7f436b_0  
parso                     0.1.1            py36h35f843b_0  
pexpect                   4.3.0            py36h673ed17_0  
pickleshare               0.7.4            py36h63277f8_0  
pip                       9.0.1            py36h6c6f9ce_4  
prompt_toolkit            1.0.15           py36h17d85b1_0  
ptyprocess                0.5.2            py36h69acd42_0  
pygments                  2.2.0            py36h0d3125c_0  
python                    3.6.3                h6c0c0dc_5  
readline                  7.0                  ha6073c6_4  
setuptools                36.5.0           py36he42e2e1_0  
simplegeneric             0.8.1            py36h2cb9092_0  
six                       1.11.0           py36h372c433_1  
sph2sed                   0.1                       <pip>
sqlite                    3.20.1               hb898158_2  
tk                        8.6.7                hc745277_3  
traitlets                 4.3.2            py36h674d592_0  
UNKNOWN                   0.0.0                     <pip>
wcwidth                   0.1.7            py36hdf4376a_0  
wheel                     0.30.0           py36hfd4bba0_1  
xz                        5.2.3                h55aa19d_2  
zlib                      1.2.11               ha838bed_2  
christopherlovell
  • 3,800
  • 4
  • 19
  • 26
  • What happens if you do `import bisect` in a (non-Cython) Python 3 module? – DavidW Dec 21 '17 at 17:44
  • Also, can you name your module something other than test, simply because there's a built-in module called test, and it sometimes causes problems (I don't think that's the case here, but it's worth checking) – DavidW Dec 21 '17 at 17:45
  • Imports and works fine, the error only occurs when it is compiled into a cython module. Have updated with a new name - same error – christopherlovell Dec 21 '17 at 17:46
  • Final small clarifying suggestion: I think you need to build with Python3 to get a module that runs with Python3: `python3 setup.py build_ext --inplace`. I suspect you've actually done this right but it's better to be sure – DavidW Dec 21 '17 at 17:50
  • @DavidW good shout, but still getting an identical error after explicitly building with python3 – christopherlovell Dec 21 '17 at 17:53
  • I'm afraid I don't have any other ideas right now - it works fine for me which makes it pretty hard to debug. – DavidW Dec 21 '17 at 17:54
  • Using the same conda environment? – christopherlovell Dec 21 '17 at 18:00
  • 1
    No. Using Python 3.6.3 and Cython 0.26.1 installed through a package manager on Linux. I don't have conda installed so I can't easily test the exact same setup – DavidW Dec 21 '17 at 18:12

0 Answers0