Skip to content

Some missing error checks on _PyObject_LookupSpecial in bytes and weakref #159430

Open

Description

@msullivan

Bug report

A robot reported these bugs to me while I had it trying to collect a list of examples of something tangentially related.

In format_obj in bytes and weakref_repr in weakref.ref, we don't check PyErr_Occurred() after a NULL return from _PyObject_LookupSpecial. Pretty marginal tbh but it triggers "returned a result with an exception set".

# bytes %-formatting ignores an error from looking up __bytes__
class BadBytes:
    @property
    def __bytes__(self):
        raise AssertionError('oop')
    def __buffer__(self, flags):
        return memoryview(b'abc')

b'%b' % BadBytes()
# SystemError: <class 'memoryview'> returned a result with an exception set
# weakref repr() ignores an error from looking up __name__
import weakref

class BadName:
    @property
    def __name__(self):
        raise AssertionError('oop')

obj = BadName()
repr(weakref.ref(obj))
# SystemError: <built-in function repr> returned a result with an exception set

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added a commit that references this issue on Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions