forked from modular/rules_mojo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_shared_library.mojo
More file actions
29 lines (22 loc) · 851 Bytes
/
python_shared_library.mojo
File metadata and controls
29 lines (22 loc) · 851 Bytes
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
from std.os import abort
from std.python import Python, PythonObject
from std.python.bindings import PythonModuleBuilder
from std.python._cpython import PyObjectPtr
@export
def PyInit_python_shared_library() -> PythonObject:
"""Create a Python module with a function binding for `mojo_count_args`."""
try:
var b = PythonModuleBuilder("python_shared_library")
b.def_py_c_function(
mojo_count_args,
"mojo_count_args",
docstring="Count the provided arguments",
)
return b.finalize()
except e:
abort(String("failed to create Python module: ", e))
@export
def mojo_count_args(py_self: PyObjectPtr, args: PyObjectPtr) -> PyObjectPtr:
ref cpython = Python().cpython()
var count = cpython.PyObject_Length(args)
return cpython.PyLong_FromSsize_t(count)