[Python-checkins] gh-101865: Deprecate `co_lnotab` from code objects as per PEP 626 (#101866)

ambv webhook-mailer at python.org
Mon Apr 3 11:35:12 EDT 2023


https://github.com/python/cpython/commit/2a721258a199e9bcdcee2069719ad9c8f8c0d030
commit: 2a721258a199e9bcdcee2069719ad9c8f8c0d030
branch: main
author: Nikita Sobolev <mail at sobolevn.me>
committer: ambv <lukasz at langa.pl>
date: 2023-04-03T17:35:04+02:00
summary:

gh-101865: Deprecate `co_lnotab` from code objects as per PEP 626 (#101866)

Co-authored-by: Oleg Iarygin <oleg at arhadthedev.net>

files:
A Misc/NEWS.d/next/Core and Builtins/2023-02-21-17-22-06.gh-issue-101865.fwrTOA.rst
M Doc/reference/datamodel.rst
M Doc/whatsnew/3.12.rst
M Lib/test/test_code.py
M Objects/codeobject.c

diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
index 1865d09fcaa1..a09d5529c8c6 100644
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -991,7 +991,8 @@ Internal types
       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
+      see the source code of the interpreter, is deprecated since 3.12
+      and may be removed in 3.14); :attr:`co_stacksize` is the
       required stack size; :attr:`co_flags` is an integer encoding a number
       of flags for the interpreter.
 
diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst
index 3df3ef711a90..88b99828f0be 100644
--- a/Doc/whatsnew/3.12.rst
+++ b/Doc/whatsnew/3.12.rst
@@ -622,6 +622,12 @@ Pending Removal in Python 3.14
   functions that have been deprecated since Python 2 but only gained a
   proper :exc:`DeprecationWarning` in 3.12. Remove them in 3.14.
 
+* Accessing ``co_lnotab`` was deprecated in :pep:`626` since 3.10
+  and was planned to be removed in 3.12
+  but it only got a proper :exc:`DeprecationWarning` in 3.12.
+  May be removed in 3.14.
+  (Contributed by Nikita Sobolev in :gh:`101866`.)
+
 * The *onerror* argument of :func:`shutil.rmtree` is deprecated in 3.12,
   and will be removed in 3.14.
 
diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py
index 0cd1fb3f9728..7543c9ab3421 100644
--- a/Lib/test/test_code.py
+++ b/Lib/test/test_code.py
@@ -338,6 +338,13 @@ def func():
         new_code = code = func.__code__.replace(co_linetable=b'')
         self.assertEqual(list(new_code.co_lines()), [])
 
+    def test_co_lnotab_is_deprecated(self):  # TODO: remove in 3.14
+        def func():
+            pass
+
+        with self.assertWarns(DeprecationWarning):
+            func.__code__.co_lnotab
+
     def test_invalid_bytecode(self):
         def foo():
             pass
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-02-21-17-22-06.gh-issue-101865.fwrTOA.rst b/Misc/NEWS.d/next/Core and Builtins/2023-02-21-17-22-06.gh-issue-101865.fwrTOA.rst
new file mode 100644
index 000000000000..876cc223a0e7
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-02-21-17-22-06.gh-issue-101865.fwrTOA.rst	
@@ -0,0 +1,2 @@
+Deprecate ``co_lnotab`` in code objects, schedule it for removal in Python
+3.14
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 65b1d258fb76..755d0b85e7cf 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -1921,6 +1921,11 @@ static PyMemberDef code_memberlist[] = {
 static PyObject *
 code_getlnotab(PyCodeObject *code, void *closure)
 {
+    if (PyErr_WarnEx(PyExc_DeprecationWarning,
+                     "co_lnotab is deprecated, use co_lines instead.",
+                     1) < 0) {
+        return NULL;
+    }
     return decode_linetable(code);
 }
 



More information about the Python-checkins mailing list