| 1 |
import sys |
| 2 |
|
| 3 |
from pypy.interpreter.error import OperationError, oefmt |
| 4 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 5 |
from rpython.rlib.rarithmetic import r_singlefloat, r_longfloat |
| 6 |
from rpython.rlib import rfloat, rawrefcount |
| 7 |
from pypy.module._rawffi.interp_rawffi import letter2tp |
| 8 |
from pypy.module._rawffi.interp_array import W_ArrayInstance |
| 9 |
from pypy.module._cppyy import helper, capi, ffitypes, lowlevelviews |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
def get_rawobject(space, w_obj, can_be_None=True): |
| 22 |
from pypy.module._cppyy.interp_cppyy import W_CPPInstance |
| 23 |
cppinstance = space.interp_w(W_CPPInstance, w_obj, can_be_None=can_be_None) |
| 24 |
if cppinstance: |
| 25 |
rawobject = cppinstance.get_rawobject() |
| 26 |
assert lltype.typeOf(rawobject) == capi.C_OBJECT |
| 27 |
return rawobject |
| 28 |
return capi.C_NULL_OBJECT |
| 29 |
|
| 30 |
def set_rawobject(space, w_obj, address): |
| 31 |
from pypy.module._cppyy.interp_cppyy import W_CPPInstance |
| 32 |
cppinstance = space.interp_w(W_CPPInstance, w_obj, can_be_None=True) |
| 33 |
if cppinstance: |
| 34 |
assert lltype.typeOf(cppinstance._rawobject) == capi.C_OBJECT |
| 35 |
cppinstance._rawobject = rffi.cast(capi.C_OBJECT, address) |
| 36 |
|
| 37 |
def get_rawobject_nonnull(space, w_obj): |
| 38 |
from pypy.module._cppyy.interp_cppyy import W_CPPInstance |
| 39 |
cppinstance = space.interp_w(W_CPPInstance, w_obj, can_be_None=True) |
| 40 |
if cppinstance: |
| 41 |
cppinstance._nullcheck() |
| 42 |
rawobject = cppinstance.get_rawobject() |
| 43 |
assert lltype.typeOf(rawobject) == capi.C_OBJECT |
| 44 |
return rawobject |
| 45 |
return capi.C_NULL_OBJECT |
| 46 |
|
| 47 |
def is_nullpointer_specialcase(space, w_obj): |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
try: |
| 52 |
return space.int_w(w_obj) == 0 |
| 53 |
except Exception: |
| 54 |
pass |
| 55 |
|
| 56 |
from pypy.module._cppyy import interp_cppyy |
| 57 |
return space.is_true(space.is_(w_obj, interp_cppyy.get_nullptr(space))) |
| 58 |
|
| 59 |
def get_rawbuffer(space, w_obj): |
| 60 |
|
| 61 |
try: |
| 62 |
buf = space.getarg_w('s*', w_obj) |
| 63 |
return rffi.cast(rffi.VOIDP, buf.get_raw_address()) |
| 64 |
except Exception: |
| 65 |
pass |
| 66 |
|
| 67 |
try: |
| 68 |
if hasattr(space, "fake"): |
| 69 |
raise NotImplementedError |
| 70 |
arr = space.interp_w(W_ArrayInstance, w_obj, can_be_None=True) |
| 71 |
if arr: |
| 72 |
return rffi.cast(rffi.VOIDP, space.uint_w(arr.getbuffer(space))) |
| 73 |
except Exception: |
| 74 |
pass |
| 75 |
|
| 76 |
if is_nullpointer_specialcase(space, w_obj): |
| 77 |
return rffi.cast(rffi.VOIDP, 0) |
| 78 |
raise TypeError("not an addressable buffer") |
| 79 |
|
| 80 |
|
| 81 |
class TypeConverter(object): |
| 82 |
_immutable_fields_ = ['cffi_name', 'name'] |
| 83 |
|
| 84 |
cffi_name = None |
| 85 |
name = "" |
| 86 |
|
| 87 |
def __init__(self, space, extra): |
| 88 |
pass |
| 89 |
|
| 90 |
def _get_raw_address(self, space, w_obj, offset): |
| 91 |
rawobject = get_rawobject_nonnull(space, w_obj) |
| 92 |
assert lltype.typeOf(rawobject) == capi.C_OBJECT |
| 93 |
if rawobject: |
| 94 |
fieldptr = capi.direct_ptradd(rawobject, offset) |
| 95 |
else: |
| 96 |
fieldptr = rffi.cast(capi.C_OBJECT, offset) |
| 97 |
return fieldptr |
| 98 |
|
| 99 |
def _is_abstract(self, space): |
| 100 |
raise oefmt(space.w_TypeError, |
| 101 |
"no converter available for '%s'", self.name) |
| 102 |
|
| 103 |
def cffi_type(self, space): |
| 104 |
from pypy.module._cppyy.interp_cppyy import FastCallNotPossible |
| 105 |
raise FastCallNotPossible |
| 106 |
|
| 107 |
def convert_argument(self, space, w_obj, address): |
| 108 |
self._is_abstract(space) |
| 109 |
|
| 110 |
def convert_argument_libffi(self, space, w_obj, address, scratch): |
| 111 |
from pypy.module._cppyy.interp_cppyy import FastCallNotPossible |
| 112 |
raise FastCallNotPossible |
| 113 |
|
| 114 |
def default_argument_libffi(self, space, address): |
| 115 |
from pypy.module._cppyy.interp_cppyy import FastCallNotPossible |
| 116 |
raise FastCallNotPossible |
| 117 |
|
| 118 |
def from_memory(self, space, w_obj, offset): |
| 119 |
self._is_abstract(space) |
| 120 |
|
| 121 |
def to_memory(self, space, w_obj, w_value, offset): |
| 122 |
self._is_abstract(space) |
| 123 |
|
| 124 |
def finalize_call(self, space, w_obj): |
| 125 |
pass |
| 126 |
|
| 127 |
def free_argument(self, space, arg): |
| 128 |
pass |
| 129 |
|
| 130 |
|
| 131 |
class ArrayTypeConverterMixin(object): |
| 132 |
_mixin_ = True |
| 133 |
_immutable_fields_ = ['size'] |
| 134 |
|
| 135 |
def __init__(self, space, array_size): |
| 136 |
if array_size <= 0: |
| 137 |
self.size = sys.maxint |
| 138 |
else: |
| 139 |
self.size = array_size |
| 140 |
|
| 141 |
def cffi_type(self, space): |
| 142 |
state = space.fromcache(ffitypes.State) |
| 143 |
return state.c_voidp |
| 144 |
|
| 145 |
def from_memory(self, space, w_obj, offset): |
| 146 |
|
| 147 |
address = self._get_raw_address(space, w_obj, offset) |
| 148 |
ipv = rffi.cast(rffi.UINTPTR_T, address) |
| 149 |
return lowlevelviews.W_LowLevelView( |
| 150 |
space, letter2tp(space, self.typecode), self.size, ipv) |
| 151 |
|
| 152 |
def to_memory(self, space, w_obj, w_value, offset): |
| 153 |
|
| 154 |
address = rffi.cast(rffi.CCHARP, self._get_raw_address(space, w_obj, offset)) |
| 155 |
buf = space.getarg_w('s*', w_value) |
| 156 |
|
| 157 |
for i in range(min(self.size*self.typesize, buf.getlength())): |
| 158 |
address[i] = buf.getitem(i) |
| 159 |
|
| 160 |
class PtrTypeConverterMixin(object): |
| 161 |
_mixin_ = True |
| 162 |
_immutable_fields_ = ['size'] |
| 163 |
|
| 164 |
def __init__(self, space, array_size): |
| 165 |
self.size = sys.maxint |
| 166 |
|
| 167 |
def cffi_type(self, space): |
| 168 |
state = space.fromcache(ffitypes.State) |
| 169 |
return state.c_voidp |
| 170 |
|
| 171 |
def convert_argument(self, space, w_obj, address): |
| 172 |
w_tc = space.findattr(w_obj, space.newtext('typecode')) |
| 173 |
if w_tc is not None and space.text_w(w_tc) != self.typecode: |
| 174 |
raise oefmt(space.w_TypeError, |
| 175 |
"expected %s pointer type, but received %s", |
| 176 |
self.typecode, space.text_w(w_tc)) |
| 177 |
x = rffi.cast(rffi.VOIDPP, address) |
| 178 |
try: |
| 179 |
x[0] = rffi.cast(rffi.VOIDP, get_rawbuffer(space, w_obj)) |
| 180 |
except TypeError: |
| 181 |
raise oefmt(space.w_TypeError, |
| 182 |
"raw buffer interface not supported") |
| 183 |
ba = rffi.cast(rffi.CCHARP, address) |
| 184 |
ba[capi.c_function_arg_typeoffset(space)] = 'o' |
| 185 |
|
| 186 |
def from_memory(self, space, w_obj, offset): |
| 187 |
|
| 188 |
address = self._get_raw_address(space, w_obj, offset) |
| 189 |
ipv = rffi.cast(rffi.UINTPTR_T, rffi.cast(rffi.VOIDPP, address)[0]) |
| 190 |
return lowlevelviews.W_LowLevelView( |
| 191 |
space, letter2tp(space, self.typecode), self.size, ipv) |
| 192 |
|
| 193 |
def to_memory(self, space, w_obj, w_value, offset): |
| 194 |
|
| 195 |
rawobject = get_rawobject_nonnull(space, w_obj) |
| 196 |
byteptr = rffi.cast(rffi.VOIDPP, capi.direct_ptradd(rawobject, offset)) |
| 197 |
buf = space.getarg_w('s*', w_value) |
| 198 |
try: |
| 199 |
byteptr[0] = rffi.cast(rffi.VOIDP, buf.get_raw_address()) |
| 200 |
except ValueError: |
| 201 |
raise oefmt(space.w_TypeError, |
| 202 |
"raw buffer interface not supported") |
| 203 |
|
| 204 |
class ArrayPtrTypeConverterMixin(PtrTypeConverterMixin): |
| 205 |
_mixin_ = True |
| 206 |
|
| 207 |
def cffi_type(self, space): |
| 208 |
state = space.fromcache(ffitypes.State) |
| 209 |
return state.c_voidpp |
| 210 |
|
| 211 |
|
| 212 |
class NumericTypeConverterMixin(object): |
| 213 |
_mixin_ = True |
| 214 |
|
| 215 |
def convert_argument_libffi(self, space, w_obj, address, scratch): |
| 216 |
x = rffi.cast(self.c_ptrtype, address) |
| 217 |
x[0] = self._unwrap_object(space, w_obj) |
| 218 |
|
| 219 |
def default_argument_libffi(self, space, address): |
| 220 |
if not self.valid_default: |
| 221 |
from pypy.module._cppyy.interp_cppyy import FastCallNotPossible |
| 222 |
raise FastCallNotPossible |
| 223 |
x = rffi.cast(self.c_ptrtype, address) |
| 224 |
x[0] = self.default |
| 225 |
|
| 226 |
def from_memory(self, space, w_obj, offset): |
| 227 |
address = self._get_raw_address(space, w_obj, offset) |
| 228 |
rffiptr = rffi.cast(self.c_ptrtype, address) |
| 229 |
return self._wrap_object(space, rffiptr[0]) |
| 230 |
|
| 231 |
def to_memory(self, space, w_obj, w_value, offset): |
| 232 |
address = self._get_raw_address(space, w_obj, offset) |
| 233 |
rffiptr = rffi.cast(self.c_ptrtype, address) |
| 234 |
rffiptr[0] = self._unwrap_object(space, w_value) |
| 235 |
|
| 236 |
class ConstRefNumericTypeConverterMixin(object): |
| 237 |
_mixin_ = True |
| 238 |
|
| 239 |
def cffi_type(self, space): |
| 240 |
state = space.fromcache(ffitypes.State) |
| 241 |
return state.c_voidp |
| 242 |
|
| 243 |
def convert_argument_libffi(self, space, w_obj, address, scratch): |
| 244 |
obj = self._unwrap_object(space, w_obj) |
| 245 |
typed_buf = rffi.cast(self.c_ptrtype, scratch) |
| 246 |
typed_buf[0] = obj |
| 247 |
x = rffi.cast(rffi.VOIDPP, address) |
| 248 |
x[0] = scratch |
| 249 |
|
| 250 |
|
| 251 |
class IntTypeConverterMixin(NumericTypeConverterMixin): |
| 252 |
_mixin_ = True |
| 253 |
|
| 254 |
def convert_argument(self, space, w_obj, address): |
| 255 |
x = rffi.cast(self.c_ptrtype, address) |
| 256 |
x[0] = self._unwrap_object(space, w_obj) |
| 257 |
ba = rffi.cast(rffi.CCHARP, address) |
| 258 |
ba[capi.c_function_arg_typeoffset(space)] = self.typecode |
| 259 |
|
| 260 |
class FloatTypeConverterMixin(NumericTypeConverterMixin): |
| 261 |
_mixin_ = True |
| 262 |
|
| 263 |
def convert_argument(self, space, w_obj, address): |
| 264 |
x = rffi.cast(self.c_ptrtype, address) |
| 265 |
x[0] = self._unwrap_object(space, w_obj) |
| 266 |
ba = rffi.cast(rffi.CCHARP, address) |
| 267 |
ba[capi.c_function_arg_typeoffset(space)] = self.typecode |
| 268 |
|
| 269 |
|
| 270 |
class VoidConverter(TypeConverter): |
| 271 |
_immutable_fields_ = ['name'] |
| 272 |
|
| 273 |
def __init__(self, space, name): |
| 274 |
self.name = name |
| 275 |
|
| 276 |
def cffi_type(self, space): |
| 277 |
state = space.fromcache(ffitypes.State) |
| 278 |
return state.c_void |
| 279 |
|
| 280 |
def convert_argument(self, space, w_obj, address): |
| 281 |
self._is_abstract(space) |
| 282 |
|
| 283 |
|
| 284 |
class BoolConverter(ffitypes.typeid(bool), TypeConverter): |
| 285 |
def convert_argument(self, space, w_obj, address): |
| 286 |
x = rffi.cast(rffi.LONGP, address) |
| 287 |
x[0] = self._unwrap_object(space, w_obj) |
| 288 |
ba = rffi.cast(rffi.CCHARP, address) |
| 289 |
ba[capi.c_function_arg_typeoffset(space)] = 'b' |
| 290 |
|
| 291 |
def convert_argument_libffi(self, space, w_obj, address, scratch): |
| 292 |
x = rffi.cast(rffi.LONGP, address) |
| 293 |
x[0] = self._unwrap_object(space, w_obj) |
| 294 |
|
| 295 |
def from_memory(self, space, w_obj, offset): |
| 296 |
address = rffi.cast(rffi.CCHARP, self._get_raw_address(space, w_obj, offset)) |
| 297 |
if address[0] == '\x01': |
| 298 |
return space.w_True |
| 299 |
return space.w_False |
| 300 |
|
| 301 |
def to_memory(self, space, w_obj, w_value, offset): |
| 302 |
address = rffi.cast(rffi.CCHARP, self._get_raw_address(space, w_obj, offset)) |
| 303 |
arg = self._unwrap_object(space, w_value) |
| 304 |
if arg: |
| 305 |
address[0] = '\x01' |
| 306 |
else: |
| 307 |
address[0] = '\x00' |
| 308 |
|
| 309 |
|
| 310 |
class CharTypeConverterMixin(object): |
| 311 |
_mixin_ = True |
| 312 |
|
| 313 |
def convert_argument(self, space, w_obj, address): |
| 314 |
x = rffi.cast(self.c_ptrtype, address) |
| 315 |
x[0] = self._unwrap_object(space, w_obj) |
| 316 |
ba = rffi.cast(rffi.CCHARP, address) |
| 317 |
ba[capi.c_function_arg_typeoffset(space)] = 'b' |
| 318 |
|
| 319 |
def convert_argument_libffi(self, space, w_obj, address, scratch): |
| 320 |
x = rffi.cast(self.c_ptrtype, address) |
| 321 |
x[0] = self._unwrap_object(space, w_obj) |
| 322 |
|
| 323 |
def from_memory(self, space, w_obj, offset): |
| 324 |
address = rffi.cast(self.c_ptrtype, self._get_raw_address(space, w_obj, offset)) |
| 325 |
return self._wrap_object(space, address[0]) |
| 326 |
|
| 327 |
def to_memory(self, space, w_obj, w_value, offset): |
| 328 |
address = rffi.cast(self.c_ptrtype, self._get_raw_address(space, w_obj, offset)) |
| 329 |
address[0] = self._unwrap_object(space, w_value) |
| 330 |
|
| 331 |
|
| 332 |
class FloatConverter(ffitypes.typeid(rffi.FLOAT), FloatTypeConverterMixin, TypeConverter): |
| 333 |
_immutable_fields_ = ['default', 'valid_default'] |
| 334 |
|
| 335 |
def __init__(self, space, default): |
| 336 |
self.valid_default = False |
| 337 |
try: |
| 338 |
fval = float(rfloat.rstring_to_float(default)) |
| 339 |
self.valid_default = True |
| 340 |
except Exception: |
| 341 |
fval = float(0.) |
| 342 |
self.default = rffi.cast(rffi.FLOAT, r_singlefloat(fval)) |
| 343 |
|
| 344 |
def from_memory(self, space, w_obj, offset): |
| 345 |
address = self._get_raw_address(space, w_obj, offset) |
| 346 |
rffiptr = rffi.cast(self.c_ptrtype, address) |
| 347 |
return self._wrap_object(space, rffiptr[0]) |
| 348 |
|
| 349 |
class ConstFloatRefConverter(ConstRefNumericTypeConverterMixin, FloatConverter): |
| 350 |
_immutable_fields_ = ['typecode'] |
| 351 |
typecode = 'f' |
| 352 |
|
| 353 |
class DoubleConverter(ffitypes.typeid(rffi.DOUBLE), FloatTypeConverterMixin, TypeConverter): |
| 354 |
_immutable_fields_ = ['default', 'valid_default'] |
| 355 |
|
| 356 |
def __init__(self, space, default): |
| 357 |
self.valid_default = False |
| 358 |
try: |
| 359 |
self.default = rffi.cast(self.c_type, rfloat.rstring_to_float(default)) |
| 360 |
self.valid_default = True |
| 361 |
except Exception: |
| 362 |
self.default = rffi.cast(self.c_type, 0.) |
| 363 |
|
| 364 |
class ConstDoubleRefConverter(ConstRefNumericTypeConverterMixin, DoubleConverter): |
| 365 |
_immutable_fields_ = ['typecode'] |
| 366 |
typecode = 'd' |
| 367 |
|
| 368 |
class LongDoubleConverter(TypeConverter): |
| 369 |
_immutable_fields_ = ['default', 'valid_default'] |
| 370 |
typecode = 'g' |
| 371 |
|
| 372 |
def __init__(self, space, default): |
| 373 |
self.valid_default = False |
| 374 |
try: |
| 375 |
|
| 376 |
fval = rffi.cast(rffi.DOUBLE, rfloat.rstring_to_float(default)) |
| 377 |
self.valid_default = True |
| 378 |
except Exception: |
| 379 |
fval = rffi.cast(rffi.DOUBLE, 0.) |
| 380 |
|
| 381 |
self.default = fval |
| 382 |
|
| 383 |
def convert_argument(self, space, w_obj, address): |
| 384 |
x = rffi.cast(rffi.VOIDP, address) |
| 385 |
capi.c_double2longdouble(space, space.float_w(w_obj), x) |
| 386 |
ba = rffi.cast(rffi.CCHARP, address) |
| 387 |
ba[capi.c_function_arg_typeoffset(space)] = self.typecode |
| 388 |
|
| 389 |
def convert_argument_libffi(self, space, w_obj, address, scratch): |
| 390 |
x = rffi.cast(rffi.VOIDP, address) |
| 391 |
capi.c_double2longdouble(space, space.float_w(w_obj), x) |
| 392 |
|
| 393 |
def default_argument_libffi(self, space, address): |
| 394 |
if not self.valid_default: |
| 395 |
from pypy.module._cppyy.interp_cppyy import FastCallNotPossible |
| 396 |
raise FastCallNotPossible |
| 397 |
x = rffi.cast(rffi.VOIDP, address) |
| 398 |
capi.c_double2longdouble(space, self.default, x) |
| 399 |
|
| 400 |
def from_memory(self, space, w_obj, offset): |
| 401 |
address = self._get_raw_address(space, w_obj, offset) |
| 402 |
rffiptr = rffi.cast(rffi.VOIDP, address) |
| 403 |
return space.newfloat(capi.c_longdouble2double(space, rffiptr)) |
| 404 |
|
| 405 |
def to_memory(self, space, w_obj, w_value, offset): |
| 406 |
address = self._get_raw_address(space, w_obj, offset) |
| 407 |
rffiptr = rffi.cast(rffi.VOIDP, address) |
| 408 |
capi.c_double2longdouble(space, space.float_w(w_value), rffiptr) |
| 409 |
|
| 410 |
class ConstLongDoubleRefConverter(ConstRefNumericTypeConverterMixin, LongDoubleConverter): |
| 411 |
_immutable_fields_ = ['typecode'] |
| 412 |
typecode = 'g' |
| 413 |
|
| 414 |
def convert_argument_libffi(self, space, w_obj, address, scratch): |
| 415 |
capi.c_double2longdouble(space, space.float_w(w_obj), rffi.cast(rffi.VOIDP, scratch)) |
| 416 |
x = rffi.cast(rffi.VOIDPP, address) |
| 417 |
x[0] = scratch |
| 418 |
|
| 419 |
|
| 420 |
class CStringConverter(TypeConverter): |
| 421 |
def convert_argument(self, space, w_obj, address): |
| 422 |
x = rffi.cast(rffi.VOIDPP, address) |
| 423 |
arg = space.text_w(w_obj) |
| 424 |
x[0] = rffi.cast(rffi.VOIDP, rffi.str2charp(arg)) |
| 425 |
ba = rffi.cast(rffi.CCHARP, address) |
| 426 |
ba[capi.c_function_arg_typeoffset(space)] = 'p' |
| 427 |
|
| 428 |
def from_memory(self, space, w_obj, offset): |
| 429 |
address = self._get_raw_address(space, w_obj, offset) |
| 430 |
charpptr = rffi.cast(rffi.CCHARPP, address) |
| 431 |
return space.newtext(rffi.charp2str(charpptr[0])) |
| 432 |
|
| 433 |
def free_argument(self, space, arg): |
| 434 |
lltype.free(rffi.cast(rffi.CCHARPP, arg)[0], flavor='raw') |
| 435 |
|
| 436 |
class CStringConverterWithSize(CStringConverter): |
| 437 |
_immutable_fields_ = ['size'] |
| 438 |
|
| 439 |
def __init__(self, space, extra): |
| 440 |
self.size = extra |
| 441 |
|
| 442 |
def from_memory(self, space, w_obj, offset): |
| 443 |
address = self._get_raw_address(space, w_obj, offset) |
| 444 |
charpptr = rffi.cast(rffi.CCHARP, address) |
| 445 |
if 0 <= self.size and self.size != 2**31-1: |
| 446 |
strsize = self.size |
| 447 |
if charpptr[self.size-1] == '\0': |
| 448 |
strsize = self.size-1 |
| 449 |
return space.newtext(rffi.charpsize2str(charpptr, strsize)) |
| 450 |
return space.newtext(rffi.charp2str(charpptr)) |
| 451 |
|
| 452 |
|
| 453 |
class VoidPtrConverter(TypeConverter): |
| 454 |
def _unwrap_object(self, space, w_obj): |
| 455 |
try: |
| 456 |
obj = get_rawbuffer(space, w_obj) |
| 457 |
except TypeError: |
| 458 |
obj = rffi.cast(rffi.VOIDP, get_rawobject(space, w_obj, False)) |
| 459 |
return obj |
| 460 |
|
| 461 |
def cffi_type(self, space): |
| 462 |
state = space.fromcache(ffitypes.State) |
| 463 |
return state.c_voidp |
| 464 |
|
| 465 |
def convert_argument(self, space, w_obj, address): |
| 466 |
x = rffi.cast(rffi.VOIDPP, address) |
| 467 |
x[0] = self._unwrap_object(space, w_obj) |
| 468 |
ba = rffi.cast(rffi.CCHARP, address) |
| 469 |
ba[capi.c_function_arg_typeoffset(space)] = 'o' |
| 470 |
|
| 471 |
def convert_argument_libffi(self, space, w_obj, address, scratch): |
| 472 |
x = rffi.cast(rffi.VOIDPP, address) |
| 473 |
x[0] = self._unwrap_object(space, w_obj) |
| 474 |
|
| 475 |
def from_memory(self, space, w_obj, offset): |
| 476 |
|
| 477 |
|
| 478 |
address = self._get_raw_address(space, w_obj, offset) |
| 479 |
ipv = rffi.cast(rffi.UINTPTR_T, rffi.cast(rffi.VOIDPP, address)[0]) |
| 480 |
if ipv == rffi.cast(rffi.UINTPTR_T, 0): |
| 481 |
from pypy.module._cppyy import interp_cppyy |
| 482 |
return interp_cppyy.get_nullptr(space) |
| 483 |
shape = letter2tp(space, 'P') |
| 484 |
return lowlevelviews.W_LowLevelView(space, shape, sys.maxint/shape.size, ipv) |
| 485 |
|
| 486 |
def to_memory(self, space, w_obj, w_value, offset): |
| 487 |
address = rffi.cast(rffi.VOIDPP, self._get_raw_address(space, w_obj, offset)) |
| 488 |
if is_nullpointer_specialcase(space, w_value): |
| 489 |
address[0] = rffi.cast(rffi.VOIDP, 0) |
| 490 |
else: |
| 491 |
address[0] = rffi.cast(rffi.VOIDP, self._unwrap_object(space, w_value)) |
| 492 |
|
| 493 |
class VoidPtrPtrConverter(TypeConverter): |
| 494 |
typecode = 'p' |
| 495 |
|
| 496 |
def __init__(self, space, extra): |
| 497 |
self.ref_buffer = lltype.nullptr(rffi.VOIDPP.TO) |
| 498 |
|
| 499 |
def convert_argument(self, space, w_obj, address): |
| 500 |
x = rffi.cast(rffi.VOIDPP, address) |
| 501 |
try: |
| 502 |
x[0] = get_rawbuffer(space, w_obj) |
| 503 |
except TypeError: |
| 504 |
ptr = rffi.cast(rffi.VOIDP, get_rawobject(space, w_obj)) |
| 505 |
self.ref_buffer = lltype.malloc(rffi.VOIDPP.TO, 1, flavor='raw') |
| 506 |
self.ref_buffer[0] = ptr |
| 507 |
x[0] = self.ref_buffer |
| 508 |
ba = rffi.cast(rffi.CCHARP, address) |
| 509 |
ba[capi.c_function_arg_typeoffset(space)] = self.typecode |
| 510 |
|
| 511 |
def finalize_call(self, space, w_obj): |
| 512 |
if self.ref_buffer: |
| 513 |
set_rawobject(space, w_obj, self.ref_buffer[0]) |
| 514 |
|
| 515 |
def free_argument(self, space, arg): |
| 516 |
if self.ref_buffer: |
| 517 |
lltype.free(self.ref_buffer, flavor='raw') |
| 518 |
self.ref_buffer = lltype.nullptr(rffi.VOIDPP.TO) |
| 519 |
|
| 520 |
class VoidPtrRefConverter(VoidPtrPtrConverter): |
| 521 |
_immutable_fields_ = ['typecode'] |
| 522 |
typecode = 'V' |
| 523 |
|
| 524 |
class InstanceRefConverter(TypeConverter): |
| 525 |
_immutable_fields_ = ['typecode', 'clsdecl'] |
| 526 |
typecode = 'V' |
| 527 |
|
| 528 |
def __init__(self, space, clsdecl): |
| 529 |
from pypy.module._cppyy.interp_cppyy import W_CPPClassDecl |
| 530 |
assert isinstance(clsdecl, W_CPPClassDecl) |
| 531 |
self.clsdecl = clsdecl |
| 532 |
|
| 533 |
def _unwrap_object(self, space, w_obj): |
| 534 |
from pypy.module._cppyy.interp_cppyy import W_CPPInstance |
| 535 |
if isinstance(w_obj, W_CPPInstance): |
| 536 |
from pypy.module._cppyy.interp_cppyy import INSTANCE_FLAGS_IS_RVALUE |
| 537 |
if w_obj.rt_flags & INSTANCE_FLAGS_IS_RVALUE: |
| 538 |
|
| 539 |
raise ValueError("lvalue expected") |
| 540 |
if capi.c_is_subtype(space, w_obj.clsdecl, self.clsdecl): |
| 541 |
rawobject = w_obj.get_rawobject() |
| 542 |
offset = capi.c_base_offset(space, w_obj.clsdecl, self.clsdecl, rawobject, 1) |
| 543 |
obj_address = capi.direct_ptradd(rawobject, offset) |
| 544 |
return rffi.cast(capi.C_OBJECT, obj_address) |
| 545 |
raise oefmt(space.w_TypeError, |
| 546 |
"cannot pass %T instance as %s", w_obj, self.clsdecl.name) |
| 547 |
|
| 548 |
def cffi_type(self, space): |
| 549 |
state = space.fromcache(ffitypes.State) |
| 550 |
return state.c_voidp |
| 551 |
|
| 552 |
def convert_argument(self, space, w_obj, address): |
| 553 |
x = rffi.cast(rffi.VOIDPP, address) |
| 554 |
x[0] = rffi.cast(rffi.VOIDP, self._unwrap_object(space, w_obj)) |
| 555 |
address = rffi.cast(capi.C_OBJECT, address) |
| 556 |
ba = rffi.cast(rffi.CCHARP, address) |
| 557 |
ba[capi.c_function_arg_typeoffset(space)] = self.typecode |
| 558 |
|
| 559 |
def convert_argument_libffi(self, space, w_obj, address, scratch): |
| 560 |
x = rffi.cast(rffi.VOIDPP, address) |
| 561 |
x[0] = rffi.cast(rffi.VOIDP, self._unwrap_object(space, w_obj)) |
| 562 |
|
| 563 |
class InstanceMoveConverter(InstanceRefConverter): |
| 564 |
def _unwrap_object(self, space, w_obj): |
| 565 |
|
| 566 |
from pypy.module._cppyy.interp_cppyy import W_CPPInstance, INSTANCE_FLAGS_IS_RVALUE |
| 567 |
obj = space.interp_w(W_CPPInstance, w_obj) |
| 568 |
if obj: |
| 569 |
if obj.rt_flags & INSTANCE_FLAGS_IS_RVALUE: |
| 570 |
obj.rt_flags &= ~INSTANCE_FLAGS_IS_RVALUE |
| 571 |
try: |
| 572 |
return InstanceRefConverter._unwrap_object(self, space, w_obj) |
| 573 |
except Exception: |
| 574 |
|
| 575 |
|
| 576 |
obj.rt_flags |= INSTANCE_FLAGS_IS_RVALUE |
| 577 |
raise |
| 578 |
raise oefmt(space.w_ValueError, "object is not an rvalue") |
| 579 |
|
| 580 |
|
| 581 |
class InstanceConverter(InstanceRefConverter): |
| 582 |
|
| 583 |
def convert_argument_libffi(self, space, w_obj, address, scratch): |
| 584 |
from pypy.module._cppyy.interp_cppyy import FastCallNotPossible |
| 585 |
raise FastCallNotPossible |
| 586 |
|
| 587 |
def from_memory(self, space, w_obj, offset): |
| 588 |
address = rffi.cast(capi.C_OBJECT, self._get_raw_address(space, w_obj, offset)) |
| 589 |
from pypy.module._cppyy import interp_cppyy |
| 590 |
return interp_cppyy.wrap_cppinstance(space, address, self.clsdecl, do_cast=False) |
| 591 |
|
| 592 |
def to_memory(self, space, w_obj, w_value, offset): |
| 593 |
address = rffi.cast(capi.C_OBJECT, self._get_raw_address(space, w_obj, offset)) |
| 594 |
assign = self.clsdecl.get_overload("__assign__") |
| 595 |
from pypy.module._cppyy import interp_cppyy |
| 596 |
assign.call_impl(address, [w_value]) |
| 597 |
|
| 598 |
class InstancePtrConverter(InstanceRefConverter): |
| 599 |
typecode = 'o' |
| 600 |
|
| 601 |
def _unwrap_object(self, space, w_obj): |
| 602 |
try: |
| 603 |
return InstanceRefConverter._unwrap_object(self, space, w_obj) |
| 604 |
except OperationError as e: |
| 605 |
|
| 606 |
if is_nullpointer_specialcase(space, w_obj): |
| 607 |
return capi.C_NULL_OBJECT |
| 608 |
raise e |
| 609 |
|
| 610 |
def from_memory(self, space, w_obj, offset): |
| 611 |
address = rffi.cast(capi.C_OBJECT, self._get_raw_address(space, w_obj, offset)) |
| 612 |
from pypy.module._cppyy import interp_cppyy |
| 613 |
return interp_cppyy.wrap_cppinstance( |
| 614 |
space, address, self.clsdecl, do_cast=False, is_ref=True) |
| 615 |
|
| 616 |
def to_memory(self, space, w_obj, w_value, offset): |
| 617 |
from pypy.module._cppyy.interp_cppyy import W_CPPInstance |
| 618 |
cppinstance = space.interp_w(W_CPPInstance, w_value, can_be_None=True) |
| 619 |
if cppinstance: |
| 620 |
|
| 621 |
rawobject = cppinstance.get_rawobject() |
| 622 |
base_offset = capi.c_base_offset(space, cppinstance.clsdecl, self.clsdecl, rawobject, 1) |
| 623 |
rawptr = capi.direct_ptradd(rawobject, base_offset) |
| 624 |
|
| 625 |
|
| 626 |
address = rffi.cast(rffi.VOIDPP, self._get_raw_address(space, w_obj, offset)) |
| 627 |
address[0] = rffi.cast(rffi.VOIDP, rawptr) |
| 628 |
|
| 629 |
|
| 630 |
from pypy.module._cppyy.interp_cppyy import memory_regulator |
| 631 |
memory_regulator.register(cppinstance) |
| 632 |
else: |
| 633 |
raise oefmt(space.w_TypeError, |
| 634 |
"cannot pass %T instance as %s", w_value, self.clsdecl.name) |
| 635 |
|
| 636 |
class InstancePtrPtrConverter(InstancePtrConverter): |
| 637 |
typecode = 'o' |
| 638 |
|
| 639 |
def __init__(self, space, extra): |
| 640 |
InstancePtrConverter.__init__(self, space, extra) |
| 641 |
self.ref_buffer = lltype.nullptr(rffi.VOIDPP.TO) |
| 642 |
|
| 643 |
def convert_argument(self, space, w_obj, address): |
| 644 |
x = rffi.cast(rffi.VOIDPP, address) |
| 645 |
ptr = rffi.cast(rffi.VOIDP, self._unwrap_object(space, w_obj)) |
| 646 |
self.ref_buffer = lltype.malloc(rffi.VOIDPP.TO, 1, flavor='raw') |
| 647 |
self.ref_buffer[0] = ptr |
| 648 |
x[0] = self.ref_buffer |
| 649 |
ba = rffi.cast(rffi.CCHARP, address) |
| 650 |
ba[capi.c_function_arg_typeoffset(space)] = self.typecode |
| 651 |
|
| 652 |
def convert_argument_libffi(self, space, w_obj, address, scratch): |
| 653 |
|
| 654 |
from pypy.module._cppyy.interp_cppyy import FastCallNotPossible |
| 655 |
raise FastCallNotPossible |
| 656 |
|
| 657 |
def from_memory(self, space, w_obj, offset): |
| 658 |
self._is_abstract(space) |
| 659 |
|
| 660 |
def to_memory(self, space, w_obj, w_value, offset): |
| 661 |
self._is_abstract(space) |
| 662 |
|
| 663 |
def finalize_call(self, space, w_obj): |
| 664 |
if self.ref_buffer: |
| 665 |
set_rawobject(space, w_obj, self.ref_buffer[0]) |
| 666 |
|
| 667 |
def free_argument(self, space, arg): |
| 668 |
if self.ref_buffer: |
| 669 |
lltype.free(self.ref_buffer, flavor='raw') |
| 670 |
self.ref_buffer = lltype.nullptr(rffi.VOIDPP.TO) |
| 671 |
|
| 672 |
class InstanceArrayConverter(InstancePtrConverter): |
| 673 |
_immutable_fields_ = ['size'] |
| 674 |
|
| 675 |
def __init__(self, space, clsdecl, array_size, dimensions): |
| 676 |
InstancePtrConverter.__init__(self, space, clsdecl) |
| 677 |
if array_size <= 0 or array_size == 2**31-1: |
| 678 |
self.size = sys.maxint |
| 679 |
else: |
| 680 |
self.size = array_size |
| 681 |
|
| 682 |
self.dimensions = dimensions[1:] |
| 683 |
|
| 684 |
def from_memory(self, space, w_obj, offset): |
| 685 |
address = rffi.cast(capi.C_OBJECT, self._get_raw_address(space, w_obj, offset)) |
| 686 |
return lowlevelviews.W_ArrayOfInstances(space, self.clsdecl, address, self.size, self.dimensions) |
| 687 |
|
| 688 |
def to_memory(self, space, w_obj, w_value, offset): |
| 689 |
self._is_abstract(space) |
| 690 |
|
| 691 |
|
| 692 |
class STLStringConverter(InstanceConverter): |
| 693 |
def __init__(self, space, extra): |
| 694 |
from pypy.module._cppyy import interp_cppyy |
| 695 |
cppclass = interp_cppyy.scope_byname(space, capi.std_string_name) |
| 696 |
InstanceConverter.__init__(self, space, cppclass) |
| 697 |
|
| 698 |
def _unwrap_object(self, space, w_obj): |
| 699 |
from pypy.module._cppyy.interp_cppyy import W_CPPInstance |
| 700 |
if isinstance(w_obj, W_CPPInstance): |
| 701 |
arg = InstanceConverter._unwrap_object(self, space, w_obj) |
| 702 |
return capi.c_stdstring2stdstring(space, arg) |
| 703 |
return capi.c_charp2stdstring(space, space.text_w(w_obj), space.len_w(w_obj)) |
| 704 |
|
| 705 |
def free_argument(self, space, arg): |
| 706 |
capi.c_destruct(space, self.clsdecl, rffi.cast(capi.C_OBJECT, rffi.cast(rffi.VOIDPP, arg)[0])) |
| 707 |
|
| 708 |
class STLStringMoveConverter(STLStringConverter): |
| 709 |
def _unwrap_object(self, space, w_obj): |
| 710 |
|
| 711 |
moveit_reason = 3 |
| 712 |
from pypy.module._cppyy.interp_cppyy import W_CPPInstance, INSTANCE_FLAGS_IS_RVALUE |
| 713 |
try: |
| 714 |
obj = space.interp_w(W_CPPInstance, w_obj) |
| 715 |
if obj and obj.rt_flags & INSTANCE_FLAGS_IS_RVALUE: |
| 716 |
obj.rt_flags &= ~INSTANCE_FLAGS_IS_RVALUE |
| 717 |
moveit_reason = 1 |
| 718 |
else: |
| 719 |
moveit_reason = 0 |
| 720 |
except: |
| 721 |
pass |
| 722 |
|
| 723 |
if moveit_reason: |
| 724 |
try: |
| 725 |
return STLStringConverter._unwrap_object(self, space, w_obj) |
| 726 |
except Exception: |
| 727 |
if moveit_reason == 1: |
| 728 |
|
| 729 |
|
| 730 |
obj = space.interp_w(W_CPPInstance, w_obj) |
| 731 |
obj.rt_flags |= INSTANCE_FLAGS_IS_RVALUE |
| 732 |
raise |
| 733 |
|
| 734 |
raise oefmt(space.w_ValueError, "object is not an rvalue") |
| 735 |
|
| 736 |
class STLStringRefConverter(InstancePtrConverter): |
| 737 |
_immutable_fields_ = ['cppclass', 'typecode'] |
| 738 |
typecode = 'V' |
| 739 |
|
| 740 |
def __init__(self, space, extra): |
| 741 |
from pypy.module._cppyy import interp_cppyy |
| 742 |
cppclass = interp_cppyy.scope_byname(space, capi.std_string_name) |
| 743 |
InstancePtrConverter.__init__(self, space, cppclass) |
| 744 |
|
| 745 |
|
| 746 |
class PyObjectConverter(TypeConverter): |
| 747 |
def cffi_type(self, space): |
| 748 |
state = space.fromcache(ffitypes.State) |
| 749 |
return state.c_voidp |
| 750 |
|
| 751 |
def convert_argument(self, space, w_obj, address): |
| 752 |
if hasattr(space, "fake"): |
| 753 |
raise NotImplementedError |
| 754 |
space.getbuiltinmodule("cpyext") |
| 755 |
from pypy.module.cpyext.pyobject import make_ref |
| 756 |
ref = make_ref(space, w_obj) |
| 757 |
x = rffi.cast(rffi.VOIDPP, address) |
| 758 |
x[0] = rffi.cast(rffi.VOIDP, ref) |
| 759 |
ba = rffi.cast(rffi.CCHARP, address) |
| 760 |
ba[capi.c_function_arg_typeoffset(space)] = 'a' |
| 761 |
|
| 762 |
def convert_argument_libffi(self, space, w_obj, address, scratch): |
| 763 |
|
| 764 |
from pypy.module._cppyy.interp_cppyy import FastCallNotPossible |
| 765 |
raise FastCallNotPossible |
| 766 |
|
| 767 |
|
| 768 |
"""if hasattr(space, "fake"): |
| 769 |
raise NotImplementedError |
| 770 |
space.getbuiltinmodule("cpyext") |
| 771 |
from pypy.module.cpyext.pyobject import make_ref |
| 772 |
ref = make_ref(space, w_obj) |
| 773 |
x = rffi.cast(rffi.VOIDPP, address) |
| 774 |
x[0] = rffi.cast(rffi.VOIDP, ref)""" |
| 775 |
|
| 776 |
def free_argument(self, space, arg): |
| 777 |
if hasattr(space, "fake"): |
| 778 |
raise NotImplementedError |
| 779 |
space.getbuiltinmodule("cpyext") |
| 780 |
from pypy.module.cpyext.pyobject import decref, PyObject |
| 781 |
decref(space, rffi.cast(PyObject, rffi.cast(rffi.VOIDPP, arg)[0])) |
| 782 |
|
| 783 |
|
| 784 |
class FunctionPointerConverter(TypeConverter): |
| 785 |
_immutable_fields_ = ['signature'] |
| 786 |
|
| 787 |
def __init__(self, space, signature): |
| 788 |
self.signature = signature |
| 789 |
|
| 790 |
def convert_argument(self, space, w_obj, address): |
| 791 |
|
| 792 |
from pypy.module._cppyy.interp_cppyy import W_CPPOverload |
| 793 |
cppol = space.interp_w(W_CPPOverload, w_obj) |
| 794 |
|
| 795 |
|
| 796 |
for i in range(len(cppol.functions)): |
| 797 |
m = cppol.functions[i] |
| 798 |
if m.signature(False) == self.signature: |
| 799 |
x = rffi.cast(rffi.VOIDPP, address) |
| 800 |
x[0] = rffi.cast(rffi.VOIDP, capi.c_function_address(space, m.cppmethod)) |
| 801 |
address = rffi.cast(capi.C_OBJECT, address) |
| 802 |
ba = rffi.cast(rffi.CCHARP, address) |
| 803 |
ba[capi.c_function_arg_typeoffset(space)] = 'p' |
| 804 |
return |
| 805 |
|
| 806 |
|
| 807 |
raise oefmt(space.w_TypeError, |
| 808 |
"no overload found matching %s", self.signature) |
| 809 |
|
| 810 |
|
| 811 |
class SmartPtrConverter(TypeConverter): |
| 812 |
_immutable_fields = ['typecode', 'smartdecl', 'rawdecl', 'deref'] |
| 813 |
typecode = 'V' |
| 814 |
|
| 815 |
def __init__(self, space, smartdecl, raw, deref): |
| 816 |
from pypy.module._cppyy.interp_cppyy import W_CPPClassDecl, get_pythonized_cppclass |
| 817 |
self.smartdecl = smartdecl |
| 818 |
w_raw = get_pythonized_cppclass(space, raw) |
| 819 |
self.rawdecl = space.interp_w(W_CPPClassDecl, |
| 820 |
space.findattr(w_raw, space.newtext("__cppdecl__"))) |
| 821 |
self.deref = deref |
| 822 |
|
| 823 |
def _unwrap_object(self, space, w_obj): |
| 824 |
from pypy.module._cppyy.interp_cppyy import W_CPPInstance |
| 825 |
if isinstance(w_obj, W_CPPInstance): |
| 826 |
|
| 827 |
have_match = False |
| 828 |
if w_obj.smartdecl and capi.c_is_subtype(space, w_obj.smartdecl, self.smartdecl): |
| 829 |
|
| 830 |
have_match = True |
| 831 |
rawobject = w_obj._rawobject |
| 832 |
offset = capi.c_base_offset(space, w_obj.smartdecl, self.smartdecl, rawobject, 1) |
| 833 |
elif capi.c_is_subtype(space, w_obj.clsdecl, self.smartdecl): |
| 834 |
|
| 835 |
have_match = True |
| 836 |
rawobject = w_obj.get_rawobject() |
| 837 |
offset = capi.c_base_offset(space, w_obj.clsdecl, self.smartdecl, rawobject, 1) |
| 838 |
if have_match: |
| 839 |
obj_address = capi.direct_ptradd(rawobject, offset) |
| 840 |
return rffi.cast(capi.C_OBJECT, obj_address) |
| 841 |
|
| 842 |
raise oefmt(space.w_TypeError, |
| 843 |
"cannot pass %T instance as %s", w_obj, self.rawdecl.name) |
| 844 |
|
| 845 |
def convert_argument(self, space, w_obj, address): |
| 846 |
x = rffi.cast(rffi.VOIDPP, address) |
| 847 |
x[0] = rffi.cast(rffi.VOIDP, self._unwrap_object(space, w_obj)) |
| 848 |
address = rffi.cast(capi.C_OBJECT, address) |
| 849 |
ba = rffi.cast(rffi.CCHARP, address) |
| 850 |
ba[capi.c_function_arg_typeoffset(space)] = self.typecode |
| 851 |
|
| 852 |
def from_memory(self, space, w_obj, offset): |
| 853 |
address = rffi.cast(capi.C_OBJECT, self._get_raw_address(space, w_obj, offset)) |
| 854 |
from pypy.module._cppyy import interp_cppyy |
| 855 |
return interp_cppyy.wrap_cppinstance(space, address, |
| 856 |
self.rawdecl, smartdecl=self.smartdecl, deref=self.deref, do_cast=False) |
| 857 |
|
| 858 |
class SmartPtrPtrConverter(SmartPtrConverter): |
| 859 |
typecode = 'o' |
| 860 |
|
| 861 |
def from_memory(self, space, w_obj, offset): |
| 862 |
self._is_abstract(space) |
| 863 |
|
| 864 |
def to_memory(self, space, w_obj, w_value, offset): |
| 865 |
self._is_abstract(space) |
| 866 |
|
| 867 |
|
| 868 |
class SmartPtrRefConverter(SmartPtrPtrConverter): |
| 869 |
typecode = 'V' |
| 870 |
|
| 871 |
|
| 872 |
class MacroConverter(TypeConverter): |
| 873 |
def from_memory(self, space, w_obj, offset): |
| 874 |
|
| 875 |
address = self._get_raw_address(space, w_obj, offset) |
| 876 |
longptr = rffi.cast(rffi.LONGP, address) |
| 877 |
return space.newlong(longptr[0]) |
| 878 |
|
| 879 |
|
| 880 |
_converters = {} |
| 881 |
_a_converters = {} |
| 882 |
def get_converter(space, _name, default): |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
try: |
| 893 |
return _converters[_name](space, default) |
| 894 |
except KeyError: |
| 895 |
pass |
| 896 |
|
| 897 |
|
| 898 |
name = capi.c_resolve_name(space, _name) |
| 899 |
try: |
| 900 |
return _converters[name](space, default) |
| 901 |
except KeyError: |
| 902 |
pass |
| 903 |
|
| 904 |
|
| 905 |
try: |
| 906 |
return _converters[helper.remove_const(name)](space, default) |
| 907 |
except KeyError: |
| 908 |
pass |
| 909 |
|
| 910 |
|
| 911 |
cpd = helper.compound(name) |
| 912 |
clean_name = capi.c_resolve_name(space, helper.clean_type(name)) |
| 913 |
try: |
| 914 |
return _converters[clean_name+cpd](space, default) |
| 915 |
except KeyError: |
| 916 |
pass |
| 917 |
|
| 918 |
|
| 919 |
array_size = -1 |
| 920 |
if cpd == "[]": |
| 921 |
array_size = helper.array_size(_name) |
| 922 |
elif cpd == '*' and ':' in default: |
| 923 |
|
| 924 |
cpd = "[]" |
| 925 |
splitpos = default.find(':') |
| 926 |
if 0 < splitpos: |
| 927 |
array_size = int(default[:splitpos]) |
| 928 |
|
| 929 |
try: |
| 930 |
|
| 931 |
|
| 932 |
return _a_converters[clean_name+cpd](space, array_size) |
| 933 |
except KeyError: |
| 934 |
pass |
| 935 |
|
| 936 |
|
| 937 |
from pypy.module._cppyy import interp_cppyy |
| 938 |
scope_decl = interp_cppyy.scope_byname(space, clean_name) |
| 939 |
if scope_decl: |
| 940 |
from pypy.module._cppyy.interp_cppyy import W_CPPClassDecl |
| 941 |
clsdecl = space.interp_w(W_CPPClassDecl, scope_decl, can_be_None=False) |
| 942 |
|
| 943 |
|
| 944 |
check_smart = capi.c_smartptr_info(space, clean_name) |
| 945 |
if check_smart[0]: |
| 946 |
if cpd == '': |
| 947 |
return SmartPtrConverter(space, clsdecl, check_smart[1], check_smart[2]) |
| 948 |
elif cpd == '*': |
| 949 |
return SmartPtrPtrConverter(space, clsdecl, check_smart[1], check_smart[2]) |
| 950 |
elif cpd == '&': |
| 951 |
return SmartPtrRefConverter(space, clsdecl, check_smart[1], check_smart[2]) |
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
if cpd == "*": |
| 956 |
return InstancePtrConverter(space, clsdecl) |
| 957 |
elif cpd == "&": |
| 958 |
return InstanceRefConverter(space, clsdecl) |
| 959 |
elif cpd == "&&": |
| 960 |
return InstanceMoveConverter(space, clsdecl) |
| 961 |
elif cpd in ["**", "*[]", "&*"]: |
| 962 |
return InstancePtrPtrConverter(space, clsdecl) |
| 963 |
elif cpd == "[]" and array_size > 0: |
| 964 |
|
| 965 |
dims = default.split(':') |
| 966 |
return InstanceArrayConverter(space, clsdecl, array_size, dims) |
| 967 |
elif cpd == "": |
| 968 |
return InstanceConverter(space, clsdecl) |
| 969 |
|
| 970 |
if "(anonymous)" in name: |
| 971 |
|
| 972 |
return _converters["internal_enum_type_t"+cpd](space, default) |
| 973 |
elif "(*)" in name or "::*)" in name: |
| 974 |
|
| 975 |
pos = name.find("*)") |
| 976 |
if pos > 0: |
| 977 |
return FunctionPointerConverter(space, name[pos+2:]) |
| 978 |
|
| 979 |
|
| 980 |
if 0 <= cpd.find('*'): |
| 981 |
return VoidPtrConverter(space, default) |
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
return VoidConverter(space, name) |
| 986 |
|
| 987 |
|
| 988 |
_converters["bool"] = BoolConverter |
| 989 |
_converters["float"] = FloatConverter |
| 990 |
_converters["const float&"] = ConstFloatRefConverter |
| 991 |
_converters["double"] = DoubleConverter |
| 992 |
_converters["const double&"] = ConstDoubleRefConverter |
| 993 |
_converters["long double"] = LongDoubleConverter |
| 994 |
_converters["const long double&"] = ConstLongDoubleRefConverter |
| 995 |
_converters["const char*"] = CStringConverter |
| 996 |
_converters["void*"] = VoidPtrConverter |
| 997 |
_converters["void**"] = VoidPtrPtrConverter |
| 998 |
_converters["void*&"] = VoidPtrRefConverter |
| 999 |
|
| 1000 |
|
| 1001 |
_converters["std::basic_string<char>"] = STLStringConverter |
| 1002 |
_converters["const std::basic_string<char>&"] = STLStringConverter |
| 1003 |
_converters["std::basic_string<char>&"] = STLStringRefConverter |
| 1004 |
_converters["std::basic_string<char>&&"] = STLStringMoveConverter |
| 1005 |
|
| 1006 |
_converters["PyObject*"] = PyObjectConverter |
| 1007 |
|
| 1008 |
_converters["#define"] = MacroConverter |
| 1009 |
|
| 1010 |
|
| 1011 |
def _build_basic_converters(): |
| 1012 |
"NOT_RPYTHON" |
| 1013 |
|
| 1014 |
type_info = { |
| 1015 |
(rffi.CHAR, "char"), |
| 1016 |
(rffi.SIGNEDCHAR, "signed char"), |
| 1017 |
(rffi.UCHAR, "unsigned char"), |
| 1018 |
(lltype.UniChar, "wchar_t"), |
| 1019 |
(ffitypes.CHAR16_T, "char16_t"), |
| 1020 |
(ffitypes.CHAR32_T, "char32_t"), |
| 1021 |
} |
| 1022 |
|
| 1023 |
for c_type, name in type_info: |
| 1024 |
class BasicConverter(ffitypes.typeid(c_type), CharTypeConverterMixin, TypeConverter): |
| 1025 |
_immutable_ = True |
| 1026 |
def __init__(self, space, default): |
| 1027 |
self.valid_default = False |
| 1028 |
try: |
| 1029 |
self.default = rffi.cast(self.c_type, capi.c_strtoull(space, default)) |
| 1030 |
self.valid_default = True |
| 1031 |
except Exception: |
| 1032 |
self.default = rffi.cast(self.c_type, 0) |
| 1033 |
class ConstRefConverter(ConstRefNumericTypeConverterMixin, BasicConverter): |
| 1034 |
_immutable_ = True |
| 1035 |
_converters[name] = BasicConverter |
| 1036 |
_converters["const "+name+"&"] = ConstRefConverter |
| 1037 |
|
| 1038 |
|
| 1039 |
type_info = ( |
| 1040 |
(ffitypes.INT8_T, ("int8_t",), 'b', capi.c_strtoll), |
| 1041 |
(ffitypes.UINT8_T, ("uint8_t", "std::byte", "byte"), 'B', capi.c_strtoull), |
| 1042 |
(rffi.SHORT, ("short", "short int"), 'h', capi.c_strtoll), |
| 1043 |
(rffi.USHORT, ("unsigned short", "unsigned short int"), 'H', capi.c_strtoull), |
| 1044 |
(rffi.INT, ("int", "internal_enum_type_t"), 'i', capi.c_strtoll), |
| 1045 |
(rffi.UINT, ("unsigned", "unsigned int"), 'I', capi.c_strtoull), |
| 1046 |
(rffi.LONG, ("long", "long int"), 'l', capi.c_strtoll), |
| 1047 |
(rffi.ULONG, ("unsigned long", "unsigned long int"), 'L', capi.c_strtoull), |
| 1048 |
(rffi.LONGLONG, ("long long", "long long int", "Long64_t"), 'q', capi.c_strtoll), |
| 1049 |
(rffi.ULONGLONG, ("unsigned long long", "unsigned long long int", "ULong64_t"), 'Q', capi.c_strtoull), |
| 1050 |
) |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
for c_type, names, c_tc, dfc in type_info: |
| 1057 |
class BasicConverter(ffitypes.typeid(c_type), IntTypeConverterMixin, TypeConverter): |
| 1058 |
_immutable_ = True |
| 1059 |
typecode = c_tc |
| 1060 |
def __init__(self, space, default): |
| 1061 |
self.valid_default = False |
| 1062 |
try: |
| 1063 |
self.default = rffi.cast(self.c_type, dfc(space, default)) |
| 1064 |
self.valid_default = True |
| 1065 |
except Exception: |
| 1066 |
self.default = rffi.cast(self.c_type, 0) |
| 1067 |
class ConstRefConverter(ConstRefNumericTypeConverterMixin, BasicConverter): |
| 1068 |
_immutable_ = True |
| 1069 |
for name in names: |
| 1070 |
_converters[name] = BasicConverter |
| 1071 |
_converters["const "+name+"&"] = ConstRefConverter |
| 1072 |
|
| 1073 |
_build_basic_converters() |
| 1074 |
|
| 1075 |
|
| 1076 |
def _build_array_converters(): |
| 1077 |
"NOT_RPYTHON" |
| 1078 |
array_info = ( |
| 1079 |
('b', rffi.sizeof(rffi.SIGNEDCHAR), ("bool",)), |
| 1080 |
('b', rffi.sizeof(rffi.SIGNEDCHAR), ("signed char",)), |
| 1081 |
('B', rffi.sizeof(rffi.UCHAR), ("unsigned char", "std::byte", "byte")), |
| 1082 |
('h', rffi.sizeof(rffi.SHORT), ("short int", "short")), |
| 1083 |
('H', rffi.sizeof(rffi.USHORT), ("unsigned short int", "unsigned short")), |
| 1084 |
('i', rffi.sizeof(rffi.INT), ("int",)), |
| 1085 |
('I', rffi.sizeof(rffi.UINT), ("unsigned int", "unsigned")), |
| 1086 |
('l', rffi.sizeof(rffi.LONG), ("long int", "long")), |
| 1087 |
('L', rffi.sizeof(rffi.ULONG), ("unsigned long int", "unsigned long")), |
| 1088 |
('q', rffi.sizeof(rffi.LONGLONG), ("long long", "long long int", "Long64_t")), |
| 1089 |
('Q', rffi.sizeof(rffi.ULONGLONG), ("unsigned long long", "unsigned long long int", "ULong64_t")), |
| 1090 |
('f', rffi.sizeof(rffi.FLOAT), ("float",)), |
| 1091 |
('d', rffi.sizeof(rffi.DOUBLE), ("double",)), |
| 1092 |
|
| 1093 |
) |
| 1094 |
|
| 1095 |
for tcode, tsize, names in array_info: |
| 1096 |
class ArrayConverter(ArrayTypeConverterMixin, TypeConverter): |
| 1097 |
_immutable_fields_ = ['typecode', 'typesize'] |
| 1098 |
typecode = tcode |
| 1099 |
typesize = tsize |
| 1100 |
class PtrConverter(PtrTypeConverterMixin, TypeConverter): |
| 1101 |
_immutable_fields_ = ['typecode', 'typesize'] |
| 1102 |
typecode = tcode |
| 1103 |
typesize = tsize |
| 1104 |
class ArrayPtrConverter(ArrayPtrTypeConverterMixin, TypeConverter): |
| 1105 |
_immutable_fields_ = ['typecode', 'typesize'] |
| 1106 |
typecode = tcode |
| 1107 |
typesize = tsize |
| 1108 |
for name in names: |
| 1109 |
_a_converters[name+'[]'] = ArrayConverter |
| 1110 |
_a_converters[name+'*'] = PtrConverter |
| 1111 |
_a_converters[name+'**'] = ArrayPtrConverter |
| 1112 |
|
| 1113 |
|
| 1114 |
_a_converters["const char[]"] = CStringConverterWithSize |
| 1115 |
_a_converters["char[]"] = _a_converters["const char[]"] |
| 1116 |
|
| 1117 |
_build_array_converters() |
| 1118 |
|
| 1119 |
|
| 1120 |
def _add_aliased_converters(): |
| 1121 |
"NOT_RPYTHON" |
| 1122 |
aliases = ( |
| 1123 |
("const char*", "char*"), |
| 1124 |
|
| 1125 |
("std::basic_string<char>", "std::string"), |
| 1126 |
("const std::basic_string<char>&", "const std::string&"), |
| 1127 |
("std::basic_string<char>&", "std::string&"), |
| 1128 |
("std::basic_string<char>&&", "std::string&&"), |
| 1129 |
|
| 1130 |
("const internal_enum_type_t&", "internal_enum_type_t&"), |
| 1131 |
|
| 1132 |
("PyObject*", "_object*"), |
| 1133 |
) |
| 1134 |
|
| 1135 |
for c_type, alias in aliases: |
| 1136 |
_converters[alias] = _converters[c_type] |
| 1137 |
_add_aliased_converters() |