diff --git a/stdlib/src/python/_cpython.mojo b/stdlib/src/python/_cpython.mojo index 02ecb515cd2..269cec91891 100644 --- a/stdlib/src/python/_cpython.mojo +++ b/stdlib/src/python/_cpython.mojo @@ -1323,15 +1323,15 @@ struct CPython: fn toPython(inout self, litBool: Bool) -> PyObjectPtr: return self.PyBool_FromLong(1 if litBool else 0) - fn PyLong_AsLong(inout self, py_object: PyObjectPtr) -> Int: + fn PyLong_AsLong(inout self, py_object_ptr: PyObjectPtr) -> Int: return self.lib.get_function[fn (PyObjectPtr) -> Int]("PyLong_AsLong")( - py_object + py_object_ptr ) - fn PyFloat_AsDouble(inout self, py_object: PyObjectPtr) -> Float64: + fn PyFloat_AsDouble(inout self, py_object_ptr: PyObjectPtr) -> Float64: return self.lib.get_function[fn (PyObjectPtr) -> Float64]( "PyFloat_AsDouble" - )(py_object) + )(py_object_ptr) fn PyFloat_FromDouble(inout self, value: Float64) -> PyObjectPtr: var r = self.lib.get_function[fn (Float64) -> PyObjectPtr]( @@ -1393,10 +1393,12 @@ struct CPython: self._inc_total_rc() return r - fn PyUnicode_AsUTF8AndSize(inout self, py_object: PyObjectPtr) -> StringRef: + fn PyUnicode_AsUTF8AndSize( + inout self, py_object_ptr: PyObjectPtr + ) -> StringRef: var result = self.lib.get_function[ fn (PyObjectPtr, UnsafePointer[Int]) -> UnsafePointer[c_char] - ]("PyUnicode_AsUTF8AndSize")(py_object, UnsafePointer[Int]()) + ]("PyUnicode_AsUTF8AndSize")(py_object_ptr, UnsafePointer[Int]()) return StringRef(result) # ===-------------------------------------------------------------------===# diff --git a/stdlib/src/python/python.mojo b/stdlib/src/python/python.mojo index 0044212d748..196501d5a74 100644 --- a/stdlib/src/python/python.mojo +++ b/stdlib/src/python/python.mojo @@ -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 @@ -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) @@ -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) @@ -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: @@ -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: @@ -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: diff --git a/stdlib/src/python/python_object.mojo b/stdlib/src/python/python_object.mojo index a662df26844..b3d407dda0a 100644 --- a/stdlib/src/python/python_object.mojo +++ b/stdlib/src/python/python_object.mojo @@ -65,7 +65,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()) @@ -94,7 +94,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: @@ -234,7 +234,7 @@ struct PythonObject( # Fields # ===-------------------------------------------------------------------===# - var py_object: PyObjectPtr + var py_object_ptr: PyObjectPtr """A pointer to the underlying Python object.""" # ===-------------------------------------------------------------------===# @@ -262,7 +262,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: @@ -336,8 +336,8 @@ struct PythonObject( none: None. """ var 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) fn __init__(inout self, integer: Int): """Initialize the object with an integer value. @@ -346,7 +346,7 @@ struct PythonObject( integer: The integer value. """ var cpython = _get_global_python_itf().cpython() - self.py_object = cpython.toPython(integer) + self.py_object_ptr = cpython.toPython(integer) fn __init__(inout self, float: Float64): """Initialize the object with an floating-point value. @@ -355,7 +355,7 @@ struct PythonObject( float: The float value. """ var cpython = _get_global_python_itf().cpython() - self.py_object = cpython.PyFloat_FromDouble(float) + self.py_object_ptr = cpython.PyFloat_FromDouble(float) fn __init__[dt: DType](inout self, value: SIMD[dt, 1]): """Initialize the object with a generic scalar value. If the scalar @@ -372,13 +372,13 @@ struct PythonObject( @parameter if dt == DType.bool: - self.py_object = cpython.toPython(value.__bool__()) + self.py_object_ptr = cpython.toPython(value.__bool__()) elif dt.is_integral(): var int_val = value.cast[DType.index]().value - self.py_object = cpython.toPython(int_val) + self.py_object_ptr = cpython.toPython(int_val) else: var fp_val = value.cast[DType.float64]() - self.py_object = cpython.PyFloat_FromDouble(fp_val) + self.py_object_ptr = cpython.PyFloat_FromDouble(fp_val) fn __init__(inout self, value: Bool): """Initialize the object from a bool. @@ -387,7 +387,7 @@ struct PythonObject( value: The boolean value. """ var cpython = _get_global_python_itf().cpython() - self.py_object = cpython.toPython(value) + self.py_object_ptr = cpython.toPython(value) fn __init__(inout self, value: StringLiteral): """Initialize the object from a string literal. @@ -404,7 +404,7 @@ struct PythonObject( strref: The string reference. """ var cpython = _get_global_python_itf().cpython() - self.py_object = cpython.toPython(strref) + self.py_object_ptr = cpython.toPython(strref) fn __init__(inout self, string: String): """Initialize the object from a string. @@ -413,7 +413,7 @@ struct PythonObject( string: The string value. """ var cpython = _get_global_python_itf().cpython() - self.py_object = cpython.toPython(string._strref_dangerous()) + self.py_object_ptr = cpython.toPython(string._strref_dangerous()) string._strref_keepalive() fn __init__[*Ts: CollectionElement](inout self, value: ListLiteral[*Ts]): @@ -426,7 +426,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))): @@ -455,8 +455,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) fn __init__[*Ts: CollectionElement](inout self, value: Tuple[*Ts]): """Initialize the object from a tuple literal. @@ -469,7 +469,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): @@ -498,8 +498,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 + ) fn __init__(inout self, value: Dict[Self, Self]): """Initialize the object from a dictionary of PythonObjects. @@ -508,10 +510,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__(inout self, existing: Self): @@ -522,9 +526,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) # ===-------------------------------------------------------------------===# # Operator dunders @@ -540,7 +544,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)) @@ -558,7 +562,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. @@ -566,8 +570,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 @@ -580,9 +584,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) fn __getattr__(self, name: StringLiteral) raises -> PythonObject: @@ -595,7 +599,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.") @@ -608,12 +612,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: @@ -626,7 +630,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: @@ -648,7 +652,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 @@ -669,7 +673,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 @@ -683,7 +687,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 @@ -701,18 +705,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) @@ -729,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 @@ -755,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") @@ -769,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") @@ -788,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) @@ -1328,7 +1334,7 @@ struct PythonObject( return self._call_zero_arg_method("__invert__") fn _get_ptr_as_int(self) -> Int: - return self.py_object._get_ptr_as_int() + return self.py_object_ptr._get_ptr_as_int() # see https://github.com/python/cpython/blob/main/Objects/call.c # for decrement rules @@ -1352,7 +1358,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: @@ -1362,12 +1368,12 @@ struct PythonObject( for entry in kwargs.items(): var key = cpython.toPython(entry[].key._strref_dangerous()) 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) @@ -1392,7 +1398,7 @@ struct PythonObject( A floating point value that represents this object. """ var cpython = _get_global_python_itf().cpython() - return cpython.PyFloat_AsDouble(self.py_object.value) + return cpython.PyFloat_AsDouble(self.py_object_ptr.value) fn __index__(self) -> Int: """Returns an index representation of the object. @@ -1409,7 +1415,7 @@ struct PythonObject( An integral value that represents this object. """ var cpython = _get_global_python_itf().cpython() - return cpython.PyLong_AsLong(self.py_object.value) + return cpython.PyLong_AsLong(self.py_object_ptr.value) fn unsafe_get_as_pointer[type: DType](self) -> UnsafePointer[Scalar[type]]: """Convert a Python-owned and managed pointer into a Mojo pointer. @@ -1440,10 +1446,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