Guido van Rossum schrieb am 09.02.22 um 19:36:
On Wed, Feb 9, 2022 at 9:41 AM Pablo Galindo Salgado wrote:
On Wed, 9 Feb 2022 at 17:38, Stefan Behnel wrote:
Pablo Galindo Salgado schrieb am 09.02.22 um 17:40:
Should there be a getter/setter for co_positions?
We consider the representation of co_postions private
Yes, and that's the issue.
I can only say that currently, I am not confident to expose such an API,
at least for co_positions, as the internal implementation is very likely to
heavily change and we want to have the possibility of changing it between
patch versions if required (to address bugs and other things like that).
>
> It might require a detailed API design proposal coming from outside
> CPython
> (e.g. from Cython) to get this to change. I imagine for co_positions in
> particular this would have to use a "builder" pattern.
>
> I am unclear on how this would work though, given that Cython generates C
> code, not CPython bytecode. How would the synthesized co_positions be
> used?
> Would Cython just generate a co_positions fragment at the moment an
> exception is raised, pointing at the .pyx file from which the code was
> generated?
So, what we currently do is to update the line number (which IIRC is really
the start line number of the current function) on the current frame when an
exception is raised, and the byte code offset to 0. That's a hack but shows
the correct code line in the traceback. Probably conflicts with pdb, but
there are still other issues with that anyway.
I remember looking into the old lnotab mapping at some point and trying to
implement that with fake byte code offsets but never got it finished.
The idea is pretty simple, though. Instead of byte code offsets, we'd count
our syntax tree nodes and just store the code position range of each syntax
node at the "byte code offset" of the node's counter number. That's
probably fairly easy to do in C code, maybe even with a statically
allocated data structure. Then, instead of setting the frame function's
line number, we'd set the frame's byte code instruction counter to the
number of the failing syntax node, and CPython would retrieve the code
position from that offset.
That sounds simple enough, probably simpler than any API usage – but
depends on implementation details.
Especially the idea of storing all this statically in the data segment of
the shared library sounds very tempting.
Stefan
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/python-dev@python.org/message/GAJFB6ABFYXF3RFXFDQ3YUZD23FMXPEY/
Code of Conduct: http://python.org/psf/codeofconduct/