Debian Codegraph @

pypy/module/_cppyy/ffitypes.py

raw symbols SCIP
1 from pypy.interpreter.error import oefmt
2 from pypy.interpreter.unicodehelper import utf8_encode_utf_16, utf8_encode_utf_32
3 from pypy.objspace.std.unicodeobject import W_UnicodeObject
4
5 from rpython.rtyper.lltypesystem import rffi, lltype
6 from rpython.rlib.rarithmetic import r_singlefloat, r_longfloat
7 from rpython.rlib.rbigint import rbigint
8
9 from pypy.module._cffi_backend import newtype
10
11 # Mixins to refactor type-specific codef from converter and executor classes
12 # (in converter.py and executor.py, respectively). To get the right mixin, a
13 # non-RPython function typeid() is used.
14
15 class State(object):
16 def __init__(self, space):
17 nt = newtype # module from _cffi_backend
18
19 # the below are (expected to be) lookups, not actual new types, hence
20 # the underlying wrapped primitive class need not be specified
21
22 # builtin types
23 self.c_void = nt.new_void_type(space)
24 self.c_bool = nt.new_primitive_type(space, '_Bool')
25 self.c_char = nt.new_primitive_type(space, 'char')
26 self.c_uchar = nt.new_primitive_type(space, 'unsigned char')
27 self.c_short = nt.new_primitive_type(space, 'short')
28 self.c_ushort = nt.new_primitive_type(space, 'unsigned short')
29 self.c_int = nt.new_primitive_type(space, 'int')
30 self.c_uint = nt.new_primitive_type(space, 'unsigned int')
31 self.c_long = nt.new_primitive_type(space, 'long')
32 self.c_ulong = nt.new_primitive_type(space, 'unsigned long')
33 self.c_llong = nt.new_primitive_type(space, 'long long')
34 self.c_ullong = nt.new_primitive_type(space, 'unsigned long long')
35 self.c_float = nt.new_primitive_type(space, 'float')
36 self.c_double = nt.new_primitive_type(space, 'double')
37 self.c_ldouble = nt.new_primitive_type(space, 'long double')
38
39 # pointer types
40 self.c_ccharp = nt.new_pointer_type(space, self.c_char)
41 self.c_voidp = nt.new_pointer_type(space, self.c_void)
42 self.c_voidpp = nt.new_pointer_type(space, self.c_voidp)
43
44 # special types
45 self.c_int8_t = nt.new_primitive_type(space, 'int8_t')
46 self.c_uint8_t = nt.new_primitive_type(space, 'uint8_t')
47 self.c_size_t = nt.new_primitive_type(space, 'size_t')
48 self.c_ptrdiff_t = nt.new_primitive_type(space, 'ptrdiff_t')
49 self.c_intptr_t = nt.new_primitive_type(space, 'intptr_t')
50 self.c_uintptr_t = nt.new_primitive_type(space, 'uintptr_t')
51 self.c_wchar_t = nt.new_primitive_type(space, 'wchar_t')
52 self.c_char16_t = nt.new_primitive_type(space, 'char16_t')
53 self.c_char32_t = nt.new_primitive_type(space, 'char32_t')
54
55
56 class BoolTypeMixin(object):
57 _mixin_ = True
58 _immutable_fields_ = ['c_type', 'c_ptrtype']
59
60 c_type = rffi.UCHAR
61 c_ptrtype = rffi.UCHARP
62
63 def _wrap_object(self, space, obj):
64 return space.newbool(bool(ord(rffi.cast(rffi.CHAR, obj))))
65
66 def _unwrap_object(self, space, w_obj):
67 arg = space.c_int_w(w_obj)
68 if arg != False and arg != True:
69 raise oefmt(space.w_ValueError,
70 "boolean value should be bool, or integer 1 or 0")
71 return arg
72
73 def cffi_type(self, space):
74 state = space.fromcache(State)
75 return state.c_bool
76
77 class CharTypeMixin(object):
78 _mixin_ = True
79 _immutable_fields_ = ['c_type', 'c_ptrtype']
80
81 c_type = rffi.CHAR
82 c_ptrtype = rffi.CCHARP # there's no such thing as rffi.CHARP
83
84 def _wrap_object(self, space, obj):
85 return space.newbytes(rffi.cast(self.c_type, obj))
86
87 def _unwrap_object(self, space, w_value):
88 # allow int to pass to char and make sure that str is of length 1
89 if space.isinstance_w(w_value, space.w_int):
90 ival = space.c_int_w(w_value)
91 if ival < -128 or 127 < ival:
92 raise oefmt(space.w_ValueError, "char arg not in range(-128,128)")
93
94 value = rffi.cast(rffi.CHAR, space.c_int_w(w_value))
95 else:
96 if space.isinstance_w(w_value, space.w_text):
97 value = space.text_w(w_value)
98 else:
99 value = space.bytes_w(w_value)
100 if len(value) != 1:
101 raise oefmt(space.w_ValueError,
102 "char expected, got string of size %d", len(value))
103
104 value = rffi.cast(rffi.CHAR, value[0])
105 return value
106
107 def cffi_type(self, space):
108 state = space.fromcache(State)
109 return state.c_char
110
111 class SCharTypeMixin(CharTypeMixin):
112 _mixin_ = True
113 _immutable_fields_ = ['c_type', 'c_ptrtype']
114
115 c_type = rffi.SIGNEDCHAR
116 c_ptrtype = rffi.CCHARP # SIGNEDCHARP is not recognized as a char type for str
117
118 def _wrap_object(self, space, obj):
119 return space.newbytes(rffi.cast(rffi.CHAR, rffi.cast(self.c_type, obj)))
120
121 class UCharTypeMixin(object):
122 _mixin_ = True
123 _immutable_fields_ = ['c_type', 'c_ptrtype']
124
125 c_type = rffi.UCHAR
126 c_ptrtype = rffi.CCHARP # UCHARP is not recognized as a char type for str
127
128 def _wrap_object(self, space, obj):
129 return space.newbytes(obj)
130
131 def _unwrap_object(self, space, w_value):
132 # allow int to pass to char and make sure that str is of length 1
133 if space.isinstance_w(w_value, space.w_int):
134 ival = space.c_int_w(w_value)
135 if ival < 0 or 256 <= ival:
136 raise oefmt(space.w_ValueError, "char arg not in range(256)")
137
138 value = rffi.cast(rffi.CHAR, space.c_int_w(w_value))
139 else:
140 if space.isinstance_w(w_value, space.w_text):
141 value = space.text_w(w_value)
142 else:
143 value = space.bytes_w(w_value)
144 if len(value) != 1:
145 raise oefmt(space.w_ValueError,
146 "unsigned char expected, got string of size %d", len(value))
147
148 value = rffi.cast(rffi.CHAR, value[0])
149 return value # turn it into a "char" to the annotator
150
151 def cffi_type(self, space):
152 state = space.fromcache(State)
153 return state.c_char
154
155 class WCharTypeMixin(object):
156 _mixin_ = True
157 _immutable_fields_ = ['c_type', 'c_ptrtype']
158
159 c_type = lltype.UniChar
160 c_ptrtype = rffi.CWCHARP
161
162 def _wrap_object(self, space, obj):
163 result = rffi.cast(self.c_type, obj)
164 u = rffi.cast(lltype.UniChar, result)
165 return W_UnicodeObject(u.encode('utf8'), 1)
166
167 def _unwrap_object(self, space, w_value):
168 utf8, length = space.utf8_len_w(space.unicode_from_object(w_value))
169 if length != 1:
170 raise oefmt(space.w_ValueError,
171 "wchar_t expected, got string of size %d", length)
172
173 with rffi.scoped_utf82wcharp(utf8, length) as u:
174 value = rffi.cast(self.c_type, u[0])
175 return value
176
177 def cffi_type(self, space):
178 state = space.fromcache(State)
179 return state.c_wchar_t
180
181
182 def select_sized_int(size):
183 for t, p in [(rffi.SHORT, rffi.SHORTP), (rffi.INT, rffi.INTP), (rffi.LONG, rffi.LONGP)]:
184 if rffi.sizeof(t) == size:
185 return t, p
186 raise NotImplementedError("no integer type of size %d available" % size)
187
188 CHAR16_T = 'char16_t'
189 class Char16TypeMixin(object):
190 _mixin_ = True
191 _immutable_fields_ = ['c_type', 'c_ptrtype']
192
193 c_type, c_ptrtype = select_sized_int(2)
194
195 def _wrap_object(self, space, obj):
196 result = rffi.cast(self.c_type, obj)
197 u = rffi.cast(lltype.UniChar, result)
198 return W_UnicodeObject(u.encode('utf8'), 1)
199
200 def _unwrap_object(self, space, w_value):
201 utf8, length = space.utf8_len_w(space.unicode_from_object(w_value))
202 if length != 1:
203 raise oefmt(space.w_ValueError,
204 "char16_t expected, got string of size %d", length)
205
206 w_s = space.newtext(utf8)
207 utf16 = utf8_encode_utf_16(space, utf8, w_s, 'strict')
208 rawstr = rffi.str2charp(utf16)
209 value = rffi.cast(self.c_ptrtype, lltype.direct_ptradd(rawstr, 2))[0] # adjust BOM
210 lltype.free(rawstr, flavor='raw')
211 return value
212
213 def cffi_type(self, space):
214 state = space.fromcache(State)
215 return state.c_char16_t
216
217 CHAR32_T = 'char32_t'
218 class Char32TypeMixin(object):
219 _mixin_ = True
220 _immutable_fields_ = ['c_type', 'c_ptrtype']
221
222 c_type, c_ptrtype = select_sized_int(4)
223
224 def _wrap_object(self, space, obj):
225 result = rffi.cast(self.c_type, obj)
226 u = rffi.cast(lltype.UniChar, result)
227 return W_UnicodeObject(u.encode('utf8'), 1)
228
229 def _unwrap_object(self, space, w_value):
230 utf8, length = space.utf8_len_w(space.unicode_from_object(w_value))
231 if length != 1:
232 raise oefmt(space.w_ValueError,
233 "char32_t expected, got string of size %d", length)
234
235 w_s = space.newtext(utf8)
236 utf32 = utf8_encode_utf_32(space, utf8, w_s, 'strict')
237 rawstr = rffi.str2charp(utf32)
238 value = rffi.cast(self.c_ptrtype, lltype.direct_ptradd(rawstr, 4))[0] # adjust BOM
239 lltype.free(rawstr, flavor='raw')
240 return value
241
242 def cffi_type(self, space):
243 state = space.fromcache(State)
244 return state.c_char32_t
245
246
247 class BaseIntTypeMixin(object):
248 _mixin_ = True
249
250 def _wrap_object(self, space, obj):
251 return space.newint(rffi.cast(rffi.INT, rffi.cast(self.c_type, obj)))
252
253 def _unwrap_object(self, space, w_obj):
254 return rffi.cast(self.c_type, space.c_int_w(w_obj))
255
256 INT8_T = 'int8_t'
257 class Int8TypeMixin(BaseIntTypeMixin):
258 _mixin_ = True
259 _immutable_fields_ = ['c_type', 'c_ptrtype']
260
261 c_type = rffi.SIGNEDCHAR
262 c_ptrtype = rffi.SIGNEDCHARP
263
264 def cffi_type(self, space):
265 state = space.fromcache(State)
266 return state.c_int8_t
267
268 UINT8_T = 'uint8_t'
269 class UInt8TypeMixin(BaseIntTypeMixin):
270 _mixin_ = True
271 _immutable_fields_ = ['c_type', 'c_ptrtype']
272
273 c_type = rffi.UCHAR
274 c_ptrtype = rffi.UCHARP
275
276 def cffi_type(self, space):
277 state = space.fromcache(State)
278 return state.c_uint8_t
279
280 class ShortTypeMixin(BaseIntTypeMixin):
281 _mixin_ = True
282 _immutable_fields_ = ['c_type', 'c_ptrtype']
283
284 c_type = rffi.SHORT
285 c_ptrtype = rffi.SHORTP
286
287 def cffi_type(self, space):
288 state = space.fromcache(State)
289 return state.c_short
290
291 class UShortTypeMixin(BaseIntTypeMixin):
292 _mixin_ = True
293 _immutable_fields_ = ['c_type', 'c_ptrtype']
294
295 c_type = rffi.USHORT
296 c_ptrtype = rffi.USHORTP
297
298 def cffi_type(self, space):
299 state = space.fromcache(State)
300 return state.c_ushort
301
302 class IntTypeMixin(BaseIntTypeMixin):
303 _mixin_ = True
304 _immutable_fields_ = ['c_type', 'c_ptrtype']
305
306 c_type = rffi.INT
307 c_ptrtype = rffi.INTP
308
309 def cffi_type(self, space):
310 state = space.fromcache(State)
311 return state.c_int
312
313 class UIntTypeMixin(object):
314 _mixin_ = True
315 _immutable_fields_ = ['c_type', 'c_ptrtype']
316
317 c_type = rffi.UINT
318 c_ptrtype = rffi.UINTP
319
320 def _wrap_object(self, space, obj):
321 return space.newlong_from_rarith_int(obj)
322
323 def _unwrap_object(self, space, w_obj):
324 return rffi.cast(self.c_type, space.uint_w(w_obj))
325
326 def cffi_type(self, space):
327 state = space.fromcache(State)
328 return state.c_uint
329
330 class LongTypeMixin(object):
331 _mixin_ = True
332 _immutable_fields_ = ['c_type', 'c_ptrtype']
333
334 c_type = rffi.LONG
335 c_ptrtype = rffi.LONGP
336
337 def _wrap_object(self, space, obj):
338 return space.newlong(obj)
339
340 def _unwrap_object(self, space, w_obj):
341 return space.int_w(w_obj)
342
343 def cffi_type(self, space):
344 state = space.fromcache(State)
345 return state.c_long
346
347 class ULongTypeMixin(object):
348 _mixin_ = True
349 _immutable_fields_ = ['c_type', 'c_ptrtype']
350
351 c_type = rffi.ULONG
352 c_ptrtype = rffi.ULONGP
353
354 def _wrap_object(self, space, obj):
355 return space.newlong_from_rarith_int(obj)
356
357 def _unwrap_object(self, space, w_obj):
358 return space.uint_w(w_obj)
359
360 def cffi_type(self, space):
361 state = space.fromcache(State)
362 return state.c_ulong
363
364 class LongLongTypeMixin(object):
365 _mixin_ = True
366 _immutable_fields_ = ['c_type', 'c_ptrtype']
367
368 c_type = rffi.LONGLONG
369 c_ptrtype = rffi.LONGLONGP
370
371 def _wrap_object(self, space, obj):
372 return space.newlong_from_rarith_int(obj)
373
374 def _unwrap_object(self, space, w_obj):
375 return space.r_longlong_w(w_obj)
376
377 def cffi_type(self, space):
378 state = space.fromcache(State)
379 return state.c_llong
380
381 class ULongLongTypeMixin(object):
382 _mixin_ = True
383 _immutable_fields_ = ['c_type', 'c_ptrtype']
384
385 c_type = rffi.ULONGLONG
386 c_ptrtype = rffi.ULONGLONGP
387
388 def _wrap_object(self, space, obj):
389 return space.newlong_from_rarith_int(obj)
390
391 def _unwrap_object(self, space, w_obj):
392 return space.r_ulonglong_w(w_obj)
393
394 def cffi_type(self, space):
395 state = space.fromcache(State)
396 return state.c_ullong
397
398 class FloatTypeMixin(object):
399 _mixin_ = True
400 _immutable_fields_ = ['c_type', 'c_ptrtype', 'typecode']
401
402 c_type = rffi.FLOAT
403 c_ptrtype = rffi.FLOATP
404 typecode = 'f'
405
406 def _unwrap_object(self, space, w_obj):
407 return r_singlefloat(space.float_w(w_obj))
408
409 def _wrap_object(self, space, obj):
410 return space.newfloat(float(obj))
411
412 def cffi_type(self, space):
413 state = space.fromcache(State)
414 return state.c_float
415
416 class DoubleTypeMixin(object):
417 _mixin_ = True
418 _immutable_fields_ = ['c_type', 'c_ptrtype', 'typecode']
419
420 c_type = rffi.DOUBLE
421 c_ptrtype = rffi.DOUBLEP
422 typecode = 'd'
423
424 def _wrap_object(self, space, obj):
425 return space.newfloat(obj)
426
427 def _unwrap_object(self, space, w_obj):
428 return space.float_w(w_obj)
429
430 def cffi_type(self, space):
431 state = space.fromcache(State)
432 return state.c_double
433
434 class LongDoubleTypeMixin(object):
435 _mixin_ = True
436 _immutable_fields_ = ['c_type', 'c_ptrtype', 'typecode']
437
438 c_type = rffi.LONGDOUBLE
439 # c_ptrtype = rffi.LONGDOUBLEP # useless type at this point
440 c_ptrtype = rffi.VOIDP
441 typecode = 'g'
442
443 # long double is not really supported ...
444 def _unwrap_object(self, space, w_obj):
445 return r_longfloat(space.float_w(w_obj))
446
447 def _wrap_object(self, space, obj):
448 return space.newfloat(obj)
449
450 def cffi_type(self, space):
451 state = space.fromcache(State)
452 return state.c_ldouble
453
454 def typeid(c_type):
455 "NOT_RPYTHON"
456 if c_type == bool: return BoolTypeMixin
457 if c_type == rffi.CHAR: return CharTypeMixin
458 if c_type == rffi.SIGNEDCHAR: return SCharTypeMixin
459 if c_type == rffi.UCHAR: return UCharTypeMixin
460 if c_type == lltype.UniChar: return WCharTypeMixin # rffi.W_CHAR_T is rffi.INT
461 if c_type == CHAR16_T: return Char16TypeMixin # no type in rffi
462 if c_type == CHAR32_T: return Char32TypeMixin # id.
463 if c_type == INT8_T: return Int8TypeMixin # id.
464 if c_type == UINT8_T: return UInt8TypeMixin # id.
465 if c_type == rffi.SHORT: return ShortTypeMixin
466 if c_type == rffi.USHORT: return UShortTypeMixin
467 if c_type == rffi.INT: return IntTypeMixin
468 if c_type == rffi.UINT: return UIntTypeMixin
469 if c_type == rffi.LONG: return LongTypeMixin
470 if c_type == rffi.ULONG: return ULongTypeMixin
471 if c_type == rffi.LONGLONG: return LongLongTypeMixin
472 if c_type == rffi.ULONGLONG: return ULongLongTypeMixin
473 if c_type == rffi.FLOAT: return FloatTypeMixin
474 if c_type == rffi.DOUBLE: return DoubleTypeMixin
475 if c_type == rffi.LONGDOUBLE: return LongDoubleTypeMixin
476
477 # should never get here
478 raise TypeError("unknown rffi type: %s" % c_type)