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

'with' loses ->bool exceptions #48839

Closed
jyasskin mannequin opened this issue Dec 8, 2008 · 10 comments
Closed

'with' loses ->bool exceptions #48839

jyasskin mannequin opened this issue Dec 8, 2008 · 10 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@jyasskin
Copy link
Mannequin

jyasskin mannequin commented Dec 8, 2008

BPO 4589
Nosy @loewis, @amauryfa
Files
  • with_badbool.patch
  • with_badbool_2.patch
  • 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:

    assignee = 'https://github.com/amauryfa'
    closed_at = <Date 2008-12-10.23:57:38.544>
    created_at = <Date 2008-12-08.06:21:09.035>
    labels = ['interpreter-core', 'type-bug']
    title = "'with' loses ->bool exceptions"
    updated_at = <Date 2008-12-10.23:57:38.520>
    user = 'https://bugs.python.org/jyasskin'

    bugs.python.org fields:

    activity = <Date 2008-12-10.23:57:38.520>
    actor = 'amaury.forgeotdarc'
    assignee = 'amaury.forgeotdarc'
    closed = True
    closed_date = <Date 2008-12-10.23:57:38.544>
    closer = 'amaury.forgeotdarc'
    components = ['Interpreter Core']
    creation = <Date 2008-12-08.06:21:09.035>
    creator = 'jyasskin'
    dependencies = []
    files = ['12280', '12285']
    hgrepos = []
    issue_num = 4589
    keywords = ['patch']
    message_count = 10.0
    messages = ['77290', '77302', '77324', '77328', '77331', '77394', '77395', '77458', '77484', '77578']
    nosy_count = 4.0
    nosy_names = ['loewis', 'amaury.forgeotdarc', 'jyasskin', 'kevinwatters']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'commit review'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue4589'
    versions = ['Python 2.6', 'Python 2.5', 'Python 3.0', 'Python 3.1', 'Python 2.7']

    @jyasskin
    Copy link
    Mannequin Author

    jyasskin mannequin commented Dec 8, 2008

    When a context manager's __exit__() method returns an object whose
    conversion to bool raises an exception, 'with' loses that exception. For
    example:

    >>> 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)
    returns non-zero, but that function returns <0 when the bool conversion
    raises an exception.

    @jyasskin jyasskin mannequin self-assigned this Dec 8, 2008
    @jyasskin jyasskin mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Dec 8, 2008
    @amauryfa
    Copy link
    Member

    amauryfa commented Dec 8, 2008

    Good catch. Here is a patch, with test.

    @jyasskin
    Copy link
    Mannequin Author

    jyasskin mannequin commented Dec 8, 2008

    I think your patch leaks a reference to 'x'. Move the Py_DECREF(x) to
    before "if (err < 0)"?

    And then really nitpicky: your test has 3 blank lines after it, and
    should have 2.

    Otherwise looks great. Thanks for picking this up!

    @amauryfa
    Copy link
    Member

    amauryfa commented Dec 8, 2008

    Running the test with -R:: indeed shows the reference leak.
    2nd patch with suggested corrections.

    @amauryfa amauryfa assigned amauryfa and unassigned jyasskin Dec 8, 2008
    @jyasskin
    Copy link
    Mannequin Author

    jyasskin mannequin commented Dec 8, 2008

    Looks good. Thanks!

    I assume this should be merged into the 2.6.x and 3.0.x branches?

    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Dec 9, 2008

    The patch doesn't apply cleanly to the 2.5 branch, because that doesn't
    have r61290. If somebody wants to backport it, go ahead (if you can do
    so by Thursday).

    @amauryfa
    Copy link
    Member

    amauryfa commented Dec 9, 2008

    Does it need to be backported to 2.5? IMO it is a corner case.

    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Dec 9, 2008

    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,
    which is fine with me. The only potential release blocker for
    a bug fix release can be a regression, which this isn't.

    @jyasskin
    Copy link
    Mannequin Author

    jyasskin mannequin commented Dec 10, 2008

    It's a corner case that would confuse anyone who ran into it, so I'm
    happy to backport it. I'll probably just re-do your patch on the 2.5
    branch since WITH_CLEANUP has a different structure there.

    @amauryfa
    Copy link
    Member

    Fixed with r67684 (2.5), r67688 (trunk), r67689 (py3k), r67690 (3.0),
    r67691 (2.6).

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant