Source

pypy / pypy / module / micronumpy / interp_boxes.py

The branch 'remove-globals-in-jit' does not exist.
Full commit
Alex Gaynor 7578bf6 
Maciej Fijalkows… bb07db6 
Alex Gaynor 37fb24c 
Jasper Schulz 9b031aa 
Alex Gaynor 64e5eaa 
Maciej Fijalkows… 80adf83 
Maciej Fijalkows… 9b0ff4a 
Alex Gaynor ef2232d 
Jasper Schulz 9b031aa 
Alex Gaynor ef2232d 
mattip 14ed3b6 
Alex Gaynor b8595c5 
Maciej Fijalkows… 10caf3a 
Alex Gaynor 7578bf6 
Alex Gaynor dc4cacb 
Alex Gaynor ef2232d 

mattip 14ed3b6 

mattip 500caf9 



mattip 14ed3b6 
Alex Gaynor 0a3cae6 
Alex Gaynor 057601b 
Maciej Fijalkows… c8916d8 
Alex Gaynor b8595c5 

Alex Gaynor 0a3cae6 
Alex Gaynor 057601b 
Maciej Fijalkows… c8916d8 
Alex Gaynor 057601b 
Maciej Fijalkows… c8916d8 
Alex Gaynor b8595c5 
Alex Gaynor 0a3cae6 
Alex Gaynor fb125d8 








Jasper Schulz 7623c78 


mattip 36f68c5 
Jasper Schulz 7623c78 





mattip 0005634 





Alex Gaynor 7578bf6 
Alex Gaynor fb125d8 

Alex Gaynor 057601b 
Alex Gaynor a099f70 
Armin Rigo 93c1adc 
Alex Gaynor a099f70 

Maciej Fijalkows… c8916d8 


Alex Gaynor 64e5eaa 
Alex Gaynor dde7484 
Alex Gaynor 64e5eaa 
Alex Gaynor dde7484 

Alex Gaynor b8595c5 

Maciej Fijalkows… c8916d8 
Alex Gaynor a86f783 

Alex Gaynor b8595c5 

mattip 3157376 
Alex Gaynor a86f783 

Alex Gaynor b8595c5 
Alex Gaynor 64e5eaa 



Alex Gaynor b8595c5 
mattip 04f3673 
Alex Gaynor b8595c5 
mattip 04f3673 

Alex Gaynor b8595c5 

Alex Gaynor 256fe9f 
mattip 04f3673 
Alex Gaynor 256fe9f 
Alex Gaynor 0a3cae6 
mattip 04f3673 
Alex Gaynor 256fe9f 


mattip 04f3673 
Alex Gaynor 256fe9f 
mattip 04f3673 

Alex Gaynor 256fe9f 

Alex Gaynor 156f37c 
Alex Gaynor 256fe9f 

Alex Gaynor 603ee5d 
Alex Gaynor 820edf2 
Michael Blume 49eae25 
Alex Gaynor 886b1bb 
Maciej Fijalkows… c14c527 
Alex Gaynor fc7d59d 

Alex Gaynor 886b1bb 


Alex Gaynor 4412e08 
Alex Gaynor b8595c5 
Alex Gaynor 64e5eaa 
Alex Gaynor ce6baab 
Alex Gaynor 64e5eaa 


Alex Gaynor b8595c5 
Alex Gaynor d54332f 
Alex Gaynor c3836e8 
Alex Gaynor 256fe9f 
Alex Gaynor fc7d59d 
Alex Gaynor a33b00b 
Michael Blume 49eae25 
Alex Gaynor fc7d59d 
Alex Gaynor 5170d70 
Alex Gaynor fc7d59d 



Alex Gaynor 0792068 
Alex Gaynor 256fe9f 
Alex Gaynor 69d95ab 
Alex Gaynor 256fe9f 

Alex Gaynor 69d95ab 
Alex Gaynor 256fe9f 
Alex Gaynor fc7d59d 









Alex Gaynor 0a3cae6 


Maciej Fijalkows… c368093 
Alex Gaynor cf00150 

Alex Gaynor fb125d8 
Maciej Fijalkows… c8916d8 
Alex Gaynor 156f37c 
Alex Gaynor 7578bf6 
Alex Gaynor a86f783 
Alex Gaynor 7578bf6 

Maciej Fijalkows… 96b51ed 
Alex Gaynor 6d4db9f 
Alex Gaynor 7578bf6 



Alex Gaynor 716c084 
Alex Gaynor b8595c5 

Alex Gaynor fb125d8 
Maciej Fijalkows… c8916d8 
Alex Gaynor b8595c5 
Alex Gaynor 716c084 
Maciej Fijalkows… c8916d8 
Alex Gaynor b8595c5 
Alex Gaynor fb125d8 
Maciej Fijalkows… c8916d8 
Alex Gaynor b8595c5 
Alex Gaynor 716c084 
Maciej Fijalkows… c8916d8 
Alex Gaynor b8595c5 
Alex Gaynor fb125d8 
Maciej Fijalkows… c8916d8 
Alex Gaynor b8595c5 
Alex Gaynor 716c084 
Maciej Fijalkows… c8916d8 
Alex Gaynor b8595c5 
Alex Gaynor fb125d8 
Maciej Fijalkows… c8916d8 
Alex Gaynor b8595c5 
Alex Gaynor 716c084 
Maciej Fijalkows… c8916d8 
Alex Gaynor ef2232d 
Alex Gaynor fb125d8 
Maciej Fijalkows… c8916d8 
Alex Gaynor a33b00b 
Maciej Fijalkows… b91d1bd 
Maciej Fijalkows… c8916d8 
Alex Gaynor b8595c5 
Alex Gaynor 716c084 
Maciej Fijalkows… c8916d8 
Alex Gaynor a33b00b 
Maciej Fijalkows… b91d1bd 
Maciej Fijalkows… c8916d8 
Alex Gaynor 7578bf6 

Alex Gaynor a86f783 
Alex Gaynor 7578bf6 

Alex Gaynor a86f783 
Alex Gaynor 7578bf6 
mattip dcccbf6 


Alex Gaynor fb125d8 
Maciej Fijalkows… c8916d8 
Alex Gaynor b8595c5 
Alex Gaynor fb125d8 
Maciej Fijalkows… c8916d8 
Alex Gaynor b8595c5 
Maciej Fijalkows… a3f0a90 
Maciej Fijalkows… b07f0c0 
Maciej Fijalkows… bd3a6f1 
Maciej Fijalkows… 5fa9d10 
Maciej Fijalkows… b07f0c0 
Maciej Fijalkows… bb07db6 

Maciej Fijalkows… bd3a6f1 
Maciej Fijalkows… bb07db6 
Alex Gaynor 37fb24c 



Jasper Schulz 9b031aa 
Maciej Fijalkows… 36e4ff5 
Maciej Fijalkows… bb07db6 


Maciej Fijalkows… b07f0c0 
Maciej Fijalkows… bb07db6 
Maciej Fijalkows… bd3a6f1 

mattip 46c9ad0 




Maciej Fijalkows… bb07db6 
Maciej Fijalkows… bd3a6f1 


Maciej Fijalkows… b07f0c0 
Maciej Fijalkows… bd3a6f1 


Maciej Fijalkows… da66ea0 
Maciej Fijalkows… bd3a6f1 
Maciej Fijalkows… a3f0a90 
Maciej Fijalkows… 80adf83 



Maciej Fijalkows… 06b6c26 
Maciej Fijalkows… 36e4ff5 


Maciej Fijalkows… 10caf3a 
Maciej Fijalkows… 36e4ff5 

Maciej Fijalkows… f7f81a1 
Maciej Fijalkows… 80adf83 

Maciej Fijalkows… 06b6c26 
Maciej Fijalkows… 36e4ff5 

Maciej Fijalkows… 9b0ff4a 
Maciej Fijalkows… 10caf3a 

Maciej Fijalkows… 9b0ff4a 


Maciej Fijalkows… f7f81a1 
Alex Gaynor 7578bf6 
Jasper Schulz 9b031aa 


mattip 657c42e 



mattip c1db76d 
mattip 657c42e 




mattip c1db76d 
Jasper Schulz 9b031aa 
mattip 207ac3d 









mattip 11f0a83 


























Jasper Schulz 7623c78 
Alex Gaynor 7578bf6 
Alex Gaynor adbd652 
Alex Gaynor b8595c5 
Alex Gaynor a099f70 
Alex Gaynor 057601b 
Alex Gaynor 64e5eaa 
Alex Gaynor dde7484 

Alex Gaynor b8595c5 

Alex Gaynor 64e5eaa 
Alex Gaynor b8595c5 
Alex Gaynor 156f37c 
Alex Gaynor 256fe9f 

Alex Gaynor 603ee5d 
Alex Gaynor 820edf2 
Michael Blume 49eae25 
Alex Gaynor 886b1bb 
Alex Gaynor fc7d59d 
Maciej Fijalkows… c14c527 
Alex Gaynor fc7d59d 

Alex Gaynor 4412e08 
Alex Gaynor 886b1bb 

Alex Gaynor 256fe9f 
Alex Gaynor c3836e8 

Alex Gaynor 256fe9f 
Alex Gaynor fc7d59d 
Alex Gaynor a33b00b 
Michael Blume 49eae25 
Alex Gaynor fc7d59d 

Alex Gaynor 5170d70 
Alex Gaynor fc7d59d 



Alex Gaynor 0792068 
Alex Gaynor 256fe9f 
Alex Gaynor b8595c5 
Alex Gaynor 64e5eaa 
Alex Gaynor ce6baab 
Alex Gaynor 64e5eaa 


Alex Gaynor b8595c5 
Alex Gaynor 69d95ab 
Alex Gaynor 256fe9f 

Alex Gaynor 69d95ab 
Alex Gaynor cf00150 
Alex Gaynor 0a3cae6 

Maciej Fijalkows… c368093 
Alex Gaynor 7578bf6 


Alex Gaynor adbd652 
Alex Gaynor 057601b 
Alex Gaynor 37fb24c 

Alex Gaynor ef2232d 


Alex Gaynor adbd652 
Alex Gaynor ef2232d 


Alex Gaynor adbd652 
Alex Gaynor ef2232d 


Alex Gaynor adbd652 
Alex Gaynor ef2232d 

Alex Gaynor 716c084 



Alex Gaynor 9e74aaa 
Alex Gaynor adbd652 
Alex Gaynor 057601b 
Alex Gaynor 37fb24c 
Alex Gaynor 9e74aaa 

Alex Gaynor 716c084 
Alex Gaynor adbd652 
Alex Gaynor 716c084 
Alex Gaynor 37fb24c 
Alex Gaynor a099f70 


Alex Gaynor adbd652 
Alex Gaynor f1d05c0 
Alex Gaynor 37fb24c 
Alex Gaynor a099f70 

Alex Gaynor 716c084 
Alex Gaynor adbd652 
Alex Gaynor 716c084 
Alex Gaynor 37fb24c 
Alex Gaynor a099f70 

Maciej Fijalkows… 3890a6e 
Alex Gaynor adbd652 
Alex Gaynor f1d05c0 
Alex Gaynor 37fb24c 
Alex Gaynor a099f70 

Alex Gaynor 716c084 
Alex Gaynor adbd652 
Alex Gaynor 716c084 
Alex Gaynor 37fb24c 
Alex Gaynor a099f70 

Alex Gaynor ef2232d 
Alex Gaynor adbd652 
Alex Gaynor b443d8b 
Alex Gaynor 37fb24c 
Alex Gaynor a099f70 

Maciej Fijalkows… 3890a6e 






Alex Gaynor 716c084 
Alex Gaynor adbd652 
Alex Gaynor 716c084 
Alex Gaynor 37fb24c 
Alex Gaynor a099f70 


Alex Gaynor adbd652 
Alex Gaynor 64e5eaa 


Alex Gaynor adbd652 
Alex Gaynor 64e5eaa 

mattip dcccbf6 





Alex Gaynor 64e5eaa 
Alex Gaynor adbd652 
Alex Gaynor 24a17a8 

Alex Gaynor 64e5eaa 


Alex Gaynor adbd652 
Alex Gaynor 057601b 

Armin Rigo 93c1adc 
Maciej Fijalkows… 27230fa 
mattip 14ed3b6 





mattip 11f0a83 







mattip 14ed3b6 





Maciej Fijalkows… a3f0a90 
mattip 11f0a83 






Maciej Fijalkows… a3f0a90 





mattip b9a4275 
Maciej Fijalkows… bb07db6 
Maciej Fijalkows… bd3a6f1 
Maciej Fijalkows… a3f0a90 
Maciej Fijalkows… 80adf83 




Maciej Fijalkows… c8916d8 
Maciej Fijalkows… 80adf83 
Maciej Fijalkows… 06b6c26 
Maciej Fijalkows… 80adf83 

Maciej Fijalkows… c8916d8 
Maciej Fijalkows… 80adf83 
Maciej Fijalkows… 06b6c26 
Maciej Fijalkows… 80adf83 
Alex Gaynor 0a3cae6 
Jasper Schulz 9b031aa 



Jasper Schulz 7623c78 
Jasper Schulz 9b031aa 


mattip 207ac3d 

Jasper Schulz 7623c78 




mattip 207ac3d 

mattip 7658692 
  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
from pypy.interpreter.baseobjspace import Wrappable
from pypy.interpreter.error import operationerrfmt, OperationError
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.typedef import TypeDef, GetSetProperty
from pypy.objspace.std.floattype import float_typedef
from pypy.objspace.std.stringtype import str_typedef
from pypy.objspace.std.unicodetype import unicode_typedef, unicode_from_object
from pypy.objspace.std.inttype import int_typedef
from pypy.objspace.std.complextype import complex_typedef
from pypy.rlib.rarithmetic import LONG_BIT
from pypy.rpython.lltypesystem import rffi
from pypy.tool.sourcetools import func_with_new_name
from pypy.module.micronumpy.arrayimpl.voidbox import VoidBoxStorage

MIXIN_32 = (int_typedef,) if LONG_BIT == 32 else ()
MIXIN_64 = (int_typedef,) if LONG_BIT == 64 else ()

# Is this the proper place for this?
long_double_size = rffi.sizeof_c_type('long double', ignore_errors=True)
import os
if long_double_size == 8 and os.name == 'nt':
    # this is a lie, or maybe a wish
    long_double_size = 12


def new_dtype_getter(name):
    def _get_dtype(space):
        from pypy.module.micronumpy.interp_dtype import get_dtype_cache
        return getattr(get_dtype_cache(space), "w_%sdtype" % name)

    def new(space, w_subtype, w_value):
        dtype = _get_dtype(space)
        return dtype.itemtype.coerce_subtype(space, w_subtype, w_value)
    return func_with_new_name(new, name + "_box_new"), staticmethod(_get_dtype)


class PrimitiveBox(object):
    _mixin_ = True

    def __init__(self, value):
        self.value = value

    def convert_to(self, dtype):
        return dtype.box(self.value)

class ComplexBox(object):
    _mixin_ = True

    def __init__(self, real, imag=0.):
        self.real = real
        self.imag = imag

    def convert_to(self, dtype):
        return dtype.box_complex(self.real, self.imag)

    def convert_real_to(self, dtype):
        return dtype.box(self.real)

    def convert_imag_to(self, dtype):
        return dtype.box(self.imag)

class W_GenericBox(Wrappable):
    _attrs_ = ()

    def descr__new__(space, w_subtype, __args__):
        raise operationerrfmt(space.w_TypeError, "cannot create '%s' instances",
            w_subtype.getname(space, '?')
        )

    def get_dtype(self, space):
        return self._get_dtype(space)

    def descr_str(self, space):
        return space.wrap(self.get_dtype(space).itemtype.str_format(self))

    def descr_format(self, space, w_spec):
        return space.format(self.item(space), w_spec)

    def descr_int(self, space):
        box = self.convert_to(W_LongBox._get_dtype(space))
        assert isinstance(box, W_LongBox)
        return space.wrap(box.value)

    def descr_float(self, space):
        box = self.convert_to(W_Float64Box._get_dtype(space))
        assert isinstance(box, W_Float64Box)
        return space.wrap(box.value)

    def descr_nonzero(self, space):
        dtype = self.get_dtype(space)
        return space.wrap(dtype.itemtype.bool(self))

    def _binop_impl(ufunc_name):
        def impl(self, space, w_other, w_out=None):
            from pypy.module.micronumpy import interp_ufuncs
            return getattr(interp_ufuncs.get(space), ufunc_name).call(space,
                                                            [self, w_other, w_out])
        return func_with_new_name(impl, "binop_%s_impl" % ufunc_name)

    def _binop_right_impl(ufunc_name):
        def impl(self, space, w_other, w_out=None):
            from pypy.module.micronumpy import interp_ufuncs
            return getattr(interp_ufuncs.get(space), ufunc_name).call(space,
                                                            [w_other, self, w_out])
        return func_with_new_name(impl, "binop_right_%s_impl" % ufunc_name)

    def _unaryop_impl(ufunc_name):
        def impl(self, space, w_out=None):
            from pypy.module.micronumpy import interp_ufuncs
            return getattr(interp_ufuncs.get(space), ufunc_name).call(space,
                                                                    [self, w_out])
        return func_with_new_name(impl, "unaryop_%s_impl" % ufunc_name)

    descr_add = _binop_impl("add")
    descr_sub = _binop_impl("subtract")
    descr_mul = _binop_impl("multiply")
    descr_div = _binop_impl("divide")
    descr_truediv = _binop_impl("true_divide")
    descr_floordiv = _binop_impl("floor_divide")
    descr_mod = _binop_impl("mod")
    descr_pow = _binop_impl("power")
    descr_lshift = _binop_impl("left_shift")
    descr_rshift = _binop_impl("right_shift")
    descr_and = _binop_impl("bitwise_and")
    descr_or = _binop_impl("bitwise_or")
    descr_xor = _binop_impl("bitwise_xor")

    descr_eq = _binop_impl("equal")
    descr_ne = _binop_impl("not_equal")
    descr_lt = _binop_impl("less")
    descr_le = _binop_impl("less_equal")
    descr_gt = _binop_impl("greater")
    descr_ge = _binop_impl("greater_equal")

    descr_radd = _binop_right_impl("add")
    descr_rsub = _binop_right_impl("subtract")
    descr_rmul = _binop_right_impl("multiply")
    descr_rdiv = _binop_right_impl("divide")
    descr_rtruediv = _binop_right_impl("true_divide")
    descr_rfloordiv = _binop_right_impl("floor_divide")
    descr_rmod = _binop_right_impl("mod")
    descr_rpow = _binop_right_impl("power")
    descr_rlshift = _binop_right_impl("left_shift")
    descr_rrshift = _binop_right_impl("right_shift")
    descr_rand = _binop_right_impl("bitwise_and")
    descr_ror = _binop_right_impl("bitwise_or")
    descr_rxor = _binop_right_impl("bitwise_xor")

    descr_pos = _unaryop_impl("positive")
    descr_neg = _unaryop_impl("negative")
    descr_abs = _unaryop_impl("absolute")
    descr_invert = _unaryop_impl("invert")

    def descr_divmod(self, space, w_other):
        w_quotient = self.descr_div(space, w_other)
        w_remainder = self.descr_mod(space, w_other)
        return space.newtuple([w_quotient, w_remainder])

    def descr_rdivmod(self, space, w_other):
        w_quotient = self.descr_rdiv(space, w_other)
        w_remainder = self.descr_rmod(space, w_other)
        return space.newtuple([w_quotient, w_remainder])

    def descr_hash(self, space):
        return space.hash(self.item(space))

    def item(self, space):
        return self.get_dtype(space).itemtype.to_builtin_type(space, self)

class W_BoolBox(W_GenericBox, PrimitiveBox):
    descr__new__, _get_dtype = new_dtype_getter("bool")

class W_NumberBox(W_GenericBox):
    _attrs_ = ()

class W_IntegerBox(W_NumberBox):
    def int_w(self, space):
        return space.int_w(self.descr_int(space))

class W_SignedIntegerBox(W_IntegerBox):
    pass

class W_UnsignedIntegerBox(W_IntegerBox):
    pass

class W_Int8Box(W_SignedIntegerBox, PrimitiveBox):
    descr__new__, _get_dtype = new_dtype_getter("int8")

class W_UInt8Box(W_UnsignedIntegerBox, PrimitiveBox):
    descr__new__, _get_dtype = new_dtype_getter("uint8")

class W_Int16Box(W_SignedIntegerBox, PrimitiveBox):
    descr__new__, _get_dtype = new_dtype_getter("int16")

class W_UInt16Box(W_UnsignedIntegerBox, PrimitiveBox):
    descr__new__, _get_dtype = new_dtype_getter("uint16")

class W_Int32Box(W_SignedIntegerBox, PrimitiveBox):
    descr__new__, _get_dtype = new_dtype_getter("int32")

class W_UInt32Box(W_UnsignedIntegerBox, PrimitiveBox):
    descr__new__, _get_dtype = new_dtype_getter("uint32")

class W_LongBox(W_SignedIntegerBox, PrimitiveBox):
    descr__new__, _get_dtype = new_dtype_getter("long")

class W_ULongBox(W_UnsignedIntegerBox, PrimitiveBox):
    descr__new__, _get_dtype = new_dtype_getter("ulong")

class W_Int64Box(W_SignedIntegerBox, PrimitiveBox):
    descr__new__, _get_dtype = new_dtype_getter("int64")

class W_LongLongBox(W_SignedIntegerBox, PrimitiveBox):
    descr__new__, _get_dtype = new_dtype_getter('longlong')

class W_UInt64Box(W_UnsignedIntegerBox, PrimitiveBox):
    descr__new__, _get_dtype = new_dtype_getter("uint64")

class W_ULongLongBox(W_SignedIntegerBox, PrimitiveBox):
    descr__new__, _get_dtype = new_dtype_getter('ulonglong')

class W_InexactBox(W_NumberBox):
    _attrs_ = ()

class W_FloatingBox(W_InexactBox):
    _attrs_ = ()

class W_Float16Box(W_FloatingBox, PrimitiveBox):
    descr__new__, _get_dtype = new_dtype_getter("float16")

class W_Float32Box(W_FloatingBox, PrimitiveBox):
    descr__new__, _get_dtype = new_dtype_getter("float32")

class W_Float64Box(W_FloatingBox, PrimitiveBox):
    descr__new__, _get_dtype = new_dtype_getter("float64")

class W_FlexibleBox(W_GenericBox):
    def __init__(self, arr, ofs, dtype):
        self.arr = arr # we have to keep array alive
        self.ofs = ofs
        self.dtype = dtype

    def get_dtype(self, space):
        return self.arr.dtype

@unwrap_spec(self=W_GenericBox)
def descr_index(space, self):
    return space.index(self.item(space))


class W_VoidBox(W_FlexibleBox):
    @unwrap_spec(item=str)
    def descr_getitem(self, space, item):
        try:
            ofs, dtype = self.dtype.fields[item]
        except KeyError:
            raise OperationError(space.w_IndexError,
                                 space.wrap("Field %s does not exist" % item))
        read_val = dtype.itemtype.read(self.arr, self.ofs, ofs, dtype)
        if isinstance (read_val, W_StringBox):
            # StringType returns a str
            return space.wrap(dtype.itemtype.to_str(read_val))
        return read_val

    @unwrap_spec(item=str)
    def descr_setitem(self, space, item, w_value):
        try:
            ofs, dtype = self.dtype.fields[item]
        except KeyError:
            raise OperationError(space.w_IndexError,
                                 space.wrap("Field %s does not exist" % item))
        dtype.itemtype.store(self.arr, self.ofs, ofs,
                             dtype.coerce(space, w_value))

class W_CharacterBox(W_FlexibleBox):
    pass

class W_StringBox(W_CharacterBox):
    def descr__new__string_box(space, w_subtype, w_arg):
        from pypy.module.micronumpy.interp_dtype import new_string_dtype

        arg = space.str_w(space.str(w_arg))
        arr = VoidBoxStorage(len(arg), new_string_dtype(space, len(arg)))
        for i in range(len(arg)):
            arr.storage[i] = arg[i]
        return W_StringBox(arr, 0, arr.dtype)

class W_UnicodeBox(W_CharacterBox):
    def descr__new__unicode_box(space, w_subtype, w_arg):
        from pypy.module.micronumpy.interp_dtype import new_unicode_dtype

        arg = space.unicode_w(unicode_from_object(space, w_arg))
        # XXX size computations, we need tests anyway
        arr = VoidBoxStorage(len(arg), new_unicode_dtype(space, len(arg)))
        # XXX not this way, we need store
        #for i in range(len(arg)):
        #    arr.storage[i] = arg[i]
        return W_UnicodeBox(arr, 0, arr.dtype)


class W_ComplexFloatingBox(W_InexactBox):
    _attrs_ = ()
    def descr_get_real(self, space):
        dtype = self._COMPONENTS_BOX._get_dtype(space)
        box = self.convert_real_to(dtype)
        assert isinstance(box, self._COMPONENTS_BOX)
        return space.wrap(box)

    def descr_get_imag(self, space):
        dtype = self._COMPONENTS_BOX._get_dtype(space)
        box = self.convert_imag_to(dtype)
        assert isinstance(box, self._COMPONENTS_BOX)
        return space.wrap(box)


class W_Complex64Box(ComplexBox, W_ComplexFloatingBox):
    descr__new__, _get_dtype = new_dtype_getter("complex64")
    _COMPONENTS_BOX = W_Float32Box


class W_Complex128Box(ComplexBox, W_ComplexFloatingBox):
    descr__new__, _get_dtype = new_dtype_getter("complex128")
    _COMPONENTS_BOX = W_Float64Box

if long_double_size == 12:
    class W_Float96Box(W_FloatingBox, PrimitiveBox):
        descr__new__, _get_dtype = new_dtype_getter("float96")

    W_LongDoubleBox = W_Float96Box

    class W_Complex192Box(ComplexBox, W_ComplexFloatingBox):
        descr__new__, _get_dtype = new_dtype_getter("complex192")
        _COMPONENTS_BOX = W_Float96Box

    W_CLongDoubleBox = W_Complex192Box

elif long_double_size == 16:
    class W_Float128Box(W_FloatingBox, PrimitiveBox):
        descr__new__, _get_dtype = new_dtype_getter("float128")
    W_LongDoubleBox = W_Float128Box

    class W_Complex256Box(ComplexBox, W_ComplexFloatingBox):
        descr__new__, _get_dtype = new_dtype_getter("complex256")
        _COMPONENTS_BOX = W_Float128Box

    W_CLongDoubleBox = W_Complex256Box

else:
    W_LongDoubleBox = W_Float64Box
    W_CLongDoubleBox = W_Complex64Box

    
W_GenericBox.typedef = TypeDef("generic",
    __module__ = "numpypy",

    __new__ = interp2app(W_GenericBox.descr__new__.im_func),

    __str__ = interp2app(W_GenericBox.descr_str),
    __repr__ = interp2app(W_GenericBox.descr_str),
    __format__ = interp2app(W_GenericBox.descr_format),
    __int__ = interp2app(W_GenericBox.descr_int),
    __float__ = interp2app(W_GenericBox.descr_float),
    __nonzero__ = interp2app(W_GenericBox.descr_nonzero),

    __add__ = interp2app(W_GenericBox.descr_add),
    __sub__ = interp2app(W_GenericBox.descr_sub),
    __mul__ = interp2app(W_GenericBox.descr_mul),
    __div__ = interp2app(W_GenericBox.descr_div),
    __truediv__ = interp2app(W_GenericBox.descr_truediv),
    __floordiv__ = interp2app(W_GenericBox.descr_floordiv),
    __mod__ = interp2app(W_GenericBox.descr_mod),
    __divmod__ = interp2app(W_GenericBox.descr_divmod),
    __pow__ = interp2app(W_GenericBox.descr_pow),
    __lshift__ = interp2app(W_GenericBox.descr_lshift),
    __rshift__ = interp2app(W_GenericBox.descr_rshift),
    __and__ = interp2app(W_GenericBox.descr_and),
    __or__ = interp2app(W_GenericBox.descr_or),
    __xor__ = interp2app(W_GenericBox.descr_xor),

    __radd__ = interp2app(W_GenericBox.descr_radd),
    __rsub__ = interp2app(W_GenericBox.descr_rsub),
    __rmul__ = interp2app(W_GenericBox.descr_rmul),
    __rdiv__ = interp2app(W_GenericBox.descr_rdiv),
    __rtruediv__ = interp2app(W_GenericBox.descr_rtruediv),
    __rfloordiv__ = interp2app(W_GenericBox.descr_rfloordiv),
    __rmod__ = interp2app(W_GenericBox.descr_rmod),
    __rdivmod__ = interp2app(W_GenericBox.descr_rdivmod),
    __rpow__ = interp2app(W_GenericBox.descr_rpow),
    __rlshift__ = interp2app(W_GenericBox.descr_rlshift),
    __rrshift__ = interp2app(W_GenericBox.descr_rrshift),
    __rand__ = interp2app(W_GenericBox.descr_rand),
    __ror__ = interp2app(W_GenericBox.descr_ror),
    __rxor__ = interp2app(W_GenericBox.descr_rxor),

    __eq__ = interp2app(W_GenericBox.descr_eq),
    __ne__ = interp2app(W_GenericBox.descr_ne),
    __lt__ = interp2app(W_GenericBox.descr_lt),
    __le__ = interp2app(W_GenericBox.descr_le),
    __gt__ = interp2app(W_GenericBox.descr_gt),
    __ge__ = interp2app(W_GenericBox.descr_ge),

    __pos__ = interp2app(W_GenericBox.descr_pos),
    __neg__ = interp2app(W_GenericBox.descr_neg),
    __abs__ = interp2app(W_GenericBox.descr_abs),
    __invert__ = interp2app(W_GenericBox.descr_invert),

    __hash__ = interp2app(W_GenericBox.descr_hash),

    tolist = interp2app(W_GenericBox.item),
)

W_BoolBox.typedef = TypeDef("bool_", W_GenericBox.typedef,
    __module__ = "numpypy",
    __new__ = interp2app(W_BoolBox.descr__new__.im_func),

    __index__ = interp2app(descr_index),
)

W_NumberBox.typedef = TypeDef("number", W_GenericBox.typedef,
    __module__ = "numpypy",
)

W_IntegerBox.typedef = TypeDef("integer", W_NumberBox.typedef,
    __module__ = "numpypy",
)

W_SignedIntegerBox.typedef = TypeDef("signedinteger", W_IntegerBox.typedef,
    __module__ = "numpypy",
)

W_UnsignedIntegerBox.typedef = TypeDef("unsignedinteger", W_IntegerBox.typedef,
    __module__ = "numpypy",
)

W_Int8Box.typedef = TypeDef("int8", W_SignedIntegerBox.typedef,
    __module__ = "numpypy",
    __new__ = interp2app(W_Int8Box.descr__new__.im_func),
    __index__ = interp2app(descr_index),
)

W_UInt8Box.typedef = TypeDef("uint8", W_UnsignedIntegerBox.typedef,
    __module__ = "numpypy",
    __new__ = interp2app(W_UInt8Box.descr__new__.im_func),
    __index__ = interp2app(descr_index),
)

W_Int16Box.typedef = TypeDef("int16", W_SignedIntegerBox.typedef,
    __module__ = "numpypy",
    __new__ = interp2app(W_Int16Box.descr__new__.im_func),
    __index__ = interp2app(descr_index),
)

W_UInt16Box.typedef = TypeDef("uint16", W_UnsignedIntegerBox.typedef,
    __module__ = "numpypy",
    __new__ = interp2app(W_UInt16Box.descr__new__.im_func),
    __index__ = interp2app(descr_index),
)

W_Int32Box.typedef = TypeDef("int32", (W_SignedIntegerBox.typedef,) + MIXIN_32,
    __module__ = "numpypy",
    __new__ = interp2app(W_Int32Box.descr__new__.im_func),
    __index__ = interp2app(descr_index),
)

W_UInt32Box.typedef = TypeDef("uint32", W_UnsignedIntegerBox.typedef,
    __module__ = "numpypy",
    __new__ = interp2app(W_UInt32Box.descr__new__.im_func),
    __index__ = interp2app(descr_index),
)

W_Int64Box.typedef = TypeDef("int64", (W_SignedIntegerBox.typedef,) + MIXIN_64,
    __module__ = "numpypy",
    __new__ = interp2app(W_Int64Box.descr__new__.im_func),
    __index__ = interp2app(descr_index),
)

if LONG_BIT == 32:
    W_LongBox = W_Int32Box
    W_ULongBox = W_UInt32Box
elif LONG_BIT == 64:
    W_LongBox = W_Int64Box
    W_ULongBox = W_UInt64Box

W_UInt64Box.typedef = TypeDef("uint64", W_UnsignedIntegerBox.typedef,
    __module__ = "numpypy",
    __new__ = interp2app(W_UInt64Box.descr__new__.im_func),
    __index__ = interp2app(descr_index),
)

W_InexactBox.typedef = TypeDef("inexact", W_NumberBox.typedef,
    __module__ = "numpypy",
)

W_FloatingBox.typedef = TypeDef("floating", W_InexactBox.typedef,
    __module__ = "numpypy",
)

W_Float16Box.typedef = TypeDef("float16", W_FloatingBox.typedef,
    __module__ = "numpypy",

    __new__ = interp2app(W_Float16Box.descr__new__.im_func),
)

W_Float32Box.typedef = TypeDef("float32", W_FloatingBox.typedef,
    __module__ = "numpypy",

    __new__ = interp2app(W_Float32Box.descr__new__.im_func),
)

W_Float64Box.typedef = TypeDef("float64", (W_FloatingBox.typedef, float_typedef),
    __module__ = "numpypy",

    __new__ = interp2app(W_Float64Box.descr__new__.im_func),
)

if long_double_size == 12:
    W_Float96Box.typedef = TypeDef("float96", (W_FloatingBox.typedef),
        __module__ = "numpypy",

        __new__ = interp2app(W_Float96Box.descr__new__.im_func),
    )

    W_Complex192Box.typedef = TypeDef("complex192", (W_ComplexFloatingBox.typedef, complex_typedef),
        __module__ = "numpypy",
        __new__ = interp2app(W_Complex192Box.descr__new__.im_func),
        real = GetSetProperty(W_ComplexFloatingBox.descr_get_real),
        imag = GetSetProperty(W_ComplexFloatingBox.descr_get_imag),
    )

elif long_double_size == 16:
    W_Float128Box.typedef = TypeDef("float128", (W_FloatingBox.typedef),
        __module__ = "numpypy",

        __new__ = interp2app(W_Float128Box.descr__new__.im_func),
    )

    W_Complex256Box.typedef = TypeDef("complex256", (W_ComplexFloatingBox.typedef, complex_typedef),
        __module__ = "numpypy",
        __new__ = interp2app(W_Complex256Box.descr__new__.im_func),
        real = GetSetProperty(W_ComplexFloatingBox.descr_get_real),
        imag = GetSetProperty(W_ComplexFloatingBox.descr_get_imag),
    )

W_FlexibleBox.typedef = TypeDef("flexible", W_GenericBox.typedef,
    __module__ = "numpypy",
)

W_VoidBox.typedef = TypeDef("void", W_FlexibleBox.typedef,
    __module__ = "numpypy",
    __new__ = interp2app(W_VoidBox.descr__new__.im_func),
    __getitem__ = interp2app(W_VoidBox.descr_getitem),
    __setitem__ = interp2app(W_VoidBox.descr_setitem),
)

W_CharacterBox.typedef = TypeDef("character", W_FlexibleBox.typedef,
    __module__ = "numpypy",
)

W_StringBox.typedef = TypeDef("string_", (str_typedef, W_CharacterBox.typedef),
    __module__ = "numpypy",
    __new__ = interp2app(W_StringBox.descr__new__string_box.im_func),
)

W_UnicodeBox.typedef = TypeDef("unicode_", (unicode_typedef, W_CharacterBox.typedef),
    __module__ = "numpypy",
    __new__ = interp2app(W_UnicodeBox.descr__new__unicode_box.im_func),
)

W_ComplexFloatingBox.typedef = TypeDef("complexfloating", W_InexactBox.typedef,
    __module__ = "numpypy",
)


W_Complex128Box.typedef = TypeDef("complex128", (W_ComplexFloatingBox.typedef, complex_typedef),
    __module__ = "numpypy",
    __new__ = interp2app(W_Complex128Box.descr__new__.im_func),
    real = GetSetProperty(W_ComplexFloatingBox.descr_get_real),
    imag = GetSetProperty(W_ComplexFloatingBox.descr_get_imag),
)

W_Complex64Box.typedef = TypeDef("complex64", (W_ComplexFloatingBox.typedef),
    __module__ = "numpypy",
    __new__ = interp2app(W_Complex64Box.descr__new__.im_func),
    real = GetSetProperty(W_ComplexFloatingBox .descr_get_real),
    imag = GetSetProperty(W_ComplexFloatingBox.descr_get_imag),
)