Navigation Menu

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

Cython's co_firstlineno doesn't include decorators, like normal python #2536

matham opened this issue Aug 7, 2018 · 1 comment


Copy link

matham commented Aug 7, 2018

Using binding=True fills in all the attributes to make cython function work with the inspect module, mostly. Some monkey patching is also required for it to recognize cython functions/methods as functions/methods (see at the end).

However, it turns out that python's co_firstlineno starts from the first decorator that decorates the function, while cython's co_firstlineno is set to the actual function start and does not include the decorator, which I think is a bug on cython's part.

You can replicate it by inspecting co_firstlineno for e.g. the following function in cython and python using binding=True.

def do_nothing(f):
    return f

def do_something():

For reference, here's my inspect monkey patching:

import inspect
from inspect import ismethod, isfunction

def ismethod_cython(object):
    if ismethod(object):
        return True
    if object.__class__.__name__ == 'cython_function_or_method' and hasattr(object, '__func__'):
        return True
    return False
inspect.ismethod = ismethod_cython

def isfunction_cython(object):
    if isfunction(object):
        return True
    if object.__class__.__name__ == 'cython_function_or_method' and not hasattr(object, '__func__'):
        return True
    return False
inspect.isfunction = isfunction_cython
Copy link

scoder commented Aug 7, 2018

Right. Probably easy to fix by keeping the first line number around explicitly. Maybe even by using the first decorator line number as the position of the function node right in the parser – if that doesn't break anything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

2 participants