PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/interpreter/test/test_zzpickle_and_slow.py

https://bitbucket.org/rokujyouhitoma/pypy/
Python | 484 lines | 417 code | 50 blank | 17 comment | 24 complexity | ffa9a65fb0400429ddb127682d996276 MD5 | raw file
  1. import py
  2. from pypy import conftest
  3. from pypy.conftest import gettestobjspace
  4. from pypy.interpreter import gateway
  5. from pypy.rlib.jit import non_virtual_ref, vref_None
  6. class AppTestSlow:
  7. def setup_class(cls):
  8. space = gettestobjspace()
  9. cls.space = space
  10. if py.test.config.option.runappdirect:
  11. filename = __file__
  12. else:
  13. filename = gateway.__file__
  14. if filename[-3:] != '.py':
  15. filename = filename[:-1]
  16. cls.w_file = space.wrap(filename)
  17. def test_inspect(self):
  18. if not hasattr(len, 'func_code'):
  19. skip("Cannot run this test if builtins have no func_code")
  20. import inspect
  21. args, varargs, varkw = inspect.getargs(len.func_code)
  22. assert args == ['obj']
  23. assert varargs is None
  24. assert varkw is None
  25. def _attach_helpers(space):
  26. from pypy.interpreter import pytraceback
  27. def hide_top_frame(space, w_frame):
  28. w_last = None
  29. while w_frame.f_backref():
  30. w_last = w_frame
  31. w_frame = w_frame.f_backref()
  32. assert w_last
  33. w_saved = w_last.f_backref()
  34. w_last.f_backref = vref_None
  35. return w_saved
  36. def restore_top_frame(space, w_frame, w_saved):
  37. while w_frame.f_backref():
  38. w_frame = w_frame.f_backref()
  39. w_frame.f_backref = non_virtual_ref(w_saved)
  40. def read_exc_type(space, w_frame):
  41. if w_frame.last_exception is None:
  42. return space.w_None
  43. else:
  44. return w_frame.last_exception.w_type
  45. from pypy.interpreter import gateway
  46. hide_gw = gateway.interp2app(hide_top_frame)
  47. space.setitem(space.builtin.w_dict,
  48. space.wrap('hide_top_frame'),
  49. space.wrap(hide_gw))
  50. restore_gw = gateway.interp2app(restore_top_frame)
  51. space.setitem(space.builtin.w_dict,
  52. space.wrap('restore_top_frame'),
  53. space.wrap(restore_gw))
  54. read_exc_type_gw = gateway.interp2app(read_exc_type)
  55. space.setitem(space.builtin.w_dict,
  56. space.wrap('read_exc_type'),
  57. space.wrap(read_exc_type_gw))
  58. def _detatch_helpers(space):
  59. space.delitem(space.builtin.w_dict,
  60. space.wrap('hide_top_frame'))
  61. space.delitem(space.builtin.w_dict,
  62. space.wrap('restore_top_frame'))
  63. class AppTestInterpObjectPickling:
  64. pytestmark = py.test.mark.skipif("config.option.runappdirect")
  65. def setup_class(cls):
  66. cls.space = gettestobjspace(usemodules=['struct'])
  67. _attach_helpers(cls.space)
  68. def teardown_class(cls):
  69. _detatch_helpers(cls.space)
  70. def test_pickle_code(self):
  71. def f():
  72. return 42
  73. import pickle
  74. code = f.func_code
  75. pckl = pickle.dumps(code)
  76. result = pickle.loads(pckl)
  77. assert code == result
  78. def test_pickle_global_func(self):
  79. import new
  80. mod = new.module('mod')
  81. import sys
  82. sys.modules['mod'] = mod
  83. try:
  84. def func():
  85. return 42
  86. mod.__dict__['func'] = func
  87. func.__module__ = 'mod'
  88. import pickle
  89. pckl = pickle.dumps(func)
  90. result = pickle.loads(pckl)
  91. assert func is result
  92. finally:
  93. del sys.modules['mod']
  94. def test_pickle_not_imported_module(self):
  95. import new
  96. mod = new.module('mod')
  97. mod.__dict__['a'] = 1
  98. import pickle
  99. pckl = pickle.dumps(mod)
  100. result = pickle.loads(pckl)
  101. assert mod.__name__ == result.__name__
  102. assert mod.__dict__ == result.__dict__
  103. def test_pickle_builtin_func(self):
  104. import pickle
  105. pckl = pickle.dumps(map)
  106. result = pickle.loads(pckl)
  107. assert map is result
  108. def test_pickle_non_top_reachable_func(self):
  109. def func():
  110. return 42
  111. global a
  112. a = 42
  113. del globals()['test_pickle_non_top_reachable_func']
  114. import pickle
  115. pckl = pickle.dumps(func)
  116. result = pickle.loads(pckl)
  117. assert func.func_name == result.func_name
  118. assert func.func_closure == result.func_closure
  119. assert func.func_code == result.func_code
  120. assert func.func_defaults == result.func_defaults
  121. assert func.func_dict == result.func_dict
  122. assert func.func_doc == result.func_doc
  123. assert func.func_globals == result.func_globals
  124. def test_pickle_cell(self):
  125. def g():
  126. x = [42]
  127. def f():
  128. x[0] += 1
  129. return x
  130. return f.func_closure[0]
  131. import pickle
  132. cell = g()
  133. pckl = pickle.dumps(cell)
  134. result = pickle.loads(pckl)
  135. assert cell == result
  136. assert not (cell != result)
  137. def test_pickle_frame(self):
  138. #import sys
  139. # avoid creating a closure for now
  140. def f():
  141. try:
  142. raise Exception()
  143. except:
  144. import sys
  145. exc_type, exc, tb = sys.exc_info()
  146. return tb.tb_frame
  147. import pickle
  148. f1 = f()
  149. saved = hide_top_frame(f1)
  150. pckl = pickle.dumps(f1)
  151. restore_top_frame(f1, saved)
  152. f2 = pickle.loads(pckl)
  153. assert type(f1) is type(f2)
  154. assert dir(f1) == dir(f2)
  155. assert f1.__doc__ == f2.__doc__
  156. assert f2.f_back is None # because we pruned it
  157. assert f1.f_builtins is f2.f_builtins
  158. assert f1.f_code == f2.f_code
  159. assert f1.f_exc_traceback is f2.f_exc_traceback
  160. assert f1.f_exc_type is f2.f_exc_type
  161. assert f1.f_exc_value is f2.f_exc_value
  162. assert f1.f_lasti == f2.f_lasti
  163. assert f1.f_lineno == f2.f_lineno
  164. assert f1.f_restricted is f2.f_restricted
  165. assert f1.f_trace is f2.f_trace
  166. def test_pickle_frame_with_exc(self):
  167. #import sys
  168. # avoid creating a closure for now
  169. self = None
  170. def f():
  171. try:
  172. raise ValueError
  173. except:
  174. import sys, pickle
  175. f = sys._getframe()
  176. saved = hide_top_frame(f)
  177. pckl = pickle.dumps(f)
  178. restore_top_frame(f, saved)
  179. return pckl
  180. import pickle
  181. pckl = f()
  182. f2 = pickle.loads(pckl)
  183. assert read_exc_type(f2) is ValueError
  184. def test_pickle_frame_clos(self):
  185. # similar to above, therefore skipping the asserts.
  186. # we just want to see that the closure works
  187. import sys # this is the difference!
  188. def f():
  189. try:
  190. raise Exception()
  191. except:
  192. exc_type, exc, tb = sys.exc_info()
  193. return tb.tb_frame
  194. import pickle
  195. f1 = f()
  196. saved = hide_top_frame(f1)
  197. pckl = pickle.dumps(f1)
  198. restore_top_frame(f1, saved)
  199. f2 = pickle.loads(pckl)
  200. def test_pickle_traceback(self):
  201. def f():
  202. try:
  203. raise Exception()
  204. except:
  205. from sys import exc_info
  206. exc_type, exc, tb = exc_info()
  207. return tb
  208. import pickle
  209. tb = f()
  210. saved = hide_top_frame(tb.tb_frame)
  211. pckl = pickle.dumps(tb)
  212. result = pickle.loads(pckl)
  213. assert type(tb) is type(result)
  214. assert tb.tb_lasti == result.tb_lasti
  215. assert tb.tb_lineno == result.tb_lineno
  216. assert tb.tb_next == result.tb_next
  217. restore_top_frame(tb.tb_frame, saved)
  218. def test_pickle_module(self):
  219. import pickle
  220. mod = pickle
  221. pckl = pickle.dumps(mod)
  222. result = pickle.loads(pckl)
  223. assert mod is result
  224. def test_pickle_moduledict(self):
  225. import pickle
  226. moddict = pickle.__dict__
  227. pckl = pickle.dumps(moddict)
  228. result = pickle.loads(pckl)
  229. assert moddict is result
  230. def test_pickle_bltins_module(self):
  231. import pickle
  232. mod = __builtins__
  233. pckl = pickle.dumps(mod)
  234. result = pickle.loads(pckl)
  235. assert mod is result
  236. def test_pickle_buffer(self):
  237. skip("Can't pickle buffer objects on top of CPython either. "
  238. "Do we really need it?")
  239. import pickle
  240. a = buffer('ABCDEF')
  241. pckl = pickle.dumps(a)
  242. result = pickle.loads(pckl)
  243. assert a == result
  244. def test_pickle_complex(self):
  245. import pickle
  246. a = complex(1.23,4.567)
  247. pckl = pickle.dumps(a)
  248. result = pickle.loads(pckl)
  249. assert a == result
  250. def test_pickle_method(self):
  251. class myclass(object):
  252. def f(self):
  253. return 42
  254. def __reduce__(self):
  255. return (myclass, ())
  256. import pickle, sys, new
  257. myclass.__module__ = 'mod'
  258. myclass_inst = myclass()
  259. mod = new.module('mod')
  260. mod.myclass = myclass
  261. sys.modules['mod'] = mod
  262. try:
  263. method = myclass_inst.f
  264. pckl = pickle.dumps(method)
  265. result = pickle.loads(pckl)
  266. # we cannot compare the objects, because the method will be a fresh one
  267. assert method() == result()
  268. finally:
  269. del sys.modules['mod']
  270. def test_pickle_staticmethod(self):
  271. class myclass(object):
  272. def f():
  273. return 42
  274. f = staticmethod(f)
  275. import pickle
  276. method = myclass.f
  277. pckl = pickle.dumps(method)
  278. result = pickle.loads(pckl)
  279. assert method() == result()
  280. def test_pickle_classmethod(self):
  281. class myclass(object):
  282. def f(cls):
  283. return cls
  284. f = classmethod(f)
  285. import pickle, sys, new
  286. myclass.__module__ = 'mod'
  287. mod = new.module('mod')
  288. mod.myclass = myclass
  289. sys.modules['mod'] = mod
  290. try:
  291. method = myclass.f
  292. pckl = pickle.dumps(method)
  293. result = pickle.loads(pckl)
  294. assert method() == result()
  295. finally:
  296. del sys.modules['mod']
  297. def test_pickle_sequenceiter(self):
  298. '''
  299. In PyPy there is no distinction here between listiterator and
  300. tupleiterator that is why you will find no test_pickle_listiter nor
  301. test_pickle_tupleiter here, just this test.
  302. '''
  303. import pickle
  304. liter = iter([3,9,6,12,15,17,19,111])
  305. liter.next()
  306. pckl = pickle.dumps(liter)
  307. result = pickle.loads(pckl)
  308. liter.next()
  309. result.next()
  310. assert type(liter) is type(result)
  311. raises(TypeError, len, liter)
  312. assert list(liter) == list(result)
  313. def test_pickle_reversesequenceiter(self):
  314. import pickle
  315. liter = reversed([3,9,6,12,15,17,19,111])
  316. liter.next()
  317. pckl = pickle.dumps(liter)
  318. result = pickle.loads(pckl)
  319. liter.next()
  320. result.next()
  321. assert type(liter) is type(result)
  322. raises(TypeError, len, liter)
  323. assert list(liter) == list(result)
  324. # This test used to be marked xfail and it tried to test for the past
  325. # support of pickling dictiter objects.
  326. def test_pickle_dictiter(self):
  327. import pickle
  328. tdict = {'2':2, '3':3, '5':5}
  329. diter = iter(tdict)
  330. diter.next()
  331. raises(TypeError, pickle.dumps, diter)
  332. def test_pickle_reversed(self):
  333. import pickle
  334. r = reversed(tuple(range(10)))
  335. r.next()
  336. r.next()
  337. pickled = pickle.dumps(r)
  338. result = pickle.loads(pickled)
  339. result.next()
  340. r.next()
  341. assert type(r) is type(result)
  342. assert list(r) == list(result)
  343. def test_pickle_enum(self):
  344. import pickle
  345. e = enumerate(range(10))
  346. e.next()
  347. e.next()
  348. pckl = pickle.dumps(e)
  349. result = pickle.loads(pckl)
  350. e.next()
  351. result.next()
  352. assert type(e) is type(result)
  353. assert list(e) == list(result)
  354. def test_pickle_xrangeiter(self):
  355. import pickle
  356. riter = iter(xrange(5))
  357. riter.next()
  358. riter.next()
  359. pckl = pickle.dumps(riter)
  360. result = pickle.loads(pckl)
  361. assert type(riter) is type(result)
  362. assert list(result) == [2,3,4]
  363. def test_pickle_generator(self):
  364. import new
  365. mod = new.module('mod')
  366. import sys
  367. sys.modules['mod'] = mod
  368. try:
  369. def giveme(n):
  370. x = 0
  371. while x < n:
  372. yield x
  373. x += 1
  374. import pickle
  375. mod.giveme = giveme
  376. giveme.__module__ = mod
  377. g1 = mod.giveme(10)
  378. #g1.next()
  379. #g1.next()
  380. pckl = pickle.dumps(g1)
  381. g2 = pickle.loads(pckl)
  382. assert list(g1) == list(g2)
  383. finally:
  384. del sys.modules['mod']
  385. def test_pickle_generator_blk(self):
  386. # same as above but with the generator inside a block
  387. import new
  388. mod = new.module('mod')
  389. import sys
  390. sys.modules['mod'] = mod
  391. try:
  392. def giveme(n):
  393. x = 0
  394. while x < n:
  395. yield x
  396. x += 1
  397. import pickle
  398. mod.giveme = giveme
  399. giveme.__module__ = mod
  400. g1 = mod.giveme(10)
  401. g1.next()
  402. g1.next()
  403. pckl = pickle.dumps(g1)
  404. g2 = pickle.loads(pckl)
  405. assert list(g1) == list(g2)
  406. finally:
  407. del sys.modules['mod']
  408. def test_pickle_builtin_method(self):
  409. import pickle
  410. a_list = [1]
  411. meth1 = a_list.append
  412. pckl = pickle.dumps(meth1)
  413. meth2 = pickle.loads(pckl)
  414. meth1(1)
  415. meth2(2)
  416. assert a_list == [1, 1]
  417. assert meth2.im_self == [1, 2]
  418. unbound_meth = list.append
  419. unbound_meth2 = pickle.loads(pickle.dumps(unbound_meth))
  420. l = []
  421. unbound_meth2(l, 1)
  422. assert l == [1]
  423. def test_pickle_submodule(self):
  424. import pickle
  425. import sys, new
  426. mod = new.module('pack.mod')
  427. sys.modules['pack.mod'] = mod
  428. pack = new.module('pack')
  429. pack.mod = mod
  430. sys.modules['pack'] = pack
  431. import pack.mod
  432. pckl = pickle.dumps(pack.mod)
  433. result = pickle.loads(pckl)
  434. assert pack.mod is result