PEP 626: Update PyLineTable_InitAddressRange spec (#1939)

* Add length parameter to PyLineTable_InitAddressRange. Allows line tables that are not sentinel terminated and makes the extent of the line table explicit.

* Make opaque part of PyCodeAddressRange more explicitly opaque.
This commit is contained in:
Mark Shannon 2021-04-28 16:15:40 +01:00 committed by GitHub
parent 9d5ca8ff2c
commit c176458336
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 8 deletions

View File

@ -282,11 +282,10 @@ Note that these functions are not part of the C-API, so will be need to be linke
int ar_start;
int ar_end;
int ar_line;
int opaque1;
void *opaque2;
struct _opaque opaque;
} PyCodeAddressRange;
void PyLineTable_InitAddressRange(char *linetable, int firstlineno, PyCodeAddressRange *range);
void PyLineTable_InitAddressRange(char *linetable, Py_ssize_t length, int firstlineno, PyCodeAddressRange *range);
int PyLineTable_NextAddressRange(PyCodeAddressRange *range);
int PyLineTable_PreviousAddressRange(PyCodeAddressRange *range);
@ -300,18 +299,22 @@ Note that these functions are not part of the C-API, so will be need to be linke
The data in ``linetable`` is immutable, but its lifetime depends on its code object.
For reliable operation, ``linetable`` should be copied into a local buffer before calling ``PyLineTable_InitAddressRange``.
Although these functions are not part of C-API, they will provided by all future versions of CPython.
The ``PyLineTable_`` functions do not call into the C-API, so can be safely copied into any tool that needs to use them.
The ``PyCodeAddressRange`` struct may acquire additional ``opaque`` fields in future versions, but the ``ar_`` fields will remain unchanged.
Although these functions are not part of C-API, they will provided by all future versions of CPython.
The ``PyLineTable_`` functions do not call into the C-API, so can be safely copied into any tool that needs to use them.
The ``PyCodeAddressRange`` struct will not be changed, but the ``_opaque`` struct is not part of the specification and may change.
.. note::
The ``PyCodeAddressRange`` struct has changed from the original version of this PEP, where the addition fields were defined, but
were liable to change.
For example, the following code prints out all the address ranges:
::
void print_address_ranges(char *linetable, int firstlineno)
void print_address_ranges(char *linetable, Py_ssize_t length, int firstlineno)
{
PyCodeAddressRange range;
PyLineTable_InitAddressRange(linetable, firstlineno, &range);
PyLineTable_InitAddressRange(linetable, length, firstlineno, &range);
while (PyLineTable_NextAddressRange(&range)) {
printf("Bytecodes from %d (inclusive) to %d (exclusive) ",
range.start, range.end);