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
PEP 3121, 384 refactoring applied to elementtree module #59856
I looked at the patch a bit more in depth and must admit that I'm reluctant to apply it. It's a very large patch with very little documentation about what steps are taken and why, and I just don't see the motivation.
The way I see it, PEP-384 is great for compatibility of third-party extensions and embeddings of Python, but much less critical for a module that's always distributed as part of stdlib and thus is kept in exact sync with the ABI of the Python version it comes with. Correct me if I'm wrong.
That said, I won't object to some refactoring if it improves the code. But when such large changes are proposed, I really prefer to see small, incremental patches that replace just a part of the code. Such patches should come with an explanation of why the change is made (i.e. which part of PEP-384 does it adhere to).
Here is a simplified patch tackling only the PEP-3121 compliance. Eli, I think this would be good to go.
elibendersky commented on Aug 6, 2013
elibenderskymannequin
Mannequin
More actions
Bless you Antoine, I've been just planning to do this myself to tackle the re-importing troubles I was having in tests the other day :-)
I'll take a look at this soon, promise!
elibendersky commented on Aug 7, 2013
elibenderskymannequin
Mannequin
More actions
Antoine, some questions about the patch:
First, I think it omits expat_capi from the state. Is that intentional?
Second, I'm not sure if this approach is fully aligned with PEP-3121. A global, shared state is still used. Instead of actually having a different module state per subinterpreter, this patch will have shared state. Another problem seems to be using PyModule_FindModule without using PyModule_AddModule first.
These problems could be shared to all of Robin's original patches. Of course, there's also the possibility that I don't fully understand PEP-3121 yet :)
First, I think it omits expat_capi from the state. Is that
intentional?
What would it do in the state? There's nothing to release.
Second, I'm not sure if this approach is fully aligned with PEP-3121.
A global, shared state is still used. Instead of actually having a
different module state per subinterpreter, this patch will have
shared state.
I don't understand what you are talking about. Perhaps you haven't looked
what PyState_FindModule() does?
> First, I think it omits expat_capi from the state. Is that
> intentional?
What would it do in the state? There's nothing to release.
That's true, but I thought one of the goals of PEP-3121 is to separate
states between sub-interpreters. So that one can't corrupt another. I'm not
sure how much it matters in practice in this case of the pyexpat capsule;
need to look into it more.
> Second, I'm not sure if this approach is fully aligned with PEP-3121.
> A global, shared state is still used. Instead of actually having a
> different module state per subinterpreter, this patch will have
> shared state.
I don't understand what you are talking about. Perhaps you haven't looked
what PyState_FindModule() does?
I did not look at the implementation yet. But the documentation says:
"""Returns the module object that was created from *def* for the current
interpreter. This method requires that the module object has been attached
to the interpreter state with
PyState_AddModule()<http://docs.python.org/dev/c-api/module.html?highlight=pymoduledef_base#PyState_AddModule\>beforehand.
In case the corresponding module object is not found or has not
been attached to the interpreter state yet, it returns NULL."""
I don't see a call to PyState_AddModule. What am I missing?
That's true, but I thought one of the goals of PEP-3121 is to
separate
states between sub-interpreters. So that one can't corrupt another.
I'm not
sure how much it matters in practice in this case of the pyexpat
capsule;
need to look into it more.
pyexpat's "capi" object is a static struct inside pyexpat.c, so that
wouldn't change anything.
Separating states between sub-interpreters only matters when said state
is mutable, which it isn't here.
I don't see a call to PyState_AddModule. What am I missing?
It is called implicitly when an extension module is imported.
elibendersky commented on Aug 8, 2013
elibenderskymannequin
Mannequin
More actions
Thanks Antoine. I think I understand the patch better now. Just a couple small questions and otherwise LGTM
This code in the beginning in PyInit__elementtree:
m=PyState_FindModule(&elementtreemodule);
if (m) {
Py_INCREF(m);
returnm;
}
Can you explain what use case it tries to cover? I couldn't find similar code in other modules we have that implement PEP-3121 (_csv, readline, io, etc.)
This code has at least one adverse effect, for testing. The problem with re-importing _elementtree I raised in http://mail.python.org/pipermail/python-dev/2013-August/127766.html is solved by moving to PEP-3121, but this piece of code above ruins it. This is because I want to set sys.modules['pyexpat'] = None and re-import _elementtree (this is what support.import_fresh_module does). But with this code in place, if _elementtree was imported any time in the past (say, in a previous test), I'll just get the instance back without attempting to do the full module initialization.
> I don't see a call to PyState_AddModule. What am I missing?
It is called implicitly when an extension module is imported.
Do you think this should be documented in the C API docs? The way they read now, it seems that calling PyState_AddModule is needed manually by extension writers.
Would you mind removing it from the patch, due to the case described
above?
ISTM that in real scenarios the sys.modules cache kicks in anyway. It
should not be really bypassed for any given sub-interpreter in sane
code.
Yeah, I think you're right. I'll submit an updated patch or, if it's
the only issue with it, perhaps you can simply remove the 3 offending
lines?
> Would you mind removing it from the patch, due to the case described
> above?
> ISTM that in real scenarios the sys.modules cache kicks in anyway. It
> should not be really bypassed for any given sub-interpreter in sane
> code.
Yeah, I think you're right. I'll submit an updated patch or, if it's
the only issue with it, perhaps you can simply remove the 3 offending
lines?
Antoine, I committed your patch (with a bit of comments added), *leaving the module caching in*. This is because removing it breaks the tests, unfortunately. The _elementtree tests are so crooked that they manage to create a situation in which the module under test throws ParseError which is a different class from ET.ParseError. This is "achieved" by multiple invocations of import_fresh_module with various fresh & blocked parameters, and I still haven't fully traced all the problems yet. Since this caching only potentially harms other tests, it's ok to leave in.
A longer term solution to all this will be http://mail.python.org/pipermail/python-dev/2013-August/127766.html - I want to eventually run all "monkey-patch the import environment to simulate some situation" sub-tests of ET in different subprocesses to they are kept independent.
Antoine, I committed your patch (with a bit of comments added),
*leaving the module caching in*.
Thanks!
A longer term solution to all this will be http://mail.python.org/pipermail/python-dev/2013-August/127766.html -
I want to eventually run all "monkey-patch the import environment to
simulate some situation" sub-tests of ET in different subprocesses to
they are kept independent.
I find it useful that the test suite stresses module unloading or
reloading. There's probably a bug either in ET or in the ET tests. I'm
not saying it's a very important issue of course, but IMHO it would be
better if we don't try to swipe it under the carpet :-)
> Antoine, I committed your patch (with a bit of comments added),
> *leaving the module caching in*.
Thanks!
> A longer term solution to all this will be
> http://mail.python.org/pipermail/python-dev/2013-August/127766.html -
> I want to eventually run all "monkey-patch the import environment to
> simulate some situation" sub-tests of ET in different subprocesses to
> they are kept independent.
I find it useful that the test suite stresses module unloading or
reloading. There's probably a bug either in ET or in the ET tests. I'm
not saying it's a very important issue of course, but IMHO it would be
better if we don't try to swipe it under the carpet :-)
I have no intention swiping things under the carpet. I'll get to the bottom
of this to understand the exact flow that causes this to happen.
But I still think ET tests should be logically separated into subprocesses.
If we want to stress test module unloading and reloading, let's have
specific, targeted tests for that.
I'd still like to see the in-PyInit__elementtree caching deleted eventually, without harming test coverage. So this issue will remain open for now, until we decide what to do.
elibendersky commented on Aug 11, 2013
elibenderskymannequin
Mannequin
More actions
BTW, Antoine, w.r.t documentation - I agree that the whole PyState_* sequence needs better documentation and examples, but in the meantime I'm attaching a simple patch for c-api/module.rst. It clarifies that PyState_AddModule doesn't really have to be called explicitly in extensions.
Activity
RobinSchreiber commentedon Aug 14, 2012
Changes proposed in PEP-3121 and PEP-384 have now been applied to the elementtree module!
pitrou commentedon Aug 14, 2012
See bpo-15390 review comments :)
RobinSchreiber commentedon Dec 14, 2012
Patch updated to work with current 3.4 Branch version of elementtree.
elibendersky commentedon Dec 29, 2012
Thanks for the patch. I'll take a look.
elibendersky commentedon Jan 13, 2013
I looked at the patch a bit more in depth and must admit that I'm reluctant to apply it. It's a very large patch with very little documentation about what steps are taken and why, and I just don't see the motivation.
The way I see it, PEP-384 is great for compatibility of third-party extensions and embeddings of Python, but much less critical for a module that's always distributed as part of stdlib and thus is kept in exact sync with the ABI of the Python version it comes with. Correct me if I'm wrong.
That said, I won't object to some refactoring if it improves the code. But when such large changes are proposed, I really prefer to see small, incremental patches that replace just a part of the code. Such patches should come with an explanation of why the change is made (i.e. which part of PEP-384 does it adhere to).
pitrou commentedon Aug 6, 2013
Here is a simplified patch tackling only the PEP-3121 compliance. Eli, I think this would be good to go.
elibendersky commentedon Aug 6, 2013
Bless you Antoine, I've been just planning to do this myself to tackle the re-importing troubles I was having in tests the other day :-)
I'll take a look at this soon, promise!
elibendersky commentedon Aug 7, 2013
Antoine, some questions about the patch:
First, I think it omits expat_capi from the state. Is that intentional?
Second, I'm not sure if this approach is fully aligned with PEP-3121. A global, shared state is still used. Instead of actually having a different module state per subinterpreter, this patch will have shared state. Another problem seems to be using PyModule_FindModule without using PyModule_AddModule first.
These problems could be shared to all of Robin's original patches. Of course, there's also the possibility that I don't fully understand PEP-3121 yet :)
pitrou commentedon Aug 7, 2013
What would it do in the state? There's nothing to release.
I don't understand what you are talking about. Perhaps you haven't looked
what PyState_FindModule() does?
elibendersky commentedon Aug 7, 2013
On Wed, Aug 7, 2013 at 6:28 AM, Antoine Pitrou <report@bugs.python.org>wrote:
That's true, but I thought one of the goals of PEP-3121 is to separate
states between sub-interpreters. So that one can't corrupt another. I'm not
sure how much it matters in practice in this case of the pyexpat capsule;
need to look into it more.
I did not look at the implementation yet. But the documentation says:
"""Returns the module object that was created from *def* for the current
interpreter. This method requires that the module object has been attached
to the interpreter state with
PyState_AddModule()<http://docs.python.org/dev/c-api/module.html?highlight=pymoduledef_base#PyState_AddModule\>beforehand.
In case the corresponding module object is not found or has not
been attached to the interpreter state yet, it returns NULL."""
I don't see a call to PyState_AddModule. What am I missing?
pitrou commentedon Aug 7, 2013
pyexpat's "capi" object is a static struct inside pyexpat.c, so that
wouldn't change anything.
Separating states between sub-interpreters only matters when said state
is mutable, which it isn't here.
It is called implicitly when an extension module is imported.
elibendersky commentedon Aug 8, 2013
Thanks Antoine. I think I understand the patch better now. Just a couple small questions and otherwise LGTM
This code in the beginning in PyInit__elementtree:
Can you explain what use case it tries to cover? I couldn't find similar code in other modules we have that implement PEP-3121 (_csv, readline, io, etc.)
This code has at least one adverse effect, for testing. The problem with re-importing _elementtree I raised in http://mail.python.org/pipermail/python-dev/2013-August/127766.html is solved by moving to PEP-3121, but this piece of code above ruins it. This is because I want to set sys.modules['pyexpat'] = None and re-import _elementtree (this is what support.import_fresh_module does). But with this code in place, if _elementtree was imported any time in the past (say, in a previous test), I'll just get the instance back without attempting to do the full module initialization.
Do you think this should be documented in the C API docs? The way they read now, it seems that calling PyState_AddModule is needed manually by extension writers.
3 remaining items
pitrou commentedon Aug 8, 2013
Yeah, I think you're right. I'll submit an updated patch or, if it's
the only issue with it, perhaps you can simply remove the 3 offending
lines?
elibendersky commentedon Aug 8, 2013
On Thu, Aug 8, 2013 at 7:14 AM, Antoine Pitrou <report@bugs.python.org>wrote:
Sure, I'll do that. Thanks.
python-dev commentedon Aug 10, 2013
New changeset 8a060e2de608 by Eli Bendersky in branch 'default':
Issue bpo-15651: PEP-3121 refactoring for _elementtree
http://hg.python.org/cpython/rev/8a060e2de608
elibendersky commentedon Aug 10, 2013
Antoine, I committed your patch (with a bit of comments added), *leaving the module caching in*. This is because removing it breaks the tests, unfortunately. The _elementtree tests are so crooked that they manage to create a situation in which the module under test throws ParseError which is a different class from ET.ParseError. This is "achieved" by multiple invocations of import_fresh_module with various fresh & blocked parameters, and I still haven't fully traced all the problems yet. Since this caching only potentially harms other tests, it's ok to leave in.
A longer term solution to all this will be http://mail.python.org/pipermail/python-dev/2013-August/127766.html - I want to eventually run all "monkey-patch the import environment to simulate some situation" sub-tests of ET in different subprocesses to they are kept independent.
pitrou commentedon Aug 10, 2013
Thanks!
I find it useful that the test suite stresses module unloading or
reloading. There's probably a bug either in ET or in the ET tests. I'm
not saying it's a very important issue of course, but IMHO it would be
better if we don't try to swipe it under the carpet :-)
elibendersky commentedon Aug 10, 2013
On Sat, Aug 10, 2013 at 10:38 AM, Antoine Pitrou <report@bugs.python.org>wrote:
I have no intention swiping things under the carpet. I'll get to the bottom
of this to understand the exact flow that causes this to happen.
But I still think ET tests should be logically separated into subprocesses.
If we want to stress test module unloading and reloading, let's have
specific, targeted tests for that.
pitrou commentedon Aug 10, 2013
Agreed.
elibendersky commentedon Aug 11, 2013
Found some problems in the interaction of PEP-3121 and import_fresh_module: http://mail.python.org/pipermail/python-dev/2013-August/127862.html
I'd still like to see the in-PyInit__elementtree caching deleted eventually, without harming test coverage. So this issue will remain open for now, until we decide what to do.
elibendersky commentedon Aug 11, 2013
BTW, Antoine, w.r.t documentation - I agree that the whole PyState_* sequence needs better documentation and examples, but in the meantime I'm attaching a simple patch for c-api/module.rst. It clarifies that PyState_AddModule doesn't really have to be called explicitly in extensions.
vstinner commentedon Dec 18, 2020
Fixed by:
commit a6109ef
Author: Erlend Egeberg Aasland <erlend.aasland@innova.no>
Date: Fri Nov 20 13:36:23 2020 +0100
vstinner commentedon Dec 18, 2020
Oops, I commented the wrong issue. I reopen it.