File: ffitypes.py

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (332 lines) | stat: -rw-r--r-- 10,728 bytes parent folder | download | duplicates (2)
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
from pypy.interpreter.error import oefmt

from rpython.rtyper.lltypesystem import rffi
from rpython.rlib.rarithmetic import r_singlefloat, r_longfloat
from rpython.rlib.rbigint import rbigint

from pypy.module._cffi_backend import newtype

# Mixins to refactor type-specific codef from converter and executor classes
# (in converter.py and executor.py, respectively). To get the right mixin, a
# non-RPython function typeid() is used.

class State(object):
    def __init__(self, space):
        nt = newtype     # module from _cffi_backend

        # builtin types
        self.c_void    = nt.new_void_type(space)
        self.c_bool    = nt.new_primitive_type(space, '_Bool')
        self.c_char    = nt.new_primitive_type(space, 'char')
        self.c_uchar   = nt.new_primitive_type(space, 'unsigned char')
        self.c_short   = nt.new_primitive_type(space, 'short')
        self.c_ushort  = nt.new_primitive_type(space, 'unsigned short')
        self.c_int     = nt.new_primitive_type(space, 'int')
        self.c_uint    = nt.new_primitive_type(space, 'unsigned int')
        self.c_long    = nt.new_primitive_type(space, 'long')
        self.c_ulong   = nt.new_primitive_type(space, 'unsigned long')
        self.c_llong   = nt.new_primitive_type(space, 'long long')
        self.c_ullong  = nt.new_primitive_type(space, 'unsigned long long')
        self.c_float   = nt.new_primitive_type(space, 'float')
        self.c_double  = nt.new_primitive_type(space, 'double')
        self.c_ldouble = nt.new_primitive_type(space, 'long double')
        
        # pointer types
        self.c_ccharp = nt.new_pointer_type(space, self.c_char)
        self.c_voidp  = nt.new_pointer_type(space, self.c_void)

        # special types
        self.c_size_t    = nt.new_primitive_type(space, 'size_t')
        self.c_ptrdiff_t = nt.new_primitive_type(space, 'ptrdiff_t')

class BoolTypeMixin(object):
    _mixin_     = True
    _immutable_fields_ = ['c_type', 'c_ptrtype']

    c_type      = rffi.UCHAR
    c_ptrtype   = rffi.UCHARP

    def _wrap_object(self, space, obj):
        return space.newbool(bool(ord(rffi.cast(rffi.CHAR, obj))))

    def _unwrap_object(self, space, w_obj):
        arg = space.c_int_w(w_obj)
        if arg != False and arg != True:
            raise oefmt(space.w_ValueError,
                        "boolean value should be bool, or integer 1 or 0")
        return arg

    def cffi_type(self, space):
        state = space.fromcache(State)
        return state.c_bool

class CharTypeMixin(object):
    _mixin_     = True
    _immutable_fields_ = ['c_type', 'c_ptrtype']

    c_type      = rffi.CHAR
    c_ptrtype   = rffi.CCHARP           # there's no such thing as rffi.CHARP

    def _wrap_object(self, space, obj):
        return space.newbytes(obj)

    def _unwrap_object(self, space, w_value):
        # allow int to pass to char and make sure that str is of length 1
        if space.isinstance_w(w_value, space.w_int):
            ival = space.c_int_w(w_value)
            if ival < -128 or 127 < ival:
                raise oefmt(space.w_ValueError, "char arg not in range(-128,128)")

            value = rffi.cast(rffi.CHAR, space.c_int_w(w_value))
        else:
            if space.isinstance_w(w_value, space.w_text):
                value = space.text_w(w_value)
            else:
                value = space.bytes_w(w_value)
            if len(value) != 1:
                raise oefmt(space.w_ValueError,
                            "char expected, got string of size %d", len(value))

        value = rffi.cast(rffi.CHAR, value[0])
        return value     # turn it into a "char" to the annotator

    def cffi_type(self, space):
        state = space.fromcache(State)
        return state.c_char

class UCharTypeMixin(object):
    _mixin_     = True
    _immutable_fields_ = ['c_type', 'c_ptrtype']

    c_type      = rffi.UCHAR
    c_ptrtype   = rffi.CCHARP           # there's no such thing as rffi.UCHARP

    def _wrap_object(self, space, obj):
        return space.newbytes(obj)

    def _unwrap_object(self, space, w_value):
        # allow int to pass to char and make sure that str is of length 1
        if space.isinstance_w(w_value, space.w_int):
            ival = space.c_int_w(w_value)
            if ival < 0 or 256 <= ival:
                raise oefmt(space.w_ValueError, "char arg not in range(256)")

            value = rffi.cast(rffi.CHAR, space.c_int_w(w_value))
        else:
            if space.isinstance_w(w_value, space.w_text):
                value = space.text_w(w_value)
            else:
                value = space.bytes_w(w_value)
            if len(value) != 1:
                raise oefmt(space.w_ValueError,
                            "unsigned char expected, got string of size %d", len(value))

        value = rffi.cast(rffi.CHAR, value[0])
        return value     # turn it into a "char" to the annotator

    def cffi_type(self, space):
        state = space.fromcache(State)
        return state.c_char

class BaseIntTypeMixin(object):
    _mixin_     = True

    def _wrap_object(self, space, obj):
        return space.newint(rffi.cast(rffi.INT, obj))

    def _unwrap_object(self, space, w_obj):
        return rffi.cast(self.c_type, space.c_int_w(w_obj))

class ShortTypeMixin(BaseIntTypeMixin):
    _mixin_     = True
    _immutable_fields_ = ['c_type', 'c_ptrtype']

    c_type      = rffi.SHORT
    c_ptrtype   = rffi.SHORTP

class UShortTypeMixin(BaseIntTypeMixin):
    def cffi_type(self, space):
        state = space.fromcache(State)
        return state.c_short

    _mixin_     = True
    _immutable_fields_ = ['c_type', 'c_ptrtype']

    c_type      = rffi.USHORT
    c_ptrtype   = rffi.USHORTP

class IntTypeMixin(BaseIntTypeMixin):
    def cffi_type(self, space):
        state = space.fromcache(State)
        return state.c_ushort

    _mixin_     = True
    _immutable_fields_ = ['c_type', 'c_ptrtype']

    c_type      = rffi.INT
    c_ptrtype   = rffi.INTP

    def cffi_type(self, space):
        state = space.fromcache(State)
        return state.c_int

class UIntTypeMixin(object):
    _mixin_     = True
    _immutable_fields_ = ['c_type', 'c_ptrtype']

    c_type      = rffi.UINT
    c_ptrtype   = rffi.UINTP

    def _wrap_object(self, space, obj):
        return space.newlong_from_rarith_int(obj)

    def _unwrap_object(self, space, w_obj):
        return rffi.cast(self.c_type, space.uint_w(w_obj))

    def cffi_type(self, space):
        state = space.fromcache(State)
        return state.c_uint

class LongTypeMixin(object):
    _mixin_     = True
    _immutable_fields_ = ['c_type', 'c_ptrtype']

    c_type      = rffi.LONG
    c_ptrtype   = rffi.LONGP

    def _wrap_object(self, space, obj):
        return space.newlong(obj)

    def _unwrap_object(self, space, w_obj):
        return space.int_w(w_obj)

    def cffi_type(self, space):
        state = space.fromcache(State)
        return state.c_long

class ULongTypeMixin(object):
    _mixin_     = True
    _immutable_fields_ = ['c_type', 'c_ptrtype']

    c_type      = rffi.ULONG
    c_ptrtype   = rffi.ULONGP

    def _wrap_object(self, space, obj):
        return space.newlong_from_rarith_int(obj)

    def _unwrap_object(self, space, w_obj):
        return space.uint_w(w_obj)

    def cffi_type(self, space):
        state = space.fromcache(State)
        return state.c_ulong

class LongLongTypeMixin(object):
    _mixin_     = True
    _immutable_fields_ = ['c_type', 'c_ptrtype']

    c_type      = rffi.LONGLONG
    c_ptrtype   = rffi.LONGLONGP

    def _wrap_object(self, space, obj):
        return space.newlong_from_rarith_int(obj)

    def _unwrap_object(self, space, w_obj):
        return space.r_longlong_w(w_obj)

    def cffi_type(self, space):
        state = space.fromcache(State)
        return state.c_llong

class ULongLongTypeMixin(object):
    _mixin_     = True
    _immutable_fields_ = ['c_type', 'c_ptrtype']

    c_type      = rffi.ULONGLONG
    c_ptrtype   = rffi.ULONGLONGP

    def _wrap_object(self, space, obj):
        return space.newlong_from_rarith_int(obj)

    def _unwrap_object(self, space, w_obj):
        return space.r_ulonglong_w(w_obj)

    def cffi_type(self, space):
        state = space.fromcache(State)
        return state.c_ullong

class FloatTypeMixin(object):
    _mixin_     = True
    _immutable_fields_ = ['c_type', 'c_ptrtype', 'typecode']

    c_type      = rffi.FLOAT
    c_ptrtype   = rffi.FLOATP
    typecode    = 'f'

    def _unwrap_object(self, space, w_obj):
        return r_singlefloat(space.float_w(w_obj))

    def _wrap_object(self, space, obj):
        return space.newfloat(float(obj))

    def cffi_type(self, space):
        state = space.fromcache(State)
        return state.c_float

class DoubleTypeMixin(object):
    _mixin_     = True
    _immutable_fields_ = ['c_type', 'c_ptrtype', 'typecode']

    c_type      = rffi.DOUBLE
    c_ptrtype   = rffi.DOUBLEP
    typecode    = 'd'

    def _wrap_object(self, space, obj):
        return space.newfloat(obj)

    def _unwrap_object(self, space, w_obj):
        return space.float_w(w_obj)

    def cffi_type(self, space):
        state = space.fromcache(State)
        return state.c_double

class LongDoubleTypeMixin(object):
    _mixin_     = True
    _immutable_fields_ = ['c_type', 'c_ptrtype', 'typecode']

    c_type      = rffi.LONGDOUBLE
    # c_ptrtype   = rffi.LONGDOUBLEP   # useless type at this point
    c_ptrtype   = rffi.VOIDP
    typecode    = 'g'

    # long double is not really supported ...
    def _unwrap_object(self, space, w_obj):
        return r_longfloat(space.float_w(w_obj))

    def _wrap_object(self, space, obj):
        return space.newfloat(obj)

    def cffi_type(self, space):
        state = space.fromcache(State)
        return state.c_ldouble

def typeid(c_type):
    "NOT_RPYTHON"
    if c_type == bool:            return BoolTypeMixin
    if c_type == rffi.CHAR:       return CharTypeMixin
    if c_type == rffi.UCHAR:      return UCharTypeMixin
    if c_type == rffi.SHORT:      return ShortTypeMixin
    if c_type == rffi.USHORT:     return UShortTypeMixin
    if c_type == rffi.INT:        return IntTypeMixin
    if c_type == rffi.UINT:       return UIntTypeMixin
    if c_type == rffi.LONG:       return LongTypeMixin
    if c_type == rffi.ULONG:      return ULongTypeMixin
    if c_type == rffi.LONGLONG:   return LongLongTypeMixin
    if c_type == rffi.ULONGLONG:  return ULongLongTypeMixin
    if c_type == rffi.FLOAT:      return FloatTypeMixin
    if c_type == rffi.DOUBLE:     return DoubleTypeMixin
    if c_type == rffi.LONGDOUBLE: return LongDoubleTypeMixin

    # should never get here
    raise TypeError("unknown rffi type: %s" % c_type)