test_regression.py 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526
  1. import copy
  2. import sys
  3. import gc
  4. import tempfile
  5. import pytest
  6. from os import path
  7. from io import BytesIO
  8. from itertools import chain
  9. import numpy as np
  10. from numpy.testing import (
  11. assert_, assert_equal, IS_PYPY, assert_almost_equal,
  12. assert_array_equal, assert_array_almost_equal, assert_raises,
  13. assert_raises_regex, assert_warns, suppress_warnings,
  14. _assert_valid_refcount, HAS_REFCOUNT,
  15. )
  16. from numpy.testing._private.utils import _no_tracing, requires_memory
  17. from numpy.compat import asbytes, asunicode, pickle
  18. try:
  19. RecursionError
  20. except NameError:
  21. RecursionError = RuntimeError # python < 3.5
  22. class TestRegression:
  23. def test_invalid_round(self):
  24. # Ticket #3
  25. v = 4.7599999999999998
  26. assert_array_equal(np.array([v]), np.array(v))
  27. def test_mem_empty(self):
  28. # Ticket #7
  29. np.empty((1,), dtype=[('x', np.int64)])
  30. def test_pickle_transposed(self):
  31. # Ticket #16
  32. a = np.transpose(np.array([[2, 9], [7, 0], [3, 8]]))
  33. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  34. with BytesIO() as f:
  35. pickle.dump(a, f, protocol=proto)
  36. f.seek(0)
  37. b = pickle.load(f)
  38. assert_array_equal(a, b)
  39. def test_dtype_names(self):
  40. # Ticket #35
  41. # Should succeed
  42. np.dtype([(('name', 'label'), np.int32, 3)])
  43. def test_reduce(self):
  44. # Ticket #40
  45. assert_almost_equal(np.add.reduce([1., .5], dtype=None), 1.5)
  46. def test_zeros_order(self):
  47. # Ticket #43
  48. np.zeros([3], int, 'C')
  49. np.zeros([3], order='C')
  50. np.zeros([3], int, order='C')
  51. def test_asarray_with_order(self):
  52. # Check that nothing is done when order='F' and array C/F-contiguous
  53. a = np.ones(2)
  54. assert_(a is np.asarray(a, order='F'))
  55. def test_ravel_with_order(self):
  56. # Check that ravel works when order='F' and array C/F-contiguous
  57. a = np.ones(2)
  58. assert_(not a.ravel('F').flags.owndata)
  59. def test_sort_bigendian(self):
  60. # Ticket #47
  61. a = np.linspace(0, 10, 11)
  62. c = a.astype(np.dtype('<f8'))
  63. c.sort()
  64. assert_array_almost_equal(c, a)
  65. def test_negative_nd_indexing(self):
  66. # Ticket #49
  67. c = np.arange(125).reshape((5, 5, 5))
  68. origidx = np.array([-1, 0, 1])
  69. idx = np.array(origidx)
  70. c[idx]
  71. assert_array_equal(idx, origidx)
  72. def test_char_dump(self):
  73. # Ticket #50
  74. ca = np.char.array(np.arange(1000, 1010), itemsize=4)
  75. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  76. with BytesIO() as f:
  77. pickle.dump(ca, f, protocol=proto)
  78. f.seek(0)
  79. ca = np.load(f, allow_pickle=True)
  80. def test_noncontiguous_fill(self):
  81. # Ticket #58.
  82. a = np.zeros((5, 3))
  83. b = a[:, :2,]
  84. def rs():
  85. b.shape = (10,)
  86. assert_raises(AttributeError, rs)
  87. def test_bool(self):
  88. # Ticket #60
  89. np.bool_(1) # Should succeed
  90. def test_indexing1(self):
  91. # Ticket #64
  92. descr = [('x', [('y', [('z', 'c16', (2,)),]),]),]
  93. buffer = ((([6j, 4j],),),)
  94. h = np.array(buffer, dtype=descr)
  95. h['x']['y']['z']
  96. def test_indexing2(self):
  97. # Ticket #65
  98. descr = [('x', 'i4', (2,))]
  99. buffer = ([3, 2],)
  100. h = np.array(buffer, dtype=descr)
  101. h['x']
  102. def test_round(self):
  103. # Ticket #67
  104. x = np.array([1+2j])
  105. assert_almost_equal(x**(-1), [1/(1+2j)])
  106. def test_scalar_compare(self):
  107. # Trac Ticket #72
  108. # https://github.com/numpy/numpy/issues/565
  109. a = np.array(['test', 'auto'])
  110. assert_array_equal(a == 'auto', np.array([False, True]))
  111. assert_(a[1] == 'auto')
  112. assert_(a[0] != 'auto')
  113. b = np.linspace(0, 10, 11)
  114. # This should return true for now, but will eventually raise an error:
  115. with suppress_warnings() as sup:
  116. sup.filter(FutureWarning)
  117. assert_(b != 'auto')
  118. assert_(b[0] != 'auto')
  119. def test_unicode_swapping(self):
  120. # Ticket #79
  121. ulen = 1
  122. ucs_value = u'\U0010FFFF'
  123. ua = np.array([[[ucs_value*ulen]*2]*3]*4, dtype='U%s' % ulen)
  124. ua.newbyteorder() # Should succeed.
  125. def test_object_array_fill(self):
  126. # Ticket #86
  127. x = np.zeros(1, 'O')
  128. x.fill([])
  129. def test_mem_dtype_align(self):
  130. # Ticket #93
  131. assert_raises(TypeError, np.dtype,
  132. {'names':['a'], 'formats':['foo']}, align=1)
  133. def test_endian_bool_indexing(self):
  134. # Ticket #105
  135. a = np.arange(10., dtype='>f8')
  136. b = np.arange(10., dtype='<f8')
  137. xa = np.where((a > 2) & (a < 6))
  138. xb = np.where((b > 2) & (b < 6))
  139. ya = ((a > 2) & (a < 6))
  140. yb = ((b > 2) & (b < 6))
  141. assert_array_almost_equal(xa, ya.nonzero())
  142. assert_array_almost_equal(xb, yb.nonzero())
  143. assert_(np.all(a[ya] > 0.5))
  144. assert_(np.all(b[yb] > 0.5))
  145. def test_endian_where(self):
  146. # GitHub issue #369
  147. net = np.zeros(3, dtype='>f4')
  148. net[1] = 0.00458849
  149. net[2] = 0.605202
  150. max_net = net.max()
  151. test = np.where(net <= 0., max_net, net)
  152. correct = np.array([ 0.60520202, 0.00458849, 0.60520202])
  153. assert_array_almost_equal(test, correct)
  154. def test_endian_recarray(self):
  155. # Ticket #2185
  156. dt = np.dtype([
  157. ('head', '>u4'),
  158. ('data', '>u4', 2),
  159. ])
  160. buf = np.recarray(1, dtype=dt)
  161. buf[0]['head'] = 1
  162. buf[0]['data'][:] = [1, 1]
  163. h = buf[0]['head']
  164. d = buf[0]['data'][0]
  165. buf[0]['head'] = h
  166. buf[0]['data'][0] = d
  167. assert_(buf[0]['head'] == 1)
  168. def test_mem_dot(self):
  169. # Ticket #106
  170. x = np.random.randn(0, 1)
  171. y = np.random.randn(10, 1)
  172. # Dummy array to detect bad memory access:
  173. _z = np.ones(10)
  174. _dummy = np.empty((0, 10))
  175. z = np.lib.stride_tricks.as_strided(_z, _dummy.shape, _dummy.strides)
  176. np.dot(x, np.transpose(y), out=z)
  177. assert_equal(_z, np.ones(10))
  178. # Do the same for the built-in dot:
  179. np.core.multiarray.dot(x, np.transpose(y), out=z)
  180. assert_equal(_z, np.ones(10))
  181. def test_arange_endian(self):
  182. # Ticket #111
  183. ref = np.arange(10)
  184. x = np.arange(10, dtype='<f8')
  185. assert_array_equal(ref, x)
  186. x = np.arange(10, dtype='>f8')
  187. assert_array_equal(ref, x)
  188. def test_arange_inf_step(self):
  189. ref = np.arange(0, 1, 10)
  190. x = np.arange(0, 1, np.inf)
  191. assert_array_equal(ref, x)
  192. ref = np.arange(0, 1, -10)
  193. x = np.arange(0, 1, -np.inf)
  194. assert_array_equal(ref, x)
  195. ref = np.arange(0, -1, -10)
  196. x = np.arange(0, -1, -np.inf)
  197. assert_array_equal(ref, x)
  198. ref = np.arange(0, -1, 10)
  199. x = np.arange(0, -1, np.inf)
  200. assert_array_equal(ref, x)
  201. def test_arange_underflow_stop_and_step(self):
  202. finfo = np.finfo(np.float64)
  203. ref = np.arange(0, finfo.eps, 2 * finfo.eps)
  204. x = np.arange(0, finfo.eps, finfo.max)
  205. assert_array_equal(ref, x)
  206. ref = np.arange(0, finfo.eps, -2 * finfo.eps)
  207. x = np.arange(0, finfo.eps, -finfo.max)
  208. assert_array_equal(ref, x)
  209. ref = np.arange(0, -finfo.eps, -2 * finfo.eps)
  210. x = np.arange(0, -finfo.eps, -finfo.max)
  211. assert_array_equal(ref, x)
  212. ref = np.arange(0, -finfo.eps, 2 * finfo.eps)
  213. x = np.arange(0, -finfo.eps, finfo.max)
  214. assert_array_equal(ref, x)
  215. def test_argmax(self):
  216. # Ticket #119
  217. a = np.random.normal(0, 1, (4, 5, 6, 7, 8))
  218. for i in range(a.ndim):
  219. a.argmax(i) # Should succeed
  220. def test_mem_divmod(self):
  221. # Ticket #126
  222. for i in range(10):
  223. divmod(np.array([i])[0], 10)
  224. def test_hstack_invalid_dims(self):
  225. # Ticket #128
  226. x = np.arange(9).reshape((3, 3))
  227. y = np.array([0, 0, 0])
  228. assert_raises(ValueError, np.hstack, (x, y))
  229. def test_squeeze_type(self):
  230. # Ticket #133
  231. a = np.array([3])
  232. b = np.array(3)
  233. assert_(type(a.squeeze()) is np.ndarray)
  234. assert_(type(b.squeeze()) is np.ndarray)
  235. def test_add_identity(self):
  236. # Ticket #143
  237. assert_equal(0, np.add.identity)
  238. def test_numpy_float_python_long_addition(self):
  239. # Check that numpy float and python longs can be added correctly.
  240. a = np.float_(23.) + 2**135
  241. assert_equal(a, 23. + 2**135)
  242. def test_binary_repr_0(self):
  243. # Ticket #151
  244. assert_equal('0', np.binary_repr(0))
  245. def test_rec_iterate(self):
  246. # Ticket #160
  247. descr = np.dtype([('i', int), ('f', float), ('s', '|S3')])
  248. x = np.rec.array([(1, 1.1, '1.0'),
  249. (2, 2.2, '2.0')], dtype=descr)
  250. x[0].tolist()
  251. [i for i in x[0]]
  252. def test_unicode_string_comparison(self):
  253. # Ticket #190
  254. a = np.array('hello', np.unicode_)
  255. b = np.array('world')
  256. a == b
  257. def test_tobytes_FORTRANORDER_discontiguous(self):
  258. # Fix in r2836
  259. # Create non-contiguous Fortran ordered array
  260. x = np.array(np.random.rand(3, 3), order='F')[:, :2]
  261. assert_array_almost_equal(x.ravel(), np.frombuffer(x.tobytes()))
  262. def test_flat_assignment(self):
  263. # Correct behaviour of ticket #194
  264. x = np.empty((3, 1))
  265. x.flat = np.arange(3)
  266. assert_array_almost_equal(x, [[0], [1], [2]])
  267. x.flat = np.arange(3, dtype=float)
  268. assert_array_almost_equal(x, [[0], [1], [2]])
  269. def test_broadcast_flat_assignment(self):
  270. # Ticket #194
  271. x = np.empty((3, 1))
  272. def bfa():
  273. x[:] = np.arange(3)
  274. def bfb():
  275. x[:] = np.arange(3, dtype=float)
  276. assert_raises(ValueError, bfa)
  277. assert_raises(ValueError, bfb)
  278. def test_nonarray_assignment(self):
  279. # See also Issue gh-2870, test for non-array assignment
  280. # and equivalent unsafe casted array assignment
  281. a = np.arange(10)
  282. b = np.ones(10, dtype=bool)
  283. r = np.arange(10)
  284. def assign(a, b, c):
  285. a[b] = c
  286. assert_raises(ValueError, assign, a, b, np.nan)
  287. a[b] = np.array(np.nan) # but not this.
  288. assert_raises(ValueError, assign, a, r, np.nan)
  289. a[r] = np.array(np.nan)
  290. def test_unpickle_dtype_with_object(self):
  291. # Implemented in r2840
  292. dt = np.dtype([('x', int), ('y', np.object_), ('z', 'O')])
  293. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  294. with BytesIO() as f:
  295. pickle.dump(dt, f, protocol=proto)
  296. f.seek(0)
  297. dt_ = pickle.load(f)
  298. assert_equal(dt, dt_)
  299. def test_mem_array_creation_invalid_specification(self):
  300. # Ticket #196
  301. dt = np.dtype([('x', int), ('y', np.object_)])
  302. # Wrong way
  303. assert_raises(ValueError, np.array, [1, 'object'], dt)
  304. # Correct way
  305. np.array([(1, 'object')], dt)
  306. def test_recarray_single_element(self):
  307. # Ticket #202
  308. a = np.array([1, 2, 3], dtype=np.int32)
  309. b = a.copy()
  310. r = np.rec.array(a, shape=1, formats=['3i4'], names=['d'])
  311. assert_array_equal(a, b)
  312. assert_equal(a, r[0][0])
  313. def test_zero_sized_array_indexing(self):
  314. # Ticket #205
  315. tmp = np.array([])
  316. def index_tmp():
  317. tmp[np.array(10)]
  318. assert_raises(IndexError, index_tmp)
  319. def test_chararray_rstrip(self):
  320. # Ticket #222
  321. x = np.chararray((1,), 5)
  322. x[0] = b'a '
  323. x = x.rstrip()
  324. assert_equal(x[0], b'a')
  325. def test_object_array_shape(self):
  326. # Ticket #239
  327. assert_equal(np.array([[1, 2], 3, 4], dtype=object).shape, (3,))
  328. assert_equal(np.array([[1, 2], [3, 4]], dtype=object).shape, (2, 2))
  329. assert_equal(np.array([(1, 2), (3, 4)], dtype=object).shape, (2, 2))
  330. assert_equal(np.array([], dtype=object).shape, (0,))
  331. assert_equal(np.array([[], [], []], dtype=object).shape, (3, 0))
  332. assert_equal(np.array([[3, 4], [5, 6], None], dtype=object).shape, (3,))
  333. def test_mem_around(self):
  334. # Ticket #243
  335. x = np.zeros((1,))
  336. y = [0]
  337. decimal = 6
  338. np.around(abs(x-y), decimal) <= 10.0**(-decimal)
  339. def test_character_array_strip(self):
  340. # Ticket #246
  341. x = np.char.array(("x", "x ", "x "))
  342. for c in x:
  343. assert_equal(c, "x")
  344. def test_lexsort(self):
  345. # Lexsort memory error
  346. v = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
  347. assert_equal(np.lexsort(v), 0)
  348. def test_lexsort_invalid_sequence(self):
  349. # Issue gh-4123
  350. class BuggySequence:
  351. def __len__(self):
  352. return 4
  353. def __getitem__(self, key):
  354. raise KeyError
  355. assert_raises(KeyError, np.lexsort, BuggySequence())
  356. def test_lexsort_zerolen_custom_strides(self):
  357. # Ticket #14228
  358. xs = np.array([], dtype='i8')
  359. assert xs.strides == (8,)
  360. assert np.lexsort((xs,)).shape[0] == 0 # Works
  361. xs.strides = (16,)
  362. assert np.lexsort((xs,)).shape[0] == 0 # Was: MemoryError
  363. def test_lexsort_zerolen_custom_strides_2d(self):
  364. xs = np.array([], dtype='i8')
  365. xs.shape = (0, 2)
  366. xs.strides = (16, 16)
  367. assert np.lexsort((xs,), axis=0).shape[0] == 0
  368. xs.shape = (2, 0)
  369. xs.strides = (16, 16)
  370. assert np.lexsort((xs,), axis=0).shape[0] == 2
  371. def test_lexsort_invalid_axis(self):
  372. assert_raises(np.AxisError, np.lexsort, (np.arange(1),), axis=2)
  373. assert_raises(np.AxisError, np.lexsort, (np.array([]),), axis=1)
  374. assert_raises(np.AxisError, np.lexsort, (np.array(1),), axis=10)
  375. def test_lexsort_zerolen_element(self):
  376. dt = np.dtype([]) # a void dtype with no fields
  377. xs = np.empty(4, dt)
  378. assert np.lexsort((xs,)).shape[0] == xs.shape[0]
  379. def test_pickle_py2_bytes_encoding(self):
  380. # Check that arrays and scalars pickled on Py2 are
  381. # unpickleable on Py3 using encoding='bytes'
  382. test_data = [
  383. # (original, py2_pickle)
  384. (np.unicode_('\u6f2c'),
  385. b"cnumpy.core.multiarray\nscalar\np0\n(cnumpy\ndtype\np1\n"
  386. b"(S'U1'\np2\nI0\nI1\ntp3\nRp4\n(I3\nS'<'\np5\nNNNI4\nI4\n"
  387. b"I0\ntp6\nbS',o\\x00\\x00'\np7\ntp8\nRp9\n."),
  388. (np.array([9e123], dtype=np.float64),
  389. b"cnumpy.core.multiarray\n_reconstruct\np0\n(cnumpy\nndarray\n"
  390. b"p1\n(I0\ntp2\nS'b'\np3\ntp4\nRp5\n(I1\n(I1\ntp6\ncnumpy\ndtype\n"
  391. b"p7\n(S'f8'\np8\nI0\nI1\ntp9\nRp10\n(I3\nS'<'\np11\nNNNI-1\nI-1\n"
  392. b"I0\ntp12\nbI00\nS'O\\x81\\xb7Z\\xaa:\\xabY'\np13\ntp14\nb."),
  393. (np.array([(9e123,)], dtype=[('name', float)]),
  394. b"cnumpy.core.multiarray\n_reconstruct\np0\n(cnumpy\nndarray\np1\n"
  395. b"(I0\ntp2\nS'b'\np3\ntp4\nRp5\n(I1\n(I1\ntp6\ncnumpy\ndtype\np7\n"
  396. b"(S'V8'\np8\nI0\nI1\ntp9\nRp10\n(I3\nS'|'\np11\nN(S'name'\np12\ntp13\n"
  397. b"(dp14\ng12\n(g7\n(S'f8'\np15\nI0\nI1\ntp16\nRp17\n(I3\nS'<'\np18\nNNNI-1\n"
  398. b"I-1\nI0\ntp19\nbI0\ntp20\nsI8\nI1\nI0\ntp21\n"
  399. b"bI00\nS'O\\x81\\xb7Z\\xaa:\\xabY'\np22\ntp23\nb."),
  400. ]
  401. for original, data in test_data:
  402. result = pickle.loads(data, encoding='bytes')
  403. assert_equal(result, original)
  404. if isinstance(result, np.ndarray) and result.dtype.names is not None:
  405. for name in result.dtype.names:
  406. assert_(isinstance(name, str))
  407. def test_pickle_dtype(self):
  408. # Ticket #251
  409. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  410. pickle.dumps(float, protocol=proto)
  411. def test_swap_real(self):
  412. # Ticket #265
  413. assert_equal(np.arange(4, dtype='>c8').imag.max(), 0.0)
  414. assert_equal(np.arange(4, dtype='<c8').imag.max(), 0.0)
  415. assert_equal(np.arange(4, dtype='>c8').real.max(), 3.0)
  416. assert_equal(np.arange(4, dtype='<c8').real.max(), 3.0)
  417. def test_object_array_from_list(self):
  418. # Ticket #270
  419. assert_(np.array([1, 'A', None]).shape == (3,))
  420. def test_multiple_assign(self):
  421. # Ticket #273
  422. a = np.zeros((3, 1), int)
  423. a[[1, 2]] = 1
  424. def test_empty_array_type(self):
  425. assert_equal(np.array([]).dtype, np.zeros(0).dtype)
  426. def test_void_copyswap(self):
  427. dt = np.dtype([('one', '<i4'), ('two', '<i4')])
  428. x = np.array((1, 2), dtype=dt)
  429. x = x.byteswap()
  430. assert_(x['one'] > 1 and x['two'] > 2)
  431. def test_method_args(self):
  432. # Make sure methods and functions have same default axis
  433. # keyword and arguments
  434. funcs1 = ['argmax', 'argmin', 'sum', ('product', 'prod'),
  435. ('sometrue', 'any'),
  436. ('alltrue', 'all'), 'cumsum', ('cumproduct', 'cumprod'),
  437. 'ptp', 'cumprod', 'prod', 'std', 'var', 'mean',
  438. 'round', 'min', 'max', 'argsort', 'sort']
  439. funcs2 = ['compress', 'take', 'repeat']
  440. for func in funcs1:
  441. arr = np.random.rand(8, 7)
  442. arr2 = arr.copy()
  443. if isinstance(func, tuple):
  444. func_meth = func[1]
  445. func = func[0]
  446. else:
  447. func_meth = func
  448. res1 = getattr(arr, func_meth)()
  449. res2 = getattr(np, func)(arr2)
  450. if res1 is None:
  451. res1 = arr
  452. if res1.dtype.kind in 'uib':
  453. assert_((res1 == res2).all(), func)
  454. else:
  455. assert_(abs(res1-res2).max() < 1e-8, func)
  456. for func in funcs2:
  457. arr1 = np.random.rand(8, 7)
  458. arr2 = np.random.rand(8, 7)
  459. res1 = None
  460. if func == 'compress':
  461. arr1 = arr1.ravel()
  462. res1 = getattr(arr2, func)(arr1)
  463. else:
  464. arr2 = (15*arr2).astype(int).ravel()
  465. if res1 is None:
  466. res1 = getattr(arr1, func)(arr2)
  467. res2 = getattr(np, func)(arr1, arr2)
  468. assert_(abs(res1-res2).max() < 1e-8, func)
  469. def test_mem_lexsort_strings(self):
  470. # Ticket #298
  471. lst = ['abc', 'cde', 'fgh']
  472. np.lexsort((lst,))
  473. def test_fancy_index(self):
  474. # Ticket #302
  475. x = np.array([1, 2])[np.array([0])]
  476. assert_equal(x.shape, (1,))
  477. def test_recarray_copy(self):
  478. # Ticket #312
  479. dt = [('x', np.int16), ('y', np.float64)]
  480. ra = np.array([(1, 2.3)], dtype=dt)
  481. rb = np.rec.array(ra, dtype=dt)
  482. rb['x'] = 2.
  483. assert_(ra['x'] != rb['x'])
  484. def test_rec_fromarray(self):
  485. # Ticket #322
  486. x1 = np.array([[1, 2], [3, 4], [5, 6]])
  487. x2 = np.array(['a', 'dd', 'xyz'])
  488. x3 = np.array([1.1, 2, 3])
  489. np.rec.fromarrays([x1, x2, x3], formats="(2,)i4,a3,f8")
  490. def test_object_array_assign(self):
  491. x = np.empty((2, 2), object)
  492. x.flat[2] = (1, 2, 3)
  493. assert_equal(x.flat[2], (1, 2, 3))
  494. def test_ndmin_float64(self):
  495. # Ticket #324
  496. x = np.array([1, 2, 3], dtype=np.float64)
  497. assert_equal(np.array(x, dtype=np.float32, ndmin=2).ndim, 2)
  498. assert_equal(np.array(x, dtype=np.float64, ndmin=2).ndim, 2)
  499. def test_ndmin_order(self):
  500. # Issue #465 and related checks
  501. assert_(np.array([1, 2], order='C', ndmin=3).flags.c_contiguous)
  502. assert_(np.array([1, 2], order='F', ndmin=3).flags.f_contiguous)
  503. assert_(np.array(np.ones((2, 2), order='F'), ndmin=3).flags.f_contiguous)
  504. assert_(np.array(np.ones((2, 2), order='C'), ndmin=3).flags.c_contiguous)
  505. def test_mem_axis_minimization(self):
  506. # Ticket #327
  507. data = np.arange(5)
  508. data = np.add.outer(data, data)
  509. def test_mem_float_imag(self):
  510. # Ticket #330
  511. np.float64(1.0).imag
  512. def test_dtype_tuple(self):
  513. # Ticket #334
  514. assert_(np.dtype('i4') == np.dtype(('i4', ())))
  515. def test_dtype_posttuple(self):
  516. # Ticket #335
  517. np.dtype([('col1', '()i4')])
  518. def test_numeric_carray_compare(self):
  519. # Ticket #341
  520. assert_equal(np.array(['X'], 'c'), b'X')
  521. def test_string_array_size(self):
  522. # Ticket #342
  523. assert_raises(ValueError,
  524. np.array, [['X'], ['X', 'X', 'X']], '|S1')
  525. def test_dtype_repr(self):
  526. # Ticket #344
  527. dt1 = np.dtype(('uint32', 2))
  528. dt2 = np.dtype(('uint32', (2,)))
  529. assert_equal(dt1.__repr__(), dt2.__repr__())
  530. def test_reshape_order(self):
  531. # Make sure reshape order works.
  532. a = np.arange(6).reshape(2, 3, order='F')
  533. assert_equal(a, [[0, 2, 4], [1, 3, 5]])
  534. a = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
  535. b = a[:, 1]
  536. assert_equal(b.reshape(2, 2, order='F'), [[2, 6], [4, 8]])
  537. def test_reshape_zero_strides(self):
  538. # Issue #380, test reshaping of zero strided arrays
  539. a = np.ones(1)
  540. a = np.lib.stride_tricks.as_strided(a, shape=(5,), strides=(0,))
  541. assert_(a.reshape(5, 1).strides[0] == 0)
  542. def test_reshape_zero_size(self):
  543. # GitHub Issue #2700, setting shape failed for 0-sized arrays
  544. a = np.ones((0, 2))
  545. a.shape = (-1, 2)
  546. # Cannot test if NPY_RELAXED_STRIDES_CHECKING changes the strides.
  547. # With NPY_RELAXED_STRIDES_CHECKING the test becomes superfluous.
  548. @pytest.mark.skipif(np.ones(1).strides[0] == np.iinfo(np.intp).max,
  549. reason="Using relaxed stride checking")
  550. def test_reshape_trailing_ones_strides(self):
  551. # GitHub issue gh-2949, bad strides for trailing ones of new shape
  552. a = np.zeros(12, dtype=np.int32)[::2] # not contiguous
  553. strides_c = (16, 8, 8, 8)
  554. strides_f = (8, 24, 48, 48)
  555. assert_equal(a.reshape(3, 2, 1, 1).strides, strides_c)
  556. assert_equal(a.reshape(3, 2, 1, 1, order='F').strides, strides_f)
  557. assert_equal(np.array(0, dtype=np.int32).reshape(1, 1).strides, (4, 4))
  558. def test_repeat_discont(self):
  559. # Ticket #352
  560. a = np.arange(12).reshape(4, 3)[:, 2]
  561. assert_equal(a.repeat(3), [2, 2, 2, 5, 5, 5, 8, 8, 8, 11, 11, 11])
  562. def test_array_index(self):
  563. # Make sure optimization is not called in this case.
  564. a = np.array([1, 2, 3])
  565. a2 = np.array([[1, 2, 3]])
  566. assert_equal(a[np.where(a == 3)], a2[np.where(a2 == 3)])
  567. def test_object_argmax(self):
  568. a = np.array([1, 2, 3], dtype=object)
  569. assert_(a.argmax() == 2)
  570. def test_recarray_fields(self):
  571. # Ticket #372
  572. dt0 = np.dtype([('f0', 'i4'), ('f1', 'i4')])
  573. dt1 = np.dtype([('f0', 'i8'), ('f1', 'i8')])
  574. for a in [np.array([(1, 2), (3, 4)], "i4,i4"),
  575. np.rec.array([(1, 2), (3, 4)], "i4,i4"),
  576. np.rec.array([(1, 2), (3, 4)]),
  577. np.rec.fromarrays([(1, 2), (3, 4)], "i4,i4"),
  578. np.rec.fromarrays([(1, 2), (3, 4)])]:
  579. assert_(a.dtype in [dt0, dt1])
  580. def test_random_shuffle(self):
  581. # Ticket #374
  582. a = np.arange(5).reshape((5, 1))
  583. b = a.copy()
  584. np.random.shuffle(b)
  585. assert_equal(np.sort(b, axis=0), a)
  586. def test_refcount_vdot(self):
  587. # Changeset #3443
  588. _assert_valid_refcount(np.vdot)
  589. def test_startswith(self):
  590. ca = np.char.array(['Hi', 'There'])
  591. assert_equal(ca.startswith('H'), [True, False])
  592. def test_noncommutative_reduce_accumulate(self):
  593. # Ticket #413
  594. tosubtract = np.arange(5)
  595. todivide = np.array([2.0, 0.5, 0.25])
  596. assert_equal(np.subtract.reduce(tosubtract), -10)
  597. assert_equal(np.divide.reduce(todivide), 16.0)
  598. assert_array_equal(np.subtract.accumulate(tosubtract),
  599. np.array([0, -1, -3, -6, -10]))
  600. assert_array_equal(np.divide.accumulate(todivide),
  601. np.array([2., 4., 16.]))
  602. def test_convolve_empty(self):
  603. # Convolve should raise an error for empty input array.
  604. assert_raises(ValueError, np.convolve, [], [1])
  605. assert_raises(ValueError, np.convolve, [1], [])
  606. def test_multidim_byteswap(self):
  607. # Ticket #449
  608. r = np.array([(1, (0, 1, 2))], dtype="i2,3i2")
  609. assert_array_equal(r.byteswap(),
  610. np.array([(256, (0, 256, 512))], r.dtype))
  611. def test_string_NULL(self):
  612. # Changeset 3557
  613. assert_equal(np.array("a\x00\x0b\x0c\x00").item(),
  614. 'a\x00\x0b\x0c')
  615. def test_junk_in_string_fields_of_recarray(self):
  616. # Ticket #483
  617. r = np.array([[b'abc']], dtype=[('var1', '|S20')])
  618. assert_(asbytes(r['var1'][0][0]) == b'abc')
  619. def test_take_output(self):
  620. # Ensure that 'take' honours output parameter.
  621. x = np.arange(12).reshape((3, 4))
  622. a = np.take(x, [0, 2], axis=1)
  623. b = np.zeros_like(a)
  624. np.take(x, [0, 2], axis=1, out=b)
  625. assert_array_equal(a, b)
  626. def test_take_object_fail(self):
  627. # Issue gh-3001
  628. d = 123.
  629. a = np.array([d, 1], dtype=object)
  630. if HAS_REFCOUNT:
  631. ref_d = sys.getrefcount(d)
  632. try:
  633. a.take([0, 100])
  634. except IndexError:
  635. pass
  636. if HAS_REFCOUNT:
  637. assert_(ref_d == sys.getrefcount(d))
  638. def test_array_str_64bit(self):
  639. # Ticket #501
  640. s = np.array([1, np.nan], dtype=np.float64)
  641. with np.errstate(all='raise'):
  642. np.array_str(s) # Should succeed
  643. def test_frompyfunc_endian(self):
  644. # Ticket #503
  645. from math import radians
  646. uradians = np.frompyfunc(radians, 1, 1)
  647. big_endian = np.array([83.4, 83.5], dtype='>f8')
  648. little_endian = np.array([83.4, 83.5], dtype='<f8')
  649. assert_almost_equal(uradians(big_endian).astype(float),
  650. uradians(little_endian).astype(float))
  651. def test_mem_string_arr(self):
  652. # Ticket #514
  653. s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  654. t = []
  655. np.hstack((t, s))
  656. def test_arr_transpose(self):
  657. # Ticket #516
  658. x = np.random.rand(*(2,)*16)
  659. x.transpose(list(range(16))) # Should succeed
  660. def test_string_mergesort(self):
  661. # Ticket #540
  662. x = np.array(['a']*32)
  663. assert_array_equal(x.argsort(kind='m'), np.arange(32))
  664. def test_argmax_byteorder(self):
  665. # Ticket #546
  666. a = np.arange(3, dtype='>f')
  667. assert_(a[a.argmax()] == a.max())
  668. def test_rand_seed(self):
  669. # Ticket #555
  670. for l in np.arange(4):
  671. np.random.seed(l)
  672. def test_mem_deallocation_leak(self):
  673. # Ticket #562
  674. a = np.zeros(5, dtype=float)
  675. b = np.array(a, dtype=float)
  676. del a, b
  677. def test_mem_on_invalid_dtype(self):
  678. "Ticket #583"
  679. assert_raises(ValueError, np.fromiter, [['12', ''], ['13', '']], str)
  680. def test_dot_negative_stride(self):
  681. # Ticket #588
  682. x = np.array([[1, 5, 25, 125., 625]])
  683. y = np.array([[20.], [160.], [640.], [1280.], [1024.]])
  684. z = y[::-1].copy()
  685. y2 = y[::-1]
  686. assert_equal(np.dot(x, z), np.dot(x, y2))
  687. def test_object_casting(self):
  688. # This used to trigger the object-type version of
  689. # the bitwise_or operation, because float64 -> object
  690. # casting succeeds
  691. def rs():
  692. x = np.ones([484, 286])
  693. y = np.zeros([484, 286])
  694. x |= y
  695. assert_raises(TypeError, rs)
  696. def test_unicode_scalar(self):
  697. # Ticket #600
  698. x = np.array(["DROND", "DROND1"], dtype="U6")
  699. el = x[1]
  700. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  701. new = pickle.loads(pickle.dumps(el, protocol=proto))
  702. assert_equal(new, el)
  703. def test_arange_non_native_dtype(self):
  704. # Ticket #616
  705. for T in ('>f4', '<f4'):
  706. dt = np.dtype(T)
  707. assert_equal(np.arange(0, dtype=dt).dtype, dt)
  708. assert_equal(np.arange(0.5, dtype=dt).dtype, dt)
  709. assert_equal(np.arange(5, dtype=dt).dtype, dt)
  710. def test_bool_flat_indexing_invalid_nr_elements(self):
  711. s = np.ones(10, dtype=float)
  712. x = np.array((15,), dtype=float)
  713. def ia(x, s, v):
  714. x[(s > 0)] = v
  715. assert_raises(IndexError, ia, x, s, np.zeros(9, dtype=float))
  716. assert_raises(IndexError, ia, x, s, np.zeros(11, dtype=float))
  717. # Old special case (different code path):
  718. assert_raises(ValueError, ia, x.flat, s, np.zeros(9, dtype=float))
  719. assert_raises(ValueError, ia, x.flat, s, np.zeros(11, dtype=float))
  720. def test_mem_scalar_indexing(self):
  721. # Ticket #603
  722. x = np.array([0], dtype=float)
  723. index = np.array(0, dtype=np.int32)
  724. x[index]
  725. def test_binary_repr_0_width(self):
  726. assert_equal(np.binary_repr(0, width=3), '000')
  727. def test_fromstring(self):
  728. assert_equal(np.fromstring("12:09:09", dtype=int, sep=":"),
  729. [12, 9, 9])
  730. def test_searchsorted_variable_length(self):
  731. x = np.array(['a', 'aa', 'b'])
  732. y = np.array(['d', 'e'])
  733. assert_equal(x.searchsorted(y), [3, 3])
  734. def test_string_argsort_with_zeros(self):
  735. # Check argsort for strings containing zeros.
  736. x = np.frombuffer(b"\x00\x02\x00\x01", dtype="|S2")
  737. assert_array_equal(x.argsort(kind='m'), np.array([1, 0]))
  738. assert_array_equal(x.argsort(kind='q'), np.array([1, 0]))
  739. def test_string_sort_with_zeros(self):
  740. # Check sort for strings containing zeros.
  741. x = np.frombuffer(b"\x00\x02\x00\x01", dtype="|S2")
  742. y = np.frombuffer(b"\x00\x01\x00\x02", dtype="|S2")
  743. assert_array_equal(np.sort(x, kind="q"), y)
  744. def test_copy_detection_zero_dim(self):
  745. # Ticket #658
  746. np.indices((0, 3, 4)).T.reshape(-1, 3)
  747. def test_flat_byteorder(self):
  748. # Ticket #657
  749. x = np.arange(10)
  750. assert_array_equal(x.astype('>i4'), x.astype('<i4').flat[:])
  751. assert_array_equal(x.astype('>i4').flat[:], x.astype('<i4'))
  752. def test_sign_bit(self):
  753. x = np.array([0, -0.0, 0])
  754. assert_equal(str(np.abs(x)), '[0. 0. 0.]')
  755. def test_flat_index_byteswap(self):
  756. for dt in (np.dtype('<i4'), np.dtype('>i4')):
  757. x = np.array([-1, 0, 1], dtype=dt)
  758. assert_equal(x.flat[0].dtype, x[0].dtype)
  759. def test_copy_detection_corner_case(self):
  760. # Ticket #658
  761. np.indices((0, 3, 4)).T.reshape(-1, 3)
  762. # Cannot test if NPY_RELAXED_STRIDES_CHECKING changes the strides.
  763. # With NPY_RELAXED_STRIDES_CHECKING the test becomes superfluous,
  764. # 0-sized reshape itself is tested elsewhere.
  765. @pytest.mark.skipif(np.ones(1).strides[0] == np.iinfo(np.intp).max,
  766. reason="Using relaxed stride checking")
  767. def test_copy_detection_corner_case2(self):
  768. # Ticket #771: strides are not set correctly when reshaping 0-sized
  769. # arrays
  770. b = np.indices((0, 3, 4)).T.reshape(-1, 3)
  771. assert_equal(b.strides, (3 * b.itemsize, b.itemsize))
  772. def test_object_array_refcounting(self):
  773. # Ticket #633
  774. if not hasattr(sys, 'getrefcount'):
  775. return
  776. # NB. this is probably CPython-specific
  777. cnt = sys.getrefcount
  778. a = object()
  779. b = object()
  780. c = object()
  781. cnt0_a = cnt(a)
  782. cnt0_b = cnt(b)
  783. cnt0_c = cnt(c)
  784. # -- 0d -> 1-d broadcast slice assignment
  785. arr = np.zeros(5, dtype=np.object_)
  786. arr[:] = a
  787. assert_equal(cnt(a), cnt0_a + 5)
  788. arr[:] = b
  789. assert_equal(cnt(a), cnt0_a)
  790. assert_equal(cnt(b), cnt0_b + 5)
  791. arr[:2] = c
  792. assert_equal(cnt(b), cnt0_b + 3)
  793. assert_equal(cnt(c), cnt0_c + 2)
  794. del arr
  795. # -- 1-d -> 2-d broadcast slice assignment
  796. arr = np.zeros((5, 2), dtype=np.object_)
  797. arr0 = np.zeros(2, dtype=np.object_)
  798. arr0[0] = a
  799. assert_(cnt(a) == cnt0_a + 1)
  800. arr0[1] = b
  801. assert_(cnt(b) == cnt0_b + 1)
  802. arr[:, :] = arr0
  803. assert_(cnt(a) == cnt0_a + 6)
  804. assert_(cnt(b) == cnt0_b + 6)
  805. arr[:, 0] = None
  806. assert_(cnt(a) == cnt0_a + 1)
  807. del arr, arr0
  808. # -- 2-d copying + flattening
  809. arr = np.zeros((5, 2), dtype=np.object_)
  810. arr[:, 0] = a
  811. arr[:, 1] = b
  812. assert_(cnt(a) == cnt0_a + 5)
  813. assert_(cnt(b) == cnt0_b + 5)
  814. arr2 = arr.copy()
  815. assert_(cnt(a) == cnt0_a + 10)
  816. assert_(cnt(b) == cnt0_b + 10)
  817. arr2 = arr[:, 0].copy()
  818. assert_(cnt(a) == cnt0_a + 10)
  819. assert_(cnt(b) == cnt0_b + 5)
  820. arr2 = arr.flatten()
  821. assert_(cnt(a) == cnt0_a + 10)
  822. assert_(cnt(b) == cnt0_b + 10)
  823. del arr, arr2
  824. # -- concatenate, repeat, take, choose
  825. arr1 = np.zeros((5, 1), dtype=np.object_)
  826. arr2 = np.zeros((5, 1), dtype=np.object_)
  827. arr1[...] = a
  828. arr2[...] = b
  829. assert_(cnt(a) == cnt0_a + 5)
  830. assert_(cnt(b) == cnt0_b + 5)
  831. tmp = np.concatenate((arr1, arr2))
  832. assert_(cnt(a) == cnt0_a + 5 + 5)
  833. assert_(cnt(b) == cnt0_b + 5 + 5)
  834. tmp = arr1.repeat(3, axis=0)
  835. assert_(cnt(a) == cnt0_a + 5 + 3*5)
  836. tmp = arr1.take([1, 2, 3], axis=0)
  837. assert_(cnt(a) == cnt0_a + 5 + 3)
  838. x = np.array([[0], [1], [0], [1], [1]], int)
  839. tmp = x.choose(arr1, arr2)
  840. assert_(cnt(a) == cnt0_a + 5 + 2)
  841. assert_(cnt(b) == cnt0_b + 5 + 3)
  842. del tmp # Avoid pyflakes unused variable warning
  843. def test_mem_custom_float_to_array(self):
  844. # Ticket 702
  845. class MyFloat:
  846. def __float__(self):
  847. return 1.0
  848. tmp = np.atleast_1d([MyFloat()])
  849. tmp.astype(float) # Should succeed
  850. def test_object_array_refcount_self_assign(self):
  851. # Ticket #711
  852. class VictimObject:
  853. deleted = False
  854. def __del__(self):
  855. self.deleted = True
  856. d = VictimObject()
  857. arr = np.zeros(5, dtype=np.object_)
  858. arr[:] = d
  859. del d
  860. arr[:] = arr # refcount of 'd' might hit zero here
  861. assert_(not arr[0].deleted)
  862. arr[:] = arr # trying to induce a segfault by doing it again...
  863. assert_(not arr[0].deleted)
  864. def test_mem_fromiter_invalid_dtype_string(self):
  865. x = [1, 2, 3]
  866. assert_raises(ValueError,
  867. np.fromiter, [xi for xi in x], dtype='S')
  868. def test_reduce_big_object_array(self):
  869. # Ticket #713
  870. oldsize = np.setbufsize(10*16)
  871. a = np.array([None]*161, object)
  872. assert_(not np.any(a))
  873. np.setbufsize(oldsize)
  874. def test_mem_0d_array_index(self):
  875. # Ticket #714
  876. np.zeros(10)[np.array(0)]
  877. def test_nonnative_endian_fill(self):
  878. # Non-native endian arrays were incorrectly filled with scalars
  879. # before r5034.
  880. if sys.byteorder == 'little':
  881. dtype = np.dtype('>i4')
  882. else:
  883. dtype = np.dtype('<i4')
  884. x = np.empty([1], dtype=dtype)
  885. x.fill(1)
  886. assert_equal(x, np.array([1], dtype=dtype))
  887. def test_dot_alignment_sse2(self):
  888. # Test for ticket #551, changeset r5140
  889. x = np.zeros((30, 40))
  890. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  891. y = pickle.loads(pickle.dumps(x, protocol=proto))
  892. # y is now typically not aligned on a 8-byte boundary
  893. z = np.ones((1, y.shape[0]))
  894. # This shouldn't cause a segmentation fault:
  895. np.dot(z, y)
  896. def test_astype_copy(self):
  897. # Ticket #788, changeset r5155
  898. # The test data file was generated by scipy.io.savemat.
  899. # The dtype is float64, but the isbuiltin attribute is 0.
  900. data_dir = path.join(path.dirname(__file__), 'data')
  901. filename = path.join(data_dir, "astype_copy.pkl")
  902. with open(filename, 'rb') as f:
  903. xp = pickle.load(f, encoding='latin1')
  904. xpd = xp.astype(np.float64)
  905. assert_((xp.__array_interface__['data'][0] !=
  906. xpd.__array_interface__['data'][0]))
  907. def test_compress_small_type(self):
  908. # Ticket #789, changeset 5217.
  909. # compress with out argument segfaulted if cannot cast safely
  910. import numpy as np
  911. a = np.array([[1, 2], [3, 4]])
  912. b = np.zeros((2, 1), dtype=np.single)
  913. try:
  914. a.compress([True, False], axis=1, out=b)
  915. raise AssertionError("compress with an out which cannot be "
  916. "safely casted should not return "
  917. "successfully")
  918. except TypeError:
  919. pass
  920. def test_attributes(self):
  921. # Ticket #791
  922. class TestArray(np.ndarray):
  923. def __new__(cls, data, info):
  924. result = np.array(data)
  925. result = result.view(cls)
  926. result.info = info
  927. return result
  928. def __array_finalize__(self, obj):
  929. self.info = getattr(obj, 'info', '')
  930. dat = TestArray([[1, 2, 3, 4], [5, 6, 7, 8]], 'jubba')
  931. assert_(dat.info == 'jubba')
  932. dat.resize((4, 2))
  933. assert_(dat.info == 'jubba')
  934. dat.sort()
  935. assert_(dat.info == 'jubba')
  936. dat.fill(2)
  937. assert_(dat.info == 'jubba')
  938. dat.put([2, 3, 4], [6, 3, 4])
  939. assert_(dat.info == 'jubba')
  940. dat.setfield(4, np.int32, 0)
  941. assert_(dat.info == 'jubba')
  942. dat.setflags()
  943. assert_(dat.info == 'jubba')
  944. assert_(dat.all(1).info == 'jubba')
  945. assert_(dat.any(1).info == 'jubba')
  946. assert_(dat.argmax(1).info == 'jubba')
  947. assert_(dat.argmin(1).info == 'jubba')
  948. assert_(dat.argsort(1).info == 'jubba')
  949. assert_(dat.astype(TestArray).info == 'jubba')
  950. assert_(dat.byteswap().info == 'jubba')
  951. assert_(dat.clip(2, 7).info == 'jubba')
  952. assert_(dat.compress([0, 1, 1]).info == 'jubba')
  953. assert_(dat.conj().info == 'jubba')
  954. assert_(dat.conjugate().info == 'jubba')
  955. assert_(dat.copy().info == 'jubba')
  956. dat2 = TestArray([2, 3, 1, 0], 'jubba')
  957. choices = [[0, 1, 2, 3], [10, 11, 12, 13],
  958. [20, 21, 22, 23], [30, 31, 32, 33]]
  959. assert_(dat2.choose(choices).info == 'jubba')
  960. assert_(dat.cumprod(1).info == 'jubba')
  961. assert_(dat.cumsum(1).info == 'jubba')
  962. assert_(dat.diagonal().info == 'jubba')
  963. assert_(dat.flatten().info == 'jubba')
  964. assert_(dat.getfield(np.int32, 0).info == 'jubba')
  965. assert_(dat.imag.info == 'jubba')
  966. assert_(dat.max(1).info == 'jubba')
  967. assert_(dat.mean(1).info == 'jubba')
  968. assert_(dat.min(1).info == 'jubba')
  969. assert_(dat.newbyteorder().info == 'jubba')
  970. assert_(dat.prod(1).info == 'jubba')
  971. assert_(dat.ptp(1).info == 'jubba')
  972. assert_(dat.ravel().info == 'jubba')
  973. assert_(dat.real.info == 'jubba')
  974. assert_(dat.repeat(2).info == 'jubba')
  975. assert_(dat.reshape((2, 4)).info == 'jubba')
  976. assert_(dat.round().info == 'jubba')
  977. assert_(dat.squeeze().info == 'jubba')
  978. assert_(dat.std(1).info == 'jubba')
  979. assert_(dat.sum(1).info == 'jubba')
  980. assert_(dat.swapaxes(0, 1).info == 'jubba')
  981. assert_(dat.take([2, 3, 5]).info == 'jubba')
  982. assert_(dat.transpose().info == 'jubba')
  983. assert_(dat.T.info == 'jubba')
  984. assert_(dat.var(1).info == 'jubba')
  985. assert_(dat.view(TestArray).info == 'jubba')
  986. # These methods do not preserve subclasses
  987. assert_(type(dat.nonzero()[0]) is np.ndarray)
  988. assert_(type(dat.nonzero()[1]) is np.ndarray)
  989. def test_recarray_tolist(self):
  990. # Ticket #793, changeset r5215
  991. # Comparisons fail for NaN, so we can't use random memory
  992. # for the test.
  993. buf = np.zeros(40, dtype=np.int8)
  994. a = np.recarray(2, formats="i4,f8,f8", names="id,x,y", buf=buf)
  995. b = a.tolist()
  996. assert_( a[0].tolist() == b[0])
  997. assert_( a[1].tolist() == b[1])
  998. def test_nonscalar_item_method(self):
  999. # Make sure that .item() fails graciously when it should
  1000. a = np.arange(5)
  1001. assert_raises(ValueError, a.item)
  1002. def test_char_array_creation(self):
  1003. a = np.array('123', dtype='c')
  1004. b = np.array([b'1', b'2', b'3'])
  1005. assert_equal(a, b)
  1006. def test_unaligned_unicode_access(self):
  1007. # Ticket #825
  1008. for i in range(1, 9):
  1009. msg = 'unicode offset: %d chars' % i
  1010. t = np.dtype([('a', 'S%d' % i), ('b', 'U2')])
  1011. x = np.array([(b'a', u'b')], dtype=t)
  1012. assert_equal(str(x), "[(b'a', 'b')]", err_msg=msg)
  1013. def test_sign_for_complex_nan(self):
  1014. # Ticket 794.
  1015. with np.errstate(invalid='ignore'):
  1016. C = np.array([-np.inf, -2+1j, 0, 2-1j, np.inf, np.nan])
  1017. have = np.sign(C)
  1018. want = np.array([-1+0j, -1+0j, 0+0j, 1+0j, 1+0j, np.nan])
  1019. assert_equal(have, want)
  1020. def test_for_equal_names(self):
  1021. # Ticket #674
  1022. dt = np.dtype([('foo', float), ('bar', float)])
  1023. a = np.zeros(10, dt)
  1024. b = list(a.dtype.names)
  1025. b[0] = "notfoo"
  1026. a.dtype.names = b
  1027. assert_(a.dtype.names[0] == "notfoo")
  1028. assert_(a.dtype.names[1] == "bar")
  1029. def test_for_object_scalar_creation(self):
  1030. # Ticket #816
  1031. a = np.object_()
  1032. b = np.object_(3)
  1033. b2 = np.object_(3.0)
  1034. c = np.object_([4, 5])
  1035. d = np.object_([None, {}, []])
  1036. assert_(a is None)
  1037. assert_(type(b) is int)
  1038. assert_(type(b2) is float)
  1039. assert_(type(c) is np.ndarray)
  1040. assert_(c.dtype == object)
  1041. assert_(d.dtype == object)
  1042. def test_array_resize_method_system_error(self):
  1043. # Ticket #840 - order should be an invalid keyword.
  1044. x = np.array([[0, 1], [2, 3]])
  1045. assert_raises(TypeError, x.resize, (2, 2), order='C')
  1046. def test_for_zero_length_in_choose(self):
  1047. "Ticket #882"
  1048. a = np.array(1)
  1049. assert_raises(ValueError, lambda x: x.choose([]), a)
  1050. def test_array_ndmin_overflow(self):
  1051. "Ticket #947."
  1052. assert_raises(ValueError, lambda: np.array([1], ndmin=33))
  1053. def test_void_scalar_with_titles(self):
  1054. # No ticket
  1055. data = [('john', 4), ('mary', 5)]
  1056. dtype1 = [(('source:yy', 'name'), 'O'), (('source:xx', 'id'), int)]
  1057. arr = np.array(data, dtype=dtype1)
  1058. assert_(arr[0][0] == 'john')
  1059. assert_(arr[0][1] == 4)
  1060. def test_void_scalar_constructor(self):
  1061. #Issue #1550
  1062. #Create test string data, construct void scalar from data and assert
  1063. #that void scalar contains original data.
  1064. test_string = np.array("test")
  1065. test_string_void_scalar = np.core.multiarray.scalar(
  1066. np.dtype(("V", test_string.dtype.itemsize)), test_string.tobytes())
  1067. assert_(test_string_void_scalar.view(test_string.dtype) == test_string)
  1068. #Create record scalar, construct from data and assert that
  1069. #reconstructed scalar is correct.
  1070. test_record = np.ones((), "i,i")
  1071. test_record_void_scalar = np.core.multiarray.scalar(
  1072. test_record.dtype, test_record.tobytes())
  1073. assert_(test_record_void_scalar == test_record)
  1074. # Test pickle and unpickle of void and record scalars
  1075. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  1076. assert_(pickle.loads(
  1077. pickle.dumps(test_string, protocol=proto)) == test_string)
  1078. assert_(pickle.loads(
  1079. pickle.dumps(test_record, protocol=proto)) == test_record)
  1080. @_no_tracing
  1081. def test_blasdot_uninitialized_memory(self):
  1082. # Ticket #950
  1083. for m in [0, 1, 2]:
  1084. for n in [0, 1, 2]:
  1085. for k in range(3):
  1086. # Try to ensure that x->data contains non-zero floats
  1087. x = np.array([123456789e199], dtype=np.float64)
  1088. if IS_PYPY:
  1089. x.resize((m, 0), refcheck=False)
  1090. else:
  1091. x.resize((m, 0))
  1092. y = np.array([123456789e199], dtype=np.float64)
  1093. if IS_PYPY:
  1094. y.resize((0, n), refcheck=False)
  1095. else:
  1096. y.resize((0, n))
  1097. # `dot` should just return zero (m, n) matrix
  1098. z = np.dot(x, y)
  1099. assert_(np.all(z == 0))
  1100. assert_(z.shape == (m, n))
  1101. def test_zeros(self):
  1102. # Regression test for #1061.
  1103. # Set a size which cannot fit into a 64 bits signed integer
  1104. sz = 2 ** 64
  1105. with assert_raises_regex(ValueError,
  1106. 'Maximum allowed dimension exceeded'):
  1107. np.empty(sz)
  1108. def test_huge_arange(self):
  1109. # Regression test for #1062.
  1110. # Set a size which cannot fit into a 64 bits signed integer
  1111. sz = 2 ** 64
  1112. with assert_raises_regex(ValueError,
  1113. 'Maximum allowed size exceeded'):
  1114. np.arange(sz)
  1115. assert_(np.size == sz)
  1116. def test_fromiter_bytes(self):
  1117. # Ticket #1058
  1118. a = np.fromiter(list(range(10)), dtype='b')
  1119. b = np.fromiter(list(range(10)), dtype='B')
  1120. assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
  1121. assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
  1122. def test_array_from_sequence_scalar_array(self):
  1123. # Ticket #1078: segfaults when creating an array with a sequence of
  1124. # 0d arrays.
  1125. a = np.array((np.ones(2), np.array(2)), dtype=object)
  1126. assert_equal(a.shape, (2,))
  1127. assert_equal(a.dtype, np.dtype(object))
  1128. assert_equal(a[0], np.ones(2))
  1129. assert_equal(a[1], np.array(2))
  1130. a = np.array(((1,), np.array(1)), dtype=object)
  1131. assert_equal(a.shape, (2,))
  1132. assert_equal(a.dtype, np.dtype(object))
  1133. assert_equal(a[0], (1,))
  1134. assert_equal(a[1], np.array(1))
  1135. def test_array_from_sequence_scalar_array2(self):
  1136. # Ticket #1081: weird array with strange input...
  1137. t = np.array([np.array([]), np.array(0, object)], dtype=object)
  1138. assert_equal(t.shape, (2,))
  1139. assert_equal(t.dtype, np.dtype(object))
  1140. def test_array_too_big(self):
  1141. # Ticket #1080.
  1142. assert_raises(ValueError, np.zeros, [975]*7, np.int8)
  1143. assert_raises(ValueError, np.zeros, [26244]*5, np.int8)
  1144. def test_dtype_keyerrors_(self):
  1145. # Ticket #1106.
  1146. dt = np.dtype([('f1', np.uint)])
  1147. assert_raises(KeyError, dt.__getitem__, "f2")
  1148. assert_raises(IndexError, dt.__getitem__, 1)
  1149. assert_raises(TypeError, dt.__getitem__, 0.0)
  1150. def test_lexsort_buffer_length(self):
  1151. # Ticket #1217, don't segfault.
  1152. a = np.ones(100, dtype=np.int8)
  1153. b = np.ones(100, dtype=np.int32)
  1154. i = np.lexsort((a[::-1], b))
  1155. assert_equal(i, np.arange(100, dtype=int))
  1156. def test_object_array_to_fixed_string(self):
  1157. # Ticket #1235.
  1158. a = np.array(['abcdefgh', 'ijklmnop'], dtype=np.object_)
  1159. b = np.array(a, dtype=(np.str_, 8))
  1160. assert_equal(a, b)
  1161. c = np.array(a, dtype=(np.str_, 5))
  1162. assert_equal(c, np.array(['abcde', 'ijklm']))
  1163. d = np.array(a, dtype=(np.str_, 12))
  1164. assert_equal(a, d)
  1165. e = np.empty((2, ), dtype=(np.str_, 8))
  1166. e[:] = a[:]
  1167. assert_equal(a, e)
  1168. def test_unicode_to_string_cast(self):
  1169. # Ticket #1240.
  1170. a = np.array([[u'abc', u'\u03a3'],
  1171. [u'asdf', u'erw']],
  1172. dtype='U')
  1173. assert_raises(UnicodeEncodeError, np.array, a, 'S4')
  1174. def test_unicode_to_string_cast_error(self):
  1175. # gh-15790
  1176. a = np.array([u'\x80'] * 129, dtype='U3')
  1177. assert_raises(UnicodeEncodeError, np.array, a, 'S')
  1178. b = a.reshape(3, 43)[:-1, :-1]
  1179. assert_raises(UnicodeEncodeError, np.array, b, 'S')
  1180. def test_mixed_string_unicode_array_creation(self):
  1181. a = np.array(['1234', u'123'])
  1182. assert_(a.itemsize == 16)
  1183. a = np.array([u'123', '1234'])
  1184. assert_(a.itemsize == 16)
  1185. a = np.array(['1234', u'123', '12345'])
  1186. assert_(a.itemsize == 20)
  1187. a = np.array([u'123', '1234', u'12345'])
  1188. assert_(a.itemsize == 20)
  1189. a = np.array([u'123', '1234', u'1234'])
  1190. assert_(a.itemsize == 16)
  1191. def test_misaligned_objects_segfault(self):
  1192. # Ticket #1198 and #1267
  1193. a1 = np.zeros((10,), dtype='O,c')
  1194. a2 = np.array(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'], 'S10')
  1195. a1['f0'] = a2
  1196. repr(a1)
  1197. np.argmax(a1['f0'])
  1198. a1['f0'][1] = "FOO"
  1199. a1['f0'] = "FOO"
  1200. np.array(a1['f0'], dtype='S')
  1201. np.nonzero(a1['f0'])
  1202. a1.sort()
  1203. copy.deepcopy(a1)
  1204. def test_misaligned_scalars_segfault(self):
  1205. # Ticket #1267
  1206. s1 = np.array(('a', 'Foo'), dtype='c,O')
  1207. s2 = np.array(('b', 'Bar'), dtype='c,O')
  1208. s1['f1'] = s2['f1']
  1209. s1['f1'] = 'Baz'
  1210. def test_misaligned_dot_product_objects(self):
  1211. # Ticket #1267
  1212. # This didn't require a fix, but it's worth testing anyway, because
  1213. # it may fail if .dot stops enforcing the arrays to be BEHAVED
  1214. a = np.array([[(1, 'a'), (0, 'a')], [(0, 'a'), (1, 'a')]], dtype='O,c')
  1215. b = np.array([[(4, 'a'), (1, 'a')], [(2, 'a'), (2, 'a')]], dtype='O,c')
  1216. np.dot(a['f0'], b['f0'])
  1217. def test_byteswap_complex_scalar(self):
  1218. # Ticket #1259 and gh-441
  1219. for dtype in [np.dtype('<'+t) for t in np.typecodes['Complex']]:
  1220. z = np.array([2.2-1.1j], dtype)
  1221. x = z[0] # always native-endian
  1222. y = x.byteswap()
  1223. if x.dtype.byteorder == z.dtype.byteorder:
  1224. # little-endian machine
  1225. assert_equal(x, np.frombuffer(y.tobytes(), dtype=dtype.newbyteorder()))
  1226. else:
  1227. # big-endian machine
  1228. assert_equal(x, np.frombuffer(y.tobytes(), dtype=dtype))
  1229. # double check real and imaginary parts:
  1230. assert_equal(x.real, y.real.byteswap())
  1231. assert_equal(x.imag, y.imag.byteswap())
  1232. def test_structured_arrays_with_objects1(self):
  1233. # Ticket #1299
  1234. stra = 'aaaa'
  1235. strb = 'bbbb'
  1236. x = np.array([[(0, stra), (1, strb)]], 'i8,O')
  1237. x[x.nonzero()] = x.ravel()[:1]
  1238. assert_(x[0, 1] == x[0, 0])
  1239. @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
  1240. def test_structured_arrays_with_objects2(self):
  1241. # Ticket #1299 second test
  1242. stra = 'aaaa'
  1243. strb = 'bbbb'
  1244. numb = sys.getrefcount(strb)
  1245. numa = sys.getrefcount(stra)
  1246. x = np.array([[(0, stra), (1, strb)]], 'i8,O')
  1247. x[x.nonzero()] = x.ravel()[:1]
  1248. assert_(sys.getrefcount(strb) == numb)
  1249. assert_(sys.getrefcount(stra) == numa + 2)
  1250. def test_duplicate_title_and_name(self):
  1251. # Ticket #1254
  1252. dtspec = [(('a', 'a'), 'i'), ('b', 'i')]
  1253. assert_raises(ValueError, np.dtype, dtspec)
  1254. def test_signed_integer_division_overflow(self):
  1255. # Ticket #1317.
  1256. def test_type(t):
  1257. min = np.array([np.iinfo(t).min])
  1258. min //= -1
  1259. with np.errstate(divide="ignore"):
  1260. for t in (np.int8, np.int16, np.int32, np.int64, int):
  1261. test_type(t)
  1262. def test_buffer_hashlib(self):
  1263. from hashlib import sha256
  1264. x = np.array([1, 2, 3], dtype=np.dtype('<i4'))
  1265. assert_equal(sha256(x).hexdigest(), '4636993d3e1da4e9d6b8f87b79e8f7c6d018580d52661950eabc3845c5897a4d')
  1266. def test_0d_string_scalar(self):
  1267. # Bug #1436; the following should succeed
  1268. np.asarray('x', '>c')
  1269. def test_log1p_compiler_shenanigans(self):
  1270. # Check if log1p is behaving on 32 bit intel systems.
  1271. assert_(np.isfinite(np.log1p(np.exp2(-53))))
  1272. def test_fromiter_comparison(self):
  1273. a = np.fromiter(list(range(10)), dtype='b')
  1274. b = np.fromiter(list(range(10)), dtype='B')
  1275. assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
  1276. assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
  1277. def test_fromstring_crash(self):
  1278. # Ticket #1345: the following should not cause a crash
  1279. with assert_warns(DeprecationWarning):
  1280. np.fromstring(b'aa, aa, 1.0', sep=',')
  1281. def test_ticket_1539(self):
  1282. dtypes = [x for x in np.typeDict.values()
  1283. if (issubclass(x, np.number)
  1284. and not issubclass(x, np.timedelta64))]
  1285. a = np.array([], np.bool_) # not x[0] because it is unordered
  1286. failures = []
  1287. for x in dtypes:
  1288. b = a.astype(x)
  1289. for y in dtypes:
  1290. c = a.astype(y)
  1291. try:
  1292. np.dot(b, c)
  1293. except TypeError:
  1294. failures.append((x, y))
  1295. if failures:
  1296. raise AssertionError("Failures: %r" % failures)
  1297. def test_ticket_1538(self):
  1298. x = np.finfo(np.float32)
  1299. for name in 'eps epsneg max min resolution tiny'.split():
  1300. assert_equal(type(getattr(x, name)), np.float32,
  1301. err_msg=name)
  1302. def test_ticket_1434(self):
  1303. # Check that the out= argument in var and std has an effect
  1304. data = np.array(((1, 2, 3), (4, 5, 6), (7, 8, 9)))
  1305. out = np.zeros((3,))
  1306. ret = data.var(axis=1, out=out)
  1307. assert_(ret is out)
  1308. assert_array_equal(ret, data.var(axis=1))
  1309. ret = data.std(axis=1, out=out)
  1310. assert_(ret is out)
  1311. assert_array_equal(ret, data.std(axis=1))
  1312. def test_complex_nan_maximum(self):
  1313. cnan = complex(0, np.nan)
  1314. assert_equal(np.maximum(1, cnan), cnan)
  1315. def test_subclass_int_tuple_assignment(self):
  1316. # ticket #1563
  1317. class Subclass(np.ndarray):
  1318. def __new__(cls, i):
  1319. return np.ones((i,)).view(cls)
  1320. x = Subclass(5)
  1321. x[(0,)] = 2 # shouldn't raise an exception
  1322. assert_equal(x[0], 2)
  1323. def test_ufunc_no_unnecessary_views(self):
  1324. # ticket #1548
  1325. class Subclass(np.ndarray):
  1326. pass
  1327. x = np.array([1, 2, 3]).view(Subclass)
  1328. y = np.add(x, x, x)
  1329. assert_equal(id(x), id(y))
  1330. @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
  1331. def test_take_refcount(self):
  1332. # ticket #939
  1333. a = np.arange(16, dtype=float)
  1334. a.shape = (4, 4)
  1335. lut = np.ones((5 + 3, 4), float)
  1336. rgba = np.empty(shape=a.shape + (4,), dtype=lut.dtype)
  1337. c1 = sys.getrefcount(rgba)
  1338. try:
  1339. lut.take(a, axis=0, mode='clip', out=rgba)
  1340. except TypeError:
  1341. pass
  1342. c2 = sys.getrefcount(rgba)
  1343. assert_equal(c1, c2)
  1344. def test_fromfile_tofile_seeks(self):
  1345. # On Python 3, tofile/fromfile used to get (#1610) the Python
  1346. # file handle out of sync
  1347. f0 = tempfile.NamedTemporaryFile()
  1348. f = f0.file
  1349. f.write(np.arange(255, dtype='u1').tobytes())
  1350. f.seek(20)
  1351. ret = np.fromfile(f, count=4, dtype='u1')
  1352. assert_equal(ret, np.array([20, 21, 22, 23], dtype='u1'))
  1353. assert_equal(f.tell(), 24)
  1354. f.seek(40)
  1355. np.array([1, 2, 3], dtype='u1').tofile(f)
  1356. assert_equal(f.tell(), 43)
  1357. f.seek(40)
  1358. data = f.read(3)
  1359. assert_equal(data, b"\x01\x02\x03")
  1360. f.seek(80)
  1361. f.read(4)
  1362. data = np.fromfile(f, dtype='u1', count=4)
  1363. assert_equal(data, np.array([84, 85, 86, 87], dtype='u1'))
  1364. f.close()
  1365. def test_complex_scalar_warning(self):
  1366. for tp in [np.csingle, np.cdouble, np.clongdouble]:
  1367. x = tp(1+2j)
  1368. assert_warns(np.ComplexWarning, float, x)
  1369. with suppress_warnings() as sup:
  1370. sup.filter(np.ComplexWarning)
  1371. assert_equal(float(x), float(x.real))
  1372. def test_complex_scalar_complex_cast(self):
  1373. for tp in [np.csingle, np.cdouble, np.clongdouble]:
  1374. x = tp(1+2j)
  1375. assert_equal(complex(x), 1+2j)
  1376. def test_complex_boolean_cast(self):
  1377. # Ticket #2218
  1378. for tp in [np.csingle, np.cdouble, np.clongdouble]:
  1379. x = np.array([0, 0+0.5j, 0.5+0j], dtype=tp)
  1380. assert_equal(x.astype(bool), np.array([0, 1, 1], dtype=bool))
  1381. assert_(np.any(x))
  1382. assert_(np.all(x[1:]))
  1383. def test_uint_int_conversion(self):
  1384. x = 2**64 - 1
  1385. assert_equal(int(np.uint64(x)), x)
  1386. def test_duplicate_field_names_assign(self):
  1387. ra = np.fromiter(((i*3, i*2) for i in range(10)), dtype='i8,f8')
  1388. ra.dtype.names = ('f1', 'f2')
  1389. repr(ra) # should not cause a segmentation fault
  1390. assert_raises(ValueError, setattr, ra.dtype, 'names', ('f1', 'f1'))
  1391. def test_eq_string_and_object_array(self):
  1392. # From e-mail thread "__eq__ with str and object" (Keith Goodman)
  1393. a1 = np.array(['a', 'b'], dtype=object)
  1394. a2 = np.array(['a', 'c'])
  1395. assert_array_equal(a1 == a2, [True, False])
  1396. assert_array_equal(a2 == a1, [True, False])
  1397. def test_nonzero_byteswap(self):
  1398. a = np.array([0x80000000, 0x00000080, 0], dtype=np.uint32)
  1399. a.dtype = np.float32
  1400. assert_equal(a.nonzero()[0], [1])
  1401. a = a.byteswap().newbyteorder()
  1402. assert_equal(a.nonzero()[0], [1]) # [0] if nonzero() ignores swap
  1403. def test_find_common_type_boolean(self):
  1404. # Ticket #1695
  1405. assert_(np.find_common_type([], ['?', '?']) == '?')
  1406. def test_empty_mul(self):
  1407. a = np.array([1.])
  1408. a[1:1] *= 2
  1409. assert_equal(a, [1.])
  1410. def test_array_side_effect(self):
  1411. # The second use of itemsize was throwing an exception because in
  1412. # ctors.c, discover_itemsize was calling PyObject_Length without
  1413. # checking the return code. This failed to get the length of the
  1414. # number 2, and the exception hung around until something checked
  1415. # PyErr_Occurred() and returned an error.
  1416. assert_equal(np.dtype('S10').itemsize, 10)
  1417. np.array([['abc', 2], ['long ', '0123456789']], dtype=np.string_)
  1418. assert_equal(np.dtype('S10').itemsize, 10)
  1419. def test_any_float(self):
  1420. # all and any for floats
  1421. a = np.array([0.1, 0.9])
  1422. assert_(np.any(a))
  1423. assert_(np.all(a))
  1424. def test_large_float_sum(self):
  1425. a = np.arange(10000, dtype='f')
  1426. assert_equal(a.sum(dtype='d'), a.astype('d').sum())
  1427. def test_ufunc_casting_out(self):
  1428. a = np.array(1.0, dtype=np.float32)
  1429. b = np.array(1.0, dtype=np.float64)
  1430. c = np.array(1.0, dtype=np.float32)
  1431. np.add(a, b, out=c)
  1432. assert_equal(c, 2.0)
  1433. def test_array_scalar_contiguous(self):
  1434. # Array scalars are both C and Fortran contiguous
  1435. assert_(np.array(1.0).flags.c_contiguous)
  1436. assert_(np.array(1.0).flags.f_contiguous)
  1437. assert_(np.array(np.float32(1.0)).flags.c_contiguous)
  1438. assert_(np.array(np.float32(1.0)).flags.f_contiguous)
  1439. def test_squeeze_contiguous(self):
  1440. # Similar to GitHub issue #387
  1441. a = np.zeros((1, 2)).squeeze()
  1442. b = np.zeros((2, 2, 2), order='F')[:, :, ::2].squeeze()
  1443. assert_(a.flags.c_contiguous)
  1444. assert_(a.flags.f_contiguous)
  1445. assert_(b.flags.f_contiguous)
  1446. def test_squeeze_axis_handling(self):
  1447. # Issue #10779
  1448. # Ensure proper handling of objects
  1449. # that don't support axis specification
  1450. # when squeezing
  1451. class OldSqueeze(np.ndarray):
  1452. def __new__(cls,
  1453. input_array):
  1454. obj = np.asarray(input_array).view(cls)
  1455. return obj
  1456. # it is perfectly reasonable that prior
  1457. # to numpy version 1.7.0 a subclass of ndarray
  1458. # might have been created that did not expect
  1459. # squeeze to have an axis argument
  1460. # NOTE: this example is somewhat artificial;
  1461. # it is designed to simulate an old API
  1462. # expectation to guard against regression
  1463. def squeeze(self):
  1464. return super(OldSqueeze, self).squeeze()
  1465. oldsqueeze = OldSqueeze(np.array([[1],[2],[3]]))
  1466. # if no axis argument is specified the old API
  1467. # expectation should give the correct result
  1468. assert_equal(np.squeeze(oldsqueeze),
  1469. np.array([1,2,3]))
  1470. # likewise, axis=None should work perfectly well
  1471. # with the old API expectation
  1472. assert_equal(np.squeeze(oldsqueeze, axis=None),
  1473. np.array([1,2,3]))
  1474. # however, specification of any particular axis
  1475. # should raise a TypeError in the context of the
  1476. # old API specification, even when using a valid
  1477. # axis specification like 1 for this array
  1478. with assert_raises(TypeError):
  1479. # this would silently succeed for array
  1480. # subclasses / objects that did not support
  1481. # squeeze axis argument handling before fixing
  1482. # Issue #10779
  1483. np.squeeze(oldsqueeze, axis=1)
  1484. # check for the same behavior when using an invalid
  1485. # axis specification -- in this case axis=0 does not
  1486. # have size 1, but the priority should be to raise
  1487. # a TypeError for the axis argument and NOT a
  1488. # ValueError for squeezing a non-empty dimension
  1489. with assert_raises(TypeError):
  1490. np.squeeze(oldsqueeze, axis=0)
  1491. # the new API knows how to handle the axis
  1492. # argument and will return a ValueError if
  1493. # attempting to squeeze an axis that is not
  1494. # of length 1
  1495. with assert_raises(ValueError):
  1496. np.squeeze(np.array([[1],[2],[3]]), axis=0)
  1497. def test_reduce_contiguous(self):
  1498. # GitHub issue #387
  1499. a = np.add.reduce(np.zeros((2, 1, 2)), (0, 1))
  1500. b = np.add.reduce(np.zeros((2, 1, 2)), 1)
  1501. assert_(a.flags.c_contiguous)
  1502. assert_(a.flags.f_contiguous)
  1503. assert_(b.flags.c_contiguous)
  1504. def test_object_array_self_reference(self):
  1505. # Object arrays with references to themselves can cause problems
  1506. a = np.array(0, dtype=object)
  1507. a[()] = a
  1508. assert_raises(RecursionError, int, a)
  1509. assert_raises(RecursionError, float, a)
  1510. a[()] = None
  1511. def test_object_array_circular_reference(self):
  1512. # Test the same for a circular reference.
  1513. a = np.array(0, dtype=object)
  1514. b = np.array(0, dtype=object)
  1515. a[()] = b
  1516. b[()] = a
  1517. assert_raises(RecursionError, int, a)
  1518. # NumPy has no tp_traverse currently, so circular references
  1519. # cannot be detected. So resolve it:
  1520. a[()] = None
  1521. # This was causing a to become like the above
  1522. a = np.array(0, dtype=object)
  1523. a[...] += 1
  1524. assert_equal(a, 1)
  1525. def test_object_array_nested(self):
  1526. # but is fine with a reference to a different array
  1527. a = np.array(0, dtype=object)
  1528. b = np.array(0, dtype=object)
  1529. a[()] = b
  1530. assert_equal(int(a), int(0))
  1531. assert_equal(float(a), float(0))
  1532. def test_object_array_self_copy(self):
  1533. # An object array being copied into itself DECREF'ed before INCREF'ing
  1534. # causing segmentation faults (gh-3787)
  1535. a = np.array(object(), dtype=object)
  1536. np.copyto(a, a)
  1537. if HAS_REFCOUNT:
  1538. assert_(sys.getrefcount(a[()]) == 2)
  1539. a[()].__class__ # will segfault if object was deleted
  1540. def test_zerosize_accumulate(self):
  1541. "Ticket #1733"
  1542. x = np.array([[42, 0]], dtype=np.uint32)
  1543. assert_equal(np.add.accumulate(x[:-1, 0]), [])
  1544. def test_objectarray_setfield(self):
  1545. # Setfield should not overwrite Object fields with non-Object data
  1546. x = np.array([1, 2, 3], dtype=object)
  1547. assert_raises(TypeError, x.setfield, 4, np.int32, 0)
  1548. def test_setting_rank0_string(self):
  1549. "Ticket #1736"
  1550. s1 = b"hello1"
  1551. s2 = b"hello2"
  1552. a = np.zeros((), dtype="S10")
  1553. a[()] = s1
  1554. assert_equal(a, np.array(s1))
  1555. a[()] = np.array(s2)
  1556. assert_equal(a, np.array(s2))
  1557. a = np.zeros((), dtype='f4')
  1558. a[()] = 3
  1559. assert_equal(a, np.array(3))
  1560. a[()] = np.array(4)
  1561. assert_equal(a, np.array(4))
  1562. def test_string_astype(self):
  1563. "Ticket #1748"
  1564. s1 = b'black'
  1565. s2 = b'white'
  1566. s3 = b'other'
  1567. a = np.array([[s1], [s2], [s3]])
  1568. assert_equal(a.dtype, np.dtype('S5'))
  1569. b = a.astype(np.dtype('S0'))
  1570. assert_equal(b.dtype, np.dtype('S5'))
  1571. def test_ticket_1756(self):
  1572. # Ticket #1756
  1573. s = b'0123456789abcdef'
  1574. a = np.array([s]*5)
  1575. for i in range(1, 17):
  1576. a1 = np.array(a, "|S%d" % i)
  1577. a2 = np.array([s[:i]]*5)
  1578. assert_equal(a1, a2)
  1579. def test_fields_strides(self):
  1580. "gh-2355"
  1581. r = np.frombuffer(b'abcdefghijklmnop'*4*3, dtype='i4,(2,3)u2')
  1582. assert_equal(r[0:3:2]['f1'], r['f1'][0:3:2])
  1583. assert_equal(r[0:3:2]['f1'][0], r[0:3:2][0]['f1'])
  1584. assert_equal(r[0:3:2]['f1'][0][()], r[0:3:2][0]['f1'][()])
  1585. assert_equal(r[0:3:2]['f1'][0].strides, r[0:3:2][0]['f1'].strides)
  1586. def test_alignment_update(self):
  1587. # Check that alignment flag is updated on stride setting
  1588. a = np.arange(10)
  1589. assert_(a.flags.aligned)
  1590. a.strides = 3
  1591. assert_(not a.flags.aligned)
  1592. def test_ticket_1770(self):
  1593. "Should not segfault on python 3k"
  1594. import numpy as np
  1595. try:
  1596. a = np.zeros((1,), dtype=[('f1', 'f')])
  1597. a['f1'] = 1
  1598. a['f2'] = 1
  1599. except ValueError:
  1600. pass
  1601. except Exception:
  1602. raise AssertionError
  1603. def test_ticket_1608(self):
  1604. "x.flat shouldn't modify data"
  1605. x = np.array([[1, 2], [3, 4]]).T
  1606. np.array(x.flat)
  1607. assert_equal(x, [[1, 3], [2, 4]])
  1608. def test_pickle_string_overwrite(self):
  1609. import re
  1610. data = np.array([1], dtype='b')
  1611. blob = pickle.dumps(data, protocol=1)
  1612. data = pickle.loads(blob)
  1613. # Check that loads does not clobber interned strings
  1614. s = re.sub("a(.)", "\x01\\1", "a_")
  1615. assert_equal(s[0], "\x01")
  1616. data[0] = 0xbb
  1617. s = re.sub("a(.)", "\x01\\1", "a_")
  1618. assert_equal(s[0], "\x01")
  1619. def test_pickle_bytes_overwrite(self):
  1620. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  1621. data = np.array([1], dtype='b')
  1622. data = pickle.loads(pickle.dumps(data, protocol=proto))
  1623. data[0] = 0xdd
  1624. bytestring = "\x01 ".encode('ascii')
  1625. assert_equal(bytestring[0:1], '\x01'.encode('ascii'))
  1626. def test_pickle_py2_array_latin1_hack(self):
  1627. # Check that unpickling hacks in Py3 that support
  1628. # encoding='latin1' work correctly.
  1629. # Python2 output for pickle.dumps(numpy.array([129], dtype='b'))
  1630. data = (b"cnumpy.core.multiarray\n_reconstruct\np0\n(cnumpy\nndarray\np1\n(I0\n"
  1631. b"tp2\nS'b'\np3\ntp4\nRp5\n(I1\n(I1\ntp6\ncnumpy\ndtype\np7\n(S'i1'\np8\n"
  1632. b"I0\nI1\ntp9\nRp10\n(I3\nS'|'\np11\nNNNI-1\nI-1\nI0\ntp12\nbI00\nS'\\x81'\n"
  1633. b"p13\ntp14\nb.")
  1634. # This should work:
  1635. result = pickle.loads(data, encoding='latin1')
  1636. assert_array_equal(result, np.array([129], dtype='b'))
  1637. # Should not segfault:
  1638. assert_raises(Exception, pickle.loads, data, encoding='koi8-r')
  1639. def test_pickle_py2_scalar_latin1_hack(self):
  1640. # Check that scalar unpickling hack in Py3 that supports
  1641. # encoding='latin1' work correctly.
  1642. # Python2 output for pickle.dumps(...)
  1643. datas = [
  1644. # (original, python2_pickle, koi8r_validity)
  1645. (np.unicode_('\u6bd2'),
  1646. (b"cnumpy.core.multiarray\nscalar\np0\n(cnumpy\ndtype\np1\n"
  1647. b"(S'U1'\np2\nI0\nI1\ntp3\nRp4\n(I3\nS'<'\np5\nNNNI4\nI4\nI0\n"
  1648. b"tp6\nbS'\\xd2k\\x00\\x00'\np7\ntp8\nRp9\n."),
  1649. 'invalid'),
  1650. (np.float64(9e123),
  1651. (b"cnumpy.core.multiarray\nscalar\np0\n(cnumpy\ndtype\np1\n(S'f8'\n"
  1652. b"p2\nI0\nI1\ntp3\nRp4\n(I3\nS'<'\np5\nNNNI-1\nI-1\nI0\ntp6\n"
  1653. b"bS'O\\x81\\xb7Z\\xaa:\\xabY'\np7\ntp8\nRp9\n."),
  1654. 'invalid'),
  1655. (np.bytes_(b'\x9c'), # different 8-bit code point in KOI8-R vs latin1
  1656. (b"cnumpy.core.multiarray\nscalar\np0\n(cnumpy\ndtype\np1\n(S'S1'\np2\n"
  1657. b"I0\nI1\ntp3\nRp4\n(I3\nS'|'\np5\nNNNI1\nI1\nI0\ntp6\nbS'\\x9c'\np7\n"
  1658. b"tp8\nRp9\n."),
  1659. 'different'),
  1660. ]
  1661. for original, data, koi8r_validity in datas:
  1662. result = pickle.loads(data, encoding='latin1')
  1663. assert_equal(result, original)
  1664. # Decoding under non-latin1 encoding (e.g.) KOI8-R can
  1665. # produce bad results, but should not segfault.
  1666. if koi8r_validity == 'different':
  1667. # Unicode code points happen to lie within latin1,
  1668. # but are different in koi8-r, resulting to silent
  1669. # bogus results
  1670. result = pickle.loads(data, encoding='koi8-r')
  1671. assert_(result != original)
  1672. elif koi8r_validity == 'invalid':
  1673. # Unicode code points outside latin1, so results
  1674. # to an encoding exception
  1675. assert_raises(ValueError, pickle.loads, data, encoding='koi8-r')
  1676. else:
  1677. raise ValueError(koi8r_validity)
  1678. def test_structured_type_to_object(self):
  1679. a_rec = np.array([(0, 1), (3, 2)], dtype='i4,i8')
  1680. a_obj = np.empty((2,), dtype=object)
  1681. a_obj[0] = (0, 1)
  1682. a_obj[1] = (3, 2)
  1683. # astype records -> object
  1684. assert_equal(a_rec.astype(object), a_obj)
  1685. # '=' records -> object
  1686. b = np.empty_like(a_obj)
  1687. b[...] = a_rec
  1688. assert_equal(b, a_obj)
  1689. # '=' object -> records
  1690. b = np.empty_like(a_rec)
  1691. b[...] = a_obj
  1692. assert_equal(b, a_rec)
  1693. def test_assign_obj_listoflists(self):
  1694. # Ticket # 1870
  1695. # The inner list should get assigned to the object elements
  1696. a = np.zeros(4, dtype=object)
  1697. b = a.copy()
  1698. a[0] = [1]
  1699. a[1] = [2]
  1700. a[2] = [3]
  1701. a[3] = [4]
  1702. b[...] = [[1], [2], [3], [4]]
  1703. assert_equal(a, b)
  1704. # The first dimension should get broadcast
  1705. a = np.zeros((2, 2), dtype=object)
  1706. a[...] = [[1, 2]]
  1707. assert_equal(a, [[1, 2], [1, 2]])
  1708. @pytest.mark.slow_pypy
  1709. def test_memoryleak(self):
  1710. # Ticket #1917 - ensure that array data doesn't leak
  1711. for i in range(1000):
  1712. # 100MB times 1000 would give 100GB of memory usage if it leaks
  1713. a = np.empty((100000000,), dtype='i1')
  1714. del a
  1715. @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
  1716. def test_ufunc_reduce_memoryleak(self):
  1717. a = np.arange(6)
  1718. acnt = sys.getrefcount(a)
  1719. np.add.reduce(a)
  1720. assert_equal(sys.getrefcount(a), acnt)
  1721. def test_search_sorted_invalid_arguments(self):
  1722. # Ticket #2021, should not segfault.
  1723. x = np.arange(0, 4, dtype='datetime64[D]')
  1724. assert_raises(TypeError, x.searchsorted, 1)
  1725. def test_string_truncation(self):
  1726. # Ticket #1990 - Data can be truncated in creation of an array from a
  1727. # mixed sequence of numeric values and strings
  1728. for val in [True, 1234, 123.4, complex(1, 234)]:
  1729. for tostr in [asunicode, asbytes]:
  1730. b = np.array([val, tostr('xx')])
  1731. assert_equal(tostr(b[0]), tostr(val))
  1732. b = np.array([tostr('xx'), val])
  1733. assert_equal(tostr(b[1]), tostr(val))
  1734. # test also with longer strings
  1735. b = np.array([val, tostr('xxxxxxxxxx')])
  1736. assert_equal(tostr(b[0]), tostr(val))
  1737. b = np.array([tostr('xxxxxxxxxx'), val])
  1738. assert_equal(tostr(b[1]), tostr(val))
  1739. def test_string_truncation_ucs2(self):
  1740. # Ticket #2081. Python compiled with two byte unicode
  1741. # can lead to truncation if itemsize is not properly
  1742. # adjusted for NumPy's four byte unicode.
  1743. a = np.array(['abcd'])
  1744. assert_equal(a.dtype.itemsize, 16)
  1745. def test_unique_stable(self):
  1746. # Ticket #2063 must always choose stable sort for argsort to
  1747. # get consistent results
  1748. v = np.array(([0]*5 + [1]*6 + [2]*6)*4)
  1749. res = np.unique(v, return_index=True)
  1750. tgt = (np.array([0, 1, 2]), np.array([ 0, 5, 11]))
  1751. assert_equal(res, tgt)
  1752. def test_unicode_alloc_dealloc_match(self):
  1753. # Ticket #1578, the mismatch only showed up when running
  1754. # python-debug for python versions >= 2.7, and then as
  1755. # a core dump and error message.
  1756. a = np.array(['abc'], dtype=np.unicode_)[0]
  1757. del a
  1758. def test_refcount_error_in_clip(self):
  1759. # Ticket #1588
  1760. a = np.zeros((2,), dtype='>i2').clip(min=0)
  1761. x = a + a
  1762. # This used to segfault:
  1763. y = str(x)
  1764. # Check the final string:
  1765. assert_(y == "[0 0]")
  1766. def test_searchsorted_wrong_dtype(self):
  1767. # Ticket #2189, it used to segfault, so we check that it raises the
  1768. # proper exception.
  1769. a = np.array([('a', 1)], dtype='S1, int')
  1770. assert_raises(TypeError, np.searchsorted, a, 1.2)
  1771. # Ticket #2066, similar problem:
  1772. dtype = np.format_parser(['i4', 'i4'], [], [])
  1773. a = np.recarray((2, ), dtype)
  1774. assert_raises(TypeError, np.searchsorted, a, 1)
  1775. def test_complex64_alignment(self):
  1776. # Issue gh-2668 (trac 2076), segfault on sparc due to misalignment
  1777. dtt = np.complex64
  1778. arr = np.arange(10, dtype=dtt)
  1779. # 2D array
  1780. arr2 = np.reshape(arr, (2, 5))
  1781. # Fortran write followed by (C or F) read caused bus error
  1782. data_str = arr2.tobytes('F')
  1783. data_back = np.ndarray(arr2.shape,
  1784. arr2.dtype,
  1785. buffer=data_str,
  1786. order='F')
  1787. assert_array_equal(arr2, data_back)
  1788. def test_structured_count_nonzero(self):
  1789. arr = np.array([0, 1]).astype('i4, (2)i4')[:1]
  1790. count = np.count_nonzero(arr)
  1791. assert_equal(count, 0)
  1792. def test_copymodule_preserves_f_contiguity(self):
  1793. a = np.empty((2, 2), order='F')
  1794. b = copy.copy(a)
  1795. c = copy.deepcopy(a)
  1796. assert_(b.flags.fortran)
  1797. assert_(b.flags.f_contiguous)
  1798. assert_(c.flags.fortran)
  1799. assert_(c.flags.f_contiguous)
  1800. def test_fortran_order_buffer(self):
  1801. import numpy as np
  1802. a = np.array([['Hello', 'Foob']], dtype='U5', order='F')
  1803. arr = np.ndarray(shape=[1, 2, 5], dtype='U1', buffer=a)
  1804. arr2 = np.array([[[u'H', u'e', u'l', u'l', u'o'],
  1805. [u'F', u'o', u'o', u'b', u'']]])
  1806. assert_array_equal(arr, arr2)
  1807. def test_assign_from_sequence_error(self):
  1808. # Ticket #4024.
  1809. arr = np.array([1, 2, 3])
  1810. assert_raises(ValueError, arr.__setitem__, slice(None), [9, 9])
  1811. arr.__setitem__(slice(None), [9])
  1812. assert_equal(arr, [9, 9, 9])
  1813. def test_format_on_flex_array_element(self):
  1814. # Ticket #4369.
  1815. dt = np.dtype([('date', '<M8[D]'), ('val', '<f8')])
  1816. arr = np.array([('2000-01-01', 1)], dt)
  1817. formatted = '{0}'.format(arr[0])
  1818. assert_equal(formatted, str(arr[0]))
  1819. def test_deepcopy_on_0d_array(self):
  1820. # Ticket #3311.
  1821. arr = np.array(3)
  1822. arr_cp = copy.deepcopy(arr)
  1823. assert_equal(arr, arr_cp)
  1824. assert_equal(arr.shape, arr_cp.shape)
  1825. assert_equal(int(arr), int(arr_cp))
  1826. assert_(arr is not arr_cp)
  1827. assert_(isinstance(arr_cp, type(arr)))
  1828. def test_deepcopy_F_order_object_array(self):
  1829. # Ticket #6456.
  1830. a = {'a': 1}
  1831. b = {'b': 2}
  1832. arr = np.array([[a, b], [a, b]], order='F')
  1833. arr_cp = copy.deepcopy(arr)
  1834. assert_equal(arr, arr_cp)
  1835. assert_(arr is not arr_cp)
  1836. # Ensure that we have actually copied the item.
  1837. assert_(arr[0, 1] is not arr_cp[1, 1])
  1838. # Ensure we are allowed to have references to the same object.
  1839. assert_(arr[0, 1] is arr[1, 1])
  1840. # Check the references hold for the copied objects.
  1841. assert_(arr_cp[0, 1] is arr_cp[1, 1])
  1842. def test_deepcopy_empty_object_array(self):
  1843. # Ticket #8536.
  1844. # Deepcopy should succeed
  1845. a = np.array([], dtype=object)
  1846. b = copy.deepcopy(a)
  1847. assert_(a.shape == b.shape)
  1848. def test_bool_subscript_crash(self):
  1849. # gh-4494
  1850. c = np.rec.array([(1, 2, 3), (4, 5, 6)])
  1851. masked = c[np.array([True, False])]
  1852. base = masked.base
  1853. del masked, c
  1854. base.dtype
  1855. def test_richcompare_crash(self):
  1856. # gh-4613
  1857. import operator as op
  1858. # dummy class where __array__ throws exception
  1859. class Foo:
  1860. __array_priority__ = 1002
  1861. def __array__(self, *args, **kwargs):
  1862. raise Exception()
  1863. rhs = Foo()
  1864. lhs = np.array(1)
  1865. for f in [op.lt, op.le, op.gt, op.ge]:
  1866. assert_raises(TypeError, f, lhs, rhs)
  1867. assert_(not op.eq(lhs, rhs))
  1868. assert_(op.ne(lhs, rhs))
  1869. def test_richcompare_scalar_and_subclass(self):
  1870. # gh-4709
  1871. class Foo(np.ndarray):
  1872. def __eq__(self, other):
  1873. return "OK"
  1874. x = np.array([1, 2, 3]).view(Foo)
  1875. assert_equal(10 == x, "OK")
  1876. assert_equal(np.int32(10) == x, "OK")
  1877. assert_equal(np.array([10]) == x, "OK")
  1878. def test_pickle_empty_string(self):
  1879. # gh-3926
  1880. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  1881. test_string = np.string_('')
  1882. assert_equal(pickle.loads(
  1883. pickle.dumps(test_string, protocol=proto)), test_string)
  1884. def test_frompyfunc_many_args(self):
  1885. # gh-5672
  1886. def passer(*args):
  1887. pass
  1888. assert_raises(ValueError, np.frompyfunc, passer, 32, 1)
  1889. def test_repeat_broadcasting(self):
  1890. # gh-5743
  1891. a = np.arange(60).reshape(3, 4, 5)
  1892. for axis in chain(range(-a.ndim, a.ndim), [None]):
  1893. assert_equal(a.repeat(2, axis=axis), a.repeat([2], axis=axis))
  1894. def test_frompyfunc_nout_0(self):
  1895. # gh-2014
  1896. def f(x):
  1897. x[0], x[-1] = x[-1], x[0]
  1898. uf = np.frompyfunc(f, 1, 0)
  1899. a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]], dtype=object)
  1900. assert_equal(uf(a), ())
  1901. expected = np.array([[3, 2, 1], [5, 4], [9, 7, 8, 6]], dtype=object)
  1902. assert_array_equal(a, expected)
  1903. @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
  1904. def test_leak_in_structured_dtype_comparison(self):
  1905. # gh-6250
  1906. recordtype = np.dtype([('a', np.float64),
  1907. ('b', np.int32),
  1908. ('d', (str, 5))])
  1909. # Simple case
  1910. a = np.zeros(2, dtype=recordtype)
  1911. for i in range(100):
  1912. a == a
  1913. assert_(sys.getrefcount(a) < 10)
  1914. # The case in the bug report.
  1915. before = sys.getrefcount(a)
  1916. u, v = a[0], a[1]
  1917. u == v
  1918. del u, v
  1919. gc.collect()
  1920. after = sys.getrefcount(a)
  1921. assert_equal(before, after)
  1922. def test_empty_percentile(self):
  1923. # gh-6530 / gh-6553
  1924. assert_array_equal(np.percentile(np.arange(10), []), np.array([]))
  1925. def test_void_compare_segfault(self):
  1926. # gh-6922. The following should not segfault
  1927. a = np.ones(3, dtype=[('object', 'O'), ('int', '<i2')])
  1928. a.sort()
  1929. def test_reshape_size_overflow(self):
  1930. # gh-7455
  1931. a = np.ones(20)[::2]
  1932. if np.dtype(np.intp).itemsize == 8:
  1933. # 64 bit. The following are the prime factors of 2**63 + 5,
  1934. # plus a leading 2, so when multiplied together as int64,
  1935. # the result overflows to a total size of 10.
  1936. new_shape = (2, 13, 419, 691, 823, 2977518503)
  1937. else:
  1938. # 32 bit. The following are the prime factors of 2**31 + 5,
  1939. # plus a leading 2, so when multiplied together as int32,
  1940. # the result overflows to a total size of 10.
  1941. new_shape = (2, 7, 7, 43826197)
  1942. assert_raises(ValueError, a.reshape, new_shape)
  1943. def test_invalid_structured_dtypes(self):
  1944. # gh-2865
  1945. # mapping python objects to other dtypes
  1946. assert_raises(ValueError, np.dtype, ('O', [('name', 'i8')]))
  1947. assert_raises(ValueError, np.dtype, ('i8', [('name', 'O')]))
  1948. assert_raises(ValueError, np.dtype,
  1949. ('i8', [('name', [('name', 'O')])]))
  1950. assert_raises(ValueError, np.dtype, ([('a', 'i4'), ('b', 'i4')], 'O'))
  1951. assert_raises(ValueError, np.dtype, ('i8', 'O'))
  1952. # wrong number/type of tuple elements in dict
  1953. assert_raises(ValueError, np.dtype,
  1954. ('i', {'name': ('i', 0, 'title', 'oops')}))
  1955. assert_raises(ValueError, np.dtype,
  1956. ('i', {'name': ('i', 'wrongtype', 'title')}))
  1957. # disallowed as of 1.13
  1958. assert_raises(ValueError, np.dtype,
  1959. ([('a', 'O'), ('b', 'O')], [('c', 'O'), ('d', 'O')]))
  1960. # allowed as a special case due to existing use, see gh-2798
  1961. a = np.ones(1, dtype=('O', [('name', 'O')]))
  1962. assert_equal(a[0], 1)
  1963. # In particular, the above union dtype (and union dtypes in general)
  1964. # should mainly behave like the main (object) dtype:
  1965. assert a[0] is a.item()
  1966. assert type(a[0]) is int
  1967. def test_correct_hash_dict(self):
  1968. # gh-8887 - __hash__ would be None despite tp_hash being set
  1969. all_types = set(np.typeDict.values()) - {np.void}
  1970. for t in all_types:
  1971. val = t()
  1972. try:
  1973. hash(val)
  1974. except TypeError as e:
  1975. assert_equal(t.__hash__, None)
  1976. else:
  1977. assert_(t.__hash__ != None)
  1978. def test_scalar_copy(self):
  1979. scalar_types = set(np.sctypeDict.values())
  1980. values = {
  1981. np.void: b"a",
  1982. np.bytes_: b"a",
  1983. np.unicode_: "a",
  1984. np.datetime64: "2017-08-25",
  1985. }
  1986. for sctype in scalar_types:
  1987. item = sctype(values.get(sctype, 1))
  1988. item2 = copy.copy(item)
  1989. assert_equal(item, item2)
  1990. def test_void_item_memview(self):
  1991. va = np.zeros(10, 'V4')
  1992. x = va[:1].item()
  1993. va[0] = b'\xff\xff\xff\xff'
  1994. del va
  1995. assert_equal(x, b'\x00\x00\x00\x00')
  1996. def test_void_getitem(self):
  1997. # Test fix for gh-11668.
  1998. assert_(np.array([b'a'], 'V1').astype('O') == b'a')
  1999. assert_(np.array([b'ab'], 'V2').astype('O') == b'ab')
  2000. assert_(np.array([b'abc'], 'V3').astype('O') == b'abc')
  2001. assert_(np.array([b'abcd'], 'V4').astype('O') == b'abcd')
  2002. def test_structarray_title(self):
  2003. # The following used to segfault on pypy, due to NPY_TITLE_KEY
  2004. # not working properly and resulting to double-decref of the
  2005. # structured array field items:
  2006. # See: https://bitbucket.org/pypy/pypy/issues/2789
  2007. for j in range(5):
  2008. structure = np.array([1], dtype=[(('x', 'X'), np.object_)])
  2009. structure[0]['x'] = np.array([2])
  2010. gc.collect()
  2011. def test_dtype_scalar_squeeze(self):
  2012. # gh-11384
  2013. values = {
  2014. 'S': b"a",
  2015. 'M': "2018-06-20",
  2016. }
  2017. for ch in np.typecodes['All']:
  2018. if ch in 'O':
  2019. continue
  2020. sctype = np.dtype(ch).type
  2021. scvalue = sctype(values.get(ch, 3))
  2022. for axis in [None, ()]:
  2023. squeezed = scvalue.squeeze(axis=axis)
  2024. assert_equal(squeezed, scvalue)
  2025. assert_equal(type(squeezed), type(scvalue))
  2026. def test_field_access_by_title(self):
  2027. # gh-11507
  2028. s = 'Some long field name'
  2029. if HAS_REFCOUNT:
  2030. base = sys.getrefcount(s)
  2031. t = np.dtype([((s, 'f1'), np.float64)])
  2032. data = np.zeros(10, t)
  2033. for i in range(10):
  2034. str(data[['f1']])
  2035. if HAS_REFCOUNT:
  2036. assert_(base <= sys.getrefcount(s))
  2037. @pytest.mark.parametrize('val', [
  2038. # arrays and scalars
  2039. np.ones((10, 10), dtype='int32'),
  2040. np.uint64(10),
  2041. ])
  2042. @pytest.mark.parametrize('protocol',
  2043. range(2, pickle.HIGHEST_PROTOCOL + 1)
  2044. )
  2045. def test_pickle_module(self, protocol, val):
  2046. # gh-12837
  2047. s = pickle.dumps(val, protocol)
  2048. assert b'_multiarray_umath' not in s
  2049. if protocol == 5 and len(val.shape) > 0:
  2050. # unpickling ndarray goes through _frombuffer for protocol 5
  2051. assert b'numpy.core.numeric' in s
  2052. else:
  2053. assert b'numpy.core.multiarray' in s
  2054. def test_object_casting_errors(self):
  2055. # gh-11993 update to ValueError (see gh-16909), since strings can in
  2056. # principle be converted to complex, but this string cannot.
  2057. arr = np.array(['AAAAA', 18465886.0, 18465886.0], dtype=object)
  2058. assert_raises(ValueError, arr.astype, 'c8')
  2059. def test_eff1d_casting(self):
  2060. # gh-12711
  2061. x = np.array([1, 2, 4, 7, 0], dtype=np.int16)
  2062. res = np.ediff1d(x, to_begin=-99, to_end=np.array([88, 99]))
  2063. assert_equal(res, [-99, 1, 2, 3, -7, 88, 99])
  2064. # The use of safe casting means, that 1<<20 is cast unsafely, an
  2065. # error may be better, but currently there is no mechanism for it.
  2066. res = np.ediff1d(x, to_begin=(1<<20), to_end=(1<<20))
  2067. assert_equal(res, [0, 1, 2, 3, -7, 0])
  2068. def test_pickle_datetime64_array(self):
  2069. # gh-12745 (would fail with pickle5 installed)
  2070. d = np.datetime64('2015-07-04 12:59:59.50', 'ns')
  2071. arr = np.array([d])
  2072. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  2073. dumped = pickle.dumps(arr, protocol=proto)
  2074. assert_equal(pickle.loads(dumped), arr)
  2075. def test_bad_array_interface(self):
  2076. class T:
  2077. __array_interface__ = {}
  2078. with assert_raises(ValueError):
  2079. np.array([T()])
  2080. def test_2d__array__shape(self):
  2081. class T(object):
  2082. def __array__(self):
  2083. return np.ndarray(shape=(0,0))
  2084. # Make sure __array__ is used instead of Sequence methods.
  2085. def __iter__(self):
  2086. return iter([])
  2087. def __getitem__(self, idx):
  2088. raise AssertionError("__getitem__ was called")
  2089. def __len__(self):
  2090. return 0
  2091. t = T()
  2092. # gh-13659, would raise in broadcasting [x=t for x in result]
  2093. arr = np.array([t])
  2094. assert arr.shape == (1, 0, 0)
  2095. @pytest.mark.skipif(sys.maxsize < 2 ** 31 + 1, reason='overflows 32-bit python')
  2096. @pytest.mark.skipif(sys.platform == 'win32' and sys.version_info[:2] < (3, 8),
  2097. reason='overflows on windows, fixed in bpo-16865')
  2098. def test_to_ctypes(self):
  2099. #gh-14214
  2100. arr = np.zeros((2 ** 31 + 1,), 'b')
  2101. assert arr.size * arr.itemsize > 2 ** 31
  2102. c_arr = np.ctypeslib.as_ctypes(arr)
  2103. assert_equal(c_arr._length_, arr.size)
  2104. def test_complex_conversion_error(self):
  2105. # gh-17068
  2106. with pytest.raises(TypeError, match=r"Unable to convert dtype.*"):
  2107. complex(np.array("now", np.datetime64))
  2108. def test__array_interface__descr(self):
  2109. # gh-17068
  2110. dt = np.dtype(dict(names=['a', 'b'],
  2111. offsets=[0, 0],
  2112. formats=[np.int64, np.int64]))
  2113. descr = np.array((1, 1), dtype=dt).__array_interface__['descr']
  2114. assert descr == [('', '|V8')] # instead of [(b'', '|V8')]
  2115. @pytest.mark.skipif(sys.maxsize < 2 ** 31 + 1, reason='overflows 32-bit python')
  2116. @requires_memory(free_bytes=9e9)
  2117. def test_dot_big_stride(self):
  2118. # gh-17111
  2119. # blas stride = stride//itemsize > int32 max
  2120. int32_max = np.iinfo(np.int32).max
  2121. n = int32_max + 3
  2122. a = np.empty([n], dtype=np.float32)
  2123. b = a[::n-1]
  2124. b[...] = 1
  2125. assert b.strides[0] > int32_max * b.dtype.itemsize
  2126. assert np.dot(b, b) == 2.0
  2127. def test_frompyfunc_name(self):
  2128. # name conversion was failing for python 3 strings
  2129. # resulting in the default '?' name. Also test utf-8
  2130. # encoding using non-ascii name.
  2131. def cassé(x):
  2132. return x
  2133. f = np.frompyfunc(cassé, 1, 1)
  2134. assert str(f) == "<ufunc 'cassé (vectorized)'>"