[Python-checkins] r84138 - in python/branches/py3k: Lib/dis.py Lib/inspect.py Misc/NEWS

nick.coghlan python-checkins at python.org
Tue Aug 17 12:18:16 CEST 2010


Author: nick.coghlan
Date: Tue Aug 17 12:18:16 2010
New Revision: 84138

Log:
Address XXX comment in dis.py: inspect.py now attempts to reuse the dis.py compiler flag values before resorting to defining its own

Modified:
   python/branches/py3k/Lib/dis.py
   python/branches/py3k/Lib/inspect.py
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Lib/dis.py
==============================================================================
--- python/branches/py3k/Lib/dis.py	(original)
+++ python/branches/py3k/Lib/dis.py	Tue Aug 17 12:18:16 2010
@@ -68,9 +68,10 @@
         while tb.tb_next: tb = tb.tb_next
     disassemble(tb.tb_frame.f_code, tb.tb_lasti)
 
-# XXX This duplicates information from code.h, also duplicated in inspect.py.
-# XXX Maybe this ought to be put in a central location, like opcode.py?
-flag2name = {
+# The inspect module interrogates this dictionary to build its
+# list of CO_* constants. It is also used by pretty_flags to
+# turn the co_flags field into a human readable list.
+COMPILER_FLAG_NAMES = {
      1: "OPTIMIZED",
      2: "NEWLOCALS",
      4: "VARARGS",
@@ -86,7 +87,7 @@
     for i in range(32):
         flag = 1<<i
         if flags & flag:
-            names.append(flag2name.get(flag, hex(flag)))
+            names.append(COMPILER_FLAG_NAMES.get(flag, hex(flag)))
             flags ^= flag
             if not flags:
                 break

Modified: python/branches/py3k/Lib/inspect.py
==============================================================================
--- python/branches/py3k/Lib/inspect.py	(original)
+++ python/branches/py3k/Lib/inspect.py	Tue Aug 17 12:18:16 2010
@@ -36,15 +36,25 @@
 import itertools
 import string
 import re
-import dis
 import imp
 import tokenize
 import linecache
 from operator import attrgetter
 from collections import namedtuple
-# These constants are from Include/code.h.
-CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS = 0x1, 0x2, 0x4, 0x8
-CO_NESTED, CO_GENERATOR, CO_NOFREE = 0x10, 0x20, 0x40
+
+# Create constants for the compiler flags in Include/code.h
+# We try to get them from dis to avoid duplication, but fall
+# back to hardcording so the dependency is optional
+try:
+    from dis import COMPILER_FLAG_NAMES as _flag_names
+except ImportError:
+    CO_OPTIMIZED, CO_NEWLOCALS = 0x1, 0x2
+    CO_VARARGS, CO_VARKEYWORDS = 0x4, 0x8
+    CO_NESTED, CO_GENERATOR, CO_NOFREE = 0x10, 0x20, 0x40
+else:
+    mod_dict = globals()
+    for k, v in _flag_names.items():
+        mod_dict["CO_" + v] = k
 
 # See Include/object.h
 TPFLAGS_IS_ABSTRACT = 1 << 20

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Tue Aug 17 12:18:16 2010
@@ -90,6 +90,9 @@
 Library
 -------
 
+- Address XXX comment in dis.py by having inspect.py prefer to reuse the
+  dis.py compiler flag values over defining its own
+
 - Issue #9147: Added dis.code_info() which is similar to show_code()
   but returns formatted code information in a string rather than
   displaying on screen.


More information about the Python-checkins mailing list