-
Notifications
You must be signed in to change notification settings - Fork 1
Closed
Description
Add 8 new functions to the limited C API to convert C <stdint.h> numbers from/to Python int:
// Return a Python int on success.
// Set an exception and return NULL on error.
PyAPI_FUNC(PyObject*) PyLong_FromInt32(int32_t value);
PyAPI_FUNC(PyObject*) PyLong_FromUInt32(uint32_t value);
PyAPI_FUNC(PyObject*) PyLong_FromInt64(int64_t value);
PyAPI_FUNC(PyObject*) PyLong_FromUInt64(uint64_t value);
// Set *value and return 0 on success.
// Set an exception and return -1 on error.
PyAPI_FUNC(int) PyLong_AsInt32(PyObject *obj, int32_t *value);
PyAPI_FUNC(int) PyLong_AsUInt32(PyObject *obj, uint32_t *value);
PyAPI_FUNC(int) PyLong_AsInt64(PyObject *obj, int64_t *value);
PyAPI_FUNC(int) PyLong_AsUInt64(PyObject *obj, uint64_t *value);UPDATE 1: I removed PyLong_FromInt32() and PyLong_FromUInt32() functions.
UPDATE 2: I renamed To functions to As functions.
UPDATE 3: I added back PyLong_FromInt32() and PyLong_FromUInt32() functions for 32-bit systems.
Asfunctions callobj.__index__()if obj is not a Python int.Asfunctions converting to unsigned integer raises ValueError if value is negativeAsfunctions raise OverflowError if the value is too big (doesn't fit into the C integer range)
Compared to PyLong_AsLong(), As function return value is status (success or error, 0 or -1) and the result is set in the value argument (on success). It avoids having to call PyErr_Occurred() to distinguish -1 (success) and -1 (error). See Problem #1 for details.
Links:
encukou and erlend-aasland
Metadata
Metadata
Assignees
Labels
No labels