X Tutup
Skip to content

Commit 2e91de8

Browse files
committed
Start using our replacement for inspect.iscode
1 parent 5bbe2c4 commit 2e91de8

File tree

10 files changed

+90
-64
lines changed

10 files changed

+90
-64
lines changed

uncompyle6/code.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import inspect
2+
class Code3:
3+
"""Class for a Python3 code object used when a Python interpreter less than 3 is
4+
working on Python3 bytecode
5+
"""
6+
def __init__(self, co_argcount, co_kwonlyargcount,co_nlocals, co_stacksize, co_flags, co_code,
7+
co_consts, co_names, co_varnames, co_filename, co_name,
8+
co_firstlineno, co_lnotab, co_freevars, co_cellvars):
9+
self.co_argcount = co_argcount
10+
self.co_kwonlyargcount = co_kwonlyargcount
11+
self.co_nlocals = co_nlocals
12+
self.co_stacksize = co_stacksize
13+
self.co_flags = co_flags
14+
self.co_code = co_code
15+
self.co_consts = co_consts
16+
self.co_names = co_names
17+
self.co_varnames = co_varnames
18+
self.co_filename = co_filename
19+
self.co_name = co_name
20+
self.co_firstlineno = co_firstlineno
21+
self.co_lnotab = co_lnotab
22+
self.co_freevars = co_freevars
23+
self.co_cellvars = co_cellvars
24+
25+
class Code2:
26+
"""Class for a Python2 code object used when a Python interpreter less than 3 is
27+
working on Python3 bytecode
28+
"""
29+
def __init__(self, co_argcount, co_kwonlyargcount,co_nlocals, co_stacksize, co_flags, co_code,
30+
co_consts, co_names, co_varnames, co_filename, co_name,
31+
co_firstlineno, co_lnotab, co_freevars, co_cellvars):
32+
self.co_argcount = co_argcount
33+
self.co_kwonlyargcount = co_kwonlyargcount
34+
self.co_nlocals = co_nlocals
35+
self.co_stacksize = co_stacksize
36+
self.co_flags = co_flags
37+
self.co_code = co_code
38+
self.co_consts = co_consts
39+
self.co_names = co_names
40+
self.co_varnames = co_varnames
41+
self.co_filename = co_filename
42+
self.co_name = co_name
43+
self.co_firstlineno = co_firstlineno
44+
self.co_lnotab = co_lnotab
45+
self.co_freevars = co_freevars
46+
self.co_cellvars = co_cellvars
47+
48+
def iscode(obj):
49+
"""A replacement for inspect.iscode() which we can't used because we may be
50+
using a different version of Python than the version of Python used
51+
in creating the byte-compiled objects. Here, he code types may mismatch.
52+
"""
53+
return inspect.iscode(obj) or isinstance(obj, Code3)

uncompyle6/disas.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Copyright (c) 1999 John Aycock
2-
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
3-
# Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org>
41
# Copyright (c) 2015 by Rocky Bernstein
2+
# Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org>
3+
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
4+
# Copyright (c) 1999 John Aycock
55

66
"""
77
CPython magic- and version- independent disassembly routines
@@ -21,15 +21,16 @@
2121
import inspect, os, sys
2222

2323
import uncompyle6
24-
from uncompyle6.scanner import get_scanner
24+
from uncompyle6.code import iscode
2525
from uncompyle6.load import check_object_path, load_module
26+
from uncompyle6.scanner import get_scanner
2627

2728
def disco(version, co, out=None):
2829
"""
2930
diassembles and deparses a given code block 'co'
3031
"""
3132

32-
assert hasattr(co, 'co_name')
33+
assert iscode(co)
3334

3435
# store final output stream for case of error
3536
real_out = out or sys.stdout

uncompyle6/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from __future__ import print_function
22
import datetime, inspect, os, sys
33

4-
from uncompyle6.disas import check_object_path
54
from uncompyle6 import verify, PYTHON_VERSION
5+
from uncompyle6.code import iscode
6+
from uncompyle6.disas import check_object_path
67
from uncompyle6.semantics import pysource
78

89
from uncompyle6.load import load_module
@@ -13,7 +14,7 @@ def uncompyle(version, co, out=None, showasm=False, showast=False,
1314
disassembles and deparses a given code block 'co'
1415
"""
1516

16-
assert hasattr(co, 'co_name')
17+
assert iscode(co)
1718

1819
# store final output stream for case of error
1920
real_out = out or sys.stdout

uncompyle6/marsh.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import uncompyle6.scanners.scanner3 as scan3
2222
from uncompyle6.magics import PYTHON_MAGIC_INT
23+
from uncompyle6.code import Code3
2324

2425
internStrings = []
2526
internObjects = []
@@ -132,10 +133,10 @@ def load_code_type(fp, magic_int, bytes_for_s=False, code_objects={}):
132133
co_filename = str(co_filename)
133134
co_name = str(co_name)
134135
if 3020 < magic_int <= 20121:
135-
code = scan3.Code3(co_argcount, kwonlyargcount,
136-
co_nlocals, co_stacksize, co_flags, co_code,
137-
co_consts, co_names, co_varnames, co_filename, co_name,
138-
co_firstlineno, co_lnotab, co_freevars, co_cellvars)
136+
code = Code3(co_argcount, kwonlyargcount,
137+
co_nlocals, co_stacksize, co_flags, co_code,
138+
co_consts, co_names, co_varnames, co_filename, co_name,
139+
co_firstlineno, co_lnotab, co_freevars, co_cellvars)
139140
else:
140141
Code = types.CodeType
141142
code = Code(co_argcount, co_nlocals, co_stacksize, co_flags, co_code,

uncompyle6/scanners/scanner27.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
# Copyright (c) 2015 by Rocky Bernstein
2+
# Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org>
3+
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
14
# Copyright (c) 1999 John Aycock
2-
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
3-
# Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org>
4-
# Copyright (c) 2015 by Rocky Bernstein
55
"""
66
Python 2.7 bytecode scanner/deparser
77
@@ -17,6 +17,7 @@
1717
from collections import namedtuple
1818
from array import array
1919

20+
from uncompyle6.code import iscode
2021
from uncompyle6.opcodes.opcode_27 import * # NOQA
2122
import uncompyle6.scanner as scan
2223

@@ -138,11 +139,7 @@ def unmangle(name):
138139
continue
139140
if op in hasconst:
140141
const = co.co_consts[oparg]
141-
# We can't use inspect.iscode() because we may be
142-
# using a different version of Python than the
143-
# one that this was byte-compiled on. So the code
144-
# types may mismatch.
145-
if hasattr(const, 'co_name'):
142+
if iscode(const):
146143
oparg = const
147144
if const.co_name == '<lambda>':
148145
assert op_name == 'LOAD_CONST'

uncompyle6/scanners/scanner3.py

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from collections import namedtuple
1414
from array import array
1515

16+
from uncompyle6.code import iscode
1617
from uncompyle6.scanner import Token
1718
from uncompyle6 import PYTHON_VERSION, PYTHON3
1819

@@ -24,30 +25,6 @@
2425

2526
import uncompyle6.scanner as scan
2627

27-
class Code3:
28-
"""Class for a Python3 code object used when a Python interpreter less than 3 is
29-
working on Python3 bytecode
30-
"""
31-
def __init__(self, co_argcount, co_kwonlyargcount,co_nlocals, co_stacksize, co_flags, co_code,
32-
co_consts, co_names, co_varnames, co_filename, co_name,
33-
co_firstlineno, co_lnotab, co_freevars, co_cellvars):
34-
self.co_argcount = co_argcount
35-
self.co_kwonlyargcount = co_kwonlyargcount
36-
self.co_nlocals = co_nlocals
37-
self.co_stacksize = co_stacksize
38-
self.co_flags = co_flags
39-
self.co_code = co_code
40-
self.co_consts = co_consts
41-
self.co_names = co_names
42-
self.co_varnames = co_varnames
43-
self.co_filename = co_filename
44-
self.co_name = co_name
45-
self.co_firstlineno = co_firstlineno
46-
self.co_lnotab = co_lnotab
47-
self.co_freevars = co_freevars
48-
self.co_cellvars = co_cellvars
49-
50-
5128
class Scanner3(scan.Scanner):
5229

5330
def __init__(self):
@@ -145,11 +122,7 @@ def unmangle(name):
145122
if not PYTHON3 and isinstance(const, str):
146123
if const in code_objects:
147124
const = code_objects[const]
148-
# Not sure if'we can inspect.iscode() because we may be
149-
# using a different version of Python than the
150-
# one that this was byte-compiled on. Is probably okay,
151-
# but we'll use hasattr instead here.
152-
if hasattr(const, 'co_name'):
125+
if iscode(const):
153126
oparg = const
154127
if const.co_name == '<lambda>':
155128
assert op_name == 'LOAD_CONST'

uncompyle6/scanners/scanner34.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import uncompyle6.scanners.scanner3 as scan3
1616

1717
from uncompyle6 import PYTHON_VERSION
18+
from uncompyle6.code import iscode
1819
from uncompyle6.scanner import Token
1920

2021
# Get all the opcodes into globals
@@ -92,13 +93,9 @@ def unmangle(name):
9293
pattr = inst.argrepr
9394
opname = inst.opname
9495

95-
# For constants, the pattr is the same as attr. Using pattr adds
96-
# an extra level of quotes which messes other things up, like getting
97-
# keyword attribute names in a call. I suspect there will be things
98-
# other than LOAD_CONST, but we'll start out with just this for now.
9996
if opname in ['LOAD_CONST']:
10097
const = inst.argval
101-
if hasattr(const, 'co_name'):
98+
if iscode(const):
10299
if const.co_name == '<lambda>':
103100
opname = 'LOAD_LAMBDA'
104101
elif const.co_name == '<genexpr>':

uncompyle6/semantics/fragments.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import inspect, re, sys
3030

3131
from uncompyle6 import PYTHON3
32+
from uncompyle6.code import iscode
3233
from uncompyle6.semantics import pysource
3334
from uncompyle6.parser import get_python_parser
3435
from uncompyle6 import parser
@@ -482,7 +483,7 @@ def comprehension_walk(self, node, iter_index):
482483
self.prec = 27
483484
code = node[-5].attr
484485

485-
assert hasattr(co, 'co_name')
486+
assert iscode(co)
486487
code = Code(code, self.scanner, self.currentclass)
487488
# assert isinstance(code, Code)
488489

@@ -525,8 +526,8 @@ def listcomprehension_walk3(self, node, iter_index, code_index=-5):
525526
self.prec = 27
526527
code = node[code_index].attr
527528

528-
assert hasattr(code, 'co_name')
529-
## Or Code3
529+
assert iscode(code)
530+
# Or Code3
530531
code = Code(code, self.scanner, self.currentclass)
531532
# assert isinstance(code, Code)
532533

@@ -1246,7 +1247,7 @@ def build_param(ast, name, default):
12461247
def deparse_code(version, co, out=StringIO(), showasm=False, showast=False,
12471248
showgrammar=False):
12481249

1249-
assert hasattr(co, 'co_name')
1250+
assert iscode(co)
12501251
# store final output stream for case of error
12511252
scanner = get_scanner(version)
12521253

uncompyle6/semantics/pysource.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import inspect, sys, re
6868

6969
from uncompyle6 import PYTHON3
70+
from uncompyle6.code import iscode
7071
from uncompyle6.parser import get_python_parser
7172
from uncompyle6.parsers.astnode import AST
7273
from uncompyle6.parsers.spark import GenericASTTraversal, DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
@@ -980,7 +981,7 @@ def comprehension_walk(self, node, iter_index, code_index=-5):
980981
self.prec = 27
981982
code = node[code_index].attr
982983

983-
assert hasattr(code, 'co_name')
984+
assert iscode(code)
984985
code = Code(code, self.scanner, self.currentclass)
985986
ast = self.build_ast(code._tokens, code._customize)
986987
self.customize(code._customize)
@@ -1025,7 +1026,7 @@ def listcomprehension_walk3(self, node, iter_index, code_index=-5):
10251026
self.prec = 27
10261027
code = node[code_index].attr
10271028

1028-
assert hasattr(code, 'co_name')
1029+
assert iscode(code)
10291030
code = Code(code, self.scanner, self.currentclass)
10301031
# assert isinstance(code, Code)
10311032

@@ -1448,7 +1449,7 @@ def build_param(ast, name, default):
14481449
defparams = node[:node[-1].attr]
14491450
code = node[code_index].attr
14501451

1451-
assert hasattr(code, 'co_name')
1452+
assert iscode(code)
14521453
code = Code(code, self.scanner, self.currentclass)
14531454
# assert isinstance(code, Code)
14541455

@@ -1515,7 +1516,7 @@ def build_param(ast, name, default):
15151516
def build_class(self, code):
15161517
"""Dump class definition, doc string and class body."""
15171518

1518-
assert hasattr(code, 'co_name')
1519+
assert iscode(code)
15191520
self.classes.append(self.currentclass)
15201521
code = Code(code, self.scanner, self.currentclass)
15211522
# assert isinstance(code, Code)
@@ -1626,7 +1627,7 @@ def deparse_code(version, co, out=sys.stdout, showasm=False, showast=False,
16261627
disassembles and deparses a given code block 'co'
16271628
"""
16281629

1629-
assert hasattr(co, 'co_name')
1630+
assert iscode(co)
16301631
# store final output stream for case of error
16311632
scanner = get_scanner(version)
16321633

uncompyle6/verify.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import uncompyle6
1414
import uncompyle6.scanner as scanner
1515
from uncompyle6 import PYTHON3
16+
from uncompyle6.code import iscode
1617
from uncompyle6.magics import PYTHON_MAGIC_INT
1718
from uncompyle6.load import load_file, load_module
1819

@@ -138,8 +139,8 @@ def cmp_code_objects(version, code_obj1, code_obj2, name=''):
138139
This is the main part of this module.
139140
"""
140141
# print code_obj1, type(code_obj2)
141-
assert code_obj1, hasattr('co_name')
142-
assert code_obj2, hasattr('co_name')
142+
assert iscode(code_obj1)
143+
assert iscode(code_obj2)
143144
# print dir(code_obj1)
144145
if isinstance(code_obj1, object):
145146
# new style classes (Python 2.2)

0 commit comments

Comments
 (0)
X Tutup