Commit cfd16e84 authored by Evan Simpson's avatar Evan Simpson

Collector #1074: Change Scripts' __name__ to None, added unit tests for the...

Collector #1074: Change Scripts' __name__ to None, added unit tests for the effect of __name__ on class definitions and imports.
parent f4dc9cd9
...@@ -100,6 +100,8 @@ Zope Changes ...@@ -100,6 +100,8 @@ Zope Changes
Bugs fixed Bugs fixed
- Collector #1074: Change Scripts' __name__ to None
- Range searches with KeywordIndexes did not work with record-style - Range searches with KeywordIndexes did not work with record-style
query parameters query parameters
......
...@@ -17,7 +17,7 @@ This product provides support for Script objects containing restricted ...@@ -17,7 +17,7 @@ This product provides support for Script objects containing restricted
Python code. Python code.
""" """
__version__='$Revision: 1.51 $'[11:-2] __version__='$Revision: 1.52 $'[11:-2]
import sys, os, traceback, re, marshal, new import sys, os, traceback, re, marshal, new
from Globals import DTMLFile, MessageDialog, package_home from Globals import DTMLFile, MessageDialog, package_home
...@@ -256,7 +256,7 @@ class PythonScript(Script, Historical, Cacheable): ...@@ -256,7 +256,7 @@ class PythonScript(Script, Historical, Cacheable):
def _newfun(self, code): def _newfun(self, code):
g = {'__debug__': __debug__, g = {'__debug__': __debug__,
'__name__': self.id, '__name__': None,
'__builtins__': safe_builtins, '__builtins__': safe_builtins,
'_getattr_': guarded_getattr, '_getattr_': guarded_getattr,
'_getitem_': guarded_getitem, '_getitem_': guarded_getitem,
......
...@@ -48,6 +48,13 @@ class TestPythonScriptNoAq(unittest.TestCase): ...@@ -48,6 +48,13 @@ class TestPythonScriptNoAq(unittest.TestCase):
ps._makeFunction() ps._makeFunction()
return ps return ps
def _filePS(self, fname, bind=None):
ps = PythonScript(fname)
ps.ZBindings_edit(bind or {})
ps.write(readf(fname))
ps._makeFunction()
return ps
def fail(self): def fail(self):
'Fail if called' 'Fail if called'
assert 0, 'Fail called' assert 0, 'Fail called'
...@@ -97,20 +104,20 @@ class TestPythonScriptNoAq(unittest.TestCase): ...@@ -97,20 +104,20 @@ class TestPythonScriptNoAq(unittest.TestCase):
assert c == 'c' assert c == 'c'
def testWhileLoop(self): def testWhileLoop(self):
one = self._newPS(readf('while_loop'))() one = self._filePS('while_loop')()
assert one == 1 assert one == 1
def testForLoop(self): def testForLoop(self):
ten = self._newPS(readf('for_loop'))() ten = self._filePS('for_loop')()
assert ten == 10 assert ten == 10
def testMutateLiterals(self): def testMutateLiterals(self):
l, d = self._newPS(readf('mutate_literals'))() l, d = self._filePS('mutate_literals')()
assert l == [2], l assert l == [2], l
assert d == {'b': 2} assert d == {'b': 2}
def testTupleUnpackAssignment(self): def testTupleUnpackAssignment(self):
d, x = self._newPS(readf('tuple_unpack_assignment'))() d, x = self._filePS('tuple_unpack_assignment')()
assert d == {'a': 0, 'b': 1, 'c': 2}, d assert d == {'a': 0, 'b': 1, 'c': 2}, d
assert x == 3, x assert x == 3, x
...@@ -119,16 +126,16 @@ class TestPythonScriptNoAq(unittest.TestCase): ...@@ -119,16 +126,16 @@ class TestPythonScriptNoAq(unittest.TestCase):
assert one == 1 assert one == 1
def testTryExcept(self): def testTryExcept(self):
a,b = self._newPS(readf('try_except'))() a,b = self._filePS('try_except')()
assert a==1 assert a==1
assert b==1 assert b==1
def testBigBoolean(self): def testBigBoolean(self):
true = self._newPS(readf('big_boolean'))() true = self._filePS('big_boolean')()
assert true, true assert true, true
def testFibonacci(self): def testFibonacci(self):
r = self._newPS(readf('fibonacci'))() r = self._filePS('fibonacci')()
assert r == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, assert r == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,
610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657,
46368, 75025, 121393, 196418, 317811, 514229, 832040, 46368, 75025, 121393, 196418, 317811, 514229, 832040,
...@@ -136,26 +143,31 @@ class TestPythonScriptNoAq(unittest.TestCase): ...@@ -136,26 +143,31 @@ class TestPythonScriptNoAq(unittest.TestCase):
24157817, 39088169, 63245986], r 24157817, 39088169, 63245986], r
def testSimplePrint(self): def testSimplePrint(self):
txt = self._newPS(readf('simple_print'))() txt = self._filePS('simple_print')()
assert txt == 'a 1 []\n', txt assert txt == 'a 1 []\n', txt
def testComplexPrint(self): def testComplexPrint(self):
txt = self._newPS(readf('complex_print'))() txt = self._filePS('complex_print')()
assert txt == 'double\ndouble\nx: 1\ny: 0 1 2\n\n', txt assert txt == 'double\ndouble\nx: 1\ny: 0 1 2\n\n', txt
def testNSBind(self): def testNSBind(self):
f = self._newPS(readf('ns_bind'), bind={'name_ns': '_'}) f = self._filePS('ns_bind', bind={'name_ns': '_'})
bound = f.__render_with_namespace__({'yes': 1, 'no': self.fail}) bound = f.__render_with_namespace__({'yes': 1, 'no': self.fail})
assert bound == 1, bound assert bound == 1, bound
def testBooleanMap(self): def testBooleanMap(self):
true = self._newPS(readf('boolean_map'))() true = self._filePS('boolean_map')()
assert true assert true
def testGetSize(self): def testGetSize(self):
f = self._newPS(readf('complex_print')) f = self._filePS('complex_print')
self.assertEqual(f.get_size(),len(f.read())) self.assertEqual(f.get_size(),len(f.read()))
def test__name__(self):
fname = 'class.__name__'
f = self._filePS(fname)
self.assertEqual(f(), ('?.foo', "'string'"))
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest( unittest.makeSuite( TestPythonScriptNoAq ) ) suite.addTest( unittest.makeSuite( TestPythonScriptNoAq ) )
......
import string
class foo:
pass
return repr(foo).split()[1], repr(string).split()[1]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment