Skip to content

Commit b25352a

Browse files
pythongh-85133: os docs: Add that getenv uses os.environ (python#91874)
Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 9166ace commit b25352a

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
@@ -177,8 +177,8 @@ process and user.
177177

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

183183
This mapping may be used to modify the environment as well as query the
184184
environment. :func:`putenv` will be called automatically when the mapping
@@ -190,8 +190,8 @@ process and user.
190190

191191
.. note::
192192

193-
Calling :func:`putenv` directly does not change ``os.environ``, so it's better
194-
to modify ``os.environ``.
193+
Calling :func:`putenv` directly does not change :data:`os.environ`, so it's better
194+
to modify :data:`os.environ`.
195195

196196
.. note::
197197

@@ -201,7 +201,7 @@ process and user.
201201

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

207207
.. versionchanged:: 3.9
@@ -292,7 +292,10 @@ process and user.
292292
.. function:: getenv(key, default=None)
293293

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

297300
On Unix, keys and values are decoded with :func:`sys.getfilesystemencoding`
298301
and ``'surrogateescape'`` error handler. Use :func:`os.getenvb` if you
@@ -304,7 +307,11 @@ process and user.
304307
.. function:: getenvb(key, default=None)
305308

306309
Return the value of the environment variable *key* if it exists, or
307-
*default* if it doesn't. *key*, *default* and the result are bytes.
310+
*default* if it doesn't. *key*, *default* and the result are bytes. Note that
311+
since :func:`getenvb` uses :data:`os.environb`, the mapping of :func:`getenvb` is
312+
similarly also captured on import, and the function may not reflect
313+
future environment changes.
314+
308315

309316
:func:`getenvb` is only available if :data:`supports_bytes_environ`
310317
is ``True``.
@@ -511,10 +518,11 @@ process and user.
511518
changes to the environment affect subprocesses started with :func:`os.system`,
512519
:func:`popen` or :func:`fork` and :func:`execv`.
513520

514-
Assignments to items in ``os.environ`` are automatically translated into
521+
Assignments to items in :data:`os.environ` are automatically translated into
515522
corresponding calls to :func:`putenv`; however, calls to :func:`putenv`
516-
don't update ``os.environ``, so it is actually preferable to assign to items
517-
of ``os.environ``.
523+
don't update :data:`os.environ`, so it is actually preferable to assign to items
524+
of :data:`os.environ`. This also applies to :func:`getenv` and :func:`getenvb`, which
525+
respectively use :data:`os.environ` and :data:`os.environb` in their implementations.
518526

519527
.. note::
520528

@@ -713,10 +721,10 @@ process and user.
713721
environment affect subprocesses started with :func:`os.system`, :func:`popen` or
714722
:func:`fork` and :func:`execv`.
715723

716-
Deletion of items in ``os.environ`` is automatically translated into a
724+
Deletion of items in :data:`os.environ` is automatically translated into a
717725
corresponding call to :func:`unsetenv`; however, calls to :func:`unsetenv`
718-
don't update ``os.environ``, so it is actually preferable to delete items of
719-
``os.environ``.
726+
don't update :data:`os.environ`, so it is actually preferable to delete items of
727+
:data:`os.environ`.
720728

721729
.. audit-event:: os.unsetenv key os.unsetenv
722730

0 commit comments

Comments
 (0)