Skip to content

[stdlib] Fix ABI for CPython.PyObject_HasAttrString #4202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions mojo/stdlib/src/python/_cpython.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1396,21 +1396,26 @@ struct CPython:
self,
obj: PyObjectPtr,
owned name: String,
) -> Int:
var r = self.lib.get_function[
fn (PyObjectPtr, UnsafePointer[c_char]) -> Int
]("PyObject_HasAttrString")(obj, name.unsafe_cstr_ptr())
return r
) -> c_int:
"""Returns `1` if `obj` has the attribute `attr_name`, and `0` otherwise.

[Reference](https://docs.python.org/3/c-api/object.html#c.PyObject_HasAttrString).
"""
# int PyObject_HasAttrString(PyObject *o, const char *attr_name)
return self.lib.call["PyObject_HasAttrString", c_int](
obj, name.unsafe_cstr_ptr()
)

fn PyObject_GetAttrString(
self,
obj: PyObjectPtr,
owned name: String,
) -> PyObjectPtr:
"""[Reference](
https://docs.python.org/3/c-api/object.html#c.PyObject_GetAttrString).
"""
"""Retrieve an attribute named `name` from object `obj`.

[Reference](https://docs.python.org/3/c-api/object.html#c.PyObject_GetAttrString).
"""
# PyObject *PyObject_GetAttrString(PyObject *o, const char *attr_name)
var r = self.lib.call["PyObject_GetAttrString", PyObjectPtr](
obj, name.unsafe_cstr_ptr()
)
Expand All @@ -1429,12 +1434,16 @@ struct CPython:
return r

fn PyObject_SetAttrString(
self, obj: PyObjectPtr, owned name: String, new_value: PyObjectPtr
self,
obj: PyObjectPtr,
owned name: String,
new_value: PyObjectPtr,
) -> c_int:
"""[Reference](
https://docs.python.org/3/c-api/object.html#c.PyObject_SetAttrString).
"""
"""Set the value of the attribute named `name`, for object `obj`, to the value `new_value`.

[Reference](https://docs.python.org/3/c-api/object.html#c.PyObject_SetAttrString).
"""
# int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)
var r = self.lib.call["PyObject_SetAttrString", c_int](
obj, name.unsafe_cstr_ptr(), new_value
)
Expand Down