Skip to content

Commit

Permalink
squash and merge
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua James Venter <[email protected]>
  • Loading branch information
jjvraw committed Nov 19, 2024
1 parent 5277e08 commit 669682f
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 90 deletions.
6 changes: 3 additions & 3 deletions stdlib/src/python/_bindings.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,9 @@ fn check_argument_type[
instance of the Mojo `T` type.
"""

var opt: Optional[UnsafePointer[T]] = obj.py_object.try_cast_to_mojo_value[
T
](type_name_id)
var opt: Optional[
UnsafePointer[T]
] = obj.py_object_ptr.try_cast_to_mojo_value[T](type_name_id)

if not opt:
raise Error(
Expand Down
14 changes: 8 additions & 6 deletions stdlib/src/python/_cpython.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1667,11 +1667,11 @@ struct CPython:
self._inc_total_rc()
return r

fn PyLong_AsSsize_t(inout self, py_object: PyObjectPtr) -> c_ssize_t:
fn PyLong_AsSsize_t(inout self, py_object_ptr: PyObjectPtr) -> c_ssize_t:
"""[Reference](
https://docs.python.org/3/c-api/long.html#c.PyLong_AsSsize_t).
"""
return self.lib.call["PyLong_AsSsize_t", c_ssize_t](py_object)
return self.lib.call["PyLong_AsSsize_t", c_ssize_t](py_object_ptr)

# ===-------------------------------------------------------------------===#
# Floating-Point Objects
Expand All @@ -1695,11 +1695,11 @@ struct CPython:
self._inc_total_rc()
return r

fn PyFloat_AsDouble(inout self, py_object: PyObjectPtr) -> Float64:
fn PyFloat_AsDouble(inout self, py_object_ptr: PyObjectPtr) -> Float64:
"""[Reference](
https://docs.python.org/3/c-api/float.html#c.PyFloat_AsDouble).
"""
return self.lib.call["PyFloat_AsDouble", Float64](py_object)
return self.lib.call["PyFloat_AsDouble", Float64](py_object_ptr)

# ===-------------------------------------------------------------------===#
# Unicode Objects
Expand Down Expand Up @@ -1775,15 +1775,17 @@ struct CPython:

return py_slice

fn PyUnicode_AsUTF8AndSize(inout self, py_object: PyObjectPtr) -> StringRef:
fn PyUnicode_AsUTF8AndSize(
inout self, py_object_ptr: PyObjectPtr
) -> StringRef:
"""[Reference](
https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_AsUTF8AndSize).
"""

var s = StringRef()
s.data = self.lib.call[
"PyUnicode_AsUTF8AndSize", UnsafePointer[c_char]
](py_object, UnsafePointer.address_of(s.length)).bitcast[UInt8]()
](py_object_ptr, UnsafePointer.address_of(s.length)).bitcast[UInt8]()
return s

# ===-------------------------------------------------------------------===#
Expand Down
25 changes: 15 additions & 10 deletions stdlib/src/python/python.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ struct Python:
cpython.PyImport_AddModule(name)
)
var dict_obj = PythonObject.from_borrowed_ptr(
cpython.PyModule_GetDict(module.py_object)
cpython.PyModule_GetDict(module.py_object_ptr)
)
if file:
# We compile the code as provided and execute in the module
Expand All @@ -153,7 +153,9 @@ struct Python:
# the module scope for this eval, they should be the same object.
var result = PythonObject(
cpython.PyEval_EvalCode(
code.py_object, dict_obj.py_object, dict_obj.py_object
code.py_object_ptr,
dict_obj.py_object_ptr,
dict_obj.py_object_ptr,
)
)
Python.throw_python_exception_if_error_state(cpython)
Expand All @@ -165,7 +167,10 @@ struct Python:
# all the globals/locals to be discarded. See above re: why the same
# dictionary is being used here for both globals and locals.
var result = cpython.PyRun_String(
expr, dict_obj.py_object, dict_obj.py_object, Py_eval_input
expr,
dict_obj.py_object_ptr,
dict_obj.py_object_ptr,
Py_eval_input,
)
# We no longer need module and dictionary, release them.
Python.throw_python_exception_if_error_state(cpython)
Expand Down Expand Up @@ -305,7 +310,7 @@ struct Python:
var result = cpython.PyModule_AddFunctions(
# Safety: `module` pointer lives long enough because its reference
# argument.
module.unsafe_as_py_object_ptr(),
module.unsafe_py_object_ptr(),
functions,
)

Expand Down Expand Up @@ -335,9 +340,9 @@ struct Python:
var cpython = _get_global_python_itf().cpython()

var result = cpython.PyModule_AddObjectRef(
module.unsafe_as_py_object_ptr(),
module.unsafe_py_object_ptr(),
name.unsafe_cstr_ptr(),
value.unsafe_as_py_object_ptr(),
value.unsafe_py_object_ptr(),
)

if result != 0:
Expand Down Expand Up @@ -376,7 +381,7 @@ struct Python:
Mojo string representing the given Python object.
"""
var cpython = self.impl.cpython()
return cpython.PyUnicode_AsUTF8AndSize(str_obj.py_object)
return cpython.PyUnicode_AsUTF8AndSize(str_obj.py_object_ptr)

@staticmethod
fn throw_python_exception_if_error_state(inout cpython: CPython) raises:
Expand Down Expand Up @@ -427,7 +432,7 @@ struct Python:
True if `x` and `y` are the same object and False otherwise.
"""
var cpython = _get_global_python_itf().cpython()
return cpython.Py_Is(x.py_object, y.py_object)
return cpython.Py_Is(x.py_object_ptr, y.py_object_ptr)

@staticmethod
fn type(obj: PythonObject) -> PythonObject:
Expand All @@ -440,7 +445,7 @@ struct Python:
A PythonObject that holds the type object.
"""
var cpython = _get_global_python_itf().cpython()
return cpython.PyObject_Type(obj.py_object)
return cpython.PyObject_Type(obj.py_object_ptr)

@staticmethod
fn none() -> PythonObject:
Expand Down Expand Up @@ -472,7 +477,7 @@ struct Python:
var cpython = Python().impl.cpython()

var long: Py_ssize_t = cpython.PyLong_AsSsize_t(
obj.unsafe_as_py_object_ptr()
obj.unsafe_py_object_ptr()
)

# Disambiguate if this is an error return setinel, or a legitimate
Expand Down
Loading

0 comments on commit 669682f

Please sign in to comment.