Debian Codegraph @

pypy/module/_hpy_universal/interp_long.py

raw symbols SCIP
1 from rpython.rlib.objectmodel import we_are_translated
2 from rpython.rlib.rarithmetic import maxint
3 from rpython.rtyper.lltypesystem import lltype, rffi
4 from pypy.interpreter.error import OperationError, oefmt
5 from pypy.module._hpy_universal.apiset import API
6
7
8 @API.func("HPy HPyLong_FromInt32_t(HPyContext *ctx, int value)")
9 def HPyLong_FromInt32_t(space, handles, ctx, value):
10 # XXX: cpyext does space.newlong: write a test and fix
11 w_obj = space.newint(rffi.cast(lltype.Signed, value))
12 return handles.new(w_obj)
13
14 @API.func("HPy HPyLong_FromUInt32_t(HPyContext *ctx, unsigned int value)")
15 def HPyLong_FromUInt32_t(space, handles, ctx, v):
16 w_obj = space.newlong_from_rarith_int(v)
17 return handles.new(w_obj)
18
19 # On windows, long is 4 bytes, on all platforms long long is 8
20 @API.func("HPy HPyLong_FromInt64_t(HPyContext *ctx, long long v)")
21 def HPyLong_FromInt64_t(space, handles, ctx, v):
22 w_obj = space.newlong_from_rarith_int(v)
23 return handles.new(w_obj)
24
25 @API.func("HPy HPyLong_FromUInt64_t(HPyContext *ctx, unsigned long long v)")
26 def HPyLong_FromUInt64_t(space, handles, ctx, v):
27 w_obj = space.newlong_from_rarith_int(v)
28 return handles.new(w_obj)
29
30 @API.func("HPy HPyLong_FromSize_t(HPyContext *ctx, size_t value)")
31 def HPyLong_FromSize_t(space, handles, ctx, v):
32 w_obj = space.newlong_from_rarith_int(v)
33 return handles.new(w_obj)
34
35 @API.func("HPy HPyLong_FromSsize_t(HPyContext *ctx, HPy_ssize_t value)")
36 def HPyLong_FromSsize_t(space, handles, ctx, v):
37 # XXX: cpyext uses space.newlong: is there any difference?
38 w_obj = space.newlong_from_rarith_int(v)
39 return handles.new(w_obj)
40
41 ULONG_MASK = (2 ** (8 * rffi.sizeof(rffi.ULONG)) -1)
42 ULONG_MAX = (2 ** (8 * rffi.sizeof(rffi.ULONG)) -1)
43 LONG_MAX = (2 ** (8 * rffi.sizeof(rffi.ULONG) - 1) -1)
44 LONG_MIN = (-2 ** (8 * rffi.sizeof(rffi.ULONG) - 1))
45 need_to_check = maxint > ULONG_MAX
46
47 @API.func("long HPyLong_AsInt32_t(HPyContext *ctx, HPy h)",
48 error_value=API.cast("long", -1))
49 def HPyLong_AsInt32_t(space, handles, ctx, h):
50 w_long = handles.deref(h)
51 val = space.int_w(space.int(w_long))
52 if need_to_check and (val > LONG_MAX or val < LONG_MIN):
53 # On win64 space.int_w will succeed for 8-byte ints
54 # but long is 4 bytes. So we must check manually
55 raise oefmt(space.w_OverflowError,
56 "Python int too large to convert to C long")
57 return rffi.cast(rffi.LONG, val)
58
59 @API.func("unsigned long HPyLong_AsUInt32_t(HPyContext *ctx, HPy h)",
60 error_value=API.cast("unsigned long", -1))
61 def HPyLong_AsUInt32_t(space, handles, ctx, h):
62 w_long = handles.deref(h)
63 try:
64 val = space.uint_w(w_long)
65 except OperationError as e:
66 if e.match(space, space.w_ValueError):
67 e.w_type = space.w_OverflowError
68 raise
69 if need_to_check and val > ULONG_MAX:
70 # On win64 space.uint_w will succeed for 8-byte ints
71 # but long is 4 bytes. So we must check manually
72 raise oefmt(space.w_OverflowError,
73 "Python int too large to convert to C unsigned long")
74 return rffi.cast(rffi.ULONG, val)
75
76 @API.func("unsigned long HPyLong_AsUInt32_tMask(HPyContext *ctx, HPy h)",
77 error_value=API.cast("unsigned long", -1))
78 def HPyLong_AsUInt32_tMask(space, handles, ctx, h):
79 w_long = handles.deref(h)
80 num = space.bigint_w(w_long)
81 val = num.uintmask()
82 if need_to_check and not we_are_translated():
83 # On win64 num.uintmask will succeed for 8-byte ints
84 # but unsigned long is 4 bytes.
85 # The cast below is sufficient when translated, but
86 # we need an extra check when running on CPython.
87 val &= ULONG_MASK
88 return rffi.cast(rffi.ULONG, val)
89
90 @API.func("long long HPyLong_AsInt64_t(HPyContext *ctx, HPy h)",
91 error_value=API.cast("long long", -1))
92 def HPyLong_AsInt64_t(space, handles, ctx, h):
93 w_long = handles.deref(h)
94 return rffi.cast(rffi.LONGLONG, space.r_longlong_w(w_long))
95
96 @API.func("unsigned long long HPyLong_AsUInt64_t(HPyContext *ctx, HPy h)",
97 error_value=API.cast("unsigned long long", -1))
98 def HPyLong_AsUInt64_t(space, handles, ctx, h):
99 w_long = handles.deref(h)
100 try:
101 return rffi.cast(rffi.ULONGLONG, space.r_ulonglong_w(
102 w_long, allow_conversion=False))
103 except OperationError as e:
104 if e.match(space, space.w_ValueError):
105 e.w_type = space.w_OverflowError
106 raise
107
108 @API.func("unsigned long long HPyLong_AsUInt64_tMask(HPyContext *ctx, HPy h)",
109 error_value=API.cast("unsigned long long", -1))
110 def HPyLong_AsUInt64_tMask(space, handles, ctx, h):
111 w_long = handles.deref(h)
112 num = space.bigint_w(w_long)
113 return num.ulonglongmask()
114
115 @API.func("size_t HPyLong_AsSize_t(HPyContext *ctx, HPy h)",
116 error_value=API.cast("size_t", -1))
117 def HPyLong_AsSize_t(space, handles, ctx, h):
118 w_long = handles.deref(h)
119 try:
120 return space.uint_w(w_long)
121 except OperationError as e:
122 if e.match(space, space.w_ValueError):
123 e.w_type = space.w_OverflowError
124 raise
125
126 @API.func("HPy_ssize_t HPyLong_AsSsize_t(HPyContext *ctx, HPy h)",
127 error_value=API.cast("ssize_t", -1))
128 def HPyLong_AsSsize_t(space, handles, ctx, h):
129 w_long = handles.deref(h)
130 return space.int_w(w_long, allow_conversion=False)
131
132 @API.func("void * HPyLong_AsVoidPtr(HPyContext *ctx, HPy h)")
133 def HPyLong_AsVoidPtr(space, handles, ctx, h):
134 w_long = handles.deref(h)
135 return rffi.cast(rffi.VOIDP, space.int_w(w_long, allow_conversion=False))
136
137 @API.func("double HPyLong_AsDouble(HPyContext *ctx, HPy h)",
138 error_value=-1.0)
139 def HPyLong_AsDouble(space, handles, ctx, h):
140 w_long = handles.deref(h)
141 return space.float_w(w_long)