Debian Codegraph @

pypy/module/_cppyy/lowlevelviews.py

raw symbols SCIP
1 # Naked C++ pointers carry no useful size or layout information, but often
2 # such information is externnally available. Low level views are arrays with
3 # a few more methods allowing such information to be set. Afterwards, it is
4 # simple to pass these views on to e.g. numpy (w/o the need to copy).
5
6 from pypy.interpreter.error import OperationError, oefmt
7 from pypy.interpreter.gateway import interp2app, unwrap_spec
8 from pypy.interpreter.typedef import TypeDef, GetSetProperty, interp_attrproperty_w
9 from pypy.interpreter.baseobjspace import W_Root
10
11 from rpython.rtyper.lltypesystem import rffi
12 from rpython.rlib.rarithmetic import intmask
13
14 from pypy.module._rawffi.interp_array import W_ArrayInstance
15 from pypy.module._rawffi.interp_rawffi import segfault_exception
16 from pypy.module._cppyy import capi
17
18
19 class W_LowLevelView(W_ArrayInstance):
20 def __init__(self, space, shape, length, address):
21 assert address # if not address, base class will allocate memory
22 W_ArrayInstance.__init__(self, space, shape, length, address)
23
24 @unwrap_spec(args_w='args_w')
25 def reshape(self, space, args_w):
26 # llviews are only created from non-zero addresses, so we only need
27 # to adjust length and shape
28
29 nargs = len(args_w)
30 if nargs == 0:
31 raise oefmt(space.w_TypeError, "reshape expects a tuple argument")
32
33 newshape_w = args_w
34 if nargs != 1 or not space.isinstance_w(args_w[0], space.w_tuple) or \
35 not space.len_w(args_w[0]) == 1:
36 raise oefmt(space.w_TypeError,
37 "tuple object of length 1 expected, received %T", args_w[0])
38
39 w_shape = args_w[0]
40
41 # shape in W_ArrayInstance-speak is somewhat different from what
42 # e.g. numpy thinks of it: self.shape contains the info (itemcode,
43 # size, etc.) of a single entry; length is user-facing shape
44 self.length = space.int_w(space.getitem(w_shape, space.newint(0)))
45
46
47 W_LowLevelView.typedef = TypeDef(
48 'LowLevelView',
49 __repr__ = interp2app(W_LowLevelView.descr_repr),
50 __setitem__ = interp2app(W_LowLevelView.descr_setitem),
51 __getitem__ = interp2app(W_LowLevelView.descr_getitem),
52 __len__ = interp2app(W_LowLevelView.getlength),
53 buffer = GetSetProperty(W_LowLevelView.getbuffer),
54 shape = interp_attrproperty_w('shape', W_LowLevelView),
55 free = interp2app(W_LowLevelView.free),
56 byptr = interp2app(W_LowLevelView.byptr),
57 itemaddress = interp2app(W_LowLevelView.descr_itemaddress),
58 reshape = interp2app(W_LowLevelView.reshape),
59 )
60 W_LowLevelView.typedef.acceptable_as_base_class = False
61
62
63 class W_ArrayOfInstances(W_Root):
64 _attrs_ = ['converter', 'baseaddress', 'clssize', 'length']
65 _immutable_fields_ = ['converter', 'baseaddress', 'clssize']
66
67 def __init__(self, space, clsdecl, address, length, dimensions):
68 from pypy.module._cppyy import converter
69 name = clsdecl.name
70 self.clssize = int(intmask(capi.c_size_of_klass(space, clsdecl)))
71 if dimensions:
72 name = name + '[' + dimensions[0] + ']'
73 for num in dimensions:
74 self.clssize *= int(num)
75 dimensions = ':'.join(dimensions)
76 self.converter = converter.get_converter(space, name, dimensions)
77 self.baseaddress = address
78 self.length = length
79
80 @unwrap_spec(idx=int)
81 def getitem(self, space, idx):
82 if not self.baseaddress:
83 raise segfault_exception(space, "accessing elements of freed array")
84 if idx >= self.length or idx < 0:
85 raise OperationError(space.w_IndexError, space.w_None)
86 itemaddress = rffi.cast(rffi.LONG, self.baseaddress)+idx*self.clssize
87 return self.converter.from_memory(space, space.w_None, itemaddress)
88
89 def getlength(self, space):
90 return space.newint(self.length)
91
92 def setlength(self, space, w_length):
93 self.length = space.int_w(w_length)
94
95 W_ArrayOfInstances.typedef = TypeDef(
96 'ArrayOfInstances',
97 __getitem__ = interp2app(W_ArrayOfInstances.getitem),
98 __len__ = interp2app(W_ArrayOfInstances.getlength),
99 size = GetSetProperty(W_ArrayOfInstances.getlength, W_ArrayOfInstances.setlength),
100 )
101 W_ArrayOfInstances.typedef.acceptable_as_base_class = False