This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients Arfrever, akira, blueyed, iritkatriel, ned.deily, pefu, serhiy.storchaka, vstinner
Date 2021-08-30.14:22:24
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1630333344.31.0.19426044089.issue20658@roundup.psfhosted.org>
In-reply-to
Content
The following command still fails on the Python main branch on Linux:
---
$ env -i =value ./python -c 'import pprint, os; pprint.pprint(os.environ); del os.environ[""]'
environ({'': 'value', 'LC_CTYPE': 'C.UTF-8'})
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/vstinner/python/main/Lib/os.py", line 689, in __delitem__
    unsetenv(encodedkey)
    ^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument
---

'del os.environ[""]' calls unsetenv("") which fails with EINVAL.

Python is a thin wrapper to C functions setenv() and unsetenv(), and raises an exception when a C function fails. It works as expected.

Python exposes the variable with an empty name which is found in the environment variables: again, it works as expected.

I don't see how Python could do better, since the glibc unsetenv() fails with EINVAL if the string is empty. It is even a documented behaviour, see the unsetenv() manual page:
---
ERRORS

   EINVAL name is NULL, points to a string of length 0, or contains an '=' character.
---
History
Date User Action Args
2021-08-30 14:22:24vstinnersetrecipients: + vstinner, pefu, blueyed, ned.deily, Arfrever, akira, serhiy.storchaka, iritkatriel
2021-08-30 14:22:24vstinnersetmessageid: <1630333344.31.0.19426044089.issue20658@roundup.psfhosted.org>
2021-08-30 14:22:24vstinnerlinkissue20658 messages
2021-08-30 14:22:24vstinnercreate