Python-checkins
Threads by month
- ----- 2024 -----
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2004 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2003 -----
- December
- November
- October
- September
- August
June 2023
- 1 participants
- 674 discussions
[3.11] gh-101006: Improve error handling when read marshal data (GH-101007) (GH-106227)
by serhiy-storchaka 29 Jun '23
by serhiy-storchaka 29 Jun '23
29 Jun '23
https://github.com/python/cpython/commit/b937ca0a5afc5158dee1a503ec0e9f6e27…
commit: b937ca0a5afc5158dee1a503ec0e9f6e2726c867
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington(a)users.noreply.github.com>
committer: serhiy-storchaka <storchaka(a)gmail.com>
date: 2023-06-29T13:47:30+03:00
summary:
[3.11] gh-101006: Improve error handling when read marshal data (GH-101007) (GH-106227)
* EOFError no longer overrides other errors such as MemoryError or OSError at
the start of the object.
* Raise more relevant error when the NULL object occurs as a code object
component.
* Minimize an overhead of calling PyErr_Occurred().
(cherry picked from commit 8bf6904b229583033035d91a3800da5604dcaad4)
Co-authored-by: Serhiy Storchaka <storchaka(a)gmail.com>
files:
A Misc/NEWS.d/next/Core and Builtins/2023-01-13-11-37-41.gh-issue-101006.fuLvn2.rst
M Python/marshal.c
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-01-13-11-37-41.gh-issue-101006.fuLvn2.rst b/Misc/NEWS.d/next/Core and Builtins/2023-01-13-11-37-41.gh-issue-101006.fuLvn2.rst
new file mode 100644
index 0000000000000..c98670d8c4963
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-01-13-11-37-41.gh-issue-101006.fuLvn2.rst
@@ -0,0 +1 @@
+Improve error handling when read :mod:`marshal` data.
diff --git a/Python/marshal.c b/Python/marshal.c
index bf607cb8ce2d0..29f3bab60a5fe 100644
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -750,23 +750,28 @@ r_string(Py_ssize_t n, RFILE *p)
static int
r_byte(RFILE *p)
{
- int c = EOF;
-
if (p->ptr != NULL) {
- if (p->ptr < p->end)
- c = (unsigned char) *p->ptr++;
- return c;
+ if (p->ptr < p->end) {
+ return (unsigned char) *p->ptr++;
+ }
}
- if (!p->readable) {
+ else if (!p->readable) {
assert(p->fp);
- c = getc(p->fp);
+ int c = getc(p->fp);
+ if (c != EOF) {
+ return c;
+ }
}
else {
const char *ptr = r_string(1, p);
- if (ptr != NULL)
- c = *(const unsigned char *) ptr;
+ if (ptr != NULL) {
+ return *(const unsigned char *) ptr;
+ }
+ return EOF;
}
- return c;
+ PyErr_SetString(PyExc_EOFError,
+ "EOF read where not expected");
+ return EOF;
}
static int
@@ -827,10 +832,11 @@ r_PyLong(RFILE *p)
digit d;
n = r_long(p);
- if (PyErr_Occurred())
- return NULL;
if (n == 0)
return (PyObject *)_PyLong_New(0);
+ if (n == -1 && PyErr_Occurred()) {
+ return NULL;
+ }
if (n < -SIZE32_MAX || n > SIZE32_MAX) {
PyErr_SetString(PyExc_ValueError,
"bad marshal data (long size out of range)");
@@ -849,10 +855,6 @@ r_PyLong(RFILE *p)
d = 0;
for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
md = r_short(p);
- if (PyErr_Occurred()) {
- Py_DECREF(ob);
- return NULL;
- }
if (md < 0 || md > PyLong_MARSHAL_BASE)
goto bad_digit;
d += (digit)md << j*PyLong_MARSHAL_SHIFT;
@@ -863,10 +865,6 @@ r_PyLong(RFILE *p)
d = 0;
for (j=0; j < shorts_in_top_digit; j++) {
md = r_short(p);
- if (PyErr_Occurred()) {
- Py_DECREF(ob);
- return NULL;
- }
if (md < 0 || md > PyLong_MARSHAL_BASE)
goto bad_digit;
/* topmost marshal digit should be nonzero */
@@ -878,18 +876,17 @@ r_PyLong(RFILE *p)
}
d += (digit)md << j*PyLong_MARSHAL_SHIFT;
}
- if (PyErr_Occurred()) {
- Py_DECREF(ob);
- return NULL;
- }
+ assert(!PyErr_Occurred());
/* top digit should be nonzero, else the resulting PyLong won't be
normalized */
ob->ob_digit[size-1] = d;
return (PyObject *)ob;
bad_digit:
Py_DECREF(ob);
- PyErr_SetString(PyExc_ValueError,
- "bad marshal data (digit out of range in long)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (digit out of range in long)");
+ }
return NULL;
}
@@ -912,8 +909,6 @@ r_float_str(RFILE *p)
const char *ptr;
n = r_byte(p);
if (n == EOF) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
return -1;
}
ptr = r_string(n, p);
@@ -992,8 +987,10 @@ r_object(RFILE *p)
PyObject *retval = NULL;
if (code == EOF) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
+ if (PyErr_ExceptionMatches(PyExc_EOFError)) {
+ PyErr_SetString(PyExc_EOFError,
+ "EOF read where object expected");
+ }
return NULL;
}
@@ -1045,7 +1042,10 @@ r_object(RFILE *p)
case TYPE_INT:
n = r_long(p);
- retval = PyErr_Occurred() ? NULL : PyLong_FromLong(n);
+ if (n == -1 && PyErr_Occurred()) {
+ break;
+ }
+ retval = PyLong_FromLong(n);
R_REF(retval);
break;
@@ -1111,10 +1111,11 @@ r_object(RFILE *p)
{
const char *ptr;
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (bytes object size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (bytes object size out of range)");
+ }
break;
}
v = PyBytes_FromStringAndSize((char *)NULL, n);
@@ -1136,10 +1137,11 @@ r_object(RFILE *p)
/* fall through */
case TYPE_ASCII:
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (string size out of range)");
+ }
break;
}
goto _read_ascii;
@@ -1150,8 +1152,6 @@ r_object(RFILE *p)
case TYPE_SHORT_ASCII:
n = r_byte(p);
if (n == EOF) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
break;
}
_read_ascii:
@@ -1178,10 +1178,11 @@ r_object(RFILE *p)
const char *buffer;
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (string size out of range)");
+ }
break;
}
if (n != 0) {
@@ -1203,16 +1204,18 @@ r_object(RFILE *p)
}
case TYPE_SMALL_TUPLE:
- n = (unsigned char) r_byte(p);
- if (PyErr_Occurred())
+ n = r_byte(p);
+ if (n == EOF) {
break;
+ }
goto _read_tuple;
case TYPE_TUPLE:
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (tuple size out of range)");
+ }
break;
}
_read_tuple:
@@ -1238,10 +1241,11 @@ r_object(RFILE *p)
case TYPE_LIST:
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (list size out of range)");
+ }
break;
}
v = PyList_New(n);
@@ -1296,10 +1300,11 @@ r_object(RFILE *p)
case TYPE_SET:
case TYPE_FROZENSET:
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (set size out of range)");
+ }
break;
}
@@ -1377,20 +1382,20 @@ r_object(RFILE *p)
/* XXX ignore long->int overflows for now */
argcount = (int)r_long(p);
- if (PyErr_Occurred())
+ if (argcount == -1 && PyErr_Occurred())
goto code_error;
posonlyargcount = (int)r_long(p);
- if (PyErr_Occurred()) {
+ if (posonlyargcount == -1 && PyErr_Occurred()) {
goto code_error;
}
kwonlyargcount = (int)r_long(p);
- if (PyErr_Occurred())
+ if (kwonlyargcount == -1 && PyErr_Occurred())
goto code_error;
stacksize = (int)r_long(p);
- if (PyErr_Occurred())
+ if (stacksize == -1 && PyErr_Occurred())
goto code_error;
flags = (int)r_long(p);
- if (PyErr_Occurred())
+ if (flags == -1 && PyErr_Occurred())
goto code_error;
code = r_object(p);
if (code == NULL)
@@ -1463,6 +1468,10 @@ r_object(RFILE *p)
v = r_ref_insert(v, idx, flag, p);
code_error:
+ if (v == NULL && !PyErr_Occurred()) {
+ PyErr_SetString(PyExc_TypeError,
+ "NULL object in marshal data for code object");
+ }
Py_XDECREF(code);
Py_XDECREF(consts);
Py_XDECREF(names);
@@ -1480,9 +1489,10 @@ r_object(RFILE *p)
case TYPE_REF:
n = r_long(p);
if (n < 0 || n >= PyList_GET_SIZE(p->refs)) {
- if (n == -1 && PyErr_Occurred())
- break;
- PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (invalid reference)");
+ }
break;
}
v = PyList_GET_ITEM(p->refs, n);
1
0
[3.12] gh-101006: Improve error handling when read marshal data (GH-101007) (GH-106226)
by serhiy-storchaka 29 Jun '23
by serhiy-storchaka 29 Jun '23
29 Jun '23
https://github.com/python/cpython/commit/c9b9555650fee56a2fcb66c1db25e2b57f…
commit: c9b9555650fee56a2fcb66c1db25e2b57f71a5d9
branch: 3.12
author: Miss Islington (bot) <31488909+miss-islington(a)users.noreply.github.com>
committer: serhiy-storchaka <storchaka(a)gmail.com>
date: 2023-06-29T13:46:51+03:00
summary:
[3.12] gh-101006: Improve error handling when read marshal data (GH-101007) (GH-106226)
* EOFError no longer overrides other errors such as MemoryError or OSError at
the start of the object.
* Raise more relevant error when the NULL object occurs as a code object
component.
* Minimize an overhead of calling PyErr_Occurred().
(cherry picked from commit 8bf6904b229583033035d91a3800da5604dcaad4)
Co-authored-by: Serhiy Storchaka <storchaka(a)gmail.com>
files:
A Misc/NEWS.d/next/Core and Builtins/2023-01-13-11-37-41.gh-issue-101006.fuLvn2.rst
M Python/marshal.c
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-01-13-11-37-41.gh-issue-101006.fuLvn2.rst b/Misc/NEWS.d/next/Core and Builtins/2023-01-13-11-37-41.gh-issue-101006.fuLvn2.rst
new file mode 100644
index 0000000000000..c98670d8c4963
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-01-13-11-37-41.gh-issue-101006.fuLvn2.rst
@@ -0,0 +1 @@
+Improve error handling when read :mod:`marshal` data.
diff --git a/Python/marshal.c b/Python/marshal.c
index 6439503d2c687..90953cbb72845 100644
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -751,23 +751,28 @@ r_string(Py_ssize_t n, RFILE *p)
static int
r_byte(RFILE *p)
{
- int c = EOF;
-
if (p->ptr != NULL) {
- if (p->ptr < p->end)
- c = (unsigned char) *p->ptr++;
- return c;
+ if (p->ptr < p->end) {
+ return (unsigned char) *p->ptr++;
+ }
}
- if (!p->readable) {
+ else if (!p->readable) {
assert(p->fp);
- c = getc(p->fp);
+ int c = getc(p->fp);
+ if (c != EOF) {
+ return c;
+ }
}
else {
const char *ptr = r_string(1, p);
- if (ptr != NULL)
- c = *(const unsigned char *) ptr;
+ if (ptr != NULL) {
+ return *(const unsigned char *) ptr;
+ }
+ return EOF;
}
- return c;
+ PyErr_SetString(PyExc_EOFError,
+ "EOF read where not expected");
+ return EOF;
}
static int
@@ -828,10 +833,11 @@ r_PyLong(RFILE *p)
digit d;
n = r_long(p);
- if (PyErr_Occurred())
- return NULL;
if (n == 0)
return (PyObject *)_PyLong_New(0);
+ if (n == -1 && PyErr_Occurred()) {
+ return NULL;
+ }
if (n < -SIZE32_MAX || n > SIZE32_MAX) {
PyErr_SetString(PyExc_ValueError,
"bad marshal data (long size out of range)");
@@ -850,10 +856,6 @@ r_PyLong(RFILE *p)
d = 0;
for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
md = r_short(p);
- if (PyErr_Occurred()) {
- Py_DECREF(ob);
- return NULL;
- }
if (md < 0 || md > PyLong_MARSHAL_BASE)
goto bad_digit;
d += (digit)md << j*PyLong_MARSHAL_SHIFT;
@@ -864,10 +866,6 @@ r_PyLong(RFILE *p)
d = 0;
for (j=0; j < shorts_in_top_digit; j++) {
md = r_short(p);
- if (PyErr_Occurred()) {
- Py_DECREF(ob);
- return NULL;
- }
if (md < 0 || md > PyLong_MARSHAL_BASE)
goto bad_digit;
/* topmost marshal digit should be nonzero */
@@ -879,18 +877,17 @@ r_PyLong(RFILE *p)
}
d += (digit)md << j*PyLong_MARSHAL_SHIFT;
}
- if (PyErr_Occurred()) {
- Py_DECREF(ob);
- return NULL;
- }
+ assert(!PyErr_Occurred());
/* top digit should be nonzero, else the resulting PyLong won't be
normalized */
ob->long_value.ob_digit[size-1] = d;
return (PyObject *)ob;
bad_digit:
Py_DECREF(ob);
- PyErr_SetString(PyExc_ValueError,
- "bad marshal data (digit out of range in long)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (digit out of range in long)");
+ }
return NULL;
}
@@ -913,8 +910,6 @@ r_float_str(RFILE *p)
const char *ptr;
n = r_byte(p);
if (n == EOF) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
return -1;
}
ptr = r_string(n, p);
@@ -992,8 +987,10 @@ r_object(RFILE *p)
PyObject *retval = NULL;
if (code == EOF) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
+ if (PyErr_ExceptionMatches(PyExc_EOFError)) {
+ PyErr_SetString(PyExc_EOFError,
+ "EOF read where object expected");
+ }
return NULL;
}
@@ -1040,7 +1037,10 @@ r_object(RFILE *p)
case TYPE_INT:
n = r_long(p);
- retval = PyErr_Occurred() ? NULL : PyLong_FromLong(n);
+ if (n == -1 && PyErr_Occurred()) {
+ break;
+ }
+ retval = PyLong_FromLong(n);
R_REF(retval);
break;
@@ -1106,10 +1106,11 @@ r_object(RFILE *p)
{
const char *ptr;
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (bytes object size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (bytes object size out of range)");
+ }
break;
}
v = PyBytes_FromStringAndSize((char *)NULL, n);
@@ -1131,10 +1132,11 @@ r_object(RFILE *p)
/* fall through */
case TYPE_ASCII:
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (string size out of range)");
+ }
break;
}
goto _read_ascii;
@@ -1145,8 +1147,6 @@ r_object(RFILE *p)
case TYPE_SHORT_ASCII:
n = r_byte(p);
if (n == EOF) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
break;
}
_read_ascii:
@@ -1173,10 +1173,11 @@ r_object(RFILE *p)
const char *buffer;
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (string size out of range)");
+ }
break;
}
if (n != 0) {
@@ -1198,16 +1199,18 @@ r_object(RFILE *p)
}
case TYPE_SMALL_TUPLE:
- n = (unsigned char) r_byte(p);
- if (PyErr_Occurred())
+ n = r_byte(p);
+ if (n == EOF) {
break;
+ }
goto _read_tuple;
case TYPE_TUPLE:
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (tuple size out of range)");
+ }
break;
}
_read_tuple:
@@ -1232,10 +1235,11 @@ r_object(RFILE *p)
case TYPE_LIST:
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (list size out of range)");
+ }
break;
}
v = PyList_New(n);
@@ -1288,10 +1292,11 @@ r_object(RFILE *p)
case TYPE_SET:
case TYPE_FROZENSET:
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (set size out of range)");
+ }
break;
}
@@ -1368,20 +1373,20 @@ r_object(RFILE *p)
/* XXX ignore long->int overflows for now */
argcount = (int)r_long(p);
- if (PyErr_Occurred())
+ if (argcount == -1 && PyErr_Occurred())
goto code_error;
posonlyargcount = (int)r_long(p);
- if (PyErr_Occurred()) {
+ if (posonlyargcount == -1 && PyErr_Occurred()) {
goto code_error;
}
kwonlyargcount = (int)r_long(p);
- if (PyErr_Occurred())
+ if (kwonlyargcount == -1 && PyErr_Occurred())
goto code_error;
stacksize = (int)r_long(p);
- if (PyErr_Occurred())
+ if (stacksize == -1 && PyErr_Occurred())
goto code_error;
flags = (int)r_long(p);
- if (PyErr_Occurred())
+ if (flags == -1 && PyErr_Occurred())
goto code_error;
code = r_object(p);
if (code == NULL)
@@ -1454,6 +1459,10 @@ r_object(RFILE *p)
v = r_ref_insert(v, idx, flag, p);
code_error:
+ if (v == NULL && !PyErr_Occurred()) {
+ PyErr_SetString(PyExc_TypeError,
+ "NULL object in marshal data for code object");
+ }
Py_XDECREF(code);
Py_XDECREF(consts);
Py_XDECREF(names);
@@ -1471,9 +1480,10 @@ r_object(RFILE *p)
case TYPE_REF:
n = r_long(p);
if (n < 0 || n >= PyList_GET_SIZE(p->refs)) {
- if (n == -1 && PyErr_Occurred())
- break;
- PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (invalid reference)");
+ }
break;
}
v = PyList_GET_ITEM(p->refs, n);
1
0
[3.12] gh-106033: Get rid of PyDict_GetItem in _PyFunction_FromConstructor (GH-106044) (GH-106228)
by serhiy-storchaka 29 Jun '23
by serhiy-storchaka 29 Jun '23
29 Jun '23
https://github.com/python/cpython/commit/e0fa531d7aa2f1e809dc77c222538620b2…
commit: e0fa531d7aa2f1e809dc77c222538620b2cf24b9
branch: 3.12
author: Miss Islington (bot) <31488909+miss-islington(a)users.noreply.github.com>
committer: serhiy-storchaka <storchaka(a)gmail.com>
date: 2023-06-29T13:45:39+03:00
summary:
[3.12] gh-106033: Get rid of PyDict_GetItem in _PyFunction_FromConstructor (GH-106044) (GH-106228)
gh-106033: Get rid of PyDict_GetItem in _PyFunction_FromConstructor (GH-106044)
(cherry picked from commit 08c08d21b03d949452a77d9ed5e3cf48d6b9804d)
Co-authored-by: Serhiy Storchaka <storchaka(a)gmail.com>
files:
M Objects/funcobject.c
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index 753038600aa85..f43e3a2787b84 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -106,9 +106,14 @@ PyFunction_ClearWatcher(int watcher_id)
PyFunctionObject *
_PyFunction_FromConstructor(PyFrameConstructor *constr)
{
+ PyObject *module = Py_XNewRef(PyDict_GetItemWithError(constr->fc_globals, &_Py_ID(__name__)));
+ if (!module && PyErr_Occurred()) {
+ return NULL;
+ }
PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
if (op == NULL) {
+ Py_XDECREF(module);
return NULL;
}
op->func_globals = Py_NewRef(constr->fc_globals);
@@ -122,10 +127,7 @@ _PyFunction_FromConstructor(PyFrameConstructor *constr)
op->func_doc = Py_NewRef(Py_None);
op->func_dict = NULL;
op->func_weakreflist = NULL;
- op->func_module = Py_XNewRef(PyDict_GetItem(op->func_globals, &_Py_ID(__name__)));
- if (!op->func_module) {
- PyErr_Clear();
- }
+ op->func_module = module;
op->func_annotations = NULL;
op->func_typeparams = NULL;
op->vectorcall = _PyFunction_Vectorcall;
1
0
https://github.com/python/cpython/commit/fb0d9b9ac1ec3ea13fae8b8ef6a4f0a5a8…
commit: fb0d9b9ac1ec3ea13fae8b8ef6a4f0a5a80482b3
branch: main
author: Charlie Zhao <zhaoyu_hit(a)qq.com>
committer: erlend-aasland <erlend.aasland(a)protonmail.com>
date: 2023-06-29T10:27:20Z
summary:
gh-106078: Convert `_decimal` types to heap types (#106079)
- Establish global state struct
- Convert static types to heap types and add them to global state:
* PyDecContextManager_Type
* PyDecContext_Type
* PyDecSignalDictMixin_Type
* PyDec_Type
- Add to global state:
* PyDecSignalDict_Type
* DecimalTuple
Co-authored-by: Kumar Aditya <59607654+kumaraditya303(a)users.noreply.github.com>
Co-authored-by: Erlend E. Aasland <erlend(a)python.org>
files:
M Lib/test/test_decimal.py
M Modules/_decimal/_decimal.c
M Tools/c-analyzer/cpython/globals-to-fix.tsv
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index 749496e3e9455..db67f37608f1f 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -34,7 +34,8 @@
import locale
from test.support import (is_resource_enabled,
requires_IEEE_754, requires_docstrings,
- requires_legacy_unicode_capi, check_sanitizer)
+ requires_legacy_unicode_capi, check_sanitizer,
+ check_disallow_instantiation)
from test.support import (TestFailed,
run_with_locale, cpython_only,
darwin_malloc_err_warning, is_emscripten)
@@ -5681,6 +5682,24 @@ def test_maxcontext_exact_arith(self):
self.assertEqual(Decimal(4) / 2, 2)
self.assertEqual(Decimal(400) ** -1, Decimal('0.0025'))
+ def test_c_immutable_types(self):
+ SignalDict = type(C.Context().flags)
+ SignalDictMixin = SignalDict.__bases__[0]
+ ContextManager = type(C.localcontext())
+ types = (
+ SignalDictMixin,
+ ContextManager,
+ C.Decimal,
+ C.Context,
+ )
+ for tp in types:
+ with self.subTest(tp=tp):
+ with self.assertRaisesRegex(TypeError, "immutable"):
+ tp.foo = 1
+
+ def test_c_disallow_instantiation(self):
+ ContextManager = type(C.localcontext())
+ check_disallow_instantiation(self, ContextManager)
@requires_docstrings
@requires_cdecimal
diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c
index 73df3f34829f0..b7cb19515b300 100644
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -39,6 +39,18 @@
#include "docstrings.h"
+typedef struct {
+ PyTypeObject *PyDecContextManager_Type;
+ PyTypeObject *PyDecContext_Type;
+ PyTypeObject *PyDecSignalDictMixin_Type;
+ PyTypeObject *PyDec_Type;
+ PyTypeObject *PyDecSignalDict_Type;
+ PyTypeObject *DecimalTuple;
+} decimal_state;
+
+static decimal_state global_state;
+
+#define GLOBAL_STATE() (&global_state)
#if !defined(MPD_VERSION_HEX) || MPD_VERSION_HEX < 0x02050000
#error "libmpdec version >= 2.5.0 required"
@@ -99,14 +111,10 @@ typedef struct {
#undef MPD
#undef CTX
-static PyTypeObject PyDec_Type;
-static PyTypeObject *PyDecSignalDict_Type;
-static PyTypeObject PyDecContext_Type;
-static PyTypeObject PyDecContextManager_Type;
-#define PyDec_CheckExact(v) Py_IS_TYPE(v, &PyDec_Type)
-#define PyDec_Check(v) PyObject_TypeCheck(v, &PyDec_Type)
-#define PyDecSignalDict_Check(v) Py_IS_TYPE(v, PyDecSignalDict_Type)
-#define PyDecContext_Check(v) PyObject_TypeCheck(v, &PyDecContext_Type)
+#define PyDec_CheckExact(st, v) Py_IS_TYPE(v, (st)->PyDec_Type)
+#define PyDec_Check(st, v) PyObject_TypeCheck(v, (st)->PyDec_Type)
+#define PyDecSignalDict_Check(st, v) Py_IS_TYPE(v, (st)->PyDecSignalDict_Type)
+#define PyDecContext_Check(st, v) PyObject_TypeCheck(v, (st)->PyDecContext_Type)
#define MPD(v) (&((PyDecObject *)v)->dec)
#define SdFlagAddr(v) (((PyDecSignalDictObject *)v)->flags)
#define SdFlags(v) (*((PyDecSignalDictObject *)v)->flags)
@@ -603,6 +611,22 @@ signaldict_setitem(PyObject *self, PyObject *key, PyObject *value)
return 0;
}
+static int
+signaldict_traverse(PyObject *self, visitproc visit, void *arg)
+{
+ Py_VISIT(Py_TYPE(self));
+ return 0;
+}
+
+static void
+signaldict_dealloc(PyObject *self)
+{
+ PyTypeObject *tp = Py_TYPE(self);
+ PyObject_GC_UnTrack(self);
+ tp->tp_free(self);
+ Py_DECREF(tp);
+}
+
static PyObject *
signaldict_repr(PyObject *self)
{
@@ -631,10 +655,11 @@ signaldict_richcompare(PyObject *v, PyObject *w, int op)
{
PyObject *res = Py_NotImplemented;
- assert(PyDecSignalDict_Check(v));
+ decimal_state *state = GLOBAL_STATE();
+ assert(PyDecSignalDict_Check(state, v));
if (op == Py_EQ || op == Py_NE) {
- if (PyDecSignalDict_Check(w)) {
+ if (PyDecSignalDict_Check(state, w)) {
res = (SdFlags(v)==SdFlags(w)) ^ (op==Py_NE) ? Py_True : Py_False;
}
else if (PyDict_Check(w)) {
@@ -664,58 +689,36 @@ signaldict_copy(PyObject *self, PyObject *args UNUSED)
}
-static PyMappingMethods signaldict_as_mapping = {
- (lenfunc)signaldict_len, /* mp_length */
- (binaryfunc)signaldict_getitem, /* mp_subscript */
- (objobjargproc)signaldict_setitem /* mp_ass_subscript */
-};
-
static PyMethodDef signaldict_methods[] = {
{ "copy", (PyCFunction)signaldict_copy, METH_NOARGS, NULL},
{NULL, NULL}
};
-static PyTypeObject PyDecSignalDictMixin_Type =
-{
- PyVarObject_HEAD_INIT(0, 0)
- "decimal.SignalDictMixin", /* tp_name */
- sizeof(PyDecSignalDictObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- 0, /* tp_dealloc */
- 0, /* tp_vectorcall_offset */
- (getattrfunc) 0, /* tp_getattr */
- (setattrfunc) 0, /* tp_setattr */
- 0, /* tp_as_async */
- (reprfunc) signaldict_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- &signaldict_as_mapping, /* tp_as_mapping */
- PyObject_HashNotImplemented, /* tp_hash */
- 0, /* tp_call */
- (reprfunc) 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- (setattrofunc) 0, /* tp_setattro */
- (PyBufferProcs *) 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- signaldict_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- (getiterfunc)signaldict_iter, /* tp_iter */
- 0, /* tp_iternext */
- signaldict_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- (initproc)signaldict_init, /* tp_init */
- 0, /* tp_alloc */
- PyType_GenericNew, /* tp_new */
+static PyType_Slot signaldict_slots[] = {
+ {Py_tp_dealloc, signaldict_dealloc},
+ {Py_tp_traverse, signaldict_traverse},
+ {Py_tp_repr, signaldict_repr},
+ {Py_tp_hash, PyObject_HashNotImplemented},
+ {Py_tp_getattro, PyObject_GenericGetAttr},
+ {Py_tp_richcompare, signaldict_richcompare},
+ {Py_tp_iter, signaldict_iter},
+ {Py_tp_methods, signaldict_methods},
+ {Py_tp_init, signaldict_init},
+
+ // Mapping protocol
+ {Py_mp_length, signaldict_len},
+ {Py_mp_subscript, signaldict_getitem},
+ {Py_mp_ass_subscript, signaldict_setitem},
+ {0, NULL},
+};
+
+static PyType_Spec signaldict_spec = {
+ .name = "decimal.SignalDictMixin",
+ .basicsize = sizeof(PyDecSignalDictObject),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
+ Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = signaldict_slots,
};
@@ -987,7 +990,8 @@ context_settraps_dict(PyObject *self, PyObject *value)
mpd_context_t *ctx;
uint32_t flags;
- if (PyDecSignalDict_Check(value)) {
+ decimal_state *state = GLOBAL_STATE();
+ if (PyDecSignalDict_Check(state, value)) {
flags = SdFlags(value);
}
else {
@@ -1051,7 +1055,8 @@ context_setstatus_dict(PyObject *self, PyObject *value)
mpd_context_t *ctx;
uint32_t flags;
- if (PyDecSignalDict_Check(value)) {
+ decimal_state *state = GLOBAL_STATE();
+ if (PyDecSignalDict_Check(state, value)) {
flags = SdFlags(value);
}
else {
@@ -1241,8 +1246,9 @@ context_new(PyTypeObject *type, PyObject *args UNUSED, PyObject *kwds UNUSED)
PyDecContextObject *self = NULL;
mpd_context_t *ctx;
- if (type == &PyDecContext_Type) {
- self = PyObject_New(PyDecContextObject, &PyDecContext_Type);
+ decimal_state *state = GLOBAL_STATE();
+ if (type == state->PyDecContext_Type) {
+ self = PyObject_GC_New(PyDecContextObject, state->PyDecContext_Type);
}
else {
self = (PyDecContextObject *)type->tp_alloc(type, 0);
@@ -1252,13 +1258,13 @@ context_new(PyTypeObject *type, PyObject *args UNUSED, PyObject *kwds UNUSED)
return NULL;
}
- self->traps = PyObject_CallObject((PyObject *)PyDecSignalDict_Type, NULL);
+ self->traps = PyObject_CallObject((PyObject *)state->PyDecSignalDict_Type, NULL);
if (self->traps == NULL) {
self->flags = NULL;
Py_DECREF(self);
return NULL;
}
- self->flags = PyObject_CallObject((PyObject *)PyDecSignalDict_Type, NULL);
+ self->flags = PyObject_CallObject((PyObject *)state->PyDecSignalDict_Type, NULL);
if (self->flags == NULL) {
Py_DECREF(self);
return NULL;
@@ -1282,18 +1288,37 @@ context_new(PyTypeObject *type, PyObject *args UNUSED, PyObject *kwds UNUSED)
return (PyObject *)self;
}
+static int
+context_traverse(PyDecContextObject *self, visitproc visit, void *arg)
+{
+ Py_VISIT(Py_TYPE(self));
+ Py_VISIT(self->traps);
+ Py_VISIT(self->flags);
+ return 0;
+}
+
+static int
+context_clear(PyDecContextObject *self)
+{
+ Py_CLEAR(self->traps);
+ Py_CLEAR(self->flags);
+ return 0;
+}
+
static void
context_dealloc(PyDecContextObject *self)
{
+ PyTypeObject *tp = Py_TYPE(self);
+ PyObject_GC_UnTrack(self);
#ifndef WITH_DECIMAL_CONTEXTVAR
- if (self == cached_context) {
- cached_context = NULL;
+ decimal_state *state = GLOBAL_STATE();
+ if (self == state->cached_context) {
+ state->cached_context = NULL;
}
#endif
-
- Py_XDECREF(self->traps);
- Py_XDECREF(self->flags);
- Py_TYPE(self)->tp_free(self);
+ (void)context_clear(self);
+ tp->tp_free(self);
+ Py_DECREF(tp);
}
static int
@@ -1337,7 +1362,10 @@ context_repr(PyDecContextObject *self)
char traps[MPD_MAX_SIGNAL_LIST];
int n, mem;
- assert(PyDecContext_Check(self));
+#ifdef Py_DEBUG
+ decimal_state *state = GLOBAL_STATE();
+ assert(PyDecContext_Check(state, self));
+#endif
ctx = CTX(self);
mem = MPD_MAX_SIGNAL_LIST;
@@ -1403,7 +1431,8 @@ ieee_context(PyObject *dummy UNUSED, PyObject *v)
goto error;
}
- context = PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL);
+ decimal_state *state = GLOBAL_STATE();
+ context = PyObject_CallObject((PyObject *)state->PyDecContext_Type, NULL);
if (context == NULL) {
return NULL;
}
@@ -1425,7 +1454,8 @@ context_copy(PyObject *self, PyObject *args UNUSED)
{
PyObject *copy;
- copy = PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL);
+ decimal_state *state = GLOBAL_STATE();
+ copy = PyObject_CallObject((PyObject *)state->PyDecContext_Type, NULL);
if (copy == NULL) {
return NULL;
}
@@ -1487,18 +1517,18 @@ static PyGetSetDef context_getsets [] =
};
-#define CONTEXT_CHECK(obj) \
- if (!PyDecContext_Check(obj)) { \
+#define CONTEXT_CHECK(state, obj) \
+ if (!PyDecContext_Check(state, obj)) { \
PyErr_SetString(PyExc_TypeError, \
"argument must be a context"); \
return NULL; \
}
-#define CONTEXT_CHECK_VA(obj) \
+#define CONTEXT_CHECK_VA(state, obj) \
if (obj == Py_None) { \
CURRENT_CONTEXT(obj); \
} \
- else if (!PyDecContext_Check(obj)) { \
+ else if (!PyDecContext_Check(state, obj)) { \
PyErr_SetString(PyExc_TypeError, \
"optional argument must be a context"); \
return NULL; \
@@ -1522,6 +1552,7 @@ static PyObject *
current_context_from_dict(void)
{
PyThreadState *tstate = _PyThreadState_GET();
+ decimal_state *modstate = GLOBAL_STATE();
#ifdef Py_DEBUG
// The caller must hold the GIL
_Py_EnsureTstateNotNULL(tstate);
@@ -1537,7 +1568,7 @@ current_context_from_dict(void)
PyObject *tl_context = PyDict_GetItemWithError(dict, tls_context_key);
if (tl_context != NULL) {
/* We already have a thread local context. */
- CONTEXT_CHECK(tl_context);
+ CONTEXT_CHECK(modstate, tl_context);
}
else {
if (PyErr_Occurred()) {
@@ -1606,7 +1637,8 @@ PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v)
{
PyObject *dict;
- CONTEXT_CHECK(v);
+ decimal_state *state = GLOBAL_STATE();
+ CONTEXT_CHECK(state, v);
dict = PyThreadState_GetDict();
if (dict == NULL) {
@@ -1693,7 +1725,8 @@ PyDec_GetCurrentContext(PyObject *self UNUSED, PyObject *args UNUSED)
static PyObject *
PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v)
{
- CONTEXT_CHECK(v);
+ decimal_state *state = GLOBAL_STATE();
+ CONTEXT_CHECK(state, v);
/* If the new context is one of the templates, make a copy.
* This is the current behavior of decimal.py. */
@@ -1733,7 +1766,6 @@ ctxmanager_new(PyTypeObject *type UNUSED, PyObject *args, PyObject *kwds)
"clamp", "flags", "traps",
NULL
};
- PyDecContextManagerObject *self;
PyObject *local = Py_None;
PyObject *global;
@@ -1746,6 +1778,7 @@ ctxmanager_new(PyTypeObject *type UNUSED, PyObject *args, PyObject *kwds)
PyObject *flags = Py_None;
PyObject *traps = Py_None;
+ decimal_state *state = GLOBAL_STATE();
CURRENT_CONTEXT(global);
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOOOOOOO", kwlist, &local,
&prec, &rounding, &Emin, &Emax, &capitals, &clamp, &flags, &traps)) {
@@ -1754,46 +1787,68 @@ ctxmanager_new(PyTypeObject *type UNUSED, PyObject *args, PyObject *kwds)
if (local == Py_None) {
local = global;
}
- else if (!PyDecContext_Check(local)) {
+ else if (!PyDecContext_Check(state, local)) {
PyErr_SetString(PyExc_TypeError,
"optional argument must be a context");
return NULL;
}
- self = PyObject_New(PyDecContextManagerObject,
- &PyDecContextManager_Type);
- if (self == NULL) {
- return NULL;
- }
-
- self->local = context_copy(local, NULL);
- if (self->local == NULL) {
- self->global = NULL;
- Py_DECREF(self);
+ PyObject *local_copy = context_copy(local, NULL);
+ if (local_copy == NULL) {
return NULL;
}
- self->global = Py_NewRef(global);
int ret = context_setattrs(
- self->local, prec, rounding,
+ local_copy, prec, rounding,
Emin, Emax, capitals,
clamp, flags, traps
);
-
if (ret < 0) {
- Py_DECREF(self);
+ Py_DECREF(local_copy);
return NULL;
}
+ PyDecContextManagerObject *self;
+ self = PyObject_GC_New(PyDecContextManagerObject,
+ state->PyDecContextManager_Type);
+ if (self == NULL) {
+ Py_DECREF(local_copy);
+ return NULL;
+ }
+
+ self->local = local_copy;
+ self->global = Py_NewRef(global);
+ PyObject_GC_Track(self);
+
return (PyObject *)self;
}
+static int
+ctxmanager_traverse(PyDecContextManagerObject *self, visitproc visit,
+ void *arg)
+{
+ Py_VISIT(Py_TYPE(self));
+ Py_VISIT(self->local);
+ Py_VISIT(self->global);
+ return 0;
+}
+
+static int
+ctxmanager_clear(PyDecContextManagerObject *self)
+{
+ Py_CLEAR(self->local);
+ Py_CLEAR(self->global);
+ return 0;
+}
+
static void
ctxmanager_dealloc(PyDecContextManagerObject *self)
{
- Py_XDECREF(self->local);
- Py_XDECREF(self->global);
- PyObject_Free(self);
+ PyTypeObject *tp = Py_TYPE(self);
+ PyObject_GC_UnTrack(self);
+ (void)ctxmanager_clear(self);
+ tp->tp_free((PyObject *)self);
+ Py_DECREF(tp);
}
static PyObject *
@@ -1832,36 +1887,21 @@ static PyMethodDef ctxmanager_methods[] = {
{NULL, NULL}
};
-static PyTypeObject PyDecContextManager_Type =
-{
- PyVarObject_HEAD_INIT(NULL, 0)
- "decimal.ContextManager", /* tp_name */
- sizeof(PyDecContextManagerObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- (destructor) ctxmanager_dealloc, /* tp_dealloc */
- 0, /* tp_vectorcall_offset */
- (getattrfunc) 0, /* tp_getattr */
- (setattrfunc) 0, /* tp_setattr */
- 0, /* tp_as_async */
- (reprfunc) 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- (getattrofunc) PyObject_GenericGetAttr, /* tp_getattro */
- (setattrofunc) 0, /* tp_setattro */
- (PyBufferProcs *) 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- ctxmanager_methods, /* tp_methods */
+static PyType_Slot ctxmanager_slots[] = {
+ {Py_tp_dealloc, ctxmanager_dealloc},
+ {Py_tp_getattro, PyObject_GenericGetAttr},
+ {Py_tp_traverse, ctxmanager_traverse},
+ {Py_tp_clear, ctxmanager_clear},
+ {Py_tp_methods, ctxmanager_methods},
+ {0, NULL},
+};
+
+static PyType_Spec ctxmanager_spec = {
+ .name = "decimal.ContextManager",
+ .basicsize = sizeof(PyDecContextManagerObject),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION),
+ .slots = ctxmanager_slots,
};
@@ -1874,8 +1914,9 @@ PyDecType_New(PyTypeObject *type)
{
PyDecObject *dec;
- if (type == &PyDec_Type) {
- dec = PyObject_New(PyDecObject, &PyDec_Type);
+ decimal_state *state = GLOBAL_STATE();
+ if (type == state->PyDec_Type) {
+ dec = PyObject_GC_New(PyDecObject, state->PyDec_Type);
}
else {
dec = (PyDecObject *)type->tp_alloc(type, 0);
@@ -1895,13 +1936,23 @@ PyDecType_New(PyTypeObject *type)
return (PyObject *)dec;
}
-#define dec_alloc() PyDecType_New(&PyDec_Type)
+#define dec_alloc(st) PyDecType_New((st)->PyDec_Type)
+
+static int
+dec_traverse(PyObject *dec, visitproc visit, void *arg)
+{
+ Py_VISIT(Py_TYPE(dec));
+ return 0;
+}
static void
dec_dealloc(PyObject *dec)
{
+ PyTypeObject *tp = Py_TYPE(dec);
+ PyObject_GC_UnTrack(dec);
mpd_del(MPD(dec));
- Py_TYPE(dec)->tp_free(dec);
+ tp->tp_free(dec);
+ Py_DECREF(tp);
}
@@ -2259,9 +2310,10 @@ PyDecType_FromFloatExact(PyTypeObject *type, PyObject *v,
uint32_t status = 0;
mpd_context_t maxctx;
-
- assert(PyType_IsSubtype(type, &PyDec_Type));
-
+#ifdef Py_DEBUG
+ decimal_state *state = GLOBAL_STATE();
+ assert(PyType_IsSubtype(type, state->PyDec_Type));
+#endif
if (PyLong_Check(v)) {
return PyDecType_FromLongExact(type, v, context);
}
@@ -2395,7 +2447,8 @@ PyDecType_FromDecimalExact(PyTypeObject *type, PyObject *v, PyObject *context)
PyObject *dec;
uint32_t status = 0;
- if (type == &PyDec_Type && PyDec_CheckExact(v)) {
+ decimal_state *state = GLOBAL_STATE();
+ if (type == state->PyDec_Type && PyDec_CheckExact(state, v)) {
return Py_NewRef(v);
}
@@ -2630,37 +2683,37 @@ PyDecType_FromSequenceExact(PyTypeObject *type, PyObject *v,
return dec;
}
-#define PyDec_FromCString(str, context) \
- PyDecType_FromCString(&PyDec_Type, str, context)
-#define PyDec_FromCStringExact(str, context) \
- PyDecType_FromCStringExact(&PyDec_Type, str, context)
+#define PyDec_FromCString(st, str, context) \
+ PyDecType_FromCString((st)->PyDec_Type, str, context)
+#define PyDec_FromCStringExact(st, str, context) \
+ PyDecType_FromCStringExact((st)->PyDec_Type, str, context)
-#define PyDec_FromUnicode(unicode, context) \
- PyDecType_FromUnicode(&PyDec_Type, unicode, context)
-#define PyDec_FromUnicodeExact(unicode, context) \
- PyDecType_FromUnicodeExact(&PyDec_Type, unicode, context)
-#define PyDec_FromUnicodeExactWS(unicode, context) \
- PyDecType_FromUnicodeExactWS(&PyDec_Type, unicode, context)
+#define PyDec_FromUnicode(st, unicode, context) \
+ PyDecType_FromUnicode((st)->PyDec_Type, unicode, context)
+#define PyDec_FromUnicodeExact(st, unicode, context) \
+ PyDecType_FromUnicodeExact((st)->PyDec_Type, unicode, context)
+#define PyDec_FromUnicodeExactWS(st, unicode, context) \
+ PyDecType_FromUnicodeExactWS((st)->PyDec_Type, unicode, context)
-#define PyDec_FromSsize(v, context) \
- PyDecType_FromSsize(&PyDec_Type, v, context)
-#define PyDec_FromSsizeExact(v, context) \
- PyDecType_FromSsizeExact(&PyDec_Type, v, context)
+#define PyDec_FromSsize(st, v, context) \
+ PyDecType_FromSsize((st)->PyDec_Type, v, context)
+#define PyDec_FromSsizeExact(st, v, context) \
+ PyDecType_FromSsizeExact((st)->PyDec_Type, v, context)
-#define PyDec_FromLong(pylong, context) \
- PyDecType_FromLong(&PyDec_Type, pylong, context)
-#define PyDec_FromLongExact(pylong, context) \
- PyDecType_FromLongExact(&PyDec_Type, pylong, context)
+#define PyDec_FromLong(st, pylong, context) \
+ PyDecType_FromLong((st)->PyDec_Type, pylong, context)
+#define PyDec_FromLongExact(st, pylong, context) \
+ PyDecType_FromLongExact((st)->PyDec_Type, pylong, context)
-#define PyDec_FromFloat(pyfloat, context) \
- PyDecType_FromFloat(&PyDec_Type, pyfloat, context)
-#define PyDec_FromFloatExact(pyfloat, context) \
- PyDecType_FromFloatExact(&PyDec_Type, pyfloat, context)
+#define PyDec_FromFloat(st, pyfloat, context) \
+ PyDecType_FromFloat((st)->PyDec_Type, pyfloat, context)
+#define PyDec_FromFloatExact(st, pyfloat, context) \
+ PyDecType_FromFloatExact((st)->PyDec_Type, pyfloat, context)
-#define PyDec_FromSequence(sequence, context) \
- PyDecType_FromSequence(&PyDec_Type, sequence, context)
-#define PyDec_FromSequenceExact(sequence, context) \
- PyDecType_FromSequenceExact(&PyDec_Type, sequence, context)
+#define PyDec_FromSequence(st, sequence, context) \
+ PyDecType_FromSequence((st)->PyDec_Type, sequence, context)
+#define PyDec_FromSequenceExact(st, sequence, context) \
+ PyDecType_FromSequenceExact((st)->PyDec_Type, sequence, context)
/* class method */
static PyObject *
@@ -2670,8 +2723,9 @@ dec_from_float(PyObject *type, PyObject *pyfloat)
PyObject *result;
CURRENT_CONTEXT(context);
- result = PyDecType_FromFloatExact(&PyDec_Type, pyfloat, context);
- if (type != (PyObject *)&PyDec_Type && result != NULL) {
+ decimal_state *state = GLOBAL_STATE();
+ result = PyDecType_FromFloatExact(state->PyDec_Type, pyfloat, context);
+ if (type != (PyObject *)state->PyDec_Type && result != NULL) {
Py_SETREF(result, PyObject_CallFunctionObjArgs(type, result, NULL));
}
@@ -2682,7 +2736,8 @@ dec_from_float(PyObject *type, PyObject *pyfloat)
static PyObject *
ctx_from_float(PyObject *context, PyObject *v)
{
- return PyDec_FromFloat(v, context);
+ decimal_state *state = GLOBAL_STATE();
+ return PyDec_FromFloat(state, v, context);
}
/* Apply the context to the input operand. Return a new PyDecObject. */
@@ -2692,7 +2747,8 @@ dec_apply(PyObject *v, PyObject *context)
PyObject *result;
uint32_t status = 0;
- result = dec_alloc();
+ decimal_state *state = GLOBAL_STATE();
+ result = dec_alloc(state);
if (result == NULL) {
return NULL;
}
@@ -2718,10 +2774,11 @@ dec_apply(PyObject *v, PyObject *context)
static PyObject *
PyDecType_FromObjectExact(PyTypeObject *type, PyObject *v, PyObject *context)
{
+ decimal_state *state = GLOBAL_STATE();
if (v == NULL) {
return PyDecType_FromSsizeExact(type, 0, context);
}
- else if (PyDec_Check(v)) {
+ else if (PyDec_Check(state, v)) {
return PyDecType_FromDecimalExact(type, v, context);
}
else if (PyUnicode_Check(v)) {
@@ -2752,10 +2809,11 @@ PyDecType_FromObjectExact(PyTypeObject *type, PyObject *v, PyObject *context)
static PyObject *
PyDec_FromObject(PyObject *v, PyObject *context)
{
+ decimal_state *state = GLOBAL_STATE();
if (v == NULL) {
- return PyDec_FromSsize(0, context);
+ return PyDec_FromSsize(state, 0, context);
}
- else if (PyDec_Check(v)) {
+ else if (PyDec_Check(state, v)) {
mpd_context_t *ctx = CTX(context);
if (mpd_isnan(MPD(v)) &&
MPD(v)->digits > ctx->prec - ctx->clamp) {
@@ -2764,7 +2822,7 @@ PyDec_FromObject(PyObject *v, PyObject *context)
if (dec_addstatus(context, MPD_Conversion_syntax)) {
return NULL;
}
- result = dec_alloc();
+ result = dec_alloc(state);
if (result == NULL) {
return NULL;
}
@@ -2774,19 +2832,19 @@ PyDec_FromObject(PyObject *v, PyObject *context)
return dec_apply(v, context);
}
else if (PyUnicode_Check(v)) {
- return PyDec_FromUnicode(v, context);
+ return PyDec_FromUnicode(state, v, context);
}
else if (PyLong_Check(v)) {
- return PyDec_FromLong(v, context);
+ return PyDec_FromLong(state, v, context);
}
else if (PyTuple_Check(v) || PyList_Check(v)) {
- return PyDec_FromSequence(v, context);
+ return PyDec_FromSequence(state, v, context);
}
else if (PyFloat_Check(v)) {
if (dec_addstatus(context, MPD_Float_operation)) {
return NULL;
}
- return PyDec_FromFloat(v, context);
+ return PyDec_FromFloat(state, v, context);
}
else {
PyErr_Format(PyExc_TypeError,
@@ -2807,7 +2865,8 @@ dec_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
&v, &context)) {
return NULL;
}
- CONTEXT_CHECK_VA(context);
+ decimal_state *state = GLOBAL_STATE();
+ CONTEXT_CHECK_VA(state, context);
return PyDecType_FromObjectExact(type, v, context);
}
@@ -2837,13 +2896,13 @@ ctx_create_decimal(PyObject *context, PyObject *args)
Py_LOCAL_INLINE(int)
convert_op(int type_err, PyObject **conv, PyObject *v, PyObject *context)
{
-
- if (PyDec_Check(v)) {
+ decimal_state *state = GLOBAL_STATE();
+ if (PyDec_Check(state, v)) {
*conv = Py_NewRef(v);
return 1;
}
if (PyLong_Check(v)) {
- *conv = PyDec_FromLongExact(v, context);
+ *conv = PyDec_FromLongExact(state, v, context);
if (*conv == NULL) {
return 0;
}
@@ -2942,7 +3001,8 @@ multiply_by_denominator(PyObject *v, PyObject *r, PyObject *context)
if (tmp == NULL) {
return NULL;
}
- denom = PyDec_FromLongExact(tmp, context);
+ decimal_state *state = GLOBAL_STATE();
+ denom = PyDec_FromLongExact(state, tmp, context);
Py_DECREF(tmp);
if (denom == NULL) {
return NULL;
@@ -2954,7 +3014,7 @@ multiply_by_denominator(PyObject *v, PyObject *r, PyObject *context)
PyErr_NoMemory();
return NULL;
}
- result = dec_alloc();
+ result = dec_alloc(state);
if (result == NULL) {
Py_DECREF(denom);
mpd_del(vv);
@@ -2996,7 +3056,8 @@ numerator_as_decimal(PyObject *r, PyObject *context)
return NULL;
}
- num = PyDec_FromLongExact(tmp, context);
+ decimal_state *state = GLOBAL_STATE();
+ num = PyDec_FromLongExact(state, tmp, context);
Py_DECREF(tmp);
return num;
}
@@ -3014,11 +3075,12 @@ convert_op_cmp(PyObject **vcmp, PyObject **wcmp, PyObject *v, PyObject *w,
*vcmp = v;
- if (PyDec_Check(w)) {
+ decimal_state *state = GLOBAL_STATE();
+ if (PyDec_Check(state, w)) {
*wcmp = Py_NewRef(w);
}
else if (PyLong_Check(w)) {
- *wcmp = PyDec_FromLongExact(w, context);
+ *wcmp = PyDec_FromLongExact(state, w, context);
}
else if (PyFloat_Check(w)) {
if (op != Py_EQ && op != Py_NE &&
@@ -3027,7 +3089,7 @@ convert_op_cmp(PyObject **vcmp, PyObject **wcmp, PyObject *v, PyObject *w,
}
else {
ctx->status |= MPD_Float_operation;
- *wcmp = PyDec_FromFloatExact(w, context);
+ *wcmp = PyDec_FromFloatExact(state, w, context);
}
}
else if (PyComplex_Check(w) && (op == Py_EQ || op == Py_NE)) {
@@ -3042,7 +3104,7 @@ convert_op_cmp(PyObject **vcmp, PyObject **wcmp, PyObject *v, PyObject *w,
}
else {
ctx->status |= MPD_Float_operation;
- *wcmp = PyDec_FromFloatExact(tmp, context);
+ *wcmp = PyDec_FromFloatExact(state, tmp, context);
Py_DECREF(tmp);
}
}
@@ -3556,7 +3618,8 @@ dec_as_integer_ratio(PyObject *self, PyObject *args UNUSED)
CURRENT_CONTEXT(context);
- tmp = dec_alloc();
+ decimal_state *state = GLOBAL_STATE();
+ tmp = dec_alloc(state);
if (tmp == NULL) {
return NULL;
}
@@ -3646,7 +3709,8 @@ PyDec_ToIntegralValue(PyObject *dec, PyObject *args, PyObject *kwds)
&rounding, &context)) {
return NULL;
}
- CONTEXT_CHECK_VA(context);
+ decimal_state *state = GLOBAL_STATE();
+ CONTEXT_CHECK_VA(state, context);
workctx = *CTX(context);
if (rounding != Py_None) {
@@ -3659,7 +3723,7 @@ PyDec_ToIntegralValue(PyObject *dec, PyObject *args, PyObject *kwds)
}
}
- result = dec_alloc();
+ result = dec_alloc(state);
if (result == NULL) {
return NULL;
}
@@ -3687,7 +3751,8 @@ PyDec_ToIntegralExact(PyObject *dec, PyObject *args, PyObject *kwds)
&rounding, &context)) {
return NULL;
}
- CONTEXT_CHECK_VA(context);
+ decimal_state *state = GLOBAL_STATE();
+ CONTEXT_CHECK_VA(state, context);
workctx = *CTX(context);
if (rounding != Py_None) {
@@ -3700,7 +3765,7 @@ PyDec_ToIntegralExact(PyObject *dec, PyObject *args, PyObject *kwds)
}
}
- result = dec_alloc();
+ result = dec_alloc(state);
if (result == NULL) {
return NULL;
}
@@ -3754,7 +3819,6 @@ PyDec_Round(PyObject *dec, PyObject *args)
uint32_t status = 0;
PyObject *context;
-
CURRENT_CONTEXT(context);
if (!PyArg_ParseTuple(args, "|O", &x)) {
return NULL;
@@ -3775,7 +3839,8 @@ PyDec_Round(PyObject *dec, PyObject *args)
if (y == -1 && PyErr_Occurred()) {
return NULL;
}
- result = dec_alloc();
+ decimal_state *state = GLOBAL_STATE();
+ result = dec_alloc(state);
if (result == NULL) {
return NULL;
}
@@ -3794,7 +3859,6 @@ PyDec_Round(PyObject *dec, PyObject *args)
}
}
-static PyTypeObject *DecimalTuple = NULL;
/* Return the DecimalTuple representation of a PyDecObject. */
static PyObject *
PyDec_AsTuple(PyObject *dec, PyObject *dummy UNUSED)
@@ -3877,7 +3941,8 @@ PyDec_AsTuple(PyObject *dec, PyObject *dummy UNUSED)
}
}
- result = PyObject_CallFunctionObjArgs((PyObject *)DecimalTuple,
+ decimal_state *state = GLOBAL_STATE();
+ result = PyObject_CallFunctionObjArgs((PyObject *)state->DecimalTuple,
sign, coeff, expt, NULL);
out:
@@ -3903,8 +3968,9 @@ nm_##MPDFUNC(PyObject *self) \
PyObject *context; \
uint32_t status = 0; \
\
+ decimal_state *state = GLOBAL_STATE(); \
CURRENT_CONTEXT(context); \
- if ((result = dec_alloc()) == NULL) { \
+ if ((result = dec_alloc(state)) == NULL) { \
return NULL; \
} \
\
@@ -3927,10 +3993,11 @@ nm_##MPDFUNC(PyObject *self, PyObject *other) \
PyObject *context; \
uint32_t status = 0; \
\
+ decimal_state *state = GLOBAL_STATE(); \
CURRENT_CONTEXT(context) ; \
CONVERT_BINOP(&a, &b, self, other, context); \
\
- if ((result = dec_alloc()) == NULL) { \
+ if ((result = dec_alloc(state)) == NULL) { \
Py_DECREF(a); \
Py_DECREF(b); \
return NULL; \
@@ -3967,7 +4034,8 @@ dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
&context)) { \
return NULL; \
} \
- CONTEXT_CHECK_VA(context); \
+ decimal_state *state = GLOBAL_STATE(); \
+ CONTEXT_CHECK_VA(state, context); \
\
return MPDFUNC(MPD(self), CTX(context)) ? incr_true() : incr_false(); \
}
@@ -3986,9 +4054,10 @@ dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
&context)) { \
return NULL; \
} \
- CONTEXT_CHECK_VA(context); \
+ decimal_state *state = GLOBAL_STATE(); \
+ CONTEXT_CHECK_VA(state, context); \
\
- if ((result = dec_alloc()) == NULL) { \
+ if ((result = dec_alloc(state)) == NULL) { \
return NULL; \
} \
\
@@ -4017,10 +4086,11 @@ dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
&other, &context)) { \
return NULL; \
} \
- CONTEXT_CHECK_VA(context); \
+ decimal_state *state = GLOBAL_STATE(); \
+ CONTEXT_CHECK_VA(state, context); \
CONVERT_BINOP_RAISE(&a, &b, self, other, context); \
\
- if ((result = dec_alloc()) == NULL) { \
+ if ((result = dec_alloc(state)) == NULL) { \
Py_DECREF(a); \
Py_DECREF(b); \
return NULL; \
@@ -4054,10 +4124,11 @@ dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
&other, &context)) { \
return NULL; \
} \
- CONTEXT_CHECK_VA(context); \
+ decimal_state *state = GLOBAL_STATE(); \
+ CONTEXT_CHECK_VA(state, context); \
CONVERT_BINOP_RAISE(&a, &b, self, other, context); \
\
- if ((result = dec_alloc()) == NULL) { \
+ if ((result = dec_alloc(state)) == NULL) { \
Py_DECREF(a); \
Py_DECREF(b); \
return NULL; \
@@ -4086,10 +4157,11 @@ dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \
&other, &third, &context)) { \
return NULL; \
} \
- CONTEXT_CHECK_VA(context); \
+ decimal_state *state = GLOBAL_STATE(); \
+ CONTEXT_CHECK_VA(state, context); \
CONVERT_TERNOP_RAISE(&a, &b, &c, self, other, third, context); \
\
- if ((result = dec_alloc()) == NULL) { \
+ if ((result = dec_alloc(state)) == NULL) { \
Py_DECREF(a); \
Py_DECREF(b); \
Py_DECREF(c); \
@@ -4151,13 +4223,14 @@ nm_mpd_qdivmod(PyObject *v, PyObject *w)
CURRENT_CONTEXT(context);
CONVERT_BINOP(&a, &b, v, w, context);
- q = dec_alloc();
+ decimal_state *state = GLOBAL_STATE();
+ q = dec_alloc(state);
if (q == NULL) {
Py_DECREF(a);
Py_DECREF(b);
return NULL;
}
- r = dec_alloc();
+ r = dec_alloc(state);
if (r == NULL) {
Py_DECREF(a);
Py_DECREF(b);
@@ -4199,7 +4272,8 @@ nm_mpd_qpow(PyObject *base, PyObject *exp, PyObject *mod)
}
}
- result = dec_alloc();
+ decimal_state *state = GLOBAL_STATE();
+ result = dec_alloc(state);
if (result == NULL) {
Py_DECREF(a);
Py_DECREF(b);
@@ -4300,7 +4374,8 @@ dec_mpd_radix(PyObject *self UNUSED, PyObject *dummy UNUSED)
{
PyObject *result;
- result = dec_alloc();
+ decimal_state *state = GLOBAL_STATE();
+ result = dec_alloc(state);
if (result == NULL) {
return NULL;
}
@@ -4315,7 +4390,8 @@ dec_mpd_qcopy_abs(PyObject *self, PyObject *dummy UNUSED)
PyObject *result;
uint32_t status = 0;
- if ((result = dec_alloc()) == NULL) {
+ decimal_state *state = GLOBAL_STATE();
+ if ((result = dec_alloc(state)) == NULL) {
return NULL;
}
@@ -4335,7 +4411,8 @@ dec_mpd_qcopy_negate(PyObject *self, PyObject *dummy UNUSED)
PyObject *result;
uint32_t status = 0;
- if ((result = dec_alloc()) == NULL) {
+ decimal_state *state = GLOBAL_STATE();
+ if ((result = dec_alloc(state)) == NULL) {
return NULL;
}
@@ -4364,7 +4441,8 @@ dec_mpd_class(PyObject *self, PyObject *args, PyObject *kwds)
&context)) {
return NULL;
}
- CONTEXT_CHECK_VA(context);
+ decimal_state *state = GLOBAL_STATE();
+ CONTEXT_CHECK_VA(state, context);
cp = mpd_class(MPD(self), CTX(context));
return PyUnicode_FromString(cp);
@@ -4383,7 +4461,8 @@ dec_mpd_to_eng(PyObject *self, PyObject *args, PyObject *kwds)
&context)) {
return NULL;
}
- CONTEXT_CHECK_VA(context);
+ decimal_state *state = GLOBAL_STATE();
+ CONTEXT_CHECK_VA(state, context);
size = mpd_to_eng_size(&s, MPD(self), CtxCaps(context));
if (size < 0) {
@@ -4415,10 +4494,11 @@ dec_mpd_qcopy_sign(PyObject *self, PyObject *args, PyObject *kwds)
&other, &context)) {
return NULL;
}
- CONTEXT_CHECK_VA(context);
+ decimal_state *state = GLOBAL_STATE();
+ CONTEXT_CHECK_VA(state, context);
CONVERT_BINOP_RAISE(&a, &b, self, other, context);
- result = dec_alloc();
+ result = dec_alloc(state);
if (result == NULL) {
Py_DECREF(a);
Py_DECREF(b);
@@ -4449,7 +4529,8 @@ dec_mpd_same_quantum(PyObject *self, PyObject *args, PyObject *kwds)
&other, &context)) {
return NULL;
}
- CONTEXT_CHECK_VA(context);
+ decimal_state *state = GLOBAL_STATE();
+ CONTEXT_CHECK_VA(state, context);
CONVERT_BINOP_RAISE(&a, &b, self, other, context);
result = mpd_same_quantum(MPD(a), MPD(b)) ? incr_true() : incr_false();
@@ -4483,7 +4564,8 @@ dec_mpd_qquantize(PyObject *v, PyObject *args, PyObject *kwds)
&w, &rounding, &context)) {
return NULL;
}
- CONTEXT_CHECK_VA(context);
+ decimal_state *state = GLOBAL_STATE();
+ CONTEXT_CHECK_VA(state, context);
workctx = *CTX(context);
if (rounding != Py_None) {
@@ -4498,7 +4580,7 @@ dec_mpd_qquantize(PyObject *v, PyObject *args, PyObject *kwds)
CONVERT_BINOP_RAISE(&a, &b, v, w, context);
- result = dec_alloc();
+ result = dec_alloc(state);
if (result == NULL) {
Py_DECREF(a);
Py_DECREF(b);
@@ -4527,8 +4609,10 @@ dec_richcompare(PyObject *v, PyObject *w, int op)
int a_issnan, b_issnan;
int r;
- assert(PyDec_Check(v));
-
+#ifdef Py_DEBUG
+ decimal_state *state = GLOBAL_STATE();
+ assert(PyDec_Check(state, v));
+#endif
CURRENT_CONTEXT(context);
CONVERT_BINOP_CMP(&a, &b, v, w, op, context);
@@ -4798,7 +4882,8 @@ dec_imag(PyObject *self UNUSED, void *closure UNUSED)
{
PyObject *result;
- result = dec_alloc();
+ decimal_state *state = GLOBAL_STATE();
+ result = dec_alloc(state);
if (result == NULL) {
return NULL;
}
@@ -4815,43 +4900,6 @@ static PyGetSetDef dec_getsets [] =
{NULL}
};
-static PyNumberMethods dec_number_methods =
-{
- (binaryfunc) nm_mpd_qadd,
- (binaryfunc) nm_mpd_qsub,
- (binaryfunc) nm_mpd_qmul,
- (binaryfunc) nm_mpd_qrem,
- (binaryfunc) nm_mpd_qdivmod,
- (ternaryfunc) nm_mpd_qpow,
- (unaryfunc) nm_mpd_qminus,
- (unaryfunc) nm_mpd_qplus,
- (unaryfunc) nm_mpd_qabs,
- (inquiry) nm_nonzero,
- (unaryfunc) 0, /* no bit-complement */
- (binaryfunc) 0, /* no shiftl */
- (binaryfunc) 0, /* no shiftr */
- (binaryfunc) 0, /* no bit-and */
- (binaryfunc) 0, /* no bit-xor */
- (binaryfunc) 0, /* no bit-ior */
- (unaryfunc) nm_dec_as_long,
- 0, /* nb_reserved */
- (unaryfunc) PyDec_AsFloat,
- 0, /* binaryfunc nb_inplace_add; */
- 0, /* binaryfunc nb_inplace_subtract; */
- 0, /* binaryfunc nb_inplace_multiply; */
- 0, /* binaryfunc nb_inplace_remainder; */
- 0, /* ternaryfunc nb_inplace_power; */
- 0, /* binaryfunc nb_inplace_lshift; */
- 0, /* binaryfunc nb_inplace_rshift; */
- 0, /* binaryfunc nb_inplace_and; */
- 0, /* binaryfunc nb_inplace_xor; */
- 0, /* binaryfunc nb_inplace_or; */
- (binaryfunc) nm_mpd_qdivint, /* binaryfunc nb_floor_divide; */
- (binaryfunc) nm_mpd_qdiv, /* binaryfunc nb_true_divide; */
- 0, /* binaryfunc nb_inplace_floor_divide; */
- 0, /* binaryfunc nb_inplace_true_divide; */
-};
-
static PyMethodDef dec_methods [] =
{
/* Unary arithmetic functions, optional context arg */
@@ -4944,48 +4992,44 @@ static PyMethodDef dec_methods [] =
{ NULL, NULL, 1 }
};
-static PyTypeObject PyDec_Type =
-{
- PyVarObject_HEAD_INIT(NULL, 0)
- "decimal.Decimal", /* tp_name */
- sizeof(PyDecObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- (destructor) dec_dealloc, /* tp_dealloc */
- 0, /* tp_vectorcall_offset */
- (getattrfunc) 0, /* tp_getattr */
- (setattrfunc) 0, /* tp_setattr */
- 0, /* tp_as_async */
- (reprfunc) dec_repr, /* tp_repr */
- &dec_number_methods, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc) dec_hash, /* tp_hash */
- 0, /* tp_call */
- (reprfunc) dec_str, /* tp_str */
- (getattrofunc) PyObject_GenericGetAttr, /* tp_getattro */
- (setattrofunc) 0, /* tp_setattro */
- (PyBufferProcs *) 0, /* tp_as_buffer */
- (Py_TPFLAGS_DEFAULT|
- Py_TPFLAGS_BASETYPE), /* tp_flags */
- doc_decimal, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- dec_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- dec_methods, /* tp_methods */
- 0, /* tp_members */
- dec_getsets, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- dec_new, /* tp_new */
- PyObject_Del, /* tp_free */
+static PyType_Slot dec_slots[] = {
+ {Py_tp_dealloc, dec_dealloc},
+ {Py_tp_getattro, PyObject_GenericGetAttr},
+ {Py_tp_traverse, dec_traverse},
+ {Py_tp_repr, dec_repr},
+ {Py_tp_hash, dec_hash},
+ {Py_tp_str, dec_str},
+ {Py_tp_doc, (void *)doc_decimal},
+ {Py_tp_richcompare, dec_richcompare},
+ {Py_tp_methods, dec_methods},
+ {Py_tp_getset, dec_getsets},
+ {Py_tp_new, dec_new},
+
+ // Number protocol
+ {Py_nb_add, nm_mpd_qadd},
+ {Py_nb_subtract, nm_mpd_qsub},
+ {Py_nb_multiply, nm_mpd_qmul},
+ {Py_nb_remainder, nm_mpd_qrem},
+ {Py_nb_divmod, nm_mpd_qdivmod},
+ {Py_nb_power, nm_mpd_qpow},
+ {Py_nb_negative, nm_mpd_qminus},
+ {Py_nb_positive, nm_mpd_qplus},
+ {Py_nb_absolute, nm_mpd_qabs},
+ {Py_nb_bool, nm_nonzero},
+ {Py_nb_int, nm_dec_as_long},
+ {Py_nb_float, PyDec_AsFloat},
+ {Py_nb_floor_divide, nm_mpd_qdivint},
+ {Py_nb_true_divide, nm_mpd_qdiv},
+ {0, NULL},
+};
+
+
+static PyType_Spec dec_spec = {
+ .name = "decimal.Decimal",
+ .basicsize = sizeof(PyDecObject),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
+ Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = dec_slots,
};
@@ -5037,8 +5081,8 @@ ctx_##MPDFUNC(PyObject *context, PyObject *v) \
uint32_t status = 0; \
\
CONVERT_OP_RAISE(&a, v, context); \
- \
- if ((result = dec_alloc()) == NULL) { \
+ decimal_state *state = GLOBAL_STATE(); \
+ if ((result = dec_alloc(state)) == NULL) { \
Py_DECREF(a); \
return NULL; \
} \
@@ -5068,8 +5112,8 @@ ctx_##MPDFUNC(PyObject *context, PyObject *args) \
} \
\
CONVERT_BINOP_RAISE(&a, &b, v, w, context); \
- \
- if ((result = dec_alloc()) == NULL) { \
+ decimal_state *state = GLOBAL_STATE(); \
+ if ((result = dec_alloc(state)) == NULL) { \
Py_DECREF(a); \
Py_DECREF(b); \
return NULL; \
@@ -5103,8 +5147,8 @@ ctx_##MPDFUNC(PyObject *context, PyObject *args) \
} \
\
CONVERT_BINOP_RAISE(&a, &b, v, w, context); \
- \
- if ((result = dec_alloc()) == NULL) { \
+ decimal_state *state = GLOBAL_STATE(); \
+ if ((result = dec_alloc(state)) == NULL) { \
Py_DECREF(a); \
Py_DECREF(b); \
return NULL; \
@@ -5132,8 +5176,8 @@ ctx_##MPDFUNC(PyObject *context, PyObject *args) \
} \
\
CONVERT_TERNOP_RAISE(&a, &b, &c, v, w, x, context); \
- \
- if ((result = dec_alloc()) == NULL) { \
+ decimal_state *state = GLOBAL_STATE(); \
+ if ((result = dec_alloc(state)) == NULL) { \
Py_DECREF(a); \
Py_DECREF(b); \
Py_DECREF(c); \
@@ -5198,14 +5242,14 @@ ctx_mpd_qdivmod(PyObject *context, PyObject *args)
}
CONVERT_BINOP_RAISE(&a, &b, v, w, context);
-
- q = dec_alloc();
+ decimal_state *state = GLOBAL_STATE();
+ q = dec_alloc(state);
if (q == NULL) {
Py_DECREF(a);
Py_DECREF(b);
return NULL;
}
- r = dec_alloc();
+ r = dec_alloc(state);
if (r == NULL) {
Py_DECREF(a);
Py_DECREF(b);
@@ -5253,7 +5297,8 @@ ctx_mpd_qpow(PyObject *context, PyObject *args, PyObject *kwds)
}
}
- result = dec_alloc();
+ decimal_state *state = GLOBAL_STATE();
+ result = dec_alloc(state);
if (result == NULL) {
Py_DECREF(a);
Py_DECREF(b);
@@ -5304,7 +5349,8 @@ DecCtx_BoolFunc_NO_CTX(mpd_iszero)
static PyObject *
ctx_iscanonical(PyObject *context UNUSED, PyObject *v)
{
- if (!PyDec_Check(v)) {
+ decimal_state *state = GLOBAL_STATE();
+ if (!PyDec_Check(state, v)) {
PyErr_SetString(PyExc_TypeError,
"argument must be a Decimal");
return NULL;
@@ -5329,7 +5375,8 @@ PyDecContext_Apply(PyObject *context, PyObject *v)
static PyObject *
ctx_canonical(PyObject *context UNUSED, PyObject *v)
{
- if (!PyDec_Check(v)) {
+ decimal_state *state = GLOBAL_STATE();
+ if (!PyDec_Check(state, v)) {
PyErr_SetString(PyExc_TypeError,
"argument must be a Decimal");
return NULL;
@@ -5345,8 +5392,8 @@ ctx_mpd_qcopy_abs(PyObject *context, PyObject *v)
uint32_t status = 0;
CONVERT_OP_RAISE(&a, v, context);
-
- result = dec_alloc();
+ decimal_state *state = GLOBAL_STATE();
+ result = dec_alloc(state);
if (result == NULL) {
Py_DECREF(a);
return NULL;
@@ -5378,8 +5425,8 @@ ctx_mpd_qcopy_negate(PyObject *context, PyObject *v)
uint32_t status = 0;
CONVERT_OP_RAISE(&a, v, context);
-
- result = dec_alloc();
+ decimal_state *state = GLOBAL_STATE();
+ result = dec_alloc(state);
if (result == NULL) {
Py_DECREF(a);
return NULL;
@@ -5475,8 +5522,8 @@ ctx_mpd_qcopy_sign(PyObject *context, PyObject *args)
}
CONVERT_BINOP_RAISE(&a, &b, v, w, context);
-
- result = dec_alloc();
+ decimal_state *state = GLOBAL_STATE();
+ result = dec_alloc(state);
if (result == NULL) {
Py_DECREF(a);
Py_DECREF(b);
@@ -5629,47 +5676,27 @@ static PyMethodDef context_methods [] =
{ NULL, NULL, 1 }
};
-static PyTypeObject PyDecContext_Type =
-{
- PyVarObject_HEAD_INIT(NULL, 0)
- "decimal.Context", /* tp_name */
- sizeof(PyDecContextObject), /* tp_basicsize */
- 0, /* tp_itemsize */
- (destructor) context_dealloc, /* tp_dealloc */
- 0, /* tp_vectorcall_offset */
- (getattrfunc) 0, /* tp_getattr */
- (setattrfunc) 0, /* tp_setattr */
- 0, /* tp_as_async */
- (reprfunc) context_repr, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- (hashfunc) 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- (getattrofunc) context_getattr, /* tp_getattro */
- (setattrofunc) context_setattr, /* tp_setattro */
- (PyBufferProcs *) 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
- doc_context, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- context_methods, /* tp_methods */
- 0, /* tp_members */
- context_getsets, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- context_init, /* tp_init */
- 0, /* tp_alloc */
- context_new, /* tp_new */
- PyObject_Del, /* tp_free */
+static PyType_Slot context_slots[] = {
+ {Py_tp_dealloc, context_dealloc},
+ {Py_tp_traverse, context_traverse},
+ {Py_tp_clear, context_clear},
+ {Py_tp_repr, context_repr},
+ {Py_tp_getattro, context_getattr},
+ {Py_tp_setattro, context_setattr},
+ {Py_tp_doc, (void *)doc_context},
+ {Py_tp_methods, context_methods},
+ {Py_tp_getset, context_getsets},
+ {Py_tp_init, context_init},
+ {Py_tp_new, context_new},
+ {0, NULL},
+};
+
+static PyType_Spec context_spec = {
+ .name = "decimal.Context",
+ .basicsize = sizeof(PyDecContextObject),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
+ Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
+ .slots = context_slots,
};
@@ -5794,6 +5821,7 @@ PyInit__decimal(void)
mpd_free = PyMem_Free;
mpd_setminalloc(_Py_DEC_MINALLOC);
+ decimal_state *state = GLOBAL_STATE();
/* Init external C-API functions */
_py_long_multiply = PyLong_Type.tp_as_number->nb_multiply;
@@ -5806,19 +5834,21 @@ PyInit__decimal(void)
/* Init types */
- PyDec_Type.tp_base = &PyBaseObject_Type;
- PyDecContext_Type.tp_base = &PyBaseObject_Type;
- PyDecContextManager_Type.tp_base = &PyBaseObject_Type;
- PyDecSignalDictMixin_Type.tp_base = &PyBaseObject_Type;
+#define CREATE_TYPE(mod, tp, spec) do { \
+ tp = (PyTypeObject *)PyType_FromMetaclass(NULL, mod, spec, NULL); \
+ CHECK_PTR(tp); \
+} while (0)
+
+ CREATE_TYPE(m, state->PyDec_Type, &dec_spec);
+ CREATE_TYPE(m, state->PyDecContext_Type, &context_spec);
+ CREATE_TYPE(m, state->PyDecContextManager_Type, &ctxmanager_spec);
+ CREATE_TYPE(m, state->PyDecSignalDictMixin_Type, &signaldict_spec);
- CHECK_INT(PyType_Ready(&PyDec_Type));
- CHECK_INT(PyType_Ready(&PyDecContext_Type));
- CHECK_INT(PyType_Ready(&PyDecSignalDictMixin_Type));
- CHECK_INT(PyType_Ready(&PyDecContextManager_Type));
+#undef CREATE_TYPE
ASSIGN_PTR(obj, PyUnicode_FromString("decimal"));
- CHECK_INT(PyDict_SetItemString(PyDec_Type.tp_dict, "__module__", obj));
- CHECK_INT(PyDict_SetItemString(PyDecContext_Type.tp_dict,
+ CHECK_INT(PyDict_SetItemString(state->PyDec_Type->tp_dict, "__module__", obj));
+ CHECK_INT(PyDict_SetItemString(state->PyDecContext_Type->tp_dict,
"__module__", obj));
Py_CLEAR(obj);
@@ -5828,7 +5858,7 @@ PyInit__decimal(void)
ASSIGN_PTR(Number, PyObject_GetAttrString(numbers, "Number"));
/* Register Decimal with the Number abstract base class */
ASSIGN_PTR(obj, PyObject_CallMethod(Number, "register", "(O)",
- (PyObject *)&PyDec_Type));
+ (PyObject *)state->PyDec_Type));
Py_CLEAR(obj);
/* Rational is a global variable used for fraction comparisons. */
ASSIGN_PTR(Rational, PyObject_GetAttrString(numbers, "Rational"));
@@ -5838,12 +5868,12 @@ PyInit__decimal(void)
/* DecimalTuple */
ASSIGN_PTR(collections, PyImport_ImportModule("collections"));
- ASSIGN_PTR(DecimalTuple, (PyTypeObject *)PyObject_CallMethod(collections,
+ ASSIGN_PTR(state->DecimalTuple, (PyTypeObject *)PyObject_CallMethod(collections,
"namedtuple", "(ss)", "DecimalTuple",
"sign digits exponent"));
ASSIGN_PTR(obj, PyUnicode_FromString("decimal"));
- CHECK_INT(PyDict_SetItemString(DecimalTuple->tp_dict, "__module__", obj));
+ CHECK_INT(PyDict_SetItemString(state->DecimalTuple->tp_dict, "__module__", obj));
Py_CLEAR(obj);
/* MutableMapping */
@@ -5851,10 +5881,10 @@ PyInit__decimal(void)
ASSIGN_PTR(MutableMapping, PyObject_GetAttrString(collections_abc,
"MutableMapping"));
/* Create SignalDict type */
- ASSIGN_PTR(PyDecSignalDict_Type,
+ ASSIGN_PTR(state->PyDecSignalDict_Type,
(PyTypeObject *)PyObject_CallFunction(
(PyObject *)&PyType_Type, "s(OO){}",
- "SignalDict", &PyDecSignalDictMixin_Type,
+ "SignalDict", state->PyDecSignalDictMixin_Type,
MutableMapping));
/* Done with collections, MutableMapping */
@@ -5867,10 +5897,9 @@ PyInit__decimal(void)
ASSIGN_PTR(m, PyModule_Create(&_decimal_module));
/* Add types to the module */
- CHECK_INT(PyModule_AddObject(m, "Decimal", Py_NewRef(&PyDec_Type)));
- CHECK_INT(PyModule_AddObject(m, "Context",
- Py_NewRef(&PyDecContext_Type)));
- CHECK_INT(PyModule_AddObject(m, "DecimalTuple", Py_NewRef(DecimalTuple)));
+ CHECK_INT(PyModule_AddType(m, state->PyDec_Type));
+ CHECK_INT(PyModule_AddType(m, state->PyDecContext_Type));
+ CHECK_INT(PyModule_AddType(m, state->DecimalTuple));
/* Create top level exception */
ASSIGN_PTR(DecimalException, PyErr_NewException(
@@ -5951,7 +5980,7 @@ PyInit__decimal(void)
/* Init default context template first */
ASSIGN_PTR(default_context_template,
- PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL));
+ PyObject_CallObject((PyObject *)state->PyDecContext_Type, NULL));
CHECK_INT(PyModule_AddObject(m, "DefaultContext",
Py_NewRef(default_context_template)));
@@ -5966,14 +5995,14 @@ PyInit__decimal(void)
/* Init basic context template */
ASSIGN_PTR(basic_context_template,
- PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL));
+ PyObject_CallObject((PyObject *)state->PyDecContext_Type, NULL));
init_basic_context(basic_context_template);
CHECK_INT(PyModule_AddObject(m, "BasicContext",
Py_NewRef(basic_context_template)));
/* Init extended context template */
ASSIGN_PTR(extended_context_template,
- PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL));
+ PyObject_CallObject((PyObject *)state->PyDecContext_Type, NULL));
init_extended_context(extended_context_template);
CHECK_INT(PyModule_AddObject(m, "ExtendedContext",
Py_NewRef(extended_context_template)));
@@ -6015,7 +6044,7 @@ PyInit__decimal(void)
Py_CLEAR(collections_abc); /* GCOV_NOT_REACHED */
Py_CLEAR(MutableMapping); /* GCOV_NOT_REACHED */
Py_CLEAR(SignalTuple); /* GCOV_NOT_REACHED */
- Py_CLEAR(DecimalTuple); /* GCOV_NOT_REACHED */
+ Py_CLEAR(state->DecimalTuple); /* GCOV_NOT_REACHED */
Py_CLEAR(default_context_template); /* GCOV_NOT_REACHED */
#ifndef WITH_DECIMAL_CONTEXTVAR
Py_CLEAR(tls_context_key); /* GCOV_NOT_REACHED */
diff --git a/Tools/c-analyzer/cpython/globals-to-fix.tsv b/Tools/c-analyzer/cpython/globals-to-fix.tsv
index 7ca14b91c841d..1131edff265ee 100644
--- a/Tools/c-analyzer/cpython/globals-to-fix.tsv
+++ b/Tools/c-analyzer/cpython/globals-to-fix.tsv
@@ -375,10 +375,6 @@ Modules/_datetimemodule.c - PyDateTime_IsoCalendarDateType -
Modules/_datetimemodule.c - PyDateTime_TZInfoType -
Modules/_datetimemodule.c - PyDateTime_TimeType -
Modules/_datetimemodule.c - PyDateTime_TimeZoneType -
-Modules/_decimal/_decimal.c - PyDecContextManager_Type -
-Modules/_decimal/_decimal.c - PyDecContext_Type -
-Modules/_decimal/_decimal.c - PyDecSignalDictMixin_Type -
-Modules/_decimal/_decimal.c - PyDec_Type -
Modules/xxmodule.c - Null_Type -
Modules/xxmodule.c - Str_Type -
Modules/xxmodule.c - Xxo_Type -
@@ -389,8 +385,6 @@ Modules/xxsubtype.c - spamlist_type -
## non-static types - initialized once
## heap types
-Modules/_decimal/_decimal.c - DecimalTuple -
-Modules/_decimal/_decimal.c - PyDecSignalDict_Type -
Modules/_tkinter.c - PyTclObject_Type -
Modules/_tkinter.c - Tkapp_Type -
Modules/_tkinter.c - Tktt_Type -
@@ -428,6 +422,7 @@ Modules/_datetimemodule.c - us_per_hour -
Modules/_datetimemodule.c - us_per_day -
Modules/_datetimemodule.c - us_per_week -
Modules/_datetimemodule.c - seconds_per_day -
+Modules/_decimal/_decimal.c - global_state -
Modules/_decimal/_decimal.c - basic_context_template -
Modules/_decimal/_decimal.c - current_context_var -
Modules/_decimal/_decimal.c - default_context_template -
1
0
https://github.com/python/cpython/commit/0e24499129f3917b199a6d46fa33eeedd2…
commit: 0e24499129f3917b199a6d46fa33eeedd2c447fc
branch: main
author: Serhiy Storchaka <storchaka(a)gmail.com>
committer: serhiy-storchaka <storchaka(a)gmail.com>
date: 2023-06-29T12:53:22+03:00
summary:
gh-89392: Make test_decimal discoverable (GH-106209)
files:
M Lib/test/test_decimal.py
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index 67ccaab40c5ed..749496e3e9455 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -20,7 +20,7 @@
This test module can be called from command line with one parameter (Arithmetic
or Behaviour) to test each part, or without parameter to test both parts. If
-you're working through IDLE, you can import this test module and call test_main()
+you're working through IDLE, you can import this test module and call test()
with the corresponding argument.
"""
@@ -32,7 +32,7 @@
import unittest
import numbers
import locale
-from test.support import (run_unittest, run_doctest, is_resource_enabled,
+from test.support import (is_resource_enabled,
requires_IEEE_754, requires_docstrings,
requires_legacy_unicode_capi, check_sanitizer)
from test.support import (TestFailed,
@@ -62,6 +62,7 @@
fractions = {C:cfractions, P:pfractions}
sys.modules['decimal'] = orig_sys_decimal
+requires_cdecimal = unittest.skipUnless(C, "test requires C version")
# Useful Test Constant
Signals = {
@@ -99,7 +100,7 @@ def assert_signals(cls, context, attr, expected):
]
# Tests are built around these assumed context defaults.
-# test_main() restores the original context.
+# test() restores the original context.
ORIGINAL_CONTEXT = {
C: C.getcontext().copy() if C else None,
P: P.getcontext().copy()
@@ -133,7 +134,7 @@ def init(m):
EXTRA_FUNCTIONALITY, "test requires regular build")
-class IBMTestCases(unittest.TestCase):
+class IBMTestCases:
"""Class which tests the Decimal class against the IBM test cases."""
def setUp(self):
@@ -488,14 +489,10 @@ def change_max_exponent(self, exp):
def change_clamp(self, clamp):
self.context.clamp = clamp
-class CIBMTestCases(IBMTestCases):
- decimal = C
-class PyIBMTestCases(IBMTestCases):
- decimal = P
# The following classes test the behaviour of Decimal according to PEP 327
-class ExplicitConstructionTest(unittest.TestCase):
+class ExplicitConstructionTest:
'''Unit tests for Explicit Construction cases of Decimal.'''
def test_explicit_empty(self):
@@ -838,12 +835,13 @@ def test_unicode_digits(self):
for input, expected in test_values.items():
self.assertEqual(str(Decimal(input)), expected)
-class CExplicitConstructionTest(ExplicitConstructionTest):
+@requires_cdecimal
+class CExplicitConstructionTest(ExplicitConstructionTest, unittest.TestCase):
decimal = C
-class PyExplicitConstructionTest(ExplicitConstructionTest):
+class PyExplicitConstructionTest(ExplicitConstructionTest, unittest.TestCase):
decimal = P
-class ImplicitConstructionTest(unittest.TestCase):
+class ImplicitConstructionTest:
'''Unit tests for Implicit Construction cases of Decimal.'''
def test_implicit_from_None(self):
@@ -920,12 +918,13 @@ def __ne__(self, other):
self.assertEqual(eval('Decimal(10)' + sym + 'E()'),
'10' + rop + 'str')
-class CImplicitConstructionTest(ImplicitConstructionTest):
+@requires_cdecimal
+class CImplicitConstructionTest(ImplicitConstructionTest, unittest.TestCase):
decimal = C
-class PyImplicitConstructionTest(ImplicitConstructionTest):
+class PyImplicitConstructionTest(ImplicitConstructionTest, unittest.TestCase):
decimal = P
-class FormatTest(unittest.TestCase):
+class FormatTest:
'''Unit tests for the format function.'''
def test_formatting(self):
Decimal = self.decimal.Decimal
@@ -1262,12 +1261,13 @@ def __init__(self, a):
a = A.from_float(42)
self.assertEqual(self.decimal.Decimal, a.a_type)
-class CFormatTest(FormatTest):
+@requires_cdecimal
+class CFormatTest(FormatTest, unittest.TestCase):
decimal = C
-class PyFormatTest(FormatTest):
+class PyFormatTest(FormatTest, unittest.TestCase):
decimal = P
-class ArithmeticOperatorsTest(unittest.TestCase):
+class ArithmeticOperatorsTest:
'''Unit tests for all arithmetic operators, binary and unary.'''
def test_addition(self):
@@ -1523,14 +1523,17 @@ def test_nan_comparisons(self):
equality_ops = operator.eq, operator.ne
# results when InvalidOperation is not trapped
- for x, y in qnan_pairs + snan_pairs:
- for op in order_ops + equality_ops:
- got = op(x, y)
- expected = True if op is operator.ne else False
- self.assertIs(expected, got,
- "expected {0!r} for operator.{1}({2!r}, {3!r}); "
- "got {4!r}".format(
- expected, op.__name__, x, y, got))
+ with localcontext() as ctx:
+ ctx.traps[InvalidOperation] = 0
+
+ for x, y in qnan_pairs + snan_pairs:
+ for op in order_ops + equality_ops:
+ got = op(x, y)
+ expected = True if op is operator.ne else False
+ self.assertIs(expected, got,
+ "expected {0!r} for operator.{1}({2!r}, {3!r}); "
+ "got {4!r}".format(
+ expected, op.__name__, x, y, got))
# repeat the above, but this time trap the InvalidOperation
with localcontext() as ctx:
@@ -1562,9 +1565,10 @@ def test_copy_sign(self):
self.assertEqual(Decimal(1).copy_sign(-2), d)
self.assertRaises(TypeError, Decimal(1).copy_sign, '-2')
-class CArithmeticOperatorsTest(ArithmeticOperatorsTest):
+@requires_cdecimal
+class CArithmeticOperatorsTest(ArithmeticOperatorsTest, unittest.TestCase):
decimal = C
-class PyArithmeticOperatorsTest(ArithmeticOperatorsTest):
+class PyArithmeticOperatorsTest(ArithmeticOperatorsTest, unittest.TestCase):
decimal = P
# The following are two functions used to test threading in the next class
@@ -1654,7 +1658,7 @@ def thfunc2(cls):
@threading_helper.requires_working_threading()
-class ThreadingTest(unittest.TestCase):
+class ThreadingTest:
'''Unit tests for thread local contexts in Decimal.'''
# Take care executing this test from IDLE, there's an issue in threading
@@ -1699,13 +1703,14 @@ def test_threading(self):
DefaultContext.Emin = save_emin
-class CThreadingTest(ThreadingTest):
+@requires_cdecimal
+class CThreadingTest(ThreadingTest, unittest.TestCase):
decimal = C
-class PyThreadingTest(ThreadingTest):
+class PyThreadingTest(ThreadingTest, unittest.TestCase):
decimal = P
-class UsabilityTest(unittest.TestCase):
+class UsabilityTest:
'''Unit tests for Usability cases of Decimal.'''
def test_comparison_operators(self):
@@ -2521,9 +2526,10 @@ def test_conversions_from_int(self):
self.assertEqual(Decimal(-12).fma(45, Decimal(67)),
Decimal(-12).fma(Decimal(45), Decimal(67)))
-class CUsabilityTest(UsabilityTest):
+@requires_cdecimal
+class CUsabilityTest(UsabilityTest, unittest.TestCase):
decimal = C
-class PyUsabilityTest(UsabilityTest):
+class PyUsabilityTest(UsabilityTest, unittest.TestCase):
decimal = P
def setUp(self):
@@ -2535,7 +2541,7 @@ def tearDown(self):
sys.set_int_max_str_digits(self._previous_int_limit)
super().tearDown()
-class PythonAPItests(unittest.TestCase):
+class PythonAPItests:
def test_abc(self):
Decimal = self.decimal.Decimal
@@ -2884,12 +2890,13 @@ def test_exception_hierarchy(self):
self.assertTrue(issubclass(decimal.DivisionUndefined, ZeroDivisionError))
self.assertTrue(issubclass(decimal.InvalidContext, InvalidOperation))
-class CPythonAPItests(PythonAPItests):
+@requires_cdecimal
+class CPythonAPItests(PythonAPItests, unittest.TestCase):
decimal = C
-class PyPythonAPItests(PythonAPItests):
+class PyPythonAPItests(PythonAPItests, unittest.TestCase):
decimal = P
-class ContextAPItests(unittest.TestCase):
+class ContextAPItests:
def test_none_args(self):
Context = self.decimal.Context
@@ -3635,12 +3642,13 @@ def test_to_integral_value(self):
self.assertRaises(TypeError, c.to_integral_value, '10')
self.assertRaises(TypeError, c.to_integral_value, 10, 'x')
-class CContextAPItests(ContextAPItests):
+@requires_cdecimal
+class CContextAPItests(ContextAPItests, unittest.TestCase):
decimal = C
-class PyContextAPItests(ContextAPItests):
+class PyContextAPItests(ContextAPItests, unittest.TestCase):
decimal = P
-class ContextWithStatement(unittest.TestCase):
+class ContextWithStatement:
# Can't do these as docstrings until Python 2.6
# as doctest can't handle __future__ statements
@@ -3704,9 +3712,13 @@ def test_localcontext_kwargs(self):
def test_local_context_kwargs_does_not_overwrite_existing_argument(self):
ctx = self.decimal.getcontext()
- ctx.prec = 28
+ orig_prec = ctx.prec
with self.decimal.localcontext(prec=10) as ctx2:
- self.assertEqual(ctx.prec, 28)
+ self.assertEqual(ctx2.prec, 10)
+ self.assertEqual(ctx.prec, orig_prec)
+ with self.decimal.localcontext(prec=20) as ctx2:
+ self.assertEqual(ctx2.prec, 20)
+ self.assertEqual(ctx.prec, orig_prec)
def test_nested_with_statements(self):
# Use a copy of the supplied context in the block
@@ -3800,12 +3812,13 @@ def test_with_statements_gc3(self):
self.assertEqual(c4.prec, 4)
del c4
-class CContextWithStatement(ContextWithStatement):
+@requires_cdecimal
+class CContextWithStatement(ContextWithStatement, unittest.TestCase):
decimal = C
-class PyContextWithStatement(ContextWithStatement):
+class PyContextWithStatement(ContextWithStatement, unittest.TestCase):
decimal = P
-class ContextFlags(unittest.TestCase):
+class ContextFlags:
def test_flags_irrelevant(self):
# check that the result (numeric result + flags raised) of an
@@ -4072,12 +4085,13 @@ def test_float_operation_default(self):
self.assertTrue(context.traps[FloatOperation])
self.assertTrue(context.traps[Inexact])
-class CContextFlags(ContextFlags):
+@requires_cdecimal
+class CContextFlags(ContextFlags, unittest.TestCase):
decimal = C
-class PyContextFlags(ContextFlags):
+class PyContextFlags(ContextFlags, unittest.TestCase):
decimal = P
-class SpecialContexts(unittest.TestCase):
+class SpecialContexts:
"""Test the context templates."""
def test_context_templates(self):
@@ -4157,12 +4171,13 @@ def test_default_context(self):
if ex:
raise ex
-class CSpecialContexts(SpecialContexts):
+@requires_cdecimal
+class CSpecialContexts(SpecialContexts, unittest.TestCase):
decimal = C
-class PySpecialContexts(SpecialContexts):
+class PySpecialContexts(SpecialContexts, unittest.TestCase):
decimal = P
-class ContextInputValidation(unittest.TestCase):
+class ContextInputValidation:
def test_invalid_context(self):
Context = self.decimal.Context
@@ -4224,12 +4239,13 @@ def test_invalid_context(self):
self.assertRaises(TypeError, Context, flags=(0,1))
self.assertRaises(TypeError, Context, traps=(1,0))
-class CContextInputValidation(ContextInputValidation):
+@requires_cdecimal
+class CContextInputValidation(ContextInputValidation, unittest.TestCase):
decimal = C
-class PyContextInputValidation(ContextInputValidation):
+class PyContextInputValidation(ContextInputValidation, unittest.TestCase):
decimal = P
-class ContextSubclassing(unittest.TestCase):
+class ContextSubclassing:
def test_context_subclassing(self):
decimal = self.decimal
@@ -4338,12 +4354,14 @@ def __init__(self, prec=None, rounding=None, Emin=None, Emax=None,
for signal in OrderedSignals[decimal]:
self.assertFalse(c.traps[signal])
-class CContextSubclassing(ContextSubclassing):
+@requires_cdecimal
+class CContextSubclassing(ContextSubclassing, unittest.TestCase):
decimal = C
-class PyContextSubclassing(ContextSubclassing):
+class PyContextSubclassing(ContextSubclassing, unittest.TestCase):
decimal = P
@skip_if_extra_functionality
+@requires_cdecimal
class CheckAttributes(unittest.TestCase):
def test_module_attributes(self):
@@ -4373,7 +4391,7 @@ def test_decimal_attributes(self):
y = [s for s in dir(C.Decimal(9)) if '__' in s or not s.startswith('_')]
self.assertEqual(set(x) - set(y), set())
-class Coverage(unittest.TestCase):
+class Coverage:
def test_adjusted(self):
Decimal = self.decimal.Decimal
@@ -4630,9 +4648,10 @@ def test_copy(self):
y = c.copy_sign(x, 1)
self.assertEqual(y, -x)
-class CCoverage(Coverage):
+@requires_cdecimal
+class CCoverage(Coverage, unittest.TestCase):
decimal = C
-class PyCoverage(Coverage):
+class PyCoverage(Coverage, unittest.TestCase):
decimal = P
def setUp(self):
@@ -4885,6 +4904,7 @@ def test_constants(self):
self.assertEqual(C.DecTraps,
C.DecErrors|C.DecOverflow|C.DecUnderflow)
+@requires_cdecimal
class CWhitebox(unittest.TestCase):
"""Whitebox testing for _decimal"""
@@ -5663,7 +5683,7 @@ def test_maxcontext_exact_arith(self):
@requires_docstrings
-(a)unittest.skipUnless(C, "test requires C version")
+@requires_cdecimal
class SignatureTest(unittest.TestCase):
"""Function signatures"""
@@ -5799,52 +5819,10 @@ def doit(ty):
doit('Context')
-all_tests = [
- CExplicitConstructionTest, PyExplicitConstructionTest,
- CImplicitConstructionTest, PyImplicitConstructionTest,
- CFormatTest, PyFormatTest,
- CArithmeticOperatorsTest, PyArithmeticOperatorsTest,
- CThreadingTest, PyThreadingTest,
- CUsabilityTest, PyUsabilityTest,
- CPythonAPItests, PyPythonAPItests,
- CContextAPItests, PyContextAPItests,
- CContextWithStatement, PyContextWithStatement,
- CContextFlags, PyContextFlags,
- CSpecialContexts, PySpecialContexts,
- CContextInputValidation, PyContextInputValidation,
- CContextSubclassing, PyContextSubclassing,
- CCoverage, PyCoverage,
- CFunctionality, PyFunctionality,
- CWhitebox, PyWhitebox,
- CIBMTestCases, PyIBMTestCases,
-]
-
-# Delete C tests if _decimal.so is not present.
-if not C:
- all_tests = all_tests[1::2]
-else:
- all_tests.insert(0, CheckAttributes)
- all_tests.insert(1, SignatureTest)
-
-
-def test_main(arith=None, verbose=None, todo_tests=None, debug=None):
- """ Execute the tests.
-
- Runs all arithmetic tests if arith is True or if the "decimal" resource
- is enabled in regrtest.py
- """
-
- init(C)
- init(P)
- global TEST_ALL, DEBUG
- TEST_ALL = arith if arith is not None else is_resource_enabled('decimal')
- DEBUG = debug
-
- if todo_tests is None:
- test_classes = all_tests
- else:
- test_classes = [CIBMTestCases, PyIBMTestCases]
-
+def load_tests(loader, tests, pattern):
+ if TODO_TESTS is not None:
+ # Run only Arithmetic tests
+ tests = loader.suiteClass()
# Dynamically build custom test definition for each file in the test
# directory and add the definitions to the DecimalTest class. This
# procedure insures that new files do not get skipped.
@@ -5852,34 +5830,69 @@ def test_main(arith=None, verbose=None, todo_tests=None, debug=None):
if '.decTest' not in filename or filename.startswith("."):
continue
head, tail = filename.split('.')
- if todo_tests is not None and head not in todo_tests:
+ if TODO_TESTS is not None and head not in TODO_TESTS:
continue
tester = lambda self, f=filename: self.eval_file(directory + f)
- setattr(CIBMTestCases, 'test_' + head, tester)
- setattr(PyIBMTestCases, 'test_' + head, tester)
+ setattr(IBMTestCases, 'test_' + head, tester)
del filename, head, tail, tester
+ for prefix, mod in ('C', C), ('Py', P):
+ if not mod:
+ continue
+ test_class = type(prefix + 'IBMTestCases',
+ (IBMTestCases, unittest.TestCase),
+ {'decimal': mod})
+ tests.addTest(loader.loadTestsFromTestCase(test_class))
+
+ if TODO_TESTS is None:
+ from doctest import DocTestSuite, IGNORE_EXCEPTION_DETAIL
+ for mod in C, P:
+ if not mod:
+ continue
+ def setUp(slf, mod=mod):
+ sys.modules['decimal'] = mod
+ def tearDown(slf):
+ sys.modules['decimal'] = orig_sys_decimal
+ optionflags = IGNORE_EXCEPTION_DETAIL if mod is C else 0
+ sys.modules['decimal'] = mod
+ tests.addTest(DocTestSuite(mod, setUp=setUp, tearDown=tearDown,
+ optionflags=optionflags))
+ sys.modules['decimal'] = orig_sys_decimal
+ return tests
+
+def setUpModule():
+ init(C)
+ init(P)
+ global TEST_ALL
+ TEST_ALL = ARITH if ARITH is not None else is_resource_enabled('decimal')
+
+def tearDownModule():
+ if C: C.setcontext(ORIGINAL_CONTEXT[C])
+ P.setcontext(ORIGINAL_CONTEXT[P])
+ if not C:
+ warnings.warn('C tests skipped: no module named _decimal.',
+ UserWarning)
+ if not orig_sys_decimal is sys.modules['decimal']:
+ raise TestFailed("Internal error: unbalanced number of changes to "
+ "sys.modules['decimal'].")
+
+
+ARITH = None
+TEST_ALL = True
+TODO_TESTS = None
+DEBUG = False
+
+def test(arith=None, verbose=None, todo_tests=None, debug=None):
+ """ Execute the tests.
+ Runs all arithmetic tests if arith is True or if the "decimal" resource
+ is enabled in regrtest.py
+ """
- try:
- run_unittest(*test_classes)
- if todo_tests is None:
- from doctest import IGNORE_EXCEPTION_DETAIL
- savedecimal = sys.modules['decimal']
- if C:
- sys.modules['decimal'] = C
- run_doctest(C, verbose, optionflags=IGNORE_EXCEPTION_DETAIL)
- sys.modules['decimal'] = P
- run_doctest(P, verbose)
- sys.modules['decimal'] = savedecimal
- finally:
- if C: C.setcontext(ORIGINAL_CONTEXT[C])
- P.setcontext(ORIGINAL_CONTEXT[P])
- if not C:
- warnings.warn('C tests skipped: no module named _decimal.',
- UserWarning)
- if not orig_sys_decimal is sys.modules['decimal']:
- raise TestFailed("Internal error: unbalanced number of changes to "
- "sys.modules['decimal'].")
+ global ARITH, TODO_TESTS, DEBUG
+ ARITH = arith
+ TODO_TESTS = todo_tests
+ DEBUG = debug
+ unittest.main(__name__, verbosity=2 if verbose else 1, exit=False, argv=[__name__])
if __name__ == '__main__':
@@ -5890,8 +5903,8 @@ def test_main(arith=None, verbose=None, todo_tests=None, debug=None):
(opt, args) = p.parse_args()
if opt.skip:
- test_main(arith=False, verbose=True)
+ test(arith=False, verbose=True)
elif args:
- test_main(arith=True, verbose=True, todo_tests=args, debug=opt.debug)
+ test(arith=True, verbose=True, todo_tests=args, debug=opt.debug)
else:
- test_main(arith=True, verbose=True)
+ test(arith=True, verbose=True)
1
0
gh-106033: Get rid of PyDict_GetItem in _PyFunction_FromConstructor (GH-106044)
by serhiy-storchaka 29 Jun '23
by serhiy-storchaka 29 Jun '23
29 Jun '23
https://github.com/python/cpython/commit/08c08d21b03d949452a77d9ed5e3cf48d6…
commit: 08c08d21b03d949452a77d9ed5e3cf48d6b9804d
branch: main
author: Serhiy Storchaka <storchaka(a)gmail.com>
committer: serhiy-storchaka <storchaka(a)gmail.com>
date: 2023-06-29T12:31:08+03:00
summary:
gh-106033: Get rid of PyDict_GetItem in _PyFunction_FromConstructor (GH-106044)
files:
M Objects/funcobject.c
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index 753038600aa85..f43e3a2787b84 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -106,9 +106,14 @@ PyFunction_ClearWatcher(int watcher_id)
PyFunctionObject *
_PyFunction_FromConstructor(PyFrameConstructor *constr)
{
+ PyObject *module = Py_XNewRef(PyDict_GetItemWithError(constr->fc_globals, &_Py_ID(__name__)));
+ if (!module && PyErr_Occurred()) {
+ return NULL;
+ }
PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
if (op == NULL) {
+ Py_XDECREF(module);
return NULL;
}
op->func_globals = Py_NewRef(constr->fc_globals);
@@ -122,10 +127,7 @@ _PyFunction_FromConstructor(PyFrameConstructor *constr)
op->func_doc = Py_NewRef(Py_None);
op->func_dict = NULL;
op->func_weakreflist = NULL;
- op->func_module = Py_XNewRef(PyDict_GetItem(op->func_globals, &_Py_ID(__name__)));
- if (!op->func_module) {
- PyErr_Clear();
- }
+ op->func_module = module;
op->func_annotations = NULL;
op->func_typeparams = NULL;
op->vectorcall = _PyFunction_Vectorcall;
1
0
29 Jun '23
https://github.com/python/cpython/commit/8bf6904b229583033035d91a3800da5604…
commit: 8bf6904b229583033035d91a3800da5604dcaad4
branch: main
author: Serhiy Storchaka <storchaka(a)gmail.com>
committer: serhiy-storchaka <storchaka(a)gmail.com>
date: 2023-06-29T12:22:19+03:00
summary:
gh-101006: Improve error handling when read marshal data (GH-101007)
* EOFError no longer overrides other errors such as MemoryError or OSError at
the start of the object.
* Raise more relevant error when the NULL object occurs as a code object
component.
* Minimize an overhead of calling PyErr_Occurred().
files:
A Misc/NEWS.d/next/Core and Builtins/2023-01-13-11-37-41.gh-issue-101006.fuLvn2.rst
M Python/marshal.c
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-01-13-11-37-41.gh-issue-101006.fuLvn2.rst b/Misc/NEWS.d/next/Core and Builtins/2023-01-13-11-37-41.gh-issue-101006.fuLvn2.rst
new file mode 100644
index 0000000000000..c98670d8c4963
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-01-13-11-37-41.gh-issue-101006.fuLvn2.rst
@@ -0,0 +1 @@
+Improve error handling when read :mod:`marshal` data.
diff --git a/Python/marshal.c b/Python/marshal.c
index 972a13876e626..7cfc7cc00306f 100644
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -751,23 +751,28 @@ r_string(Py_ssize_t n, RFILE *p)
static int
r_byte(RFILE *p)
{
- int c = EOF;
-
if (p->ptr != NULL) {
- if (p->ptr < p->end)
- c = (unsigned char) *p->ptr++;
- return c;
+ if (p->ptr < p->end) {
+ return (unsigned char) *p->ptr++;
+ }
}
- if (!p->readable) {
+ else if (!p->readable) {
assert(p->fp);
- c = getc(p->fp);
+ int c = getc(p->fp);
+ if (c != EOF) {
+ return c;
+ }
}
else {
const char *ptr = r_string(1, p);
- if (ptr != NULL)
- c = *(const unsigned char *) ptr;
+ if (ptr != NULL) {
+ return *(const unsigned char *) ptr;
+ }
+ return EOF;
}
- return c;
+ PyErr_SetString(PyExc_EOFError,
+ "EOF read where not expected");
+ return EOF;
}
static int
@@ -828,10 +833,11 @@ r_PyLong(RFILE *p)
digit d;
n = r_long(p);
- if (PyErr_Occurred())
- return NULL;
if (n == 0)
return (PyObject *)_PyLong_New(0);
+ if (n == -1 && PyErr_Occurred()) {
+ return NULL;
+ }
if (n < -SIZE32_MAX || n > SIZE32_MAX) {
PyErr_SetString(PyExc_ValueError,
"bad marshal data (long size out of range)");
@@ -850,10 +856,6 @@ r_PyLong(RFILE *p)
d = 0;
for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
md = r_short(p);
- if (PyErr_Occurred()) {
- Py_DECREF(ob);
- return NULL;
- }
if (md < 0 || md > PyLong_MARSHAL_BASE)
goto bad_digit;
d += (digit)md << j*PyLong_MARSHAL_SHIFT;
@@ -864,10 +866,6 @@ r_PyLong(RFILE *p)
d = 0;
for (j=0; j < shorts_in_top_digit; j++) {
md = r_short(p);
- if (PyErr_Occurred()) {
- Py_DECREF(ob);
- return NULL;
- }
if (md < 0 || md > PyLong_MARSHAL_BASE)
goto bad_digit;
/* topmost marshal digit should be nonzero */
@@ -879,18 +877,17 @@ r_PyLong(RFILE *p)
}
d += (digit)md << j*PyLong_MARSHAL_SHIFT;
}
- if (PyErr_Occurred()) {
- Py_DECREF(ob);
- return NULL;
- }
+ assert(!PyErr_Occurred());
/* top digit should be nonzero, else the resulting PyLong won't be
normalized */
ob->long_value.ob_digit[size-1] = d;
return (PyObject *)ob;
bad_digit:
Py_DECREF(ob);
- PyErr_SetString(PyExc_ValueError,
- "bad marshal data (digit out of range in long)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (digit out of range in long)");
+ }
return NULL;
}
@@ -913,8 +910,6 @@ r_float_str(RFILE *p)
const char *ptr;
n = r_byte(p);
if (n == EOF) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
return -1;
}
ptr = r_string(n, p);
@@ -992,8 +987,10 @@ r_object(RFILE *p)
PyObject *retval = NULL;
if (code == EOF) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
+ if (PyErr_ExceptionMatches(PyExc_EOFError)) {
+ PyErr_SetString(PyExc_EOFError,
+ "EOF read where object expected");
+ }
return NULL;
}
@@ -1040,7 +1037,10 @@ r_object(RFILE *p)
case TYPE_INT:
n = r_long(p);
- retval = PyErr_Occurred() ? NULL : PyLong_FromLong(n);
+ if (n == -1 && PyErr_Occurred()) {
+ break;
+ }
+ retval = PyLong_FromLong(n);
R_REF(retval);
break;
@@ -1106,10 +1106,11 @@ r_object(RFILE *p)
{
const char *ptr;
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (bytes object size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (bytes object size out of range)");
+ }
break;
}
v = PyBytes_FromStringAndSize((char *)NULL, n);
@@ -1131,10 +1132,11 @@ r_object(RFILE *p)
/* fall through */
case TYPE_ASCII:
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (string size out of range)");
+ }
break;
}
goto _read_ascii;
@@ -1145,8 +1147,6 @@ r_object(RFILE *p)
case TYPE_SHORT_ASCII:
n = r_byte(p);
if (n == EOF) {
- PyErr_SetString(PyExc_EOFError,
- "EOF read where object expected");
break;
}
_read_ascii:
@@ -1173,10 +1173,11 @@ r_object(RFILE *p)
const char *buffer;
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (string size out of range)");
+ }
break;
}
if (n != 0) {
@@ -1198,16 +1199,18 @@ r_object(RFILE *p)
}
case TYPE_SMALL_TUPLE:
- n = (unsigned char) r_byte(p);
- if (PyErr_Occurred())
+ n = r_byte(p);
+ if (n == EOF) {
break;
+ }
goto _read_tuple;
case TYPE_TUPLE:
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (tuple size out of range)");
+ }
break;
}
_read_tuple:
@@ -1232,10 +1235,11 @@ r_object(RFILE *p)
case TYPE_LIST:
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (list size out of range)");
+ }
break;
}
v = PyList_New(n);
@@ -1288,10 +1292,11 @@ r_object(RFILE *p)
case TYPE_SET:
case TYPE_FROZENSET:
n = r_long(p);
- if (PyErr_Occurred())
- break;
if (n < 0 || n > SIZE32_MAX) {
- PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (set size out of range)");
+ }
break;
}
@@ -1368,20 +1373,20 @@ r_object(RFILE *p)
/* XXX ignore long->int overflows for now */
argcount = (int)r_long(p);
- if (PyErr_Occurred())
+ if (argcount == -1 && PyErr_Occurred())
goto code_error;
posonlyargcount = (int)r_long(p);
- if (PyErr_Occurred()) {
+ if (posonlyargcount == -1 && PyErr_Occurred()) {
goto code_error;
}
kwonlyargcount = (int)r_long(p);
- if (PyErr_Occurred())
+ if (kwonlyargcount == -1 && PyErr_Occurred())
goto code_error;
stacksize = (int)r_long(p);
- if (PyErr_Occurred())
+ if (stacksize == -1 && PyErr_Occurred())
goto code_error;
flags = (int)r_long(p);
- if (PyErr_Occurred())
+ if (flags == -1 && PyErr_Occurred())
goto code_error;
code = r_object(p);
if (code == NULL)
@@ -1454,6 +1459,10 @@ r_object(RFILE *p)
v = r_ref_insert(v, idx, flag, p);
code_error:
+ if (v == NULL && !PyErr_Occurred()) {
+ PyErr_SetString(PyExc_TypeError,
+ "NULL object in marshal data for code object");
+ }
Py_XDECREF(code);
Py_XDECREF(consts);
Py_XDECREF(names);
@@ -1471,9 +1480,10 @@ r_object(RFILE *p)
case TYPE_REF:
n = r_long(p);
if (n < 0 || n >= PyList_GET_SIZE(p->refs)) {
- if (n == -1 && PyErr_Occurred())
- break;
- PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)");
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "bad marshal data (invalid reference)");
+ }
break;
}
v = PyList_GET_ITEM(p->refs, n);
1
0
[3.12] gh-106194: Rename duplicated tests in `test_curses` (GH-106196) (#106216)
by hugovk 28 Jun '23
by hugovk 28 Jun '23
28 Jun '23
https://github.com/python/cpython/commit/e12045d64853ddfd5f4dbf193270b644de…
commit: e12045d64853ddfd5f4dbf193270b644de3b8d9a
branch: 3.12
author: Miss Islington (bot) <31488909+miss-islington(a)users.noreply.github.com>
committer: hugovk <hugovk(a)users.noreply.github.com>
date: 2023-06-28T21:12:12Z
summary:
[3.12] gh-106194: Rename duplicated tests in `test_curses` (GH-106196) (#106216)
Co-authored-by: Nikita Sobolev <mail(a)sobolevn.me>
files:
M Lib/test/test_curses.py
diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py
index 3ab837e4f9568..31bc108e7712e 100644
--- a/Lib/test/test_curses.py
+++ b/Lib/test/test_curses.py
@@ -1364,26 +1364,33 @@ def test_move_left(self):
self.mock_win.reset_mock()
self.textbox.do_command(curses.KEY_LEFT)
self.mock_win.move.assert_called_with(1, 0)
+ self.mock_win.reset_mock()
+
+ def test_move_right(self):
+ """Test moving the cursor right."""
+ self.mock_win.reset_mock()
self.textbox.do_command(curses.KEY_RIGHT)
self.mock_win.move.assert_called_with(1, 2)
self.mock_win.reset_mock()
- def test_move_left(self):
- """Test moving the cursor left."""
+ def test_move_left_and_right(self):
+ """Test moving the cursor left and then right."""
self.mock_win.reset_mock()
+ self.textbox.do_command(curses.KEY_LEFT)
+ self.mock_win.move.assert_called_with(1, 0)
self.textbox.do_command(curses.KEY_RIGHT)
self.mock_win.move.assert_called_with(1, 2)
self.mock_win.reset_mock()
def test_move_up(self):
- """Test moving the cursor left."""
+ """Test moving the cursor up."""
self.mock_win.reset_mock()
self.textbox.do_command(curses.KEY_UP)
self.mock_win.move.assert_called_with(0, 1)
self.mock_win.reset_mock()
def test_move_down(self):
- """Test moving the cursor left."""
+ """Test moving the cursor down."""
self.mock_win.reset_mock()
self.textbox.do_command(curses.KEY_DOWN)
self.mock_win.move.assert_called_with(2, 1)
1
0
https://github.com/python/cpython/commit/3fb7c608e5764559a718ce8cb81350d7a3…
commit: 3fb7c608e5764559a718ce8cb81350d7a3df0356
branch: main
author: Nikita Sobolev <mail(a)sobolevn.me>
committer: hugovk <hugovk(a)users.noreply.github.com>
date: 2023-06-28T14:41:08-06:00
summary:
gh-106194: Rename duplicated tests in `test_curses` (#106196)
files:
M Lib/test/test_curses.py
diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py
index 3ab837e4f9568..31bc108e7712e 100644
--- a/Lib/test/test_curses.py
+++ b/Lib/test/test_curses.py
@@ -1364,26 +1364,33 @@ def test_move_left(self):
self.mock_win.reset_mock()
self.textbox.do_command(curses.KEY_LEFT)
self.mock_win.move.assert_called_with(1, 0)
+ self.mock_win.reset_mock()
+
+ def test_move_right(self):
+ """Test moving the cursor right."""
+ self.mock_win.reset_mock()
self.textbox.do_command(curses.KEY_RIGHT)
self.mock_win.move.assert_called_with(1, 2)
self.mock_win.reset_mock()
- def test_move_left(self):
- """Test moving the cursor left."""
+ def test_move_left_and_right(self):
+ """Test moving the cursor left and then right."""
self.mock_win.reset_mock()
+ self.textbox.do_command(curses.KEY_LEFT)
+ self.mock_win.move.assert_called_with(1, 0)
self.textbox.do_command(curses.KEY_RIGHT)
self.mock_win.move.assert_called_with(1, 2)
self.mock_win.reset_mock()
def test_move_up(self):
- """Test moving the cursor left."""
+ """Test moving the cursor up."""
self.mock_win.reset_mock()
self.textbox.do_command(curses.KEY_UP)
self.mock_win.move.assert_called_with(0, 1)
self.mock_win.reset_mock()
def test_move_down(self):
- """Test moving the cursor left."""
+ """Test moving the cursor down."""
self.mock_win.reset_mock()
self.textbox.do_command(curses.KEY_DOWN)
self.mock_win.move.assert_called_with(2, 1)
1
0
GH-106214: Fix `test_opcache` to skip threaded tests on non-threaded platforms (GH-106166)
by brettcannon 28 Jun '23
by brettcannon 28 Jun '23
28 Jun '23
https://github.com/python/cpython/commit/4bde89462a95e5962e1467cfc1af5a6094…
commit: 4bde89462a95e5962e1467cfc1af5a6094c0c858
branch: main
author: Hood Chatham <roberthoodchatham(a)gmail.com>
committer: brettcannon <brett(a)python.org>
date: 2023-06-28T13:04:59-07:00
summary:
GH-106214: Fix `test_opcache` to skip threaded tests on non-threaded platforms (GH-106166)
This skips the test added in GH-105953 on threadless builds.
files:
M Lib/test/test_opcache.py
diff --git a/Lib/test/test_opcache.py b/Lib/test/test_opcache.py
index 2f6f91ded248b..564dc4745ae64 100644
--- a/Lib/test/test_opcache.py
+++ b/Lib/test/test_opcache.py
@@ -2,6 +2,7 @@
import threading
import types
import unittest
+from test.support import threading_helper
class TestLoadSuperAttrCache(unittest.TestCase):
@@ -484,6 +485,7 @@ def f(x, y):
f()
+(a)threading_helper.requires_working_threading()
class TestRacesDoNotCrash(unittest.TestCase):
# Careful with these. Bigger numbers have a higher chance of catching bugs,
# but you can also burn through a *ton* of type/dict/function versions:
1
0