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

Skip to content

Commit dfdac1a

Browse files
committed
Several changes for Jython portability. This closes SF patch
#403666. Specifically, In codestr, force `c' to be global. It's unclear what the semantics should be for a code object compiled at module scope, but bound and run in a function. In CPython, `c' is global (by accident?) while in Jython, `c' is local. The intent of the test clearly is to make `c' global, so let's be explicit about it. Jython also does not have a __builtins__ name in the module's namespace, so we use a more portable alternative (though I'm not sure why the test requires "__builtins__" in the g namespace). Finally, skip the new.code() test if the new module doesn't have a `code' attribute. Jython will never have this.
1 parent 101651c commit dfdac1a

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

Lib/test/test_new.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,21 @@ def break_yolks(self):
4747
verify(c.get_yolks() == 1 and c.get_more_yolks() == 4,
4848
'Broken call of hand-crafted instance method')
4949

50+
# It's unclear what the semantics should be for a code object compiled at
51+
# module scope, but bound and run in a function. In CPython, `c' is global
52+
# (by accident?) while in Jython, `c' is local. The intent of the test
53+
# clearly is to make `c' global, so let's be explicit about it.
5054
codestr = '''
55+
global c
5156
a = 1
5257
b = 2
5358
c = a + b
5459
'''
5560

5661
ccode = compile(codestr, '<string>', 'exec')
57-
g = {'c': 0, '__builtins__': __builtins__}
62+
# Jython doesn't have a __builtins__, so use a portable alternative
63+
import __builtin__
64+
g = {'c': 0, '__builtins__': __builtin__}
5865
# this test could be more robust
5966
print 'new.function()'
6067
func = new.function(ccode, g)
@@ -65,11 +72,13 @@ def break_yolks(self):
6572
'Could not create a proper function object')
6673

6774
# bogus test of new.code()
68-
print 'new.code()'
69-
d = new.code(3, 3, 3, 3, codestr, (), (), (),
70-
"<string>", "<name>", 1, "", (), ())
71-
# test backwards-compatibility version with no freevars or cellvars
72-
d = new.code(3, 3, 3, 3, codestr, (), (), (),
73-
"<string>", "<name>", 1, "")
74-
if verbose:
75-
print d
75+
# Note: Jython will never have new.code()
76+
if hasattr(new, 'code'):
77+
print 'new.code()'
78+
d = new.code(3, 3, 3, 3, codestr, (), (), (),
79+
"<string>", "<name>", 1, "", (), ())
80+
# test backwards-compatibility version with no freevars or cellvars
81+
d = new.code(3, 3, 3, 3, codestr, (), (), (),
82+
"<string>", "<name>", 1, "")
83+
if verbose:
84+
print d

0 commit comments

Comments
 (0)