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

Exceptions raised in cursed-in dunder methods are not raised to calling code #56

Open
EZForever opened this issue Jun 13, 2021 · 0 comments

Comments

@EZForever
Copy link

EZForever commented Jun 13, 2021

Proof of concept:

#!/usr/bin/env python3

from forbiddenfruit import curse

class foo(object):
    def __init__(self): pass

def foo_add(self: foo, other: int) -> foo:
    raise TypeError('This exception should be caught')

curse(foo, '__add__', foo_add)

try:
    print(foo() + 1) # <class '__main__.foo'> (on Windows), __main__ (on Linux)
except TypeError:
    print('TypeError caught') # Not caught (wrong)

Test result:

(venv) C:\dev\test> python bug.py  
Exception ignored on calling ctypes callback function: <function foo_add at 0x0000026EC623FEE0>
Traceback (most recent call last):
  File "E:\src\pytrickz\venv\lib\site-packages\forbiddenfruit\__init__.py", line 328, in wrapper
    return func(*args, **kwargs)
  File "bug.py", line 13, in foo_add
    raise TypeError('This exception should be caught')
TypeError: This exception should be caught
<class '__main__.foo'>

(venv) C:\dev\test> 

The exception is printed to the screen (presumably by ctypes?) but not raised, except TypeError not triggered, print() receives a garbage result that depends on Python version and class structure (for example, if I remove foo.__init__ from code the result becomes None on Windows).

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

No branches or pull requests

1 participant