You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/user/Interoperability.md
+167-64
Original file line number
Diff line number
Diff line change
@@ -249,8 +249,174 @@ An example in this sense are the `numpy` numeric types (for example, `numpy.int3
249
249
| register_interop_behavior | Takes the receiver **type** as first argument. The remainder keyword arguments correspond to the respective interop messages. Not All interop messages are supported. |
250
250
| get_registered_interop_behavior | Takes the receiver **type** as first argument. Returns the list of extended interop messages for the given type. |
251
251
| @interop_behavior | Class decorator, takes the receiver **type** as only argument. The interop messages are extended via **static** methods defined in the decorated class (supplier). |
252
+
| register_interop_type | Takes a `foreign class` and `python class` as positional arguments and `allow_method_overwrites` as optional argument (default: `False`). Every instance of foreign class is then treated as an instance of the given python class. |
253
+
| @interop_type | Class decorator, takes the `foreign class` and optionally `allow_method_overwrites` as arguments. The instances of foreign class will be treated as an instance of the annotated python class. |
252
254
253
-
#### Supported messages
255
+
### Usage Examples
256
+
257
+
#### Interop Behavior
258
+
259
+
A simple `register_interop_behavior` API is available to register interop behaviors for existing types:
260
+
261
+
```python
262
+
import polyglot
263
+
import numpy
264
+
265
+
polyglot.register_interop_behavior(numpy.int32,
266
+
is_number=True,
267
+
fitsInByte=lambda v: -128 <= v < 128,
268
+
fitsInShort=lambda v: -0x8000 <= v < 0x8000,
269
+
fitsInInt = True,
270
+
fitsInLong = True,
271
+
fitsInBigInteger = True,
272
+
asByte = int,
273
+
asShort = int,
274
+
asInt = int,
275
+
asLong = int,
276
+
asBigInteger = int,
277
+
)
278
+
```
279
+
280
+
The `@interop_behavior` decorator may be more convenient when declaring more behaviors.
281
+
Interop message extension is achieved via **static** methods of the decorated class.
282
+
The names of the static methods are identical to the keyword names expected by `register_interop_behavior`.
283
+
284
+
```python
285
+
from polyglot import interop_behavior
286
+
import numpy
287
+
288
+
289
+
@interop_behavior(numpy.float64)
290
+
class Int8InteropBehaviorSupplier:
291
+
@staticmethod
292
+
def is_number(_):
293
+
return True
294
+
295
+
@staticmethod
296
+
def fitsInDouble(_):
297
+
return True
298
+
299
+
@staticmethod
300
+
def asDouble(v):
301
+
return float(v)
302
+
```
303
+
304
+
Both classes can then behave as expected when embedded:
The majority (with some exceptions) of the interop messages are supported by the interop behavior extension API, as shown in the table below.
256
422
The naming convention for the `register_interop_behavior` keyword arguments follows the _snake_case_ naming convention, i.e. the interop `fitsInLong` message
@@ -314,66 +480,3 @@ The table below describes the supported interop messages:
0 commit comments