Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Cythonized API Classes #2029

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

HJJ256
Copy link

@HJJ256 HJJ256 commented Apr 9, 2024

What kind of change does this PR introduce?

  • bug fix
  • feature
  • docs update
  • tests/coverage improvement
  • refactoring
  • other

What is the related issue number (starting with #)
#1814

What is the current behavior? (You can also link to an open issue here)
Currently, when an API class is cythonized, the methods for the class do not pass the decoratable_types check. The issue #1814 was raised 5 years ago and is still relevant.

What is the new behavior (if this is a feature change)?
Cythonized API class methods are getting mounted in cherrypy.tree and the API runs successfully.

Other information:
I tried the approach suggested by @webknjaz to put the types.BuiltinFunctionType in the decoratable_types, however, not all cythonized methods are of types.BuiltinFunctionType. The one thing these functions have in common is the "func_code" attribute.

As for the testing of this change, I am still looking into it. Currently, I can only come up with one way where I create a .whl file and install the cythonized API as a package, then import from the package and test if CherryPy is working, but this way does not seem very efficient.

At worst, the hasattr check may fail in later python versions (very unlikely since func_code is a core cython method attribute). This feature is important for people who want to speedup and package their code using Cython.

Checklist:

  • I think the code is well written
  • I wrote good commit messages
  • I have squashed related commits together after the changes have been approved
  • Unit tests for the changes exist
  • Integration tests for the changes exist (if applicable)
  • I used the same coding conventions as the rest of the project
  • The new code doesn't generate linter offenses
  • Documentation reflects the changes
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences

@webknjaz
Copy link
Member

That's an interesting approach. Do you have any references to the Cython docs that would suggest such a check? We'd need a code comment with such a reference explaining what's listed there.

It's also a good idea to integrate Cython into testing somehow.

however, not all cythonized methods are of types.BuiltinFunctionType

What are the other types, then? AFAIU, this type corresponds to anything built with C-API, not just Cython. Checking against a known list of types seems like a safer bet to me so I'd prefer such an implementation. My feeling is that we should be able to use Cython on the fly right from within the normal CPython runtime but this needs some explotatory effort.

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.

None yet

2 participants