8000 gh-85133: os docs: Add that getenv uses os.environ (GH-91874) · python/cpython@9ce39cc · GitHub
[go: up one dir, main page]

Skip to content

Commit 9ce39cc

Browse files
gh-85133: os docs: Add that getenv uses os.environ (GH-91874)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> (cherry picked from commit b25352a) Co-authored-by: slateny <46876382+slateny@users.noreply.github.com>
1 parent bba721d commit 9ce39cc

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``.
@@ -510,10 +517,11 @@ process and user.
510517
changes to the environment affect subprocesses started with :func:`os.system`,
511518
:func:`popen` or :func:`fork` and :func:`execv`.
512519

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

518526
.. note::
519527

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

715-
Deletion of items in ``os.environ`` is automatically translated into a
723+
Deletion of items in :data:`os.environ` is automatically translated into a
716724
corresponding call to :func:`unsetenv`; however, calls to :func:`unsetenv`
717-
don't update ``os.environ``, so it is actually preferable to delete items of
718-
``os.environ``.
725+
don't update :data:`os.environ`, so it is actually preferable to delete items of
726+
:data:`os.environ`.
719727

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

0 commit comments

Comments
 (0)
0