/lib-python/modified-2.5.2/test/test_funcattrs.py

https://github.com/lalitjsraks/pypy
Python | 414 lines | 327 code | 72 blank | 15 comment | 88 complexity | 4b8fd33b0e9c5bd4e7bffc0a13b9a0b6 MD5 | raw file
  1. from test.test_support import verbose, TestFailed, verify
  2. import types
  3. class F:
  4. def a(self):
  5. pass
  6. def b():
  7. 'my docstring'
  8. pass
  9. # __module__ is a special attribute
  10. verify(b.__module__ == __name__)
  11. verify(verify.__module__ == "test.test_support")
  12. # setting attributes on functions
  13. try:
  14. b.publish
  15. except AttributeError: pass
  16. else: raise TestFailed, 'expected AttributeError'
  17. if b.__dict__ <> {}:
  18. raise TestFailed, 'expected unassigned func.__dict__ to be {}'
  19. b.publish = 1
  20. if b.publish <> 1:
  21. raise TestFailed, 'function attribute not set to expected value'
  22. docstring = 'its docstring'
  23. b.__doc__ = docstring
  24. if b.__doc__ <> docstring:
  25. raise TestFailed, 'problem with setting __doc__ attribute'
  26. if 'publish' not in dir(b):
  27. raise TestFailed, 'attribute not in dir()'
  28. try:
  29. del b.__dict__
  30. except (AttributeError, TypeError): pass
  31. else: raise TestFailed, 'expected AttributeError or TypeError'
  32. b.publish = 1
  33. try:
  34. b.__dict__ = None
  35. except TypeError: pass
  36. else: raise TestFailed, 'func.__dict__ = None expected TypeError'
  37. d = {'hello': 'world'}
  38. b.__dict__ = d
  39. if b.func_dict is not d:
  40. raise TestFailed, 'func.__dict__ assignment to dictionary failed'
  41. if b.hello <> 'world':
  42. raise TestFailed, 'attribute after func.__dict__ assignment failed'
  43. f1 = F()
  44. f2 = F()
  45. try:
  46. F.a.publish
  47. except AttributeError: pass
  48. else: raise TestFailed, 'expected AttributeError'
  49. try:
  50. f1.a.publish
  51. except AttributeError: pass
  52. else: raise TestFailed, 'expected AttributeError'
  53. # In Python 2.1 beta 1, we disallowed setting attributes on unbound methods
  54. # (it was already disallowed on bound methods). See the PEP for details.
  55. try:
  56. F.a.publish = 1
  57. except (AttributeError, TypeError): pass
  58. else: raise TestFailed, 'expected AttributeError or TypeError'
  59. # But setting it explicitly on the underlying function object is okay.
  60. F.a.im_func.publish = 1
  61. if F.a.publish <> 1:
  62. raise TestFailed, 'unbound method attribute not set to expected value'
  63. if f1.a.publish <> 1:
  64. raise TestFailed, 'bound method attribute access did not work'
  65. if f2.a.publish <> 1:
  66. raise TestFailed, 'bound method attribute access did not work'
  67. if 'publish' not in dir(F.a):
  68. raise TestFailed, 'attribute not in dir()'
  69. try:
  70. f1.a.publish = 0
  71. except (AttributeError, TypeError): pass
  72. else: raise TestFailed, 'expected AttributeError or TypeError'
  73. # See the comment above about the change in semantics for Python 2.1b1
  74. try:
  75. F.a.myclass = F
  76. except (AttributeError, TypeError): pass
  77. else: raise TestFailed, 'expected AttributeError or TypeError'
  78. F.a.im_func.myclass = F
  79. f1.a.myclass
  80. f2.a.myclass
  81. f1.a.myclass
  82. F.a.myclass
  83. if f1.a.myclass is not f2.a.myclass or \
  84. f1.a.myclass is not F.a.myclass:
  85. raise TestFailed, 'attributes were not the same'
  86. # try setting __dict__
  87. try:
  88. F.a.__dict__ = (1, 2, 3)
  89. except (AttributeError, TypeError): pass
  90. else: raise TestFailed, 'expected TypeError or AttributeError'
  91. F.a.im_func.__dict__ = {'one': 11, 'two': 22, 'three': 33}
  92. if f1.a.two <> 22:
  93. raise TestFailed, 'setting __dict__'
  94. from UserDict import UserDict
  95. d = UserDict({'four': 44, 'five': 55})
  96. try:
  97. F.a.__dict__ = d
  98. except (AttributeError, TypeError): pass
  99. else: raise TestFailed
  100. if f2.a.one <> f1.a.one <> F.a.one <> 11:
  101. raise TestFailed
  102. # im_func may not be a Python method!
  103. import new
  104. F.id = new.instancemethod(id, None, F)
  105. eff = F()
  106. if eff.id() <> id(eff):
  107. raise TestFailed
  108. try:
  109. F.id.foo
  110. except AttributeError: pass
  111. else: raise TestFailed
  112. try:
  113. F.id.foo = 12
  114. except (AttributeError, TypeError): pass
  115. else: raise TestFailed
  116. try:
  117. F.id.foo
  118. except AttributeError: pass
  119. else: raise TestFailed
  120. try:
  121. eff.id.foo
  122. except AttributeError: pass
  123. else: raise TestFailed
  124. try:
  125. eff.id.foo = 12
  126. except (AttributeError, TypeError): pass
  127. else: raise TestFailed
  128. try:
  129. eff.id.foo
  130. except AttributeError: pass
  131. else: raise TestFailed
  132. # Regression test for a crash in pre-2.1a1
  133. def another():
  134. pass
  135. try:
  136. del another.__dict__
  137. except (TypeError, AttributeError): pass
  138. else: raise TestFailed, 'del another.__dict__ did not fail'
  139. try:
  140. del another.func_dict
  141. except (TypeError, AttributeError): pass
  142. else: raise TestFailed, 'del another.func_dict did not fail'
  143. try:
  144. another.func_dict = None
  145. except TypeError: pass
  146. else: raise TestFailed
  147. try:
  148. del another.bar
  149. except AttributeError: pass
  150. else: raise TestFailed
  151. # This isn't specifically related to function attributes, but it does test a
  152. # core dump regression in funcobject.c
  153. del another.func_defaults
  154. def foo():
  155. pass
  156. def bar():
  157. pass
  158. def temp():
  159. print 1
  160. if foo==bar:
  161. raise TestFailed
  162. d={}
  163. d[foo] = 1
  164. foo.func_code = temp.func_code
  165. d[foo]
  166. # Test all predefined function attributes systematically
  167. def cantset(obj, name, value, exception=(AttributeError, TypeError)):
  168. verify(hasattr(obj, name)) # Otherwise it's probably a typo
  169. try:
  170. setattr(obj, name, value)
  171. except exception:
  172. pass
  173. else:
  174. raise TestFailed, "shouldn't be able to set %s to %r" % (name, value)
  175. try:
  176. delattr(obj, name)
  177. except (AttributeError, TypeError):
  178. pass
  179. else:
  180. raise TestFailed, "shouldn't be able to del %s" % name
  181. def test_func_closure():
  182. a = 12
  183. def f(): print a
  184. c = f.func_closure
  185. verify(isinstance(c, tuple))
  186. verify(len(c) == 1)
  187. verify(c[0].__class__.__name__ == "cell") # don't have a type object handy
  188. cantset(f, "func_closure", c)
  189. def test_empty_cell():
  190. def f(): print a
  191. try:
  192. f.func_closure[0].cell_contents
  193. except ValueError:
  194. pass
  195. else:
  196. raise TestFailed, "shouldn't be able to read an empty cell"
  197. a = 12
  198. def test_func_doc():
  199. def f(): pass
  200. verify(f.__doc__ is None)
  201. verify(f.func_doc is None)
  202. f.__doc__ = "hello"
  203. verify(f.__doc__ == "hello")
  204. verify(f.func_doc == "hello")
  205. del f.__doc__
  206. verify(f.__doc__ is None)
  207. verify(f.func_doc is None)
  208. f.func_doc = "world"
  209. verify(f.__doc__ == "world")
  210. verify(f.func_doc == "world")
  211. del f.func_doc
  212. verify(f.func_doc is None)
  213. verify(f.__doc__ is None)
  214. def test_func_globals():
  215. def f(): pass
  216. verify(f.func_globals is globals())
  217. cantset(f, "func_globals", globals())
  218. def test_func_name():
  219. def f(): pass
  220. verify(f.__name__ == "f")
  221. verify(f.func_name == "f")
  222. f.__name__ = "g"
  223. verify(f.__name__ == "g")
  224. verify(f.func_name == "g")
  225. f.func_name = "h"
  226. verify(f.__name__ == "h")
  227. verify(f.func_name == "h")
  228. cantset(f, "func_globals", 1)
  229. cantset(f, "__name__", 1)
  230. # test that you can access func.__name__ in restricted mode
  231. s = """def f(): pass\nf.__name__"""
  232. exec s in {'__builtins__':{}}
  233. def test_func_code():
  234. a = b = 24
  235. def f(): pass
  236. def g(): print 12
  237. def f1(): print a
  238. def g1(): print b
  239. def f2(): print a, b
  240. verify(type(f.func_code) is types.CodeType)
  241. f.func_code = g.func_code
  242. cantset(f, "func_code", None)
  243. # can't change the number of free vars
  244. cantset(f, "func_code", f1.func_code, exception=ValueError)
  245. cantset(f1, "func_code", f.func_code, exception=ValueError)
  246. cantset(f1, "func_code", f2.func_code, exception=ValueError)
  247. f1.func_code = g1.func_code
  248. def test_func_defaults():
  249. def f(a, b): return (a, b)
  250. verify(f.func_defaults is None)
  251. f.func_defaults = (1, 2)
  252. verify(f.func_defaults == (1, 2))
  253. verify(f(10) == (10, 2))
  254. def g(a=1, b=2): return (a, b)
  255. verify(g.func_defaults == (1, 2))
  256. del g.func_defaults
  257. verify(g.func_defaults is None)
  258. try:
  259. g()
  260. except TypeError:
  261. pass
  262. else:
  263. raise TestFailed, "shouldn't be allowed to call g() w/o defaults"
  264. def test_func_dict():
  265. def f(): pass
  266. a = f.__dict__
  267. b = f.func_dict
  268. verify(a == {})
  269. verify(a is b)
  270. f.hello = 'world'
  271. verify(a == {'hello': 'world'})
  272. verify(f.func_dict is a is f.__dict__)
  273. f.func_dict = {}
  274. verify(not hasattr(f, "hello"))
  275. f.__dict__ = {'world': 'hello'}
  276. verify(f.world == "hello")
  277. verify(f.__dict__ is f.func_dict == {'world': 'hello'})
  278. cantset(f, "func_dict", None)
  279. cantset(f, "__dict__", None)
  280. def test_im_class():
  281. class C:
  282. def foo(self): pass
  283. verify(C.foo.im_class is C)
  284. verify(C().foo.im_class is C)
  285. cantset(C.foo, "im_class", C)
  286. cantset(C().foo, "im_class", C)
  287. def test_im_func():
  288. def foo(self): pass
  289. class C:
  290. pass
  291. C.foo = foo
  292. verify(C.foo.im_func is foo)
  293. verify(C().foo.im_func is foo)
  294. cantset(C.foo, "im_func", foo)
  295. cantset(C().foo, "im_func", foo)
  296. def test_im_self():
  297. class C:
  298. def foo(self): pass
  299. verify(C.foo.im_self is None)
  300. c = C()
  301. verify(c.foo.im_self is c)
  302. cantset(C.foo, "im_self", None)
  303. cantset(c.foo, "im_self", c)
  304. def test_im_dict():
  305. class C:
  306. def foo(self): pass
  307. foo.bar = 42
  308. verify(C.foo.__dict__ == {'bar': 42})
  309. verify(C().foo.__dict__ == {'bar': 42})
  310. cantset(C.foo, "__dict__", C.foo.__dict__)
  311. cantset(C().foo, "__dict__", C.foo.__dict__)
  312. def test_im_doc():
  313. class C:
  314. def foo(self): "hello"
  315. verify(C.foo.__doc__ == "hello")
  316. verify(C().foo.__doc__ == "hello")
  317. cantset(C.foo, "__doc__", "hello")
  318. cantset(C().foo, "__doc__", "hello")
  319. def test_im_name():
  320. class C:
  321. def foo(self): pass
  322. verify(C.foo.__name__ == "foo")
  323. verify(C().foo.__name__ == "foo")
  324. cantset(C.foo, "__name__", "foo")
  325. cantset(C().foo, "__name__", "foo")
  326. def testmore():
  327. test_func_closure()
  328. test_empty_cell()
  329. test_func_doc()
  330. test_func_globals()
  331. test_func_name()
  332. test_func_code()
  333. test_func_defaults()
  334. test_func_dict()
  335. # Tests for instance method attributes
  336. test_im_class()
  337. test_im_func()
  338. test_im_self()
  339. test_im_dict()
  340. test_im_doc()
  341. test_im_name()
  342. testmore()