[Python-checkins] bpo-45168: change dis output to omit missing values rather than replacing them by their index (GH-28313)

iritkatriel webhook-mailer at python.org
Tue Sep 14 05:09:09 EDT 2021


https://github.com/python/cpython/commit/c99fc4e53a60084df88ac5c69b3b13bc033677e1
commit: c99fc4e53a60084df88ac5c69b3b13bc033677e1
branch: main
author: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>
committer: iritkatriel <1055913+iritkatriel at users.noreply.github.com>
date: 2021-09-14T10:09:05+01:00
summary:

bpo-45168: change dis output to omit missing values rather than replacing them by their index (GH-28313)

files:
A Misc/NEWS.d/next/Library/2021-09-13-14-28-49.bpo-45168.Z1mfW4.rst
M Doc/library/dis.rst
M Lib/dis.py
M Lib/test/test_dis.py

diff --git a/Doc/library/dis.rst b/Doc/library/dis.rst
index 21747069b3a19..9b683083f6b62 100644
--- a/Doc/library/dis.rst
+++ b/Doc/library/dis.rst
@@ -293,12 +293,13 @@ details of bytecode instructions as :class:`Instruction` instances:
 
    .. data:: argval
 
-      resolved arg value (if known), otherwise same as arg
+      resolved arg value (if any), otherwise ``None``
 
 
    .. data:: argrepr
 
-      human readable description of operation argument
+      human readable description of operation argument (if any),
+      otherwise an empty string.
 
 
    .. data:: offset
diff --git a/Lib/dis.py b/Lib/dis.py
index a073572e59e66..b9f8658100884 100644
--- a/Lib/dis.py
+++ b/Lib/dis.py
@@ -125,6 +125,13 @@ def pretty_flags(flags):
         names.append(hex(flags))
     return ", ".join(names)
 
+class _Unknown:
+    def __repr__(self):
+        return "<unknown>"
+
+# Sentinel to represent values that cannot be calculated
+UNKNOWN = _Unknown()
+
 def _get_code_object(x):
     """Helper to handle methods, compiled or raw code objects, and strings."""
     # Extract functions from methods.
@@ -315,28 +322,28 @@ def _get_const_info(const_index, const_list):
 
        Returns the dereferenced constant and its repr if the constant
        list is defined.
-       Otherwise returns the constant index and its repr().
+       Otherwise returns the sentinel value dis.UNKNOWN for the value
+       and an empty string for its repr.
     """
-    argval = const_index
     if const_list is not None:
         argval = const_list[const_index]
-    return argval, repr(argval)
+        return argval, repr(argval)
+    else:
+        return UNKNOWN, ''
 
 def _get_name_info(name_index, get_name, **extrainfo):
     """Helper to get optional details about named references
 
        Returns the dereferenced name as both value and repr if the name
        list is defined.
-       Otherwise returns the name index and its repr().
+       Otherwise returns the sentinel value dis.UNKNOWN for the value
+       and an empty string for its repr.
     """
-    argval = name_index
     if get_name is not None:
         argval = get_name(name_index, **extrainfo)
-        argrepr = argval
+        return argval, argval
     else:
-        argrepr = repr(argval)
-    return argval, argrepr
-
+        return UNKNOWN, ''
 
 def parse_varint(iterator):
     b = next(iterator)
diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py
index a140a89f0e7e8..0899db66d8241 100644
--- a/Lib/test/test_dis.py
+++ b/Lib/test/test_dis.py
@@ -48,12 +48,12 @@ def cm(cls, x):
 """ % (_C.__init__.__code__.co_firstlineno + 1,)
 
 dis_c_instance_method_bytes = """\
-          0 LOAD_FAST                1 (1)
-          2 LOAD_CONST               1 (1)
+          0 LOAD_FAST                1
+          2 LOAD_CONST               1
           4 COMPARE_OP               2 (==)
-          6 LOAD_FAST                0 (0)
-          8 STORE_ATTR               0 (0)
-         10 LOAD_CONST               0 (0)
+          6 LOAD_FAST                0
+          8 STORE_ATTR               0
+         10 LOAD_CONST               0
          12 RETURN_VALUE
 """
 
@@ -105,11 +105,11 @@ def _f(a):
 
 
 dis_f_co_code = """\
-          0 LOAD_GLOBAL              0 (0)
-          2 LOAD_FAST                0 (0)
+          0 LOAD_GLOBAL              0
+          2 LOAD_FAST                0
           4 CALL_FUNCTION            1
           6 POP_TOP
-          8 LOAD_CONST               1 (1)
+          8 LOAD_CONST               1
          10 RETURN_VALUE
 """
 
diff --git a/Misc/NEWS.d/next/Library/2021-09-13-14-28-49.bpo-45168.Z1mfW4.rst b/Misc/NEWS.d/next/Library/2021-09-13-14-28-49.bpo-45168.Z1mfW4.rst
new file mode 100644
index 0000000000000..4e12e7d51e2c7
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-09-13-14-28-49.bpo-45168.Z1mfW4.rst
@@ -0,0 +1 @@
+Change :func:`dis.dis` output to omit op arg values that cannot be resolved due to ``co_consts``, ``co_names`` etc not being provided. Previously the oparg itself was repeated in the value field, which is not useful and can be confusing.



More information about the Python-checkins mailing list