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
'with' loses ->bool exceptions #48839
Comments
When a context manager's __exit__() method returns an object whose >>> class CM(object):
... def __init__(self, exit_result):
... self.exit_result = exit_result
... def __enter__(self):
... return 3
... def __exit__(self, a, b, c):
... return self.exit_result()
...
>>> class TrueAsBool(object):
... def __nonzero__(self): return True
...
>>> class FalseAsBool(object):
... def __nonzero__(self): return False
...
>>> class FailAsBool(object):
... def __nonzero__(self):
... raise RuntimeError("Should see this but won't")
...
>>> with CM(TrueAsBool):
... raise AssertionError("Should NOT see this")
...
>>> with CM(FalseAsBool):
... raise AssertionError("Should see this")
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
AssertionError: Should see this
>>> with CM(FailAsBool):
... raise AssertionError("Should see RuntimeException (oops)")
...
>>> The problem is that WITH_CLEANUP only checks if PyObject_IsTrue(x) |
Good catch. Here is a patch, with test. |
I think your patch leaks a reference to 'x'. Move the Py_DECREF(x) to And then really nitpicky: your test has 3 blank lines after it, and Otherwise looks great. Thanks for picking this up! |
Running the test with -R:: indeed shows the reference leak. |
Looks good. Thanks! I assume this should be merged into the 2.6.x and 3.0.x branches? |
The patch doesn't apply cleanly to the 2.5 branch, because that doesn't |
Does it need to be backported to 2.5? IMO it is a corner case. |
Not necessarily. If nobody volunteers, it won't be backported, |
It's a corner case that would confuse anyone who ran into it, so I'm |
Fixed with r67684 (2.5), r67688 (trunk), r67689 (py3k), r67690 (3.0), |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: