Skip to content
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
4 changes: 2 additions & 2 deletions fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <locale.h>

#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
#if defined(ON_CPYTHON) && PY_VERSION_HEX >= 0x030D00A0

static void
unknown_presentation_type(Py_UCS4 presentation_type, PyObject* type_name)
Expand Down Expand Up @@ -1167,4 +1167,4 @@ __format__(PyObject *self, PyObject *format_spec)
Py_DECREF(integer);
return res;
}
#endif /* !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON) */
#endif /* defined(ON_CPYTHON) && PY_VERSION_HEX >= 0x030D00A0 */
69 changes: 15 additions & 54 deletions gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
#include <stdlib.h>
#include <string.h>

#define ON_CPYTHON 1
#if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
# undef ON_CPYTHON
#endif

#ifdef ON_CPYTHON
# define MAX_FREELIST_SIZE 100
# define MAX_FREELIST_SIZEOF 256
Expand Down Expand Up @@ -188,8 +183,7 @@ MPZ_from_str(PyObject *obj, int base)
static MPZ_Object *
MPZ_from_int(PyObject *obj)
{
#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON) \
&& !defined(Py_LIMITED_API)
#if defined(ON_CPYTHON) && !defined(Py_LIMITED_API)
PyLongExport long_export = {0, 0, 0, 0, 0};
const zz_layout *int_layout = (zz_layout *)PyLong_GetNativeLayout();
MPZ_Object *res = NULL;
Expand Down Expand Up @@ -251,7 +245,7 @@ MPZ_from_int(PyObject *obj)

Py_DECREF(str);
return res;
#endif
#endif /* defined(ON_CPYTHON) && !defined(Py_LIMITED_API) */
}

static PyObject *
Expand All @@ -263,8 +257,7 @@ MPZ_to_int(MPZ_Object *u)
return PyLong_FromInt64(value);
}

#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON) \
&& !defined(Py_LIMITED_API)
#if defined(ON_CPYTHON) && !defined(Py_LIMITED_API)
const zz_layout *int_layout = (zz_layout *)PyLong_GetNativeLayout();
size_t size = (zz_bitlen(&u->z) + int_layout->bits_per_digit
- 1)/int_layout->bits_per_digit;
Expand Down Expand Up @@ -296,7 +289,7 @@ MPZ_to_int(MPZ_Object *u)

free(buf);
return res;
#endif
#endif /* defined(ON_CPYTHON) && !defined(Py_LIMITED_API) */
}

static void
Expand Down Expand Up @@ -506,46 +499,14 @@ new_impl(PyTypeObject *Py_UNUSED(type), PyObject *arg, PyObject *base_arg)
return Py_NewRef(arg);
}
if (PyNumber_Check(arg)) {
PyObject *integer = NULL;
unaryfunc nb_int = PyType_GetSlot(Py_TYPE(arg), Py_nb_int);
PyObject *integer = PyNumber_Long(arg);
PyObject *mpz = NULL;

if (nb_int) {
integer = nb_int(arg);
if (!integer) {
return NULL;
}
if (!PyLong_Check(integer)) {
PyErr_Format(PyExc_TypeError,
"__int__ returned non-int (type %U)",
PyType_GetFullyQualifiedName(Py_TYPE(integer)));
Py_XDECREF(integer);
return NULL;
}
if (!PyLong_CheckExact(integer)
&& PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
"__int__ returned non-int (type %U). "
"The ability to return an instance of a "
"strict subclass of int "
"is deprecated, and may be removed "
"in a future version of Python.",
PyType_GetFullyQualifiedName(Py_TYPE(integer))))
{
Py_XDECREF(integer);
return NULL;
}
}
else {
integer = PyNumber_Index(arg);
if (!integer) {
return NULL;
}
}
if (integer) {
PyObject *mpz = (PyObject *)MPZ_from_int(integer);

mpz = (PyObject *)MPZ_from_int(integer);
Py_DECREF(integer);
return (PyObject *)mpz;
}
return mpz;
}
goto str;
}
Expand Down Expand Up @@ -2055,8 +2016,8 @@ gmp_gcdext(PyObject *Py_UNUSED(module), PyObject *const *args,

zz_err ret = zz_gcdext(&x->z, &y->z, &g->z, &s->z, &t->z);

Py_XDECREF((PyObject *)x);
Py_XDECREF((PyObject *)y);
Py_DECREF(x);
Py_DECREF(y);
if (ret == ZZ_MEM) {
return PyErr_NoMemory(); /* LCOV_EXCL_LINE */
}
Expand Down Expand Up @@ -2200,7 +2161,7 @@ gmp_fac(PyObject *Py_UNUSED(module), PyObject *arg)
ULONG_MAX);
goto err;
}
Py_XDECREF((PyObject *)x);
Py_DECREF((PyObject *)x);

zz_err ret = zz_fac((zz_digit_t)n, &res->z);

Expand Down Expand Up @@ -2250,8 +2211,8 @@ gmp_comb(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
ULONG_MAX);
goto err;
}
Py_XDECREF((PyObject *)x);
Py_XDECREF((PyObject *)y);
Py_DECREF((PyObject *)x);
Py_DECREF((PyObject *)y);

zz_err ret = zz_bin(n, k, &res->z);

Expand Down Expand Up @@ -2304,8 +2265,8 @@ gmp_perm(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
ULONG_MAX);
goto err;
}
Py_XDECREF((PyObject *)x);
Py_XDECREF((PyObject *)y);
Py_DECREF((PyObject *)x);
Py_DECREF((PyObject *)y);
if (k > n) {
return (PyObject *)res;
}
Expand Down
49 changes: 1 addition & 48 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,7 @@ gmp_PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode)
}

Py_UCS1 *out = PyUnicode_1BYTE_DATA(result);
#if defined(__GNUC__) || defined(__clang__)
# pragma GCC diagnostic push /* XXX: oracle/graalpython#580 */
# pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
int kind = PyUnicode_KIND(unicode);
#if defined(__GNUC__) || defined(__clang__)
# pragma GCC diagnostic pop
#endif
int kind = (int)PyUnicode_KIND(unicode); /* oracle/graalpython#580 */
const void *data = PyUnicode_DATA(unicode);

for (Py_ssize_t i = 0; i < len; ++i) {
Expand All @@ -112,43 +105,3 @@ gmp_PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode)
}
return result;
}

#if PY_VERSION_HEX < 0x030D00A0
static PyObject *
PyType_GetModuleName(PyTypeObject *type)
{
return PyObject_GetAttrString((PyObject *)type, "__module__");
}

PyObject *
_PyType_GetFullyQualifiedName(PyTypeObject *type)
{
PyObject *qualname = PyType_GetQualName(type);
if (qualname == NULL) {
return NULL; /* LCOV_EXCL_LINE */
}

PyObject *module = PyType_GetModuleName(type);
if (module == NULL) {
/* LCOV_EXCL_START */
Py_DECREF(qualname);
return NULL;
/* LCOV_EXCL_STOP */
}

PyObject *result;

if (PyUnicode_Check(module)
&& !PyUnicode_EqualToUTF8(module, "builtins")
&& !PyUnicode_EqualToUTF8(module, "__main__"))
{
result = PyUnicode_FromFormat("%U.%U", module, qualname);
}
else {
result = Py_NewRef(qualname);
}
Py_XDECREF(module);
Py_XDECREF(qualname);
return result;
}
#endif
10 changes: 5 additions & 5 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
# pragma GCC diagnostic pop
#endif

#define ON_CPYTHON 1
#if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
# undef ON_CPYTHON
#endif

typedef struct gmp_pyargs {
Py_ssize_t maxpos;
Py_ssize_t minargs;
Expand All @@ -31,9 +36,4 @@ int gmp_parse_pyargs(const gmp_pyargs *fnargs, Py_ssize_t argidx[],

PyObject * gmp_PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode);

#if PY_VERSION_HEX < 0x030D00A0
extern PyObject * _PyType_GetFullyQualifiedName(PyTypeObject *type);
#define PyType_GetFullyQualifiedName _PyType_GetFullyQualifiedName
#endif

#endif /* UTILS_H */