Skip to content

Commit e060fd3

Browse files
miss-islingtonslateny
authored andcommitted
pythongh-85133: os docs: Add that getenv uses os.environ (pythonGH-91874)
Co-authored-by: Jelle Zijlstra <[email protected]> (cherry picked from commit b25352a) Co-authored-by: slateny <[email protected]>
1 parent 5c07a84 commit e060fd3

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

Doc/library/os.rst

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ process and user.
109109

110110
This mapping is captured the first time the :mod:`os` module is imported,
111111
typically during Python startup as part of processing :file:`site.py`. Changes
112-
to the environment made after this time are not reflected in ``os.environ``,
113-
except for changes made by modifying ``os.environ`` directly.
112+
to the environment made after this time are not reflected in :data:`os.environ`,
113+
except for changes made by modifying :data:`os.environ` directly.
114114

115115
This mapping may be used to modify the environment as well as query the
116116
environment. :func:`putenv` will be called automatically when the mapping
@@ -122,8 +122,8 @@ process and user.
122122

123123
.. note::
124124

125-
Calling :func:`putenv` directly does not change ``os.environ``, so it's better
126-
to modify ``os.environ``.
125+
Calling :func:`putenv` directly does not change :data:`os.environ`, so it's better
126+
to modify :data:`os.environ`.
127127

128128
.. note::
129129

@@ -133,7 +133,7 @@ process and user.
133133

134134
You can delete items in this mapping to unset environment variables.
135135
:func:`unsetenv` will be called automatically when an item is deleted from
136-
``os.environ``, and when one of the :meth:`pop` or :meth:`clear` methods is
136+
:data:`os.environ`, and when one of the :meth:`pop` or :meth:`clear` methods is
137137
called.
138138

139139
.. versionchanged:: 3.9
@@ -224,7 +224,10 @@ process and user.
224224
.. function:: getenv(key, default=None)
225225

226226
Return the value of the environment variable *key* if it exists, or
227-
*default* if it doesn't. *key*, *default* and the result are str.
227+
*default* if it doesn't. *key*, *default* and the result are str. Note that
228+
since :func:`getenv` uses :data:`os.environ`, the mapping of :func:`getenv` is
229+
similarly also captured on import, and the function may not reflect
230+
future environment changes.
228231

229232
On Unix, keys and values are decoded with :func:`sys.getfilesystemencoding`
230233
and ``'surrogateescape'`` error handler. Use :func:`os.getenvb` if you
@@ -236,7 +239,11 @@ process and user.
236239
.. function:: getenvb(key, default=None)
237240

238241
Return the value of the environment variable *key* if it exists, or
239-
*default* if it doesn't. *key*, *default* and the result are bytes.
242+
*default* if it doesn't. *key*, *default* and the result are bytes. Note that
243+
since :func:`getenvb` uses :data:`os.environb`, the mapping of :func:`getenvb` is
244+
similarly also captured on import, and the function may not reflect
245+
future environment changes.
246+
240247

241248
:func:`getenvb` is only available if :data:`supports_bytes_environ`
242249
is ``True``.
@@ -443,10 +450,11 @@ process and user.
443450
changes to the environment affect subprocesses started with :func:`os.system`,
444451
:func:`popen` or :func:`fork` and :func:`execv`.
445452

446-
Assignments to items in ``os.environ`` are automatically translated into
453+
Assignments to items in :data:`os.environ` are automatically translated into
447454
corresponding calls to :func:`putenv`; however, calls to :func:`putenv`
448-
don't update ``os.environ``, so it is actually preferable to assign to items
449-
of ``os.environ``.
455+
don't update :data:`os.environ`, so it is actually preferable to assign to items
456+
of :data:`os.environ`. This also applies to :func:`getenv` and :func:`getenvb`, which
457+
respectively use :data:`os.environ` and :data:`os.environb` in their implementations.
450458

451459
.. note::
452460

@@ -645,10 +653,10 @@ process and user.
645653
environment affect subprocesses started with :func:`os.system`, :func:`popen` or
646654
:func:`fork` and :func:`execv`.
647655

648-
Deletion of items in ``os.environ`` is automatically translated into a
656+
Deletion of items in :data:`os.environ` is automatically translated into a
649657
corresponding call to :func:`unsetenv`; however, calls to :func:`unsetenv`
650-
don't update ``os.environ``, so it is actually preferable to delete items of
651-
``os.environ``.
658+
don't update :data:`os.environ`, so it is actually preferable to delete items of
659+
:data:`os.environ`.
652660

653661
.. audit-event:: os.unsetenv key os.unsetenv
654662

0 commit comments

Comments
 (0)