forked from oracle/graalpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_cext.py
More file actions
1367 lines (1014 loc) · 34.8 KB
/
python_cext.py
File metadata and controls
1367 lines (1014 loc) · 34.8 KB
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# The Universal Permissive License (UPL), Version 1.0
#
# Subject to the condition set forth below, permission is hereby granted to any
# person obtaining a copy of this software, associated documentation and/or
# data (collectively the "Software"), free of charge and under any and all
# copyright rights in the Software, and any and all patent rights owned or
# freely licensable by each licensor hereunder covering either (i) the
# unmodified Software as contributed to or provided by such licensor, or (ii)
# the Larger Works (as defined below), to deal in both
#
# (a) the Software, and
#
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
# one is included with the Software each a "Larger Work" to which the Software
# is contributed by such licensors),
#
# without restriction, including without limitation the rights to copy, create
# derivative works of, display, perform, and distribute the Software and make,
# use, sell, offer for sale, import, export, have made, and have sold the
# Software and the Larger Work(s), and to sublicense the foregoing rights on
# either these or other terms.
#
# This license is subject to the following condition:
#
# The above copyright notice and either this complete permission notice or at a
# minimum a reference to the UPL must be included in all copies or substantial
# portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import _imp
import sys
import _thread
capi = capi_to_java = None
_capi_hooks = []
def register_capi_hook(hook):
assert callable(hook)
if capi:
hook()
else:
_capi_hooks.append(hook)
def may_raise(error_result=native_null):
if isinstance(error_result, type(may_raise)):
# direct annotation
return may_raise(native_null)(error_result)
else:
def decorator(fun):
return make_may_raise_wrapper(fun, error_result)
return decorator
def Py_ErrorHandler():
return to_sulong(error_handler)
def Py_NotImplemented():
return NotImplemented
def Py_True():
return True
def Py_False():
return False
def Py_Ellipsis():
return ...
moduletype = type(sys)
def _PyModule_CreateInitialized_PyModule_New(name):
# see CPython's Objects/moduleobject.c - _PyModule_CreateInitialized for
# comparison how they handle _Py_PackageContext
if _imp._py_package_context:
if _imp._py_package_context.endswith(name):
name = _imp._py_package_context
_imp._py_package_context = None
new_module = moduletype(name)
# TODO: (tfel) I don't think this is the right place to set it, but somehow
# at least in the import of sklearn.neighbors.dist_metrics through
# sklearn.neighbors.ball_tree the __package__ attribute seems to be already
# set in CPython. To not produce a warning, I'm setting it here, although I
# could not find what CPython really does
if "." in name:
new_module.__package__ = name.rpartition('.')[0]
return new_module
def PyModule_SetDocString(module, string):
module.__doc__ = string
def PyModule_NewObject(name):
return moduletype(name)
##################### DICT
def PyDict_New():
return {}
@may_raise
def PyDict_Next(dictObj, pos):
if not isinstance(dictObj, dict):
return native_null
curPos = 0
max = len(dictObj)
if pos >= max:
return native_null
for key in dictObj:
if curPos == pos:
return key, dictObj[key]
curPos = curPos + 1
return native_null
@may_raise(-1)
def PyDict_Size(dictObj):
if not isinstance(dictObj, dict):
raise TypeError('expected dict, {!s} found'.format(type(dictObj)))
return len(dictObj)
@may_raise(None)
def PyDict_Copy(dictObj):
if not isinstance(dictObj, dict):
__bad_internal_call(None, None, dictObj)
return dictObj.copy()
@may_raise
def PyDict_GetItem(dictObj, key):
if not isinstance(dictObj, dict):
raise TypeError('expected dict, {!s} found'.format(type(dictObj)))
return dictObj.get(key, native_null)
@may_raise(-1)
def PyDict_SetItem(dictObj, key, value):
if not isinstance(dictObj, dict):
raise TypeError('expected dict, {!s} found'.format(type(dictObj)))
dictObj[key] = value
return 0
@may_raise(-1)
def PyDict_DelItem(dictObj, key):
if not isinstance(dictObj, dict):
raise TypeError('expected dict, {!s} found'.format(type(dictObj)))
del dictObj[key]
return 0
@may_raise(-1)
def PyDict_Contains(dictObj, key):
if not isinstance(dictObj, dict):
__bad_internal_call(None, None, dictObj)
return key in dictObj
##################### SET, FROZENSET
@may_raise
def PySet_New(iterable):
if iterable:
return set(iterable)
else:
return set()
@may_raise(-1)
def PySet_Contains(anyset, item):
if not (isinstance(anyset, set) or isinstance(anyset, frozenset)):
__bad_internal_call(None, None, anyset)
return item in anyset
@may_raise
def PySet_Pop(anyset):
if not isinstance(anyset, set):
__bad_internal_call(None, None, anyset)
return anyset.pop()
@may_raise
def PyFrozenSet_New(iterable):
if iterable:
return frozenset(iterable)
else:
return frozenset()
##################### MAPPINGPROXY
def PyDictProxy_New(mapping):
mappingproxy = type(type.__dict__)
return mappingproxy(mapping)
def Py_DECREF(obj):
pass
def Py_INCREF(obj):
pass
def Py_XINCREF(obj):
pass
def PyObject_LEN(obj):
return len(obj)
def PyTruffle_Object_LEN(obj):
return len(to_java(obj))
##################### BYTES
def PyBytes_AsStringCheckEmbeddedNull(obj, encoding):
if not PyBytes_Check(obj):
raise TypeError('expected bytes, {!s} found'.format(type(obj)))
result = obj.decode(encoding)
for ch in obj:
if ch == 0:
raise ValueError('embedded null byte')
return result
def PyBytes_Size(obj):
return PyObject_Size(obj)
def PyBytes_Check(obj):
return isinstance(obj, bytes)
@may_raise
def PyBytes_Concat(original, newpart):
return original + newpart
def PyBytes_FromFormat(fmt, args):
formatted = fmt % args
return formatted.encode()
@may_raise
def PyBytes_Join(sep, iterable):
return sep.join(iterable)
##################### LIST
@may_raise
def PyList_New(size):
if size < 0:
__bad_internal_call(None, None, None)
return [None] * size
@may_raise
def PyList_GetItem(listObj, pos):
if not isinstance(listObj, list):
__bad_internal_call(None, None, listObj)
if pos < 0:
raise IndexError("list index out of range")
return listObj[pos]
@may_raise(-1)
def PyList_SetItem(listObj, pos, newitem):
if not isinstance(listObj, list):
__bad_internal_call(None, None, listObj)
if pos < 0:
raise IndexError("list assignment index out of range")
listObj[pos] = newitem
return 0
@may_raise(-1)
def PyList_Append(listObj, newitem):
if not isinstance(listObj, list):
__bad_internal_call(None, None, listObj)
listObj.append(newitem)
return 0
@may_raise
def PyList_AsTuple(listObj):
if not isinstance(listObj, list):
raise SystemError("expected list type")
return tuple(listObj)
@may_raise
def PyList_GetSlice(listObj, ilow, ihigh):
if not isinstance(listObj, list):
__bad_internal_call(None, None, listObj)
return listObj[ilow:ihigh]
@may_raise(-1)
def PyList_SetSlice(listObj, ilow, ihigh, s):
if not isinstance(listObj, list):
__bad_internal_call(None, None, listObj)
listObj[ilow:ihigh] = s
return 0
@may_raise(-1)
def PyList_Size(listObj):
if not isinstance(listObj, list):
__bad_internal_call(None, None, listObj)
return len(listObj)
@may_raise(-1)
def PyList_Sort(listObj):
if not isinstance(listObj, list):
__bad_internal_call(None, None, listObj)
listObj.sort()
return 0
##################### LONG
@may_raise(-1)
def PyLong_AsPrimitive(n, signed, size):
return TrufflePInt_AsPrimitive(int(n), signed, size)
def _PyLong_Sign(n):
if n==0:
return 0
elif n < 0:
return -1
else:
return 1
@may_raise
def PyLong_FromDouble(d):
return int(d)
@may_raise
def PyLong_FromString(string, base, negative):
result = int(string, base)
if negative:
return -result
else:
return result
##################### FLOAT
@may_raise
def PyFloat_FromDouble(n):
return float(n)
##################### COMPLEX
@may_raise
def PyComplex_AsCComplex(n):
obj = complex(n)
return (obj.real, obj.imag)
##################### NUMBER
def _safe_check(v, type_check):
try:
return type_check(v)
except:
return False
def PyNumber_Check(v):
return _safe_check(v, lambda x: isinstance(int(x), int)) or _safe_check(v, lambda x: isinstance(float(x), float))
@may_raise
def PyNumber_BinOp(v, w, binop):
if binop == 0:
return v + w
elif binop == 1:
return v - w
elif binop == 2:
return v * w
elif binop == 3:
return v / w
elif binop == 4:
return v << w
elif binop == 5:
return v >> w
elif binop == 6:
return v | w
elif binop == 7:
return v & w
elif binop == 8:
return v ^ w
elif binop == 9:
return v // w
elif binop == 10:
return v % w
elif binop == 12:
return v @ w
else:
raise SystemError("unknown binary operator (code=%s)" % binop)
def _binop_name(binop):
if binop == 0:
return "+"
elif binop == 1:
return "-"
elif binop == 2:
return "*"
elif binop == 3:
return "/"
elif binop == 4:
return "<<"
elif binop == 5:
return ">>"
elif binop == 6:
return "|"
elif binop == 7:
return "&"
elif binop == 8:
return "^"
elif binop == 9:
return "//"
elif binop == 10:
return "%"
elif binop == 12:
return "@"
@may_raise
def PyNumber_InPlaceBinOp(v, w, binop):
if binop == 0:
v += w
elif binop == 1:
v -= w
elif binop == 2:
v *= w
elif binop == 3:
v /= w
elif binop == 4:
v <<= w
elif binop == 5:
v >>= w
elif binop == 6:
v |= w
elif binop == 7:
v &= w
elif binop == 8:
v ^= w
elif binop == 9:
v //= w
elif binop == 10:
v %= w
elif binop == 12:
v @= w
else:
raise SystemError("unknown in-place binary operator (code=%s)" % binop)
# nothing else required; the operator will automatically fall back if
# no in-place operation is available
return v
@may_raise
def PyNumber_UnaryOp(v, unaryop):
if unaryop == 0:
return +v
elif unaryop == 1:
return -v
elif unaryop == 2:
return ~v
else:
raise SystemError("unknown unary operator (code=%s)" % unaryop)
@may_raise
def PyNumber_Index(v):
if not hasattr(v, "__index__"):
raise TypeError("'%s' object cannot be interpreted as an integer" % type(v))
result = v.__index__()
result_type = type(result)
if not isinstance(result, int):
raise TypeError("__index__ returned non-int (type %s)" % result_type)
if result_type is not int:
from warnings import warn
warn("__index__ returned non-int (type %s). The ability to return an instance of a strict subclass of int "
"is deprecated, and may be removed in a future version of Python." % result_type)
return result
@may_raise
def PyNumber_Long(v):
return int(v)
@may_raise
def PyNumber_Absolute(v):
return abs(v)
@may_raise
def PyNumber_Divmod(a, b):
return divmod(a, b)
@may_raise
def PyIter_Next(itObj):
try:
return next(itObj)
except StopIteration:
PyErr_Restore(None, None, None)
return native_null
##################### SEQUENCE
@may_raise
def PySequence_Tuple(obj):
return tuple(obj)
@may_raise
def PySequence_List(obj):
return list(obj)
@may_raise
def PySequence_GetItem(obj, key):
if not hasattr(obj, '__getitem__'):
raise TypeError("'%s' object does not support indexing)" % repr(obj))
if len(obj) < 0:
return native_null
return obj[key]
@may_raise(-1)
def PySequence_SetItem(obj, key, value):
if not hasattr(obj, '__setitem__'):
raise TypeError("'%s' object does not support item assignment)" % repr(obj))
if len(obj) < 0:
return -1
obj.__setitem__(key, value)
return 0
@may_raise(-1)
def PySequence_Contains(haystack, needle):
return needle in haystack
##################### UNICODE
@may_raise
def PyUnicode_FromObject(o):
if not isinstance(o, str):
raise TypeError("Can't convert '%s' object to str implicitly" % type(o).__name__)
return str(o)
@may_raise(-1)
def PyUnicode_GetLength(o):
if not isinstance(o, str):
raise TypeError("bad argument type for built-in operation");
return len(o)
@may_raise
def PyUnicode_Concat(left, right):
if not isinstance(left, str):
raise TypeError("must be str, not %s" % type(left));
if not isinstance(right, str):
raise TypeError("must be str, not %s" % type(right));
return left + right
@may_raise
def PyUnicode_FromEncodedObject(obj, encoding, errors):
if isinstance(obj, bytes):
return obj.decode(encoding, errors)
if isinstance(obj, str):
raise TypeError("decoding str is not supported")
def PyUnicode_InternInPlace(s):
return sys.intern(s)
@may_raise
def PyUnicode_Format(format, args):
if not isinstance(format, str):
raise TypeError("Must be str, not %s" % type(format).__name__)
return format % args
@may_raise(-1)
def PyUnicode_FindChar(string, char, start, end, direction):
if not isinstance(string, str):
raise TypeError("Must be str, not %s" % type(string).__name__)
if direction > 0:
return string.find(chr(char), start, end)
else:
return string.rfind(chr(char), start, end)
@may_raise
def PyUnicode_Substring(string, start, end):
if not isinstance(string, str):
raise TypeError("Must be str, not %s" % type(string).__name__)
return string[start:end]
@may_raise
def PyUnicode_Join(separator, seq):
if not isinstance(separator, str):
raise TypeError("Must be str, not %s" % type(separator).__name__)
return separator.join(seq)
@may_raise(-1)
def PyUnicode_Compare(left, right):
if left == right:
return 0
elif left < right:
return -1
else:
return 1
##################### CAPSULE
class PyCapsule:
name = None
pointer = None
context = None
def __init__(self, name, pointer, destructor):
self.name = name
self.pointer = to_sulong(pointer)
def __repr__(self):
name = "NULL" if self.name is None else self.name
quote = "" if self.name is None else '"'
return "<capsule object %s%s%s at %p>" % (quote, name, quote, self.pointer)
@may_raise
def PyCapsule_GetContext(obj):
if not isinstance(obj, PyCapsule) or obj.pointer is None:
raise ValueError("PyCapsule_GetContext called with invalid PyCapsule object")
return obj.context
@may_raise
def PyCapsule_GetPointer(obj, name):
if not isinstance(obj, PyCapsule) or obj.pointer is None:
raise ValueError("PyCapsule_GetPointer called with invalid PyCapsule object")
if name != None and name != obj.name:
raise ValueError("PyCapsule_GetPointer called with incorrect name")
return obj.pointer
@may_raise
def PyCapsule_Import(name, no_block):
obj = None
mod = name.split(".")[0]
try:
obj = __import__(mod)
except:
raise ImportError('PyCapsule_Import could not import module "%s"' % name)
for attr in name.split(".")[1:]:
obj = getattr(obj, attr)
if PyCapsule_IsValid(obj, name):
return obj.pointer
else:
raise AttributeError('PyCapsule_Import "%s" is not valid' % name)
def PyCapsule_IsValid(obj, name):
return (isinstance(obj, PyCapsule) and
obj.pointer != None and
obj.name == name)
def PyModule_AddObject(m, k, v):
m.__dict__[k] = v
return None
@may_raise
def PyStructSequence_New(typ):
n = len(typ._fields)
return typ(*([None]*n))
namedtuple_type = None
@may_raise
def PyStructSequence_InitType2(type_name, type_doc, field_names, field_docs):
assert len(field_names) == len(field_docs)
global namedtuple_type
if not namedtuple_type:
from collections import namedtuple as namedtuple_type
new_type = namedtuple_type(type_name, field_names)
new_type.__doc__ = type_doc
for i in range(len(field_names)):
prop = getattr(new_type, field_names[i])
assert isinstance(prop, property)
prop.__doc__ = field_docs[i]
# ensure '_fields' attribute; required in 'PyStructSequence_New'
assert hasattr(new_type, "_fields")
return new_type
def METH_UNSUPPORTED(fun):
raise NotImplementedError("unsupported message type")
def METH_DIRECT(fun):
return fun
class _C:
def _m(self): pass
methodtype = type(_C()._m)
class modulemethod(methodtype):
def __new__(cls, mod, func):
return super().__new__(cls, mod, func)
class cstaticmethod():
def __init__(self, func):
self.__func__ = func
def __get__(self, instance, owner=None):
return methodtype(None, self.__func__)
def __call__(*args, **kwargs):
return self.__func__(None, *args, **kwargs)
def AddFunction(primary, tpDict, name, cfunc, cwrapper, wrapper, doc, isclass=False, isstatic=False):
owner = to_java_type(primary)
if isinstance(owner, moduletype):
# module case, we create the bound function-or-method
func = PyCFunction_NewEx(name, cfunc, cwrapper, wrapper, owner, owner.__name__, doc)
object.__setattr__(owner, name, func)
else:
func = wrapper(CreateFunction(name, cfunc, cwrapper, owner))
if isclass:
func = classmethod(func)
elif isstatic:
func = cstaticmethod(func)
PyTruffle_SetAttr(func, "__name__", name)
PyTruffle_SetAttr(func, "__doc__", doc)
type_dict = to_java(tpDict)
if name == "__init__":
def __init__(self, *args, **kwargs):
if func(self, *args, **kwargs) != 0:
raise TypeError("__init__ failed")
type_dict[name] = __init__
else:
type_dict[name] = func
def PyCFunction_NewEx(name, cfunc, cwrapper, wrapper, self, module, doc):
func = wrapper(CreateFunction(name, cfunc, cwrapper))
PyTruffle_SetAttr(func, "__name__", name)
PyTruffle_SetAttr(func, "__doc__", doc)
method = PyTruffle_BuiltinMethod(self, func)
PyTruffle_SetAttr(method, "__module__", to_java(module))
return method
def PyMethod_New(func, self):
# TODO we should use the method constructor
# e.g. methodtype(func, self)
def bound_function(*args, **kwargs):
return func(self, *args, **kwargs)
return bound_function
def AddMember(primary, tpDict, name, memberType, offset, canSet, doc):
# the ReadMemberFunctions and WriteMemberFunctions don't have a wrapper to
# convert arguments to Sulong, so we can avoid boxing the offsets into PInts
pclass = to_java_type(primary)
member = property()
getter = ReadMemberFunctions[memberType]
def member_getter(self):
return to_java(getter(to_sulong(self), TrufflePInt_AsPrimitive(offset, 1, 8)))
member.getter(member_getter)
if canSet:
setter = WriteMemberFunctions[memberType]
def member_setter(self, value):
setter(to_sulong(self), TrufflePInt_AsPrimitive(offset, 1, 8), to_sulong(value))
member.setter(member_setter)
member.__doc__ = doc
type_dict = to_java(tpDict)
type_dict[name] = member
getset_descriptor = type(type(AddMember).__code__)
def AddGetSet(primary, name, getter, getter_wrapper, setter, setter_wrapper, doc, closure):
pclass = to_java_type(primary)
fset = fget = None
if getter:
getter_w = CreateFunction(name, getter, getter_wrapper, pclass)
def member_getter(self):
# NOTE: The 'to_java' is intended and correct because this call will do a downcall an
# all args will go through 'to_sulong' then. So, if we don't convert the pointer
# 'closure' to a Python value, we will get the wrong wrapper from 'to_sulong'.
return capi_to_java(getter_w(self, to_java(closure)))
fget = member_getter
if setter:
setter_w = CreateFunction(name, setter, setter_wrapper, pclass)
def member_setter(self, value):
result = setter_w(self, value, closure)
if result != 0:
raise
return None
fset = member_setter
else:
fset = lambda self, value: GetSet_SetNotWritable(self, value, name)
getset = PyTruffle_GetSetDescriptor(fget=fget, fset=fset, name=name, owner=pclass)
PyTruffle_SetAttr(getset, "__doc__", doc)
PyTruffle_SetAttr(pclass, name, getset)
def GetSet_SetNotWritable(self, value, attr):
raise AttributeError("attribute '%s' of '%s' objects is not writable" % (attr, type(self).__name__))
def PyObject_Str(o):
return str(o)
def PyObject_Repr(o):
return repr(o)
@may_raise(-1)
def PyTuple_Size(t):
if not isinstance(t, tuple):
__bad_internal_call(None, None, t)
return len(t)
@may_raise
def PyTuple_GetSlice(t, start, end):
if not isinstance(t, tuple):
__bad_internal_call(None, None, t)
return t[start:end]
@may_raise
def dict_from_list(lst):
if len(lst) % 2 != 0:
raise SystemError("list cannot be converted to dict")
d = {}
for i in range(0, len(lst), 2):
d[lst[i]] = lst[i + 1]
return d
def PyObject_Size(obj):
try:
return int(len(obj))
except Exception:
return -1
@may_raise
def PyObject_Call(callee, args, kwargs):
return callee(*args, **kwargs)
@may_raise
def PyObject_CallMethod(rcvr, method, args):
# TODO(fa) that seems to be a workaround
if type(args) is tuple:
return getattr(rcvr, method)(*args)
elif args is not None:
return getattr(rcvr, method)(args)
return getattr(rcvr, method)()
@may_raise
def PyObject_GetItem(obj, key):
return obj[key]
@may_raise(1)
def PyObject_SetItem(obj, key, value):
obj[key] = value
return 0
def PyObject_IsInstance(obj, typ):
if isinstance(obj, typ):
return 1
else:
return 0
def PyObject_IsSubclass(derived, cls):
if issubclass(derived, cls):
return 1
else:
return 0
@may_raise
def PyObject_RichCompare(left, right, op):
return do_richcompare(left, right, op)
def PyObject_AsFileDescriptor(obj):
if isinstance(obj, int):
result = obj
elif hasattr(obj, "fileno"):
result = obj.fileno()
if not isinstance(result, int):
raise TypeError("fileno() returned a non-integer")
else:
raise TypeError("argument must be an int, or have a fileno() method")
if result < 0:
raise ValueError("file descriptor cannot be a negative integer (%d)" % result)
return int(result)
@may_raise
def PyObject_GetAttr(obj, attr):
return getattr(obj, attr)
@may_raise(-1)
def PyObject_SetAttr(obj, attr, value):
setattr(obj, attr, value)
return 0
def PyObject_HasAttr(obj, attr):
return 1 if hasattr(obj, attr) else 0
def PyObject_HashNotImplemented(obj):
return TypeError("unhashable type: '%s'" % type(obj).__name__)
def PyObject_IsTrue(obj):
return 1 if obj else 0
## EXCEPTIONS
@may_raise(None)
def PyErr_CreateAndSetException(exception_type, msg):
if not _is_exception_class(exception_type):
raise SystemError("exception %r not a BaseException subclass" % exception_type)
if msg is None: