/Modules/testcapi_long.h (6bddad7bb5d249e779fa23a44af5f9194091a37c) (6933 bytes) (mode 100644) (type blob)
/* Poor-man's template. Macros used:
TESTNAME name of the test (like test_long_api_inner)
TYPENAME the signed type (like long)
F_S_TO_PY convert signed to pylong; TYPENAME -> PyObject*
F_PY_TO_S convert pylong to signed; PyObject* -> TYPENAME
F_U_TO_PY convert unsigned to pylong; unsigned TYPENAME -> PyObject*
F_PY_TO_U convert pylong to unsigned; PyObject* -> unsigned TYPENAME
*/
static PyObject *
TESTNAME(PyObject *error(const char*))
{
const int NBITS = sizeof(TYPENAME) * 8;
unsigned TYPENAME base;
PyObject *pyresult;
int i;
/* Note: This test lets PyObjects leak if an error is raised. Since
an error should never be raised, leaks are impossible <wink>. */
/* Test native -> PyLong -> native roundtrip identity.
* Generate all powers of 2, and test them and their negations,
* plus the numbers +-1 off from them.
*/
base = 1;
for (i = 0;
i < NBITS + 1; /* on last, base overflows to 0 */
++i, base <<= 1)
{
int j;
for (j = 0; j < 6; ++j) {
TYPENAME in, out;
unsigned TYPENAME uin, uout;
/* For 0, 1, 2 use base; for 3, 4, 5 use -base */
uin = j < 3 ? base : 0U - base;
/* For 0 & 3, subtract 1.
* For 1 & 4, leave alone.
* For 2 & 5, add 1.
*/
uin += (unsigned TYPENAME)(TYPENAME)(j % 3 - 1);
pyresult = F_U_TO_PY(uin);
if (pyresult == NULL)
return error(
"unsigned unexpected null result");
uout = F_PY_TO_U(pyresult);
if (uout == (unsigned TYPENAME)-1 && PyErr_Occurred())
return error(
"unsigned unexpected -1 result");
if (uout != uin)
return error(
"unsigned output != input");
UNBIND(pyresult);
in = (TYPENAME)uin;
pyresult = F_S_TO_PY(in);
if (pyresult == NULL)
return error(
"signed unexpected null result");
out = F_PY_TO_S(pyresult);
if (out == (TYPENAME)-1 && PyErr_Occurred())
return error(
"signed unexpected -1 result");
if (out != in)
return error(
"signed output != input");
UNBIND(pyresult);
}
}
/* Overflow tests. The loop above ensured that all limit cases that
* should not overflow don't overflow, so all we need to do here is
* provoke one-over-the-limit cases (not exhaustive, but sharp).
*/
{
PyObject *one, *x, *y;
TYPENAME out;
unsigned TYPENAME uout;
one = PyLong_FromLong(1);
if (one == NULL)
return error(
"unexpected NULL from PyLong_FromLong");
/* Unsigned complains about -1? */
x = PyNumber_Negative(one);
if (x == NULL)
return error(
"unexpected NULL from PyNumber_Negative");
uout = F_PY_TO_U(x);
if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
return error(
"PyLong_AsUnsignedXXX(-1) didn't complain");
if (!PyErr_ExceptionMatches(PyExc_OverflowError))
return error(
"PyLong_AsUnsignedXXX(-1) raised "
"something other than OverflowError");
PyErr_Clear();
UNBIND(x);
/* Unsigned complains about 2**NBITS? */
y = PyLong_FromLong((long)NBITS);
if (y == NULL)
return error(
"unexpected NULL from PyLong_FromLong");
x = PyNumber_Lshift(one, y); /* 1L << NBITS, == 2**NBITS */
UNBIND(y);
if (x == NULL)
return error(
"unexpected NULL from PyNumber_Lshift");
uout = F_PY_TO_U(x);
if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
return error(
"PyLong_AsUnsignedXXX(2**NBITS) didn't "
"complain");
if (!PyErr_ExceptionMatches(PyExc_OverflowError))
return error(
"PyLong_AsUnsignedXXX(2**NBITS) raised "
"something other than OverflowError");
PyErr_Clear();
/* Signed complains about 2**(NBITS-1)?
x still has 2**NBITS. */
y = PyNumber_Rshift(x, one); /* 2**(NBITS-1) */
UNBIND(x);
if (y == NULL)
return error(
"unexpected NULL from PyNumber_Rshift");
out = F_PY_TO_S(y);
if (out != (TYPENAME)-1 || !PyErr_Occurred())
return error(
"PyLong_AsXXX(2**(NBITS-1)) didn't "
"complain");
if (!PyErr_ExceptionMatches(PyExc_OverflowError))
return error(
"PyLong_AsXXX(2**(NBITS-1)) raised "
"something other than OverflowError");
PyErr_Clear();
/* Signed complains about -2**(NBITS-1)-1?;
y still has 2**(NBITS-1). */
x = PyNumber_Negative(y); /* -(2**(NBITS-1)) */
UNBIND(y);
if (x == NULL)
return error(
"unexpected NULL from PyNumber_Negative");
y = PyNumber_Subtract(x, one); /* -(2**(NBITS-1))-1 */
UNBIND(x);
if (y == NULL)
return error(
"unexpected NULL from PyNumber_Subtract");
out = F_PY_TO_S(y);
if (out != (TYPENAME)-1 || !PyErr_Occurred())
return error(
"PyLong_AsXXX(-2**(NBITS-1)-1) didn't "
"complain");
if (!PyErr_ExceptionMatches(PyExc_OverflowError))
return error(
"PyLong_AsXXX(-2**(NBITS-1)-1) raised "
"something other than OverflowError");
PyErr_Clear();
UNBIND(y);
Py_XDECREF(x);
Py_XDECREF(y);
Py_DECREF(one);
}
/* Test F_PY_TO_{S,U} on non-pylong input. This should raise a TypeError. */
{
TYPENAME out;
unsigned TYPENAME uout;
Py_INCREF(Py_None);
out = F_PY_TO_S(Py_None);
if (out != (TYPENAME)-1 || !PyErr_Occurred())
return error("PyLong_AsXXX(None) didn't complain");
if (!PyErr_ExceptionMatches(PyExc_TypeError))
return error("PyLong_AsXXX(None) raised "
"something other than TypeError");
PyErr_Clear();
uout = F_PY_TO_U(Py_None);
if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
return error("PyLong_AsXXX(None) didn't complain");
if (!PyErr_ExceptionMatches(PyExc_TypeError))
return error("PyLong_AsXXX(None) raised "
"something other than TypeError");
PyErr_Clear();
Py_DECREF(Py_None);
}
Py_INCREF(Py_None);
return Py_None;
}
Mode |
Type |
Size |
Ref |
File |
100644 |
blob |
582 |
58471109208922c9ee8c4b06135725f03ed16814 |
.bzrignore |
100644 |
blob |
545 |
fcf9df6a7a698e4bd87ed0c1cc4ed70bad8b9887 |
.codecov.yml |
100644 |
blob |
255 |
82694d81f276b2c59a0a93a4f678e1852e625052 |
.gitattributes |
040000 |
tree |
- |
7e849e161267e730810fbbe6a848b14d5d002788 |
.github |
100644 |
blob |
1397 |
8b54c2c4861389f6e8bbfbab5ae0c8b6bbbad041 |
.gitignore |
100644 |
blob |
1060 |
eb19a6c88d28d05588db25d21525ee2e19c22666 |
.hgeol |
100644 |
blob |
1358 |
68c607f2e8d420c8dfd0748efcd3b3b5447def16 |
.hgignore |
100644 |
blob |
8917 |
8f51c2ced49aed46d8b480280b630ea4264c57c3 |
.hgtags |
100644 |
blob |
1328 |
b9be0f11fdb829f16e9de1921257eb7ee45fac57 |
.hgtouch |
100644 |
blob |
248 |
0614a299b6221dc7faedaa9139ae8b034e618a85 |
.mention-bot |
100644 |
blob |
3512 |
e7e8694530ca21a6d7a19da3fab687a3e9d79e9c |
.travis.yml |
040000 |
tree |
- |
ef39fb38058ea1cb07d5a66c7d0c338d5c9c634d |
Doc |
040000 |
tree |
- |
5dd6fc9dc09374506491247872c868eca111e256 |
Grammar |
040000 |
tree |
- |
df0de9d4359f11311c74fd0dbad471bb2613a2d4 |
Include |
100644 |
blob |
12773 |
f5d0b39a0cdddb91a31a537052b7d8d31a4aa79f |
LICENSE |
040000 |
tree |
- |
41936e36e534f9069cb438b31908d8e097c673ee |
Lib |
040000 |
tree |
- |
1db7415d4375525eaf8d05ddd5b088de3321041c |
Mac |
100644 |
blob |
58983 |
4145634c032d543d02295bd2c28a0c6ce839fa86 |
Makefile.pre.in |
040000 |
tree |
- |
5ce39d28202a5713d0cff1c4cd7eb73267ea1a08 |
Misc |
040000 |
tree |
- |
b74dfdc5f8f5700baaab0dbd2aa197899f8b79d9 |
Modules |
040000 |
tree |
- |
774ee56f73f58d31a7e7d59314020c9176eeb9d8 |
Objects |
040000 |
tree |
- |
ed4f35810e9633502c16ae038c2ce697d3987201 |
PC |
040000 |
tree |
- |
37a613ac0022a9cfefaf3f13913fec7debe59259 |
PCbuild |
040000 |
tree |
- |
75771c7c20fe7a121d596299c5440aef10c6f884 |
Parser |
040000 |
tree |
- |
3efbcc80237ab7c3d4eb5bf31c893ca6de88e747 |
Programs |
040000 |
tree |
- |
ec8f975802930f631229dd3f1c1dd6e327557ae8 |
Python |
100644 |
blob |
9325 |
9c95815d9e9d91b8dae8e05d8bbc696fe19f796b |
README.rst |
040000 |
tree |
- |
6f90a7ecc8b4a12e0377f892c8639ef99da08920 |
Tools |
100644 |
blob |
10910 |
9a9cc557281571f0d46c506c0e9d1b9fb25e063c |
aclocal.m4 |
100755 |
blob |
42856 |
1f5c50c0d1529d50b94dc3533ca72a47f0fa5849 |
config.guess |
100755 |
blob |
35740 |
d654d03cdcd2226a5d7584890717e674a8122f4f |
config.sub |
100755 |
blob |
485283 |
87504d206837baf5a5781b6e1cc44dcce7138af9 |
configure |
100644 |
blob |
160661 |
f9bd92ce3da29ea7674a32bd5fe511b1fc4c4d0a |
configure.ac |
100755 |
blob |
7122 |
0ec27bcd488da5cad6ead13d70accbdbc40d31ef |
install-sh |
100644 |
blob |
41449 |
21354a5cb84fe5530dd0d460561ba95569abe1d4 |
pyconfig.h.in |
100644 |
blob |
98743 |
3b3d097454211c790c1602d759918bb65a622c97 |
setup.py |
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"
Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/benf_wspdigital/cpython
Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/benf_wspdigital/cpython
Clone this repository using git:
git clone git://git.rocketgit.com/user/benf_wspdigital/cpython
You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a
merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main