Several changes for Jython portability. This closes SF patch
authorBarry Warsaw <barry@python.org>
Fri, 23 Mar 2001 16:13:30 +0000 (16:13 +0000)
committerBarry Warsaw <barry@python.org>
Fri, 23 Mar 2001 16:13:30 +0000 (16:13 +0000)
#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.

Lib/test/test_new.py

index 8a7b9a6a257eb25276a1fc0e946272f63be79757..ab5eedec47f48d9252743f656cce6a39ea0f168c 100644 (file)
@@ -47,14 +47,21 @@ im()
 verify(c.get_yolks() == 1 and c.get_more_yolks() == 4,
        'Broken call of hand-crafted instance method')
 
+# 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.
 codestr = '''
+global c
 a = 1
 b = 2
 c = a + b
 '''
 
 ccode = compile(codestr, '<string>', 'exec')
-g = {'c': 0, '__builtins__': __builtins__}
+# Jython doesn't have a __builtins__, so use a portable alternative
+import __builtin__
+g = {'c': 0, '__builtins__': __builtin__}
 # this test could be more robust
 print 'new.function()'
 func = new.function(ccode, g)
@@ -65,11 +72,13 @@ verify(g['c'] == 3,
        'Could not create a proper function object')
 
 # bogus test of new.code()
-print 'new.code()'
-d = new.code(3, 3, 3, 3, codestr, (), (), (),
-             "<string>", "<name>", 1, "", (), ())
-# test backwards-compatibility version with no freevars or cellvars
-d = new.code(3, 3, 3, 3, codestr, (), (), (),
-             "<string>", "<name>", 1, "")
-if verbose:
-    print d
+# Note: Jython will never have new.code()
+if hasattr(new, 'code'):
+    print 'new.code()'
+    d = new.code(3, 3, 3, 3, codestr, (), (), (),
+                 "<string>", "<name>", 1, "", (), ())
+    # test backwards-compatibility version with no freevars or cellvars
+    d = new.code(3, 3, 3, 3, codestr, (), (), (),
+                 "<string>", "<name>", 1, "")
+    if verbose:
+        print d