python / python/cpython

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

Open
#156,491 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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
  • gh-156492

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the os._Environ implementations of get() and contains() described in the issue, and inspect how missing keys are currently handled. The change is complete when those methods avoid raising and immediately catching KeyError while preserving their existing results for missing keys.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.