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.

classification
Title: Unhelpful __repr__() in os.environ
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: akuchling, amaury.forgeotdarc, ezio.melotti, kjohnson, r.david.murray
Priority: normal Keywords: easy, needs review, patch

Created on 2009-11-12 11:53 by kjohnson, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue7310.diff ezio.melotti, 2009-12-18 16:54 Patch against py3k + tests
Messages (10)
msg95160 - (view) Author: Kent Johnson (kjohnson) * Date: 2009-11-12 11:53
In Python 2.x, os.environ extends UserDict.IterableUserDict and
therefore os.environ.__repr__() shows the environment. This makes it
easy and intuitive to view the entire environment in the interactive
interpreter.

In Python 3.1, os.environ extends _abcoll.MutableMapping and uses
object.__repr__(). This is a much less useful representation.

I suggest adding this __repr__() method to class os._Environ (os.py line
380):

    def __repr__(self): return repr(self.data)
msg95224 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2009-11-14 01:34
Here is a patch against py3k.
msg95358 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-11-16 19:11
A unit test would be appreciated. It should check that the output looks
like a dict.
msg95366 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2009-11-17 07:42
New patch that includes a unittest.

I was also considering to replace the repr() with something that looks
like _Environ({dict-here}). That will still contain all the information
and also clarify that os.environ is not just a simple dict. (Even if
it's probably not relevant, if we want eval(repr(os.environ)) to work,
the repr() should also include the other args accepted by the _Environ()
constructor.)

If you think this is a good idea I can make a new patch, otherwise, if
the current patch is fine, I'll commit it as it is.
msg96570 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2009-12-18 16:54
New patch, I added the _Environ() around the dict in the repr and
improved the tests.
msg99709 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2010-02-22 03:58
The patch looks fine to me.  Ezio, please commit it (remembering to add a NEWS entry).
msg99748 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-02-22 15:27
Do you have any preference about the text that should appear in the repr?
There have been some discussion on #python-dev about it, and the two main proposal were:
1) _Environ({...}) -> the right class name but kind of "ugly" and probably unnecessary (if no one cares about the name of the real class);
2) environ({...}) -> non-existing name, but enough to clarify that it's not a plain dict and less "ugly" name;
msg99751 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-02-22 15:43
I like (2).  If someone tries to use the repr to recreate the object, it will fail with a message that mentions _Environ.  So no information is lost, I think, by using (2).
msg99753 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2010-02-22 15:47
After talking w/ RDM about this, I'm also fine with 2), but either option is OK.
msg99764 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-02-22 16:02
Fixed in r78316 (py3k) and r78317 (release31-maint), thanks for the feedback!
History
Date User Action Args
2022-04-11 14:56:54adminsetgithub: 51559
2010-02-22 16:02:33ezio.melottisetstatus: open -> closed
resolution: fixed
messages: + msg99764

stage: patch review -> resolved
2010-02-22 15:47:42akuchlingsetmessages: + msg99753
2010-02-22 15:43:35r.david.murraysetnosy: + r.david.murray
messages: + msg99751
2010-02-22 15:27:45ezio.melottisetmessages: + msg99748
2010-02-22 03:58:20akuchlingsetnosy: + akuchling
messages: + msg99709
2009-12-18 16:54:30ezio.melottisetfiles: + issue7310.diff

messages: + msg96570
2009-12-18 16:51:34ezio.melottisetfiles: - issue7310.diff
2009-11-17 07:42:52ezio.melottisetfiles: + issue7310.diff

messages: + msg95366
2009-11-17 07:21:04ezio.melottisetfiles: - issue7310.diff
2009-11-16 19:11:14amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg95358
2009-11-14 02:09:22ezio.melottisetkeywords: + needs review
2009-11-14 01:34:58ezio.melottisetfiles: + issue7310.diff
priority: normal

assignee: ezio.melotti
versions: + Python 3.2
keywords: + patch, easy
nosy: + ezio.melotti

messages: + msg95224
stage: patch review
2009-11-12 11:53:59kjohnsoncreate