Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua James Venter <[email protected]>
  • Loading branch information
jjvraw committed Oct 16, 2024
1 parent ecffba0 commit dc853ec
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 87 deletions.
4 changes: 2 additions & 2 deletions stdlib/src/python/_bindings.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,14 @@ fn check_argument_type[
instance of the Mojo `T` type.
"""

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

if not opt:
var cpython = _get_global_python_itf().cpython()

var actual_type = cpython.Py_TYPE(obj.unsafe_as_py_object_ptr())
var actual_type = cpython.Py_TYPE(obj.unsafe_py_object_ptr())
var actual_type_name = PythonObject(cpython.PyType_GetName(actual_type))

raise Error(
Expand Down
12 changes: 6 additions & 6 deletions stdlib/src/python/_cpython.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1686,12 +1686,12 @@ struct CPython:
return r

# Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
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:
"""See https://docs.python.org/3/c-api/long.html#c.PyLong_AsSsize_t."""

return self.lib.get_function[fn (PyObjectPtr) -> c_ssize_t](
"PyLong_AsSsize_t"
)(py_object)
)(py_object_ptr)

# Floating-Point Objects
# ref: https://docs.python.org/3/c-api/float.html
Expand All @@ -1717,12 +1717,12 @@ struct CPython:
return r

# double PyFloat_AsDouble(PyObject *pyfloat)
fn PyFloat_AsDouble(inout self, py_object: PyObjectPtr) -> Float64:
fn PyFloat_AsDouble(inout self, py_object_ptr: PyObjectPtr) -> Float64:
"""See https://docs.python.org/3/c-api/float.html#c.PyFloat_AsDouble."""

return self.lib.get_function[fn (PyObjectPtr) -> Float64](
"PyFloat_AsDouble"
)(py_object)
)(py_object_ptr)

# Unicode Objects
# https://docs.python.org/3/c-api/unicode.html
Expand Down Expand Up @@ -1783,15 +1783,15 @@ struct CPython:
return py_slice

# const char *PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *size)
fn PyUnicode_AsUTF8AndSize(inout self, py_object: PyObjectPtr) -> StringRef:
fn PyUnicode_AsUTF8AndSize(inout self, py_object_ptr: PyObjectPtr) -> StringRef:
"""See https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_AsUTF8AndSize.
"""

s = StringRef()
s.data = self.lib.get_function[
fn (PyObjectPtr, UnsafePointer[c_ssize_t]) -> UnsafePointer[c_char]
]("PyUnicode_AsUTF8AndSize")(
py_object, UnsafePointer.address_of(s.length)
py_object_ptr, UnsafePointer.address_of(s.length)
).bitcast[
UInt8
]()
Expand Down
23 changes: 14 additions & 9 deletions stdlib/src/python/python.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,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 @@ -139,7 +139,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 @@ -151,7 +153,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 @@ -291,7 +296,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 @@ -321,9 +326,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 @@ -362,7 +367,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 @@ -413,7 +418,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 @@ -426,7 +431,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
Loading

0 comments on commit dc853ec

Please sign in to comment.