Source

pypy / pypy / module / micronumpy / interp_dtype.py

Full commit
Maciej Fijalkows… b834132 
Alex Gaynor 26af0e3 
Philip Jenvey 681043e 
Maciej Fijalkows… c483988 
Alex Gaynor 9e74aaa 

Maciej Fijalkows… 2de8853 
Alexander Hesse f86b274 


mattip 418caa1 
Alex Gaynor 2322508 
Brian Kearns 0032c18 





Alex Gaynor 20c9eeb 
Justin Peel 779c6bc 
Alex Gaynor 2322508 
Alex Gaynor f77427e 

mattip 11f0a83 
Maciej Fijalkows… a3f0a90 
Maciej Fijalkows… 18ad78a 

mattip 6171c09 

Alex Gaynor fc7d508 
Maciej Fijalkows… 44b208d 
Maciej Fijalkows… c483988 
Maciej Fijalkows… 44b208d 



Maciej Fijalkows… 2de8853 











mattip fd71a98 
Maciej Fijalkows… 2de8853 

Alex Gaynor 26af0e3 
Romain Guilleber… 217977b 
Alex Gaynor e5ef850 
Maciej Fijalkows… e7b2477 
Brian Kearns 0032c18 
Romain Guilleber… 217977b 
Alex Gaynor f9f4bed 


Alex Gaynor 4f8dd56 

Alex Gaynor 9e74aaa 
Alex Gaynor 4f8dd56 
Alex Gaynor 923df65 
Brian Kearns 0032c18 
Maciej Fijalkows… a3f0a90 

Maciej Fijalkows… e7b2477 
Romain Guilleber… 217977b 

mattip ca3ad0a 



Alex Gaynor f9f4bed 




Jasper Schulz 7623c78 



mattip 92ac356 

Alex Gaynor f9f4bed 
Maciej Fijalkows… bd3a6f1 
Alex Gaynor f9f4bed 
Maciej Fijalkows… bd3a6f1 
mattip e08dfdf 

Alex Gaynor f9f4bed 
Maciej Fijalkows… bd3a6f1 
Maciej Fijalkows… da66ea0 
Maciej Fijalkows… bec8944 
Maciej Fijalkows… 802976e 

Alex Gaynor f9f4bed 
Alex Gaynor b8595c5 
Maciej Fijalkows… de511ed 
Alex Gaynor 2322508 
mattip 46c9ad0 




Alex Gaynor 4f8dd56 
mattip 46c9ad0 
Alex Gaynor 4f8dd56 
Alex Gaynor 2322508 
mattip 46c9ad0 
Alex Gaynor 2322508 
Alex Gaynor b8595c5 

Alex Gaynor 2322508 
Maciej Fijalkows… e7b2477 




Amaury Forgeot d… 96ef4e2 














Maciej Fijalkows… 273d1c9 


Andrews Medina ec63aaa 


mattip ca3ad0a 


Romain Guilleber… 217977b 


Alex Gaynor 2efe81d 
Romain Guilleber… 217977b 

Alex Gaynor 2efe81d 
Maciej Fijalkows… b4faea8 
Alex Gaynor 0e63f32 
Brian Kearns 05a9b19 




Maciej Fijalkows… b4faea8 






Maciej Fijalkows… a3f0a90 








Romain Guilleber… ef6ba39 

Romain Guilleber… 44b9969 
Romain Guilleber… ef6ba39 
Romain Guilleber… 8b80747 
Romain Guilleber… bf43d52 















Romain Guilleber… ef6ba39 
Maciej Fijalkows… a3f0a90 




Romain Guilleber… ef6ba39 













Maciej Fijalkows… a3f0a90 
Maciej Fijalkows… e16a35f 
Maciej Fijalkows… a3f0a90 






Maciej Fijalkows… ad840d9 
Maciej Fijalkows… 6a5df76 

Maciej Fijalkows… ad840d9 
Maciej Fijalkows… 040a65b 


mattip 4ff04cf 
Brian Kearns 0032c18 
mattip 4ff04cf 
mattip 76bdd20 


Maciej Fijalkows… ad840d9 


Maciej Fijalkows… 729aca0 


mattip 30146f8 


mattip ae156bb 


mattip 45b875b 
mattip ae156bb 
mattip 45b875b 
Maciej Fijalkows… 3c80e27 




Maciej Fijalkows… de511ed 


Romain Guilleber… 3a23062 










Brian Kearns 09b3651 
Romain Guilleber… 3a23062 



Romain Guilleber… 44b9969 


Romain Guilleber… 3a23062 
Brian Kearns 09b3651 
Romain Guilleber… 3a23062 

Brian Kearns 09b3651 
Romain Guilleber… 3a23062 








Romain Guilleber… ef6ba39 



Romain Guilleber… 44b9969 
Romain Guilleber… ef6ba39 
Romain Guilleber… bf43d52 
Romain Guilleber… ef6ba39 
Romain Guilleber… c4923f8 
Romain Guilleber… bf43d52 
Romain Guilleber… 44b9969 
Romain Guilleber… ef6ba39 
Maciej Fijalkows… 273d1c9 

Maciej Fijalkows… a3f0a90 
Maciej Fijalkows… a3d4b51 
Maciej Fijalkows… a3f0a90 

Maciej Fijalkows… 273d1c9 
Romain Guilleber… 217977b 






Carl Friedrich B… 0dbb3ef 
Romain Guilleber… 217977b 
Maciej Fijalkows… a3f0a90 


Maciej Fijalkows… 090669b 
Maciej Fijalkows… a3f0a90 

Romain Guilleber… 217977b 
Maciej Fijalkows… a3f0a90 
Maciej Fijalkows… bd3a6f1 
Maciej Fijalkows… a3f0a90 


Maciej Fijalkows… 273d1c9 
Maciej Fijalkows… 0901f6d 
Maciej Fijalkows… 6ee326b 

Maciej Fijalkows… 0901f6d 
Maciej Fijalkows… 6ee326b 

Romain Guilleber… 3a23062 
Maciej Fijalkows… 18ad78a 
Romain Guilleber… 217977b 
Romain Guilleber… ef6ba39 
Maciej Fijalkows… 273d1c9 

Romain Guilleber… 217977b 













Maciej Fijalkows… c483988 
Maciej Fijalkows… 273d1c9 




Maciej Fijalkows… 18ad78a 

Maciej Fijalkows… 273d1c9 



mattip 2184458 
Maciej Fijalkows… 18ad78a 
mattip 2184458 

Maciej Fijalkows… 273d1c9 

Romain Guilleber… 217977b 

Maciej Fijalkows… 18ad78a 

mattip fa8c91a 




Philip Jenvey 681043e 

Maciej Fijalkows… 273d1c9 
Alex Gaynor 2322508 
Alex Gaynor adbd652 
Maciej Fijalkows… 273d1c9 
Alex Gaynor 2322508 
Alex Gaynor 4f8dd56 
Alex Gaynor 2322508 
Maciej Fijalkows… b4faea8 

Maciej Fijalkows… a3f0a90 
Alex Gaynor 2322508 
Romain Guilleber… 3a23062 
Romain Guilleber… ef6ba39 
Romain Guilleber… 3a23062 
Alex Gaynor 2322508 

Maciej Fijalkows… a3f0a90 
Alex Gaynor 9e74aaa 
Maciej Fijalkows… e7b2477 
Amaury Forgeot d… 96ef4e2 
Alex Gaynor b8595c5 
Maciej Fijalkows… 273d1c9 
Andrews Medina ec63aaa 
Alex Gaynor 2efe81d 
Maciej Fijalkows… ecfe72e 
Maciej Fijalkows… a3f0a90 

Romain Guilleber… 217977b 
mattip ca3ad0a 
Alex Gaynor 69827a9 
Justin Peel 4218de3 
Alex Gaynor f9f4bed 
Brian Kearns 0032c18 











Brian Kearns 1b0b29a 



Brian Kearns 0032c18 


















Maciej Fijalkows… b834132 
Maciej Fijalkows… 36e4ff5 
Brian Kearns e1560a7 
Maciej Fijalkows… 36e4ff5 
Brian Kearns e1560a7 
Maciej Fijalkows… 36e4ff5 

Brian Kearns e1560a7 

Maciej Fijalkows… 36e4ff5 



Brian Kearns e1560a7 
Maciej Fijalkows… 36e4ff5 
Brian Kearns e1560a7 
Maciej Fijalkows… 36e4ff5 

Brian Kearns e1560a7 

Maciej Fijalkows… 36e4ff5 



Alex Gaynor f9f4bed 
Alex Gaynor e54c4fc 
Alex Gaynor f9f4bed 



Alex Gaynor 4f8dd56 

Alex Gaynor 923df65 
Alex Gaynor 4f8dd56 
Alex Gaynor f9f4bed 




Alex Gaynor 4f8dd56 

Alex Gaynor 923df65 
Alex Gaynor f9f4bed 




Alex Gaynor 4f8dd56 

Alex Gaynor 923df65 
Alex Gaynor f9f4bed 




Alex Gaynor 4f8dd56 

Alex Gaynor 923df65 
Alex Gaynor f9f4bed 




Alex Gaynor 4f8dd56 

Alex Gaynor 923df65 
Alex Gaynor f9f4bed 




Alex Gaynor 4f8dd56 

Alex Gaynor 923df65 
Alex Gaynor 9e74aaa 
Alex Gaynor f9f4bed 



Alex Gaynor 4f8dd56 

Alex Gaynor 923df65 
Alex Gaynor f9f4bed 

Alex Gaynor ef2232d 
Alex Gaynor f9f4bed 

Brian Kearns 82f81f0 
Alex Gaynor 4f8dd56 
Alex Gaynor 923df65 
mattip fa8c91a 



Maciej Fijalkows… 63fa48a 
Alex Gaynor f9f4bed 

Alex Gaynor ef2232d 
Alex Gaynor f9f4bed 

Brian Kearns 82f81f0 
Alex Gaynor 4f8dd56 
Alex Gaynor 923df65 
mattip fa8c91a 

Brian Kearns 7d06b6f 
Alex Gaynor f9f4bed 




Alex Gaynor 4f8dd56 

Alex Gaynor 923df65 
Maciej Fijalkows… 80e8108 
Alex Gaynor f9f4bed 




Alex Gaynor 4f8dd56 

Alex Gaynor 923df65 
Alex Gaynor f9f4bed 




Alex Gaynor 4f8dd56 

Alex Gaynor 923df65 
Alex Gaynor f9f4bed 




Alex Gaynor 603ee5d 
Alex Gaynor 4f8dd56 
Alex Gaynor 9e74aaa 
Brian Kearns c6a40e5 
mattip fa8c91a 

Romain Guilleber… c0ad61f 
Alex Gaynor f9f4bed 
Brian Kearns 82f81f0 



Brian Kearns 394a633 
Brian Kearns 82f81f0 



Brian Kearns 0032c18 
mattip b53ceee 





mattip b36dcab 
mattip b53ceee 
Brian Kearns 0032c18 
mattip b53ceee 







mattip b36dcab 
mattip b53ceee 
Brian Kearns 0032c18 
Brian Kearns 82f81f0 


Brian Kearns 394a633 
Brian Kearns 82f81f0 




Maciej Fijalkows… 18ad78a 
mattip ae156bb 
Maciej Fijalkows… 18ad78a 




Romain Guilleber… 36f863c 
Romain Guilleber… c0ad61f 
Maciej Fijalkows… 18ad78a 

Amaury Forgeot d… 96ef4e2 
Maciej Fijalkows… 18ad78a 















mattip fa8c91a 

Maciej Fijalkows… 18ad78a 
mattip dcccbf6 

mattip 6f2eb14 
mattip dcccbf6 




mattip 6171c09 



Brian Kearns 4846add 
mattip 6171c09 

Brian Kearns 4846add 
mattip 6171c09 


Brian Kearns 4846add 
mattip 6171c09 

Brian Kearns 4846add 
mattip 6171c09 



Brian Kearns 4846add 
mattip 6171c09 



Brian Kearns 513c099 
mattip 6171c09 

Brian Kearns 4846add 
mattip 6171c09 



Brian Kearns 513c099 
Brian Kearns 82f81f0 



Alex Gaynor f9f4bed 
Brian Kearns 513c099 




Brian Kearns 82f81f0 

Brian Kearns 513c099 

Alex Gaynor f9f4bed 
Maciej Fijalkows… 80e8108 
Alex Gaynor f9f4bed 
mattip 3963e06 
Alex Gaynor f9f4bed 
Brian Kearns 11d464c 
Maciej Fijalkows… b834132 
Maciej Fijalkows… 107df78 


Brian Kearns 11d464c 
Maciej Fijalkows… b834132 



Maciej Fijalkows… e7b2477 
Maciej Fijalkows… b834132 

Maciej Fijalkows… 18ad78a 
Maciej Fijalkows… b834132 
Maciej Fijalkows… 18ad78a 
Maciej Fijalkows… e7b2477 

mattip a130f27 









Maciej Fijalkows… b834132 


Alex Gaynor e54c4fc 
Maciej Fijalkows… 040a65b 









Maciej Fijalkows… 80e8108 
Maciej Fijalkows… 040a65b 
Brian Kearns 0b3416c 

Brian Kearns 82f81f0 
Maciej Fijalkows… 040a65b 

mattip 6171c09 

Brian Kearns 18fc7ef 
Maciej Fijalkows… 040a65b 



Brian Kearns 82f81f0 
Maciej Fijalkows… 040a65b 



mattip 3963e06 
Maciej Fijalkows… 040a65b 







mattip 11f0a83 
Maciej Fijalkows… 040a65b 











Brian Kearns 0b3416c 
Maciej Fijalkows… 040a65b 












Brian Kearns 11d464c 
Maciej Fijalkows… 040a65b 

Alex Gaynor f9f4bed 
Maciej Fijalkows… 1f2faa7 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
import sys
from pypy.interpreter.baseobjspace import W_Root
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.typedef import (TypeDef, GetSetProperty,
    interp_attrproperty, interp_attrproperty_w)
from pypy.module.micronumpy import types, interp_boxes, base
from rpython.rlib.objectmodel import specialize
from rpython.rlib.rarithmetic import LONG_BIT, r_longlong, r_ulonglong
from rpython.rtyper.lltypesystem import rffi
from rpython.rlib import jit

if sys.byteorder == 'little':
    byteorder_prefix = '<'
    nonnative_byteorder_prefix = '>'
else:
    byteorder_prefix = '>'
    nonnative_byteorder_prefix = '<'

UNSIGNEDLTR = "u"
SIGNEDLTR = "i"
BOOLLTR = "b"
FLOATINGLTR = "f"
COMPLEXLTR = "c"
VOIDLTR = 'V'
STRINGLTR = 'S'
UNICODELTR = 'U'
INTPLTR = 'p'
UINTPLTR = 'P'

def decode_w_dtype(space, w_dtype):
    if space.is_none(w_dtype):
        return None
    return space.interp_w(W_Dtype,
          space.call_function(space.gettypefor(W_Dtype), w_dtype))

@jit.unroll_safe
def dtype_agreement(space, w_arr_list, shape, out=None):
    """ agree on dtype from a list of arrays. if out is allocated,
    use it's dtype, otherwise allocate a new one with agreed dtype
    """
    from pypy.module.micronumpy.interp_ufuncs import find_binop_result_dtype

    if not space.is_none(out):
        return out
    dtype = w_arr_list[0].get_dtype()
    for w_arr in w_arr_list[1:]:
        dtype = find_binop_result_dtype(space, dtype, w_arr.get_dtype())
    out = base.W_NDimArray.from_shape(space, shape, dtype)
    return out

class W_Dtype(W_Root):
    _immutable_fields_ = ["itemtype", "num", "kind", "shape"]

    def __init__(self, itemtype, num, kind, name, char, w_box_type,
                 alternate_constructors=[], aliases=[], float_type=None,
                 fields=None, fieldnames=None, native=True, shape=[], subdtype=None):
        self.itemtype = itemtype
        self.num = num
        self.kind = kind
        self.name = name
        self.char = char
        self.w_box_type = w_box_type
        self.alternate_constructors = alternate_constructors
        self.aliases = aliases
        self.float_type = float_type
        self.fields = fields
        self.fieldnames = fieldnames
        self.native = native
        self.shape = list(shape)
        self.subdtype = subdtype
        if not subdtype:
            self.base = self
        else:
            self.base = subdtype.base

    @specialize.argtype(1)
    def box(self, value):
        return self.itemtype.box(value)

    @specialize.argtype(1, 2)
    def box_complex(self, real, imag):
        return self.itemtype.box_complex(real, imag)

    def build_and_convert(self, space, box):
        return self.itemtype.build_and_convert(space, self, box)
    def coerce(self, space, w_item):
        return self.itemtype.coerce(space, self, w_item)

    def getitem(self, arr, i):
        item = self.itemtype.read(arr, i, 0)
        return item

    def getitem_bool(self, arr, i):
        return self.itemtype.read_bool(arr, i, 0)

    def setitem(self, arr, i, box):
        self.itemtype.store(arr, i, 0, box)

    def fill(self, storage, box, start, stop):
        self.itemtype.fill(storage, self.get_size(), box, start, stop, 0)

    def get_name(self):
        if self.char == 'S':
            return '|S' + str(self.get_size())
        return self.name

    def descr_str(self, space):
        return space.wrap(self.get_name())

    def descr_repr(self, space):
        return space.wrap("dtype('%s')" % self.get_name())

    def descr_get_itemsize(self, space):
        return space.wrap(self.itemtype.get_element_size())

    def descr_get_byteorder(self, space):
        if self.native:
            return space.wrap('=')
        return space.wrap(nonnative_byteorder_prefix)

    def descr_get_str(self, space):
        size = self.get_size()
        basic = self.kind
        if basic == UNICODELTR:
            size >>= 2
            endian = byteorder_prefix
        elif size <= 1:
            endian = '|'  # ignore
        elif self.native:
            endian = byteorder_prefix
        else:
            endian = nonnative_byteorder_prefix

        return space.wrap("%s%s%s" % (endian, basic, size))

    def descr_get_alignment(self, space):
        return space.wrap(self.itemtype.alignment)

    def descr_get_isnative(self, space):
        return space.wrap(self.native)

    def descr_get_base(self, space):
        return space.wrap(self.base)

    def descr_get_subdtype(self, space):
        return space.newtuple([space.wrap(self.subdtype), self.descr_get_shape(space)])

    def descr_get_shape(self, space):
        w_shape = [space.wrap(dim) for dim in self.shape]
        return space.newtuple(w_shape)

    def eq(self, space, w_other):
        w_other = space.call_function(space.gettypefor(W_Dtype), w_other)
        if space.is_w(self, w_other):
            return True
        if isinstance(w_other, W_Dtype):
            return space.eq_w(self.descr_reduce(space), w_other.descr_reduce(space))
        return False

    def descr_eq(self, space, w_other):
        return space.wrap(self.eq(space, w_other))

    def descr_ne(self, space, w_other):
        return space.wrap(not self.eq(space, w_other))

    def descr_get_fields(self, space):
        if self.fields is None:
            return space.w_None
        w_d = space.newdict()
        for name, (offset, subdtype) in self.fields.iteritems():
            space.setitem(w_d, space.wrap(name), space.newtuple([subdtype,
                                                                 space.wrap(offset)]))
        return w_d

    def set_fields(self, space, w_fields):
        if w_fields == space.w_None:
            self.fields = None
        else:
            self.fields = {}
            ofs_and_items = []
            size = 0
            for key in space.listview(w_fields):
                value = space.getitem(w_fields, key)

                dtype = space.getitem(value, space.wrap(0))
                assert isinstance(dtype, W_Dtype)

                offset = space.int_w(space.getitem(value, space.wrap(1)))
                self.fields[space.str_w(key)] = offset, dtype

                ofs_and_items.append((offset, dtype.itemtype))
                size += dtype.itemtype.get_element_size()

            self.itemtype = types.RecordType(ofs_and_items, size)
            self.name = "void" + str(8 * self.itemtype.get_element_size())

    def descr_get_names(self, space):
        if self.fieldnames is None:
            return space.w_None
        return space.newtuple([space.wrap(name) for name in self.fieldnames])

    def set_names(self, space, w_names):
        if w_names == space.w_None:
            self.fieldnames = None
        else:
            self.fieldnames = []
            iter = space.iter(w_names)
            while True:
                try:
                    self.fieldnames.append(space.str_w(space.next(iter)))
                except OperationError, e:
                    if not e.match(space, space.w_StopIteration):
                        raise
                    break

    @unwrap_spec(item=str)
    def descr_getitem(self, space, item):
        if self.fields is None:
            raise OperationError(space.w_KeyError, space.wrap("There are no keys in dtypes %s" % self.name))
        try:
            return self.fields[item][1]
        except KeyError:
            raise OperationError(space.w_KeyError, space.wrap("Field named %s not found" % item))

    def is_int_type(self):
        return (self.kind == SIGNEDLTR or self.kind == UNSIGNEDLTR or
                self.kind == BOOLLTR)

    def is_signed(self):
        return self.kind == SIGNEDLTR

    def is_complex_type(self):
        return self.kind == COMPLEXLTR

    def is_float_type(self):
        return (self.kind == FLOATINGLTR or self.float_type is not None)

    def is_bool_type(self):
        return self.kind == BOOLLTR

    def is_record_type(self):
        return self.fields is not None

    def is_str_type(self):
        return self.num == 18

    def is_str_or_unicode(self):
        return (self.num == 18 or self.num == 19)

    def is_flexible_type(self):
        return (self.is_str_or_unicode() or self.is_record_type())

    def __repr__(self):
        if self.fields is not None:
            return '<DType %r>' % self.fields
        return '<DType %r>' % self.itemtype

    def get_size(self):
        return self.itemtype.get_element_size()

    def descr_reduce(self, space):
        w_class = space.type(self)

        kind = self.kind
        elemsize = self.itemtype.get_element_size()
        builder_args = space.newtuple([space.wrap("%s%d" % (kind, elemsize)), space.wrap(0), space.wrap(1)])

        version = space.wrap(3)
        names = self.descr_get_names(space)
        values = self.descr_get_fields(space)
        if self.fields:
            order = space.wrap('|')
            #TODO: Implement this when subarrays are implemented
            subdescr = space.w_None
            size = 0
            for key in self.fields:
                dtype = self.fields[key][1]
                assert isinstance(dtype, W_Dtype)
                size += dtype.get_size()
            w_size = space.wrap(size)
            #TODO: Change this when alignment is implemented
            alignment = space.wrap(1)
        else:
            order = space.wrap(byteorder_prefix if self.native else nonnative_byteorder_prefix)
            subdescr = space.w_None
            w_size = space.wrap(-1)
            alignment = space.wrap(-1)
        flags = space.wrap(0)

        data = space.newtuple([version, order, subdescr, names, values, w_size, alignment, flags])

        return space.newtuple([w_class, builder_args, data])

    def descr_setstate(self, space, w_data):
        if space.int_w(space.getitem(w_data, space.wrap(0))) != 3:
            raise OperationError(space.w_NotImplementedError, space.wrap("Pickling protocol version not supported"))

        self.native = space.str_w(space.getitem(w_data, space.wrap(1))) == byteorder_prefix

        fieldnames = space.getitem(w_data, space.wrap(3))
        self.set_names(space, fieldnames)

        fields = space.getitem(w_data, space.wrap(4))
        self.set_fields(space, fields)

def dtype_from_list(space, w_lst):
    lst_w = space.listview(w_lst)
    fields = {}
    offset = 0
    ofs_and_items = []
    fieldnames = []
    for w_elem in lst_w:
        size = 1
        w_shape = space.newtuple([])
        if space.len_w(w_elem) == 3:
            w_fldname, w_flddesc, w_shape = space.fixedview(w_elem)
            if not base.issequence_w(space, w_shape):
                w_shape = space.newtuple([w_shape,])
        else:
            w_fldname, w_flddesc = space.fixedview(w_elem, 2)
        subdtype = descr__new__(space, space.gettypefor(W_Dtype), w_flddesc, w_shape=w_shape)
        fldname = space.str_w(w_fldname)
        if fldname in fields:
            raise OperationError(space.w_ValueError, space.wrap("two fields with the same name"))
        assert isinstance(subdtype, W_Dtype)
        fields[fldname] = (offset, subdtype)
        ofs_and_items.append((offset, subdtype.itemtype))
        offset += subdtype.itemtype.get_element_size() * size
        fieldnames.append(fldname)
    itemtype = types.RecordType(ofs_and_items, offset)
    return W_Dtype(itemtype, 20, VOIDLTR, "void" + str(8 * itemtype.get_element_size()),
                   "V", space.gettypefor(interp_boxes.W_VoidBox), fields=fields,
                   fieldnames=fieldnames)

def dtype_from_dict(space, w_dict):
    raise OperationError(space.w_NotImplementedError, space.wrap(
        "dtype from dict"))

def dtype_from_spec(space, name):
        raise OperationError(space.w_NotImplementedError, space.wrap(
            "dtype from spec"))

def descr__new__(space, w_subtype, w_dtype, w_align=None, w_copy=None, w_shape=None):
    # w_align and w_copy are necessary for pickling
    cache = get_dtype_cache(space)

    if w_shape is not None and (space.isinstance_w(w_shape, space.w_int) or space.len_w(w_shape) > 0):
        subdtype = descr__new__(space, w_subtype, w_dtype, w_align, w_copy)
        assert isinstance(subdtype, W_Dtype)
        size = 1
        if space.isinstance_w(w_shape, space.w_int):
            w_shape = space.newtuple([w_shape])
        shape = []
        for w_dim in space.fixedview(w_shape):
            dim = space.int_w(w_dim)
            shape.append(dim)
            size *= dim
        return W_Dtype(types.VoidType(subdtype.itemtype.get_element_size() * size), 20, VOIDLTR, "void" + str(8 * subdtype.itemtype.get_element_size() * size),
                    "V", space.gettypefor(interp_boxes.W_VoidBox), shape=shape, subdtype=subdtype)

    if space.is_none(w_dtype):
        return cache.w_float64dtype
    elif space.isinstance_w(w_dtype, w_subtype):
        return w_dtype
    elif space.isinstance_w(w_dtype, space.w_str):
        name = space.str_w(w_dtype)
        if ',' in name:
            return dtype_from_spec(space, name)
        try:
            return cache.dtypes_by_name[name]
        except KeyError:
            pass
        if name[0] in 'VSUc' or name[0] in '<>=' and name[1] in 'VSUc':
            return variable_dtype(space, name)
        raise OperationError(space.w_TypeError, space.wrap(
                       "data type %s not understood" % name))
    elif space.isinstance_w(w_dtype, space.w_list):
        return dtype_from_list(space, w_dtype)
    elif space.isinstance_w(w_dtype, space.w_tuple):
        return descr__new__(space, w_subtype, space.getitem(w_dtype, space.wrap(0)), w_align, w_copy, w_shape=space.getitem(w_dtype, space.wrap(1)))
    elif space.isinstance_w(w_dtype, space.w_dict):
        return dtype_from_dict(space, w_dtype)
    for dtype in cache.builtin_dtypes:
        if w_dtype in dtype.alternate_constructors:
            return dtype
        if w_dtype is dtype.w_box_type:
            return dtype
    msg = "data type not understood (value of type %T not expected here)"
    raise operationerrfmt(space.w_TypeError, msg, w_dtype)

W_Dtype.typedef = TypeDef("dtype",
    __module__ = "numpypy",
    __new__ = interp2app(descr__new__),

    __str__= interp2app(W_Dtype.descr_str),
    __repr__ = interp2app(W_Dtype.descr_repr),
    __eq__ = interp2app(W_Dtype.descr_eq),
    __ne__ = interp2app(W_Dtype.descr_ne),
    __getitem__ = interp2app(W_Dtype.descr_getitem),

    __reduce__ = interp2app(W_Dtype.descr_reduce),
    __setstate__ = interp2app(W_Dtype.descr_setstate),

    num = interp_attrproperty("num", cls=W_Dtype),
    kind = interp_attrproperty("kind", cls=W_Dtype),
    char = interp_attrproperty("char", cls=W_Dtype),
    type = interp_attrproperty_w("w_box_type", cls=W_Dtype),
    byteorder = GetSetProperty(W_Dtype.descr_get_byteorder),
    str = GetSetProperty(W_Dtype.descr_get_str),
    itemsize = GetSetProperty(W_Dtype.descr_get_itemsize),
    alignment = GetSetProperty(W_Dtype.descr_get_alignment),
    isnative = GetSetProperty(W_Dtype.descr_get_isnative),
    shape = GetSetProperty(W_Dtype.descr_get_shape),
    name = interp_attrproperty('name', cls=W_Dtype),
    fields = GetSetProperty(W_Dtype.descr_get_fields),
    names = GetSetProperty(W_Dtype.descr_get_names),
    subdtype = GetSetProperty(W_Dtype.descr_get_subdtype),
    base = GetSetProperty(W_Dtype.descr_get_base),
)
W_Dtype.typedef.acceptable_as_base_class = False


def variable_dtype(space, name):
    if name[0] in '<>=':
        name = name[1:]
    char = name[0]
    if len(name) == 1:
        size = 0
    else:
        try:
            size = int(name[1:])
        except ValueError:
            raise OperationError(space.w_TypeError, space.wrap("data type not understood"))
    if char == 'c':
        char = 'S'
        size = 1
    if char == 'S':
        itemtype = types.StringType(size)
        basename = 'string'
        num = 18
        w_box_type = space.gettypefor(interp_boxes.W_StringBox)
    elif char == 'V':
        num = 20
        basename = 'void'
        itemtype = types.VoidType(size)
        return W_Dtype(itemtype, 20, VOIDLTR, "void" + str(size),
                    "V", space.gettypefor(interp_boxes.W_VoidBox))
    else:
        assert char == 'U'
        basename = 'unicode'
        itemtype = types.UnicodeType(size)
        num = 19
        w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox)
    return W_Dtype(itemtype, num, char,
                   basename + str(8 * itemtype.get_element_size()),
                   char, w_box_type)

def new_string_dtype(space, size):
    itemtype = types.StringType(size)
    return W_Dtype(
        itemtype,
        num=18,
        kind=STRINGLTR,
        name='string' + str(8 * itemtype.get_element_size()),
        char='S',
        w_box_type = space.gettypefor(interp_boxes.W_StringBox),
    )

def new_unicode_dtype(space, size):
    itemtype = types.UnicodeType(size)
    return W_Dtype(
        itemtype,
        num=19,
        kind=UNICODELTR,
        name='unicode' + str(8 * itemtype.get_element_size()),
        char='U',
        w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox),
    )


class DtypeCache(object):
    def __init__(self, space):
        self.w_booldtype = W_Dtype(
            types.Bool(),
            num=0,
            kind=BOOLLTR,
            name="bool",
            char="?",
            w_box_type=space.gettypefor(interp_boxes.W_BoolBox),
            alternate_constructors=[space.w_bool],
        )
        self.w_int8dtype = W_Dtype(
            types.Int8(),
            num=1,
            kind=SIGNEDLTR,
            name="int8",
            char="b",
            w_box_type=space.gettypefor(interp_boxes.W_Int8Box)
        )
        self.w_uint8dtype = W_Dtype(
            types.UInt8(),
            num=2,
            kind=UNSIGNEDLTR,
            name="uint8",
            char="B",
            w_box_type=space.gettypefor(interp_boxes.W_UInt8Box),
        )
        self.w_int16dtype = W_Dtype(
            types.Int16(),
            num=3,
            kind=SIGNEDLTR,
            name="int16",
            char="h",
            w_box_type=space.gettypefor(interp_boxes.W_Int16Box),
        )
        self.w_uint16dtype = W_Dtype(
            types.UInt16(),
            num=4,
            kind=UNSIGNEDLTR,
            name="uint16",
            char="H",
            w_box_type=space.gettypefor(interp_boxes.W_UInt16Box),
        )
        self.w_int32dtype = W_Dtype(
            types.Int32(),
            num=5,
            kind=SIGNEDLTR,
            name="int32",
            char="i",
            w_box_type=space.gettypefor(interp_boxes.W_Int32Box),
       )
        self.w_uint32dtype = W_Dtype(
            types.UInt32(),
            num=6,
            kind=UNSIGNEDLTR,
            name="uint32",
            char="I",
            w_box_type=space.gettypefor(interp_boxes.W_UInt32Box),
        )
        self.w_longdtype = W_Dtype(
            types.Long(),
            num=7,
            kind=SIGNEDLTR,
            name="int%d" % LONG_BIT,
            char="l",
            w_box_type=space.gettypefor(interp_boxes.W_LongBox),
            alternate_constructors=[space.w_int,
                                    space.gettypefor(interp_boxes.W_IntegerBox),
                                    space.gettypefor(interp_boxes.W_SignedIntegerBox),
                                   ],
            aliases=['int'],
        )
        self.w_ulongdtype = W_Dtype(
            types.ULong(),
            num=8,
            kind=UNSIGNEDLTR,
            name="uint%d" % LONG_BIT,
            char="L",
            w_box_type=space.gettypefor(interp_boxes.W_ULongBox),
            alternate_constructors=[ space.gettypefor(interp_boxes.W_UnsignedIntegerBox),
                                   ],
            aliases=['uint'],
        )
        self.w_int64dtype = W_Dtype(
            types.Int64(),
            num=9,
            kind=SIGNEDLTR,
            name="int64",
            char="q",
            w_box_type=space.gettypefor(interp_boxes.W_Int64Box),
            alternate_constructors=[space.w_long],
        )
        self.w_uint64dtype = W_Dtype(
            types.UInt64(),
            num=10,
            kind=UNSIGNEDLTR,
            name="uint64",
            char="Q",
            w_box_type=space.gettypefor(interp_boxes.W_UInt64Box),
        )
        self.w_float32dtype = W_Dtype(
            types.Float32(),
            num=11,
            kind=FLOATINGLTR,
            name="float32",
            char="f",
            w_box_type=space.gettypefor(interp_boxes.W_Float32Box),
        )
        self.w_float64dtype = W_Dtype(
            types.Float64(),
            num=12,
            kind=FLOATINGLTR,
            name="float64",
            char="d",
            w_box_type = space.gettypefor(interp_boxes.W_Float64Box),
            alternate_constructors=[space.w_float,
                                    space.gettypefor(interp_boxes.W_NumberBox),
                                   ],
            aliases=["float", "double"],
        )
        self.w_floatlongdtype = W_Dtype(
            types.FloatLong(),
            num=13,
            kind=FLOATINGLTR,
            name="float%d" % (interp_boxes.long_double_size * 8),
            char="g",
            w_box_type=space.gettypefor(interp_boxes.W_FloatLongBox),
            aliases=["longdouble", "longfloat"],
        )
        self.w_complex64dtype = W_Dtype(
            types.Complex64(),
            num=14,
            kind=COMPLEXLTR,
            name="complex64",
            char="F",
            w_box_type = space.gettypefor(interp_boxes.W_Complex64Box),
            float_type = self.w_float32dtype,
        )
        self.w_complex128dtype = W_Dtype(
            types.Complex128(),
            num=15,
            kind=COMPLEXLTR,
            name="complex128",
            char="D",
            w_box_type = space.gettypefor(interp_boxes.W_Complex128Box),
            alternate_constructors=[space.w_complex],
            aliases=["complex"],
            float_type = self.w_float64dtype,
        )
        self.w_complexlongdtype = W_Dtype(
            types.ComplexLong(),
            num=16,
            kind=COMPLEXLTR,
            name="complex%d" % (interp_boxes.long_double_size * 16),
            char="G",
            w_box_type = space.gettypefor(interp_boxes.W_ComplexLongBox),
            aliases=["clongdouble", "clongfloat"],
            float_type = self.w_floatlongdtype,
        )
        self.w_stringdtype = W_Dtype(
            types.StringType(0),
            num=18,
            kind=STRINGLTR,
            name='string',
            char='S',
            w_box_type = space.gettypefor(interp_boxes.W_StringBox),
            alternate_constructors=[space.w_str, space.gettypefor(interp_boxes.W_CharacterBox)],
            aliases=["str"],
        )
        self.w_unicodedtype = W_Dtype(
            types.UnicodeType(0),
            num=19,
            kind=UNICODELTR,
            name='unicode',
            char='U',
            w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox),
            alternate_constructors=[space.w_unicode],
        )
        self.w_voiddtype = W_Dtype(
            types.VoidType(0),
            num=20,
            kind=VOIDLTR,
            name='void',
            char='V',
            w_box_type = space.gettypefor(interp_boxes.W_VoidBox),
            #alternate_constructors=[space.w_buffer],
            # XXX no buffer in space
            #alternate_constructors=[space.gettypefor(interp_boxes.W_GenericBox)],
            # XXX fix, leads to _coerce error
        )
        self.w_float16dtype = W_Dtype(
            types.Float16(),
            num=23,
            kind=FLOATINGLTR,
            name="float16",
            char="e",
            w_box_type=space.gettypefor(interp_boxes.W_Float16Box),
        )
        ptr_size = rffi.sizeof(rffi.CCHARP)
        if ptr_size == 4:
            intp_box = interp_boxes.W_Int32Box
            intp_type = types.Int32()
            intp_num = 5
            uintp_box = interp_boxes.W_UInt32Box
            uintp_type = types.UInt32()
            uintp_num = 6
        elif ptr_size == 8:
            intp_box = interp_boxes.W_Int64Box
            intp_type = types.Int64()
            intp_num = 7
            uintp_box = interp_boxes.W_UInt64Box
            uintp_type = types.UInt64()
            uintp_num = 8
        else:
            raise ValueError('unknown point size %d' % ptr_size)
        self.w_intpdtype = W_Dtype(
            intp_type,
            num=intp_num,
            kind=INTPLTR,
            name='intp',
            char=INTPLTR,
            w_box_type = space.gettypefor(intp_box),
        )
        self.w_uintpdtype = W_Dtype(
            uintp_type,
            num=uintp_num,
            kind=UINTPLTR,
            name='uintp',
            char=UINTPLTR,
            w_box_type = space.gettypefor(uintp_box),
        )
        float_dtypes = [self.w_float16dtype, self.w_float32dtype,
                        self.w_float64dtype, self.w_floatlongdtype]
        complex_dtypes = [self.w_complex64dtype, self.w_complex128dtype,
                          self.w_complexlongdtype]
        self.builtin_dtypes = [
            self.w_booldtype,
            self.w_int8dtype, self.w_uint8dtype,
            self.w_int16dtype, self.w_uint16dtype,
            self.w_longdtype, self.w_ulongdtype,
            self.w_int32dtype, self.w_uint32dtype,
            self.w_int64dtype, self.w_uint64dtype,
            ] + float_dtypes + complex_dtypes + [
            self.w_stringdtype, self.w_unicodedtype, self.w_voiddtype,
            self.w_intpdtype, self.w_uintpdtype,
        ]
        self.float_dtypes_by_num_bytes = sorted(
            (dtype.itemtype.get_element_size(), dtype)
            for dtype in float_dtypes
        )
        self.dtypes_by_num = {}
        self.dtypes_by_name = {}
        # we reverse, so the stuff with lower numbers override stuff with
        # higher numbers
        for dtype in reversed(self.builtin_dtypes):
            self.dtypes_by_num[dtype.num] = dtype
            self.dtypes_by_name[dtype.name] = dtype
            can_name = dtype.kind + str(dtype.itemtype.get_element_size())
            self.dtypes_by_name[can_name] = dtype
            self.dtypes_by_name[byteorder_prefix + can_name] = dtype
            self.dtypes_by_name['=' + can_name] = dtype
            new_name = nonnative_byteorder_prefix + can_name
            itemtypename = dtype.itemtype.__class__.__name__
            itemtype = getattr(types, 'NonNative' + itemtypename)()
            self.dtypes_by_name[new_name] = W_Dtype(
                itemtype,
                dtype.num, dtype.kind, new_name, dtype.char, dtype.w_box_type,
                native=False)
            if dtype.kind != dtype.char:
                can_name = dtype.char
                self.dtypes_by_name[byteorder_prefix + can_name] = dtype
                self.dtypes_by_name['=' + can_name] = dtype
                new_name = nonnative_byteorder_prefix + can_name
                self.dtypes_by_name[new_name] = W_Dtype(
                    itemtype,
                    dtype.num, dtype.kind, new_name, dtype.char, dtype.w_box_type,
                    native=False)

            for alias in dtype.aliases:
                self.dtypes_by_name[alias] = dtype
            self.dtypes_by_name[dtype.char] = dtype

        typeinfo_full = {
            'LONGLONG': self.w_int64dtype,
            'SHORT': self.w_int16dtype,
            'VOID': self.w_voiddtype,
            'UBYTE': self.w_uint8dtype,
            'UINTP': self.w_ulongdtype,
            'ULONG': self.w_ulongdtype,
            'LONG': self.w_longdtype,
            'UNICODE': self.w_unicodedtype,
            #'OBJECT',
            'ULONGLONG': self.w_uint64dtype,
            'STRING': self.w_stringdtype,
            'CFLOAT': self.w_complex64dtype,
            'CDOUBLE': self.w_complex128dtype,
            'CLONGDOUBLE': self.w_complexlongdtype,
            #'DATETIME',
            'UINT': self.w_uint32dtype,
            'INTP': self.w_intpdtype,
            'UINTP': self.w_uintpdtype,
            'HALF': self.w_float16dtype,
            'BYTE': self.w_int8dtype,
            #'TIMEDELTA',
            'INT': self.w_int32dtype,
            'DOUBLE': self.w_float64dtype,
            'LONGDOUBLE': self.w_floatlongdtype,
            'USHORT': self.w_uint16dtype,
            'FLOAT': self.w_float32dtype,
            'BOOL': self.w_booldtype,
        }

        typeinfo_partial = {
            'Generic': interp_boxes.W_GenericBox,
            'Character': interp_boxes.W_CharacterBox,
            'Flexible': interp_boxes.W_FlexibleBox,
            'Inexact': interp_boxes.W_InexactBox,
            'Integer': interp_boxes.W_IntegerBox,
            'SignedInteger': interp_boxes.W_SignedIntegerBox,
            'UnsignedInteger': interp_boxes.W_UnsignedIntegerBox,
            'ComplexFloating': interp_boxes.W_ComplexFloatingBox,
            'Number': interp_boxes.W_NumberBox,
            'Floating': interp_boxes.W_FloatingBox
        }
        w_typeinfo = space.newdict()
        for k, v in typeinfo_partial.iteritems():
            space.setitem(w_typeinfo, space.wrap(k), space.gettypefor(v))
        for k, dtype in typeinfo_full.iteritems():
            itemsize = dtype.itemtype.get_element_size()
            items_w = [space.wrap(dtype.char),
                       space.wrap(dtype.num),
                       space.wrap(itemsize * 8), # in case of changing
                       # number of bits per byte in the future
                       space.wrap(itemsize / (2 if dtype.kind == COMPLEXLTR else 1) or 1)]
            if dtype.is_int_type():
                if dtype.kind == BOOLLTR:
                    w_maxobj = space.wrap(1)
                    w_minobj = space.wrap(0)
                elif dtype.is_signed():
                    w_maxobj = space.wrap(r_longlong((1 << (itemsize*8 - 1))
                                          - 1))
                    w_minobj = space.wrap(r_longlong(-1) << (itemsize*8 - 1))
                else:
                    w_maxobj = space.wrap(r_ulonglong(1 << (itemsize*8)) - 1)
                    w_minobj = space.wrap(0)
                items_w = items_w + [w_maxobj, w_minobj]
            items_w = items_w + [dtype.w_box_type]
            space.setitem(w_typeinfo, space.wrap(k), space.newtuple(items_w))
        self.w_typeinfo = w_typeinfo

def get_dtype_cache(space):
    return space.fromcache(DtypeCache)