You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
print(os.environ.keys()) should only print the keys #76481
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=Noneclosed_at=Nonecreated_at=<Date2017-12-13.08:37:00.625>labels= ['type-bug', 'library', '3.9', '3.10']
title='print(os.environ.keys()) should only print the keys'updated_at=<Date2020-11-06.19:11:50.449>user='https://github.com/asmeurer'
Take the following scenario which happened to me recently. I am trying to debug an issue on Travis CI involving environment variables. Basically, I am not sure if an environment variable is being set correctly. So in my code, I put
print(os.environ.keys())
The reason I put keys() was 1, I didn't care about the values, and 2, I have secure environment variables set on Travis.
To my surprise, in the Travis logs, I found something like this
So instead of just printing the keys like I asked for, it printed the whole environment, plus "KeysView(environ(". Included here was my secure environment variable.
Aside from being a potential security issue, it's just annoying that it prints the whole environment. The values are much larger than the keys. With a normal dictionary, print(d.keys()) just prints the keys:
For your current situation, list(os.environ) or iter(os.environ) both return keys only.
It looks like the __repr__ on the class for os.environ is printed for os.environ (which is expected). For os.environ.keys(), the same __repr__ is wrapped as a KeysView, for os.environ.values(), it's wrapped as a ValuesView, and os.environ.items(), as an ItemsView.
This is a consequence of the repr used by KeysView, which it inherits from MappingView. I agree that the result is surprising, but there may not be a generic fix. It's not entirely clear what KeysView should do here, but presumably we could at least add a repr to environ's use of it that would produce something useful.
Note that a workaround when doing something like a travis debug is to print os.environ._data.keys(), which will print the bytes versions of the keys. I wouldn't recommend depending on that continuing to, though ;)
Don't make KeysView.__repr__ and ValuesView.__repr__ containing the lists of all keys and values. This will make the repr of the view of a mapping which is a proxy of an external DB containing the full content of that DB, which can be gigabytes. See for example rejected bpo-21670.
asmeurer commented on Dec 13, 2017
asmeurermannequin
MannequinAuthor
More actions
So the best fix is to just override keys() in the _Environ class, so that it returns an EnvironKeysView class that overrides __repr__?
Don't make KeysView.__repr__ and ValuesView.__repr__ containing the lists of all keys and values. This will make the repr of the view of a mapping which is a proxy of an external DB containing the full content of that DB, which can be gigabytes. See for example rejected bpo-21670.
The current implementation displays repr(self._mapping):
Never mind. I see it's in bytes. I missed that the first time. But, if it wasn't in bytes, would that be an OK solution? Or is it bad to override those methods in that class?
Activity
asmeurer commentedon Dec 13, 2017
Take the following scenario which happened to me recently. I am trying to debug an issue on Travis CI involving environment variables. Basically, I am not sure if an environment variable is being set correctly. So in my code, I put
The reason I put keys() was 1, I didn't care about the values, and 2, I have secure environment variables set on Travis.
To my surprise, in the Travis logs, I found something like this
So instead of just printing the keys like I asked for, it printed the whole environment, plus "KeysView(environ(". Included here was my secure environment variable.
Now, fortunately, Travis hides the contents of secure environment variables in the logs, but it didn't used to (https://blog.travis-ci.com/2017-05-08-security-advisory).
Aside from being a potential security issue, it's just annoying that it prints the whole environment. The values are much larger than the keys. With a normal dictionary, print(d.keys()) just prints the keys:
csabella commentedon Dec 13, 2017
For your current situation, list(os.environ) or iter(os.environ) both return keys only.
It looks like the __repr__ on the class for os.environ is printed for os.environ (which is expected). For os.environ.keys(), the same __repr__ is wrapped as a KeysView, for os.environ.values(), it's wrapped as a ValuesView, and os.environ.items(), as an ItemsView.
In 2.7, os.environ.keys() print just keys.
bitdancer commentedon Dec 13, 2017
This is a consequence of the repr used by KeysView, which it inherits from MappingView. I agree that the result is surprising, but there may not be a generic fix. It's not entirely clear what KeysView should do here, but presumably we could at least add a repr to environ's use of it that would produce something useful.
Note that a workaround when doing something like a travis debug is to print os.environ._data.keys(), which will print the bytes versions of the keys. I wouldn't recommend depending on that continuing to, though ;)
vstinner commentedon Dec 13, 2017
Usually, I use print(sorted(os.environ)) since I prefer a sorted list and it prevents such issue :-)
David:
What about something like:
vstinner@apu$ ./python -c 'import os; print(os.environ.keys())'
<KeysView ['LS_COLORS', 'XDG_MENU_PREFIX', 'LANG', 'GDM_LANG', 'HISTIGNORE', 'HISTCONTROL', 'DISPLAY', 'HOSTNAME', 'EDITOR', 'COLORTERM', 'DESKTOP_AUTOSTART_ID', 'USERNAME', 'XDG_VTNR', 'SSH_AUTH_SOCK', 'XDG_SESSION_ID', 'USER', 'DESKTOP_SESSION', 'GTK2_RC_FILES', 'PWD', 'SSH_ASKPASS', 'HOME', 'JOURNAL_STREAM', 'XDG_SESSION_TYPE', 'XDG_DATA_DIRS', 'XDG_SESSION_DESKTOP', 'LOADEDMODULES', 'MAIL', 'WINDOWPATH', 'VTE_VERSION', 'TERM', 'SHELL', 'QT_IM_MODULE', 'XMODIFIERS', 'XDG_CURRENT_DESKTOP', 'XDG_SEAT', 'SHLVL', 'PYTHONPATH', 'WINDOWID', 'MODULEPATH', 'GDMSESSION', 'GNOME_DESKTOP_SESSION_ID', 'LOGNAME', 'DBUS_SESSION_BUS_ADDRESS', 'XDG_RUNTIME_DIR', 'XAUTHORITY', 'PATH', 'PS1', 'MODULESHOME', 'MAKEFLAGS', 'HISTSIZE', 'SESSION_MANAGER', 'LESSOPEN', 'BASH_FUNC_module%%', 'BASH_FUNC_scl%%', '_', 'OLDPWD']>
list(key_view) is valid. I mimicked dict views implementation:
vstinner commentedon Dec 13, 2017
If we decide to change abc.KeysView.__repr__, IMHO we should also modify abc.ValuesView.__repr__, and maybe also abc.ItemsView.__repr__.
bitdancer commentedon Dec 13, 2017
Agreed about the other classes if we change this. Your solution looks reasonable to me.
vstinner commentedon Dec 13, 2017
Cheryl: would you like to work on a PR? If yes, tests are needed.
serhiy-storchaka commentedon Dec 13, 2017
Don't make KeysView.__repr__ and ValuesView.__repr__ containing the lists of all keys and values. This will make the repr of the view of a mapping which is a proxy of an external DB containing the full content of that DB, which can be gigabytes. See for example rejected bpo-21670.
asmeurer commentedon Dec 13, 2017
So the best fix is to just override keys() in the _Environ class, so that it returns an EnvironKeysView class that overrides __repr__?
vstinner commentedon Dec 13, 2017
The current implementation displays repr(self._mapping):
For your proxy example: what is the proxy? The KeysView subtype, or the mapping?
repr(list), repr(dict) and repr(set) all render their full content in the result, no?
repr() on a list of 1 million of items *will* render all items, even if nobody wants to read such very long string :-)
If you want to get a smarter __repr__() than the default implementation: just override __repr__(), no?
I don't see why KeysView must be special.
serhiy-storchaka commentedon Dec 13, 2017
shelve.Shelf is the example of such kind.
6 remaining items
asmeurer commentedon Dec 14, 2017
Can't third party code write their own proxies? Why do we have to do that?
csabella commentedon Dec 15, 2017
I'm missing something here, so please forgive me for asking, but why is it bad to add:
to the _Environ class?
csabella commentedon Dec 15, 2017
Never mind. I see it's in bytes. I missed that the first time. But, if it wasn't in bytes, would that be an OK solution? Or is it bad to override those methods in that class?