Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4065)

Side by Side Diff: Lib/test/test_funcattrs.py

Issue 14369: make __closure__ writable
Patch Set: Created 1 year ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Doc/reference/datamodel.rst ('k') | Objects/funcobject.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 from test import support 1 from test import support
2 import types 2 import types
3 import unittest 3 import unittest
4 4
5 5
6 def global_function(): 6 def global_function():
7 def inner_function(): 7 def inner_function():
8 class LocalClass: 8 class LocalClass:
9 pass 9 pass
10 return LocalClass 10 return LocalClass
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 test.__code__ = self.b.__code__ 64 test.__code__ = self.b.__code__
65 self.assertEqual(test(), 3) # self.b always returns 3, arbitrarily 65 self.assertEqual(test(), 3) # self.b always returns 3, arbitrarily
66 66
67 def test___globals__(self): 67 def test___globals__(self):
68 self.assertIs(self.b.__globals__, globals()) 68 self.assertIs(self.b.__globals__, globals())
69 self.cannot_set_attr(self.b, '__globals__', 2, 69 self.cannot_set_attr(self.b, '__globals__', 2,
70 (AttributeError, TypeError)) 70 (AttributeError, TypeError))
71 71
72 def test___closure__(self): 72 def test___closure__(self):
73 a = 12 73 a = 12
74 def f(): print(a) 74 b = 67
75 def f():
76 return a + b
75 c = f.__closure__ 77 c = f.__closure__
76 self.assertIsInstance(c, tuple) 78 self.assertIsInstance(c, tuple)
77 self.assertEqual(len(c), 1) 79 self.assertEqual(len(c), 2)
80
78 # don't have a type object handy 81 # don't have a type object handy
79 self.assertEqual(c[0].__class__.__name__, "cell") 82 self.assertEqual(c[0].__class__.__name__, "cell")
80 self.cannot_set_attr(f, "__closure__", c, AttributeError) 83
84 # check that we can overwrite the __closure__
85 self.assertEqual(f(), 79)
86 good_closure = f.__closure__ = (cell(42), cell(34))
87 self.assertEqual(f(), 76)
88
89 # check that we can't write anything besides a 2-tuple of cells
90 with self.assertRaises(TypeError):
91 f.__closure__ = 'spam' # wrong type
92 with self.assertRaises(ValueError):
93 f.__closure__ = None # too short
94 with self.assertRaises(ValueError):
95 f.__closure__ = () # too short
96 with self.assertRaises(ValueError):
97 f.__closure__ = (cell(1),) # too short
98 with self.assertRaises(ValueError):
99 f.__closure__ = (cell(1), cell(2), cell(3)) # too long
100 with self.assertRaises(TypeError):
101 f.__closure__ = (cell(1), 2) # wrong element type
102
103 # __closure__ should not be affected by the previous exceptions
104 self.assertIs(f.__closure__, good_closure)
105 self.assertEqual(f(), 76)
106
107 def g():
108 return 123
109
110 # check we can't write anything besides None or () -
111 # type.FunctionType allows either for code object with
112 # trivial closure
113 g.__closure__ = None
114 self.assertEqual(g(), 123)
115 self.assertEqual(g.__closure__, None)
116
117 g.__closure__ = ()
118 self.assertEqual(g(), 123)
119 self.assertEqual(g.__closure__, ())
120
121 with self.assertRaises(ValueError):
122 g.__closure__ = (cell(1), cell(2))
123
124 with self.assertRaises(TypeError):
125 g.__closure__ = "spam"
81 126
82 def test_empty_cell(self): 127 def test_empty_cell(self):
83 def f(): print(a) 128 def f(): print(a)
84 try: 129 try:
85 f.__closure__[0].cell_contents 130 f.__closure__[0].cell_contents
86 except ValueError: 131 except ValueError:
87 pass 132 pass
88 else: 133 else:
89 self.fail("shouldn't be able to read an empty cell") 134 self.fail("shouldn't be able to read an empty cell")
90 a = 12 135 a = 12
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 414
370 def test_main(): 415 def test_main():
371 support.run_unittest(FunctionPropertiesTest, InstancemethodAttrTest, 416 support.run_unittest(FunctionPropertiesTest, InstancemethodAttrTest,
372 ArbitraryFunctionAttrTest, FunctionDictsTest, 417 ArbitraryFunctionAttrTest, FunctionDictsTest,
373 FunctionDocstringTest, CellTest, 418 FunctionDocstringTest, CellTest,
374 StaticMethodAttrsTest, 419 StaticMethodAttrsTest,
375 BuiltinFunctionPropertiesTest) 420 BuiltinFunctionPropertiesTest)
376 421
377 if __name__ == "__main__": 422 if __name__ == "__main__":
378 test_main() 423 test_main()
OLDNEW
« no previous file with comments | « Doc/reference/datamodel.rst ('k') | Objects/funcobject.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7