summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_code.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_code.py')
-rw-r--r--Lib/test/test_code.py54
1 files changed, 38 insertions, 16 deletions
diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py
index 97adf59..e1c7a78 100644
--- a/Lib/test/test_code.py
+++ b/Lib/test/test_code.py
@@ -6,9 +6,10 @@
... return g
...
->>> dump(f.func_code)
+>>> dump(f.__code__)
name: f
argcount: 1
+kwonlyargcount: 0
names: ()
varnames: ('x', 'g')
cellvars: ('x',)
@@ -17,9 +18,10 @@ nlocals: 2
flags: 3
consts: ('None', '<code object g>')
->>> dump(f(4).func_code)
+>>> dump(f(4).__code__)
name: g
argcount: 1
+kwonlyargcount: 0
names: ()
varnames: ('y',)
cellvars: ()
@@ -34,9 +36,11 @@ consts: ('None',)
... c = a * b
... return c
...
->>> dump(h.func_code)
+
+>>> dump(h.__code__)
name: h
argcount: 2
+kwonlyargcount: 0
names: ()
varnames: ('x', 'y', 'a', 'b', 'c')
cellvars: ()
@@ -46,14 +50,15 @@ flags: 67
consts: ('None',)
>>> def attrs(obj):
-... print obj.attr1
-... print obj.attr2
-... print obj.attr3
+... print(obj.attr1)
+... print(obj.attr2)
+... print(obj.attr3)
->>> dump(attrs.func_code)
+>>> dump(attrs.__code__)
name: attrs
argcount: 1
-names: ('attr1', 'attr2', 'attr3')
+kwonlyargcount: 0
+names: ('print', 'attr1', 'attr2', 'attr3')
varnames: ('obj',)
cellvars: ()
freevars: ()
@@ -65,11 +70,12 @@ consts: ('None',)
... 'doc string'
... 'not a docstring'
... 53
-... 53L
+... 0x53
->>> dump(optimize_away.func_code)
+>>> dump(optimize_away.__code__)
name: optimize_away
argcount: 0
+kwonlyargcount: 0
names: ()
varnames: ()
cellvars: ()
@@ -78,6 +84,22 @@ nlocals: 0
flags: 67
consts: ("'doc string'", 'None')
+>>> def keywordonly_args(a,b,*,k1):
+... return a,b,k1
+...
+
+>>> dump(keywordonly_args.__code__)
+name: keywordonly_args
+argcount: 2
+kwonlyargcount: 1
+names: ()
+varnames: ('a', 'b', 'k1')
+cellvars: ()
+freevars: ()
+nlocals: 3
+flags: 67
+consts: ('None',)
+
"""
import unittest
@@ -96,10 +118,10 @@ def consts(t):
def dump(co):
"""Print out a text representation of a code object."""
- for attr in ["name", "argcount", "names", "varnames", "cellvars",
- "freevars", "nlocals", "flags"]:
- print "%s: %s" % (attr, getattr(co, "co_" + attr))
- print "consts:", tuple(consts(co.co_consts))
+ for attr in ["name", "argcount", "kwonlyargcount", "names", "varnames",
+ "cellvars", "freevars", "nlocals", "flags"]:
+ print("%s: %s" % (attr, getattr(co, "co_" + attr)))
+ print("consts:", tuple(consts(co.co_consts)))
class CodeTest(unittest.TestCase):
@@ -117,7 +139,7 @@ class CodeWeakRefTest(unittest.TestCase):
# Create a code object in a clean environment so that we know we have
# the only reference to it left.
namespace = {}
- exec "def f(): pass" in globals(), namespace
+ exec("def f(): pass", globals(), namespace)
f = namespace["f"]
del namespace
@@ -137,7 +159,7 @@ class CodeWeakRefTest(unittest.TestCase):
def test_main(verbose=None):
- from test.test_support import run_doctest, run_unittest
+ from test.support import run_doctest, run_unittest
from test import test_code
run_doctest(test_code, verbose)
run_unittest(CodeTest, CodeWeakRefTest)