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?

On Wed, Feb 9, 2022 at 9:41 AM Pablo Galindo Salgado <> wrote:
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).

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.

> so we don't want (for now) to ad
> getters/setters. If you want to get the position of a instruction, you can
> use PyCode_Addr2Location

What Cython needs is the other direction. How can we provide the current
source position range for a given piece of code to an exception?

As it stands, the way to do this is to copy the implementation details of
CPython into Cython in order to let it expose the specific data structures
that CPython uses for its internal representation of code positions.

I would prefer using an API instead that allows exposing this mapping
directly to CPython's traceback handling, rather than having to emulate
byte code positions. While that would probably be quite doable, it's far
from a nice interface for something that is not based on byte code.

And that's not just a Cython issue. The same applies to Domain Specific
Languages or other programming languages that integrate with Python and
want to show users code positions for their source code.


Python-Dev mailing list --
To unsubscribe send an email to
Message archived at
Code of Conduct:
Python-Dev mailing list --
To unsubscribe send an email to
Message archived at
Code of Conduct:

--Guido van Rossum (
Pronouns: he/him (why is my pronoun here?)