Skip to content

Unexpected SystemError in Python3.12.0a7: <method 'startswith' of 'str' objects> returned a result with an exception set#103632

@abravalheri

Description

@abravalheri

Bug report

I noticed recently that Setuptools CI started failing for Python 3.12.0a7 with a SystemError that is difficult to understand:

SystemError: <built-in method startswith of str object at 0x7fe399991298> returned a result with an exception set

At a first glance this error does not make much sense because str.startswith is a built-in method in a built-in data structure. I am struggling to see how it can have an exception set attached to the result.

I managed to create the following simplified reproducer:

dockerrun--rm-itpython:3.12.0a7-bullseyePython3.12.0a7 (main, Apr122023, 14:13:09) [GCC10.2.120210110] onlinuxType"help", "copyright", "credits"or"license"formoreinformation. >>>classDistribution: ... @property ... defversion(self): ... try: ... returnself._version ... exceptAttributeErrorase: ... # ... complex code omitted for the sake of simplification ... ... raiseValueError("Missing Version") ... def__getattr__(self, attr): ... ifattr.startswith("_"): ... raiseAttributeError(attr) ... # ... complex code omitted for the sake of simplification ... ... return42 ... >>>dist=Distribution() >>>dist.versionTraceback (mostrecentcalllast): File"<stdin>", line5, inversionFile"<stdin>", line11, in__getattr__AttributeError: _version. Didyoumean: 'version'? Duringhandlingoftheaboveexception, anotherexceptionoccurred: Traceback (mostrecentcalllast): File"<stdin>", line8, inversionValueError: MissingVersionTheaboveexceptionwasthedirectcauseofthefollowingexception: Traceback (mostrecentcalllast): File"<stdin>", line1, in<module>File"<stdin>", line10, in__getattr__SystemError: <method'startswith'of'str'objects>returnedaresultwithanexceptionset>>>

The error seems to be related to the combination of the following factors:

  1. A property is being used1
  2. The property getter access an undefined attribute and triggers __getattr__
  3. __getattr__ raises an AttributeError
  4. The property getter is supposed to catch the AttributeError exception and raise a different exeception.

My expectation is that the SystemError never gets triggered with the confusing message. Instead the example should end up with a ValueError.

Please note that the example is very simplified. For the realistic implementation please consider pkg_resources.Distribution.

Your environment

  • CPython versions tested on: 3.12.0a7
  • Operating system and architecture:
    Reproducer tested on both Ubuntu 20.04.6 LTS machine and python:3.12.0a7-bullseye container

Problem also identified in GitHub Actions runners: ubuntu-latest, macos-latest, windows-latest (see https://github.com/pypa/setuptools/actions/runs/4710601389?pr=3893) for Python 3.12-dev.

Footnotes

  1. If we replace the property with a regular method, the example works as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions