Skip to content

Conversation

Copy link

Copilot AI commented Sep 18, 2025

This PR fixes compilation failures on Python 3.12 by updating the Cython template to use modern function signatures compatible with newer Cython versions.

Problem

The current build fails on Python 3.12 with errors like:

Cannot assign type 'int (const_void *, const_void *) except? -1 nogil' to 'int (*)(const_void *, const_void *) noexcept nogil'. Exception values are incompatible. Suggest adding 'noexcept' to the type of 'int_compare'.

This happens because modern Cython versions (3.1+) are stricter about exception handling in nogil functions and generate Python 3.12-compatible C code that expects noexcept qualifiers.

Solution

Updated three comparison functions in lightfm/_lightfm_fast.pyx.template to include the noexcept qualifier:

  • reverse_pair_compare
  • int_compare
  • flt_compare

These functions are used with qsort() and bsearch() from the C standard library, which expect non-throwing comparison functions.

Changes

-cdef int reverse_pair_compare(const_void *a, const_void *b) nogil:
+cdef int reverse_pair_compare(const_void *a, const_void *b) noexcept nogil:

-cdef int int_compare(const_void *a, const_void *b) nogil:
+cdef int int_compare(const_void *a, const_void *b) noexcept nogil:

-cdef int flt_compare(const_void *a, const_void *b) nogil:
+cdef int flt_compare(const_void *a, const_void *b) noexcept nogil:

Testing

  • ✅ Compiles successfully with Python 3.12.3 and Cython 3.1.4
  • ✅ All core functionality works (import, model creation)
  • ✅ Maintains backward compatibility with earlier Python versions
  • ✅ Generated wheel installs and runs correctly

Impact

This minimal change enables LightFM to build and run on Python 3.12 environments, including modern Databricks clusters, without requiring users to downgrade their Python runtime.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Act as a senior software engineer specializing in Python, Cython, and C. I need to create a Python wheel for the lightfm library that is compatible with Python 3.12 on a Linux (x86_64) environment. My production environment is Databricks, and I am un... Fix Python 3.12 compatibility by updating Cython template function signatures Sep 18, 2025
Copilot AI requested a review from pedrodicati September 18, 2025 02:19
@pedrodicati pedrodicati requested a review from Copilot September 18, 2025 02:21
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes Python 3.12 compatibility issues by adding noexcept qualifiers to Cython function signatures. The changes address compilation failures caused by stricter exception handling requirements in modern Cython versions when generating Python 3.12-compatible code.

  • Updates three comparison function signatures to include noexcept qualifier
  • Maintains backward compatibility while enabling builds on Python 3.12
  • Resolves Cython compilation errors related to exception value incompatibilities

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@pedrodicati pedrodicati marked this pull request as ready for review September 18, 2025 02:22
@pedrodicati pedrodicati merged commit fbaaa0f into master Sep 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants