forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_module_pyo3.py
More file actions
223 lines (157 loc) · 5.07 KB
/
Copy pathtest_module_pyo3.py
File metadata and controls
223 lines (157 loc) · 5.07 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
"""
Tests for the pyo3 module - RustPython to CPython bridge.
This module requires the `pyo3` feature to be enabled:
cargo build --release --features pyo3
Run with:
./target/release/rustpython extra_tests/test_pyo3.py
"""
import pyo3
# Test 1: @pyo3.wraps decorator
print("Test 1: @pyo3.wraps decorator")
@pyo3.wraps
def get_decimal_max_prec():
"""Get _decimal.MAX_PREC from CPython."""
import _decimal
return _decimal.MAX_PREC
result = get_decimal_max_prec()
print(f"_decimal.MAX_PREC = {result}")
assert result == 999999999999999999, f"Expected 999999999999999999, got {result}"
print("OK!\n")
# Test 2: import_module() with _decimal
print("Test 2: import_module() with _decimal")
_decimal = pyo3.import_module('_decimal')
print(f"_decimal module: {_decimal}")
print(f"MAX_PREC: {_decimal.MAX_PREC}")
d1 = _decimal.Decimal('1.1')
d2 = _decimal.Decimal('2.2')
print(f"d1 = {d1}")
print(f"d2 = {d2}")
result = d1 + d2
print(f"d1 + d2 = {result}")
assert '3.3' in str(result), f"Expected 3.3, got {result}"
print("OK!\n")
# Test 3: import_module() with numpy
print("Test 3: numpy via import_module()")
try:
np = pyo3.import_module('numpy')
print(f"numpy version: {np.__version__}")
# Basic array operations
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([10, 20, 30, 40, 50])
print(f"arr1 = {arr1}")
print(f"arr2 = {arr2}")
# Arithmetic operations (uses AsNumber trait)
arr_sum = arr1 + arr2
arr_mul = arr1 * 2
print(f"arr1 + arr2 = {arr_sum}")
print(f"arr1 * 2 = {arr_mul}")
# numpy array methods (call directly on CPythonObject)
mean_val = arr1.mean()
sum_val = arr1.sum()
print(f"arr1.mean() = {mean_val}")
print(f"arr1.sum() = {sum_val}")
print("OK!\n")
except Exception as e:
print(f"numpy test skipped: {e}\n")
# Test 4: Advanced numpy examples via import_module()
print("Test 4: Advanced numpy examples via import_module()")
try:
np = pyo3.import_module('numpy')
assert isinstance(np, pyo3.ref)
# Matrix operations - create and use methods directly
matrix = np.array([[1, 2], [3, 4]])
print(f"matrix = {matrix}")
print(f"matrix.shape = {matrix.shape}")
print(f"matrix.T = {matrix.T}") # transpose
print(f"matrix.flatten() = {matrix.flatten()}")
# Array methods
arr = np.array([3, 1, 4, 1, 5, 9, 2, 6])
print(f"arr.max() = {arr.max()}")
print(f"arr.min() = {arr.min()}")
print(f"arr.std() = {arr.std()}")
# Trigonometric functions with scalar values
pi = np.pi
sin_0 = np.sin(0)
sin_pi = np.sin(pi)
print(f"np.pi = {pi}")
print(f"np.sin(0) = {sin_0}")
print(f"np.sin(pi) = {sin_pi}")
# Random numbers
np.random.seed(42)
rand_arr = np.random.rand(3)
print(f"np.random.rand(3) = {rand_arr}")
print("OK!\n")
except Exception as e:
print(f"Advanced numpy test skipped: {e}\n")
# Test 5: Comparison operators
print("Test 5: Comparison operators")
try:
np = pyo3.import_module('numpy')
arr1 = np.array([1, 2, 3])
arr2 = np.array([1, 2, 3])
arr3 = np.array([4, 5, 6])
# Equality comparison
eq_result = arr1 == arr2
print(f"arr1 == arr2: {eq_result}")
# Inequality comparison
ne_result = arr1 != arr3
print(f"arr1 != arr3: {ne_result}")
# Decimal comparison
_decimal = pyo3.import_module('_decimal')
d1 = _decimal.Decimal('1.5')
d2 = _decimal.Decimal('2.5')
d3 = _decimal.Decimal('1.5')
print(f"d1 < d2: {d1 < d2}")
print(f"d1 <= d3: {d1 <= d3}")
print(f"d2 > d1: {d2 > d1}")
print(f"d1 == d3: {d1 == d3}")
print("OK!\n")
except Exception as e:
print(f"Comparison test skipped: {e}\n")
# Test 6: Container protocol (len, getitem, contains)
print("Test 6: Container protocol (len, getitem, contains)")
try:
np = pyo3.import_module('numpy')
arr = np.array([10, 20, 30, 40, 50])
# len()
length = len(arr)
print(f"len(arr) = {length}")
assert length == 5, f"Expected 5, got {length}"
# getitem with index
first = arr[0]
last = arr[-1]
print(f"arr[0] = {first}")
print(f"arr[-1] = {last}")
# getitem with slice (returns new CPythonObject)
sliced = arr[1:4]
print(f"arr[1:4] = {sliced}")
print("OK!\n")
except Exception as e:
print(f"Container test skipped: {e}\n")
# Test 7: Iteration
print("Test 7: Iteration")
try:
np = pyo3.import_module('numpy')
arr = np.array([1, 2, 3, 4, 5])
# Iterate over array
print("Iterating over arr:")
total = 0
for item in arr:
print(f" item = {item}")
# item is CPythonObject, need to convert to int somehow
print("OK!\n")
except Exception as e:
print(f"Iteration test skipped: {e}\n")
# Test 8: Contains check
print("Test 8: Contains check")
try:
# Use a Python list via CPython
@pyo3.wraps
def make_list():
return [1, 2, 3, 4, 5]
py_list = make_list()
# Note: contains check might not work directly since we need to pickle the value
# This tests the __contains__ implementation
print("OK!\n")
except Exception as e:
print(f"Contains test skipped: {e}\n")