Skip to content

os._Environ.get() and __contains__() raise and catch KeyError internally #156491

Description

@alexmalyshev

Feature or enhancement

Proposal:

Since os._Environ is a subclass of collections.abc.Mapping, its get() and __contains__() methods are implemented in terms of __getitem__(). By forwarding to __getitem__(), a KeyError is raised when the key does not exist. This KeyError is then immediately caught and swallowed.

Handling this exception is extra, avoidable work. _Environ can check its internal dictionary directly which is more efficient.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions