Skip to content

[bitset_base.pxd] Use integer division #34768

@alexhutman

Description

@alexhutman

Change / to !// in src/sage/data_structures/bitset_base.pxd (wasn't sure how to add this in the ticket, feel free to modify ticket or let me know how) to force integer division in Python3. This change is backwards compatible as !// designates integer division in Python2 as well.


I was having trouble compiling a module that I was working on that depends on bitsets. When compiling using the

compiler_directives={'language_level' : "3"}

option in my setup.py, I was getting the following errors:

Error compiling Cython file:
------------------------------------------------------------
...

    cdef mp_bitcnt_t extra

    bits.size = size
    if fused_bitset_t is bitset_t:
        bits.limbs = (size - 1) / (8 * LIMB_SIZE) + 1
                                                 ^
------------------------------------------------------------

/mnt/e/projects/python/sage/src/sage/data_structures/bitset_base.pxd:177:50: Cannot assign type 'double' to 'mp_size_t'

Error compiling Cython file:
------------------------------------------------------------
...
    bits.size = size
    if fused_bitset_t is bitset_t:
        bits.limbs = (size - 1) / (8 * LIMB_SIZE) + 1
        bits.bits = <mp_limb_t*>check_calloc(bits.limbs, LIMB_SIZE)
    else:
        bits.limbs = ((size - 1) / (8*ALIGNMENT) + 1) * (ALIGNMENT/LIMB_SIZE)
                                                     ^
------------------------------------------------------------

/mnt/e/projects/python/sage/src/sage/data_structures/bitset_base.pxd:180:54: Cannot assign type 'double' to 'mp_size_t'

Error compiling Cython file:
------------------------------------------------------------
...
        bits.limbs = ((size - 1) / (8*ALIGNMENT) + 1) * (ALIGNMENT/LIMB_SIZE)
        extra = (ALIGNMENT + LIMB_SIZE - 2) // LIMB_SIZE
        bits.mem = check_calloc(bits.limbs + extra, LIMB_SIZE)
        bits.bits = <mp_limb_t*> align(bits.mem, ALIGNMENT)
        bits.non_zero_chunks_are_initialized = False
        bits.non_zero_chunks = <mp_bitcnt_t*> check_allocarray((bits.limbs*LIMB_SIZE) / ALIGNMENT, sizeof(mp_bitcnt_t))
                                                                                     ^
------------------------------------------------------------

/mnt/e/projects/python/sage/src/sage/data_structures/bitset_base.pxd:185:86: Cannot assign type 'double' to 'size_t'

Error compiling Cython file:
------------------------------------------------------------
...
    if size_old == size:
        return 0
    if size <= 0:
        raise ValueError("bitset capacity must be greater than 0")

    cdef mp_size_t limbs_new = (size - 1) / (8 * LIMB_SIZE) + 1
                                                           ^
------------------------------------------------------------

/mnt/e/projects/python/sage/src/sage/data_structures/bitset_base.pxd:206:60: Cannot assign type 'double' to 'mp_size_t'
Compiling zeroforcing/fastqueue.pyx because it changed.
Compiling zeroforcing/metagraph.pyx because it changed.
[1/2] Cythonizing zeroforcing/fastqueue.pyx
[2/2] Cythonizing zeroforcing/metagraph.pyx
Traceback (most recent call last):
  File "/mnt/e/projects/python/Zero-Forcing-Number/setup.py", line 21, in <module>
    ext_modules=cythonize(
  File "/mnt/e/projects/python/sage/local/var/lib/sage/venv-python3.10.5/lib/python3.10/site-packages/Cython/Build/Dependencies.py", line 1127, in cythonize
    cythonize_one(*args)
  File "/mnt/e/projects/python/sage/local/var/lib/sage/venv-python3.10.5/lib/python3.10/site-packages/Cython/Build/Dependencies.py", line 1250, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: zeroforcing/metagraph.pyx

I then tried compiling using the 'language_level' : "2" directive in my setup.py. It worked fine with this. When modifying bitset_base.pxd to contain the proposed changes, this error disappeared using the originally-tried 'language_level' : "3" directive, and I also made sure that it was backwards compatible by testing it with 'language_level' : "2".


Thank you to Julian Rüth and Robert Bradshaw for their help with this issue via Zulip and email, respectively.

Upstream: None of the above - read trac for reasoning.

Component: cython

Author: Alex Hutman

Issue created by migration from https://trac.sagemath.org/ticket/34768

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions