Skip to content

os.putenv should be documented as not affecting os.getenv's return value #85133

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

Closed
DanielMartin mannequin opened this issue Jun 12, 2020 · 1 comment
Closed

os.putenv should be documented as not affecting os.getenv's return value #85133

DanielMartin mannequin opened this issue Jun 12, 2020 · 1 comment
Labels
3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes 3.10 only security fixes docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@DanielMartin
Copy link
Mannequin

DanielMartin mannequin commented Jun 12, 2020

BPO 40961

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 = None
closed_at = None
created_at = <Date 2020-06-12.14:36:10.253>
labels = ['type-bug', '3.8', '3.9', '3.10', '3.7', 'docs']
title = "os.putenv should be documented as not affecting os.getenv's return value"
updated_at = <Date 2020-06-12.14:36:10.253>
user = 'https://bugs.python.org/DanielMartin'

bugs.python.org fields:

activity = <Date 2020-06-12.14:36:10.253>
actor = 'Daniel Martin'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2020-06-12.14:36:10.253>
creator = 'Daniel Martin'
dependencies = []
files = []
hgrepos = []
issue_num = 40961
keywords = []
message_count = 1.0
messages = ['371377']
nosy_count = 2.0
nosy_names = ['docs@python', 'Daniel Martin']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue40961'
versions = ['Python 3.5', 'Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10']

@DanielMartin
Copy link
Mannequin Author

DanielMartin mannequin commented Jun 12, 2020

I find this behavior extremely surprising:

$ ABC=def python
Python 3.7.4 (v3.7.4:e09359112e, Jul  8 2019, 14:54:52) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getenv('ABC')
'def'
>>> os.putenv('ABC', 'xyz')
>>> os.getenv('ABC')
'def'

Although the documentation for the os module does say in the documentation for putenv that

"When putenv() is supported, assignments to items in os.environ
are automatically translated into corresponding calls to putenv();
however, calls to putenv() don’t update os.environ, so it is
actually preferable to assign to items of os.environ"

, there is absolutely nothing in the documentation to give the impression that os.putenv WILL NOT affect the result of os.getenv in the same process, as it is completely undocumented that os.getenv is a wrapper around os.environ.get().

The documentation for os.environ is careful to note that the copy of the environment stored in os.environ is read only once at startup, and has a note specifically about putenv; however, given this care the lack of any similar language in the os.getenv documentation gives the impression that os.getenv behaves differently.

Ideally, the documentation of both os.getenv and os.putenv would be modified. The getenv documentation should note that it pulls from a mapping captured at first import and does not, as the name may imply, result in a call to the C function getenv. The putenv documentation should note that calls to putenv don’t update os.environ, and therefore do not affect the result of os.getenv.

Possibly, given the thread safety issues, direct calls to os.getenv and os.putenv should be deprecated in favor of os.environ use.

@DanielMartin DanielMartin mannequin added 3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes 3.10 only security fixes labels Jun 12, 2020
@DanielMartin DanielMartin mannequin assigned docspython Jun 12, 2020
@DanielMartin DanielMartin mannequin added docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error 3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes 3.10 only security fixes labels Jun 12, 2020
@DanielMartin DanielMartin mannequin assigned docspython Jun 12, 2020
@DanielMartin DanielMartin mannequin added docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error labels Jun 12, 2020
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 2, 2022
)

Co-authored-by: Jelle Zijlstra <[email protected]>
(cherry picked from commit b25352a)

Co-authored-by: slateny <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 2, 2022
)

Co-authored-by: Jelle Zijlstra <[email protected]>
(cherry picked from commit b25352a)

Co-authored-by: slateny <[email protected]>
JelleZijlstra added a commit that referenced this issue May 2, 2022
miss-islington added a commit that referenced this issue May 2, 2022
Co-authored-by: Jelle Zijlstra <[email protected]>
(cherry picked from commit b25352a)

Co-authored-by: slateny <[email protected]>
miss-islington added a commit that referenced this issue May 2, 2022
Co-authored-by: Jelle Zijlstra <[email protected]>
(cherry picked from commit b25352a)

Co-authored-by: slateny <[email protected]>
@slateny slateny closed this as completed May 4, 2022
hello-adam pushed a commit to hello-adam/cpython that referenced this issue Jun 2, 2022
)

Co-authored-by: Jelle Zijlstra <[email protected]>
(cherry picked from commit b25352a)

Co-authored-by: slateny <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes 3.10 only security fixes docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant