Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 09c8123

Browse files
committed
Address XXX comment in dis.py: inspect.py now attempts to reuse the dis.py compiler flag values before resorting to defining its own
1 parent 77203ad commit 09c8123

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

Lib/dis.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ def distb(tb=None):
6868
while tb.tb_next: tb = tb.tb_next
6969
disassemble(tb.tb_frame.f_code, tb.tb_lasti)
7070

71-
# XXX This duplicates information from code.h, also duplicated in inspect.py.
72-
# XXX Maybe this ought to be put in a central location, like opcode.py?
73-
flag2name = {
71+
# The inspect module interrogates this dictionary to build its
72+
# list of CO_* constants. It is also used by pretty_flags to
73+
# turn the co_flags field into a human readable list.
74+
COMPILER_FLAG_NAMES = {
7475
1: "OPTIMIZED",
7576
2: "NEWLOCALS",
7677
4: "VARARGS",
@@ -86,7 +87,7 @@ def pretty_flags(flags):
8687
for i in range(32):
8788
flag = 1<<i
8889
if flags & flag:
89-
names.append(flag2name.get(flag, hex(flag)))
90+
names.append(COMPILER_FLAG_NAMES.get(flag, hex(flag)))
9091
flags ^= flag
9192
if not flags:
9293
break

Lib/inspect.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,25 @@
3636
import itertools
3737
import string
3838
import re
39-
import dis
4039
import imp
4140
import tokenize
4241
import linecache
4342
from operator import attrgetter
4443
from collections import namedtuple
45-
# These constants are from Include/code.h.
46-
CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS = 0x1, 0x2, 0x4, 0x8
47-
CO_NESTED, CO_GENERATOR, CO_NOFREE = 0x10, 0x20, 0x40
44+
45+
# Create constants for the compiler flags in Include/code.h
46+
# We try to get them from dis to avoid duplication, but fall
47+
# back to hardcording so the dependency is optional
48+
try:
49+
from dis import COMPILER_FLAG_NAMES as _flag_names
50+
except ImportError:
51+
CO_OPTIMIZED, CO_NEWLOCALS = 0x1, 0x2
52+
CO_VARARGS, CO_VARKEYWORDS = 0x4, 0x8
53+
CO_NESTED, CO_GENERATOR, CO_NOFREE = 0x10, 0x20, 0x40
54+
else:
55+
mod_dict = globals()
56+
for k, v in _flag_names.items():
57+
mod_dict["CO_" + v] = k
4858

4959
# See Include/object.h
5060
TPFLAGS_IS_ABSTRACT = 1 << 20

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ Extensions
9090
Library
9191
-------
9292

93+
- Address XXX comment in dis.py by having inspect.py prefer to reuse the
94+
dis.py compiler flag values over defining its own
95+
9396
- Issue #9147: Added dis.code_info() which is similar to show_code()
9497
but returns formatted code information in a string rather than
9598
displaying on screen.

0 commit comments

Comments
 (0)