Skip to content

Commit

Permalink
pythongh-101865: Remove deprecated co_lnotab from code objects as p…
Browse files Browse the repository at this point in the history
…er PEP626
  • Loading branch information
sobolevn committed Feb 21, 2023
1 parent 6ef6915 commit 06a79d9
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 340 deletions.
4 changes: 0 additions & 4 deletions Doc/library/inspect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
| | | read more :ref:`here |
| | | <inspect-module-co-flags>`|
+-----------+-------------------+---------------------------+
| | co_lnotab | encoded mapping of line |
| | | numbers to bytecode |
| | | indices |
+-----------+-------------------+---------------------------+
| | co_freevars | tuple of names of free |
| | | variables (referenced via |
| | | a function's closure) |
Expand Down
5 changes: 1 addition & 4 deletions Doc/reference/datamodel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,6 @@ Internal types
single: co_filename (code object attribute)
single: co_firstlineno (code object attribute)
single: co_flags (code object attribute)
single: co_lnotab (code object attribute)
single: co_name (code object attribute)
single: co_names (code object attribute)
single: co_nlocals (code object attribute)
Expand All @@ -989,9 +988,7 @@ Internal types
a tuple containing the literals used by the bytecode; :attr:`co_names` is
a tuple containing the names used by the bytecode; :attr:`co_filename` is
the filename from which the code was compiled; :attr:`co_firstlineno` is
the first line number of the function; :attr:`co_lnotab` is a string
encoding the mapping from bytecode offsets to line numbers (for details
see the source code of the interpreter); :attr:`co_stacksize` is the
the first line number of the function; :attr:`co_stacksize` is the
required stack size; :attr:`co_flags` is an integer encoding a number
of flags for the interpreter.

Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,10 @@ Removed
*context* parameter instead.
(Contributed by Victor Stinner in :gh:`94172`.)

* Remove ``co_lnotab`` attribute from code objects per :pep:`626`:
use ``co_lines`` method instead.
(Contributed by Nikita Sobolev in :gh:`101865`.)


Porting to Python 3.12
======================
Expand Down
2 changes: 0 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,6 @@ def iscode(object):
co_freevars tuple of names of free variables
co_posonlyargcount number of positional only arguments
co_kwonlyargcount number of keyword only arguments (not including ** arg)
co_lnotab encoded mapping of line numbers to bytecode indices
co_name name with which this code object was defined
co_names tuple of names other than arguments and function locals
co_nlocals number of local variables
Expand Down Expand Up @@ -1708,7 +1707,6 @@ def getframeinfo(frame, context=1):

def getlineno(frame):
"""Get the line number from a frame object, allowing for optimization."""
# FrameType.f_lineno is now a descriptor that grovels co_lnotab
return frame.f_lineno

_FrameInfo = namedtuple('_FrameInfo', ('frame',) + Traceback._fields)
Expand Down
13 changes: 5 additions & 8 deletions Lib/pydoc_data/topics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Autogenerated by Sphinx on Tue Feb 7 13:18:04 2023
# Autogenerated by Sphinx on Mon Feb 13 10:21:05 2023
topics = {'assert': 'The "assert" statement\n'
'**********************\n'
'\n'
Expand Down Expand Up @@ -13704,13 +13704,10 @@
' bytecode; "co_filename" is the filename from which the code '
'was\n'
' compiled; "co_firstlineno" is the first line number of the\n'
' function; "co_lnotab" is a string encoding the mapping from\n'
' bytecode offsets to line numbers (for details see the source\n'
' code of the interpreter); "co_stacksize" is the required '
'stack\n'
' size; "co_flags" is an integer encoding a number of flags '
'for\n'
' the interpreter.\n'
' function; "co_stacksize" is the required stack size; '
'"co_flags"\n'
' is an integer encoding a number of flags for the '
'interpreter.\n'
'\n'
' The following flag bits are defined for "co_flags": bit '
'"0x04"\n'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove deprecated ``co_lnotab`` from code objects as per :pep:`626`.
20 changes: 1 addition & 19 deletions Misc/gdbinit
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,7 @@ end
# A rewrite of the Python interpreter's line number calculator in GDB's
# command language
define lineno
set $__continue = 1
set $__co = f->f_code
set $__lasti = f->f_lasti
set $__sz = ((PyVarObject *)$__co->co_lnotab)->ob_size/2
set $__p = (unsigned char *)((PyBytesObject *)$__co->co_lnotab)->ob_sval
set $__li = $__co->co_firstlineno
set $__ad = 0
while ($__sz-1 >= 0 && $__continue)
set $__sz = $__sz - 1
set $__ad = $__ad + *$__p
set $__p = $__p + 1
if ($__ad > $__lasti)
set $__continue = 0
else
set $__li = $__li + *$__p
set $__p = $__p + 1
end
end
printf "%d", $__li
printf "%d", f->f_lineno
end

define pyframev
Expand Down
78 changes: 0 additions & 78 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1097,77 +1097,6 @@ _PyLineTable_NextAddressRange(PyCodeAddressRange *range)
return 1;
}

static int
emit_pair(PyObject **bytes, int *offset, int a, int b)
{
Py_ssize_t len = PyBytes_GET_SIZE(*bytes);
if (*offset + 2 >= len) {
if (_PyBytes_Resize(bytes, len * 2) < 0)
return 0;
}
unsigned char *lnotab = (unsigned char *) PyBytes_AS_STRING(*bytes);
lnotab += *offset;
*lnotab++ = a;
*lnotab++ = b;
*offset += 2;
return 1;
}

static int
emit_delta(PyObject **bytes, int bdelta, int ldelta, int *offset)
{
while (bdelta > 255) {
if (!emit_pair(bytes, offset, 255, 0)) {
return 0;
}
bdelta -= 255;
}
while (ldelta > 127) {
if (!emit_pair(bytes, offset, bdelta, 127)) {
return 0;
}
bdelta = 0;
ldelta -= 127;
}
while (ldelta < -128) {
if (!emit_pair(bytes, offset, bdelta, -128)) {
return 0;
}
bdelta = 0;
ldelta += 128;
}
return emit_pair(bytes, offset, bdelta, ldelta);
}

static PyObject *
decode_linetable(PyCodeObject *code)
{
PyCodeAddressRange bounds;
PyObject *bytes;
int table_offset = 0;
int code_offset = 0;
int line = code->co_firstlineno;
bytes = PyBytes_FromStringAndSize(NULL, 64);
if (bytes == NULL) {
return NULL;
}
_PyCode_InitAddressRange(code, &bounds);
while (_PyLineTable_NextAddressRange(&bounds)) {
if (bounds.opaque.computed_line != line) {
int bdelta = bounds.ar_start - code_offset;
int ldelta = bounds.opaque.computed_line - line;
if (!emit_delta(&bytes, bdelta, ldelta, &table_offset)) {
Py_DECREF(bytes);
return NULL;
}
code_offset = bounds.ar_start;
line = bounds.opaque.computed_line;
}
}
_PyBytes_Resize(&bytes, table_offset);
return bytes;
}


typedef struct {
PyObject_HEAD
Expand Down Expand Up @@ -1881,12 +1810,6 @@ static PyMemberDef code_memberlist[] = {
};


static PyObject *
code_getlnotab(PyCodeObject *code, void *closure)
{
return decode_linetable(code);
}

static PyObject *
code_getvarnames(PyCodeObject *code, void *closure)
{
Expand Down Expand Up @@ -1919,7 +1842,6 @@ code_getcode(PyCodeObject *code, void *closure)
}

static PyGetSetDef code_getsetlist[] = {
{"co_lnotab", (getter)code_getlnotab, NULL, NULL},
{"_co_code_adaptive", (getter)code_getcodeadaptive, NULL, NULL},
// The following old names are kept for backward compatibility.
{"co_varnames", (getter)code_getvarnames, NULL, NULL},
Expand Down
Loading

0 comments on commit 06a79d9

Please sign in to comment.