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
Allow mappings as globals (was: Fix dictionary subclass ...) #42784
There is an inconsistancy in the way that dictionary
subclasses behave
when they are used as as namespaces in execs.
Basically, while python 2.4 permits the usage of
dictionary subclasses
for local environments, it still bypasses the subclass
functions and
uses the C API for global environments. The attached
patch (and
unittest!) addresses this issue.
Do you have any use cases? AFAICT, there were no unmet
needs with locals option.
Also, there was a reason that globals weren't included but I
remember what it was.
crutchergmail commented on Jan 11, 2006
crutchergmailmannequin
MannequinAuthor
More actions
Logged In: YES
user_id=1424288
With the ability to use dictionary subclasses for local and
global environments, it becomes very easy to implement
execution environments with extended variable semantics for
special purposes.
This includes driving interactive processes, config file
processors, type checking, lazy object construction, and
read-only variables.
As it currently stands, the semantics are inconsistent. The
local case should not have been changed if the global case
was not going to be changed.
crutchergmail commented on Jan 11, 2006
crutchergmailmannequin
MannequinAuthor
More actions
Logged In: YES
user_id=1424288
Here's an example of a little typed environment. It's not
the most robust, but it gets you thinking.
Okay, I will take it under consideration. Also, I'll try to
remember the reason it wasn't done the first time around.
No promises though -- by itself consistency is a weak
motivation.
loewis commented on Jan 12, 2006
loewismannequin
Mannequin
More actions
Logged In: YES
user_id=21627
I think the history is this: it originates from
python.org/sf/215126, which requested that you can pass dict
subtypes to eval. Armin noted that eval will always produce
LOAD/STORE_NAME, so just modifying these opcodes is
sufficient to fulfil the feature request.
crutchergmail commented on Jan 12, 2006
crutchergmailmannequin
MannequinAuthor
More actions
Logged In: YES
user_id=1424288
Well, why fix it for eval but not for exec? I don't think
the time hit is noticeable, I ran 'time make test' twice
each on the trunk with and without the patch. Here are the
results:
Trunk:
real 9m17.117s
user 3m30.930s
sys 0m35.417s
real 9m9.471s
user 3m31.484s
sys 0m34.978s
Patch:
real 9m32.469s
user 3m40.134s
sys 0m36.140s
real 9m6.779s
user 3m27.529s
sys 0m34.716s
arigo commented on Jan 14, 2006
arigomannequin
Mannequin
More actions
Logged In: YES
user_id=4771
Provided this can be done with no measurable performance hit,
I guess that I'm fine with the idea. The patch needs a bit
more work, though: I don't see why it should accept dict
subclasses as globals but not arbitrary mappings (as it now
does for the locals). This is mainly an issue of removing
a few checks in various places, like EXEC_STMT and the eval()
and execfile() built-ins.
There is a missing exception check/clear in the part about
LOAD_NAME, after the PyObject_GetItem(f->f_globals, w).
A side note: in the current trunk already, LOAD_GLOBAL
contains a couple of checks, namely PyString_CheckExact()
and hash != -1. We might be able to prove in advance that
these two conditions are always true. We could then remove
the checks. Not sure the difference measurable, though.
crutchergmail commented on Jan 22, 2006
crutchergmailmannequin
MannequinAuthor
More actions
Logged In: YES
user_id=1424288
I'm going to fix the missed exception test this weekend, and
try to get an updated patch to you.
crutchergmail commented on Jan 22, 2006
crutchergmailmannequin
MannequinAuthor
More actions
Logged In: YES
user_id=1424288
I've fixed up the exception case, and extended the test case
to check for it. Is there anything else I can do to get this in?
13 years past from this proposed. Is this still good feature for Python?
Personally, I dislike adding more dynamic flexibility to Python name space.
Python is very dynamic language, and it made difficult to make Python faster.
For example, PHP is not so dynamic as Python, and it is one of reasons why PHP VM is now much faster than Python VM. They can easily do more optimization like function inlining statically.
I'm interested in optimizing Python. But I'm not interested in adding more "slow path" we need to maintain forever.
Activity
crutchergmail commentedon Jan 11, 2006
There is an inconsistancy in the way that dictionary
subclasses behave
when they are used as as namespaces in execs.
Basically, while python 2.4 permits the usage of
dictionary subclasses
for local environments, it still bypasses the subclass
functions and
uses the C API for global environments. The attached
patch (and
unittest!) addresses this issue.
I'm pretty sure we keep the fast path in this.
rhettinger commentedon Jan 11, 2006
Logged In: YES
user_id=80475
Do you have any use cases? AFAICT, there were no unmet
needs with locals option.
Also, there was a reason that globals weren't included but I
remember what it was.
crutchergmail commentedon Jan 11, 2006
Logged In: YES
user_id=1424288
With the ability to use dictionary subclasses for local and
global environments, it becomes very easy to implement
execution environments with extended variable semantics for
special purposes.
This includes driving interactive processes, config file
processors, type checking, lazy object construction, and
read-only variables.
As it currently stands, the semantics are inconsistent. The
local case should not have been changed if the global case
was not going to be changed.
crutchergmail commentedon Jan 11, 2006
Logged In: YES
user_id=1424288
Here's an example of a little typed environment. It's not
the most robust, but it gets you thinking.
crutchergmail commentedon Jan 11, 2006
Logged In: YES
user_id=1424288
Here's a more interesting example. This works fine, unless
the variables fall through to the global dictionary, or some
code marks them global.
rhettinger commentedon Jan 12, 2006
Logged In: YES
user_id=80475
Okay, I will take it under consideration. Also, I'll try to
remember the reason it wasn't done the first time around.
No promises though -- by itself consistency is a weak
motivation.
loewis commentedon Jan 12, 2006
Logged In: YES
user_id=21627
I think the history is this: it originates from
python.org/sf/215126, which requested that you can pass dict
subtypes to eval. Armin noted that eval will always produce
LOAD/STORE_NAME, so just modifying these opcodes is
sufficient to fulfil the feature request.
crutchergmail commentedon Jan 12, 2006
Logged In: YES
user_id=1424288
Well, why fix it for eval but not for exec? I don't think
the time hit is noticeable, I ran 'time make test' twice
each on the trunk with and without the patch. Here are the
results:
Trunk:
real 9m17.117s
user 3m30.930s
sys 0m35.417s
real 9m9.471s
user 3m31.484s
sys 0m34.978s
Patch:
real 9m32.469s
user 3m40.134s
sys 0m36.140s
real 9m6.779s
user 3m27.529s
sys 0m34.716s
arigo commentedon Jan 14, 2006
Logged In: YES
user_id=4771
Provided this can be done with no measurable performance hit,
I guess that I'm fine with the idea. The patch needs a bit
more work, though: I don't see why it should accept dict
subclasses as globals but not arbitrary mappings (as it now
does for the locals). This is mainly an issue of removing
a few checks in various places, like EXEC_STMT and the eval()
and execfile() built-ins.
There is a missing exception check/clear in the part about
LOAD_NAME, after the PyObject_GetItem(f->f_globals, w).
A side note: in the current trunk already, LOAD_GLOBAL
contains a couple of checks, namely PyString_CheckExact()
and hash != -1. We might be able to prove in advance that
these two conditions are always true. We could then remove
the checks. Not sure the difference measurable, though.
crutchergmail commentedon Jan 22, 2006
Logged In: YES
user_id=1424288
I'm going to fix the missed exception test this weekend, and
try to get an updated patch to you.
crutchergmail commentedon Jan 22, 2006
Logged In: YES
user_id=1424288
I've fixed up the exception case, and extended the test case
to check for it. Is there anything else I can do to get this in?
9 remaining items
rhettinger commentedon Jan 2, 2009
Haven't had a chance to work on this one. Unassigning.
terryjreedy commentedon Aug 9, 2010
I believe this is covered by the PEP-3003 3.2 change moratorium.
methane commentedon Apr 14, 2019
13 years past from this proposed. Is this still good feature for Python?
Personally, I dislike adding more dynamic flexibility to Python name space.
Python is very dynamic language, and it made difficult to make Python faster.
For example, PHP is not so dynamic as Python, and it is one of reasons why PHP VM is now much faster than Python VM. They can easily do more optimization like function inlining statically.
I'm interested in optimizing Python. But I'm not interested in adding more "slow path" we need to maintain forever.
terryjreedy commentedon Apr 14, 2019
I am willing to close it.
pppery commentedon Apr 14, 2019
See also bpo-32615
rhettinger commentedon Apr 15, 2019
I concur that this should be closed. We have a decade of evidence that no one really needs this.