Skip to content

Add PyLong_GetSign() function #99

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

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ Latest version of the header file:
`pythoncapi_compat.h <https://raw.githubusercontent.com/python/pythoncapi-compat/master/pythoncapi_compat.h>`_.


Python 3.14
-----------

.. c:function:: int PyLong_GetSign(PyObject *obj, int *sign)

See `PyLong_GetSign() documentation <https://docs.python.org/dev/c-api/long.html#c.PyLong_GetSign>`__.

Python 3.13
-----------

Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Changelog
=========

* 2024-06-03: Add ``PyLong_GetSign()``.
* 2024-04-23: Drop Python 3.5 support. It cannot be tested anymore (pip fails).
* 2024-04-02: Add ``PyDict_SetDefaultRef()`` function.
* 2024-03-29: Add ``PyList_GetItemRef()`` function.
Expand Down
15 changes: 15 additions & 0 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,21 @@ PyDict_SetDefaultRef(PyObject *d, PyObject *key, PyObject *default_value,
#endif


// gh-116560 added PyLong_GetSign() to Python 3.14a4
#if PY_VERSION_HEX < 0x030E00A1
static inline int PyLong_GetSign(PyObject *obj, int *sign)
{
if (!PyLong_Check(obj)) {
PyErr_Format(PyExc_TypeError, "expect int, got %s", Py_TYPE(obj)->tp_name);
return -1;
}

*sign = _PyLong_Sign(obj);
return 0;
}
#endif


#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 6 additions & 0 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
// Marker to check that pointer value was set
static const char uninitialized[] = "uninitialized";
#define UNINITIALIZED_OBJ ((PyObject *)uninitialized)
#define UNINITIALIZED_INT 0x83ff979


static PyObject*
Expand Down Expand Up @@ -1413,6 +1414,11 @@ test_long_api(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
PyErr_Clear();
Py_DECREF(obj2);

// test PyLong_GetSign()
int sign = UNINITIALIZED_INT;
assert(PyLong_GetSign(obj, &sign) == 0);
assert(sign == 1);

Py_RETURN_NONE;
}

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy