From 669682f026803c55f4b0c5fcd612520110e83c54 Mon Sep 17 00:00:00 2001 From: Joshua James Venter Date: Fri, 4 Oct 2024 02:49:50 +0200 Subject: [PATCH] squash and merge Signed-off-by: Joshua James Venter --- stdlib/src/python/_bindings.mojo | 6 +- stdlib/src/python/_cpython.mojo | 14 +-- stdlib/src/python/python.mojo | 25 +++-- stdlib/src/python/python_object.mojo | 150 ++++++++++++++------------- 4 files changed, 105 insertions(+), 90 deletions(-) diff --git a/stdlib/src/python/_bindings.mojo b/stdlib/src/python/_bindings.mojo index 4046ca5f50..f2756b094a 100644 --- a/stdlib/src/python/_bindings.mojo +++ b/stdlib/src/python/_bindings.mojo @@ -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( diff --git a/stdlib/src/python/_cpython.mojo b/stdlib/src/python/_cpython.mojo index ef437a3d06..934b750060 100644 --- a/stdlib/src/python/_cpython.mojo +++ b/stdlib/src/python/_cpython.mojo @@ -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 @@ -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 @@ -1775,7 +1775,9 @@ 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). """ @@ -1783,7 +1785,7 @@ struct CPython: 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 # ===-------------------------------------------------------------------===# diff --git a/stdlib/src/python/python.mojo b/stdlib/src/python/python.mojo index 990daaf583..9c6b03ad3e 100644 --- a/stdlib/src/python/python.mojo +++ b/stdlib/src/python/python.mojo @@ -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 @@ -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) @@ -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) @@ -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, ) @@ -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: @@ -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: @@ -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: @@ -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: @@ -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 diff --git a/stdlib/src/python/python_object.mojo b/stdlib/src/python/python_object.mojo index 151336f510..576f3f0ba0 100644 --- a/stdlib/src/python/python_object.mojo +++ b/stdlib/src/python/python_object.mojo @@ -69,7 +69,7 @@ struct _PyIter(Sized): """ var cpython = _get_global_python_itf().cpython() self.iterator = iter - var maybeNextItem = cpython.PyIter_Next(self.iterator.py_object) + var maybeNextItem = cpython.PyIter_Next(self.iterator.py_object_ptr) if maybeNextItem.is_null(): self.isDone = True self.preparedNextItem = PythonObject(PyObjectPtr()) @@ -98,7 +98,7 @@ struct _PyIter(Sized): return self.iterator var cpython = _get_global_python_itf().cpython() var current = self.preparedNextItem - var maybeNextItem = cpython.PyIter_Next(self.iterator.py_object) + var maybeNextItem = cpython.PyIter_Next(self.iterator.py_object_ptr) if maybeNextItem.is_null(): self.isDone = True else: @@ -183,7 +183,7 @@ struct TypedPythonObject[type_hint: StringLiteral]( # TODO: # This should have origin, or we should do this with a context # manager, to prevent use after ASAP destruction. - fn unsafe_as_py_object_ptr(self) -> PyObjectPtr: + fn unsafe_py_object_ptr(self) -> PyObjectPtr: """Get the underlying PyObject pointer. Returns: @@ -193,7 +193,7 @@ struct TypedPythonObject[type_hint: StringLiteral]( Use-after-free: The caller must take care that `self` outlives the usage of the pointer returned by this function. """ - return self._obj.unsafe_as_py_object_ptr() + return self._obj.unsafe_py_object_ptr() # ===-------------------------------------------------------------------===# # 'Tuple' Operations @@ -214,7 +214,7 @@ struct TypedPythonObject[type_hint: StringLiteral]( var cpython = _get_global_python_itf().cpython() var item: PyObjectPtr = cpython.PyTuple_GetItem( - self.unsafe_as_py_object_ptr(), + self.unsafe_py_object_ptr(), pos, ) @@ -243,7 +243,7 @@ struct PythonObject( # Fields # ===-------------------------------------------------------------------===# - var py_object: PyObjectPtr + var py_object_ptr: PyObjectPtr """A pointer to the underlying Python object.""" # ===-------------------------------------------------------------------===# @@ -272,7 +272,7 @@ struct PythonObject( Args: ptr: The `PyObjectPtr` to take ownership of. """ - self.py_object = ptr + self.py_object_ptr = ptr @staticmethod fn from_borrowed_ptr(borrowed_ptr: PyObjectPtr) -> Self: @@ -350,8 +350,8 @@ struct PythonObject( none: None. """ cpython = _get_global_python_itf().cpython() - self.py_object = cpython.Py_None() - cpython.Py_IncRef(self.py_object) + self.py_object_ptr = cpython.Py_None() + cpython.Py_IncRef(self.py_object_ptr) @implicit fn __init__(out self, value: Bool): @@ -361,7 +361,7 @@ struct PythonObject( value: The boolean value. """ cpython = _get_global_python_itf().cpython() - self.py_object = cpython.PyBool_FromLong(int(value)) + self.py_object_ptr = cpython.PyBool_FromLong(int(value)) @implicit fn __init__(out self, integer: Int): @@ -371,7 +371,7 @@ struct PythonObject( integer: The integer value. """ cpython = _get_global_python_itf().cpython() - self.py_object = cpython.PyLong_FromSsize_t(integer) + self.py_object_ptr = cpython.PyLong_FromSsize_t(integer) @implicit fn __init__[dt: DType](inout self, value: SIMD[dt, 1]): @@ -389,13 +389,13 @@ struct PythonObject( @parameter if dt is DType.bool: - self.py_object = cpython.PyBool_FromLong(int(value)) + self.py_object_ptr = cpython.PyBool_FromLong(int(value)) elif dt.is_integral(): int_val = value.cast[DType.index]().value - self.py_object = cpython.PyLong_FromSsize_t(int_val) + self.py_object_ptr = cpython.PyLong_FromSsize_t(int_val) else: fp_val = value.cast[DType.float64]() - self.py_object = cpython.PyFloat_FromDouble(fp_val) + self.py_object_ptr = cpython.PyFloat_FromDouble(fp_val) @implicit fn __init__(out self, value: StringLiteral): @@ -414,7 +414,7 @@ struct PythonObject( strref: The string reference. """ cpython = _get_global_python_itf().cpython() - self.py_object = cpython.PyUnicode_DecodeUTF8(strref) + self.py_object_ptr = cpython.PyUnicode_DecodeUTF8(strref) @implicit fn __init__(out self, string: String): @@ -424,7 +424,9 @@ struct PythonObject( string: The string value. """ cpython = _get_global_python_itf().cpython() - self.py_object = cpython.PyUnicode_DecodeUTF8(string.as_string_slice()) + self.py_object_ptr = cpython.PyUnicode_DecodeUTF8( + string.as_string_slice() + ) @implicit fn __init__[*Ts: CollectionElement](inout self, value: ListLiteral[*Ts]): @@ -437,7 +439,7 @@ struct PythonObject( value: The list value. """ var cpython = _get_global_python_itf().cpython() - self.py_object = cpython.PyList_New(len(value)) + self.py_object_ptr = cpython.PyList_New(len(value)) @parameter for i in range(len(VariadicList(Ts))): @@ -466,8 +468,8 @@ struct PythonObject( False, "cannot convert list element to python object" ]() - cpython.Py_IncRef(obj.py_object) - _ = cpython.PyList_SetItem(self.py_object, i, obj.py_object) + cpython.Py_IncRef(obj.py_object_ptr) + _ = cpython.PyList_SetItem(self.py_object_ptr, i, obj.py_object_ptr) @implicit fn __init__[*Ts: CollectionElement](inout self, value: Tuple[*Ts]): @@ -481,7 +483,7 @@ struct PythonObject( """ var cpython = _get_global_python_itf().cpython() alias length = len(VariadicList(Ts)) - self.py_object = cpython.PyTuple_New(length) + self.py_object_ptr = cpython.PyTuple_New(length) @parameter for i in range(length): @@ -510,8 +512,10 @@ struct PythonObject( False, "cannot convert list element to python object" ]() - cpython.Py_IncRef(obj.py_object) - _ = cpython.PyTuple_SetItem(self.py_object, i, obj.py_object) + cpython.Py_IncRef(obj.py_object_ptr) + _ = cpython.PyTuple_SetItem( + self.py_object_ptr, i, obj.py_object_ptr + ) @implicit fn __init__(out self, slice: Slice): @@ -520,7 +524,7 @@ struct PythonObject( Args: slice: The dictionary value. """ - self.py_object = _slice_to_py_object_ptr(slice) + self.py_object_ptr = _slice_to_py_object_ptr(slice) @implicit fn __init__(out self, value: Dict[Self, Self]): @@ -530,10 +534,12 @@ struct PythonObject( value: The dictionary value. """ var cpython = _get_global_python_itf().cpython() - self.py_object = cpython.PyDict_New() + self.py_object_ptr = cpython.PyDict_New() for entry in value.items(): var result = cpython.PyDict_SetItem( - self.py_object, entry[].key.py_object, entry[].value.py_object + self.py_object_ptr, + entry[].key.py_object_ptr, + entry[].value.py_object_ptr, ) fn __copyinit__(out self, existing: Self): @@ -544,9 +550,9 @@ struct PythonObject( Args: existing: The value to copy. """ - self.py_object = existing.py_object + self.py_object_ptr = existing.py_object_ptr var cpython = _get_global_python_itf().cpython() - cpython.Py_IncRef(self.py_object) + cpython.Py_IncRef(self.py_object_ptr) fn __del__(owned self): """Destroy the object. @@ -557,9 +563,9 @@ struct PythonObject( # Acquire GIL such that __del__ can be called safely for cases where the # PyObject is handled in non-python contexts. var state = cpython.PyGILState_Ensure() - if not self.py_object.is_null(): - cpython.Py_DecRef(self.py_object) - self.py_object = PyObjectPtr() + if not self.py_object_ptr.is_null(): + cpython.Py_DecRef(self.py_object_ptr) + self.py_object_ptr = PyObjectPtr() cpython.PyGILState_Release(state) # ===-------------------------------------------------------------------===# @@ -576,7 +582,7 @@ struct PythonObject( If the object is not iterable. """ var cpython = _get_global_python_itf().cpython() - var iter = cpython.PyObject_GetIter(self.py_object) + var iter = cpython.PyObject_GetIter(self.py_object_ptr) Python.throw_python_exception_if_error_state(cpython) return _PyIter(PythonObject(iter)) @@ -590,7 +596,7 @@ struct PythonObject( The value of the object attribute with the given name. """ var cpython = _get_global_python_itf().cpython() - var result = cpython.PyObject_GetAttrString(self.py_object, name) + var result = cpython.PyObject_GetAttrString(self.py_object_ptr, name) Python.throw_python_exception_if_error_state(cpython) if result.is_null(): raise Error("Attribute is not found.") @@ -603,12 +609,12 @@ struct PythonObject( name: The name of the object attribute to set. newValue: The new value to be set for that attribute. """ - return self._setattr(name, newValue.py_object) + return self._setattr(name, newValue.py_object_ptr) fn _setattr(self, name: StringLiteral, newValue: PyObjectPtr) raises: var cpython = _get_global_python_itf().cpython() var result = cpython.PyObject_SetAttrString( - self.py_object, name, newValue + self.py_object_ptr, name, newValue ) Python.throw_python_exception_if_error_state(cpython) if result < 0: @@ -621,7 +627,7 @@ struct PythonObject( Whether the object evaluates as true. """ var cpython = _get_global_python_itf().cpython() - return cpython.PyObject_IsTrue(self.py_object) == 1 + return cpython.PyObject_IsTrue(self.py_object_ptr) == 1 @always_inline fn __as_bool__(self) -> Bool: @@ -643,7 +649,7 @@ struct PythonObject( True if they are the same object and False otherwise. """ var cpython = _get_global_python_itf().cpython() - return cpython.Py_Is(self.py_object, other.py_object) + return cpython.Py_Is(self.py_object_ptr, other.py_object_ptr) fn __isnot__(self, other: PythonObject) -> Bool: """Test if the PythonObject is not the `other` PythonObject, the same as `x is not y` in @@ -670,18 +676,18 @@ struct PythonObject( var size = len(args) var key_obj: PyObjectPtr if size == 1: - key_obj = args[0].py_object + key_obj = args[0].py_object_ptr else: key_obj = cpython.PyTuple_New(size) for i in range(size): - var arg_value = args[i].py_object + var arg_value = args[i].py_object_ptr cpython.Py_IncRef(arg_value) var result = cpython.PyTuple_SetItem(key_obj, i, arg_value) if result != 0: raise Error("internal error: PyTuple_SetItem failed") cpython.Py_IncRef(key_obj) - var result = cpython.PyObject_GetItem(self.py_object, key_obj) + var result = cpython.PyObject_GetItem(self.py_object_ptr, key_obj) cpython.Py_DecRef(key_obj) Python.throw_python_exception_if_error_state(cpython) return PythonObject(result) @@ -710,7 +716,7 @@ struct PythonObject( raise Error("internal error: PyTuple_SetItem failed") cpython.Py_IncRef(key_obj) - var result = cpython.PyObject_GetItem(self.py_object, key_obj) + var result = cpython.PyObject_GetItem(self.py_object_ptr, key_obj) cpython.Py_DecRef(key_obj) Python.throw_python_exception_if_error_state(cpython) return PythonObject(result) @@ -727,25 +733,25 @@ struct PythonObject( var key_obj: PyObjectPtr if size == 1: - key_obj = args[0].py_object + key_obj = args[0].py_object_ptr else: key_obj = cpython.PyTuple_New(size) for i in range(size): - var arg_value = args[i].py_object + var arg_value = args[i].py_object_ptr cpython.Py_IncRef(arg_value) var result = cpython.PyTuple_SetItem(key_obj, i, arg_value) if result != 0: raise Error("internal error: PyTuple_SetItem failed") cpython.Py_IncRef(key_obj) - cpython.Py_IncRef(value.py_object) + cpython.Py_IncRef(value.py_object_ptr) var result = cpython.PyObject_SetItem( - self.py_object, key_obj, value.py_object + self.py_object_ptr, key_obj, value.py_object_ptr ) if result != 0: Python.throw_python_exception_if_error_state(cpython) cpython.Py_DecRef(key_obj) - cpython.Py_DecRef(value.py_object) + cpython.Py_DecRef(value.py_object_ptr) fn _call_zero_arg_method( self, method_name: StringRef @@ -753,7 +759,7 @@ struct PythonObject( var cpython = _get_global_python_itf().cpython() var tuple_obj = cpython.PyTuple_New(0) var callable_obj = cpython.PyObject_GetAttrString( - self.py_object, method_name + self.py_object_ptr, method_name ) if callable_obj.is_null(): raise Error("internal error: PyObject_GetAttrString failed") @@ -767,12 +773,12 @@ struct PythonObject( ) raises -> PythonObject: var cpython = _get_global_python_itf().cpython() var tuple_obj = cpython.PyTuple_New(1) - var result = cpython.PyTuple_SetItem(tuple_obj, 0, rhs.py_object) + var result = cpython.PyTuple_SetItem(tuple_obj, 0, rhs.py_object_ptr) if result != 0: raise Error("internal error: PyTuple_SetItem failed") - cpython.Py_IncRef(rhs.py_object) + cpython.Py_IncRef(rhs.py_object_ptr) var callable_obj = cpython.PyObject_GetAttrString( - self.py_object, method_name + self.py_object_ptr, method_name ) if callable_obj.is_null(): raise Error("internal error: PyObject_GetAttrString failed") @@ -786,22 +792,24 @@ struct PythonObject( ) raises: var cpython = _get_global_python_itf().cpython() var tuple_obj = cpython.PyTuple_New(1) - var result = cpython.PyTuple_SetItem(tuple_obj, 0, rhs.py_object) + var result = cpython.PyTuple_SetItem(tuple_obj, 0, rhs.py_object_ptr) if result != 0: raise Error("internal error: PyTuple_SetItem failed") - cpython.Py_IncRef(rhs.py_object) + cpython.Py_IncRef(rhs.py_object_ptr) var callable_obj = cpython.PyObject_GetAttrString( - self.py_object, method_name + self.py_object_ptr, method_name ) if callable_obj.is_null(): raise Error("internal error: PyObject_GetAttrString failed") # Destroy previously stored pyobject - if not self.py_object.is_null(): - cpython.Py_DecRef(self.py_object) + if not self.py_object_ptr.is_null(): + cpython.Py_DecRef(self.py_object_ptr) - self.py_object = cpython.PyObject_CallObject(callable_obj, tuple_obj) + self.py_object_ptr = cpython.PyObject_CallObject( + callable_obj, tuple_obj + ) cpython.Py_DecRef(tuple_obj) cpython.Py_DecRef(callable_obj) @@ -1347,7 +1355,7 @@ struct PythonObject( var num_pos_args = len(args) var tuple_obj = cpython.PyTuple_New(num_pos_args) for i in range(num_pos_args): - var arg_value = args[i].py_object + var arg_value = args[i].py_object_ptr cpython.Py_IncRef(arg_value) var result = cpython.PyTuple_SetItem(tuple_obj, i, arg_value) if result != 0: @@ -1359,12 +1367,12 @@ struct PythonObject( entry[].key.as_string_slice() ) var result = cpython.PyDict_SetItem( - dict_obj, key, entry[].value.py_object + dict_obj, key, entry[].value.py_object_ptr ) if result != 0: raise Error("internal error: PyDict_SetItem failed") - var callable_obj = self.py_object + var callable_obj = self.py_object_ptr cpython.Py_IncRef(callable_obj) var result = cpython.PyObject_Call(callable_obj, tuple_obj, dict_obj) cpython.Py_DecRef(callable_obj) @@ -1393,7 +1401,7 @@ struct PythonObject( The length of the object. """ var cpython = _get_global_python_itf().cpython() - var result = cpython.PyObject_Length(self.py_object) + var result = cpython.PyObject_Length(self.py_object_ptr) if result == -1: # TODO: Improve error message so we say # "object of type 'int' has no len()" function to match Python @@ -1407,7 +1415,7 @@ struct PythonObject( The length of the object. """ var cpython = _get_global_python_itf().cpython() - var result = cpython.PyObject_Length(self.py_object) + var result = cpython.PyObject_Length(self.py_object_ptr) # TODO: make this function raise when we can raise parametrically. debug_assert(result != -1, "object is not hashable") return result @@ -1422,7 +1430,7 @@ struct PythonObject( hasher: The hasher instance. """ var cpython = _get_global_python_itf().cpython() - var result = cpython.PyObject_Hash(self.py_object) + var result = cpython.PyObject_Hash(self.py_object_ptr) # TODO: make this function raise when we can raise parametrically. debug_assert(result != -1, "object is not hashable") hasher.update(result) @@ -1442,7 +1450,7 @@ struct PythonObject( An integral value that represents this object. """ cpython = _get_global_python_itf().cpython() - return cpython.PyLong_AsSsize_t(self.py_object) + return cpython.PyLong_AsSsize_t(self.py_object_ptr) fn __float__(self) -> Float64: """Returns a float representation of the object. @@ -1451,7 +1459,7 @@ struct PythonObject( A floating point value that represents this object. """ cpython = _get_global_python_itf().cpython() - return cpython.PyFloat_AsDouble(self.py_object) + return cpython.PyFloat_AsDouble(self.py_object_ptr) @deprecated("Use `float(obj)` instead.") fn to_float64(self) -> Float64: @@ -1471,10 +1479,10 @@ struct PythonObject( A string that represents this object. """ var cpython = _get_global_python_itf().cpython() - var python_str: PythonObject = cpython.PyObject_Str(self.py_object) + var python_str: PythonObject = cpython.PyObject_Str(self.py_object_ptr) # copy the string var mojo_str = String( - cpython.PyUnicode_AsUTF8AndSize(python_str.py_object) + cpython.PyUnicode_AsUTF8AndSize(python_str.py_object_ptr) ) # keep python object alive so the copy can occur _ = python_str @@ -1498,7 +1506,7 @@ struct PythonObject( # Methods # ===-------------------------------------------------------------------===# - fn unsafe_as_py_object_ptr(self) -> PyObjectPtr: + fn unsafe_py_object_ptr(self) -> PyObjectPtr: """Get the underlying PyObject pointer. Returns: @@ -1508,7 +1516,7 @@ struct PythonObject( Use-after-free: The caller must take care that `self` outlives the usage of the pointer returned by this function. """ - return self.py_object + return self.py_object_ptr fn steal_data(owned self) -> PyObjectPtr: """Take ownership of the underlying pointer from the Python object. @@ -1516,8 +1524,8 @@ struct PythonObject( Returns: The underlying data. """ - var ptr = self.py_object - self.py_object = PyObjectPtr() + var ptr = self.py_object_ptr + self.py_object_ptr = PyObjectPtr() return ptr @@ -1542,12 +1550,12 @@ struct PythonObject( return result fn _get_ptr_as_int(self) -> Int: - return self.py_object._get_ptr_as_int() + return self.py_object_ptr._get_ptr_as_int() fn _get_type_name(self) -> String: var cpython = Python().impl.cpython() - var actual_type = cpython.Py_TYPE(self.unsafe_as_py_object_ptr()) + var actual_type = cpython.Py_TYPE(self.unsafe_py_object_ptr()) var actual_type_name = PythonObject(cpython.PyType_GetName(actual_type)) return str(actual_type_name)