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

[BUG] __self__ attribute differs on Cython compiled functions #4036

Closed
youmeiyougc opened this issue Mar 15, 2021 · 7 comments · Fixed by #4051
Closed

[BUG] __self__ attribute differs on Cython compiled functions #4036

youmeiyougc opened this issue Mar 15, 2021 · 7 comments · Fixed by #4051

Comments

@youmeiyougc
Copy link

youmeiyougc commented Mar 15, 2021

from wrapt import decorator

@decorator
def d1(wrapped, instance, args, kwargs):
    print(f'd1: {args}, {kwargs}')
    return wrapped(*args, **kwargs)

def d2(*argsT, **kwargsT):
    @decorator
    def d3(wrapped, instance, args, kwargs):
        print(f'd3: {argsT}, {kwargsT}, {args}, {kwargs}')
        return wrapped(*args, **kwargs)
    return d3

@d1
@d2(a=int, b=str)
def hello():
    print('hello')

Save above code as hello.py, use another module, import hello and call hello.hello(), all success.

import Cython.Build
import distutils.core
 
def py2c(file):
    cpy = Cython.Build.cythonize(file)
    distutils.core.setup(
	    name = 'test',
	    version = "1.0",
	    ext_modules= cpy,
	    author = "test",
	    author_email='test@163.com'
	)

if __name__ == '__main__':
    file = "hello.py"
    py2c(file)

Save above code as py2c.py, run "python py2c.py build_ext --inplace" ,it generate hello.pyd, import again, raise TypeError.

Expect it run success and print "hello".

  • OS: Windows 7
  • Python version 3.6.8
  • Cython version 0.29.22

raw_code
py2c
exception

@da-woods
Copy link
Contributor

def d2(*argsT, **kwargsT):
    @decorator
    def d3(wrapped, instance, args, kwargs, extra_arg=None):
        print(wrapped, instance)
        print(f'd3: {argsT}, {kwargsT}, {args}, {kwargs} {extra_arg}')
        return wrapped(*args, **kwargs)
    return d3

@d2(a=int, b=str)
def hello():
    print('hello')

For the line print(wrapped, instance) it prints

<modulename.__pyx_scope_struct__d2 object at 0x7f64a8584df0> <cyfunction hello at 0x7f64a8394040>

i.e. it's pulled the internal closure out of d3, which is stored as __self__ in d3 and is then passing it to d3 as the first argument (which pushes all the other arguments along one).

I think this is probably wrapt trying to be too clever and picking apart Cython objects in a way that doesn't really make sense. But I'm not completely sure.

@da-woods
Copy link
Contributor

I'm not hugely convinced that CyFunction should have a __self__ attribute (that just points to the closure). I think FusedFunction might need to (and CyFunction probably has it just because they share some implementation) but I think maybe it should be removed from CyFunction.

With the stray self attribute gone wrapt would probably work.

@youmeiyougc
Copy link
Author

I'm not hugely convinced that CyFunction should have a __self__ attribute (that just points to the closure). I think FusedFunction might need to (and CyFunction probably has it just because they share some implementation) but I think maybe it should be removed from CyFunction.

With the stray self attribute gone wrapt would probably work.

It's too hard to me.I have abandoned the plan, and use a simple decorator like d1.
Thank you.

@GrahamDumpleton
Copy link

The __self__ attribute has a special purpose in Python for methods of classes and is an indication that binding has occurred. If cython is using the __self__ attribute to mean something entirely different to what it means in the Python object model that could cause problems for packages which expect it to have the meaning it does in the context of the Python object model.

In other words, if cython is using the __self__ attribute for something else, it will break wrapt, which needs to be able to know in certain contexts when binding of functions has occurred. So if __self__ in cython has a completely different purpose, it may be better to use a different name for the attribute in cython.

@da-woods
Copy link
Contributor

@GrahamDumpleton Yes I agree - I think it's currently being misused in Cython (and thus it's reasonable for wrapt to fail on it). I'm going try to fix the bug in Cython when I have time. I think it's easily removed for most Cython functions but it's possible it may have to remain for "fused functions". Those are a fairly obscure edge-case though, that we may have to live with for now.

da-woods added a commit to da-woods/cython that referenced this issue Mar 18, 2021
The __self__ argument should be present only for bound functions.
Fixes cython#4036

Currently it isn't easily possible to get this working absolutely
correctly for fused functions. I raise an attribute error but
hasattr still returns True

Additionally, Python 2 methods always have a __self__ attribute
but set it to None. I follow Python 3 behaviour and don't have
the attribute
@scoder
Copy link
Contributor

scoder commented Mar 21, 2021

@da-woods why is this only relevant for fused functions and not for regular (bound) methods?

@scoder scoder changed the title [BUG] use wrapt success in .py, but failed in .pyd [BUG] __self__ attribute differs on Cython compiled functions Mar 21, 2021
@da-woods
Copy link
Contributor

da-woods commented Mar 21, 2021

@da-woods why is this only relevant for fused functions and not for regular (bound) methods?

I believe that bound CyFunction get wrapped up with PyMethod_New (

__Pyx_PyMethod_New, /*tp_descr_get*/
) and so the method object provides the __self__ attribute. Therefore CyFunction doesn't have to.

For fused functions, a bound fused function remains a fused function (so [] indexing still works) -

__pyx_FusedFunction_descr_get, /*tp_descr_get*/
. Therefore, they do need a __self__ attribute.


The current status is that Cython returns the closure as __self__ which I don't think is ever right.

@scoder scoder added this to the 3.0 milestone Mar 30, 2021
scoder pushed a commit that referenced this issue Mar 30, 2021
The __self__ argument should be present only for bound functions.
Fixes #4036

Currently it isn't easily possible to get this working absolutely correctly for fused functions. I raise an attribute error but hasattr still returns True.

Additionally, Python 2 methods always have a __self__ attribute but set it to None. I follow Python 3 behaviour and don't have the attribute.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants