PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/objspace/std/test/test_liststrategies.py

https://bitbucket.org/pypy/pypy/
Python | 1061 lines | 884 code | 153 blank | 24 comment | 20 complexity | a86830f9c4ce7c60a936ed9fade90028 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. import sys
  2. import py
  3. from pypy.objspace.std.listobject import (
  4. W_ListObject, EmptyListStrategy, ObjectListStrategy, IntegerListStrategy,
  5. FloatListStrategy, BytesListStrategy, RangeListStrategy,
  6. SimpleRangeListStrategy, make_range_list, UnicodeListStrategy,
  7. IntOrFloatListStrategy)
  8. from pypy.objspace.std import listobject
  9. from pypy.objspace.std.test.test_listobject import TestW_ListObject
  10. class TestW_ListStrategies(TestW_ListObject):
  11. def test_check_strategy(self):
  12. space = self.space
  13. w = space.wrap
  14. wb = space.newbytes
  15. assert isinstance(W_ListObject(space, []).strategy, EmptyListStrategy)
  16. assert isinstance(W_ListObject(space, [w(1),wb('a')]).strategy, ObjectListStrategy)
  17. assert isinstance(W_ListObject(space, [w(1),w(2),w(3)]).strategy,
  18. IntegerListStrategy)
  19. assert isinstance(W_ListObject(space, [wb('a'), wb('b')]).strategy,
  20. BytesListStrategy)
  21. assert isinstance(W_ListObject(space, [w(u'a'), w(u'b')]).strategy,
  22. UnicodeListStrategy)
  23. assert isinstance(W_ListObject(space, [w(u'a'), wb('b')]).strategy,
  24. ObjectListStrategy) # mixed unicode and bytes
  25. def test_empty_to_any(self):
  26. space = self.space
  27. w = space.wrap
  28. wb = space.newbytes
  29. l = W_ListObject(space, [])
  30. assert isinstance(l.strategy, EmptyListStrategy)
  31. l.append(w((1,3)))
  32. assert isinstance(l.strategy, ObjectListStrategy)
  33. l = W_ListObject(space, [])
  34. assert isinstance(l.strategy, EmptyListStrategy)
  35. l.append(w(1))
  36. assert isinstance(l.strategy, IntegerListStrategy)
  37. l = W_ListObject(space, [])
  38. assert isinstance(l.strategy, EmptyListStrategy)
  39. l.append(wb('a'))
  40. assert isinstance(l.strategy, BytesListStrategy)
  41. l = W_ListObject(space, [])
  42. assert isinstance(l.strategy, EmptyListStrategy)
  43. l.append(w(u'a'))
  44. assert isinstance(l.strategy, UnicodeListStrategy)
  45. l = W_ListObject(space, [])
  46. assert isinstance(l.strategy, EmptyListStrategy)
  47. l.append(w(1.2))
  48. assert isinstance(l.strategy, FloatListStrategy)
  49. def test_int_to_any(self):
  50. l = W_ListObject(self.space,
  51. [self.space.wrap(1),self.space.wrap(2),self.space.wrap(3)])
  52. assert isinstance(l.strategy, IntegerListStrategy)
  53. l.append(self.space.wrap(4))
  54. assert isinstance(l.strategy, IntegerListStrategy)
  55. l.append(self.space.wrap('a'))
  56. assert isinstance(l.strategy, ObjectListStrategy)
  57. def test_string_to_any(self):
  58. l = W_ListObject(self.space,
  59. [self.space.newbytes('a'), self.space.newbytes('b'),
  60. self.space.newbytes('c')])
  61. assert isinstance(l.strategy, BytesListStrategy)
  62. l.append(self.space.newbytes('d'))
  63. assert isinstance(l.strategy, BytesListStrategy)
  64. l.append(self.space.wrap(3))
  65. assert isinstance(l.strategy, ObjectListStrategy)
  66. def test_unicode_to_any(self):
  67. space = self.space
  68. l = W_ListObject(space, [space.wrap(u'a'), space.wrap(u'b'), space.wrap(u'c')])
  69. assert isinstance(l.strategy, UnicodeListStrategy)
  70. l.append(space.wrap(u'd'))
  71. assert isinstance(l.strategy, UnicodeListStrategy)
  72. l.append(space.wrap(3))
  73. assert isinstance(l.strategy, ObjectListStrategy)
  74. def test_float_to_any(self):
  75. l = W_ListObject(self.space,
  76. [self.space.wrap(1.1),self.space.wrap(2.2),self.space.wrap(3.3)])
  77. assert isinstance(l.strategy, FloatListStrategy)
  78. l.append(self.space.wrap(4.4))
  79. assert isinstance(l.strategy, FloatListStrategy)
  80. l.append(self.space.wrap("a"))
  81. assert isinstance(l.strategy, ObjectListStrategy)
  82. def test_setitem(self):
  83. space = self.space
  84. w = space.wrap
  85. wb = space.newbytes
  86. # This should work if test_listobject.py passes
  87. l = W_ListObject(space, [w('a'),w('b'),w('c')])
  88. assert space.eq_w(l.getitem(0), w('a'))
  89. l.setitem(0, w('d'))
  90. assert space.eq_w(l.getitem(0), w('d'))
  91. assert isinstance(l.strategy, BytesListStrategy)
  92. # IntStrategy to ObjectStrategy
  93. l = W_ListObject(space, [w(1),w(2),w(3)])
  94. assert isinstance(l.strategy, IntegerListStrategy)
  95. l.setitem(0, w('d'))
  96. assert isinstance(l.strategy, ObjectListStrategy)
  97. # BytesStrategy to ObjectStrategy
  98. l = W_ListObject(space, [wb('a'),wb('b'),wb('c')])
  99. assert isinstance(l.strategy, BytesListStrategy)
  100. l.setitem(0, w(2))
  101. assert isinstance(l.strategy, ObjectListStrategy)
  102. # UnicodeStrategy to ObjectStrategy
  103. l = W_ListObject(space, [w(u'a'),w(u'b'),w(u'c')])
  104. assert isinstance(l.strategy, UnicodeListStrategy)
  105. l.setitem(0, w(2))
  106. assert isinstance(l.strategy, ObjectListStrategy)
  107. # FloatStrategy to ObjectStrategy
  108. l = W_ListObject(space, [w(1.2),w(2.3),w(3.4)])
  109. assert isinstance(l.strategy, FloatListStrategy)
  110. l.setitem(0, w("a"))
  111. assert isinstance(l.strategy, ObjectListStrategy)
  112. def test_insert(self):
  113. space = self.space
  114. w = space.wrap
  115. wb = space.newbytes
  116. # no change
  117. l = W_ListObject(space, [w(1),w(2),w(3)])
  118. assert isinstance(l.strategy, IntegerListStrategy)
  119. l.insert(3, w(4))
  120. assert isinstance(l.strategy, IntegerListStrategy)
  121. # BytesStrategy
  122. l = W_ListObject(space, [wb('a'),wb('b'),wb('c')])
  123. assert isinstance(l.strategy, BytesListStrategy)
  124. l.insert(3, w(2))
  125. assert isinstance(l.strategy, ObjectListStrategy)
  126. # UnicodeStrategy
  127. l = W_ListObject(space, [w(u'a'),w(u'b'),w(u'c')])
  128. assert isinstance(l.strategy, UnicodeListStrategy)
  129. l.insert(3, w(2))
  130. assert isinstance(l.strategy, ObjectListStrategy)
  131. # IntegerStrategy
  132. l = W_ListObject(space, [w(1),w(2),w(3)])
  133. assert isinstance(l.strategy, IntegerListStrategy)
  134. l.insert(3, w('d'))
  135. assert isinstance(l.strategy, ObjectListStrategy)
  136. # FloatStrategy
  137. l = W_ListObject(space, [w(1.1),w(2.2),w(3.3)])
  138. assert isinstance(l.strategy, FloatListStrategy)
  139. l.insert(3, w('d'))
  140. assert isinstance(l.strategy, ObjectListStrategy)
  141. # EmptyStrategy
  142. l = W_ListObject(space, [])
  143. assert isinstance(l.strategy, EmptyListStrategy)
  144. l.insert(0, wb('a'))
  145. assert isinstance(l.strategy, BytesListStrategy)
  146. l = W_ListObject(space, [])
  147. assert isinstance(l.strategy, EmptyListStrategy)
  148. l.insert(0, w(2))
  149. assert isinstance(l.strategy, IntegerListStrategy)
  150. def test_list_empty_after_delete(self):
  151. py.test.skip("return to emptyliststrategy is not supported anymore")
  152. l = W_ListObject(self.space, [self.space.wrap(3)])
  153. assert isinstance(l.strategy, IntegerListStrategy)
  154. l.deleteitem(0)
  155. assert isinstance(l.strategy, EmptyListStrategy)
  156. l = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2)])
  157. assert isinstance(l.strategy, IntegerListStrategy)
  158. l.deleteslice(0, 1, 2)
  159. assert isinstance(l.strategy, EmptyListStrategy)
  160. l = W_ListObject(self.space, [self.space.wrap(1)])
  161. assert isinstance(l.strategy, IntegerListStrategy)
  162. l.pop(-1)
  163. assert isinstance(l.strategy, EmptyListStrategy)
  164. def test_setslice(self):
  165. space = self.space
  166. w = space.wrap
  167. wb = space.newbytes
  168. l = W_ListObject(space, [])
  169. assert isinstance(l.strategy, EmptyListStrategy)
  170. l.setslice(0, 1, 2, W_ListObject(space, [w(1), w(2), w(3)]))
  171. assert isinstance(l.strategy, IntegerListStrategy)
  172. # IntegerStrategy to IntegerStrategy
  173. l = W_ListObject(space, [w(1), w(2), w(3)])
  174. assert isinstance(l.strategy, IntegerListStrategy)
  175. l.setslice(0, 1, 2, W_ListObject(space, [w(4), w(5), w(6)]))
  176. assert isinstance(l.strategy, IntegerListStrategy)
  177. # ObjectStrategy to ObjectStrategy
  178. l = W_ListObject(space, [w(1), w('b'), w(3)])
  179. assert isinstance(l.strategy, ObjectListStrategy)
  180. l.setslice(0, 1, 2, W_ListObject(space, [w(1), w(2), w(3)]))
  181. assert isinstance(l.strategy, ObjectListStrategy)
  182. # IntegerStrategy to ObjectStrategy
  183. l = W_ListObject(space, [w(1), w(2), w(3)])
  184. assert isinstance(l.strategy, IntegerListStrategy)
  185. l.setslice(0, 1, 2, W_ListObject(space, [w('a'), w('b'), w('c')]))
  186. assert isinstance(l.strategy, ObjectListStrategy)
  187. # BytesStrategy to ObjectStrategy
  188. l = W_ListObject(space, [wb('a'), wb('b'), wb('c')])
  189. assert isinstance(l.strategy, BytesListStrategy)
  190. l.setslice(0, 1, 2, W_ListObject(space, [w(1), w(2), w(3)]))
  191. assert isinstance(l.strategy, ObjectListStrategy)
  192. # UnicodeStrategy to ObjectStrategy
  193. l = W_ListObject(space, [w(u'a'), w(u'b'), w(u'c')])
  194. assert isinstance(l.strategy, UnicodeListStrategy)
  195. l.setslice(0, 1, 2, W_ListObject(space, [w(1), w(2), w(3)]))
  196. assert isinstance(l.strategy, ObjectListStrategy)
  197. # FloatStrategy to ObjectStrategy
  198. l = W_ListObject(space, [w(1.1), w(2.2), w(3.3)])
  199. assert isinstance(l.strategy, FloatListStrategy)
  200. l.setslice(0, 1, 2, W_ListObject(space, [w('a'), w(2), w(3)]))
  201. assert isinstance(l.strategy, ObjectListStrategy)
  202. def test_setslice_int_range(self):
  203. space = self.space
  204. w = space.wrap
  205. l = W_ListObject(space, [w(1), w(2), w(3)])
  206. assert isinstance(l.strategy, IntegerListStrategy)
  207. l.setslice(0, 1, 2, make_range_list(space, 5, 1, 4))
  208. assert isinstance(l.strategy, IntegerListStrategy)
  209. def test_setslice_List(self):
  210. space = self.space
  211. def wrapitems(items):
  212. items_w = []
  213. for i in items:
  214. items_w.append(space.wrap(i))
  215. return items_w
  216. def keep_other_strategy(w_list, start, step, length, w_other):
  217. other_strategy = w_other.strategy
  218. w_list.setslice(start, step, length, w_other)
  219. assert w_other.strategy is other_strategy
  220. l = W_ListObject(space, wrapitems([1,2,3,4,5]))
  221. other = W_ListObject(space, wrapitems(["a", "b", "c"]))
  222. keep_other_strategy(l, 0, 2, other.length(), other)
  223. assert l.strategy is space.fromcache(ObjectListStrategy)
  224. l = W_ListObject(space, wrapitems([1,2,3,4,5]))
  225. other = W_ListObject(space, wrapitems([6, 6, 6]))
  226. keep_other_strategy(l, 0, 2, other.length(), other)
  227. assert l.strategy is space.fromcache(IntegerListStrategy)
  228. l = W_ListObject(space, wrapitems(["a","b","c","d","e"]))
  229. other = W_ListObject(space, wrapitems(["a", "b", "c"]))
  230. keep_other_strategy(l, 0, 2, other.length(), other)
  231. assert l.strategy is space.fromcache(BytesListStrategy)
  232. l = W_ListObject(space, wrapitems([u"a",u"b",u"c",u"d",u"e"]))
  233. other = W_ListObject(space, wrapitems([u"a", u"b", u"c"]))
  234. keep_other_strategy(l, 0, 2, other.length(), other)
  235. assert l.strategy is space.fromcache(UnicodeListStrategy)
  236. l = W_ListObject(space, wrapitems([1.1, 2.2, 3.3, 4.4, 5.5]))
  237. other = W_ListObject(space, [])
  238. keep_other_strategy(l, 0, 1, l.length(), other)
  239. assert l.strategy is space.fromcache(FloatListStrategy)
  240. l = W_ListObject(space, wrapitems(["a",3,"c",4,"e"]))
  241. other = W_ListObject(space, wrapitems(["a", "b", "c"]))
  242. keep_other_strategy(l, 0, 2, other.length(), other)
  243. assert l.strategy is space.fromcache(ObjectListStrategy)
  244. l = W_ListObject(space, wrapitems(["a",3,"c",4,"e"]))
  245. other = W_ListObject(space, [])
  246. keep_other_strategy(l, 0, 1, l.length(), other)
  247. assert l.strategy is space.fromcache(ObjectListStrategy)
  248. def test_empty_setslice_with_objectlist(self):
  249. space = self.space
  250. w = space.wrap
  251. l = W_ListObject(space, [])
  252. o = W_ListObject(space, [space.wrap(1), space.wrap("2"), space.wrap(3)])
  253. l.setslice(0, 1, o.length(), o)
  254. assert l.getitems() == o.getitems()
  255. l.append(space.wrap(17))
  256. assert l.getitems() != o.getitems()
  257. def test_extend(self):
  258. space = self.space
  259. w = space.wrap
  260. l = W_ListObject(space, [])
  261. assert isinstance(l.strategy, EmptyListStrategy)
  262. l.extend(W_ListObject(space, [w(1), w(2), w(3)]))
  263. assert isinstance(l.strategy, IntegerListStrategy)
  264. l = W_ListObject(space, [w(1), w(2), w(3)])
  265. assert isinstance(l.strategy, IntegerListStrategy)
  266. l.extend(W_ListObject(space, [w('a'), w('b'), w('c')]))
  267. assert isinstance(l.strategy, ObjectListStrategy)
  268. l = W_ListObject(space, [w(1), w(2), w(3)])
  269. assert isinstance(l.strategy, IntegerListStrategy)
  270. l.extend(W_ListObject(space, [w(4), w(5), w(6)]))
  271. assert isinstance(l.strategy, IntegerListStrategy)
  272. l = W_ListObject(space, [w(1.1), w(2.2), w(3.3)])
  273. assert isinstance(l.strategy, FloatListStrategy)
  274. l.extend(W_ListObject(space, [w("abc"), w("def"), w("ghi")]))
  275. assert isinstance(l.strategy, ObjectListStrategy)
  276. def test_empty_extend_with_any(self):
  277. space = self.space
  278. w = space.wrap
  279. wb = space.newbytes
  280. empty = W_ListObject(space, [])
  281. assert isinstance(empty.strategy, EmptyListStrategy)
  282. empty.extend(W_ListObject(space, [w(1), w(2), w(3)]))
  283. assert isinstance(empty.strategy, IntegerListStrategy)
  284. empty = W_ListObject(space, [])
  285. assert isinstance(empty.strategy, EmptyListStrategy)
  286. empty.extend(W_ListObject(space, [wb("a"), wb("b"), wb("c")]))
  287. assert isinstance(empty.strategy, BytesListStrategy)
  288. empty = W_ListObject(space, [])
  289. assert isinstance(empty.strategy, EmptyListStrategy)
  290. empty.extend(W_ListObject(space, [w(u"a"), w(u"b"), w(u"c")]))
  291. assert isinstance(empty.strategy, UnicodeListStrategy)
  292. empty = W_ListObject(space, [])
  293. assert isinstance(empty.strategy, EmptyListStrategy)
  294. r = make_range_list(space, 1,3,7)
  295. empty.extend(r)
  296. assert isinstance(empty.strategy, RangeListStrategy)
  297. print empty.getitem(6)
  298. assert space.is_true(space.eq(empty.getitem(1), w(4)))
  299. empty = W_ListObject(space, [])
  300. assert isinstance(empty.strategy, EmptyListStrategy)
  301. r = make_range_list(space, 0, 1, 10)
  302. empty.extend(r)
  303. assert isinstance(empty.strategy, SimpleRangeListStrategy)
  304. assert space.is_true(space.eq(empty.getitem(1), w(1)))
  305. empty = W_ListObject(space, [])
  306. assert isinstance(empty.strategy, EmptyListStrategy)
  307. empty.extend(W_ListObject(space, [w(1), w(2), w(3)]))
  308. assert isinstance(empty.strategy, IntegerListStrategy)
  309. empty = W_ListObject(space, [])
  310. assert isinstance(empty.strategy, EmptyListStrategy)
  311. empty.extend(W_ListObject(space, [w(1.1), w(2.2), w(3.3)]))
  312. assert isinstance(empty.strategy, FloatListStrategy)
  313. empty = W_ListObject(space, [])
  314. assert isinstance(empty.strategy, EmptyListStrategy)
  315. empty.extend(W_ListObject(space, []))
  316. assert isinstance(empty.strategy, EmptyListStrategy)
  317. def test_extend_other_with_empty(self):
  318. space = self.space
  319. w = space.wrap
  320. l = W_ListObject(space, [w(1), w(2), w(3)])
  321. assert isinstance(l.strategy, IntegerListStrategy)
  322. l.extend(W_ListObject(space, []))
  323. assert isinstance(l.strategy, IntegerListStrategy)
  324. def test_rangelist(self):
  325. l = make_range_list(self.space, 1,3,7)
  326. assert isinstance(l.strategy, RangeListStrategy)
  327. v = l.pop(5)
  328. assert self.space.eq_w(v, self.space.wrap(16))
  329. assert isinstance(l.strategy, IntegerListStrategy)
  330. l = make_range_list(self.space, 1,3,7)
  331. assert isinstance(l.strategy, RangeListStrategy)
  332. v = l.pop(0)
  333. assert self.space.eq_w(v, self.space.wrap(1))
  334. assert isinstance(l.strategy, RangeListStrategy)
  335. v = l.pop(l.length() - 1)
  336. assert self.space.eq_w(v, self.space.wrap(19))
  337. assert isinstance(l.strategy, RangeListStrategy)
  338. v = l.pop_end()
  339. assert self.space.eq_w(v, self.space.wrap(16))
  340. assert isinstance(l.strategy, RangeListStrategy)
  341. l = make_range_list(self.space, 1,3,7)
  342. assert isinstance(l.strategy, RangeListStrategy)
  343. l.append(self.space.wrap("string"))
  344. assert isinstance(l.strategy, ObjectListStrategy)
  345. l = make_range_list(self.space, 1,1,5)
  346. assert isinstance(l.strategy, RangeListStrategy)
  347. l.append(self.space.wrap(19))
  348. assert isinstance(l.strategy, IntegerListStrategy)
  349. def test_simplerangelist(self):
  350. l = make_range_list(self.space, 0, 1, 10)
  351. assert isinstance(l.strategy, SimpleRangeListStrategy)
  352. v = l.pop(5)
  353. assert self.space.eq_w(v, self.space.wrap(5))
  354. assert isinstance(l.strategy, IntegerListStrategy)
  355. l = make_range_list(self.space, 0, 1, 10)
  356. assert isinstance(l.strategy, SimpleRangeListStrategy)
  357. v = l.pop(0)
  358. assert self.space.eq_w(v, self.space.wrap(0))
  359. assert isinstance(l.strategy, IntegerListStrategy)
  360. l = make_range_list(self.space, 0, 1, 10)
  361. assert isinstance(l.strategy, SimpleRangeListStrategy)
  362. v = l.pop_end()
  363. assert self.space.eq_w(v, self.space.wrap(9))
  364. assert isinstance(l.strategy, SimpleRangeListStrategy)
  365. v = l.pop_end()
  366. assert self.space.eq_w(v, self.space.wrap(8))
  367. assert isinstance(l.strategy, SimpleRangeListStrategy)
  368. l = make_range_list(self.space, 0, 1, 5)
  369. assert isinstance(l.strategy, SimpleRangeListStrategy)
  370. l.append(self.space.wrap("string"))
  371. assert isinstance(l.strategy, ObjectListStrategy)
  372. l = make_range_list(self.space, 0,1,5)
  373. assert isinstance(l.strategy, SimpleRangeListStrategy)
  374. l.append(self.space.wrap(19))
  375. assert isinstance(l.strategy, IntegerListStrategy)
  376. l = make_range_list(self.space, 0,1,5)
  377. assert isinstance(l.strategy, SimpleRangeListStrategy)
  378. assert l.find(self.space.wrap(0)) == 0
  379. assert l.find(self.space.wrap(4)) == 4
  380. try:
  381. l.find(self.space.wrap(5))
  382. except ValueError:
  383. pass
  384. else:
  385. assert False, "Did not raise ValueError"
  386. try:
  387. l.find(self.space.wrap(0), 5, 6)
  388. except ValueError:
  389. pass
  390. else:
  391. assert False, "Did not raise ValueError"
  392. assert l.length() == 5
  393. l = make_range_list(self.space, 0, 1, 1)
  394. assert self.space.eq_w(l.pop(0), self.space.wrap(0))
  395. l = make_range_list(self.space, 0, 1, 10)
  396. l.sort(False)
  397. assert isinstance(l.strategy, SimpleRangeListStrategy)
  398. assert self.space.eq_w(l.getitem(5), self.space.wrap(5))
  399. l = make_range_list(self.space, 0, 1, 1)
  400. assert self.space.eq_w(l.pop_end(), self.space.wrap(0))
  401. assert isinstance(l.strategy, EmptyListStrategy)
  402. def test_keep_range(self):
  403. # simple list
  404. l = make_range_list(self.space, 1,1,5)
  405. assert isinstance(l.strategy, RangeListStrategy)
  406. x = l.pop(0)
  407. assert self.space.eq_w(x, self.space.wrap(1))
  408. assert isinstance(l.strategy, RangeListStrategy)
  409. l.pop(l.length()-1)
  410. assert isinstance(l.strategy, RangeListStrategy)
  411. l.append(self.space.wrap(5))
  412. assert isinstance(l.strategy, IntegerListStrategy)
  413. # complex list
  414. l = make_range_list(self.space, 1,3,5)
  415. assert isinstance(l.strategy, RangeListStrategy)
  416. l.append(self.space.wrap(16))
  417. assert isinstance(l.strategy, IntegerListStrategy)
  418. def test_empty_range(self):
  419. l = make_range_list(self.space, 0, 0, 0)
  420. assert isinstance(l.strategy, EmptyListStrategy)
  421. l = make_range_list(self.space, 1, 1, 10)
  422. for i in l.getitems():
  423. assert isinstance(l.strategy, RangeListStrategy)
  424. l.pop(l.length()-1)
  425. assert isinstance(l.strategy, RangeListStrategy)
  426. def test_range_getslice_ovf(self):
  427. l = make_range_list(self.space, -sys.maxint, sys.maxint // 10, 21)
  428. assert isinstance(l.strategy, RangeListStrategy)
  429. l2 = l.getslice(0, 21, 11, 2)
  430. assert isinstance(l2.strategy, IntegerListStrategy)
  431. def test_range_setslice(self):
  432. l = make_range_list(self.space, 1, 3, 5)
  433. assert isinstance(l.strategy, RangeListStrategy)
  434. l.setslice(0, 1, 3, W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]))
  435. assert isinstance(l.strategy, IntegerListStrategy)
  436. def test_range_reverse_ovf(self):
  437. l = make_range_list(self.space, 0, -sys.maxint - 1, 1)
  438. assert isinstance(l.strategy, RangeListStrategy)
  439. l.reverse()
  440. assert isinstance(l.strategy, IntegerListStrategy)
  441. l = make_range_list(self.space, 0, -sys.maxint - 1, 1)
  442. l.sort(False)
  443. assert isinstance(l.strategy, IntegerListStrategy)
  444. def test_copy_list(self):
  445. l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
  446. l2 = l1.clone()
  447. l2.append(self.space.wrap(4))
  448. assert not l2 == l1.getitems()
  449. def test_getitems_does_not_copy_object_list(self):
  450. l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap("two"), self.space.wrap(3)])
  451. l2 = l1.getitems()
  452. l2.append(self.space.wrap("four"))
  453. assert l2 == l1.getitems()
  454. def test_clone(self):
  455. l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
  456. clone = l1.clone()
  457. assert isinstance(clone.strategy, IntegerListStrategy)
  458. clone.append(self.space.wrap(7))
  459. assert not self.space.eq_w(l1, clone)
  460. def test_add_does_not_use_getitems(self):
  461. l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
  462. l1.getitems = None
  463. l2 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
  464. l2.getitems = None
  465. l3 = self.space.add(l1, l2)
  466. l4 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3), self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
  467. assert self.space.eq_w(l3, l4)
  468. def test_add_of_range_and_int(self):
  469. l1 = make_range_list(self.space, 0, 1, 100)
  470. l2 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
  471. l3 = self.space.add(l2, l1)
  472. assert l3.strategy is l2.strategy
  473. def test_mul(self):
  474. l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
  475. l2 = l1.mul(2)
  476. l3 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3), self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
  477. assert self.space.eq_w(l2, l3)
  478. l4 = make_range_list(self.space, 1, 1, 3)
  479. assert self.space.eq_w(l4, l1)
  480. l5 = l4.mul(2)
  481. assert self.space.eq_w(l5, l3)
  482. def test_mul_same_strategy_but_different_object(self):
  483. l1 = W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
  484. l2 = l1.mul(1)
  485. assert self.space.eq_w(l1, l2)
  486. l1.setitem(0, self.space.wrap(5))
  487. assert not self.space.eq_w(l1, l2)
  488. def test_weird_rangelist_bug(self):
  489. space = self.space
  490. l = make_range_list(space, 1, 1, 3)
  491. # should not raise
  492. w_slice = space.newslice(space.wrap(15), space.wrap(2222), space.wrap(1))
  493. assert l.descr_getitem(space, w_slice).strategy == space.fromcache(EmptyListStrategy)
  494. def test_add_to_rangelist(self):
  495. l1 = make_range_list(self.space, 1, 1, 3)
  496. l2 = W_ListObject(self.space, [self.space.wrap(4), self.space.wrap(5)])
  497. l3 = l1.descr_add(self.space, l2)
  498. assert self.space.eq_w(l3, W_ListObject(self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3), self.space.wrap(4), self.space.wrap(5)]))
  499. def test_unicode(self):
  500. l1 = W_ListObject(self.space, [self.space.newbytes("eins"), self.space.newbytes("zwei")])
  501. assert isinstance(l1.strategy, BytesListStrategy)
  502. l2 = W_ListObject(self.space, [self.space.newunicode(u"eins"), self.space.newunicode(u"zwei")])
  503. assert isinstance(l2.strategy, UnicodeListStrategy)
  504. l3 = W_ListObject(self.space, [self.space.newbytes("eins"), self.space.newunicode(u"zwei")])
  505. assert isinstance(l3.strategy, ObjectListStrategy)
  506. def test_listview_bytes(self):
  507. space = self.space
  508. assert space.listview_bytes(space.wrap(1)) == None
  509. w_l = self.space.newlist([self.space.newbytes('a'), self.space.newbytes('b')])
  510. assert space.listview_bytes(w_l) == ["a", "b"]
  511. def test_listview_unicode(self):
  512. space = self.space
  513. assert space.listview_unicode(space.wrap(1)) == None
  514. w_l = self.space.newlist([self.space.wrap(u'a'), self.space.wrap(u'b')])
  515. assert space.listview_unicode(w_l) == [u"a", u"b"]
  516. def test_string_join_uses_listview_bytes(self):
  517. space = self.space
  518. w_l = self.space.newlist([self.space.wrap('a'), self.space.wrap('b')])
  519. w_l.getitems = None
  520. assert space.str_w(space.call_method(space.wrap("c"), "join", w_l)) == "acb"
  521. #
  522. # the same for unicode
  523. w_l = self.space.newlist([self.space.wrap(u'a'), self.space.wrap(u'b')])
  524. w_l.getitems = None
  525. assert space.unicode_w(space.call_method(space.wrap(u"c"), "join", w_l)) == u"acb"
  526. def test_string_join_returns_same_instance(self):
  527. space = self.space
  528. w_text = space.wrap("text")
  529. w_l = self.space.newlist([w_text])
  530. w_l.getitems = None
  531. assert space.is_w(space.call_method(space.wrap(" -- "), "join", w_l), w_text)
  532. #
  533. # the same for unicode
  534. w_text = space.wrap(u"text")
  535. w_l = self.space.newlist([w_text])
  536. w_l.getitems = None
  537. assert space.is_w(space.call_method(space.wrap(u" -- "), "join", w_l), w_text)
  538. def test_newlist_bytes(self):
  539. space = self.space
  540. l = ['a', 'b']
  541. w_l = self.space.newlist_bytes(l)
  542. assert isinstance(w_l.strategy, BytesListStrategy)
  543. assert space.listview_bytes(w_l) is l
  544. def test_string_uses_newlist_bytes(self):
  545. space = self.space
  546. w_s = space.newbytes("a b c")
  547. space.newlist = None
  548. try:
  549. w_l = space.call_method(w_s, "split")
  550. w_l2 = space.call_method(w_s, "split", space.newbytes(" "))
  551. w_l3 = space.call_method(w_s, "rsplit")
  552. w_l4 = space.call_method(w_s, "rsplit", space.newbytes(" "))
  553. finally:
  554. del space.newlist
  555. assert space.listview_bytes(w_l) == ["a", "b", "c"]
  556. assert space.listview_bytes(w_l2) == ["a", "b", "c"]
  557. assert space.listview_bytes(w_l3) == ["a", "b", "c"]
  558. assert space.listview_bytes(w_l4) == ["a", "b", "c"]
  559. def test_unicode_uses_newlist_unicode(self):
  560. space = self.space
  561. w_u = space.wrap(u"a b c")
  562. space.newlist = None
  563. try:
  564. w_l = space.call_method(w_u, "split")
  565. w_l2 = space.call_method(w_u, "split", space.wrap(" "))
  566. w_l3 = space.call_method(w_u, "rsplit")
  567. w_l4 = space.call_method(w_u, "rsplit", space.wrap(" "))
  568. finally:
  569. del space.newlist
  570. assert space.listview_unicode(w_l) == [u"a", u"b", u"c"]
  571. assert space.listview_unicode(w_l2) == [u"a", u"b", u"c"]
  572. assert space.listview_unicode(w_l3) == [u"a", u"b", u"c"]
  573. assert space.listview_unicode(w_l4) == [u"a", u"b", u"c"]
  574. def test_pop_without_argument_is_fast(self):
  575. space = self.space
  576. w_l = W_ListObject(space, [space.wrap(1), space.wrap(2), space.wrap(3)])
  577. w_l.pop = None
  578. w_res = w_l.descr_pop(space)
  579. assert space.unwrap(w_res) == 3
  580. def test_create_list_from_set(self):
  581. from pypy.objspace.std.setobject import W_SetObject
  582. from pypy.objspace.std.setobject import _initialize_set
  583. space = self.space
  584. w = space.wrap
  585. w_l = W_ListObject(space, [space.wrap(1), space.wrap(2), space.wrap(3)])
  586. w_set = W_SetObject(self.space)
  587. _initialize_set(self.space, w_set, w_l)
  588. w_set.iter = None # make sure fast path is used
  589. w_l2 = W_ListObject(space, [])
  590. space.call_method(w_l2, "__init__", w_set)
  591. w_l2.sort(False)
  592. assert space.eq_w(w_l, w_l2)
  593. w_l = W_ListObject(space, [space.wrap("a"), space.wrap("b"), space.wrap("c")])
  594. _initialize_set(self.space, w_set, w_l)
  595. space.call_method(w_l2, "__init__", w_set)
  596. w_l2.sort(False)
  597. assert space.eq_w(w_l, w_l2)
  598. def test_listview_bytes_list(self):
  599. space = self.space
  600. w_l = W_ListObject(space, [space.newbytes("a"), space.newbytes("b")])
  601. assert self.space.listview_bytes(w_l) == ["a", "b"]
  602. def test_listview_unicode_list(self):
  603. space = self.space
  604. w_l = W_ListObject(space, [space.wrap(u"a"), space.wrap(u"b")])
  605. assert self.space.listview_unicode(w_l) == [u"a", u"b"]
  606. def test_listview_int_list(self):
  607. space = self.space
  608. w_l = W_ListObject(space, [space.wrap(1), space.wrap(2), space.wrap(3)])
  609. assert self.space.listview_int(w_l) == [1, 2, 3]
  610. def test_listview_float_list(self):
  611. space = self.space
  612. w_l = W_ListObject(space, [space.wrap(1.1), space.wrap(2.2), space.wrap(3.3)])
  613. assert self.space.listview_float(w_l) == [1.1, 2.2, 3.3]
  614. def test_unpackiterable_int_list(self):
  615. space = self.space
  616. w_l = W_ListObject(space, [space.wrap(1), space.wrap(2), space.wrap(3)])
  617. list_orig = self.space.listview_int(w_l)
  618. list_copy = self.space.unpackiterable_int(w_l)
  619. assert list_orig == list_copy == [1, 2, 3]
  620. list_copy[0] = 42
  621. assert list_orig == [1, 2, 3]
  622. def test_int_or_float_special_nan(self):
  623. from rpython.rlib import longlong2float, rarithmetic
  624. space = self.space
  625. ll = rarithmetic.r_longlong(0xfffffffe12345678 - 2**64)
  626. specialnan = longlong2float.longlong2float(ll)
  627. w_l = W_ListObject(space, [space.wrap(1), space.wrap(specialnan)])
  628. assert isinstance(w_l.strategy, ObjectListStrategy)
  629. def test_int_or_float_int_overflow(self):
  630. if sys.maxint == 2147483647:
  631. py.test.skip("only on 64-bit")
  632. space = self.space
  633. ok1 = 2**31 - 1
  634. ok2 = -2**31
  635. ovf1 = ok1 + 1
  636. ovf2 = ok2 - 1
  637. w_l = W_ListObject(space, [space.wrap(1.2), space.wrap(ovf1)])
  638. assert isinstance(w_l.strategy, ObjectListStrategy)
  639. w_l = W_ListObject(space, [space.wrap(1.2), space.wrap(ovf2)])
  640. assert isinstance(w_l.strategy, ObjectListStrategy)
  641. w_l = W_ListObject(space, [space.wrap(1.2), space.wrap(ok1)])
  642. assert isinstance(w_l.strategy, IntOrFloatListStrategy)
  643. w_l = W_ListObject(space, [space.wrap(1.2), space.wrap(ok2)])
  644. assert isinstance(w_l.strategy, IntOrFloatListStrategy)
  645. def test_int_or_float_base(self):
  646. from rpython.rlib.rfloat import INFINITY, NAN
  647. space = self.space
  648. w = space.wrap
  649. w_l = W_ListObject(space, [space.wrap(1), space.wrap(2.3)])
  650. assert isinstance(w_l.strategy, IntOrFloatListStrategy)
  651. w_l.append(w(int(2**31-1)))
  652. assert isinstance(w_l.strategy, IntOrFloatListStrategy)
  653. w_l.append(w(-5.1))
  654. assert isinstance(w_l.strategy, IntOrFloatListStrategy)
  655. assert space.int_w(w_l.getitem(2)) == 2**31-1
  656. assert space.float_w(w_l.getitem(3)) == -5.1
  657. w_l.append(w(INFINITY))
  658. assert isinstance(w_l.strategy, IntOrFloatListStrategy)
  659. w_l.append(w(NAN))
  660. assert isinstance(w_l.strategy, IntOrFloatListStrategy)
  661. w_l.append(w(-NAN))
  662. assert isinstance(w_l.strategy, IntOrFloatListStrategy)
  663. w_l.append(space.newlist([]))
  664. assert isinstance(w_l.strategy, ObjectListStrategy)
  665. def test_int_or_float_from_integer(self):
  666. space = self.space
  667. w = space.wrap
  668. w_l = W_ListObject(space, [space.wrap(int(-2**31))])
  669. assert isinstance(w_l.strategy, IntegerListStrategy)
  670. w_l.append(w(-5.1))
  671. assert isinstance(w_l.strategy, IntOrFloatListStrategy)
  672. assert space.int_w(w_l.getitem(0)) == -2**31
  673. assert space.float_w(w_l.getitem(1)) == -5.1
  674. assert space.len_w(w_l) == 2
  675. def test_int_or_float_from_integer_overflow(self):
  676. if sys.maxint == 2147483647:
  677. py.test.skip("only on 64-bit")
  678. space = self.space
  679. w = space.wrap
  680. ovf1 = -2**31 - 1
  681. w_l = W_ListObject(space, [space.wrap(ovf1)])
  682. assert isinstance(w_l.strategy, IntegerListStrategy)
  683. w_l.append(w(-5.1))
  684. assert isinstance(w_l.strategy, ObjectListStrategy)
  685. assert space.int_w(w_l.getitem(0)) == ovf1
  686. assert space.float_w(w_l.getitem(1)) == -5.1
  687. assert space.len_w(w_l) == 2
  688. def test_int_or_float_from_integer_special_nan(self):
  689. from rpython.rlib import longlong2float, rarithmetic
  690. space = self.space
  691. w = space.wrap
  692. w_l = W_ListObject(space, [space.wrap(int(-2**31))])
  693. assert isinstance(w_l.strategy, IntegerListStrategy)
  694. ll = rarithmetic.r_longlong(0xfffffffe12345678 - 2**64)
  695. specialnan = longlong2float.longlong2float(ll)
  696. w_l.append(w(specialnan))
  697. assert isinstance(w_l.strategy, ObjectListStrategy)
  698. assert space.int_w(w_l.getitem(0)) == -2**31
  699. assert space.len_w(w_l) == 2
  700. def test_int_or_float_from_float(self):
  701. space = self.space
  702. w = space.wrap
  703. w_l = W_ListObject(space, [space.wrap(-42.5)])
  704. assert isinstance(w_l.strategy, FloatListStrategy)
  705. w_l.append(w(-15))
  706. assert isinstance(w_l.strategy, IntOrFloatListStrategy)
  707. assert space.float_w(w_l.getitem(0)) == -42.5
  708. assert space.int_w(w_l.getitem(1)) == -15
  709. assert space.len_w(w_l) == 2
  710. def test_int_or_float_from_float_int_overflow(self):
  711. if sys.maxint == 2147483647:
  712. py.test.skip("only on 64-bit")
  713. space = self.space
  714. w = space.wrap
  715. ovf1 = 2 ** 31
  716. w_l = W_ListObject(space, [space.wrap(1.2)])
  717. assert isinstance(w_l.strategy, FloatListStrategy)
  718. w_l.append(w(ovf1))
  719. assert isinstance(w_l.strategy, ObjectListStrategy)
  720. assert space.float_w(w_l.getitem(0)) == 1.2
  721. assert space.int_w(w_l.getitem(1)) == ovf1
  722. assert space.len_w(w_l) == 2
  723. def test_int_or_float_from_float_special_nan(self):
  724. from rpython.rlib import longlong2float, rarithmetic
  725. space = self.space
  726. w = space.wrap
  727. ll = rarithmetic.r_longlong(0xfffffffe12345678 - 2**64)
  728. specialnan = longlong2float.longlong2float(ll)
  729. w_l = W_ListObject(space, [space.wrap(specialnan)])
  730. assert isinstance(w_l.strategy, FloatListStrategy)
  731. w_l.append(w(42))
  732. assert isinstance(w_l.strategy, ObjectListStrategy)
  733. assert space.int_w(w_l.getitem(1)) == 42
  734. assert space.len_w(w_l) == 2
  735. def test_int_or_float_extend(self):
  736. space = self.space
  737. w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
  738. w_l2 = W_ListObject(space, [space.wrap(3), space.wrap(4.5)])
  739. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  740. assert isinstance(w_l2.strategy, IntOrFloatListStrategy)
  741. w_l1.extend(w_l2)
  742. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  743. assert space.unwrap(w_l1) == [0, 1.2, 3, 4.5]
  744. def test_int_or_float_extend_mixed_1(self):
  745. space = self.space
  746. w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
  747. w_l2 = W_ListObject(space, [space.wrap(3)])
  748. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  749. assert isinstance(w_l2.strategy, IntegerListStrategy)
  750. w_l1.extend(w_l2)
  751. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  752. assert [(type(x), x) for x in space.unwrap(w_l1)] == [
  753. (int, 0), (float, 1.2), (int, 3)]
  754. def test_int_or_float_extend_mixed_2(self):
  755. space = self.space
  756. w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
  757. w_l2 = W_ListObject(space, [space.wrap(3.4)])
  758. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  759. assert isinstance(w_l2.strategy, FloatListStrategy)
  760. w_l1.extend(w_l2)
  761. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  762. assert space.unwrap(w_l1) == [0, 1.2, 3.4]
  763. def test_int_or_float_extend_mixed_3(self):
  764. space = self.space
  765. w_l1 = W_ListObject(space, [space.wrap(0)])
  766. w_l2 = W_ListObject(space, [space.wrap(3.4)])
  767. assert isinstance(w_l1.strategy, IntegerListStrategy)
  768. assert isinstance(w_l2.strategy, FloatListStrategy)
  769. w_l1.extend(w_l2)
  770. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  771. assert space.unwrap(w_l1) == [0, 3.4]
  772. def test_int_or_float_extend_mixed_4(self):
  773. space = self.space
  774. w_l1 = W_ListObject(space, [space.wrap(0)])
  775. w_l2 = W_ListObject(space, [space.wrap(3.4), space.wrap(-2)])
  776. assert isinstance(w_l1.strategy, IntegerListStrategy)
  777. assert isinstance(w_l2.strategy, IntOrFloatListStrategy)
  778. w_l1.extend(w_l2)
  779. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  780. assert space.unwrap(w_l1) == [0, 3.4, -2]
  781. def test_int_or_float_extend_mixed_5(self):
  782. space = self.space
  783. w_l1 = W_ListObject(space, [space.wrap(-2.5)])
  784. w_l2 = W_ListObject(space, [space.wrap(42)])
  785. assert isinstance(w_l1.strategy, FloatListStrategy)
  786. assert isinstance(w_l2.strategy, IntegerListStrategy)
  787. w_l1.extend(w_l2)
  788. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  789. assert space.unwrap(w_l1) == [-2.5, 42]
  790. def test_int_or_float_extend_mixed_5_overflow(self):
  791. if sys.maxint == 2147483647:
  792. py.test.skip("only on 64-bit")
  793. space = self.space
  794. ovf1 = 2 ** 35
  795. w_l1 = W_ListObject(space, [space.wrap(-2.5)])
  796. w_l2 = W_ListObject(space, [space.wrap(ovf1)])
  797. assert isinstance(w_l1.strategy, FloatListStrategy)
  798. assert isinstance(w_l2.strategy, IntegerListStrategy)
  799. w_l1.extend(w_l2)
  800. assert isinstance(w_l1.strategy, ObjectListStrategy)
  801. assert space.unwrap(w_l1) == [-2.5, ovf1]
  802. def test_int_or_float_extend_mixed_6(self):
  803. space = self.space
  804. w_l1 = W_ListObject(space, [space.wrap(-2.5)])
  805. w_l2 = W_ListObject(space, [space.wrap(3.4), space.wrap(-2)])
  806. assert isinstance(w_l1.strategy, FloatListStrategy)
  807. assert isinstance(w_l2.strategy, IntOrFloatListStrategy)
  808. w_l1.extend(w_l2)
  809. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  810. assert space.unwrap(w_l1) == [-2.5, 3.4, -2]
  811. def test_int_or_float_setslice(self):
  812. space = self.space
  813. w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
  814. w_l2 = W_ListObject(space, [space.wrap(3), space.wrap(4.5)])
  815. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  816. assert isinstance(w_l2.strategy, IntOrFloatListStrategy)
  817. w_l1.setslice(0, 1, 1, w_l2)
  818. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  819. assert space.unwrap(w_l1) == [3, 4.5, 1.2]
  820. def test_int_or_float_setslice_mixed_1(self):
  821. space = self.space
  822. w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(12)])
  823. w_l2 = W_ListObject(space, [space.wrap(3.2), space.wrap(4.5)])
  824. assert isinstance(w_l1.strategy, IntegerListStrategy)
  825. assert isinstance(w_l2.strategy, FloatListStrategy)
  826. w_l1.setslice(0, 1, 1, w_l2)
  827. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  828. assert space.unwrap(w_l1) == [3.2, 4.5, 12]
  829. def test_int_or_float_setslice_mixed_2(self):
  830. space = self.space
  831. w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(12)])
  832. w_l2 = W_ListObject(space, [space.wrap(3.2), space.wrap(45)])
  833. assert isinstance(w_l1.strategy, IntegerListStrategy)
  834. assert isinstance(w_l2.strategy, IntOrFloatListStrategy)
  835. w_l1.setslice(0, 1, 1, w_l2)
  836. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  837. assert space.unwrap(w_l1) == [3.2, 45, 12]
  838. def test_int_or_float_setslice_mixed_3(self):
  839. space = self.space
  840. w_l1 = W_ListObject(space, [space.wrap(0.1), space.wrap(1.2)])
  841. w_l2 = W_ListObject(space, [space.wrap(32), space.wrap(45)])
  842. assert isinstance(w_l1.strategy, FloatListStrategy)
  843. assert isinstance(w_l2.strategy, IntegerListStrategy)
  844. w_l1.setslice(0, 1, 1, w_l2)
  845. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  846. assert space.unwrap(w_l1) == [32, 45, 1.2]
  847. def test_int_or_float_setslice_mixed_4(self):
  848. space = self.space
  849. w_l1 = W_ListObject(space, [space.wrap(0.1), space.wrap(1.2)])
  850. w_l2 = W_ListObject(space, [space.wrap(3.2), space.wrap(45)])
  851. assert isinstance(w_l1.strategy, FloatListStrategy)
  852. assert isinstance(w_l2.strategy, IntOrFloatListStrategy)
  853. w_l1.setslice(0, 1, 1, w_l2)
  854. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  855. assert space.unwrap(w_l1) == [3.2, 45, 1.2]
  856. def test_int_or_float_setslice_mixed_5(self):
  857. space = self.space
  858. w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
  859. w_l2 = W_ListObject(space, [space.wrap(32), space.wrap(45)])
  860. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  861. assert isinstance(w_l2.strategy, IntegerListStrategy)
  862. w_l1.setslice(0, 1, 1, w_l2)
  863. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  864. assert space.unwrap(w_l1) == [32, 45, 1.2]
  865. def test_int_or_float_setslice_mixed_5_overflow(self):
  866. if sys.maxint == 2147483647:
  867. py.test.skip("only on 64-bit")
  868. space = self.space
  869. ovf1 = 2 ** 35
  870. w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
  871. w_l2 = W_ListObject(space, [space.wrap(32), space.wrap(ovf1)])
  872. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  873. assert isinstance(w_l2.strategy, IntegerListStrategy)
  874. w_l1.setslice(0, 1, 1, w_l2)
  875. assert isinstance(w_l1.strategy, ObjectListStrategy)
  876. assert space.unwrap(w_l1) == [32, ovf1, 1.2]
  877. def test_int_or_float_setslice_mixed_6(self):
  878. space = self.space
  879. w_l1 = W_ListObject(space, [space.wrap(0), space.wrap(1.2)])
  880. w_l2 = W_ListObject(space, [space.wrap(3.2), space.wrap(4.5)])
  881. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  882. assert isinstance(w_l2.strategy, FloatListStrategy)
  883. w_l1.setslice(0, 1, 1, w_l2)
  884. assert isinstance(w_l1.strategy, IntOrFloatListStrategy)
  885. assert space.unwrap(w_l1) == [3.2, 4.5, 1.2]
  886. def test_int_or_float_sort(self):
  887. space = self.space
  888. w_l = W_ListObject(space, [space.wrap(1.2), space.wrap(1),
  889. space.wrap(1.0), space.wrap(5)])
  890. w_l.sort(False)
  891. assert [(type(x), x) for x in space.unwrap(w_l)] == [
  892. (int, 1), (float, 1.0), (float, 1.2), (int, 5)]
  893. w_l.sort(True)
  894. assert [(type(x), x) for x in space.unwrap(w_l)] == [
  895. (int, 5), (float, 1.2), (int, 1), (float, 1.0)]
  896. def test_stringstrategy_wraps_bytes(self):
  897. space = self.space
  898. wb = space.newbytes
  899. l = W_ListObject(space, [wb('a'), wb('b')])
  900. w_item = l.getitem(0)
  901. assert isinstance(w_item, space.StringObjectCls)
  902. class TestW_ListStrategiesDisabled:
  903. spaceconfig = {"objspace.std.withliststrategies": False}
  904. def test_check_strategy(self):
  905. assert isinstance(W_ListObject(self.space, []).strategy, ObjectListStrategy)
  906. assert isinstance(W_ListObject(self.space, [self.space.wrap(1),self.space.wrap('a')]).strategy, ObjectListStrategy)
  907. assert isinstance(W_ListObject(self.space, [self.space.wrap(1),self.space.wrap(2),self.space.wrap(3)]).strategy, ObjectListStrategy)
  908. assert isinstance(W_ListObject(self.space, [self.space.wrap('a'), self.space.wrap('b')]).strategy, ObjectListStrategy)