[Python-checkins] python/dist/src/Lib/test test_builtin.py, 1.29,
1.30 test_descrtut.py, 1.19, 1.20
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Fri Jul 2 02:41:38 EDT 2004
Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18086/Lib/test
Modified Files:
test_builtin.py test_descrtut.py
Log Message:
SF Bug #215126: Over restricted type checking on eval() function
The builtin eval() function now accepts any mapping for the locals argument.
Time sensitive steps guarded by PyDict_CheckExact() to keep from slowing
down the normal case. My timings so no measurable impact.
Index: test_builtin.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_builtin.py,v
retrieving revision 1.29
retrieving revision 1.30
diff -C2 -d -r1.29 -r1.30
*** test_builtin.py 12 Feb 2004 17:35:11 -0000 1.29
--- test_builtin.py 2 Jul 2004 06:41:05 -0000 1.30
***************
*** 4,8 ****
from test.test_support import fcmp, have_unicode, TESTFN, unlink
! import sys, warnings, cStringIO, random
warnings.filterwarnings("ignore", "hex../oct.. of negative int",
FutureWarning, __name__)
--- 4,8 ----
from test.test_support import fcmp, have_unicode, TESTFN, unlink
! import sys, warnings, cStringIO, random, UserDict
warnings.filterwarnings("ignore", "hex../oct.. of negative int",
FutureWarning, __name__)
***************
*** 263,266 ****
--- 263,320 ----
self.assertRaises(TypeError, eval, ())
+ def test_general_eval(self):
+ # Tests that general mappings can be used for the locals argument
+
+ class M:
+ "Test mapping interface versus possible calls from eval()."
+ def __getitem__(self, key):
+ if key == 'a':
+ return 12
+ raise KeyError
+ def keys(self):
+ return list('xyz')
+
+ m = M()
+ g = globals()
+ self.assertEqual(eval('a', g, m), 12)
+ self.assertRaises(NameError, eval, 'b', g, m)
+ self.assertEqual(eval('dir()', g, m), list('xyz'))
+ self.assertEqual(eval('globals()', g, m), g)
+ self.assertEqual(eval('locals()', g, m), m)
+
+ # Verify that dict subclasses work as well
+ class D(dict):
+ def __getitem__(self, key):
+ if key == 'a':
+ return 12
+ return dict.__getitem__(self, key)
+ def keys(self):
+ return list('xyz')
+
+ d = D()
+ self.assertEqual(eval('a', g, d), 12)
+ self.assertRaises(NameError, eval, 'b', g, d)
+ self.assertEqual(eval('dir()', g, d), list('xyz'))
+ self.assertEqual(eval('globals()', g, d), g)
+ self.assertEqual(eval('locals()', g, d), d)
+
+ # Verify locals stores (used by list comps)
+ eval('[locals() for i in (2,3)]', g, d)
+ eval('[locals() for i in (2,3)]', g, UserDict.UserDict())
+
+ class SpreadSheet:
+ "Sample application showing nested, calculated lookups."
+ _cells = {}
+ def __setitem__(self, key, formula):
+ self._cells[key] = formula
+ def __getitem__(self, key ):
+ return eval(self._cells[key], globals(), self)
+
+ ss = SpreadSheet()
+ ss['a1'] = '5'
+ ss['a2'] = 'a1*6'
+ ss['a3'] = 'a2*7'
+ self.assertEqual(ss['a3'], 210)
+
# Done outside of the method test_z to get the correct scope
z = 0
Index: test_descrtut.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_descrtut.py,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -d -r1.19 -r1.20
*** test_descrtut.py 17 Dec 2003 20:43:32 -0000 1.19
--- test_descrtut.py 2 Jul 2004 06:41:05 -0000 1.20
***************
*** 79,92 ****
>>>
- However, our __getitem__() method is not used for variable access by the
- interpreter:
-
- >>> exec "print foo" in a
- Traceback (most recent call last):
- File "<stdin>", line 1, in ?
- File "<string>", line 1, in ?
- NameError: name 'foo' is not defined
- >>>
-
Now I'll show that defaultdict instances have dynamic instance variables,
just like classic classes:
--- 79,82 ----
More information about the Python-checkins
mailing list