[Python] Cython build with bisect fails for python 3

Discussão em 'Python' iniciado por Stack, Maio 13, 2025.

  1. Stack

    Stack Membro Participativo

    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 != 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

    Continue reading...

Compartilhe esta Página