New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove usage of UserDict from os.py #42629
Comments
This patch removes UserDict usage from os.py. Patch was generated against Revision 41544 |
Logged In: YES Subclassing 'dict' to modify some behavior only works more For example, with the patch:
stores the key 'a' directly in os.environ, bypassing |
Logged In: YES The intend was to remove dependency on UserDict. Less |
I believe the patch is wrong: it does not support setdefault() (which the current code does). It would be good if there were test cases for os.environ that made sure all dictionary methods had the correct effect on the environment. |
I don't think the point of this patch is worth doing. The existing |
I was optimization tuning and wondered why UserDict was imported by os. Replacing I see the concerns that doing that replacement is not future proof. Strange then (The other place is an internal class in _strptime.) In looking over existing third-party code, I see this nuance of when to use UserDict |
I should have added my preference. I would like to see UserDict replaced with As for future-proofing, of course when there's a change in a base class then Yes, there's a subtlety here that most people don't know about. But it's not As for the evil that is 'exec': exec "locals().data['MACHTYPE']=1; print MACHTYPE" in {}, os.environ gives me another way to mess things up. A point of unit tests is to allow changes like this without worry about code If it's not compatible with Jython or IronPython or PyPy then ignore what I I can give a lightning talk about this at PyCon. :) |
That's true for UserDict but not DictMixin. IIRC, |
Ahh, so the bug here that the environ dict should use neither UserDict nor Martin mentioned that the patch doesn't support setdefault. He didn't note >>> import os
>>> os.environ["USER"]
'dalke'
>>> os.environ["USER"] = "nobody"
>>> os.system("echo $USER")
nobody
0
>>> del os.environ["USER"]
>>> os.system("echo $USER")
0
>>> os.environ["USER"] = "dalke"
>>> while os.environ: print os.environ.popitem()
...
('GROUP', 'staff')
('XDG_DATA_HOME', '/Users/dalke/.local/share')
('TERM_PROGRAM_VERSION', '133')
('CVS_RSH', 'ssh')
('LOGNAME', 'dalke')
('USER', 'dalke')
... removed for conciseness ...
('QTDIR', '/usr/local/qt')
>>> os.system("echo $USER")
dalke
0
>>> Not enough people know about DictMixin. |
I would say dict's failure to call overloaded __setitem__ from A fix would be trivial: =================================================================== --- Objects/dictobject.c (revision 61014)
+++ Objects/dictobject.c (working copy)
@@ -1861,7 +1861,7 @@
val = ep->me_value;
if (val == NULL) {
val = failobj;
- if (PyDict_SetItem((PyObject*)mp, key, failobj))
+ if (PyObject_SetItem(mp, key, failobj))
val = NULL;
}
Py_XINCREF(val); but I have no clue what performance or other implications would be. Maybe something like this could be considered for Py3k - reviewing dict |
It's intentional that dict doesn't dynamically bind any calls; to |
Recommend closing this one. There is no point in going on the warpath |
See comments at bpo-2144. Benjamin Peterson demonstrated a more than 2x |
I think some variant of these patches was rejected before and part of IMO, this just isn't work it. The os.environ use cases are not Looking at the patch, I find the existing code to be more self- |
I'm rejecting this patch, for the same reasons as bpo-2144. If further |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: