summaryrefslogtreecommitdiff
path: root/Modules
diff options
context:
space:
mode:
authorStefan Krah <skrah@bytereef.org>2016-07-17 14:12:59 +0200
committerStefan Krah <skrah@bytereef.org>2016-07-17 14:12:59 +0200
commit5482ab1b94bd1679fb491c1195e6f0757afab4d4 (patch)
tree93dd61331f9f49648394ea1d6b02856604ae6e82 /Modules
parent47bf84d959f87af22da5c325a67a6b0b89dd9a77 (diff)
parent9eb921ecf5483ed903332ec829264bcf38aeb5f5 (diff)
downloadcpython-5482ab1b94bd1679fb491c1195e6f0757afab4d4.tar.gz
Merge 3.5.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_bisectmodule.c6
-rw-r--r--Modules/_bz2module.c3
-rw-r--r--Modules/_codecsmodule.c261
-rw-r--r--Modules/_collectionsmodule.c671
-rw-r--r--Modules/_csv.c8
-rw-r--r--Modules/_ctypes/_ctypes.c34
-rw-r--r--Modules/_ctypes/callbacks.c2
-rw-r--r--Modules/_ctypes/callproc.c22
-rw-r--r--Modules/_ctypes/ctypes.h2
-rw-r--r--Modules/_curses_panel.c4
-rw-r--r--Modules/_datetimemodule.c255
-rw-r--r--Modules/_dbmmodule.c8
-rw-r--r--Modules/_decimal/_decimal.c101
-rw-r--r--Modules/_decimal/docstrings.h9
-rw-r--r--Modules/_decimal/libmpdec/basearith.c1
-rw-r--r--Modules/_decimal/libmpdec/io.c1
-rw-r--r--Modules/_decimal/libmpdec/memory.c7
-rw-r--r--Modules/_decimal/libmpdec/mpalloc.h (renamed from Modules/_decimal/libmpdec/memory.h)4
-rw-r--r--Modules/_decimal/libmpdec/mpdecimal.c2
-rw-r--r--Modules/_decimal/libmpdec/mpdecimal.h4
-rw-r--r--Modules/_decimal/tests/deccheck.py6
-rw-r--r--Modules/_elementtree.c575
-rw-r--r--Modules/_gdbmmodule.c2
-rw-r--r--Modules/_heapqmodule.c10
-rw-r--r--Modules/_io/_iomodule.c61
-rw-r--r--Modules/_io/_iomodule.h2
-rw-r--r--Modules/_io/bufferedio.c2
-rw-r--r--Modules/_io/bytesio.c6
-rw-r--r--Modules/_io/clinic/_iomodule.c.h5
-rw-r--r--Modules/_io/clinic/bufferedio.c.h65
-rw-r--r--Modules/_io/clinic/bytesio.c.h26
-rw-r--r--Modules/_io/clinic/fileio.c.h26
-rw-r--r--Modules/_io/clinic/iobase.c.h11
-rw-r--r--Modules/_io/clinic/stringio.c.h17
-rw-r--r--Modules/_io/clinic/textio.c.h26
-rw-r--r--Modules/_io/fileio.c7
-rw-r--r--Modules/_io/iobase.c2
-rw-r--r--Modules/_io/stringio.c2
-rw-r--r--Modules/_io/textio.c26
-rw-r--r--Modules/_json.c4
-rw-r--r--Modules/_localemodule.c2
-rw-r--r--Modules/_lsprof.c7
-rw-r--r--Modules/_lzmamodule.c14
-rw-r--r--Modules/_multiprocessing/multiprocessing.c2
-rw-r--r--Modules/_pickle.c298
-rw-r--r--Modules/_posixsubprocess.c5
-rw-r--r--Modules/_randommodule.c4
-rw-r--r--Modules/_scproxy.c2
-rw-r--r--Modules/_sqlite/cache.c3
-rw-r--r--Modules/_sqlite/connection.c26
-rw-r--r--Modules/_sqlite/cursor.c20
-rw-r--r--Modules/_sqlite/module.c13
-rw-r--r--Modules/_sre.c12
-rw-r--r--Modules/_ssl.c4
-rw-r--r--Modules/_struct.c10
-rw-r--r--Modules/_testbuffer.c2
-rw-r--r--Modules/_testcapimodule.c169
-rw-r--r--Modules/_testmultiphase.c2
-rw-r--r--Modules/_tkinter.c73
-rw-r--r--Modules/_tracemalloc.c584
-rw-r--r--Modules/_winapi.c8
-rw-r--r--Modules/arraymodule.c40
-rw-r--r--Modules/audioop.c20
-rw-r--r--Modules/binascii.c287
-rw-r--r--Modules/cjkcodecs/clinic/multibytecodec.c.h29
-rw-r--r--Modules/clinic/_bz2module.c.h26
-rw-r--r--Modules/clinic/_codecsmodule.c.h221
-rw-r--r--Modules/clinic/_cryptmodule.c.h5
-rw-r--r--Modules/clinic/_cursesmodule.c.h14
-rw-r--r--Modules/clinic/_datetimemodule.c.h5
-rw-r--r--Modules/clinic/_dbmmodule.c.h11
-rw-r--r--Modules/clinic/_elementtree.c.h56
-rw-r--r--Modules/clinic/_gdbmmodule.c.h14
-rw-r--r--Modules/clinic/_lzmamodule.c.h29
-rw-r--r--Modules/clinic/_opcode.c.h8
-rw-r--r--Modules/clinic/_pickle.c.h29
-rw-r--r--Modules/clinic/_sre.c.h71
-rw-r--r--Modules/clinic/_ssl.c.h101
-rw-r--r--Modules/clinic/_tkinter.c.h53
-rw-r--r--Modules/clinic/_weakref.c.h5
-rw-r--r--Modules/clinic/_winapi.c.h116
-rw-r--r--Modules/clinic/arraymodule.c.h29
-rw-r--r--Modules/clinic/audioop.c.h167
-rw-r--r--Modules/clinic/binascii.c.h99
-rw-r--r--Modules/clinic/cmathmodule.c.h74
-rw-r--r--Modules/clinic/fcntlmodule.c.h14
-rw-r--r--Modules/clinic/grpmodule.c.h8
-rw-r--r--Modules/clinic/md5module.c.h5
-rw-r--r--Modules/clinic/posixmodule.c.h520
-rw-r--r--Modules/clinic/pwdmodule.c.h5
-rw-r--r--Modules/clinic/pyexpat.c.h23
-rw-r--r--Modules/clinic/sha1module.c.h5
-rw-r--r--Modules/clinic/sha256module.c.h8
-rw-r--r--Modules/clinic/sha512module.c.h8
-rw-r--r--Modules/clinic/signalmodule.c.h32
-rw-r--r--Modules/clinic/spwdmodule.c.h5
-rw-r--r--Modules/clinic/unicodedata.c.h44
-rw-r--r--Modules/clinic/zlibmodule.c.h76
-rw-r--r--Modules/faulthandler.c298
-rw-r--r--Modules/gcmodule.c2
-rw-r--r--Modules/getaddrinfo.c4
-rw-r--r--Modules/getpath.c4
-rw-r--r--Modules/grpmodule.c21
-rw-r--r--Modules/hashtable.c196
-rw-r--r--Modules/hashtable.h181
-rw-r--r--Modules/itertoolsmodule.c391
-rw-r--r--Modules/main.c47
-rw-r--r--Modules/mathmodule.c4
-rw-r--r--Modules/mmapmodule.c5
-rw-r--r--Modules/nismodule.c6
-rw-r--r--Modules/ossaudiodev.c4
-rw-r--r--Modules/parsermodule.c2555
-rw-r--r--Modules/posixmodule.c493
-rw-r--r--Modules/pwdmodule.c8
-rw-r--r--Modules/pyexpat.c26
-rw-r--r--Modules/readline.c27
-rw-r--r--Modules/selectmodule.c4
-rw-r--r--Modules/socketmodule.c185
-rw-r--r--Modules/spwdmodule.c5
-rw-r--r--Modules/sre_lib.h59
-rw-r--r--Modules/timemodule.c7
-rw-r--r--Modules/unicodedata.c4
-rw-r--r--Modules/xxlimited.c2
-rw-r--r--Modules/xxmodule.c2
-rw-r--r--Modules/zlibmodule.c20
125 files changed, 4928 insertions, 5440 deletions
diff --git a/Modules/_bisectmodule.c b/Modules/_bisectmodule.c
index 02b55d1b41..22ddbf2a56 100644
--- a/Modules/_bisectmodule.c
+++ b/Modules/_bisectmodule.c
@@ -12,7 +12,8 @@ static Py_ssize_t
internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi)
{
PyObject *litem;
- Py_ssize_t mid, res;
+ Py_ssize_t mid;
+ int res;
if (lo < 0) {
PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
@@ -115,7 +116,8 @@ static Py_ssize_t
internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi)
{
PyObject *litem;
- Py_ssize_t mid, res;
+ Py_ssize_t mid;
+ int res;
if (lo < 0) {
PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
diff --git a/Modules/_bz2module.c b/Modules/_bz2module.c
index e3e0eb1f23..f4077faa06 100644
--- a/Modules/_bz2module.c
+++ b/Modules/_bz2module.c
@@ -592,7 +592,6 @@ error:
/*[clinic input]
_bz2.BZ2Decompressor.decompress
- self: self(type="BZ2Decompressor *")
data: Py_buffer
max_length: Py_ssize_t=-1
@@ -615,7 +614,7 @@ the unused_data attribute.
static PyObject *
_bz2_BZ2Decompressor_decompress_impl(BZ2Decompressor *self, Py_buffer *data,
Py_ssize_t max_length)
-/*[clinic end generated code: output=23e41045deb240a3 input=9558b424c8b00516]*/
+/*[clinic end generated code: output=23e41045deb240a3 input=52e1ffc66a8ea624]*/
{
PyObject *result = NULL;
diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c
index 9b1194eae8..4e75995bbf 100644
--- a/Modules/_codecsmodule.c
+++ b/Modules/_codecsmodule.c
@@ -20,10 +20,6 @@
<encoding>_decode(char_buffer_obj[,errors='strict']) ->
(Unicode object, bytes consumed)
- <encoding>_encode() interfaces also accept non-Unicode object as
- input. The objects are then converted to Unicode using
- PyUnicode_FromObject() prior to applying the conversion.
-
These <encoding>s are available: utf_8, unicode_escape,
raw_unicode_escape, unicode_internal, latin_1, ascii (7-bit),
mbcs (on win32).
@@ -718,7 +714,7 @@ _codecs_unicode_internal_encode_impl(PyObject *module, PyObject *obj,
/*[clinic input]
_codecs.utf_7_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -726,24 +722,15 @@ _codecs.utf_7_encode
static PyObject *
_codecs_utf_7_encode_impl(PyObject *module, PyObject *str,
const char *errors)
-/*[clinic end generated code: output=0feda21ffc921bc8 input=fd91a78f103b0421]*/
+/*[clinic end generated code: output=0feda21ffc921bc8 input=d1a47579e79cbe15]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_EncodeUTF7(str, 0, 0, errors),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_EncodeUTF7(str, 0, 0, errors),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.utf_8_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -751,19 +738,10 @@ _codecs.utf_8_encode
static PyObject *
_codecs_utf_8_encode_impl(PyObject *module, PyObject *str,
const char *errors)
-/*[clinic end generated code: output=02bf47332b9c796c input=2c22d40532f071f3]*/
+/*[clinic end generated code: output=02bf47332b9c796c input=42e3ba73c4392eef]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(PyUnicode_AsEncodedString(str, "utf-8", errors),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_AsUTF8String(str, errors),
+ PyUnicode_GET_LENGTH(str));
}
/* This version provides access to the byteorder parameter of the
@@ -775,7 +753,7 @@ _codecs_utf_8_encode_impl(PyObject *module, PyObject *str,
/*[clinic input]
_codecs.utf_16_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
byteorder: int = 0
/
@@ -784,24 +762,15 @@ _codecs.utf_16_encode
static PyObject *
_codecs_utf_16_encode_impl(PyObject *module, PyObject *str,
const char *errors, int byteorder)
-/*[clinic end generated code: output=c654e13efa2e64e4 input=3935a489b2d5385e]*/
+/*[clinic end generated code: output=c654e13efa2e64e4 input=ff46416b04edb944]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, byteorder),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_EncodeUTF16(str, errors, byteorder),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.utf_16_le_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -809,24 +778,15 @@ _codecs.utf_16_le_encode
static PyObject *
_codecs_utf_16_le_encode_impl(PyObject *module, PyObject *str,
const char *errors)
-/*[clinic end generated code: output=431b01e55f2d4995 input=bc27df05d1d20dfe]*/
+/*[clinic end generated code: output=431b01e55f2d4995 input=cb385455ea8f2fe0]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, -1),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_EncodeUTF16(str, errors, -1),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.utf_16_be_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -834,19 +794,10 @@ _codecs.utf_16_be_encode
static PyObject *
_codecs_utf_16_be_encode_impl(PyObject *module, PyObject *str,
const char *errors)
-/*[clinic end generated code: output=96886a6fd54dcae3 input=5a69d4112763462b]*/
+/*[clinic end generated code: output=96886a6fd54dcae3 input=9119997066bdaefd]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, +1),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_EncodeUTF16(str, errors, +1),
+ PyUnicode_GET_LENGTH(str));
}
/* This version provides access to the byteorder parameter of the
@@ -858,7 +809,7 @@ _codecs_utf_16_be_encode_impl(PyObject *module, PyObject *str,
/*[clinic input]
_codecs.utf_32_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
byteorder: int = 0
/
@@ -867,24 +818,15 @@ _codecs.utf_32_encode
static PyObject *
_codecs_utf_32_encode_impl(PyObject *module, PyObject *str,
const char *errors, int byteorder)
-/*[clinic end generated code: output=5c760da0c09a8b83 input=434a1efa492b8d58]*/
+/*[clinic end generated code: output=5c760da0c09a8b83 input=c5e77da82fbe5c2a]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, byteorder),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_EncodeUTF32(str, errors, byteorder),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.utf_32_le_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -892,24 +834,15 @@ _codecs.utf_32_le_encode
static PyObject *
_codecs_utf_32_le_encode_impl(PyObject *module, PyObject *str,
const char *errors)
-/*[clinic end generated code: output=b65cd176de8e36d6 input=dfa2d7dc78b99422]*/
+/*[clinic end generated code: output=b65cd176de8e36d6 input=9993b25fe0877848]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, -1),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_EncodeUTF32(str, errors, -1),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.utf_32_be_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -917,24 +850,15 @@ _codecs.utf_32_be_encode
static PyObject *
_codecs_utf_32_be_encode_impl(PyObject *module, PyObject *str,
const char *errors)
-/*[clinic end generated code: output=1d9e71a9358709e9 input=4595617b18169002]*/
+/*[clinic end generated code: output=1d9e71a9358709e9 input=d3e0ccaa02920431]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, +1),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_EncodeUTF32(str, errors, +1),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.unicode_escape_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -942,24 +866,15 @@ _codecs.unicode_escape_encode
static PyObject *
_codecs_unicode_escape_encode_impl(PyObject *module, PyObject *str,
const char *errors)
-/*[clinic end generated code: output=66271b30bc4f7a3c input=8273506f14076912]*/
+/*[clinic end generated code: output=66271b30bc4f7a3c input=65d9eefca65b455a]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(PyUnicode_AsUnicodeEscapeString(str),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(PyUnicode_AsUnicodeEscapeString(str),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.raw_unicode_escape_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -967,24 +882,15 @@ _codecs.raw_unicode_escape_encode
static PyObject *
_codecs_raw_unicode_escape_encode_impl(PyObject *module, PyObject *str,
const char *errors)
-/*[clinic end generated code: output=a66a806ed01c830a input=181755d5dfacef3c]*/
+/*[clinic end generated code: output=a66a806ed01c830a input=5aa33e4a133391ab]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(PyUnicode_AsRawUnicodeEscapeString(str),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(PyUnicode_AsRawUnicodeEscapeString(str),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.latin_1_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -992,24 +898,15 @@ _codecs.latin_1_encode
static PyObject *
_codecs_latin_1_encode_impl(PyObject *module, PyObject *str,
const char *errors)
-/*[clinic end generated code: output=2c28c83a27884e08 input=f03f6dcf1d84bee4]*/
+/*[clinic end generated code: output=2c28c83a27884e08 input=30b11c9e49a65150]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_AsLatin1String(str, errors),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_AsLatin1String(str, errors),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.ascii_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -1017,24 +914,15 @@ _codecs.ascii_encode
static PyObject *
_codecs_ascii_encode_impl(PyObject *module, PyObject *str,
const char *errors)
-/*[clinic end generated code: output=b5e035182d33befc input=d87e25a10a593fee]*/
+/*[clinic end generated code: output=b5e035182d33befc input=843a1d268e6dfa8e]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_AsASCIIString(str, errors),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_AsASCIIString(str, errors),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.charmap_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
mapping: object = NULL
/
@@ -1043,22 +931,13 @@ _codecs.charmap_encode
static PyObject *
_codecs_charmap_encode_impl(PyObject *module, PyObject *str,
const char *errors, PyObject *mapping)
-/*[clinic end generated code: output=047476f48495a9e9 input=85f4172661e8dad9]*/
+/*[clinic end generated code: output=047476f48495a9e9 input=0752cde07a6d6d00]*/
{
- PyObject *v;
-
if (mapping == Py_None)
mapping = NULL;
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_EncodeCharmap(str, mapping, errors),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_EncodeCharmap(str, mapping, errors),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
@@ -1078,32 +957,23 @@ _codecs_charmap_build_impl(PyObject *module, PyObject *map)
/*[clinic input]
_codecs.mbcs_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
static PyObject *
_codecs_mbcs_encode_impl(PyObject *module, PyObject *str, const char *errors)
-/*[clinic end generated code: output=76e2e170c966c080 input=65c09ee1e4203263]*/
+/*[clinic end generated code: output=76e2e170c966c080 input=de471e0815947553]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(PyUnicode_EncodeCodePage(CP_ACP, str, errors),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(PyUnicode_EncodeCodePage(CP_ACP, str, errors),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.code_page_encode
code_page: int
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -1111,21 +981,10 @@ _codecs.code_page_encode
static PyObject *
_codecs_code_page_encode_impl(PyObject *module, int code_page, PyObject *str,
const char *errors)
-/*[clinic end generated code: output=45673f6085657a9e input=c8562ec460c2e309]*/
+/*[clinic end generated code: output=45673f6085657a9e input=786421ae617d680b]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(PyUnicode_EncodeCodePage(code_page,
- str,
- errors),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(PyUnicode_EncodeCodePage(code_page, str, errors),
+ PyUnicode_GET_LENGTH(str));
}
#endif /* HAVE_MBCS */
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index 10fbcfe6b9..3410dfec07 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -81,7 +81,7 @@ typedef struct {
Py_ssize_t leftindex; /* 0 <= leftindex < BLOCKLEN */
Py_ssize_t rightindex; /* 0 <= rightindex < BLOCKLEN */
size_t state; /* incremented whenever the indices move */
- Py_ssize_t maxlen;
+ Py_ssize_t maxlen; /* maxlen is -1 for unbounded deques */
PyObject *weakreflist;
} dequeobject;
@@ -108,29 +108,18 @@ static PyTypeObject deque_type;
#define CHECK_NOT_END(link)
#endif
-/* To prevent len from overflowing PY_SSIZE_T_MAX, we refuse to
- allocate new blocks if the current len is nearing overflow.
-*/
-
-#define MAX_DEQUE_LEN (PY_SSIZE_T_MAX - 3*BLOCKLEN)
-
/* A simple freelisting scheme is used to minimize calls to the memory
allocator. It accommodates common use cases where new blocks are being
added at about the same rate as old blocks are being freed.
*/
-#define MAXFREEBLOCKS 10
+#define MAXFREEBLOCKS 16
static Py_ssize_t numfreeblocks = 0;
static block *freeblocks[MAXFREEBLOCKS];
static block *
-newblock(Py_ssize_t len) {
+newblock(void) {
block *b;
- if (len >= MAX_DEQUE_LEN) {
- PyErr_SetString(PyExc_OverflowError,
- "cannot add more blocks to the deque");
- return NULL;
- }
if (numfreeblocks) {
numfreeblocks--;
return freeblocks[numfreeblocks];
@@ -154,12 +143,6 @@ freeblock(block *b)
}
}
-/* XXX Todo:
- If aligned memory allocations become available, make the
- deque object 64 byte aligned so that all of the fields
- can be retrieved or updated in a single cache line.
-*/
-
static PyObject *
deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
@@ -171,7 +154,7 @@ deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (deque == NULL)
return NULL;
- b = newblock(0);
+ b = newblock();
if (b == NULL) {
Py_DECREF(deque);
return NULL;
@@ -207,7 +190,7 @@ deque_pop(dequeobject *deque, PyObject *unused)
Py_SIZE(deque)--;
deque->state++;
- if (deque->rightindex == -1) {
+ if (deque->rightindex < 0) {
if (Py_SIZE(deque)) {
prevblock = deque->rightblock->leftlink;
assert(deque->leftblock != deque->rightblock);
@@ -270,42 +253,24 @@ PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element.");
/* The deque's size limit is d.maxlen. The limit can be zero or positive.
* If there is no limit, then d.maxlen == -1.
*
- * After an item is added to a deque, we check to see if the size has grown past
- * the limit. If it has, we get the size back down to the limit by popping an
- * item off of the opposite end. The methods that can trigger this are append(),
- * appendleft(), extend(), and extendleft().
+ * After an item is added to a deque, we check to see if the size has
+ * grown past the limit. If it has, we get the size back down to the limit
+ * by popping an item off of the opposite end. The methods that can
+ * trigger this are append(), appendleft(), extend(), and extendleft().
+ *
+ * The macro to check whether a deque needs to be trimmed uses a single
+ * unsigned test that returns true whenever 0 <= maxlen < Py_SIZE(deque).
*/
-static void
-deque_trim_right(dequeobject *deque)
-{
- if (deque->maxlen != -1 && Py_SIZE(deque) > deque->maxlen) {
- PyObject *rv = deque_pop(deque, NULL);
- assert(rv != NULL);
- assert(Py_SIZE(deque) <= deque->maxlen);
- Py_DECREF(rv);
- }
-}
+#define NEEDS_TRIM(deque, maxlen) ((size_t)(maxlen) < (size_t)(Py_SIZE(deque)))
-static void
-deque_trim_left(dequeobject *deque)
-{
- if (deque->maxlen != -1 && Py_SIZE(deque) > deque->maxlen) {
- PyObject *rv = deque_popleft(deque, NULL);
- assert(rv != NULL);
- assert(Py_SIZE(deque) <= deque->maxlen);
- Py_DECREF(rv);
- }
-}
-
-static PyObject *
-deque_append(dequeobject *deque, PyObject *item)
+static int
+deque_append_internal(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
{
- deque->state++;
if (deque->rightindex == BLOCKLEN - 1) {
- block *b = newblock(Py_SIZE(deque));
+ block *b = newblock();
if (b == NULL)
- return NULL;
+ return -1;
b->leftlink = deque->rightblock;
CHECK_END(deque->rightblock->rightlink);
deque->rightblock->rightlink = b;
@@ -313,24 +278,36 @@ deque_append(dequeobject *deque, PyObject *item)
MARK_END(b->rightlink);
deque->rightindex = -1;
}
- Py_INCREF(item);
Py_SIZE(deque)++;
deque->rightindex++;
deque->rightblock->data[deque->rightindex] = item;
- deque_trim_left(deque);
+ if (NEEDS_TRIM(deque, maxlen)) {
+ PyObject *olditem = deque_popleft(deque, NULL);
+ Py_DECREF(olditem);
+ } else {
+ deque->state++;
+ }
+ return 0;
+}
+
+static PyObject *
+deque_append(dequeobject *deque, PyObject *item)
+{
+ Py_INCREF(item);
+ if (deque_append_internal(deque, item, deque->maxlen) < 0)
+ return NULL;
Py_RETURN_NONE;
}
PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque.");
-static PyObject *
-deque_appendleft(dequeobject *deque, PyObject *item)
+static int
+deque_appendleft_internal(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
{
- deque->state++;
if (deque->leftindex == 0) {
- block *b = newblock(Py_SIZE(deque));
+ block *b = newblock();
if (b == NULL)
- return NULL;
+ return -1;
b->rightlink = deque->leftblock;
CHECK_END(deque->leftblock->leftlink);
deque->leftblock->leftlink = b;
@@ -338,37 +315,65 @@ deque_appendleft(dequeobject *deque, PyObject *item)
MARK_END(b->leftlink);
deque->leftindex = BLOCKLEN;
}
- Py_INCREF(item);
Py_SIZE(deque)++;
deque->leftindex--;
deque->leftblock->data[deque->leftindex] = item;
- deque_trim_right(deque);
+ if (NEEDS_TRIM(deque, deque->maxlen)) {
+ PyObject *olditem = deque_pop(deque, NULL);
+ Py_DECREF(olditem);
+ } else {
+ deque->state++;
+ }
+ return 0;
+}
+
+static PyObject *
+deque_appendleft(dequeobject *deque, PyObject *item)
+{
+ Py_INCREF(item);
+ if (deque_appendleft_internal(deque, item, deque->maxlen) < 0)
+ return NULL;
Py_RETURN_NONE;
}
PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.");
+static PyObject*
+finalize_iterator(PyObject *it)
+{
+ if (PyErr_Occurred()) {
+ if (PyErr_ExceptionMatches(PyExc_StopIteration))
+ PyErr_Clear();
+ else {
+ Py_DECREF(it);
+ return NULL;
+ }
+ }
+ Py_DECREF(it);
+ Py_RETURN_NONE;
+}
/* Run an iterator to exhaustion. Shortcut for
the extend/extendleft methods when maxlen == 0. */
static PyObject*
consume_iterator(PyObject *it)
{
+ PyObject *(*iternext)(PyObject *);
PyObject *item;
- while ((item = PyIter_Next(it)) != NULL) {
+ iternext = *Py_TYPE(it)->tp_iternext;
+ while ((item = iternext(it)) != NULL) {
Py_DECREF(item);
}
- Py_DECREF(it);
- if (PyErr_Occurred())
- return NULL;
- Py_RETURN_NONE;
+ return finalize_iterator(it);
}
static PyObject *
deque_extend(dequeobject *deque, PyObject *iterable)
{
PyObject *it, *item;
+ PyObject *(*iternext)(PyObject *);
+ Py_ssize_t maxlen = deque->maxlen;
/* Handle case where id(deque) == id(iterable) */
if ((PyObject *)deque == iterable) {
@@ -381,6 +386,13 @@ deque_extend(dequeobject *deque, PyObject *iterable)
return result;
}
+ it = PyObject_GetIter(iterable);
+ if (it == NULL)
+ return NULL;
+
+ if (maxlen == 0)
+ return consume_iterator(it);
+
/* Space saving heuristic. Start filling from the left */
if (Py_SIZE(deque) == 0) {
assert(deque->leftblock == deque->rightblock);
@@ -389,40 +401,15 @@ deque_extend(dequeobject *deque, PyObject *iterable)
deque->rightindex = 0;
}
- it = PyObject_GetIter(iterable);
- if (it == NULL)
- return NULL;
-
- if (deque->maxlen == 0)
- return consume_iterator(it);
-
- while ((item = PyIter_Next(it)) != NULL) {
- deque->state++;
- if (deque->rightindex == BLOCKLEN - 1) {
- block *b = newblock(Py_SIZE(deque));
- if (b == NULL) {
- Py_DECREF(item);
- Py_DECREF(it);
- return NULL;
- }
- b->leftlink = deque->rightblock;
- CHECK_END(deque->rightblock->rightlink);
- deque->rightblock->rightlink = b;
- deque->rightblock = b;
- MARK_END(b->rightlink);
- deque->rightindex = -1;
+ iternext = *Py_TYPE(it)->tp_iternext;
+ while ((item = iternext(it)) != NULL) {
+ if (deque_append_internal(deque, item, maxlen) < 0) {
+ Py_DECREF(item);
+ Py_DECREF(it);
+ return NULL;
}
- Py_SIZE(deque)++;
- deque->rightindex++;
- deque->rightblock->data[deque->rightindex] = item;
- deque_trim_left(deque);
}
- if (PyErr_Occurred()) {
- Py_DECREF(it);
- return NULL;
- }
- Py_DECREF(it);
- Py_RETURN_NONE;
+ return finalize_iterator(it);
}
PyDoc_STRVAR(extend_doc,
@@ -432,6 +419,8 @@ static PyObject *
deque_extendleft(dequeobject *deque, PyObject *iterable)
{
PyObject *it, *item;
+ PyObject *(*iternext)(PyObject *);
+ Py_ssize_t maxlen = deque->maxlen;
/* Handle case where id(deque) == id(iterable) */
if ((PyObject *)deque == iterable) {
@@ -444,6 +433,13 @@ deque_extendleft(dequeobject *deque, PyObject *iterable)
return result;
}
+ it = PyObject_GetIter(iterable);
+ if (it == NULL)
+ return NULL;
+
+ if (maxlen == 0)
+ return consume_iterator(it);
+
/* Space saving heuristic. Start filling from the right */
if (Py_SIZE(deque) == 0) {
assert(deque->leftblock == deque->rightblock);
@@ -452,40 +448,15 @@ deque_extendleft(dequeobject *deque, PyObject *iterable)
deque->rightindex = BLOCKLEN - 2;
}
- it = PyObject_GetIter(iterable);
- if (it == NULL)
- return NULL;
-
- if (deque->maxlen == 0)
- return consume_iterator(it);
-
- while ((item = PyIter_Next(it)) != NULL) {
- deque->state++;
- if (deque->leftindex == 0) {
- block *b = newblock(Py_SIZE(deque));
- if (b == NULL) {
- Py_DECREF(item);
- Py_DECREF(it);
- return NULL;
- }
- b->rightlink = deque->leftblock;
- CHECK_END(deque->leftblock->leftlink);
- deque->leftblock->leftlink = b;
- deque->leftblock = b;
- MARK_END(b->leftlink);
- deque->leftindex = BLOCKLEN;
+ iternext = *Py_TYPE(it)->tp_iternext;
+ while ((item = iternext(it)) != NULL) {
+ if (deque_appendleft_internal(deque, item, maxlen) < 0) {
+ Py_DECREF(item);
+ Py_DECREF(it);
+ return NULL;
}
- Py_SIZE(deque)++;
- deque->leftindex--;
- deque->leftblock->data[deque->leftindex] = item;
- deque_trim_right(deque);
- }
- if (PyErr_Occurred()) {
- Py_DECREF(it);
- return NULL;
}
- Py_DECREF(it);
- Py_RETURN_NONE;
+ return finalize_iterator(it);
}
PyDoc_STRVAR(extendleft_doc,
@@ -504,7 +475,40 @@ deque_inplace_concat(dequeobject *deque, PyObject *other)
return (PyObject *)deque;
}
-static PyObject *deque_copy(PyObject *deque);
+static PyObject *
+deque_copy(PyObject *deque)
+{
+ dequeobject *old_deque = (dequeobject *)deque;
+ if (Py_TYPE(deque) == &deque_type) {
+ dequeobject *new_deque;
+ PyObject *rv;
+
+ new_deque = (dequeobject *)deque_new(&deque_type, (PyObject *)NULL, (PyObject *)NULL);
+ if (new_deque == NULL)
+ return NULL;
+ new_deque->maxlen = old_deque->maxlen;
+ /* Fast path for the deque_repeat() common case where len(deque) == 1 */
+ if (Py_SIZE(deque) == 1) {
+ PyObject *item = old_deque->leftblock->data[old_deque->leftindex];
+ rv = deque_append(new_deque, item);
+ } else {
+ rv = deque_extend(new_deque, deque);
+ }
+ if (rv != NULL) {
+ Py_DECREF(rv);
+ return (PyObject *)new_deque;
+ }
+ Py_DECREF(new_deque);
+ return NULL;
+ }
+ if (old_deque->maxlen < 0)
+ return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL);
+ else
+ return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
+ deque, old_deque->maxlen, NULL);
+}
+
+PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
static PyObject *
deque_concat(dequeobject *deque, PyObject *other)
@@ -534,38 +538,102 @@ deque_concat(dequeobject *deque, PyObject *other)
return new_deque;
}
-static void deque_clear(dequeobject *deque);
-
-static PyObject *
-deque_repeat(dequeobject *deque, Py_ssize_t n)
+static void
+deque_clear(dequeobject *deque)
{
- dequeobject *new_deque;
- PyObject *result;
+ block *b;
+ block *prevblock;
+ block *leftblock;
+ Py_ssize_t leftindex;
+ Py_ssize_t n, m;
+ PyObject *item;
+ PyObject **itemptr, **limit;
- /* XXX add a special case for when maxlen is defined */
- if (n < 0)
- n = 0;
- else if (n > 0 && Py_SIZE(deque) > MAX_DEQUE_LEN / n)
- return PyErr_NoMemory();
+ if (Py_SIZE(deque) == 0)
+ return;
- new_deque = (dequeobject *)deque_new(&deque_type, (PyObject *)NULL, (PyObject *)NULL);
- new_deque->maxlen = deque->maxlen;
+ /* During the process of clearing a deque, decrefs can cause the
+ deque to mutate. To avoid fatal confusion, we have to make the
+ deque empty before clearing the blocks and never refer to
+ anything via deque->ref while clearing. (This is the same
+ technique used for clearing lists, sets, and dicts.)
- for ( ; n ; n--) {
- result = deque_extend(new_deque, (PyObject *)deque);
- if (result == NULL) {
- Py_DECREF(new_deque);
- return NULL;
+ Making the deque empty requires allocating a new empty block. In
+ the unlikely event that memory is full, we fall back to an
+ alternate method that doesn't require a new block. Repeating
+ pops in a while-loop is slower, possibly re-entrant (and a clever
+ adversary could cause it to never terminate).
+ */
+
+ b = newblock();
+ if (b == NULL) {
+ PyErr_Clear();
+ goto alternate_method;
+ }
+
+ /* Remember the old size, leftblock, and leftindex */
+ n = Py_SIZE(deque);
+ leftblock = deque->leftblock;
+ leftindex = deque->leftindex;
+
+ /* Set the deque to be empty using the newly allocated block */
+ MARK_END(b->leftlink);
+ MARK_END(b->rightlink);
+ Py_SIZE(deque) = 0;
+ deque->leftblock = b;
+ deque->rightblock = b;
+ deque->leftindex = CENTER + 1;
+ deque->rightindex = CENTER;
+ deque->state++;
+
+ /* Now the old size, leftblock, and leftindex are disconnected from
+ the empty deque and we can use them to decref the pointers.
+ */
+ m = (BLOCKLEN - leftindex > n) ? n : BLOCKLEN - leftindex;
+ itemptr = &leftblock->data[leftindex];
+ limit = itemptr + m;
+ n -= m;
+ while (1) {
+ if (itemptr == limit) {
+ if (n == 0)
+ break;
+ CHECK_NOT_END(leftblock->rightlink);
+ prevblock = leftblock;
+ leftblock = leftblock->rightlink;
+ m = (n > BLOCKLEN) ? BLOCKLEN : n;
+ itemptr = leftblock->data;
+ limit = itemptr + m;
+ n -= m;
+ freeblock(prevblock);
}
- Py_DECREF(result);
+ item = *(itemptr++);
+ Py_DECREF(item);
}
- return (PyObject *)new_deque;
+ CHECK_END(leftblock->rightlink);
+ freeblock(leftblock);
+ return;
+
+ alternate_method:
+ while (Py_SIZE(deque)) {
+ item = deque_pop(deque, NULL);
+ assert (item != NULL);
+ Py_DECREF(item);
+ }
+}
+
+static PyObject *
+deque_clearmethod(dequeobject *deque)
+{
+ deque_clear(deque);
+ Py_RETURN_NONE;
}
+PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
+
static PyObject *
deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
{
- Py_ssize_t i, size;
+ Py_ssize_t i, m, size;
PyObject *seq;
PyObject *rv;
@@ -581,27 +649,47 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
return (PyObject *)deque;
}
- if (size > MAX_DEQUE_LEN / n) {
- return PyErr_NoMemory();
- }
-
if (size == 1) {
/* common case, repeating a single element */
PyObject *item = deque->leftblock->data[deque->leftindex];
- if (deque->maxlen != -1 && n > deque->maxlen)
+ if (deque->maxlen >= 0 && n > deque->maxlen)
n = deque->maxlen;
- for (i = 0 ; i < n-1 ; i++) {
- rv = deque_append(deque, item);
- if (rv == NULL)
- return NULL;
- Py_DECREF(rv);
+ deque->state++;
+ for (i = 0 ; i < n-1 ; ) {
+ if (deque->rightindex == BLOCKLEN - 1) {
+ block *b = newblock();
+ if (b == NULL) {
+ Py_SIZE(deque) += i;
+ return NULL;
+ }
+ b->leftlink = deque->rightblock;
+ CHECK_END(deque->rightblock->rightlink);
+ deque->rightblock->rightlink = b;
+ deque->rightblock = b;
+ MARK_END(b->rightlink);
+ deque->rightindex = -1;
+ }
+ m = n - 1 - i;
+ if (m > BLOCKLEN - 1 - deque->rightindex)
+ m = BLOCKLEN - 1 - deque->rightindex;
+ i += m;
+ while (m--) {
+ deque->rightindex++;
+ Py_INCREF(item);
+ deque->rightblock->data[deque->rightindex] = item;
+ }
}
+ Py_SIZE(deque) += i;
Py_INCREF(deque);
return (PyObject *)deque;
}
+ if ((size_t)size > PY_SSIZE_T_MAX / (size_t)n) {
+ return PyErr_NoMemory();
+ }
+
seq = PySequence_List((PyObject *)deque);
if (seq == NULL)
return seq;
@@ -619,6 +707,20 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
return (PyObject *)deque;
}
+static PyObject *
+deque_repeat(dequeobject *deque, Py_ssize_t n)
+{
+ dequeobject *new_deque;
+ PyObject *rv;
+
+ new_deque = (dequeobject *)deque_copy((PyObject *) deque);
+ if (new_deque == NULL)
+ return NULL;
+ rv = deque_inplace_repeat(new_deque, n);
+ Py_DECREF(new_deque);
+ return rv;
+}
+
/* The rotate() method is part of the public API and is used internally
as a primitive for other methods.
@@ -671,7 +773,7 @@ _deque_rotate(dequeobject *deque, Py_ssize_t n)
while (n > 0) {
if (leftindex == 0) {
if (b == NULL) {
- b = newblock(len);
+ b = newblock();
if (b == NULL)
goto done;
}
@@ -702,7 +804,7 @@ _deque_rotate(dequeobject *deque, Py_ssize_t n)
*(dest++) = *(src++);
} while (--m);
}
- if (rightindex == -1) {
+ if (rightindex < 0) {
assert(leftblock != rightblock);
assert(b == NULL);
b = rightblock;
@@ -715,7 +817,7 @@ _deque_rotate(dequeobject *deque, Py_ssize_t n)
while (n < 0) {
if (rightindex == BLOCKLEN - 1) {
if (b == NULL) {
- b = newblock(len);
+ b = newblock();
if (b == NULL)
goto done;
}
@@ -790,11 +892,11 @@ deque_reverse(dequeobject *deque, PyObject *unused)
block *rightblock = deque->rightblock;
Py_ssize_t leftindex = deque->leftindex;
Py_ssize_t rightindex = deque->rightindex;
- Py_ssize_t n = Py_SIZE(deque) / 2;
- Py_ssize_t i;
+ Py_ssize_t n = Py_SIZE(deque) >> 1;
PyObject *tmp;
- for (i=0 ; i<n ; i++) {
+ n++;
+ while (--n) {
/* Validate that pointers haven't met in the middle */
assert(leftblock != rightblock || leftindex < rightindex);
CHECK_NOT_END(leftblock);
@@ -814,7 +916,7 @@ deque_reverse(dequeobject *deque, PyObject *unused)
/* Step backwards with the right block/index pair */
rightindex--;
- if (rightindex == -1) {
+ if (rightindex < 0) {
rightblock = rightblock->leftlink;
rightindex = BLOCKLEN - 1;
}
@@ -831,20 +933,19 @@ deque_count(dequeobject *deque, PyObject *v)
block *b = deque->leftblock;
Py_ssize_t index = deque->leftindex;
Py_ssize_t n = Py_SIZE(deque);
- Py_ssize_t i;
Py_ssize_t count = 0;
size_t start_state = deque->state;
PyObject *item;
int cmp;
- for (i=0 ; i<n ; i++) {
+ n++;
+ while (--n) {
CHECK_NOT_END(b);
item = b->data[index];
cmp = PyObject_RichCompareBool(item, v, Py_EQ);
- if (cmp > 0)
- count++;
- else if (cmp < 0)
+ if (cmp < 0)
return NULL;
+ count += cmp;
if (start_state != deque->state) {
PyErr_SetString(PyExc_RuntimeError,
@@ -871,12 +972,12 @@ deque_contains(dequeobject *deque, PyObject *v)
block *b = deque->leftblock;
Py_ssize_t index = deque->leftindex;
Py_ssize_t n = Py_SIZE(deque);
- Py_ssize_t i;
size_t start_state = deque->state;
PyObject *item;
int cmp;
- for (i=0 ; i<n ; i++) {
+ n++;
+ while (--n) {
CHECK_NOT_END(b);
item = b->data[index];
cmp = PyObject_RichCompareBool(item, v, Py_EQ);
@@ -906,11 +1007,12 @@ deque_len(dequeobject *deque)
static PyObject *
deque_index(dequeobject *deque, PyObject *args)
{
- Py_ssize_t i, start=0, stop=Py_SIZE(deque);
+ Py_ssize_t i, n, start=0, stop=Py_SIZE(deque);
PyObject *v, *item;
block *b = deque->leftblock;
Py_ssize_t index = deque->leftindex;
size_t start_state = deque->state;
+ int cmp;
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
_PyEval_SliceIndex, &start,
@@ -928,22 +1030,32 @@ deque_index(dequeobject *deque, PyObject *args)
}
if (stop > Py_SIZE(deque))
stop = Py_SIZE(deque);
+ if (start > stop)
+ start = stop;
+ assert(0 <= start && start <= stop && stop <= Py_SIZE(deque));
- for (i=0 ; i<stop ; i++) {
- if (i >= start) {
- int cmp;
- CHECK_NOT_END(b);
- item = b->data[index];
- cmp = PyObject_RichCompareBool(item, v, Py_EQ);
- if (cmp > 0)
- return PyLong_FromSsize_t(i);
- else if (cmp < 0)
- return NULL;
- if (start_state != deque->state) {
- PyErr_SetString(PyExc_RuntimeError,
- "deque mutated during iteration");
- return NULL;
- }
+ /* XXX Replace this loop with faster code from deque_item() */
+ for (i=0 ; i<start ; i++) {
+ index++;
+ if (index == BLOCKLEN) {
+ b = b->rightlink;
+ index = 0;
+ }
+ }
+
+ n = stop - i + 1;
+ while (--n) {
+ CHECK_NOT_END(b);
+ item = b->data[index];
+ cmp = PyObject_RichCompareBool(item, v, Py_EQ);
+ if (cmp > 0)
+ return PyLong_FromSsize_t(stop - n);
+ if (cmp < 0)
+ return NULL;
+ if (start_state != deque->state) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "deque mutated during iteration");
+ return NULL;
}
index++;
if (index == BLOCKLEN) {
@@ -1037,84 +1149,10 @@ deque_remove(dequeobject *deque, PyObject *value)
PyDoc_STRVAR(remove_doc,
"D.remove(value) -- remove first occurrence of value.");
-static void
-deque_clear(dequeobject *deque)
-{
- block *b;
- block *prevblock;
- block *leftblock;
- Py_ssize_t leftindex;
- Py_ssize_t n;
- PyObject *item;
-
- if (Py_SIZE(deque) == 0)
- return;
-
- /* During the process of clearing a deque, decrefs can cause the
- deque to mutate. To avoid fatal confusion, we have to make the
- deque empty before clearing the blocks and never refer to
- anything via deque->ref while clearing. (This is the same
- technique used for clearing lists, sets, and dicts.)
-
- Making the deque empty requires allocating a new empty block. In
- the unlikely event that memory is full, we fall back to an
- alternate method that doesn't require a new block. Repeating
- pops in a while-loop is slower, possibly re-entrant (and a clever
- adversary could cause it to never terminate).
- */
-
- b = newblock(0);
- if (b == NULL) {
- PyErr_Clear();
- goto alternate_method;
- }
-
- /* Remember the old size, leftblock, and leftindex */
- leftblock = deque->leftblock;
- leftindex = deque->leftindex;
- n = Py_SIZE(deque);
-
- /* Set the deque to be empty using the newly allocated block */
- MARK_END(b->leftlink);
- MARK_END(b->rightlink);
- Py_SIZE(deque) = 0;
- deque->leftblock = b;
- deque->rightblock = b;
- deque->leftindex = CENTER + 1;
- deque->rightindex = CENTER;
- deque->state++;
-
- /* Now the old size, leftblock, and leftindex are disconnected from
- the empty deque and we can use them to decref the pointers.
- */
- while (n--) {
- item = leftblock->data[leftindex];
- Py_DECREF(item);
- leftindex++;
- if (leftindex == BLOCKLEN && n) {
- CHECK_NOT_END(leftblock->rightlink);
- prevblock = leftblock;
- leftblock = leftblock->rightlink;
- leftindex = 0;
- freeblock(prevblock);
- }
- }
- CHECK_END(leftblock->rightlink);
- freeblock(leftblock);
- return;
-
- alternate_method:
- while (Py_SIZE(deque)) {
- item = deque_pop(deque, NULL);
- assert (item != NULL);
- Py_DECREF(item);
- }
-}
-
static int
valid_index(Py_ssize_t i, Py_ssize_t limit)
{
- /* The cast to size_t let us use just a single comparison
+ /* The cast to size_t lets us use just a single comparison
to check whether i is in the range: 0 <= i < limit */
return (size_t) i < (size_t) limit;
}
@@ -1143,14 +1181,16 @@ deque_item(dequeobject *deque, Py_ssize_t i)
i = (Py_ssize_t)((size_t) i % BLOCKLEN);
if (index < (Py_SIZE(deque) >> 1)) {
b = deque->leftblock;
- while (n--)
+ n++;
+ while (--n)
b = b->rightlink;
} else {
n = (Py_ssize_t)(
((size_t)(deque->leftindex + Py_SIZE(deque) - 1))
/ BLOCKLEN - n);
b = deque->rightblock;
- while (n--)
+ n++;
+ while (--n)
b = b->leftlink;
}
}
@@ -1194,14 +1234,16 @@ deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
i = (Py_ssize_t)((size_t) i % BLOCKLEN);
if (index <= halflen) {
b = deque->leftblock;
- while (n--)
+ n++;
+ while (--n)
b = b->rightlink;
} else {
n = (Py_ssize_t)(
((size_t)(deque->leftindex + Py_SIZE(deque) - 1))
/ BLOCKLEN - n);
b = deque->rightblock;
- while (n--)
+ n++;
+ while (--n)
b = b->leftlink;
}
Py_INCREF(v);
@@ -1211,15 +1253,6 @@ deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
return 0;
}
-static PyObject *
-deque_clearmethod(dequeobject *deque)
-{
- deque_clear(deque);
- Py_RETURN_NONE;
-}
-
-PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
-
static void
deque_dealloc(dequeobject *deque)
{
@@ -1243,6 +1276,7 @@ deque_traverse(dequeobject *deque, visitproc visit, void *arg)
PyObject *item;
Py_ssize_t index;
Py_ssize_t indexlo = deque->leftindex;
+ Py_ssize_t indexhigh;
for (b = deque->leftblock; b != deque->rightblock; b = b->rightlink) {
for (index = indexlo; index < BLOCKLEN ; index++) {
@@ -1251,7 +1285,8 @@ deque_traverse(dequeobject *deque, visitproc visit, void *arg)
}
indexlo = 0;
}
- for (index = indexlo; index <= deque->rightindex; index++) {
+ indexhigh = deque->rightindex;
+ for (index = indexlo; index <= indexhigh; index++) {
item = b->data[index];
Py_VISIT(item);
}
@@ -1259,45 +1294,33 @@ deque_traverse(dequeobject *deque, visitproc visit, void *arg)
}
static PyObject *
-deque_copy(PyObject *deque)
-{
- if (((dequeobject *)deque)->maxlen == -1)
- return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL);
- else
- return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
- deque, ((dequeobject *)deque)->maxlen, NULL);
-}
-
-PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
-
-static PyObject *
deque_reduce(dequeobject *deque)
{
- PyObject *dict, *result, *aslist;
+ PyObject *dict, *it;
_Py_IDENTIFIER(__dict__);
dict = _PyObject_GetAttrId((PyObject *)deque, &PyId___dict__);
- if (dict == NULL)
+ if (dict == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
+ return NULL;
+ }
PyErr_Clear();
- aslist = PySequence_List((PyObject *)deque);
- if (aslist == NULL) {
- Py_XDECREF(dict);
+ dict = Py_None;
+ Py_INCREF(dict);
+ }
+
+ it = PyObject_GetIter((PyObject *)deque);
+ if (it == NULL) {
+ Py_DECREF(dict);
return NULL;
}
- if (dict == NULL) {
- if (deque->maxlen == -1)
- result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist);
- else
- result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen);
- } else {
- if (deque->maxlen == -1)
- result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict);
- else
- result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict);
+
+ if (deque->maxlen < 0) {
+ return Py_BuildValue("O()NN", Py_TYPE(deque), dict, it);
+ }
+ else {
+ return Py_BuildValue("O(()n)NN", Py_TYPE(deque), deque->maxlen, dict, it);
}
- Py_XDECREF(dict);
- Py_DECREF(aslist);
- return result;
}
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
@@ -1320,7 +1343,7 @@ deque_repr(PyObject *deque)
Py_ReprLeave(deque);
return NULL;
}
- if (((dequeobject *)deque)->maxlen != -1)
+ if (((dequeobject *)deque)->maxlen >= 0)
result = PyUnicode_FromFormat("deque(%R, maxlen=%zd)",
aslist, ((dequeobject *)deque)->maxlen);
else
@@ -1381,7 +1404,7 @@ deque_richcompare(PyObject *v, PyObject *w, int op)
}
Py_DECREF(x);
Py_DECREF(y);
- if (b == -1)
+ if (b < 0)
goto done;
}
/* We reached the end of one deque or both */
@@ -1416,8 +1439,14 @@ deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
Py_ssize_t maxlen = -1;
char *kwlist[] = {"iterable", "maxlen", 0};
- if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj))
- return -1;
+ if (kwdargs == NULL) {
+ if (!PyArg_UnpackTuple(args, "deque()", 0, 2, &iterable, &maxlenobj))
+ return -1;
+ } else {
+ if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist,
+ &iterable, &maxlenobj))
+ return -1;
+ }
if (maxlenobj != NULL && maxlenobj != Py_None) {
maxlen = PyLong_AsSsize_t(maxlenobj);
if (maxlen == -1 && PyErr_Occurred())
@@ -1446,7 +1475,7 @@ deque_sizeof(dequeobject *deque, void *unused)
Py_ssize_t blocks;
res = _PyObject_SIZE(Py_TYPE(deque));
- blocks = (deque->leftindex + Py_SIZE(deque) + BLOCKLEN - 1) / BLOCKLEN;
+ blocks = (size_t)(deque->leftindex + Py_SIZE(deque) + BLOCKLEN - 1) / BLOCKLEN;
assert(deque->leftindex + Py_SIZE(deque) - 1 ==
(blocks - 1) * BLOCKLEN + deque->rightindex);
res += blocks * sizeof(block);
@@ -1465,7 +1494,7 @@ deque_bool(dequeobject *deque)
static PyObject *
deque_get_maxlen(dequeobject *deque)
{
- if (deque->maxlen == -1)
+ if (deque->maxlen < 0)
Py_RETURN_NONE;
return PyLong_FromSsize_t(deque->maxlen);
}
@@ -1806,7 +1835,7 @@ dequereviter_next(dequeiterobject *it)
item = it->b->data[it->index];
it->index--;
it->counter--;
- if (it->index == -1 && it->counter > 0) {
+ if (it->index < 0 && it->counter > 0) {
CHECK_NOT_END(it->b->leftlink);
it->b = it->b->leftlink;
it->index = BLOCKLEN - 1;
@@ -2235,13 +2264,13 @@ _count_elements(PyObject *self, PyObject *args)
oldval = _PyDict_GetItem_KnownHash(mapping, key, hash);
if (oldval == NULL) {
- if (_PyDict_SetItem_KnownHash(mapping, key, one, hash) == -1)
+ if (_PyDict_SetItem_KnownHash(mapping, key, one, hash) < 0)
goto done;
} else {
newval = PyNumber_Add(oldval, one);
if (newval == NULL)
goto done;
- if (_PyDict_SetItem_KnownHash(mapping, key, newval, hash) == -1)
+ if (_PyDict_SetItem_KnownHash(mapping, key, newval, hash) < 0)
goto done;
Py_CLEAR(newval);
}
@@ -2267,7 +2296,7 @@ _count_elements(PyObject *self, PyObject *args)
Py_DECREF(oldval);
if (newval == NULL)
break;
- if (PyObject_SetItem(mapping, key, newval) == -1)
+ if (PyObject_SetItem(mapping, key, newval) < 0)
break;
Py_CLEAR(newval);
Py_DECREF(key);
diff --git a/Modules/_csv.c b/Modules/_csv.c
index 101f449d06..4284477a99 100644
--- a/Modules/_csv.c
+++ b/Modules/_csv.c
@@ -60,10 +60,10 @@ typedef enum {
typedef struct {
QuoteStyle style;
- char *name;
+ const char *name;
} StyleDesc;
-static StyleDesc quote_styles[] = {
+static const StyleDesc quote_styles[] = {
{ QUOTE_MINIMAL, "QUOTE_MINIMAL" },
{ QUOTE_ALL, "QUOTE_ALL" },
{ QUOTE_NONNUMERIC, "QUOTE_NONNUMERIC" },
@@ -286,7 +286,7 @@ _set_str(const char *name, PyObject **target, PyObject *src, const char *dflt)
static int
dialect_check_quoting(int quoting)
{
- StyleDesc *qs;
+ const StyleDesc *qs;
for (qs = quote_styles; qs->name; qs++) {
if ((int)qs->style == quoting)
@@ -1633,7 +1633,7 @@ PyMODINIT_FUNC
PyInit__csv(void)
{
PyObject *module;
- StyleDesc *style;
+ const StyleDesc *style;
if (PyType_Ready(&Dialect_Type) < 0)
return NULL;
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index a7a81056bc..331343bfba 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -435,7 +435,7 @@ UnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return StructUnionType_new(type, args, kwds, 0);
}
-static char from_address_doc[] =
+static const char from_address_doc[] =
"C.from_address(integer) -> C instance\naccess a C instance at the specified address";
static PyObject *
@@ -453,7 +453,7 @@ CDataType_from_address(PyObject *type, PyObject *value)
return PyCData_AtAddress(type, buf);
}
-static char from_buffer_doc[] =
+static const char from_buffer_doc[] =
"C.from_buffer(object, offset=0) -> C instance\ncreate a C instance from a writeable buffer";
static int
@@ -524,7 +524,7 @@ CDataType_from_buffer(PyObject *type, PyObject *args)
return result;
}
-static char from_buffer_copy_doc[] =
+static const char from_buffer_copy_doc[] =
"C.from_buffer_copy(object, offset=0) -> C instance\ncreate a C instance from a readable buffer";
static PyObject *
@@ -566,7 +566,7 @@ CDataType_from_buffer_copy(PyObject *type, PyObject *args)
return result;
}
-static char in_dll_doc[] =
+static const char in_dll_doc[] =
"C.in_dll(dll, name) -> C instance\naccess a C instance in a dll";
static PyObject *
@@ -623,7 +623,7 @@ CDataType_in_dll(PyObject *type, PyObject *args)
return PyCData_AtAddress(type, address);
}
-static char from_param_doc[] =
+static const char from_param_doc[] =
"Convert a Python object into a function call parameter.";
static PyObject *
@@ -1487,7 +1487,7 @@ _type_ attribute.
*/
-static char *SIMPLE_TYPE_CHARS = "cbBhHiIlLdfuzZqQPXOv?g";
+static const char SIMPLE_TYPE_CHARS[] = "cbBhHiIlLdfuzZqQPXOv?g";
static PyObject *
c_wchar_p_from_param(PyObject *type, PyObject *value)
@@ -2412,7 +2412,7 @@ unique_key(CDataObject *target, Py_ssize_t index)
char *cp = string;
size_t bytes_left;
- assert(sizeof(string) - 1 > sizeof(Py_ssize_t) * 2);
+ Py_BUILD_ASSERT(sizeof(string) - 1 > sizeof(Py_ssize_t) * 2);
cp += sprintf(cp, "%x", Py_SAFE_DOWNCAST(index, Py_ssize_t, int));
while (target->b_base) {
bytes_left = sizeof(string) - (cp - string) - 1;
@@ -3207,7 +3207,7 @@ _validate_paramflags(PyTypeObject *type, PyObject *paramflags)
}
static int
-_get_name(PyObject *obj, char **pname)
+_get_name(PyObject *obj, const char **pname)
{
#ifdef MS_WIN32
if (PyLong_Check(obj)) {
@@ -3235,7 +3235,7 @@ _get_name(PyObject *obj, char **pname)
static PyObject *
PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- char *name;
+ const char *name;
int (* address)(void);
PyObject *ftuple;
PyObject *dll;
@@ -5125,29 +5125,28 @@ static const char module_docs[] =
#ifdef MS_WIN32
-static char comerror_doc[] = "Raised when a COM method call failed.";
+static const char comerror_doc[] = "Raised when a COM method call failed.";
int
comerror_init(PyObject *self, PyObject *args, PyObject *kwds)
{
PyObject *hresult, *text, *details;
- PyBaseExceptionObject *bself;
PyObject *a;
int status;
if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
- return -1;
+ return -1;
if (!PyArg_ParseTuple(args, "OOO:COMError", &hresult, &text, &details))
return -1;
a = PySequence_GetSlice(args, 1, PySequence_Size(args));
if (!a)
- return -1;
+ return -1;
status = PyObject_SetAttrString(self, "args", a);
Py_DECREF(a);
if (status < 0)
- return -1;
+ return -1;
if (PyObject_SetAttrString(self, "hresult", hresult) < 0)
return -1;
@@ -5158,9 +5157,8 @@ comerror_init(PyObject *self, PyObject *args, PyObject *kwds)
if (PyObject_SetAttrString(self, "details", details) < 0)
return -1;
- bself = (PyBaseExceptionObject *)self;
Py_INCREF(args);
- Py_SETREF(bself->args, args);
+ Py_SETREF(((PyBaseExceptionObject *)self)->args, args);
return 0;
}
@@ -5490,14 +5488,14 @@ PyInit__ctypes(void)
#endif
/* If RTLD_LOCAL is not defined (Windows!), set it to zero. */
-#ifndef RTLD_LOCAL
+#if !HAVE_DECL_RTLD_LOCAL
#define RTLD_LOCAL 0
#endif
/* If RTLD_GLOBAL is not defined (cygwin), set it to the same value as
RTLD_LOCAL.
*/
-#ifndef RTLD_GLOBAL
+#if !HAVE_DECL_RTLD_GLOBAL
#define RTLD_GLOBAL RTLD_LOCAL
#endif
diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c
index 7cd61640f8..00e8e66a2c 100644
--- a/Modules/_ctypes/callbacks.c
+++ b/Modules/_ctypes/callbacks.c
@@ -77,7 +77,7 @@ PyTypeObject PyCThunk_Type = {
/**************************************************************/
static void
-PrintError(char *msg, ...)
+PrintError(const char *msg, ...)
{
char buf[512];
PyObject *f = PySys_GetObject("stderr");
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
index 30e3a969fc..a276fcdfdf 100644
--- a/Modules/_ctypes/callproc.c
+++ b/Modules/_ctypes/callproc.c
@@ -382,7 +382,7 @@ static void SetException(DWORD code, EXCEPTION_RECORD *pr)
whose operation is not allowed in the current
machine mode. */
PyErr_SetString(PyExc_OSError,
- "exception: priviledged instruction");
+ "exception: privileged instruction");
break;
case EXCEPTION_NONCONTINUABLE_EXCEPTION:
@@ -930,7 +930,7 @@ static PyObject *GetResult(PyObject *restype, void *result, PyObject *checker)
* Raise a new exception 'exc_class', adding additional text to the original
* exception string.
*/
-void _ctypes_extend_error(PyObject *exc_class, char *fmt, ...)
+void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...)
{
va_list vargs;
PyObject *tp, *v, *tb, *s, *cls_str, *msg_str;
@@ -1203,7 +1203,7 @@ _parse_voidp(PyObject *obj, void **address)
#ifdef MS_WIN32
-static char format_error_doc[] =
+static const char format_error_doc[] =
"FormatError([integer]) -> string\n\
\n\
Convert a win32 error code into a string. If the error code is not\n\
@@ -1227,7 +1227,7 @@ static PyObject *format_error(PyObject *self, PyObject *args)
return result;
}
-static char load_library_doc[] =
+static const char load_library_doc[] =
"LoadLibrary(name) -> handle\n\
\n\
Load an executable (usually a DLL), and return a handle to it.\n\
@@ -1256,7 +1256,7 @@ static PyObject *load_library(PyObject *self, PyObject *args)
#endif
}
-static char free_library_doc[] =
+static const char free_library_doc[] =
"FreeLibrary(handle) -> void\n\
\n\
Free the handle of an executable previously loaded by LoadLibrary.\n";
@@ -1271,7 +1271,7 @@ static PyObject *free_library(PyObject *self, PyObject *args)
return Py_None;
}
-static char copy_com_pointer_doc[] =
+static const char copy_com_pointer_doc[] =
"CopyComPointer(src, dst) -> HRESULT value\n";
static PyObject *
@@ -1309,7 +1309,7 @@ static PyObject *py_dl_open(PyObject *self, PyObject *args)
PyObject *name, *name2;
char *name_str;
void * handle;
-#ifdef RTLD_LOCAL
+#if HAVE_DECL_RTLD_LOCAL
int mode = RTLD_NOW | RTLD_LOCAL;
#else
/* cygwin doesn't define RTLD_LOCAL */
@@ -1441,7 +1441,7 @@ call_cdeclfunction(PyObject *self, PyObject *args)
/*****************************************************************
* functions
*/
-static char sizeof_doc[] =
+static const char sizeof_doc[] =
"sizeof(C type) -> integer\n"
"sizeof(C instance) -> integer\n"
"Return the size in bytes of a C instance";
@@ -1462,7 +1462,7 @@ sizeof_func(PyObject *self, PyObject *obj)
return NULL;
}
-static char alignment_doc[] =
+static const char alignment_doc[] =
"alignment(C type) -> integer\n"
"alignment(C instance) -> integer\n"
"Return the alignment requirements of a C instance";
@@ -1485,7 +1485,7 @@ align_func(PyObject *self, PyObject *obj)
return NULL;
}
-static char byref_doc[] =
+static const char byref_doc[] =
"byref(C instance[, offset=0]) -> byref-object\n"
"Return a pointer lookalike to a C instance, only usable\n"
"as function argument";
@@ -1529,7 +1529,7 @@ byref(PyObject *self, PyObject *args)
return (PyObject *)parg;
}
-static char addressof_doc[] =
+static const char addressof_doc[] =
"addressof(C instance) -> integer\n"
"Return the address of the C instance internal buffer";
diff --git a/Modules/_ctypes/ctypes.h b/Modules/_ctypes/ctypes.h
index 0d3f7241ca..b06ba8ae46 100644
--- a/Modules/_ctypes/ctypes.h
+++ b/Modules/_ctypes/ctypes.h
@@ -327,7 +327,7 @@ extern int
PyCData_set(PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
Py_ssize_t index, Py_ssize_t size, char *ptr);
-extern void _ctypes_extend_error(PyObject *exc_class, char *fmt, ...);
+extern void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...);
struct basespec {
CDataObject *base;
diff --git a/Modules/_curses_panel.c b/Modules/_curses_panel.c
index 228f4971ac..18ef335b82 100644
--- a/Modules/_curses_panel.c
+++ b/Modules/_curses_panel.c
@@ -6,7 +6,7 @@
/* Release Number */
-static char *PyCursesVersion = "2.1";
+static const char PyCursesVersion[] = "2.1";
/* Includes */
@@ -56,7 +56,7 @@ static struct PyModuleDef _curses_panelmodule;
*/
static PyObject *
-PyCursesCheckERR(int code, char *fname)
+PyCursesCheckERR(int code, const char *fname)
{
if (code != ERR) {
Py_INCREF(Py_None);
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index f419cb22ef..61b66a66cb 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -184,12 +184,12 @@ divide_nearest(PyObject *m, PyObject *n)
* and the number of days before that month in the same year. These
* are correct for non-leap years only.
*/
-static int _days_in_month[] = {
+static const int _days_in_month[] = {
0, /* unused; this vector uses 1-based indexing */
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
-static int _days_before_month[] = {
+static const int _days_before_month[] = {
0, /* unused; this vector uses 1-based indexing */
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
};
@@ -873,7 +873,7 @@ get_tzinfo_member(PyObject *self)
* this returns NULL. Else result is returned.
*/
static PyObject *
-call_tzinfo_method(PyObject *tzinfo, char *name, PyObject *tzinfoarg)
+call_tzinfo_method(PyObject *tzinfo, const char *name, PyObject *tzinfoarg)
{
PyObject *offset;
@@ -1009,10 +1009,10 @@ append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo)
static PyObject *
format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds)
{
- static const char *DayNames[] = {
+ static const char * const DayNames[] = {
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
};
- static const char *MonthNames[] = {
+ static const char * const MonthNames[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
@@ -1057,10 +1057,8 @@ format_utcoffset(char *buf, size_t buflen, const char *sep,
}
/* Offset is normalized, so it is negative if days < 0 */
if (GET_TD_DAYS(offset) < 0) {
- PyObject *temp = offset;
sign = '-';
- offset = delta_negative((PyDateTime_Delta *)offset);
- Py_DECREF(temp);
+ Py_SETREF(offset, delta_negative((PyDateTime_Delta *)offset));
if (offset == NULL)
return -1;
}
@@ -2307,7 +2305,7 @@ static PyMethodDef delta_methods[] = {
{NULL, NULL},
};
-static char delta_doc[] =
+static const char delta_doc[] =
PyDoc_STR("Difference between two datetime values.");
static PyNumberMethods delta_as_number = {
@@ -2886,7 +2884,7 @@ static PyMethodDef date_methods[] = {
{NULL, NULL}
};
-static char date_doc[] =
+static const char date_doc[] =
PyDoc_STR("date(year, month, day) --> date object");
static PyNumberMethods date_as_number = {
@@ -3047,10 +3045,8 @@ tzinfo_fromutc(PyDateTime_TZInfo *self, PyObject *dt)
if (dst == Py_None)
goto Inconsistent;
if (delta_bool((PyDateTime_Delta *)dst) != 0) {
- PyObject *temp = result;
- result = add_datetime_timedelta((PyDateTime_DateTime *)result,
- (PyDateTime_Delta *)dst, 1);
- Py_DECREF(temp);
+ Py_SETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result,
+ (PyDateTime_Delta *)dst, 1));
if (result == NULL)
goto Fail;
}
@@ -3155,7 +3151,7 @@ static PyMethodDef tzinfo_methods[] = {
{NULL, NULL}
};
-static char tzinfo_doc[] =
+static const char tzinfo_doc[] =
PyDoc_STR("Abstract base class for time zone info objects.");
static PyTypeObject PyDateTime_TZInfoType = {
@@ -3287,6 +3283,11 @@ timezone_str(PyDateTime_TimeZone *self)
Py_INCREF(self->name);
return self->name;
}
+ if ((PyObject *)self == PyDateTime_TimeZone_UTC ||
+ (GET_TD_DAYS(self->offset) == 0 &&
+ GET_TD_SECONDS(self->offset) == 0 &&
+ GET_TD_MICROSECONDS(self->offset) == 0))
+ return PyUnicode_FromString("UTC");
/* Offset is normalized, so it is negative if days < 0 */
if (GET_TD_DAYS(self->offset) < 0) {
sign = '-';
@@ -3382,7 +3383,7 @@ static PyMethodDef timezone_methods[] = {
{NULL, NULL}
};
-static char timezone_doc[] =
+static const char timezone_doc[] =
PyDoc_STR("Fixed offset from UTC implementation of tzinfo.");
static PyTypeObject PyDateTime_TimeZoneType = {
@@ -3607,23 +3608,56 @@ time_str(PyDateTime_Time *self)
}
static PyObject *
-time_isoformat(PyDateTime_Time *self, PyObject *unused)
+time_isoformat(PyDateTime_Time *self, PyObject *args, PyObject *kw)
{
char buf[100];
+ char *timespec = NULL;
+ static char *keywords[] = {"timespec", NULL};
PyObject *result;
int us = TIME_GET_MICROSECOND(self);
+ static char *specs[][2] = {
+ {"hours", "%02d"},
+ {"minutes", "%02d:%02d"},
+ {"seconds", "%02d:%02d:%02d"},
+ {"milliseconds", "%02d:%02d:%02d.%03d"},
+ {"microseconds", "%02d:%02d:%02d.%06d"},
+ };
+ size_t given_spec;
- if (us)
- result = PyUnicode_FromFormat("%02d:%02d:%02d.%06d",
- TIME_GET_HOUR(self),
- TIME_GET_MINUTE(self),
- TIME_GET_SECOND(self),
- us);
- else
- result = PyUnicode_FromFormat("%02d:%02d:%02d",
- TIME_GET_HOUR(self),
- TIME_GET_MINUTE(self),
- TIME_GET_SECOND(self));
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "|s:isoformat", keywords, &timespec))
+ return NULL;
+
+ if (timespec == NULL || strcmp(timespec, "auto") == 0) {
+ if (us == 0) {
+ /* seconds */
+ given_spec = 2;
+ }
+ else {
+ /* microseconds */
+ given_spec = 4;
+ }
+ }
+ else {
+ for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) {
+ if (strcmp(timespec, specs[given_spec][0]) == 0) {
+ if (given_spec == 3) {
+ /* milliseconds */
+ us = us / 1000;
+ }
+ break;
+ }
+ }
+ }
+
+ if (given_spec == Py_ARRAY_LENGTH(specs)) {
+ PyErr_Format(PyExc_ValueError, "Unknown timespec value");
+ return NULL;
+ }
+ else {
+ result = PyUnicode_FromFormat(specs[given_spec][1],
+ TIME_GET_HOUR(self), TIME_GET_MINUTE(self),
+ TIME_GET_SECOND(self), us);
+ }
if (result == NULL || !HASTZINFO(self) || self->tzinfo == Py_None)
return result;
@@ -3844,9 +3878,10 @@ time_reduce(PyDateTime_Time *self, PyObject *arg)
static PyMethodDef time_methods[] = {
- {"isoformat", (PyCFunction)time_isoformat, METH_NOARGS,
- PyDoc_STR("Return string in ISO 8601 format, HH:MM:SS[.mmmmmm]"
- "[+HH:MM].")},
+ {"isoformat", (PyCFunction)time_isoformat, METH_VARARGS | METH_KEYWORDS,
+ PyDoc_STR("Return string in ISO 8601 format, [HH[:MM[:SS[.mmm[uuu]]]]]"
+ "[+HH:MM].\n\n"
+ "timespec specifies what components of the time to include.\n")},
{"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYWORDS,
PyDoc_STR("format -> strftime() style string.")},
@@ -3872,7 +3907,7 @@ static PyMethodDef time_methods[] = {
{NULL, NULL}
};
-static char time_doc[] =
+static const char time_doc[] =
PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n\
\n\
All arguments are optional. tzinfo may be None, or an instance of\n\
@@ -4083,44 +4118,6 @@ datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us,
tzinfo);
}
-static time_t
-_PyTime_DoubleToTimet(double x)
-{
- time_t result;
- double diff;
-
- result = (time_t)x;
- /* How much info did we lose? time_t may be an integral or
- * floating type, and we don't know which. If it's integral,
- * we don't know whether C truncates, rounds, returns the floor,
- * etc. If we lost a second or more, the C rounding is
- * unreasonable, or the input just doesn't fit in a time_t;
- * call it an error regardless. Note that the original cast to
- * time_t can cause a C error too, but nothing we can do to
- * worm around that.
- */
- diff = x - (double)result;
- if (diff <= -1.0 || diff >= 1.0) {
- PyErr_SetString(PyExc_OverflowError,
- "timestamp out of range for platform time_t");
- result = (time_t)-1;
- }
- return result;
-}
-
-/* Round a double to the nearest long. |x| must be small enough to fit
- * in a C long; this is not checked.
- */
-static double
-_PyTime_RoundHalfEven(double x)
-{
- double rounded = round(x);
- if (fabs(x-rounded) == 0.5)
- /* halfway case: round to even */
- rounded = 2.0*round(x/2.0);
- return rounded;
-}
-
/* Internal helper.
* Build datetime from a Python timestamp. Pass localtime or gmtime for f,
* to control the interpretation of the timestamp. Since a double doesn't
@@ -4129,32 +4126,17 @@ _PyTime_RoundHalfEven(double x)
* to get that much precision (e.g., C time() isn't good enough).
*/
static PyObject *
-datetime_from_timestamp(PyObject *cls, TM_FUNC f, double timestamp,
+datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp,
PyObject *tzinfo)
{
time_t timet;
- double fraction;
- int us;
+ long us;
- timet = _PyTime_DoubleToTimet(timestamp);
- if (timet == (time_t)-1 && PyErr_Occurred())
+ if (_PyTime_ObjectToTimeval(timestamp,
+ &timet, &us, _PyTime_ROUND_HALF_EVEN) == -1)
return NULL;
- fraction = timestamp - (double)timet;
- us = (int)_PyTime_RoundHalfEven(fraction * 1e6);
- if (us < 0) {
- /* Truncation towards zero is not what we wanted
- for negative numbers (Python's mod semantics) */
- timet -= 1;
- us += 1000000;
- }
- /* If timestamp is less than one microsecond smaller than a
- * full second, round up. Otherwise, ValueErrors are raised
- * for some floats. */
- if (us == 1000000) {
- timet += 1;
- us = 0;
- }
- return datetime_from_timet_and_us(cls, f, timet, us, tzinfo);
+
+ return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo);
}
/* Internal helper.
@@ -4205,10 +4187,7 @@ datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz)
tz);
if (self != NULL && tz != Py_None) {
/* Convert UTC to tzinfo's zone. */
- PyObject *temp = self;
-
- self = _PyObject_CallMethodId(tz, &PyId_fromutc, "O", self);
- Py_DECREF(temp);
+ self = _PyObject_CallMethodId(tz, &PyId_fromutc, "N", self);
}
return self;
}
@@ -4227,11 +4206,11 @@ static PyObject *
datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw)
{
PyObject *self;
- double timestamp;
+ PyObject *timestamp;
PyObject *tzinfo = Py_None;
static char *keywords[] = {"timestamp", "tz", NULL};
- if (! PyArg_ParseTupleAndKeywords(args, kw, "d|O:fromtimestamp",
+ if (! PyArg_ParseTupleAndKeywords(args, kw, "O|O:fromtimestamp",
keywords, &timestamp, &tzinfo))
return NULL;
if (check_tzinfo_subclass(tzinfo) < 0)
@@ -4243,10 +4222,7 @@ datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw)
tzinfo);
if (self != NULL && tzinfo != Py_None) {
/* Convert UTC to tzinfo's zone. */
- PyObject *temp = self;
-
- self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "O", self);
- Py_DECREF(temp);
+ self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "N", self);
}
return self;
}
@@ -4255,10 +4231,10 @@ datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw)
static PyObject *
datetime_utcfromtimestamp(PyObject *cls, PyObject *args)
{
- double timestamp;
+ PyObject *timestamp;
PyObject *result = NULL;
- if (PyArg_ParseTuple(args, "d:utcfromtimestamp", &timestamp))
+ if (PyArg_ParseTuple(args, "O:utcfromtimestamp", &timestamp))
result = datetime_from_timestamp(cls, gmtime, timestamp,
Py_None);
return result;
@@ -4469,9 +4445,7 @@ datetime_subtract(PyObject *left, PyObject *right)
return NULL;
if (offdiff != NULL) {
- PyObject *temp = result;
- result = delta_subtract(result, offdiff);
- Py_DECREF(temp);
+ Py_SETREF(result, delta_subtract(result, offdiff));
Py_DECREF(offdiff);
}
}
@@ -4536,25 +4510,55 @@ static PyObject *
datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
{
int sep = 'T';
- static char *keywords[] = {"sep", NULL};
+ char *timespec = NULL;
+ static char *keywords[] = {"sep", "timespec", NULL};
char buffer[100];
- PyObject *result;
+ PyObject *result = NULL;
int us = DATE_GET_MICROSECOND(self);
+ static char *specs[][2] = {
+ {"hours", "%04d-%02d-%02d%c%02d"},
+ {"minutes", "%04d-%02d-%02d%c%02d:%02d"},
+ {"seconds", "%04d-%02d-%02d%c%02d:%02d:%02d"},
+ {"milliseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%03d"},
+ {"microseconds", "%04d-%02d-%02d%c%02d:%02d:%02d.%06d"},
+ };
+ size_t given_spec;
- if (!PyArg_ParseTupleAndKeywords(args, kw, "|C:isoformat", keywords, &sep))
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "|Cs:isoformat", keywords, &sep, &timespec))
return NULL;
- if (us)
- result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d.%06d",
+
+ if (timespec == NULL || strcmp(timespec, "auto") == 0) {
+ if (us == 0) {
+ /* seconds */
+ given_spec = 2;
+ }
+ else {
+ /* microseconds */
+ given_spec = 4;
+ }
+ }
+ else {
+ for (given_spec = 0; given_spec < Py_ARRAY_LENGTH(specs); given_spec++) {
+ if (strcmp(timespec, specs[given_spec][0]) == 0) {
+ if (given_spec == 3) {
+ us = us / 1000;
+ }
+ break;
+ }
+ }
+ }
+
+ if (given_spec == Py_ARRAY_LENGTH(specs)) {
+ PyErr_Format(PyExc_ValueError, "Unknown timespec value");
+ return NULL;
+ }
+ else {
+ result = PyUnicode_FromFormat(specs[given_spec][1],
GET_YEAR(self), GET_MONTH(self),
GET_DAY(self), (int)sep,
DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
DATE_GET_SECOND(self), us);
- else
- result = PyUnicode_FromFormat("%04d-%02d-%02d%c%02d:%02d:%02d",
- GET_YEAR(self), GET_MONTH(self),
- GET_DAY(self), (int)sep,
- DATE_GET_HOUR(self), DATE_GET_MINUTE(self),
- DATE_GET_SECOND(self));
+ }
if (!result || !HASTZINFO(self))
return result;
@@ -4766,7 +4770,7 @@ local_timezone(PyDateTime_DateTime *utc_time)
if (seconds == NULL)
goto error;
Py_DECREF(delta);
- timestamp = PyLong_AsLong(seconds);
+ timestamp = _PyLong_AsTime_t(seconds);
Py_DECREF(seconds);
if (timestamp == -1 && PyErr_Occurred())
return NULL;
@@ -5093,9 +5097,12 @@ static PyMethodDef datetime_methods[] = {
{"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS,
PyDoc_STR("[sep] -> string in ISO 8601 format, "
- "YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM].\n\n"
+ "YYYY-MM-DDT[HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].\n"
"sep is used to separate the year from the time, and "
- "defaults to 'T'.")},
+ "defaults to 'T'.\n"
+ "timespec specifies what components of the time to include"
+ " (allowed values are 'auto', 'hours', 'minutes', 'seconds',"
+ " 'milliseconds', and 'microseconds').\n")},
{"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS,
PyDoc_STR("Return self.tzinfo.utcoffset(self).")},
@@ -5118,7 +5125,7 @@ static PyMethodDef datetime_methods[] = {
{NULL, NULL}
};
-static char datetime_doc[] =
+static const char datetime_doc[] =
PyDoc_STR("datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n\
\n\
The year, month and day arguments are required. tzinfo may be None, or an\n\
@@ -5382,19 +5389,19 @@ PyInit__datetime(void)
/* A 4-year cycle has an extra leap day over what we'd get from
* pasting together 4 single years.
*/
- assert(DI4Y == 4 * 365 + 1);
+ Py_BUILD_ASSERT(DI4Y == 4 * 365 + 1);
assert(DI4Y == days_before_year(4+1));
/* Similarly, a 400-year cycle has an extra leap day over what we'd
* get from pasting together 4 100-year cycles.
*/
- assert(DI400Y == 4 * DI100Y + 1);
+ Py_BUILD_ASSERT(DI400Y == 4 * DI100Y + 1);
assert(DI400Y == days_before_year(400+1));
/* OTOH, a 100-year cycle has one fewer leap day than we'd get from
* pasting together 25 4-year cycles.
*/
- assert(DI100Y == 25 * DI4Y - 1);
+ Py_BUILD_ASSERT(DI100Y == 25 * DI4Y - 1);
assert(DI100Y == days_before_year(100+1));
one = PyLong_FromLong(1);
diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c
index ea8790b0e7..804978a6ac 100644
--- a/Modules/_dbmmodule.c
+++ b/Modules/_dbmmodule.c
@@ -14,16 +14,16 @@
*/
#if defined(HAVE_NDBM_H)
#include <ndbm.h>
-static char *which_dbm = "GNU gdbm"; /* EMX port of GDBM */
+static const char which_dbm[] = "GNU gdbm"; /* EMX port of GDBM */
#elif defined(HAVE_GDBM_NDBM_H)
#include <gdbm/ndbm.h>
-static char *which_dbm = "GNU gdbm";
+static const char which_dbm[] = "GNU gdbm";
#elif defined(HAVE_GDBM_DASH_NDBM_H)
#include <gdbm-ndbm.h>
-static char *which_dbm = "GNU gdbm";
+static const char which_dbm[] = "GNU gdbm";
#elif defined(HAVE_BERKDB_H)
#include <db.h>
-static char *which_dbm = "Berkeley DB";
+static const char which_dbm[] = "Berkeley DB";
#else
#error "No ndbm.h available!"
#endif
diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c
index e15941aec8..3ba8e35ce9 100644
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -36,7 +36,6 @@
#include <stdlib.h>
#include "docstrings.h"
-#include "memory.h"
#if !defined(MPD_VERSION_HEX) || MPD_VERSION_HEX < 0x02040100
@@ -3394,6 +3393,105 @@ dec_as_long(PyObject *dec, PyObject *context, int round)
return (PyObject *) pylong;
}
+/* Convert a Decimal to its exact integer ratio representation. */
+static PyObject *
+dec_as_integer_ratio(PyObject *self, PyObject *args UNUSED)
+{
+ PyObject *numerator = NULL;
+ PyObject *denominator = NULL;
+ PyObject *exponent = NULL;
+ PyObject *result = NULL;
+ PyObject *tmp;
+ mpd_ssize_t exp;
+ PyObject *context;
+ uint32_t status = 0;
+
+ if (mpd_isspecial(MPD(self))) {
+ if (mpd_isnan(MPD(self))) {
+ PyErr_SetString(PyExc_ValueError,
+ "cannot convert NaN to integer ratio");
+ }
+ else {
+ PyErr_SetString(PyExc_OverflowError,
+ "cannot convert Infinity to integer ratio");
+ }
+ return NULL;
+ }
+
+ CURRENT_CONTEXT(context);
+
+ tmp = dec_alloc();
+ if (tmp == NULL) {
+ return NULL;
+ }
+
+ if (!mpd_qcopy(MPD(tmp), MPD(self), &status)) {
+ Py_DECREF(tmp);
+ PyErr_NoMemory();
+ return NULL;
+ }
+
+ exp = mpd_iszero(MPD(tmp)) ? 0 : MPD(tmp)->exp;
+ MPD(tmp)->exp = 0;
+
+ /* context and rounding are unused here: the conversion is exact */
+ numerator = dec_as_long(tmp, context, MPD_ROUND_FLOOR);
+ Py_DECREF(tmp);
+ if (numerator == NULL) {
+ goto error;
+ }
+
+ exponent = PyLong_FromSsize_t(exp < 0 ? -exp : exp);
+ if (exponent == NULL) {
+ goto error;
+ }
+
+ tmp = PyLong_FromLong(10);
+ if (tmp == NULL) {
+ goto error;
+ }
+
+ Py_SETREF(exponent, _py_long_power(tmp, exponent, Py_None));
+ Py_DECREF(tmp);
+ if (exponent == NULL) {
+ goto error;
+ }
+
+ if (exp >= 0) {
+ Py_SETREF(numerator, _py_long_multiply(numerator, exponent));
+ if (numerator == NULL) {
+ goto error;
+ }
+ denominator = PyLong_FromLong(1);
+ if (denominator == NULL) {
+ goto error;
+ }
+ }
+ else {
+ denominator = exponent;
+ exponent = NULL;
+ tmp = _PyLong_GCD(numerator, denominator);
+ if (tmp == NULL) {
+ goto error;
+ }
+ Py_SETREF(numerator, _py_long_floor_divide(numerator, tmp));
+ Py_SETREF(denominator, _py_long_floor_divide(denominator, tmp));
+ Py_DECREF(tmp);
+ if (numerator == NULL || denominator == NULL) {
+ goto error;
+ }
+ }
+
+ result = PyTuple_Pack(2, numerator, denominator);
+
+
+error:
+ Py_XDECREF(exponent);
+ Py_XDECREF(denominator);
+ Py_XDECREF(numerator);
+ return result;
+}
+
static PyObject *
PyDec_ToIntegralValue(PyObject *dec, PyObject *args, PyObject *kwds)
{
@@ -4702,6 +4800,7 @@ static PyMethodDef dec_methods [] =
/* Miscellaneous */
{ "from_float", dec_from_float, METH_O|METH_CLASS, doc_from_float },
{ "as_tuple", PyDec_AsTuple, METH_NOARGS, doc_as_tuple },
+ { "as_integer_ratio", dec_as_integer_ratio, METH_NOARGS, doc_as_integer_ratio },
/* Special methods */
{ "__copy__", dec_copy, METH_NOARGS, NULL },
diff --git a/Modules/_decimal/docstrings.h b/Modules/_decimal/docstrings.h
index 71029a994b..f7fd6e7952 100644
--- a/Modules/_decimal/docstrings.h
+++ b/Modules/_decimal/docstrings.h
@@ -70,6 +70,15 @@ PyDoc_STRVAR(doc_as_tuple,
Return a tuple representation of the number.\n\
\n");
+PyDoc_STRVAR(doc_as_integer_ratio,
+"as_integer_ratio($self, /)\n--\n\n\
+Decimal.as_integer_ratio() -> (int, int)\n\
+\n\
+Return a pair of integers, whose ratio is exactly equal to the original\n\
+Decimal and with a positive denominator. The ratio is in lowest terms.\n\
+Raise OverflowError on infinities and a ValueError on NaNs.\n\
+\n");
+
PyDoc_STRVAR(doc_canonical,
"canonical($self, /)\n--\n\n\
Return the canonical encoding of the argument. Currently, the encoding\n\
diff --git a/Modules/_decimal/libmpdec/basearith.c b/Modules/_decimal/libmpdec/basearith.c
index 35de6b8284..dfe1523927 100644
--- a/Modules/_decimal/libmpdec/basearith.c
+++ b/Modules/_decimal/libmpdec/basearith.c
@@ -32,7 +32,6 @@
#include <string.h>
#include <assert.h>
#include "constants.h"
-#include "memory.h"
#include "typearith.h"
#include "basearith.h"
diff --git a/Modules/_decimal/libmpdec/io.c b/Modules/_decimal/libmpdec/io.c
index a45a429dbf..3aadfb0437 100644
--- a/Modules/_decimal/libmpdec/io.c
+++ b/Modules/_decimal/libmpdec/io.c
@@ -37,7 +37,6 @@
#include <locale.h>
#include "bits.h"
#include "constants.h"
-#include "memory.h"
#include "typearith.h"
#include "io.h"
diff --git a/Modules/_decimal/libmpdec/memory.c b/Modules/_decimal/libmpdec/memory.c
index 0f41fe5064..a854e09911 100644
--- a/Modules/_decimal/libmpdec/memory.c
+++ b/Modules/_decimal/libmpdec/memory.c
@@ -30,7 +30,12 @@
#include <stdio.h>
#include <stdlib.h>
#include "typearith.h"
-#include "memory.h"
+#include "mpalloc.h"
+
+
+#if defined(_MSC_VER)
+ #pragma warning(disable : 4232)
+#endif
/* Guaranteed minimum allocation for a coefficient. May be changed once
diff --git a/Modules/_decimal/libmpdec/memory.h b/Modules/_decimal/libmpdec/mpalloc.h
index 9c98d1a400..efd711953a 100644
--- a/Modules/_decimal/libmpdec/memory.h
+++ b/Modules/_decimal/libmpdec/mpalloc.h
@@ -26,8 +26,8 @@
*/
-#ifndef MEMORY_H
-#define MEMORY_H
+#ifndef MPALLOC_H
+#define MPALLOC_H
#include "mpdecimal.h"
diff --git a/Modules/_decimal/libmpdec/mpdecimal.c b/Modules/_decimal/libmpdec/mpdecimal.c
index 593f9f5e03..328ab92246 100644
--- a/Modules/_decimal/libmpdec/mpdecimal.c
+++ b/Modules/_decimal/libmpdec/mpdecimal.c
@@ -36,7 +36,7 @@
#include "bits.h"
#include "convolute.h"
#include "crt.h"
-#include "memory.h"
+#include "mpalloc.h"
#include "typearith.h"
#include "umodarith.h"
diff --git a/Modules/_decimal/libmpdec/mpdecimal.h b/Modules/_decimal/libmpdec/mpdecimal.h
index 5ca74135bf..56e4887cc8 100644
--- a/Modules/_decimal/libmpdec/mpdecimal.h
+++ b/Modules/_decimal/libmpdec/mpdecimal.h
@@ -108,9 +108,9 @@ MPD_PRAGMA(MPD_HIDE_SYMBOLS_START)
#define MPD_MAJOR_VERSION 2
#define MPD_MINOR_VERSION 4
-#define MPD_MICRO_VERSION 1
+#define MPD_MICRO_VERSION 2
-#define MPD_VERSION "2.4.1"
+#define MPD_VERSION "2.4.2"
#define MPD_VERSION_HEX ((MPD_MAJOR_VERSION << 24) | \
(MPD_MINOR_VERSION << 16) | \
diff --git a/Modules/_decimal/tests/deccheck.py b/Modules/_decimal/tests/deccheck.py
index ab7d5bdf4e..f907531e1f 100644
--- a/Modules/_decimal/tests/deccheck.py
+++ b/Modules/_decimal/tests/deccheck.py
@@ -50,8 +50,8 @@ Functions = {
'__abs__', '__bool__', '__ceil__', '__complex__', '__copy__',
'__floor__', '__float__', '__hash__', '__int__', '__neg__',
'__pos__', '__reduce__', '__repr__', '__str__', '__trunc__',
- 'adjusted', 'as_tuple', 'canonical', 'conjugate', 'copy_abs',
- 'copy_negate', 'is_canonical', 'is_finite', 'is_infinite',
+ 'adjusted', 'as_integer_ratio', 'as_tuple', 'canonical', 'conjugate',
+ 'copy_abs', 'copy_negate', 'is_canonical', 'is_finite', 'is_infinite',
'is_nan', 'is_qnan', 'is_signed', 'is_snan', 'is_zero', 'radix'
),
# Unary with optional context:
@@ -128,7 +128,7 @@ ContextFunctions = {
# Functions that require a restricted exponent range for reasonable runtimes.
UnaryRestricted = [
'__ceil__', '__floor__', '__int__', '__trunc__',
- 'to_integral', 'to_integral_value'
+ 'as_integer_ratio', 'to_integral', 'to_integral_value'
]
BinaryRestricted = ['__round__']
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 85ffca206b..b32f2ad283 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -129,30 +129,6 @@ elementtree_free(void *m)
/* helpers */
LOCAL(PyObject*)
-deepcopy(PyObject* object, PyObject* memo)
-{
- /* do a deep copy of the given object */
- PyObject* args;
- PyObject* result;
- elementtreestate *st = ET_STATE_GLOBAL;
-
- if (!st->deepcopy_obj) {
- PyErr_SetString(
- PyExc_RuntimeError,
- "deepcopy helper not found"
- );
- return NULL;
- }
-
- args = PyTuple_Pack(2, object, memo);
- if (!args)
- return NULL;
- result = PyObject_CallObject(st->deepcopy_obj, args);
- Py_DECREF(args);
- return result;
-}
-
-LOCAL(PyObject*)
list_join(PyObject* list)
{
/* join list elements (destroying the list in the process) */
@@ -420,10 +396,8 @@ element_init(PyObject *self, PyObject *args, PyObject *kwds)
Py_XDECREF(attrib);
/* Replace the objects already pointed to by tag, text and tail. */
- tmp = self_elem->tag;
Py_INCREF(tag);
- self_elem->tag = tag;
- Py_DECREF(tmp);
+ Py_XSETREF(self_elem->tag, tag);
tmp = self_elem->text;
Py_INCREF(Py_None);
@@ -748,6 +722,9 @@ _elementtree_Element___copy___impl(ElementObject *self)
return (PyObject*) element;
}
+/* Helper for a deep copy. */
+LOCAL(PyObject *) deepcopy(PyObject *, PyObject *);
+
/*[clinic input]
_elementtree.Element.__deepcopy__
@@ -838,6 +815,57 @@ _elementtree_Element___deepcopy__(ElementObject *self, PyObject *memo)
return NULL;
}
+LOCAL(PyObject *)
+deepcopy(PyObject *object, PyObject *memo)
+{
+ /* do a deep copy of the given object */
+ PyObject *args;
+ PyObject *result;
+ elementtreestate *st;
+
+ /* Fast paths */
+ if (object == Py_None || PyUnicode_CheckExact(object)) {
+ Py_INCREF(object);
+ return object;
+ }
+
+ if (Py_REFCNT(object) == 1) {
+ if (PyDict_CheckExact(object)) {
+ PyObject *key, *value;
+ Py_ssize_t pos = 0;
+ int simple = 1;
+ while (PyDict_Next(object, &pos, &key, &value)) {
+ if (!PyUnicode_CheckExact(key) || !PyUnicode_CheckExact(value)) {
+ simple = 0;
+ break;
+ }
+ }
+ if (simple)
+ return PyDict_Copy(object);
+ /* Fall through to general case */
+ }
+ else if (Element_CheckExact(object)) {
+ return _elementtree_Element___deepcopy__((ElementObject *)object, memo);
+ }
+ }
+
+ /* General case */
+ st = ET_STATE_GLOBAL;
+ if (!st->deepcopy_obj) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "deepcopy helper not found");
+ return NULL;
+ }
+
+ args = PyTuple_Pack(2, object, memo);
+ if (!args)
+ return NULL;
+ result = PyObject_CallObject(st->deepcopy_obj, args);
+ Py_DECREF(args);
+ return result;
+}
+
+
/*[clinic input]
_elementtree.Element.__sizeof__ -> Py_ssize_t
@@ -1892,92 +1920,90 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value)
}
static PyObject*
-element_getattro(ElementObject* self, PyObject* nameobj)
+element_tag_getter(ElementObject *self, void *closure)
{
- PyObject* res;
- char *name = "";
+ PyObject *res = self->tag;
+ Py_INCREF(res);
+ return res;
+}
- if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
+static PyObject*
+element_text_getter(ElementObject *self, void *closure)
+{
+ PyObject *res = element_get_text(self);
+ Py_XINCREF(res);
+ return res;
+}
- if (name == NULL)
- return NULL;
+static PyObject*
+element_tail_getter(ElementObject *self, void *closure)
+{
+ PyObject *res = element_get_tail(self);
+ Py_XINCREF(res);
+ return res;
+}
- /* handle common attributes first */
- if (strcmp(name, "tag") == 0) {
- res = self->tag;
- Py_INCREF(res);
- return res;
- } else if (strcmp(name, "text") == 0) {
- res = element_get_text(self);
- Py_XINCREF(res);
- return res;
+static PyObject*
+element_attrib_getter(ElementObject *self, void *closure)
+{
+ PyObject *res;
+ if (!self->extra) {
+ if (create_extra(self, NULL) < 0)
+ return NULL;
}
+ res = element_get_attrib(self);
+ Py_XINCREF(res);
+ return res;
+}
- /* methods */
- res = PyObject_GenericGetAttr((PyObject*) self, nameobj);
- if (res)
- return res;
-
- /* less common attributes */
- if (strcmp(name, "tail") == 0) {
- PyErr_Clear();
- res = element_get_tail(self);
- } else if (strcmp(name, "attrib") == 0) {
- PyErr_Clear();
- if (!self->extra) {
- if (create_extra(self, NULL) < 0)
- return NULL;
- }
- res = element_get_attrib(self);
+/* macro for setter validation */
+#define _VALIDATE_ATTR_VALUE(V) \
+ if ((V) == NULL) { \
+ PyErr_SetString( \
+ PyExc_AttributeError, \
+ "can't delete element attribute"); \
+ return -1; \
}
- if (!res)
- return NULL;
-
- Py_INCREF(res);
- return res;
+static int
+element_tag_setter(ElementObject *self, PyObject *value, void *closure)
+{
+ _VALIDATE_ATTR_VALUE(value);
+ Py_INCREF(value);
+ Py_SETREF(self->tag, value);
+ return 0;
}
static int
-element_setattro(ElementObject* self, PyObject* nameobj, PyObject* value)
+element_text_setter(ElementObject *self, PyObject *value, void *closure)
{
- char *name = "";
+ _VALIDATE_ATTR_VALUE(value);
+ Py_INCREF(value);
+ Py_DECREF(JOIN_OBJ(self->text));
+ self->text = value;
+ return 0;
+}
- if (value == NULL) {
- PyErr_SetString(PyExc_AttributeError,
- "can't delete attribute");
- return -1;
- }
- if (PyUnicode_Check(nameobj))
- name = _PyUnicode_AsString(nameobj);
- if (name == NULL)
- return -1;
+static int
+element_tail_setter(ElementObject *self, PyObject *value, void *closure)
+{
+ _VALIDATE_ATTR_VALUE(value);
+ Py_INCREF(value);
+ Py_DECREF(JOIN_OBJ(self->tail));
+ self->tail = value;
+ return 0;
+}
- if (strcmp(name, "tag") == 0) {
- Py_INCREF(value);
- Py_SETREF(self->tag, value);
- } else if (strcmp(name, "text") == 0) {
- Py_DECREF(JOIN_OBJ(self->text));
- self->text = value;
- Py_INCREF(self->text);
- } else if (strcmp(name, "tail") == 0) {
- Py_DECREF(JOIN_OBJ(self->tail));
- self->tail = value;
- Py_INCREF(self->tail);
- } else if (strcmp(name, "attrib") == 0) {
- if (!self->extra) {
- if (create_extra(self, NULL) < 0)
- return -1;
- }
- Py_INCREF(value);
- Py_SETREF(self->extra->attrib, value);
- } else {
- PyErr_SetString(PyExc_AttributeError,
- "Can't set arbitrary attributes on Element");
- return -1;
+static int
+element_attrib_setter(ElementObject *self, PyObject *value, void *closure)
+{
+ _VALIDATE_ATTR_VALUE(value);
+ if (!self->extra) {
+ if (create_extra(self, NULL) < 0)
+ return -1;
}
-
+ Py_INCREF(value);
+ Py_SETREF(self->extra->attrib, value);
return 0;
}
@@ -1997,22 +2023,22 @@ static PySequenceMethods element_as_sequence = {
* pre-order traversal. To keep track of which sub-element should be returned
* next, a stack of parents is maintained. This is a standard stack-based
* iterative pre-order traversal of a tree.
- * The stack is managed using a single-linked list starting at parent_stack.
- * Each stack node contains the saved parent to which we should return after
+ * The stack is managed using a continuous array.
+ * Each stack item contains the saved parent to which we should return after
* the current one is exhausted, and the next child to examine in that parent.
*/
typedef struct ParentLocator_t {
ElementObject *parent;
Py_ssize_t child_index;
- struct ParentLocator_t *next;
} ParentLocator;
typedef struct {
PyObject_HEAD
ParentLocator *parent_stack;
+ Py_ssize_t parent_stack_used;
+ Py_ssize_t parent_stack_size;
ElementObject *root_element;
PyObject *sought_tag;
- int root_done;
int gettext;
} ElementIterObject;
@@ -2020,13 +2046,11 @@ typedef struct {
static void
elementiter_dealloc(ElementIterObject *it)
{
- ParentLocator *p = it->parent_stack;
- while (p) {
- ParentLocator *temp = p;
- Py_XDECREF(p->parent);
- p = p->next;
- PyObject_Free(temp);
- }
+ Py_ssize_t i = it->parent_stack_used;
+ it->parent_stack_used = 0;
+ while (i--)
+ Py_XDECREF(it->parent_stack[i].parent);
+ PyMem_Free(it->parent_stack);
Py_XDECREF(it->sought_tag);
Py_XDECREF(it->root_element);
@@ -2038,11 +2062,9 @@ elementiter_dealloc(ElementIterObject *it)
static int
elementiter_traverse(ElementIterObject *it, visitproc visit, void *arg)
{
- ParentLocator *p = it->parent_stack;
- while (p) {
- Py_VISIT(p->parent);
- p = p->next;
- }
+ Py_ssize_t i = it->parent_stack_used;
+ while (i--)
+ Py_VISIT(it->parent_stack[i].parent);
Py_VISIT(it->root_element);
Py_VISIT(it->sought_tag);
@@ -2051,17 +2073,25 @@ elementiter_traverse(ElementIterObject *it, visitproc visit, void *arg)
/* Helper function for elementiter_next. Add a new parent to the parent stack.
*/
-static ParentLocator *
-parent_stack_push_new(ParentLocator *stack, ElementObject *parent)
+static int
+parent_stack_push_new(ElementIterObject *it, ElementObject *parent)
{
- ParentLocator *new_node = PyObject_Malloc(sizeof(ParentLocator));
- if (new_node) {
- new_node->parent = parent;
- Py_INCREF(parent);
- new_node->child_index = 0;
- new_node->next = stack;
+ ParentLocator *item;
+
+ if (it->parent_stack_used >= it->parent_stack_size) {
+ Py_ssize_t new_size = it->parent_stack_size * 2; /* never overflow */
+ ParentLocator *parent_stack = it->parent_stack;
+ PyMem_Resize(parent_stack, ParentLocator, new_size);
+ if (parent_stack == NULL)
+ return -1;
+ it->parent_stack = parent_stack;
+ it->parent_stack_size = new_size;
}
- return new_node;
+ item = it->parent_stack + it->parent_stack_used++;
+ Py_INCREF(parent);
+ item->parent = parent;
+ item->child_index = 0;
+ return 0;
}
static PyObject *
@@ -2078,151 +2108,91 @@ elementiter_next(ElementIterObject *it)
* - itertext() also has to handle tail, after finishing with all the
* children of a node.
*/
- ElementObject *cur_parent;
- Py_ssize_t child_index;
int rc;
ElementObject *elem;
+ PyObject *text;
while (1) {
/* Handle the case reached in the beginning and end of iteration, where
- * the parent stack is empty. The root_done flag gives us indication
- * whether we've just started iterating (so root_done is 0), in which
- * case the root is returned. If root_done is 1 and we're here, the
+ * the parent stack is empty. If root_element is NULL and we're here, the
* iterator is exhausted.
*/
- if (!it->parent_stack->parent) {
- if (it->root_done) {
+ if (!it->parent_stack_used) {
+ if (!it->root_element) {
PyErr_SetNone(PyExc_StopIteration);
return NULL;
- } else {
- elem = it->root_element;
- it->parent_stack = parent_stack_push_new(it->parent_stack,
- elem);
- if (!it->parent_stack) {
- PyErr_NoMemory();
- return NULL;
- }
+ }
- Py_INCREF(elem);
- it->root_done = 1;
- rc = (it->sought_tag == Py_None);
- if (!rc) {
- rc = PyObject_RichCompareBool(elem->tag,
- it->sought_tag, Py_EQ);
- if (rc < 0) {
- Py_DECREF(elem);
- return NULL;
- }
- }
- if (rc) {
- if (it->gettext) {
- PyObject *text = element_get_text(elem);
- if (!text) {
- Py_DECREF(elem);
- return NULL;
- }
- Py_INCREF(text);
- Py_DECREF(elem);
- rc = PyObject_IsTrue(text);
- if (rc > 0)
- return text;
- Py_DECREF(text);
- if (rc < 0)
- return NULL;
- } else {
- return (PyObject *)elem;
- }
- }
- else {
- Py_DECREF(elem);
+ elem = it->root_element; /* steals a reference */
+ it->root_element = NULL;
+ }
+ else {
+ /* See if there are children left to traverse in the current parent. If
+ * yes, visit the next child. If not, pop the stack and try again.
+ */
+ ParentLocator *item = &it->parent_stack[it->parent_stack_used - 1];
+ Py_ssize_t child_index = item->child_index;
+ ElementObjectExtra *extra;
+ elem = item->parent;
+ extra = elem->extra;
+ if (!extra || child_index >= extra->length) {
+ it->parent_stack_used--;
+ /* Note that extra condition on it->parent_stack_used here;
+ * this is because itertext() is supposed to only return *inner*
+ * text, not text following the element it began iteration with.
+ */
+ if (it->gettext && it->parent_stack_used) {
+ text = element_get_tail(elem);
+ goto gettext;
}
+ Py_DECREF(elem);
+ continue;
}
+
+ elem = (ElementObject *)extra->children[child_index];
+ item->child_index++;
+ Py_INCREF(elem);
}
- /* See if there are children left to traverse in the current parent. If
- * yes, visit the next child. If not, pop the stack and try again.
- */
- cur_parent = it->parent_stack->parent;
- child_index = it->parent_stack->child_index;
- if (cur_parent->extra && child_index < cur_parent->extra->length) {
- elem = (ElementObject *)cur_parent->extra->children[child_index];
- it->parent_stack->child_index++;
- it->parent_stack = parent_stack_push_new(it->parent_stack,
- elem);
- if (!it->parent_stack) {
- PyErr_NoMemory();
- return NULL;
- }
+ if (parent_stack_push_new(it, elem) < 0) {
+ Py_DECREF(elem);
+ PyErr_NoMemory();
+ return NULL;
+ }
+ if (it->gettext) {
+ text = element_get_text(elem);
+ goto gettext;
+ }
- Py_INCREF(elem);
- if (it->gettext) {
- PyObject *text = element_get_text(elem);
- if (!text) {
- Py_DECREF(elem);
- return NULL;
- }
- Py_INCREF(text);
- Py_DECREF(elem);
- rc = PyObject_IsTrue(text);
- if (rc > 0)
- return text;
- Py_DECREF(text);
- if (rc < 0)
- return NULL;
- } else {
- rc = (it->sought_tag == Py_None);
- if (!rc) {
- rc = PyObject_RichCompareBool(elem->tag,
- it->sought_tag, Py_EQ);
- if (rc < 0) {
- Py_DECREF(elem);
- return NULL;
- }
- }
- if (rc) {
- return (PyObject *)elem;
- }
- Py_DECREF(elem);
- }
+ if (it->sought_tag == Py_None)
+ return (PyObject *)elem;
+
+ rc = PyObject_RichCompareBool(elem->tag, it->sought_tag, Py_EQ);
+ if (rc > 0)
+ return (PyObject *)elem;
+
+ Py_DECREF(elem);
+ if (rc < 0)
+ return NULL;
+ continue;
+
+gettext:
+ if (!text) {
+ Py_DECREF(elem);
+ return NULL;
+ }
+ if (text == Py_None) {
+ Py_DECREF(elem);
}
else {
- PyObject *tail;
- ParentLocator *next;
- if (it->gettext) {
- Py_INCREF(cur_parent);
- tail = element_get_tail(cur_parent);
- if (!tail) {
- Py_DECREF(cur_parent);
- return NULL;
- }
- Py_INCREF(tail);
- Py_DECREF(cur_parent);
- }
- else {
- tail = Py_None;
- Py_INCREF(tail);
- }
- next = it->parent_stack->next;
- cur_parent = it->parent_stack->parent;
- PyObject_Free(it->parent_stack);
- it->parent_stack = next;
- Py_XDECREF(cur_parent);
-
- /* Note that extra condition on it->parent_stack->parent here;
- * this is because itertext() is supposed to only return *inner*
- * text, not text following the element it began iteration with.
- */
- if (it->parent_stack->parent) {
- rc = PyObject_IsTrue(tail);
- if (rc > 0)
- return tail;
- Py_DECREF(tail);
- if (rc < 0)
- return NULL;
- }
- else {
- Py_DECREF(tail);
- }
+ Py_INCREF(text);
+ Py_DECREF(elem);
+ rc = PyObject_IsTrue(text);
+ if (rc > 0)
+ return text;
+ Py_DECREF(text);
+ if (rc < 0)
+ return NULL;
}
}
@@ -2274,6 +2244,7 @@ static PyTypeObject ElementIter_Type = {
0, /* tp_new */
};
+#define INIT_PARENT_STACK_SIZE 8
static PyObject *
create_elementiter(ElementObject *self, PyObject *tag, int gettext)
@@ -2286,22 +2257,20 @@ create_elementiter(ElementObject *self, PyObject *tag, int gettext)
Py_INCREF(tag);
it->sought_tag = tag;
- it->root_done = 0;
it->gettext = gettext;
Py_INCREF(self);
it->root_element = self;
PyObject_GC_Track(it);
- it->parent_stack = PyObject_Malloc(sizeof(ParentLocator));
+ it->parent_stack = PyMem_New(ParentLocator, INIT_PARENT_STACK_SIZE);
if (it->parent_stack == NULL) {
Py_DECREF(it);
PyErr_NoMemory();
return NULL;
}
- it->parent_stack->parent = NULL;
- it->parent_stack->child_index = 0;
- it->parent_stack->next = NULL;
+ it->parent_stack_used = 0;
+ it->parent_stack_size = INIT_PARENT_STACK_SIZE;
return (PyObject *)it;
}
@@ -2326,7 +2295,7 @@ typedef struct {
PyObject *element_factory;
/* element tracing */
- PyObject *events; /* list of events, or NULL if not collecting */
+ PyObject *events_append; /* the append method of the list of events, or NULL */
PyObject *start_event_obj; /* event objects (NULL to ignore) */
PyObject *end_event_obj;
PyObject *start_ns_event_obj;
@@ -2361,7 +2330,7 @@ treebuilder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
t->index = 0;
- t->events = NULL;
+ t->events_append = NULL;
t->start_event_obj = t->end_event_obj = NULL;
t->start_ns_event_obj = t->end_ns_event_obj = NULL;
}
@@ -2380,13 +2349,9 @@ _elementtree_TreeBuilder___init___impl(TreeBuilderObject *self,
PyObject *element_factory)
/*[clinic end generated code: output=91cfa7558970ee96 input=1b424eeefc35249c]*/
{
- PyObject *tmp;
-
if (element_factory) {
Py_INCREF(element_factory);
- tmp = self->element_factory;
- self->element_factory = element_factory;
- Py_XDECREF(tmp);
+ Py_XSETREF(self->element_factory, element_factory);
}
return 0;
@@ -2411,7 +2376,7 @@ treebuilder_gc_clear(TreeBuilderObject *self)
Py_CLEAR(self->start_ns_event_obj);
Py_CLEAR(self->end_event_obj);
Py_CLEAR(self->start_event_obj);
- Py_CLEAR(self->events);
+ Py_CLEAR(self->events_append);
Py_CLEAR(self->stack);
Py_CLEAR(self->data);
Py_CLEAR(self->last);
@@ -2492,13 +2457,14 @@ treebuilder_append_event(TreeBuilderObject *self, PyObject *action,
PyObject *node)
{
if (action != NULL) {
- PyObject *res = PyTuple_Pack(2, action, node);
- if (res == NULL)
+ PyObject *res;
+ PyObject *event = PyTuple_Pack(2, action, node);
+ if (event == NULL)
return -1;
- if (PyList_Append(self->events, res) < 0) {
- Py_DECREF(res);
+ res = PyObject_CallFunctionObjArgs(self->events_append, event, NULL);
+ Py_DECREF(event);
+ if (res == NULL)
return -1;
- }
Py_DECREF(res);
}
return 0;
@@ -2527,10 +2493,17 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
self->data = NULL;
}
- if (self->element_factory && self->element_factory != Py_None) {
- node = PyObject_CallFunction(self->element_factory, "OO", tag, attrib);
- } else {
+ if (!self->element_factory || self->element_factory == Py_None) {
node = create_new_element(tag, attrib);
+ } else if (attrib == Py_None) {
+ attrib = PyDict_New();
+ if (!attrib)
+ return NULL;
+ node = PyObject_CallFunction(self->element_factory, "OO", tag, attrib);
+ Py_DECREF(attrib);
+ }
+ else {
+ node = PyObject_CallFunction(self->element_factory, "OO", tag, attrib);
}
if (!node) {
return NULL;
@@ -2989,12 +2962,8 @@ expat_start_handler(XMLParserObject* self, const XML_Char* tag_in,
attrib_in += 2;
}
} else {
- /* Pass an empty dictionary on */
- attrib = PyDict_New();
- if (!attrib) {
- Py_DECREF(tag);
- return;
- }
+ Py_INCREF(Py_None);
+ attrib = Py_None;
}
if (TreeBuilder_CheckExact(self->target)) {
@@ -3003,6 +2972,14 @@ expat_start_handler(XMLParserObject* self, const XML_Char* tag_in,
tag, attrib);
}
else if (self->handle_start) {
+ if (attrib == Py_None) {
+ Py_DECREF(attrib);
+ attrib = PyDict_New();
+ if (!attrib) {
+ Py_DECREF(tag);
+ return;
+ }
+ }
res = PyObject_CallFunction(self->handle_start, "OO", tag, attrib);
} else
res = NULL;
@@ -3076,7 +3053,7 @@ expat_start_ns_handler(XMLParserObject* self, const XML_Char* prefix,
if (PyErr_Occurred())
return;
- if (!target->events || !target->start_ns_event_obj)
+ if (!target->events_append || !target->start_ns_event_obj)
return;
if (!uri)
@@ -3099,7 +3076,7 @@ expat_end_ns_handler(XMLParserObject* self, const XML_Char* prefix_in)
if (PyErr_Occurred())
return;
- if (!target->events)
+ if (!target->events_append)
return;
treebuilder_append_event(target, target->end_ns_event_obj, Py_None);
@@ -3588,7 +3565,7 @@ _elementtree_XMLParser_doctype_impl(XMLParserObject *self, PyObject *name,
/*[clinic input]
_elementtree.XMLParser._setevents
- events_queue: object(subclass_of='&PyList_Type')
+ events_queue: object
events_to_report: object = None
/
@@ -3598,12 +3575,12 @@ static PyObject *
_elementtree_XMLParser__setevents_impl(XMLParserObject *self,
PyObject *events_queue,
PyObject *events_to_report)
-/*[clinic end generated code: output=1440092922b13ed1 input=59db9742910c6174]*/
+/*[clinic end generated code: output=1440092922b13ed1 input=abf90830a1c3b0fc]*/
{
/* activate element event reporting */
Py_ssize_t i;
TreeBuilderObject *target;
- PyObject *events_seq;
+ PyObject *events_append, *events_seq;
if (!TreeBuilder_CheckExact(self->target)) {
PyErr_SetString(
@@ -3616,8 +3593,10 @@ _elementtree_XMLParser__setevents_impl(XMLParserObject *self,
target = (TreeBuilderObject*) self->target;
- Py_INCREF(events_queue);
- Py_XSETREF(target->events, events_queue);
+ events_append = PyObject_GetAttrString(events_queue, "append");
+ if (events_append == NULL)
+ return NULL;
+ Py_XSETREF(target->events_append, events_append);
/* clear out existing events */
Py_CLEAR(target->start_event_obj);
@@ -3750,6 +3729,26 @@ static PyMappingMethods element_as_mapping = {
(objobjargproc) element_ass_subscr,
};
+static PyGetSetDef element_getsetlist[] = {
+ {"tag",
+ (getter)element_tag_getter,
+ (setter)element_tag_setter,
+ "A string identifying what kind of data this element represents"},
+ {"text",
+ (getter)element_text_getter,
+ (setter)element_text_setter,
+ "A string of text directly after the start tag, or None"},
+ {"tail",
+ (getter)element_tail_getter,
+ (setter)element_tail_setter,
+ "A string of text directly after the end tag, or None"},
+ {"attrib",
+ (getter)element_attrib_getter,
+ (setter)element_attrib_setter,
+ "A dictionary containing the element's attributes"},
+ {NULL},
+};
+
static PyTypeObject Element_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"xml.etree.ElementTree.Element", sizeof(ElementObject), 0,
@@ -3766,8 +3765,8 @@ static PyTypeObject Element_Type = {
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
- (getattrofunc)element_getattro, /* tp_getattro */
- (setattrofunc)element_setattro, /* tp_setattro */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
/* tp_flags */
@@ -3780,7 +3779,7 @@ static PyTypeObject Element_Type = {
0, /* tp_iternext */
element_methods, /* tp_methods */
0, /* tp_members */
- 0, /* tp_getset */
+ element_getsetlist, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c
index b840bf5005..daae71dc8d 100644
--- a/Modules/_gdbmmodule.c
+++ b/Modules/_gdbmmodule.c
@@ -614,7 +614,7 @@ dbmopen_impl(PyObject *module, const char *name, const char *flags, int mode)
return newdbmobject(name, iflags, mode);
}
-static char dbmmodule_open_flags[] = "rwcn"
+static const char dbmmodule_open_flags[] = "rwcn"
#ifdef GDBM_FAST
"f"
#endif
diff --git a/Modules/_heapqmodule.c b/Modules/_heapqmodule.c
index 136abf5985..b499e1f668 100644
--- a/Modules/_heapqmodule.c
+++ b/Modules/_heapqmodule.c
@@ -66,7 +66,7 @@ siftup(PyListObject *heap, Py_ssize_t pos)
/* Bubble up the smaller child until hitting a leaf. */
arr = _PyList_ITEMS(heap);
- limit = endpos / 2; /* smallest pos that has no child */
+ limit = endpos >> 1; /* smallest pos that has no child */
while (pos < limit) {
/* Set childpos to index of smaller child. */
childpos = 2*pos + 1; /* leftmost child position */
@@ -78,6 +78,7 @@ siftup(PyListObject *heap, Py_ssize_t pos)
if (cmp < 0)
return -1;
childpos += ((unsigned)cmp ^ 1); /* increment when cmp==0 */
+ arr = _PyList_ITEMS(heap); /* arr may have changed */
if (endpos != PyList_GET_SIZE(heap)) {
PyErr_SetString(PyExc_RuntimeError,
"list changed size during iteration");
@@ -85,7 +86,6 @@ siftup(PyListObject *heap, Py_ssize_t pos)
}
}
/* Move the smaller child up. */
- arr = _PyList_ITEMS(heap);
tmp1 = arr[childpos];
tmp2 = arr[pos];
arr[childpos] = tmp2;
@@ -347,7 +347,7 @@ heapify_internal(PyObject *heap, int siftup_func(PyListObject *, Py_ssize_t))
n is odd = 2*j+1, this is (2*j+1-1)/2 = j so j-1 is the largest,
and that's again n//2-1.
*/
- for (i = n/2 - 1 ; i >= 0 ; i--)
+ for (i = (n >> 1) - 1 ; i >= 0 ; i--)
if (siftup_func((PyListObject *)heap, i))
return NULL;
Py_RETURN_NONE;
@@ -420,7 +420,7 @@ siftup_max(PyListObject *heap, Py_ssize_t pos)
/* Bubble up the smaller child until hitting a leaf. */
arr = _PyList_ITEMS(heap);
- limit = endpos / 2; /* smallest pos that has no child */
+ limit = endpos >> 1; /* smallest pos that has no child */
while (pos < limit) {
/* Set childpos to index of smaller child. */
childpos = 2*pos + 1; /* leftmost child position */
@@ -432,6 +432,7 @@ siftup_max(PyListObject *heap, Py_ssize_t pos)
if (cmp < 0)
return -1;
childpos += ((unsigned)cmp ^ 1); /* increment when cmp==0 */
+ arr = _PyList_ITEMS(heap); /* arr may have changed */
if (endpos != PyList_GET_SIZE(heap)) {
PyErr_SetString(PyExc_RuntimeError,
"list changed size during iteration");
@@ -439,7 +440,6 @@ siftup_max(PyListObject *heap, Py_ssize_t pos)
}
}
/* Move the smaller child up. */
- arr = _PyList_ITEMS(heap);
tmp1 = arr[childpos];
tmp2 = arr[pos];
arr[childpos] = tmp2;
diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c
index 65c955ac69..ec700f64c8 100644
--- a/Modules/_io/_iomodule.c
+++ b/Modules/_io/_iomodule.c
@@ -238,20 +238,33 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
int text = 0, binary = 0, universal = 0;
char rawmode[6], *m;
- int line_buffering, isatty;
+ int line_buffering, is_number;
+ long isatty;
- PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL;
+ PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL, *path_or_fd = NULL;
_Py_IDENTIFIER(_blksize);
_Py_IDENTIFIER(isatty);
_Py_IDENTIFIER(mode);
_Py_IDENTIFIER(close);
- if (!PyUnicode_Check(file) &&
- !PyBytes_Check(file) &&
- !PyNumber_Check(file)) {
+ is_number = PyNumber_Check(file);
+
+ if (is_number) {
+ path_or_fd = file;
+ Py_INCREF(path_or_fd);
+ } else {
+ path_or_fd = PyOS_FSPath(file);
+ if (path_or_fd == NULL) {
+ return NULL;
+ }
+ }
+
+ if (!is_number &&
+ !PyUnicode_Check(path_or_fd) &&
+ !PyBytes_Check(path_or_fd)) {
PyErr_Format(PyExc_TypeError, "invalid file: %R", file);
- return NULL;
+ goto error;
}
/* Decode mode */
@@ -292,7 +305,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
if (strchr(mode+i+1, c)) {
invalid_mode:
PyErr_Format(PyExc_ValueError, "invalid mode: '%s'", mode);
- return NULL;
+ goto error;
}
}
@@ -307,54 +320,57 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
/* Parameters validation */
if (universal) {
- if (writing || appending) {
+ if (creating || writing || appending || updating) {
PyErr_SetString(PyExc_ValueError,
- "can't use U and writing mode at once");
- return NULL;
+ "mode U cannot be combined with x', 'w', 'a', or '+'");
+ goto error;
}
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"'U' mode is deprecated", 1) < 0)
- return NULL;
+ goto error;
reading = 1;
}
if (text && binary) {
PyErr_SetString(PyExc_ValueError,
"can't have text and binary mode at once");
- return NULL;
+ goto error;
}
if (creating + reading + writing + appending > 1) {
PyErr_SetString(PyExc_ValueError,
"must have exactly one of create/read/write/append mode");
- return NULL;
+ goto error;
}
if (binary && encoding != NULL) {
PyErr_SetString(PyExc_ValueError,
"binary mode doesn't take an encoding argument");
- return NULL;
+ goto error;
}
if (binary && errors != NULL) {
PyErr_SetString(PyExc_ValueError,
"binary mode doesn't take an errors argument");
- return NULL;
+ goto error;
}
if (binary && newline != NULL) {
PyErr_SetString(PyExc_ValueError,
"binary mode doesn't take a newline argument");
- return NULL;
+ goto error;
}
/* Create the Raw file stream */
raw = PyObject_CallFunction((PyObject *)&PyFileIO_Type,
- "OsiO", file, rawmode, closefd, opener);
+ "OsiO", path_or_fd, rawmode, closefd, opener);
if (raw == NULL)
- return NULL;
+ goto error;
result = raw;
+ Py_DECREF(path_or_fd);
+ path_or_fd = NULL;
+
modeobj = PyUnicode_FromString(mode);
if (modeobj == NULL)
goto error;
@@ -437,10 +453,10 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
/* wraps into a TextIOWrapper */
wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type,
- "Osssi",
- buffer,
- encoding, errors, newline,
- line_buffering);
+ "Osssi",
+ buffer,
+ encoding, errors, newline,
+ line_buffering);
if (wrapper == NULL)
goto error;
result = wrapper;
@@ -460,6 +476,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
Py_XDECREF(close_result);
Py_DECREF(result);
}
+ Py_XDECREF(path_or_fd);
Py_XDECREF(modeobj);
return NULL;
}
diff --git a/Modules/_io/_iomodule.h b/Modules/_io/_iomodule.h
index 0c6eae26b7..3c48ff3aac 100644
--- a/Modules/_io/_iomodule.h
+++ b/Modules/_io/_iomodule.h
@@ -60,7 +60,7 @@ extern PyObject *_PyIncrementalNewlineDecoder_decode(
* Otherwise, the line ending is specified by readnl, a str object */
extern Py_ssize_t _PyIO_find_line_ending(
int translated, int universal, PyObject *readnl,
- int kind, char *start, char *end, Py_ssize_t *consumed);
+ int kind, const char *start, const char *end, Py_ssize_t *consumed);
/* Return 1 if an EnvironmentError with errno == EINTR is set (and then
clears the error indicator), 0 otherwise.
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index 6d67751c7d..cbe7425eae 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -659,7 +659,7 @@ _bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len);
/* Sets the current error to BlockingIOError */
static void
-_set_BlockingIOError(char *msg, Py_ssize_t written)
+_set_BlockingIOError(const char *msg, Py_ssize_t written)
{
PyObject *err;
PyErr_Clear();
diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c
index 9e5d78b166..a1ba121e26 100644
--- a/Modules/_io/bytesio.c
+++ b/Modules/_io/bytesio.c
@@ -87,7 +87,7 @@ scan_eol(bytesio *self, Py_ssize_t len)
static int
unshare_buffer(bytesio *self, size_t size)
{
- PyObject *new_buf, *old_buf;
+ PyObject *new_buf;
assert(SHARED_BUF(self));
assert(self->exports == 0);
assert(size >= (size_t)self->string_size);
@@ -96,9 +96,7 @@ unshare_buffer(bytesio *self, size_t size)
return -1;
memcpy(PyBytes_AS_STRING(new_buf), PyBytes_AS_STRING(self->buf),
self->string_size);
- old_buf = self->buf;
- self->buf = new_buf;
- Py_DECREF(old_buf);
+ Py_SETREF(self->buf, new_buf);
return 0;
}
diff --git a/Modules/_io/clinic/_iomodule.c.h b/Modules/_io/clinic/_iomodule.c.h
index 6c88e32e5a..ee01cfb7b3 100644
--- a/Modules/_io/clinic/_iomodule.c.h
+++ b/Modules/_io/clinic/_iomodule.c.h
@@ -149,11 +149,12 @@ _io_open(PyObject *module, PyObject *args, PyObject *kwargs)
PyObject *opener = Py_None;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|sizzziO:open", _keywords,
- &file, &mode, &buffering, &encoding, &errors, &newline, &closefd, &opener))
+ &file, &mode, &buffering, &encoding, &errors, &newline, &closefd, &opener)) {
goto exit;
+ }
return_value = _io_open_impl(module, file, mode, buffering, encoding, errors, newline, closefd, opener);
exit:
return return_value;
}
-/*[clinic end generated code: output=bc2c003cb7daeafe input=a9049054013a1b77]*/
+/*[clinic end generated code: output=ae2facf262cf464e input=a9049054013a1b77]*/
diff --git a/Modules/_io/clinic/bufferedio.c.h b/Modules/_io/clinic/bufferedio.c.h
index 437e275730..c4dfc1640e 100644
--- a/Modules/_io/clinic/bufferedio.c.h
+++ b/Modules/_io/clinic/bufferedio.c.h
@@ -19,14 +19,16 @@ _io__BufferedIOBase_readinto(PyObject *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};
- if (!PyArg_Parse(arg, "w*:readinto", &buffer))
+ if (!PyArg_Parse(arg, "w*:readinto", &buffer)) {
goto exit;
+ }
return_value = _io__BufferedIOBase_readinto_impl(self, &buffer);
exit:
/* Cleanup for buffer */
- if (buffer.obj)
+ if (buffer.obj) {
PyBuffer_Release(&buffer);
+ }
return return_value;
}
@@ -48,14 +50,16 @@ _io__BufferedIOBase_readinto1(PyObject *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};
- if (!PyArg_Parse(arg, "w*:readinto1", &buffer))
+ if (!PyArg_Parse(arg, "w*:readinto1", &buffer)) {
goto exit;
+ }
return_value = _io__BufferedIOBase_readinto1_impl(self, &buffer);
exit:
/* Cleanup for buffer */
- if (buffer.obj)
+ if (buffer.obj) {
PyBuffer_Release(&buffer);
+ }
return return_value;
}
@@ -99,8 +103,9 @@ _io__Buffered_peek(buffered *self, PyObject *args)
Py_ssize_t size = 0;
if (!PyArg_ParseTuple(args, "|n:peek",
- &size))
+ &size)) {
goto exit;
+ }
return_value = _io__Buffered_peek_impl(self, size);
exit:
@@ -125,8 +130,9 @@ _io__Buffered_read(buffered *self, PyObject *args)
Py_ssize_t n = -1;
if (!PyArg_ParseTuple(args, "|O&:read",
- _PyIO_ConvertSsize_t, &n))
+ _PyIO_ConvertSsize_t, &n)) {
goto exit;
+ }
return_value = _io__Buffered_read_impl(self, n);
exit:
@@ -150,8 +156,9 @@ _io__Buffered_read1(buffered *self, PyObject *arg)
PyObject *return_value = NULL;
Py_ssize_t n;
- if (!PyArg_Parse(arg, "n:read1", &n))
+ if (!PyArg_Parse(arg, "n:read1", &n)) {
goto exit;
+ }
return_value = _io__Buffered_read1_impl(self, n);
exit:
@@ -175,14 +182,16 @@ _io__Buffered_readinto(buffered *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};
- if (!PyArg_Parse(arg, "w*:readinto", &buffer))
+ if (!PyArg_Parse(arg, "w*:readinto", &buffer)) {
goto exit;
+ }
return_value = _io__Buffered_readinto_impl(self, &buffer);
exit:
/* Cleanup for buffer */
- if (buffer.obj)
+ if (buffer.obj) {
PyBuffer_Release(&buffer);
+ }
return return_value;
}
@@ -204,14 +213,16 @@ _io__Buffered_readinto1(buffered *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};
- if (!PyArg_Parse(arg, "w*:readinto1", &buffer))
+ if (!PyArg_Parse(arg, "w*:readinto1", &buffer)) {
goto exit;
+ }
return_value = _io__Buffered_readinto1_impl(self, &buffer);
exit:
/* Cleanup for buffer */
- if (buffer.obj)
+ if (buffer.obj) {
PyBuffer_Release(&buffer);
+ }
return return_value;
}
@@ -234,8 +245,9 @@ _io__Buffered_readline(buffered *self, PyObject *args)
Py_ssize_t size = -1;
if (!PyArg_ParseTuple(args, "|O&:readline",
- _PyIO_ConvertSsize_t, &size))
+ _PyIO_ConvertSsize_t, &size)) {
goto exit;
+ }
return_value = _io__Buffered_readline_impl(self, size);
exit:
@@ -261,8 +273,9 @@ _io__Buffered_seek(buffered *self, PyObject *args)
int whence = 0;
if (!PyArg_ParseTuple(args, "O|i:seek",
- &targetobj, &whence))
+ &targetobj, &whence)) {
goto exit;
+ }
return_value = _io__Buffered_seek_impl(self, targetobj, whence);
exit:
@@ -288,8 +301,9 @@ _io__Buffered_truncate(buffered *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "truncate",
0, 1,
- &pos))
+ &pos)) {
goto exit;
+ }
return_value = _io__Buffered_truncate_impl(self, pos);
exit:
@@ -315,8 +329,9 @@ _io_BufferedReader___init__(PyObject *self, PyObject *args, PyObject *kwargs)
Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|n:BufferedReader", _keywords,
- &raw, &buffer_size))
+ &raw, &buffer_size)) {
goto exit;
+ }
return_value = _io_BufferedReader___init___impl((buffered *)self, raw, buffer_size);
exit:
@@ -346,8 +361,9 @@ _io_BufferedWriter___init__(PyObject *self, PyObject *args, PyObject *kwargs)
Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|n:BufferedWriter", _keywords,
- &raw, &buffer_size))
+ &raw, &buffer_size)) {
goto exit;
+ }
return_value = _io_BufferedWriter___init___impl((buffered *)self, raw, buffer_size);
exit:
@@ -371,14 +387,16 @@ _io_BufferedWriter_write(buffered *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};
- if (!PyArg_Parse(arg, "y*:write", &buffer))
+ if (!PyArg_Parse(arg, "y*:write", &buffer)) {
goto exit;
+ }
return_value = _io_BufferedWriter_write_impl(self, &buffer);
exit:
/* Cleanup for buffer */
- if (buffer.obj)
+ if (buffer.obj) {
PyBuffer_Release(&buffer);
+ }
return return_value;
}
@@ -410,11 +428,13 @@ _io_BufferedRWPair___init__(PyObject *self, PyObject *args, PyObject *kwargs)
Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
if ((Py_TYPE(self) == &PyBufferedRWPair_Type) &&
- !_PyArg_NoKeywords("BufferedRWPair", kwargs))
+ !_PyArg_NoKeywords("BufferedRWPair", kwargs)) {
goto exit;
+ }
if (!PyArg_ParseTuple(args, "OO|n:BufferedRWPair",
- &reader, &writer, &buffer_size))
+ &reader, &writer, &buffer_size)) {
goto exit;
+ }
return_value = _io_BufferedRWPair___init___impl((rwpair *)self, reader, writer, buffer_size);
exit:
@@ -444,11 +464,12 @@ _io_BufferedRandom___init__(PyObject *self, PyObject *args, PyObject *kwargs)
Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|n:BufferedRandom", _keywords,
- &raw, &buffer_size))
+ &raw, &buffer_size)) {
goto exit;
+ }
return_value = _io_BufferedRandom___init___impl((buffered *)self, raw, buffer_size);
exit:
return return_value;
}
-/*[clinic end generated code: output=2bbb5e239b4ffe6f input=a9049054013a1b77]*/
+/*[clinic end generated code: output=4f6196c756b880c8 input=a9049054013a1b77]*/
diff --git a/Modules/_io/clinic/bytesio.c.h b/Modules/_io/clinic/bytesio.c.h
index 5f2abb03a7..1f736fed80 100644
--- a/Modules/_io/clinic/bytesio.c.h
+++ b/Modules/_io/clinic/bytesio.c.h
@@ -171,8 +171,9 @@ _io_BytesIO_read(bytesio *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "read",
0, 1,
- &arg))
+ &arg)) {
goto exit;
+ }
return_value = _io_BytesIO_read_impl(self, arg);
exit:
@@ -215,8 +216,9 @@ _io_BytesIO_readline(bytesio *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "readline",
0, 1,
- &arg))
+ &arg)) {
goto exit;
+ }
return_value = _io_BytesIO_readline_impl(self, arg);
exit:
@@ -247,8 +249,9 @@ _io_BytesIO_readlines(bytesio *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "readlines",
0, 1,
- &arg))
+ &arg)) {
goto exit;
+ }
return_value = _io_BytesIO_readlines_impl(self, arg);
exit:
@@ -276,14 +279,16 @@ _io_BytesIO_readinto(bytesio *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};
- if (!PyArg_Parse(arg, "w*:readinto", &buffer))
+ if (!PyArg_Parse(arg, "w*:readinto", &buffer)) {
goto exit;
+ }
return_value = _io_BytesIO_readinto_impl(self, &buffer);
exit:
/* Cleanup for buffer */
- if (buffer.obj)
+ if (buffer.obj) {
PyBuffer_Release(&buffer);
+ }
return return_value;
}
@@ -311,8 +316,9 @@ _io_BytesIO_truncate(bytesio *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "truncate",
0, 1,
- &arg))
+ &arg)) {
goto exit;
+ }
return_value = _io_BytesIO_truncate_impl(self, arg);
exit:
@@ -345,8 +351,9 @@ _io_BytesIO_seek(bytesio *self, PyObject *args)
int whence = 0;
if (!PyArg_ParseTuple(args, "n|i:seek",
- &pos, &whence))
+ &pos, &whence)) {
goto exit;
+ }
return_value = _io_BytesIO_seek_impl(self, pos, whence);
exit:
@@ -412,11 +419,12 @@ _io_BytesIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
PyObject *initvalue = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:BytesIO", _keywords,
- &initvalue))
+ &initvalue)) {
goto exit;
+ }
return_value = _io_BytesIO___init___impl((bytesio *)self, initvalue);
exit:
return return_value;
}
-/*[clinic end generated code: output=60ce2c6272718431 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=3fdb62f3e3b0544d input=a9049054013a1b77]*/
diff --git a/Modules/_io/clinic/fileio.c.h b/Modules/_io/clinic/fileio.c.h
index 10420082ac..038836a2c1 100644
--- a/Modules/_io/clinic/fileio.c.h
+++ b/Modules/_io/clinic/fileio.c.h
@@ -56,8 +56,9 @@ _io_FileIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
PyObject *opener = Py_None;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|siO:FileIO", _keywords,
- &nameobj, &mode, &closefd, &opener))
+ &nameobj, &mode, &closefd, &opener)) {
goto exit;
+ }
return_value = _io_FileIO___init___impl((fileio *)self, nameobj, mode, closefd, opener);
exit:
@@ -154,14 +155,16 @@ _io_FileIO_readinto(fileio *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};
- if (!PyArg_Parse(arg, "w*:readinto", &buffer))
+ if (!PyArg_Parse(arg, "w*:readinto", &buffer)) {
goto exit;
+ }
return_value = _io_FileIO_readinto_impl(self, &buffer);
exit:
/* Cleanup for buffer */
- if (buffer.obj)
+ if (buffer.obj) {
PyBuffer_Release(&buffer);
+ }
return return_value;
}
@@ -210,8 +213,9 @@ _io_FileIO_read(fileio *self, PyObject *args)
Py_ssize_t size = -1;
if (!PyArg_ParseTuple(args, "|O&:read",
- _PyIO_ConvertSsize_t, &size))
+ _PyIO_ConvertSsize_t, &size)) {
goto exit;
+ }
return_value = _io_FileIO_read_impl(self, size);
exit:
@@ -240,14 +244,16 @@ _io_FileIO_write(fileio *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer b = {NULL, NULL};
- if (!PyArg_Parse(arg, "y*:write", &b))
+ if (!PyArg_Parse(arg, "y*:write", &b)) {
goto exit;
+ }
return_value = _io_FileIO_write_impl(self, &b);
exit:
/* Cleanup for b */
- if (b.obj)
+ if (b.obj) {
PyBuffer_Release(&b);
+ }
return return_value;
}
@@ -280,8 +286,9 @@ _io_FileIO_seek(fileio *self, PyObject *args)
int whence = 0;
if (!PyArg_ParseTuple(args, "O|i:seek",
- &pos, &whence))
+ &pos, &whence)) {
goto exit;
+ }
return_value = _io_FileIO_seek_impl(self, pos, whence);
exit:
@@ -333,8 +340,9 @@ _io_FileIO_truncate(fileio *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "truncate",
0, 1,
- &posobj))
+ &posobj)) {
goto exit;
+ }
return_value = _io_FileIO_truncate_impl(self, posobj);
exit:
@@ -364,4 +372,4 @@ _io_FileIO_isatty(fileio *self, PyObject *Py_UNUSED(ignored))
#ifndef _IO_FILEIO_TRUNCATE_METHODDEF
#define _IO_FILEIO_TRUNCATE_METHODDEF
#endif /* !defined(_IO_FILEIO_TRUNCATE_METHODDEF) */
-/*[clinic end generated code: output=dcbc39b466598492 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=bf4b4bd6b976346d input=a9049054013a1b77]*/
diff --git a/Modules/_io/clinic/iobase.c.h b/Modules/_io/clinic/iobase.c.h
index 9762f11222..edbf73a40b 100644
--- a/Modules/_io/clinic/iobase.c.h
+++ b/Modules/_io/clinic/iobase.c.h
@@ -186,8 +186,9 @@ _io__IOBase_readline(PyObject *self, PyObject *args)
Py_ssize_t limit = -1;
if (!PyArg_ParseTuple(args, "|O&:readline",
- _PyIO_ConvertSsize_t, &limit))
+ _PyIO_ConvertSsize_t, &limit)) {
goto exit;
+ }
return_value = _io__IOBase_readline_impl(self, limit);
exit:
@@ -217,8 +218,9 @@ _io__IOBase_readlines(PyObject *self, PyObject *args)
Py_ssize_t hint = -1;
if (!PyArg_ParseTuple(args, "|O&:readlines",
- _PyIO_ConvertSsize_t, &hint))
+ _PyIO_ConvertSsize_t, &hint)) {
goto exit;
+ }
return_value = _io__IOBase_readlines_impl(self, hint);
exit:
@@ -251,8 +253,9 @@ _io__RawIOBase_read(PyObject *self, PyObject *args)
Py_ssize_t n = -1;
if (!PyArg_ParseTuple(args, "|n:read",
- &n))
+ &n)) {
goto exit;
+ }
return_value = _io__RawIOBase_read_impl(self, n);
exit:
@@ -276,4 +279,4 @@ _io__RawIOBase_readall(PyObject *self, PyObject *Py_UNUSED(ignored))
{
return _io__RawIOBase_readall_impl(self);
}
-/*[clinic end generated code: output=b874952f5cc248a4 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=0f53fed928d8e02f input=a9049054013a1b77]*/
diff --git a/Modules/_io/clinic/stringio.c.h b/Modules/_io/clinic/stringio.c.h
index a8e32a3376..ce9f46e879 100644
--- a/Modules/_io/clinic/stringio.c.h
+++ b/Modules/_io/clinic/stringio.c.h
@@ -61,8 +61,9 @@ _io_StringIO_read(stringio *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "read",
0, 1,
- &arg))
+ &arg)) {
goto exit;
+ }
return_value = _io_StringIO_read_impl(self, arg);
exit:
@@ -91,8 +92,9 @@ _io_StringIO_readline(stringio *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "readline",
0, 1,
- &arg))
+ &arg)) {
goto exit;
+ }
return_value = _io_StringIO_readline_impl(self, arg);
exit:
@@ -123,8 +125,9 @@ _io_StringIO_truncate(stringio *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "truncate",
0, 1,
- &arg))
+ &arg)) {
goto exit;
+ }
return_value = _io_StringIO_truncate_impl(self, arg);
exit:
@@ -157,8 +160,9 @@ _io_StringIO_seek(stringio *self, PyObject *args)
int whence = 0;
if (!PyArg_ParseTuple(args, "n|i:seek",
- &pos, &whence))
+ &pos, &whence)) {
goto exit;
+ }
return_value = _io_StringIO_seek_impl(self, pos, whence);
exit:
@@ -222,8 +226,9 @@ _io_StringIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
PyObject *newline_obj = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO:StringIO", _keywords,
- &value, &newline_obj))
+ &value, &newline_obj)) {
goto exit;
+ }
return_value = _io_StringIO___init___impl((stringio *)self, value, newline_obj);
exit:
@@ -283,4 +288,4 @@ _io_StringIO_seekable(stringio *self, PyObject *Py_UNUSED(ignored))
{
return _io_StringIO_seekable_impl(self);
}
-/*[clinic end generated code: output=f061cf3a20cd14ed input=a9049054013a1b77]*/
+/*[clinic end generated code: output=0513219581cbe952 input=a9049054013a1b77]*/
diff --git a/Modules/_io/clinic/textio.c.h b/Modules/_io/clinic/textio.c.h
index dc7e8c7584..f115326a00 100644
--- a/Modules/_io/clinic/textio.c.h
+++ b/Modules/_io/clinic/textio.c.h
@@ -30,8 +30,9 @@ _io_IncrementalNewlineDecoder___init__(PyObject *self, PyObject *args, PyObject
PyObject *errors = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|O:IncrementalNewlineDecoder", _keywords,
- &decoder, &translate, &errors))
+ &decoder, &translate, &errors)) {
goto exit;
+ }
return_value = _io_IncrementalNewlineDecoder___init___impl((nldecoder_object *)self, decoder, translate, errors);
exit:
@@ -59,8 +60,9 @@ _io_IncrementalNewlineDecoder_decode(nldecoder_object *self, PyObject *args, PyO
int final = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:decode", _keywords,
- &input, &final))
+ &input, &final)) {
goto exit;
+ }
return_value = _io_IncrementalNewlineDecoder_decode_impl(self, input, final);
exit:
@@ -162,8 +164,9 @@ _io_TextIOWrapper___init__(PyObject *self, PyObject *args, PyObject *kwargs)
int write_through = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|zzzii:TextIOWrapper", _keywords,
- &buffer, &encoding, &errors, &newline, &line_buffering, &write_through))
+ &buffer, &encoding, &errors, &newline, &line_buffering, &write_through)) {
goto exit;
+ }
return_value = _io_TextIOWrapper___init___impl((textio *)self, buffer, encoding, errors, newline, line_buffering, write_through);
exit:
@@ -204,8 +207,9 @@ _io_TextIOWrapper_write(textio *self, PyObject *arg)
PyObject *return_value = NULL;
PyObject *text;
- if (!PyArg_Parse(arg, "U:write", &text))
+ if (!PyArg_Parse(arg, "U:write", &text)) {
goto exit;
+ }
return_value = _io_TextIOWrapper_write_impl(self, text);
exit:
@@ -230,8 +234,9 @@ _io_TextIOWrapper_read(textio *self, PyObject *args)
Py_ssize_t n = -1;
if (!PyArg_ParseTuple(args, "|O&:read",
- _PyIO_ConvertSsize_t, &n))
+ _PyIO_ConvertSsize_t, &n)) {
goto exit;
+ }
return_value = _io_TextIOWrapper_read_impl(self, n);
exit:
@@ -256,8 +261,9 @@ _io_TextIOWrapper_readline(textio *self, PyObject *args)
Py_ssize_t size = -1;
if (!PyArg_ParseTuple(args, "|n:readline",
- &size))
+ &size)) {
goto exit;
+ }
return_value = _io_TextIOWrapper_readline_impl(self, size);
exit:
@@ -283,8 +289,9 @@ _io_TextIOWrapper_seek(textio *self, PyObject *args)
int whence = 0;
if (!PyArg_ParseTuple(args, "O|i:seek",
- &cookieObj, &whence))
+ &cookieObj, &whence)) {
goto exit;
+ }
return_value = _io_TextIOWrapper_seek_impl(self, cookieObj, whence);
exit:
@@ -327,8 +334,9 @@ _io_TextIOWrapper_truncate(textio *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "truncate",
0, 1,
- &pos))
+ &pos)) {
goto exit;
+ }
return_value = _io_TextIOWrapper_truncate_impl(self, pos);
exit:
@@ -453,4 +461,4 @@ _io_TextIOWrapper_close(textio *self, PyObject *Py_UNUSED(ignored))
{
return _io_TextIOWrapper_close_impl(self);
}
-/*[clinic end generated code: output=690608f85aab8ba5 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=31a39bbbe07ae4e7 input=a9049054013a1b77]*/
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 919cf502dc..12e5156fbb 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -92,8 +92,7 @@ fileio_dealloc_warn(fileio *self, PyObject *source)
if (self->fd >= 0 && self->closefd) {
PyObject *exc, *val, *tb;
PyErr_Fetch(&exc, &val, &tb);
- if (PyErr_WarnFormat(PyExc_ResourceWarning, 1,
- "unclosed file %R", source)) {
+ if (PyErr_ResourceWarning(source, 1, "unclosed file %R", source)) {
/* Spurious errors can appear at shutdown */
if (PyErr_ExceptionMatches(PyExc_Warning))
PyErr_WriteUnraisable((PyObject *) self);
@@ -546,7 +545,7 @@ err_closed(void)
}
static PyObject *
-err_mode(char *action)
+err_mode(const char *action)
{
_PyIO_State *state = IO_STATE();
if (state != NULL)
@@ -1049,7 +1048,7 @@ _io_FileIO_truncate_impl(fileio *self, PyObject *posobj)
}
#endif /* HAVE_FTRUNCATE */
-static char *
+static const char *
mode_string(fileio *self)
{
if (self->created) {
diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c
index 212b0ddcfc..f07a0ca295 100644
--- a/Modules/_io/iobase.c
+++ b/Modules/_io/iobase.c
@@ -828,7 +828,7 @@ PyTypeObject PyIOBase_Type = {
0, /* tp_weaklist */
0, /* tp_del */
0, /* tp_version_tag */
- (destructor)iobase_finalize, /* tp_finalize */
+ iobase_finalize, /* tp_finalize */
};
diff --git a/Modules/_io/stringio.c b/Modules/_io/stringio.c
index 06b4144578..ecf6dc1963 100644
--- a/Modules/_io/stringio.c
+++ b/Modules/_io/stringio.c
@@ -438,7 +438,7 @@ stringio_iternext(stringio *self)
_PyIO_str_readline, NULL);
if (line && !PyUnicode_Check(line)) {
PyErr_Format(PyExc_IOError,
- "readline() should have returned an str object, "
+ "readline() should have returned a str object, "
"not '%.200s'", Py_TYPE(line)->tp_name);
Py_DECREF(line);
return NULL;
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 063caa6067..96c8e7b453 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -772,7 +772,7 @@ typedef struct {
encodefunc_t encodefunc;
} encodefuncentry;
-static encodefuncentry encodefuncs[] = {
+static const encodefuncentry encodefuncs[] = {
{"ascii", (encodefunc_t) ascii_encode},
{"iso8859-1", (encodefunc_t) latin1_encode},
{"utf-8", (encodefunc_t) utf8_encode},
@@ -1021,7 +1021,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
goto error;
}
else if (PyUnicode_Check(res)) {
- encodefuncentry *e = encodefuncs;
+ const encodefuncentry *e = encodefuncs;
while (e->name != NULL) {
if (!PyUnicode_CompareWithASCIIString(res, e->name)) {
self->encodefunc = e->encodefunc;
@@ -1644,8 +1644,8 @@ _io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n)
/* NOTE: `end` must point to the real end of the Py_UCS4 storage,
that is to the NUL character. Otherwise the function will produce
incorrect results. */
-static char *
-find_control_char(int kind, char *s, char *end, Py_UCS4 ch)
+static const char *
+find_control_char(int kind, const char *s, const char *end, Py_UCS4 ch)
{
if (kind == PyUnicode_1BYTE_KIND) {
assert(ch < 256);
@@ -1665,13 +1665,13 @@ find_control_char(int kind, char *s, char *end, Py_UCS4 ch)
Py_ssize_t
_PyIO_find_line_ending(
int translated, int universal, PyObject *readnl,
- int kind, char *start, char *end, Py_ssize_t *consumed)
+ int kind, const char *start, const char *end, Py_ssize_t *consumed)
{
Py_ssize_t len = ((char*)end - (char*)start)/kind;
if (translated) {
/* Newlines are already translated, only search for \n */
- char *pos = find_control_char(kind, start, end, '\n');
+ const char *pos = find_control_char(kind, start, end, '\n');
if (pos != NULL)
return (pos - start)/kind + 1;
else {
@@ -1683,7 +1683,7 @@ _PyIO_find_line_ending(
/* Universal newline search. Find any of \r, \r\n, \n
* The decoder ensures that \r\n are not split in two pieces
*/
- char *s = start;
+ const char *s = start;
for (;;) {
Py_UCS4 ch;
/* Fast path for non-control chars. The loop always ends
@@ -1713,21 +1713,21 @@ _PyIO_find_line_ending(
/* Assume that readnl is an ASCII character. */
assert(PyUnicode_KIND(readnl) == PyUnicode_1BYTE_KIND);
if (readnl_len == 1) {
- char *pos = find_control_char(kind, start, end, nl[0]);
+ const char *pos = find_control_char(kind, start, end, nl[0]);
if (pos != NULL)
return (pos - start)/kind + 1;
*consumed = len;
return -1;
}
else {
- char *s = start;
- char *e = end - (readnl_len - 1)*kind;
- char *pos;
+ const char *s = start;
+ const char *e = end - (readnl_len - 1)*kind;
+ const char *pos;
if (e < s)
e = s;
while (s < e) {
Py_ssize_t i;
- char *pos = find_control_char(kind, s, end, nl[0]);
+ const char *pos = find_control_char(kind, s, end, nl[0]);
if (pos == NULL || pos >= e)
break;
for (i = 1; i < readnl_len; i++) {
@@ -2689,7 +2689,7 @@ textiowrapper_iternext(textio *self)
_PyIO_str_readline, NULL);
if (line && !PyUnicode_Check(line)) {
PyErr_Format(PyExc_IOError,
- "readline() should have returned an str object, "
+ "readline() should have returned a str object, "
"not '%.200s'", Py_TYPE(line)->tp_name);
Py_DECREF(line);
return NULL;
diff --git a/Modules/_json.c b/Modules/_json.c
index f82af347cb..d3dbf98805 100644
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -112,7 +112,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc, PyObject *dct, Py_ssiz
static PyObject *
_encoded_const(PyObject *obj);
static void
-raise_errmsg(char *msg, PyObject *s, Py_ssize_t end);
+raise_errmsg(const char *msg, PyObject *s, Py_ssize_t end);
static PyObject *
encoder_encode_string(PyEncoderObject *s, PyObject *obj);
static PyObject *
@@ -321,7 +321,7 @@ escape_unicode(PyObject *pystr)
}
static void
-raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
+raise_errmsg(const char *msg, PyObject *s, Py_ssize_t end)
{
/* Use JSONDecodeError exception to raise a nice looking ValueError subclass */
static PyObject *JSONDecodeError = NULL;
diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c
index b1d6add477..a92fb10dd2 100644
--- a/Modules/_localemodule.c
+++ b/Modules/_localemodule.c
@@ -49,7 +49,7 @@ PyDoc_STRVAR(setlocale__doc__,
/* the grouping is terminated by either 0 or CHAR_MAX */
static PyObject*
-copy_grouping(char* s)
+copy_grouping(const char* s)
{
int i;
PyObject *result, *val = NULL;
diff --git a/Modules/_lsprof.c b/Modules/_lsprof.c
index 66e534f2a8..ccfb513038 100644
--- a/Modules/_lsprof.c
+++ b/Modules/_lsprof.c
@@ -762,7 +762,6 @@ profiler_dealloc(ProfilerObject *op)
static int
profiler_init(ProfilerObject *pObj, PyObject *args, PyObject *kw)
{
- PyObject *o;
PyObject *timer = NULL;
double timeunit = 0.0;
int subcalls = 1;
@@ -777,11 +776,9 @@ profiler_init(ProfilerObject *pObj, PyObject *args, PyObject *kw)
if (setSubcalls(pObj, subcalls) < 0 || setBuiltins(pObj, builtins) < 0)
return -1;
- o = pObj->externalTimer;
- pObj->externalTimer = timer;
- Py_XINCREF(timer);
- Py_XDECREF(o);
pObj->externalTimerUnit = timeunit;
+ Py_XINCREF(timer);
+ Py_XSETREF(pObj->externalTimer, timer);
return 0;
}
diff --git a/Modules/_lzmamodule.c b/Modules/_lzmamodule.c
index bc01ffe7ac..ce4e17fc86 100644
--- a/Modules/_lzmamodule.c
+++ b/Modules/_lzmamodule.c
@@ -553,7 +553,6 @@ error:
/*[clinic input]
_lzma.LZMACompressor.compress
- self: self(type="Compressor *")
data: Py_buffer
/
@@ -567,7 +566,7 @@ flush() method to finish the compression process.
static PyObject *
_lzma_LZMACompressor_compress_impl(Compressor *self, Py_buffer *data)
-/*[clinic end generated code: output=31f615136963e00f input=8b60cb13e0ce6420]*/
+/*[clinic end generated code: output=31f615136963e00f input=64019eac7f2cc8d0]*/
{
PyObject *result = NULL;
@@ -583,8 +582,6 @@ _lzma_LZMACompressor_compress_impl(Compressor *self, Py_buffer *data)
/*[clinic input]
_lzma.LZMACompressor.flush
- self: self(type="Compressor *")
-
Finish the compression process.
Returns the compressed data left in internal buffers.
@@ -594,7 +591,7 @@ The compressor object may not be used after this method is called.
static PyObject *
_lzma_LZMACompressor_flush_impl(Compressor *self)
-/*[clinic end generated code: output=fec21f3e22504f50 input=3060fb26f9b4042c]*/
+/*[clinic end generated code: output=fec21f3e22504f50 input=6b369303f67ad0a8]*/
{
PyObject *result = NULL;
@@ -698,7 +695,6 @@ Compressor_init_raw(lzma_stream *lzs, PyObject *filterspecs)
/*[-clinic input]
_lzma.LZMACompressor.__init__
- self: self(type="Compressor *")
format: int(c_default="FORMAT_XZ") = FORMAT_XZ
The container format to use for the output. This can
be FORMAT_XZ (default), FORMAT_ALONE, or FORMAT_RAW.
@@ -1063,7 +1059,6 @@ error:
/*[clinic input]
_lzma.LZMADecompressor.decompress
- self: self(type="Decompressor *")
data: Py_buffer
max_length: Py_ssize_t=-1
@@ -1086,7 +1081,7 @@ the unused_data attribute.
static PyObject *
_lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data,
Py_ssize_t max_length)
-/*[clinic end generated code: output=ef4e20ec7122241d input=f2bb902cc1caf203]*/
+/*[clinic end generated code: output=ef4e20ec7122241d input=60c1f135820e309d]*/
{
PyObject *result = NULL;
@@ -1126,7 +1121,6 @@ Decompressor_init_raw(lzma_stream *lzs, PyObject *filterspecs)
/*[clinic input]
_lzma.LZMADecompressor.__init__
- self: self(type="Decompressor *")
format: int(c_default="FORMAT_AUTO") = FORMAT_AUTO
Specifies the container format of the input stream. If this is
FORMAT_AUTO (the default), the decompressor will automatically detect
@@ -1152,7 +1146,7 @@ For one-shot decompression, use the decompress() function instead.
static int
_lzma_LZMADecompressor___init___impl(Decompressor *self, int format,
PyObject *memlimit, PyObject *filters)
-/*[clinic end generated code: output=3e1821f8aa36564c input=458ca6132ef29801]*/
+/*[clinic end generated code: output=3e1821f8aa36564c input=81fe684a6c2f8a27]*/
{
const uint32_t decoder_flags = LZMA_TELL_ANY_CHECK | LZMA_TELL_NO_CHECK;
uint64_t memlimit_ = UINT64_MAX;
diff --git a/Modules/_multiprocessing/multiprocessing.c b/Modules/_multiprocessing/multiprocessing.c
index 4ae638eea5..d92a8bf21c 100644
--- a/Modules/_multiprocessing/multiprocessing.c
+++ b/Modules/_multiprocessing/multiprocessing.c
@@ -128,7 +128,7 @@ static PyMethodDef module_methods[] = {
{"recv", multiprocessing_recv, METH_VARARGS, ""},
{"send", multiprocessing_send, METH_VARARGS, ""},
#endif
-#ifndef POSIX_SEMAPHORES_NOT_ENABLED
+#if !defined(POSIX_SEMAPHORES_NOT_ENABLED) && !defined(__ANDROID__)
{"sem_unlink", _PyMp_sem_unlink, METH_VARARGS, ""},
#endif
{NULL}
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index 3c21b6aac4..ddac24334d 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -153,6 +153,9 @@ typedef struct {
PyObject *codecs_encode;
/* builtins.getattr, used for saving nested names with protocol < 4 */
PyObject *getattr;
+ /* functools.partial, used for implementing __newobj_ex__ with protocols
+ 2 and 3 */
+ PyObject *partial;
} PickleState;
/* Forward declaration of the _pickle module definition. */
@@ -190,6 +193,7 @@ _Pickle_ClearState(PickleState *st)
Py_CLEAR(st->import_mapping_3to2);
Py_CLEAR(st->codecs_encode);
Py_CLEAR(st->getattr);
+ Py_CLEAR(st->partial);
}
/* Initialize the given pickle module state. */
@@ -200,6 +204,7 @@ _Pickle_InitState(PickleState *st)
PyObject *copyreg = NULL;
PyObject *compat_pickle = NULL;
PyObject *codecs = NULL;
+ PyObject *functools = NULL;
builtins = PyEval_GetBuiltins();
if (builtins == NULL)
@@ -314,12 +319,21 @@ _Pickle_InitState(PickleState *st)
}
Py_CLEAR(codecs);
+ functools = PyImport_ImportModule("functools");
+ if (!functools)
+ goto error;
+ st->partial = PyObject_GetAttrString(functools, "partial");
+ if (!st->partial)
+ goto error;
+ Py_CLEAR(functools);
+
return 0;
error:
Py_CLEAR(copyreg);
Py_CLEAR(compat_pickle);
Py_CLEAR(codecs);
+ Py_CLEAR(functools);
_Pickle_ClearState(st);
return -1;
}
@@ -356,18 +370,12 @@ _Pickle_FastCall(PyObject *func, PyObject *obj)
/*************************************************************************/
-static int
-stack_underflow(void)
-{
- PickleState *st = _Pickle_GetGlobalState();
- PyErr_SetString(st->UnpicklingError, "unpickling stack underflow");
- return -1;
-}
-
/* Internal data type used as the unpickling stack. */
typedef struct {
PyObject_VAR_HEAD
PyObject **data;
+ int mark_set; /* is MARK set? */
+ Py_ssize_t fence; /* position of top MARK or 0 */
Py_ssize_t allocated; /* number of slots in data allocated */
} Pdata;
@@ -398,6 +406,8 @@ Pdata_New(void)
if (!(self = PyObject_New(Pdata, &Pdata_Type)))
return NULL;
Py_SIZE(self) = 0;
+ self->mark_set = 0;
+ self->fence = 0;
self->allocated = 8;
self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
if (self->data)
@@ -415,8 +425,7 @@ Pdata_clear(Pdata *self, Py_ssize_t clearto)
{
Py_ssize_t i = Py_SIZE(self);
- if (clearto < 0)
- return stack_underflow();
+ assert(clearto >= self->fence);
if (clearto >= i)
return 0;
@@ -452,6 +461,17 @@ Pdata_grow(Pdata *self)
return -1;
}
+static int
+Pdata_stack_underflow(Pdata *self)
+{
+ PickleState *st = _Pickle_GetGlobalState();
+ PyErr_SetString(st->UnpicklingError,
+ self->mark_set ?
+ "unexpected MARK found" :
+ "unpickling stack underflow");
+ return -1;
+}
+
/* D is a Pdata*. Pop the topmost element and store it into V, which
* must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
* is raised and V is set to NULL.
@@ -459,9 +479,8 @@ Pdata_grow(Pdata *self)
static PyObject *
Pdata_pop(Pdata *self)
{
- if (Py_SIZE(self) == 0) {
- PickleState *st = _Pickle_GetGlobalState();
- PyErr_SetString(st->UnpicklingError, "bad pickle data");
+ if (Py_SIZE(self) <= self->fence) {
+ Pdata_stack_underflow(self);
return NULL;
}
return self->data[--Py_SIZE(self)];
@@ -493,6 +512,10 @@ Pdata_poptuple(Pdata *self, Py_ssize_t start)
PyObject *tuple;
Py_ssize_t len, i, j;
+ if (start < self->fence) {
+ Pdata_stack_underflow(self);
+ return NULL;
+ }
len = Py_SIZE(self) - start;
tuple = PyTuple_New(len);
if (tuple == NULL)
@@ -860,7 +883,7 @@ _write_size64(char *out, size_t value)
{
size_t i;
- assert(sizeof(size_t) <= 8);
+ Py_BUILD_ASSERT(sizeof(size_t) <= 8);
for (i = 0; i < sizeof(size_t); i++) {
out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
@@ -1174,21 +1197,9 @@ _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
return read_size;
}
-/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
-
- This should be used for all data reads, rather than accessing the unpickler's
- input buffer directly. This method deals correctly with reading from input
- streams, which the input buffer doesn't deal with.
-
- Note that when reading from a file-like object, self->next_read_idx won't
- be updated (it should remain at 0 for the entire unpickling process). You
- should use this function's return value to know how many bytes you can
- consume.
-
- Returns -1 (with an exception set) on failure. On success, return the
- number of chars read. */
+/* Don't call it directly: use _Unpickler_Read() */
static Py_ssize_t
-_Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
+_Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
{
Py_ssize_t num_read;
@@ -1199,11 +1210,10 @@ _Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
"read would overflow (invalid bytecode)");
return -1;
}
- if (self->next_read_idx + n <= self->input_len) {
- *s = self->input_buffer + self->next_read_idx;
- self->next_read_idx += n;
- return n;
- }
+
+ /* This case is handled by the _Unpickler_Read() macro for efficiency */
+ assert(self->next_read_idx + n > self->input_len);
+
if (!self->read) {
PyErr_Format(PyExc_EOFError, "Ran out of input");
return -1;
@@ -1220,6 +1230,26 @@ _Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
return n;
}
+/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
+
+ This should be used for all data reads, rather than accessing the unpickler's
+ input buffer directly. This method deals correctly with reading from input
+ streams, which the input buffer doesn't deal with.
+
+ Note that when reading from a file-like object, self->next_read_idx won't
+ be updated (it should remain at 0 for the entire unpickling process). You
+ should use this function's return value to know how many bytes you can
+ consume.
+
+ Returns -1 (with an exception set) on failure. On success, return the
+ number of chars read. */
+#define _Unpickler_Read(self, s, n) \
+ (((n) <= (self)->input_len - (self)->next_read_idx) \
+ ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \
+ (self)->next_read_idx += (n), \
+ (n)) \
+ : _Unpickler_ReadImpl(self, (s), (n)))
+
static Py_ssize_t
_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
char **result)
@@ -2096,38 +2126,35 @@ save_bytes(PicklerObject *self, PyObject *obj)
static PyObject *
raw_unicode_escape(PyObject *obj)
{
- PyObject *repr;
char *p;
Py_ssize_t i, size;
- size_t expandsize;
void *data;
unsigned int kind;
+ _PyBytesWriter writer;
if (PyUnicode_READY(obj))
return NULL;
+ _PyBytesWriter_Init(&writer);
+
size = PyUnicode_GET_LENGTH(obj);
data = PyUnicode_DATA(obj);
kind = PyUnicode_KIND(obj);
- if (kind == PyUnicode_4BYTE_KIND)
- expandsize = 10;
- else
- expandsize = 6;
- if ((size_t)size > (size_t)PY_SSIZE_T_MAX / expandsize)
- return PyErr_NoMemory();
- repr = PyBytes_FromStringAndSize(NULL, expandsize * size);
- if (repr == NULL)
- return NULL;
- if (size == 0)
- return repr;
- assert(Py_REFCNT(repr) == 1);
+ p = _PyBytesWriter_Alloc(&writer, size);
+ if (p == NULL)
+ goto error;
+ writer.overallocate = 1;
- p = PyBytes_AS_STRING(repr);
for (i=0; i < size; i++) {
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
/* Map 32-bit characters to '\Uxxxxxxxx' */
if (ch >= 0x10000) {
+ /* -1: substract 1 preallocated byte */
+ p = _PyBytesWriter_Prepare(&writer, p, 10-1);
+ if (p == NULL)
+ goto error;
+
*p++ = '\\';
*p++ = 'U';
*p++ = Py_hexdigits[(ch >> 28) & 0xf];
@@ -2139,8 +2166,13 @@ raw_unicode_escape(PyObject *obj)
*p++ = Py_hexdigits[(ch >> 4) & 0xf];
*p++ = Py_hexdigits[ch & 15];
}
- /* Map 16-bit characters to '\uxxxx' */
+ /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
else if (ch >= 256 || ch == '\\' || ch == '\n') {
+ /* -1: substract 1 preallocated byte */
+ p = _PyBytesWriter_Prepare(&writer, p, 6-1);
+ if (p == NULL)
+ goto error;
+
*p++ = '\\';
*p++ = 'u';
*p++ = Py_hexdigits[(ch >> 12) & 0xf];
@@ -2152,14 +2184,16 @@ raw_unicode_escape(PyObject *obj)
else
*p++ = (char) ch;
}
- size = p - PyBytes_AS_STRING(repr);
- if (_PyBytes_Resize(&repr, size) < 0)
- return NULL;
- return repr;
+
+ return _PyBytesWriter_Finish(&writer, p);
+
+error:
+ _PyBytesWriter_Dealloc(&writer);
+ return NULL;
}
static int
-write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
+write_utf8(PicklerObject *self, const char *data, Py_ssize_t size)
{
char header[9];
Py_ssize_t len;
@@ -3535,11 +3569,9 @@ save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
PyErr_Clear();
}
else if (PyUnicode_Check(name)) {
- if (self->proto >= 4) {
- _Py_IDENTIFIER(__newobj_ex__);
- use_newobj_ex = PyUnicode_Compare(
- name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
- }
+ _Py_IDENTIFIER(__newobj_ex__);
+ use_newobj_ex = PyUnicode_Compare(
+ name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
if (!use_newobj_ex) {
_Py_IDENTIFIER(__newobj__);
use_newobj = PyUnicode_Compare(
@@ -3583,11 +3615,58 @@ save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
return -1;
}
- if (save(self, cls, 0) < 0 ||
- save(self, args, 0) < 0 ||
- save(self, kwargs, 0) < 0 ||
- _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
- return -1;
+ if (self->proto >= 4) {
+ if (save(self, cls, 0) < 0 ||
+ save(self, args, 0) < 0 ||
+ save(self, kwargs, 0) < 0 ||
+ _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
+ return -1;
+ }
+ }
+ else {
+ PyObject *newargs;
+ PyObject *cls_new;
+ Py_ssize_t i;
+ _Py_IDENTIFIER(__new__);
+
+ newargs = PyTuple_New(Py_SIZE(args) + 2);
+ if (newargs == NULL)
+ return -1;
+
+ cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
+ if (cls_new == NULL) {
+ Py_DECREF(newargs);
+ return -1;
+ }
+ PyTuple_SET_ITEM(newargs, 0, cls_new);
+ Py_INCREF(cls);
+ PyTuple_SET_ITEM(newargs, 1, cls);
+ for (i = 0; i < Py_SIZE(args); i++) {
+ PyObject *item = PyTuple_GET_ITEM(args, i);
+ Py_INCREF(item);
+ PyTuple_SET_ITEM(newargs, i + 2, item);
+ }
+
+ callable = PyObject_Call(st->partial, newargs, kwargs);
+ Py_DECREF(newargs);
+ if (callable == NULL)
+ return -1;
+
+ newargs = PyTuple_New(0);
+ if (newargs == NULL) {
+ Py_DECREF(callable);
+ return -1;
+ }
+
+ if (save(self, callable, 0) < 0 ||
+ save(self, newargs, 0) < 0 ||
+ _Pickler_Write(self, &reduce_op, 1) < 0) {
+ Py_DECREF(newargs);
+ Py_DECREF(callable);
+ return -1;
+ }
+ Py_DECREF(newargs);
+ Py_DECREF(callable);
}
}
else if (use_newobj) {
@@ -4397,7 +4476,7 @@ Pickler_set_memo(PicklerObject *self, PyObject *obj)
}
else {
PyErr_Format(PyExc_TypeError,
- "'memo' attribute must be an PicklerMemoProxy object"
+ "'memo' attribute must be a PicklerMemoProxy object"
"or dict, not %.200s", Py_TYPE(obj)->tp_name);
return -1;
}
@@ -4426,8 +4505,6 @@ Pickler_get_persid(PicklerObject *self)
static int
Pickler_set_persid(PicklerObject *self, PyObject *value)
{
- PyObject *tmp;
-
if (value == NULL) {
PyErr_SetString(PyExc_TypeError,
"attribute deletion is not supported");
@@ -4439,10 +4516,8 @@ Pickler_set_persid(PicklerObject *self, PyObject *value)
return -1;
}
- tmp = self->pers_func;
Py_INCREF(value);
- self->pers_func = value;
- Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
+ Py_XSETREF(self->pers_func, value);
return 0;
}
@@ -4524,13 +4599,19 @@ find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
static Py_ssize_t
marker(UnpicklerObject *self)
{
- PickleState *st = _Pickle_GetGlobalState();
+ Py_ssize_t mark;
+
if (self->num_marks < 1) {
+ PickleState *st = _Pickle_GetGlobalState();
PyErr_SetString(st->UnpicklingError, "could not find MARK");
return -1;
}
- return self->marks[--self->num_marks];
+ mark = self->marks[--self->num_marks];
+ self->stack->mark_set = self->num_marks != 0;
+ self->stack->fence = self->num_marks ?
+ self->marks[self->num_marks - 1] : 0;
+ return mark;
}
static int
@@ -4991,7 +5072,7 @@ load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
PyObject *tuple;
if (Py_SIZE(self->stack) < len)
- return stack_underflow();
+ return Pdata_stack_underflow(self->stack);
tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
if (tuple == NULL)
@@ -5073,6 +5154,13 @@ load_dict(UnpicklerObject *self)
if ((dict = PyDict_New()) == NULL)
return -1;
+ if ((j - i) % 2 != 0) {
+ PickleState *st = _Pickle_GetGlobalState();
+ PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
+ Py_DECREF(dict);
+ return -1;
+ }
+
for (k = i + 1; k < j; k += 2) {
key = self->stack->data[k - 1];
value = self->stack->data[k];
@@ -5140,7 +5228,7 @@ load_obj(UnpicklerObject *self)
return -1;
if (Py_SIZE(self->stack) - i < 1)
- return stack_underflow();
+ return Pdata_stack_underflow(self->stack);
args = Pdata_poptuple(self->stack, i + 1);
if (args == NULL)
@@ -5463,12 +5551,15 @@ load_pop(UnpicklerObject *self)
*/
if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
self->num_marks--;
- } else if (len > 0) {
+ self->stack->mark_set = self->num_marks != 0;
+ self->stack->fence = self->num_marks ?
+ self->marks[self->num_marks - 1] : 0;
+ } else if (len <= self->stack->fence)
+ return Pdata_stack_underflow(self->stack);
+ else {
len--;
Py_DECREF(self->stack->data[len]);
Py_SIZE(self->stack) = len;
- } else {
- return stack_underflow();
}
return 0;
}
@@ -5490,10 +5581,10 @@ static int
load_dup(UnpicklerObject *self)
{
PyObject *last;
- Py_ssize_t len;
+ Py_ssize_t len = Py_SIZE(self->stack);
- if ((len = Py_SIZE(self->stack)) <= 0)
- return stack_underflow();
+ if (len <= self->stack->fence)
+ return Pdata_stack_underflow(self->stack);
last = self->stack->data[len - 1];
PDATA_APPEND(self->stack, last, -1);
return 0;
@@ -5676,8 +5767,8 @@ load_put(UnpicklerObject *self)
return -1;
if (len < 2)
return bad_readline();
- if (Py_SIZE(self->stack) <= 0)
- return stack_underflow();
+ if (Py_SIZE(self->stack) <= self->stack->fence)
+ return Pdata_stack_underflow(self->stack);
value = self->stack->data[Py_SIZE(self->stack) - 1];
key = PyLong_FromString(s, NULL, 10);
@@ -5705,8 +5796,8 @@ load_binput(UnpicklerObject *self)
if (_Unpickler_Read(self, &s, 1) < 0)
return -1;
- if (Py_SIZE(self->stack) <= 0)
- return stack_underflow();
+ if (Py_SIZE(self->stack) <= self->stack->fence)
+ return Pdata_stack_underflow(self->stack);
value = self->stack->data[Py_SIZE(self->stack) - 1];
idx = Py_CHARMASK(s[0]);
@@ -5724,8 +5815,8 @@ load_long_binput(UnpicklerObject *self)
if (_Unpickler_Read(self, &s, 4) < 0)
return -1;
- if (Py_SIZE(self->stack) <= 0)
- return stack_underflow();
+ if (Py_SIZE(self->stack) <= self->stack->fence)
+ return Pdata_stack_underflow(self->stack);
value = self->stack->data[Py_SIZE(self->stack) - 1];
idx = calc_binsize(s, 4);
@@ -5743,8 +5834,8 @@ load_memoize(UnpicklerObject *self)
{
PyObject *value;
- if (Py_SIZE(self->stack) <= 0)
- return stack_underflow();
+ if (Py_SIZE(self->stack) <= self->stack->fence)
+ return Pdata_stack_underflow(self->stack);
value = self->stack->data[Py_SIZE(self->stack) - 1];
return _Unpickler_MemoPut(self, self->memo_len, value);
@@ -5758,8 +5849,8 @@ do_append(UnpicklerObject *self, Py_ssize_t x)
Py_ssize_t len, i;
len = Py_SIZE(self->stack);
- if (x > len || x <= 0)
- return stack_underflow();
+ if (x > len || x <= self->stack->fence)
+ return Pdata_stack_underflow(self->stack);
if (len == x) /* nothing to do */
return 0;
@@ -5808,8 +5899,8 @@ do_append(UnpicklerObject *self, Py_ssize_t x)
static int
load_append(UnpicklerObject *self)
{
- if (Py_SIZE(self->stack) - 1 <= 0)
- return stack_underflow();
+ if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
+ return Pdata_stack_underflow(self->stack);
return do_append(self, Py_SIZE(self->stack) - 1);
}
@@ -5831,8 +5922,8 @@ do_setitems(UnpicklerObject *self, Py_ssize_t x)
int status = 0;
len = Py_SIZE(self->stack);
- if (x > len || x <= 0)
- return stack_underflow();
+ if (x > len || x <= self->stack->fence)
+ return Pdata_stack_underflow(self->stack);
if (len == x) /* nothing to do */
return 0;
if ((len - x) % 2 != 0) {
@@ -5885,8 +5976,8 @@ load_additems(UnpicklerObject *self)
if (mark < 0)
return -1;
len = Py_SIZE(self->stack);
- if (mark > len || mark <= 0)
- return stack_underflow();
+ if (mark > len || mark <= self->stack->fence)
+ return Pdata_stack_underflow(self->stack);
if (len == mark) /* nothing to do */
return 0;
@@ -5941,8 +6032,8 @@ load_build(UnpicklerObject *self)
/* Stack is ... instance, state. We want to leave instance at
* the stack top, possibly mutated via instance.__setstate__(state).
*/
- if (Py_SIZE(self->stack) < 2)
- return stack_underflow();
+ if (Py_SIZE(self->stack) - 2 < self->stack->fence)
+ return Pdata_stack_underflow(self->stack);
PDATA_POP(self->stack, state);
if (state == NULL)
@@ -6078,7 +6169,8 @@ load_mark(UnpicklerObject *self)
self->marks_size = (Py_ssize_t)alloc;
}
- self->marks[self->num_marks++] = Py_SIZE(self->stack);
+ self->stack->mark_set = 1;
+ self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
return 0;
}
@@ -6161,6 +6253,8 @@ load(UnpicklerObject *self)
char *s = NULL;
self->num_marks = 0;
+ self->stack->mark_set = 0;
+ self->stack->fence = 0;
self->proto = 0;
if (Py_SIZE(self->stack))
Pdata_clear(self->stack, 0);
@@ -6865,8 +6959,6 @@ Unpickler_get_persload(UnpicklerObject *self)
static int
Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
{
- PyObject *tmp;
-
if (value == NULL) {
PyErr_SetString(PyExc_TypeError,
"attribute deletion is not supported");
@@ -6879,10 +6971,8 @@ Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
return -1;
}
- tmp = self->pers_func;
Py_INCREF(value);
- self->pers_func = value;
- Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
+ Py_XSETREF(self->pers_func, value);
return 0;
}
diff --git a/Modules/_posixsubprocess.c b/Modules/_posixsubprocess.c
index 8bedab5c27..0ca0aa5cda 100644
--- a/Modules/_posixsubprocess.c
+++ b/Modules/_posixsubprocess.c
@@ -21,8 +21,7 @@
#include <dirent.h>
#endif
-#if defined(__ANDROID__) && !defined(SYS_getdents64)
-/* Android doesn't expose syscalls, add the definition manually. */
+#if defined(__ANDROID__) && __ANDROID_API__ < 21 && !defined(SYS_getdents64)
# include <sys/linux-syscalls.h>
# define SYS_getdents64 __NR_getdents64
#endif
@@ -72,7 +71,7 @@ _enable_gc(int need_to_reenable_gc, PyObject *gc_module)
/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
static int
-_pos_int_from_ascii(char *name)
+_pos_int_from_ascii(const char *name)
{
int num = 0;
while (*name >= '0' && *name <= '9') {
diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c
index 95ad4a429a..fd6b230e85 100644
--- a/Modules/_randommodule.c
+++ b/Modules/_randommodule.c
@@ -99,7 +99,7 @@ static PY_UINT32_T
genrand_int32(RandomObject *self)
{
PY_UINT32_T y;
- static PY_UINT32_T mag01[2]={0x0U, MATRIX_A};
+ static const PY_UINT32_T mag01[2] = {0x0U, MATRIX_A};
/* mag01[x] = x * MATRIX_A for x=0,1 */
PY_UINT32_T *mt;
@@ -136,7 +136,7 @@ genrand_int32(RandomObject *self)
* optimize the division away at compile-time. 67108864 is 2**26. In
* effect, a contains 27 random bits shifted left 26, and b fills in the
* lower 26 bits of the 53-bit numerator.
- * The orginal code credited Isaku Wada for this algorithm, 2002/01/09.
+ * The original code credited Isaku Wada for this algorithm, 2002/01/09.
*/
static PyObject *
random_random(RandomObject *self)
diff --git a/Modules/_scproxy.c b/Modules/_scproxy.c
index 66b6e3439f..68be458bf4 100644
--- a/Modules/_scproxy.c
+++ b/Modules/_scproxy.c
@@ -130,7 +130,7 @@ error:
}
static int
-set_proxy(PyObject* proxies, char* proto, CFDictionaryRef proxyDict,
+set_proxy(PyObject* proxies, const char* proto, CFDictionaryRef proxyDict,
CFStringRef enabledKey,
CFStringRef hostKey, CFStringRef portKey)
{
diff --git a/Modules/_sqlite/cache.c b/Modules/_sqlite/cache.c
index 3689a4e387..62c58931fa 100644
--- a/Modules/_sqlite/cache.c
+++ b/Modules/_sqlite/cache.c
@@ -244,8 +244,7 @@ PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)
ptr = ptr->next;
}
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
static PyMethodDef cache_methods[] = {
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index 6aa4764b6c..5349299765 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -352,8 +352,7 @@ PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
}
}
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
/*
@@ -861,8 +860,7 @@ PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObjec
if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
}
@@ -893,8 +891,7 @@ PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObje
if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
}
@@ -1029,8 +1026,7 @@ static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, P
if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
}
@@ -1059,8 +1055,7 @@ static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* s
return NULL;
}
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
@@ -1087,8 +1082,7 @@ static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* sel
sqlite3_trace(self->db, _trace_callback, trace_callback);
}
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
#ifdef HAVE_LOAD_EXTENSION
@@ -1111,8 +1105,7 @@ static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObj
PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
return NULL;
} else {
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
}
@@ -1135,8 +1128,7 @@ static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* ar
PyErr_SetString(pysqlite_OperationalError, errmsg);
return NULL;
} else {
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
}
#endif
@@ -1626,7 +1618,7 @@ pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
Py_RETURN_FALSE;
}
-static char connection_doc[] =
+static const char connection_doc[] =
PyDoc_STR("SQLite database connection object.");
static PyGetSetDef connection_getset[] = {
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index 300da2878e..9b206785e5 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -27,7 +27,7 @@
PyObject* pysqlite_cursor_iternext(pysqlite_Cursor* self);
-static char* errmsg_fetch_across_rollback = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from.";
+static const char errmsg_fetch_across_rollback[] = "Cursor needed to be reset because of commit/rollback and can no longer be fetched from.";
static pysqlite_StatementKind detect_statement_type(const char* statement)
{
@@ -242,8 +242,7 @@ PyObject* _pysqlite_build_column_name(const char* colname)
const char* pos;
if (!colname) {
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
for (pos = colname;; pos++) {
@@ -699,7 +698,9 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
}
Py_DECREF(self->lastrowid);
- if (!multiple && statement_type == STATEMENT_INSERT) {
+ if (!multiple &&
+ /* REPLACE is an alias for INSERT OR REPLACE */
+ (statement_type == STATEMENT_INSERT || statement_type == STATEMENT_REPLACE)) {
sqlite_int64 lastrowid;
Py_BEGIN_ALLOW_THREADS
lastrowid = sqlite3_last_insert_rowid(self->connection->db);
@@ -914,8 +915,7 @@ PyObject* pysqlite_cursor_fetchone(pysqlite_Cursor* self, PyObject* args)
row = pysqlite_cursor_iternext(self);
if (!row && !PyErr_Occurred()) {
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
return row;
@@ -996,8 +996,7 @@ PyObject* pysqlite_cursor_fetchall(pysqlite_Cursor* self, PyObject* args)
PyObject* pysqlite_noop(pysqlite_Connection* self, PyObject* args)
{
/* don't care, return None */
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
@@ -1013,8 +1012,7 @@ PyObject* pysqlite_cursor_close(pysqlite_Cursor* self, PyObject* args)
self->closed = 1;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
static PyMethodDef cursor_methods[] = {
@@ -1050,7 +1048,7 @@ static struct PyMemberDef cursor_members[] =
{NULL}
};
-static char cursor_doc[] =
+static const char cursor_doc[] =
PyDoc_STR("SQLite database cursor class.");
PyTypeObject pysqlite_CursorType = {
diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c
index 7a7e86040a..7cd6d2abeb 100644
--- a/Modules/_sqlite/module.c
+++ b/Modules/_sqlite/module.c
@@ -139,8 +139,7 @@ static PyObject* module_enable_shared_cache(PyObject* self, PyObject* args, PyOb
PyErr_SetString(pysqlite_OperationalError, "Changing the shared_cache flag failed");
return NULL;
} else {
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
}
@@ -172,8 +171,7 @@ static PyObject* module_register_adapter(PyObject* self, PyObject* args)
if (rc == -1)
return NULL;
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(module_register_adapter_doc,
@@ -221,8 +219,7 @@ static PyObject* enable_callback_tracebacks(PyObject* self, PyObject* args)
return NULL;
}
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(enable_callback_tracebacks_doc,
@@ -261,13 +258,13 @@ static PyMethodDef module_methods[] = {
};
struct _IntConstantPair {
- char* constant_name;
+ const char *constant_name;
int constant_value;
};
typedef struct _IntConstantPair IntConstantPair;
-static IntConstantPair _int_constants[] = {
+static const IntConstantPair _int_constants[] = {
{"PARSE_DECLTYPES", PARSE_DECLTYPES},
{"PARSE_COLNAMES", PARSE_COLNAMES},
diff --git a/Modules/_sre.c b/Modules/_sre.c
index 84c8769455..512b3f2d86 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -35,7 +35,7 @@
* other compatibility work.
*/
-static char copyright[] =
+static const char copyright[] =
" SRE 2.2.2 Copyright (c) 1997-2002 by Secret Labs AB ";
#define PY_SSIZE_T_CLEAN
@@ -62,9 +62,6 @@ static char copyright[] =
/* -------------------------------------------------------------------- */
/* optional features */
-/* enables fast searching */
-#define USE_FAST_SEARCH
-
/* enables copy/deepcopy handling (work in progress) */
#undef USE_BUILTIN_COPY
@@ -717,7 +714,7 @@ _sre_SRE_Pattern_search_impl(PatternObject *self, PyObject *string,
}
static PyObject*
-call(char* module, char* function, PyObject* args)
+call(const char* module, const char* function, PyObject* args)
{
PyObject* name;
PyObject* mod;
@@ -2052,8 +2049,9 @@ match_getindex(MatchObject* self, PyObject* index)
/* Default value */
return 0;
- if (PyLong_Check(index))
- return PyLong_AsSsize_t(index);
+ if (PyIndex_Check(index)) {
+ return PyNumber_AsSsize_t(index, NULL);
+ }
i = -1;
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
index 273241654d..0e979e0305 100644
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -378,7 +378,7 @@ fail:
}
static PyObject *
-PySSL_SetError(PySSLSocket *obj, int ret, char *filename, int lineno)
+PySSL_SetError(PySSLSocket *obj, int ret, const char *filename, int lineno)
{
PyObject *type = PySSLErrorObject;
char *errstr = NULL;
@@ -460,7 +460,7 @@ PySSL_SetError(PySSLSocket *obj, int ret, char *filename, int lineno)
}
static PyObject *
-_setSSLError (char *errstr, int errcode, char *filename, int lineno) {
+_setSSLError (const char *errstr, int errcode, const char *filename, int lineno) {
if (errstr == NULL)
errcode = ERR_peek_last_error();
diff --git a/Modules/_struct.c b/Modules/_struct.c
index f965541a5b..df81900d6d 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -723,7 +723,7 @@ np_void_p(char *p, PyObject *v, const formatdef *f)
return 0;
}
-static formatdef native_table[] = {
+static const formatdef native_table[] = {
{'x', sizeof(char), 0, NULL},
{'b', sizeof(char), 0, nu_byte, np_byte},
{'B', sizeof(char), 0, nu_ubyte, np_ubyte},
@@ -1189,7 +1189,7 @@ static formatdef lilendian_table[] = {
static const formatdef *
-whichtable(char **pfmt)
+whichtable(const char **pfmt)
{
const char *fmt = (*pfmt)++; /* May be backed out of later */
switch (*fmt) {
@@ -1268,7 +1268,7 @@ prepare_s(PyStructObject *self)
fmt = PyBytes_AS_STRING(self->s_format);
- f = whichtable((char **)&fmt);
+ f = whichtable(&fmt);
s = fmt;
size = 0;
@@ -1456,7 +1456,7 @@ s_dealloc(PyStructObject *s)
}
static PyObject *
-s_unpack_internal(PyStructObject *soself, char *startfrom) {
+s_unpack_internal(PyStructObject *soself, const char *startfrom) {
formatcode *code;
Py_ssize_t i = 0;
PyObject *result = PyTuple_New(soself->s_len);
@@ -2279,7 +2279,7 @@ PyInit__struct(void)
/* Check endian and swap in faster functions */
{
- formatdef *native = native_table;
+ const formatdef *native = native_table;
formatdef *other, *ptr;
#if PY_LITTLE_ENDIAN
other = lilendian_table;
diff --git a/Modules/_testbuffer.c b/Modules/_testbuffer.c
index 43db8a8e53..13d3cccfa4 100644
--- a/Modules/_testbuffer.c
+++ b/Modules/_testbuffer.c
@@ -13,7 +13,7 @@ PyObject *Struct = NULL;
PyObject *calcsize = NULL;
/* cache simple format string */
-static const char *simple_fmt = "B";
+static const char simple_fmt[] = "B";
PyObject *simple_format = NULL;
#define SIMPLE_FORMAT(fmt) (fmt == NULL || strcmp(fmt, "B") == 0)
#define FIX_FORMAT(fmt) (fmt == NULL ? "B" : fmt)
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 3810e944ce..5d661f7235 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -1001,7 +1001,7 @@ static PyObject *
getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
{
static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
- static char *fmt="(ii)i|(i(ii))(iii)i";
+ static const char fmt[] = "(ii)i|(i(ii))(iii)i";
int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
@@ -1028,6 +1028,21 @@ getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs)
return Py_BuildValue("iii", required, optional, keyword_only);
}
+/* test PyArg_ParseTupleAndKeywords positional-only arguments */
+static PyObject *
+getargs_positional_only_and_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+ static char *keywords[] = {"", "", "keyword", NULL};
+ int required = -1;
+ int optional = -1;
+ int keyword = -1;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii", keywords,
+ &required, &optional, &keyword))
+ return NULL;
+ return Py_BuildValue("iii", required, optional, keyword);
+}
+
/* Functions to call PyArg_ParseTuple with integer format codes,
and return the result.
*/
@@ -1161,7 +1176,7 @@ test_k_code(PyObject *self)
value = PyLong_AsUnsignedLongMask(num);
if (value != ULONG_MAX)
return raiseTestError("test_k_code",
- "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
+ "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
PyTuple_SET_ITEM(tuple, 0, num);
@@ -1180,7 +1195,7 @@ test_k_code(PyObject *self)
value = PyLong_AsUnsignedLongMask(num);
if (value != (unsigned long)-0x42)
return raiseTestError("test_k_code",
- "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
+ "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
PyTuple_SET_ITEM(tuple, 0, num);
@@ -2904,7 +2919,9 @@ run_in_subinterp(PyObject *self, PyObject *args)
static int
check_time_rounding(int round)
{
- if (round != _PyTime_ROUND_FLOOR && round != _PyTime_ROUND_CEILING) {
+ if (round != _PyTime_ROUND_FLOOR
+ && round != _PyTime_ROUND_CEILING
+ && round != _PyTime_ROUND_HALF_EVEN) {
PyErr_SetString(PyExc_ValueError, "invalid rounding");
return -1;
}
@@ -3785,6 +3802,137 @@ get_recursion_depth(PyObject *self, PyObject *args)
return PyLong_FromLong(tstate->recursion_depth - 1);
}
+static PyObject*
+pymem_buffer_overflow(PyObject *self, PyObject *args)
+{
+ char *buffer;
+
+ /* Deliberate buffer overflow to check that PyMem_Free() detects
+ the overflow when debug hooks are installed. */
+ buffer = PyMem_Malloc(16);
+ buffer[16] = 'x';
+ PyMem_Free(buffer);
+
+ Py_RETURN_NONE;
+}
+
+static PyObject*
+pymem_api_misuse(PyObject *self, PyObject *args)
+{
+ char *buffer;
+
+ /* Deliberate misusage of Python allocators:
+ allococate with PyMem but release with PyMem_Raw. */
+ buffer = PyMem_Malloc(16);
+ PyMem_RawFree(buffer);
+
+ Py_RETURN_NONE;
+}
+
+static PyObject*
+pymem_malloc_without_gil(PyObject *self, PyObject *args)
+{
+ char *buffer;
+
+ /* Deliberate bug to test debug hooks on Python memory allocators:
+ call PyMem_Malloc() without holding the GIL */
+ Py_BEGIN_ALLOW_THREADS
+ buffer = PyMem_Malloc(10);
+ Py_END_ALLOW_THREADS
+
+ PyMem_Free(buffer);
+
+ Py_RETURN_NONE;
+}
+
+static PyObject*
+pyobject_malloc_without_gil(PyObject *self, PyObject *args)
+{
+ char *buffer;
+
+ /* Deliberate bug to test debug hooks on Python memory allocators:
+ call PyObject_Malloc() without holding the GIL */
+ Py_BEGIN_ALLOW_THREADS
+ buffer = PyObject_Malloc(10);
+ Py_END_ALLOW_THREADS
+
+ PyObject_Free(buffer);
+
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+tracemalloc_track(PyObject *self, PyObject *args)
+{
+ unsigned int domain;
+ PyObject *ptr_obj;
+ void *ptr;
+ Py_ssize_t size;
+ int release_gil = 0;
+ int res;
+
+ if (!PyArg_ParseTuple(args, "IOn|i", &domain, &ptr_obj, &size, &release_gil))
+ return NULL;
+ ptr = PyLong_AsVoidPtr(ptr_obj);
+ if (PyErr_Occurred())
+ return NULL;
+
+ if (release_gil) {
+ Py_BEGIN_ALLOW_THREADS
+ res = _PyTraceMalloc_Track(domain, (Py_uintptr_t)ptr, size);
+ Py_END_ALLOW_THREADS
+ }
+ else {
+ res = _PyTraceMalloc_Track(domain, (Py_uintptr_t)ptr, size);
+ }
+
+ if (res < 0) {
+ PyErr_SetString(PyExc_RuntimeError, "_PyTraceMalloc_Track error");
+ return NULL;
+ }
+
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+tracemalloc_untrack(PyObject *self, PyObject *args)
+{
+ unsigned int domain;
+ PyObject *ptr_obj;
+ void *ptr;
+ int res;
+
+ if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj))
+ return NULL;
+ ptr = PyLong_AsVoidPtr(ptr_obj);
+ if (PyErr_Occurred())
+ return NULL;
+
+ res = _PyTraceMalloc_Untrack(domain, (Py_uintptr_t)ptr);
+ if (res < 0) {
+ PyErr_SetString(PyExc_RuntimeError, "_PyTraceMalloc_Track error");
+ return NULL;
+ }
+
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+tracemalloc_get_traceback(PyObject *self, PyObject *args)
+{
+ unsigned int domain;
+ PyObject *ptr_obj;
+ void *ptr;
+
+ if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj))
+ return NULL;
+ ptr = PyLong_AsVoidPtr(ptr_obj);
+ if (PyErr_Occurred())
+ return NULL;
+
+ return _PyTraceMalloc_GetTraceback(domain, (Py_uintptr_t)ptr);
+}
+
static PyMethodDef TestMethods[] = {
{"raise_exception", raise_exception, METH_VARARGS},
@@ -3830,6 +3978,9 @@ static PyMethodDef TestMethods[] = {
METH_VARARGS|METH_KEYWORDS},
{"getargs_keyword_only", (PyCFunction)getargs_keyword_only,
METH_VARARGS|METH_KEYWORDS},
+ {"getargs_positional_only_and_keywords",
+ (PyCFunction)getargs_positional_only_and_keywords,
+ METH_VARARGS|METH_KEYWORDS},
{"getargs_b", getargs_b, METH_VARARGS},
{"getargs_B", getargs_B, METH_VARARGS},
{"getargs_h", getargs_h, METH_VARARGS},
@@ -3976,6 +4127,13 @@ static PyMethodDef TestMethods[] = {
{"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS},
{"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS},
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
+ {"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS},
+ {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS},
+ {"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS},
+ {"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS},
+ {"tracemalloc_track", tracemalloc_track, METH_VARARGS},
+ {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS},
+ {"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS},
{NULL, NULL} /* sentinel */
};
@@ -4039,7 +4197,7 @@ test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
"T_LONGLONG", "T_ULONGLONG",
#endif
NULL};
- static char *fmt = "|bbBhHiIlknfds#"
+ static const char fmt[] = "|bbBhHiIlknfds#"
#ifdef HAVE_LONG_LONG
"LK"
#endif
@@ -4394,6 +4552,7 @@ PyInit__testcapi(void)
PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head)));
+ PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t)));
Py_INCREF(&PyInstanceMethod_Type);
PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
diff --git a/Modules/_testmultiphase.c b/Modules/_testmultiphase.c
index 2005205d33..03eda279f7 100644
--- a/Modules/_testmultiphase.c
+++ b/Modules/_testmultiphase.c
@@ -61,7 +61,7 @@ Example_getattro(ExampleObject *self, PyObject *name)
}
static int
-Example_setattr(ExampleObject *self, char *name, PyObject *v)
+Example_setattr(ExampleObject *self, const char *name, PyObject *v)
{
if (self->x_attr == NULL) {
self->x_attr = PyDict_New();
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
index 316f9325b6..42771e3b02 100644
--- a/Modules/_tkinter.c
+++ b/Modules/_tkinter.c
@@ -462,7 +462,7 @@ Split(const char *list)
v = NULL;
break;
}
- PyTuple_SetItem(v, i, w);
+ PyTuple_SET_ITEM(v, i, w);
}
}
Tcl_Free(FREECAST argv);
@@ -480,13 +480,13 @@ SplitObj(PyObject *arg)
Py_ssize_t i, size;
PyObject *elem, *newelem, *result;
- size = PyTuple_Size(arg);
+ size = PyTuple_GET_SIZE(arg);
result = NULL;
/* Recursively invoke SplitObj for all tuple items.
If this does not return a new object, no action is
needed. */
for(i = 0; i < size; i++) {
- elem = PyTuple_GetItem(arg, i);
+ elem = PyTuple_GET_ITEM(arg, i);
newelem = SplitObj(elem);
if (!newelem) {
Py_XDECREF(result);
@@ -502,12 +502,12 @@ SplitObj(PyObject *arg)
if (!result)
return NULL;
for(k = 0; k < i; k++) {
- elem = PyTuple_GetItem(arg, k);
+ elem = PyTuple_GET_ITEM(arg, k);
Py_INCREF(elem);
- PyTuple_SetItem(result, k, elem);
+ PyTuple_SET_ITEM(result, k, elem);
}
}
- PyTuple_SetItem(result, i, newelem);
+ PyTuple_SET_ITEM(result, i, newelem);
}
if (result)
return result;
@@ -529,7 +529,7 @@ SplitObj(PyObject *arg)
Py_XDECREF(result);
return NULL;
}
- PyTuple_SetItem(result, i, newelem);
+ PyTuple_SET_ITEM(result, i, newelem);
}
return result;
}
@@ -551,7 +551,7 @@ SplitObj(PyObject *arg)
else if (PyBytes_Check(arg)) {
int argc;
const char **argv;
- char *list = PyBytes_AsString(arg);
+ char *list = PyBytes_AS_STRING(arg);
if (Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
Py_INCREF(arg);
@@ -559,7 +559,7 @@ SplitObj(PyObject *arg)
}
Tcl_Free(FREECAST argv);
if (argc > 1)
- return Split(PyBytes_AsString(arg));
+ return Split(PyBytes_AS_STRING(arg));
/* Fall through, returning arg. */
}
Py_INCREF(arg);
@@ -758,7 +758,7 @@ Tkapp_New(const char *screenName, const char *className,
}
Tcl_SetVar(v->interp,
"tcl_library",
- PyBytes_AsString(utf8_path),
+ PyBytes_AS_STRING(utf8_path),
TCL_GLOBAL_ONLY);
Py_DECREF(utf8_path);
}
@@ -841,7 +841,7 @@ PyTclObject_dealloc(PyTclObject *self)
Py_DECREF(tp);
}
-static char*
+static const char *
PyTclObject_TclString(PyObject *self)
{
return Tcl_GetString(((PyTclObject*)self)->value);
@@ -1306,7 +1306,7 @@ FromObj(PyObject* tkapp, Tcl_Obj *value)
Py_DECREF(result);
return NULL;
}
- PyTuple_SetItem(result, i, elem);
+ PyTuple_SET_ITEM(result, i, elem);
}
return result;
}
@@ -1517,8 +1517,8 @@ Tkapp_Call(PyObject *selfptr, PyObject *args)
int flags = TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL;
/* If args is a single tuple, replace with contents of tuple */
- if (1 == PyTuple_Size(args)){
- PyObject* item = PyTuple_GetItem(args, 0);
+ if (PyTuple_GET_SIZE(args) == 1) {
+ PyObject *item = PyTuple_GET_ITEM(args, 0);
if (PyTuple_Check(item))
args = item;
}
@@ -1726,14 +1726,14 @@ static int
varname_converter(PyObject *in, void *_out)
{
char *s;
- char **out = (char**)_out;
+ const char **out = (const char**)_out;
if (PyBytes_Check(in)) {
- if (PyBytes_Size(in) > INT_MAX) {
+ if (PyBytes_GET_SIZE(in) > INT_MAX) {
PyErr_SetString(PyExc_OverflowError, "bytes object is too long");
return 0;
}
- s = PyBytes_AsString(in);
- if (strlen(s) != (size_t)PyBytes_Size(in)) {
+ s = PyBytes_AS_STRING(in);
+ if (strlen(s) != (size_t)PyBytes_GET_SIZE(in)) {
PyErr_SetString(PyExc_ValueError, "embedded null byte");
return 0;
}
@@ -1846,7 +1846,7 @@ var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags)
static PyObject *
SetVar(PyObject *self, PyObject *args, int flags)
{
- char *name1, *name2;
+ const char *name1, *name2;
PyObject *newValue;
PyObject *res = NULL;
Tcl_Obj *newval, *ok;
@@ -1915,7 +1915,7 @@ Tkapp_GlobalSetVar(PyObject *self, PyObject *args)
static PyObject *
GetVar(PyObject *self, PyObject *args, int flags)
{
- char *name1, *name2=NULL;
+ const char *name1, *name2=NULL;
PyObject *res = NULL;
Tcl_Obj *tres;
@@ -2274,10 +2274,11 @@ _tkinter_tkapp_splitlist(TkappObject *self, PyObject *arg)
return NULL;
for (i = 0; i < objc; i++) {
PyObject *s = FromObj((PyObject*)self, objv[i]);
- if (!s || PyTuple_SetItem(v, i, s)) {
+ if (!s) {
Py_DECREF(v);
return NULL;
}
+ PyTuple_SET_ITEM(v, i, s);
}
return v;
}
@@ -2304,11 +2305,12 @@ _tkinter_tkapp_splitlist(TkappObject *self, PyObject *arg)
for (i = 0; i < argc; i++) {
PyObject *s = unicodeFromTclString(argv[i]);
- if (!s || PyTuple_SetItem(v, i, s)) {
+ if (!s) {
Py_DECREF(v);
v = NULL;
goto finally;
}
+ PyTuple_SET_ITEM(v, i, s);
}
finally:
@@ -2349,10 +2351,11 @@ _tkinter_tkapp_split(TkappObject *self, PyObject *arg)
return NULL;
for (i = 0; i < objc; i++) {
PyObject *s = FromObj((PyObject*)self, objv[i]);
- if (!s || PyTuple_SetItem(v, i, s)) {
+ if (!s) {
Py_DECREF(v);
return NULL;
}
+ PyTuple_SET_ITEM(v, i, s);
}
return v;
}
@@ -2410,10 +2413,11 @@ PythonCmd(ClientData clientData, Tcl_Interp *interp, int argc, const char *argv[
for (i = 0; i < (argc - 1); i++) {
PyObject *s = unicodeFromTclString(argv[i + 1]);
- if (!s || PyTuple_SetItem(arg, i, s)) {
+ if (!s) {
Py_DECREF(arg);
return PythonCmd_Error(interp);
}
+ PyTuple_SET_ITEM(arg, i, s);
}
res = PyEval_CallObject(func, arg);
Py_DECREF(arg);
@@ -2485,7 +2489,6 @@ Tkapp_CommandProc(CommandEvent *ev, int flags)
/*[clinic input]
_tkinter.tkapp.createcommand
- self: self(type="TkappObject *")
name: str
func: object
/
@@ -2495,7 +2498,7 @@ _tkinter.tkapp.createcommand
static PyObject *
_tkinter_tkapp_createcommand_impl(TkappObject *self, const char *name,
PyObject *func)
-/*[clinic end generated code: output=2a1c79a4ee2af410 input=2bc2c046a0914234]*/
+/*[clinic end generated code: output=2a1c79a4ee2af410 input=255785cb70edc6a0]*/
{
PythonCmd_ClientData *data;
int err;
@@ -2561,7 +2564,6 @@ _tkinter_tkapp_createcommand_impl(TkappObject *self, const char *name,
/*[clinic input]
_tkinter.tkapp.deletecommand
- self: self(type="TkappObject *")
name: str
/
@@ -2569,7 +2571,7 @@ _tkinter.tkapp.deletecommand
static PyObject *
_tkinter_tkapp_deletecommand_impl(TkappObject *self, const char *name)
-/*[clinic end generated code: output=a67e8cb5845e0d2d input=b6306468f10b219c]*/
+/*[clinic end generated code: output=a67e8cb5845e0d2d input=53e9952eae1f85f5]*/
{
int err;
@@ -2762,13 +2764,11 @@ typedef struct {
/*[clinic input]
_tkinter.tktimertoken.deletetimerhandler
- self: self(type="TkttObject *")
-
[clinic start generated code]*/
static PyObject *
_tkinter_tktimertoken_deletetimerhandler_impl(TkttObject *self)
-/*[clinic end generated code: output=bd7fe17f328cfa55 input=25ba5dd594e52084]*/
+/*[clinic end generated code: output=bd7fe17f328cfa55 input=40bd070ff85f5cf3]*/
{
TkttObject *v = self;
PyObject *func = v->func;
@@ -2894,7 +2894,6 @@ _tkinter_tkapp_createtimerhandler_impl(TkappObject *self, int milliseconds,
/*[clinic input]
_tkinter.tkapp.mainloop
- self: self(type="TkappObject *")
threshold: int = 0
/
@@ -2902,7 +2901,7 @@ _tkinter.tkapp.mainloop
static PyObject *
_tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)
-/*[clinic end generated code: output=0ba8eabbe57841b0 input=ad57c9c1dd2b9470]*/
+/*[clinic end generated code: output=0ba8eabbe57841b0 input=036bcdcf03d5eca0]*/
{
#ifdef WITH_THREAD
PyThreadState *tstate = PyThreadState_Get();
@@ -3072,13 +3071,11 @@ Tkapp_WantObjects(PyObject *self, PyObject *args)
/*[clinic input]
_tkinter.tkapp.willdispatch
- self: self(type="TkappObject *")
-
[clinic start generated code]*/
static PyObject *
_tkinter_tkapp_willdispatch_impl(TkappObject *self)
-/*[clinic end generated code: output=0e3f46d244642155 input=2630699767808970]*/
+/*[clinic end generated code: output=0e3f46d244642155 input=d88f5970843d6dab]*/
{
self->dispatching = 1;
@@ -3629,14 +3626,14 @@ PyInit__tkinter(void)
}
}
- Tcl_FindExecutable(PyBytes_AsString(cexe));
+ Tcl_FindExecutable(PyBytes_AS_STRING(cexe));
if (set_var) {
SetEnvironmentVariableW(L"TCL_LIBRARY", NULL);
PyMem_Free(wcs_path);
}
#else
- Tcl_FindExecutable(PyBytes_AsString(cexe));
+ Tcl_FindExecutable(PyBytes_AS_STRING(cexe));
#endif /* MS_WINDOWS */
}
Py_XDECREF(cexe);
diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c
index 796ac0fa33..e3329c722f 100644
--- a/Modules/_tracemalloc.c
+++ b/Modules/_tracemalloc.c
@@ -39,7 +39,11 @@ static struct {
/* limit of the number of frames in a traceback, 1 by default.
Variable protected by the GIL. */
int max_nframe;
-} tracemalloc_config = {TRACEMALLOC_NOT_INITIALIZED, 0, 1};
+
+ /* use domain in trace key?
+ Variable protected by the GIL. */
+ int use_domain;
+} tracemalloc_config = {TRACEMALLOC_NOT_INITIALIZED, 0, 1, 0};
#if defined(TRACE_RAW_MALLOC) && defined(WITH_THREAD)
/* This lock is needed because tracemalloc_free() is called without
@@ -54,10 +58,21 @@ static PyThread_type_lock tables_lock;
# define TABLES_UNLOCK()
#endif
+
+#define DEFAULT_DOMAIN 0
+
+/* Pack the frame_t structure to reduce the memory footprint. */
+typedef struct
+#ifdef __GNUC__
+__attribute__((packed))
+#endif
+{
+ Py_uintptr_t ptr;
+ _PyTraceMalloc_domain_t domain;
+} pointer_t;
+
/* Pack the frame_t structure to reduce the memory footprint on 64-bit
- architectures: 12 bytes instead of 16. This optimization might produce
- SIGBUS on architectures not supporting unaligned memory accesses (64-bit
- MIPS CPU?): on such architecture, the structure must not be packed. */
+ architectures: 12 bytes instead of 16. */
typedef struct
#ifdef __GNUC__
__attribute__((packed))
@@ -65,10 +80,13 @@ __attribute__((packed))
_declspec(align(4))
#endif
{
+ /* filename cannot be NULL: "<unknown>" is used if the Python frame
+ filename is NULL */
PyObject *filename;
unsigned int lineno;
} frame_t;
+
typedef struct {
Py_uhash_t hash;
int nframe;
@@ -81,6 +99,7 @@ typedef struct {
#define MAX_NFRAME \
((INT_MAX - (int)sizeof(traceback_t)) / (int)sizeof(frame_t) + 1)
+
static PyObject *unknown_filename = NULL;
static traceback_t tracemalloc_empty_traceback;
@@ -93,6 +112,7 @@ typedef struct {
traceback_t *traceback;
} trace_t;
+
/* Size in bytes of currently traced memory.
Protected by TABLES_LOCK(). */
static size_t tracemalloc_traced_memory = 0;
@@ -119,6 +139,7 @@ static _Py_hashtable_t *tracemalloc_tracebacks = NULL;
Protected by TABLES_LOCK(). */
static _Py_hashtable_t *tracemalloc_traces = NULL;
+
#ifdef TRACE_DEBUG
static void
tracemalloc_error(const char *format, ...)
@@ -133,6 +154,7 @@ tracemalloc_error(const char *format, ...)
}
#endif
+
#if defined(WITH_THREAD) && defined(TRACE_RAW_MALLOC)
#define REENTRANT_THREADLOCAL
@@ -143,7 +165,7 @@ tracemalloc_error(const char *format, ...)
# error "need native thread local storage (TLS)"
#endif
-static int tracemalloc_reentrant_key;
+static int tracemalloc_reentrant_key = -1;
/* Any non-NULL pointer can be used */
#define REENTRANT Py_True
@@ -151,7 +173,10 @@ static int tracemalloc_reentrant_key;
static int
get_reentrant(void)
{
- void *ptr = PyThread_get_key_value(tracemalloc_reentrant_key);
+ void *ptr;
+
+ assert(tracemalloc_reentrant_key != -1);
+ ptr = PyThread_get_key_value(tracemalloc_reentrant_key);
if (ptr != NULL) {
assert(ptr == REENTRANT);
return 1;
@@ -164,12 +189,14 @@ static void
set_reentrant(int reentrant)
{
assert(reentrant == 0 || reentrant == 1);
+ assert(tracemalloc_reentrant_key != -1);
+
if (reentrant) {
- assert(PyThread_get_key_value(tracemalloc_reentrant_key) == NULL);
+ assert(!get_reentrant());
PyThread_set_key_value(tracemalloc_reentrant_key, REENTRANT);
}
else {
- assert(PyThread_get_key_value(tracemalloc_reentrant_key) == REENTRANT);
+ assert(get_reentrant());
PyThread_set_key_value(tracemalloc_reentrant_key, NULL);
}
}
@@ -194,27 +221,75 @@ set_reentrant(int reentrant)
}
#endif
+
+static Py_uhash_t
+hashtable_hash_pyobject(_Py_hashtable_t *ht, const void *pkey)
+{
+ PyObject *obj;
+
+ _Py_HASHTABLE_READ_KEY(ht, pkey, obj);
+ return PyObject_Hash(obj);
+}
+
+
static int
-hashtable_compare_unicode(const void *key, const _Py_hashtable_entry_t *entry)
+hashtable_compare_unicode(_Py_hashtable_t *ht, const void *pkey,
+ const _Py_hashtable_entry_t *entry)
{
- if (key != NULL && entry->key != NULL)
- return (PyUnicode_Compare((PyObject *)key, (PyObject *)entry->key) == 0);
+ PyObject *key1, *key2;
+
+ _Py_HASHTABLE_READ_KEY(ht, pkey, key1);
+ _Py_HASHTABLE_ENTRY_READ_KEY(ht, entry, key2);
+
+ if (key1 != NULL && key2 != NULL)
+ return (PyUnicode_Compare(key1, key2) == 0);
else
- return key == entry->key;
+ return key1 == key2;
+}
+
+
+static Py_uhash_t
+hashtable_hash_pointer_t(_Py_hashtable_t *ht, const void *pkey)
+{
+ pointer_t ptr;
+ Py_uhash_t hash;
+
+ _Py_HASHTABLE_READ_KEY(ht, pkey, ptr);
+
+ hash = (Py_uhash_t)_Py_HashPointer((void*)ptr.ptr);
+ hash ^= ptr.domain;
+ return hash;
+}
+
+
+static int
+hashtable_compare_pointer_t(_Py_hashtable_t *ht, const void *pkey,
+ const _Py_hashtable_entry_t *entry)
+{
+ pointer_t ptr1, ptr2;
+
+ _Py_HASHTABLE_READ_KEY(ht, pkey, ptr1);
+ _Py_HASHTABLE_ENTRY_READ_KEY(ht, entry, ptr2);
+
+ /* compare pointer before domain, because pointer is more likely to be
+ different */
+ return (ptr1.ptr == ptr2.ptr && ptr1.domain == ptr2.domain);
+
}
-static _Py_hashtable_allocator_t hashtable_alloc = {malloc, free};
static _Py_hashtable_t *
-hashtable_new(size_t data_size,
+hashtable_new(size_t key_size, size_t data_size,
_Py_hashtable_hash_func hash_func,
_Py_hashtable_compare_func compare_func)
{
- return _Py_hashtable_new_full(data_size, 0,
+ _Py_hashtable_allocator_t hashtable_alloc = {malloc, free};
+ return _Py_hashtable_new_full(key_size, data_size, 0,
hash_func, compare_func,
- NULL, NULL, NULL, &hashtable_alloc);
+ &hashtable_alloc);
}
+
static void*
raw_malloc(size_t size)
{
@@ -227,21 +302,28 @@ raw_free(void *ptr)
allocators.raw.free(allocators.raw.ctx, ptr);
}
+
static Py_uhash_t
-hashtable_hash_traceback(const void *key)
+hashtable_hash_traceback(_Py_hashtable_t *ht, const void *pkey)
{
- const traceback_t *traceback = key;
+ traceback_t *traceback;
+
+ _Py_HASHTABLE_READ_KEY(ht, pkey, traceback);
return traceback->hash;
}
+
static int
-hashtable_compare_traceback(const traceback_t *traceback1,
- const _Py_hashtable_entry_t *he)
+hashtable_compare_traceback(_Py_hashtable_t *ht, const void *pkey,
+ const _Py_hashtable_entry_t *entry)
{
- const traceback_t *traceback2 = he->key;
+ traceback_t *traceback1, *traceback2;
const frame_t *frame1, *frame2;
int i;
+ _Py_HASHTABLE_READ_KEY(ht, pkey, traceback1);
+ _Py_HASHTABLE_ENTRY_READ_KEY(ht, entry, traceback2);
+
if (traceback1->nframe != traceback2->nframe)
return 0;
@@ -260,6 +342,7 @@ hashtable_compare_traceback(const traceback_t *traceback1,
return 1;
}
+
static void
tracemalloc_get_frame(PyFrameObject *pyframe, frame_t *frame)
{
@@ -310,15 +393,15 @@ tracemalloc_get_frame(PyFrameObject *pyframe, frame_t *frame)
}
/* intern the filename */
- entry = _Py_hashtable_get_entry(tracemalloc_filenames, filename);
+ entry = _Py_HASHTABLE_GET_ENTRY(tracemalloc_filenames, filename);
if (entry != NULL) {
- filename = (PyObject *)entry->key;
+ _Py_HASHTABLE_ENTRY_READ_KEY(tracemalloc_filenames, entry, filename);
}
else {
/* tracemalloc_filenames is responsible to keep a reference
to the filename */
Py_INCREF(filename);
- if (_Py_hashtable_set(tracemalloc_filenames, filename, NULL, 0) < 0) {
+ if (_Py_HASHTABLE_SET_NODATA(tracemalloc_filenames, filename) < 0) {
Py_DECREF(filename);
#ifdef TRACE_DEBUG
tracemalloc_error("failed to intern the filename");
@@ -331,6 +414,7 @@ tracemalloc_get_frame(PyFrameObject *pyframe, frame_t *frame)
frame->filename = filename;
}
+
static Py_uhash_t
traceback_hash(traceback_t *traceback)
{
@@ -355,6 +439,7 @@ traceback_hash(traceback_t *traceback)
return x;
}
+
static void
traceback_get_frames(traceback_t *traceback)
{
@@ -382,6 +467,7 @@ traceback_get_frames(traceback_t *traceback)
}
}
+
static traceback_t *
traceback_new(void)
{
@@ -401,9 +487,9 @@ traceback_new(void)
traceback->hash = traceback_hash(traceback);
/* intern the traceback */
- entry = _Py_hashtable_get_entry(tracemalloc_tracebacks, traceback);
+ entry = _Py_HASHTABLE_GET_ENTRY(tracemalloc_tracebacks, traceback);
if (entry != NULL) {
- traceback = (traceback_t *)entry->key;
+ _Py_HASHTABLE_ENTRY_READ_KEY(tracemalloc_tracebacks, entry, traceback);
}
else {
traceback_t *copy;
@@ -420,7 +506,7 @@ traceback_new(void)
}
memcpy(copy, traceback, traceback_size);
- if (_Py_hashtable_set(tracemalloc_tracebacks, copy, NULL, 0) < 0) {
+ if (_Py_HASHTABLE_SET_NODATA(tracemalloc_tracebacks, copy) < 0) {
raw_free(copy);
#ifdef TRACE_DEBUG
tracemalloc_error("failed to intern the traceback: putdata failed");
@@ -432,46 +518,154 @@ traceback_new(void)
return traceback;
}
+
static int
-tracemalloc_add_trace(void *ptr, size_t size)
+tracemalloc_use_domain_cb(_Py_hashtable_t *old_traces,
+ _Py_hashtable_entry_t *entry, void *user_data)
{
- traceback_t *traceback;
- trace_t trace;
- int res;
+ Py_uintptr_t ptr;
+ pointer_t key;
+ _Py_hashtable_t *new_traces = (_Py_hashtable_t *)user_data;
+ const void *pdata = _Py_HASHTABLE_ENTRY_PDATA(old_traces, entry);
+
+ _Py_HASHTABLE_ENTRY_READ_KEY(old_traces, entry, ptr);
+ key.ptr = ptr;
+ key.domain = DEFAULT_DOMAIN;
+
+ return _Py_hashtable_set(new_traces,
+ sizeof(key), &key,
+ old_traces->data_size, pdata);
+}
-#ifdef WITH_THREAD
- assert(PyGILState_Check());
-#endif
- traceback = traceback_new();
- if (traceback == NULL)
- return -1;
+/* Convert tracemalloc_traces from compact key (Py_uintptr_t) to pointer_t key.
+ * Return 0 on success, -1 on error. */
+static int
+tracemalloc_use_domain(void)
+{
+ _Py_hashtable_t *new_traces = NULL;
- trace.size = size;
- trace.traceback = traceback;
+ assert(!tracemalloc_config.use_domain);
- res = _Py_HASHTABLE_SET(tracemalloc_traces, ptr, trace);
- if (res == 0) {
- assert(tracemalloc_traced_memory <= PY_SIZE_MAX - size);
- tracemalloc_traced_memory += size;
- if (tracemalloc_traced_memory > tracemalloc_peak_traced_memory)
- tracemalloc_peak_traced_memory = tracemalloc_traced_memory;
+ new_traces = hashtable_new(sizeof(pointer_t),
+ sizeof(trace_t),
+ hashtable_hash_pointer_t,
+ hashtable_compare_pointer_t);
+ if (new_traces == NULL) {
+ return -1;
}
- return res;
+ if (_Py_hashtable_foreach(tracemalloc_traces, tracemalloc_use_domain_cb,
+ new_traces) < 0)
+ {
+ _Py_hashtable_destroy(new_traces);
+ return -1;
+ }
+
+ _Py_hashtable_destroy(tracemalloc_traces);
+ tracemalloc_traces = new_traces;
+
+ tracemalloc_config.use_domain = 1;
+
+ return 0;
}
+
static void
-tracemalloc_remove_trace(void *ptr)
+tracemalloc_remove_trace(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr)
+{
+ trace_t trace;
+ int removed;
+
+ assert(tracemalloc_config.tracing);
+
+ if (tracemalloc_config.use_domain) {
+ pointer_t key = {ptr, domain};
+ removed = _Py_HASHTABLE_POP(tracemalloc_traces, key, trace);
+ }
+ else {
+ removed = _Py_HASHTABLE_POP(tracemalloc_traces, ptr, trace);
+ }
+ if (!removed) {
+ return;
+ }
+
+ assert(tracemalloc_traced_memory >= trace.size);
+ tracemalloc_traced_memory -= trace.size;
+}
+
+#define REMOVE_TRACE(ptr) \
+ tracemalloc_remove_trace(DEFAULT_DOMAIN, (Py_uintptr_t)(ptr))
+
+
+static int
+tracemalloc_add_trace(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr,
+ size_t size)
{
+ pointer_t key = {ptr, domain};
+ traceback_t *traceback;
trace_t trace;
+ _Py_hashtable_entry_t* entry;
+ int res;
- if (_Py_hashtable_pop(tracemalloc_traces, ptr, &trace, sizeof(trace))) {
+ assert(tracemalloc_config.tracing);
+
+ traceback = traceback_new();
+ if (traceback == NULL) {
+ return -1;
+ }
+
+ if (!tracemalloc_config.use_domain && domain != DEFAULT_DOMAIN) {
+ /* first trace using a non-zero domain whereas traces use compact
+ (Py_uintptr_t) keys: switch to pointer_t keys. */
+ if (tracemalloc_use_domain() < 0) {
+ return -1;
+ }
+ }
+
+ if (tracemalloc_config.use_domain) {
+ entry = _Py_HASHTABLE_GET_ENTRY(tracemalloc_traces, key);
+ }
+ else {
+ entry = _Py_HASHTABLE_GET_ENTRY(tracemalloc_traces, ptr);
+ }
+
+ if (entry != NULL) {
+ /* the memory block is already tracked */
+ _Py_HASHTABLE_ENTRY_READ_DATA(tracemalloc_traces, entry, trace);
assert(tracemalloc_traced_memory >= trace.size);
tracemalloc_traced_memory -= trace.size;
+
+ trace.size = size;
+ trace.traceback = traceback;
+ _Py_HASHTABLE_ENTRY_WRITE_DATA(tracemalloc_traces, entry, trace);
}
+ else {
+ trace.size = size;
+ trace.traceback = traceback;
+
+ if (tracemalloc_config.use_domain) {
+ res = _Py_HASHTABLE_SET(tracemalloc_traces, key, trace);
+ }
+ else {
+ res = _Py_HASHTABLE_SET(tracemalloc_traces, ptr, trace);
+ }
+ if (res != 0) {
+ return res;
+ }
+ }
+
+ assert(tracemalloc_traced_memory <= PY_SIZE_MAX - size);
+ tracemalloc_traced_memory += size;
+ if (tracemalloc_traced_memory > tracemalloc_peak_traced_memory)
+ tracemalloc_peak_traced_memory = tracemalloc_traced_memory;
+ return 0;
}
+#define ADD_TRACE(ptr, size) \
+ tracemalloc_add_trace(DEFAULT_DOMAIN, (Py_uintptr_t)(ptr), size)
+
+
static void*
tracemalloc_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize)
{
@@ -488,7 +682,7 @@ tracemalloc_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize)
return NULL;
TABLES_LOCK();
- if (tracemalloc_add_trace(ptr, nelem * elsize) < 0) {
+ if (ADD_TRACE(ptr, nelem * elsize) < 0) {
/* Failed to allocate a trace for the new memory block */
TABLES_UNLOCK();
alloc->free(alloc->ctx, ptr);
@@ -498,6 +692,7 @@ tracemalloc_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize)
return ptr;
}
+
static void*
tracemalloc_realloc(void *ctx, void *ptr, size_t new_size)
{
@@ -512,9 +707,14 @@ tracemalloc_realloc(void *ctx, void *ptr, size_t new_size)
/* an existing memory block has been resized */
TABLES_LOCK();
- tracemalloc_remove_trace(ptr);
- if (tracemalloc_add_trace(ptr2, new_size) < 0) {
+ /* tracemalloc_add_trace() updates the trace if there is already
+ a trace at address (domain, ptr2) */
+ if (ptr2 != ptr) {
+ REMOVE_TRACE(ptr);
+ }
+
+ if (ADD_TRACE(ptr2, new_size) < 0) {
/* Memory allocation failed. The error cannot be reported to
the caller, because realloc() may already have shrinked the
memory block and so removed bytes.
@@ -532,7 +732,7 @@ tracemalloc_realloc(void *ctx, void *ptr, size_t new_size)
/* new allocation */
TABLES_LOCK();
- if (tracemalloc_add_trace(ptr2, new_size) < 0) {
+ if (ADD_TRACE(ptr2, new_size) < 0) {
/* Failed to allocate a trace for the new memory block */
TABLES_UNLOCK();
alloc->free(alloc->ctx, ptr2);
@@ -543,6 +743,7 @@ tracemalloc_realloc(void *ctx, void *ptr, size_t new_size)
return ptr2;
}
+
static void
tracemalloc_free(void *ctx, void *ptr)
{
@@ -557,10 +758,11 @@ tracemalloc_free(void *ctx, void *ptr)
alloc->free(alloc->ctx, ptr);
TABLES_LOCK();
- tracemalloc_remove_trace(ptr);
+ REMOVE_TRACE(ptr);
TABLES_UNLOCK();
}
+
static void*
tracemalloc_alloc_gil(int use_calloc, void *ctx, size_t nelem, size_t elsize)
{
@@ -585,18 +787,21 @@ tracemalloc_alloc_gil(int use_calloc, void *ctx, size_t nelem, size_t elsize)
return ptr;
}
+
static void*
tracemalloc_malloc_gil(void *ctx, size_t size)
{
return tracemalloc_alloc_gil(0, ctx, 1, size);
}
+
static void*
tracemalloc_calloc_gil(void *ctx, size_t nelem, size_t elsize)
{
return tracemalloc_alloc_gil(1, ctx, nelem, elsize);
}
+
static void*
tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size)
{
@@ -612,7 +817,7 @@ tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size)
ptr2 = alloc->realloc(alloc->ctx, ptr, new_size);
if (ptr2 != NULL && ptr != NULL) {
TABLES_LOCK();
- tracemalloc_remove_trace(ptr);
+ REMOVE_TRACE(ptr);
TABLES_UNLOCK();
}
return ptr2;
@@ -629,6 +834,7 @@ tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size)
return ptr2;
}
+
#ifdef TRACE_RAW_MALLOC
static void*
tracemalloc_raw_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize)
@@ -663,18 +869,21 @@ tracemalloc_raw_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize)
return ptr;
}
+
static void*
tracemalloc_raw_malloc(void *ctx, size_t size)
{
return tracemalloc_raw_alloc(0, ctx, 1, size);
}
+
static void*
tracemalloc_raw_calloc(void *ctx, size_t nelem, size_t elsize)
{
return tracemalloc_raw_alloc(1, ctx, nelem, elsize);
}
+
static void*
tracemalloc_raw_realloc(void *ctx, void *ptr, size_t new_size)
{
@@ -691,7 +900,7 @@ tracemalloc_raw_realloc(void *ctx, void *ptr, size_t new_size)
if (ptr2 != NULL && ptr != NULL) {
TABLES_LOCK();
- tracemalloc_remove_trace(ptr);
+ REMOVE_TRACE(ptr);
TABLES_UNLOCK();
}
return ptr2;
@@ -715,22 +924,31 @@ tracemalloc_raw_realloc(void *ctx, void *ptr, size_t new_size)
}
#endif /* TRACE_RAW_MALLOC */
+
static int
-tracemalloc_clear_filename(_Py_hashtable_entry_t *entry, void *user_data)
+tracemalloc_clear_filename(_Py_hashtable_t *ht, _Py_hashtable_entry_t *entry,
+ void *user_data)
{
- PyObject *filename = (PyObject *)entry->key;
+ PyObject *filename;
+
+ _Py_HASHTABLE_ENTRY_READ_KEY(ht, entry, filename);
Py_DECREF(filename);
return 0;
}
+
static int
-traceback_free_traceback(_Py_hashtable_entry_t *entry, void *user_data)
+traceback_free_traceback(_Py_hashtable_t *ht, _Py_hashtable_entry_t *entry,
+ void *user_data)
{
- traceback_t *traceback = (traceback_t *)entry->key;
+ traceback_t *traceback;
+
+ _Py_HASHTABLE_ENTRY_READ_KEY(ht, entry, traceback);
raw_free(traceback);
return 0;
}
+
/* reentrant flag must be set to call this function and GIL must be held */
static void
tracemalloc_clear_traces(void)
@@ -753,6 +971,7 @@ tracemalloc_clear_traces(void)
_Py_hashtable_clear(tracemalloc_filenames);
}
+
static int
tracemalloc_init(void)
{
@@ -789,21 +1008,29 @@ tracemalloc_init(void)
}
#endif
- tracemalloc_filenames = hashtable_new(0,
- (_Py_hashtable_hash_func)PyObject_Hash,
+ tracemalloc_filenames = hashtable_new(sizeof(PyObject *), 0,
+ hashtable_hash_pyobject,
hashtable_compare_unicode);
- tracemalloc_tracebacks = hashtable_new(0,
- (_Py_hashtable_hash_func)hashtable_hash_traceback,
- (_Py_hashtable_compare_func)hashtable_compare_traceback);
+ tracemalloc_tracebacks = hashtable_new(sizeof(traceback_t *), 0,
+ hashtable_hash_traceback,
+ hashtable_compare_traceback);
- tracemalloc_traces = hashtable_new(sizeof(trace_t),
- _Py_hashtable_hash_ptr,
- _Py_hashtable_compare_direct);
+ if (tracemalloc_config.use_domain) {
+ tracemalloc_traces = hashtable_new(sizeof(pointer_t),
+ sizeof(trace_t),
+ hashtable_hash_pointer_t,
+ hashtable_compare_pointer_t);
+ }
+ else {
+ tracemalloc_traces = hashtable_new(sizeof(Py_uintptr_t),
+ sizeof(trace_t),
+ _Py_hashtable_hash_ptr,
+ _Py_hashtable_compare_direct);
+ }
if (tracemalloc_filenames == NULL || tracemalloc_tracebacks == NULL
- || tracemalloc_traces == NULL)
- {
+ || tracemalloc_traces == NULL) {
PyErr_NoMemory();
return -1;
}
@@ -823,6 +1050,7 @@ tracemalloc_init(void)
return 0;
}
+
static void
tracemalloc_deinit(void)
{
@@ -833,9 +1061,9 @@ tracemalloc_deinit(void)
tracemalloc_stop();
/* destroy hash tables */
- _Py_hashtable_destroy(tracemalloc_traces);
_Py_hashtable_destroy(tracemalloc_tracebacks);
_Py_hashtable_destroy(tracemalloc_filenames);
+ _Py_hashtable_destroy(tracemalloc_traces);
#if defined(WITH_THREAD) && defined(TRACE_RAW_MALLOC)
if (tables_lock != NULL) {
@@ -846,11 +1074,13 @@ tracemalloc_deinit(void)
#ifdef REENTRANT_THREADLOCAL
PyThread_delete_key(tracemalloc_reentrant_key);
+ tracemalloc_reentrant_key = -1;
#endif
Py_XDECREF(unknown_filename);
}
+
static int
tracemalloc_start(int max_nframe)
{
@@ -907,6 +1137,7 @@ tracemalloc_start(int max_nframe)
return 0;
}
+
static void
tracemalloc_stop(void)
{
@@ -923,8 +1154,9 @@ tracemalloc_stop(void)
PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &allocators.mem);
PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &allocators.obj);
- /* release memory */
tracemalloc_clear_traces();
+
+ /* release memory */
raw_free(tracemalloc_traceback);
tracemalloc_traceback = NULL;
}
@@ -935,6 +1167,7 @@ PyDoc_STRVAR(tracemalloc_is_tracing_doc,
"True if the tracemalloc module is tracing Python memory allocations,\n"
"False otherwise.");
+
static PyObject*
py_tracemalloc_is_tracing(PyObject *self)
{
@@ -946,6 +1179,7 @@ PyDoc_STRVAR(tracemalloc_clear_traces_doc,
"\n"
"Clear traces of memory blocks allocated by Python.");
+
static PyObject*
py_tracemalloc_clear_traces(PyObject *self)
{
@@ -959,6 +1193,7 @@ py_tracemalloc_clear_traces(PyObject *self)
Py_RETURN_NONE;
}
+
static PyObject*
frame_to_pyobject(frame_t *frame)
{
@@ -968,8 +1203,6 @@ frame_to_pyobject(frame_t *frame)
if (frame_obj == NULL)
return NULL;
- if (frame->filename == NULL)
- frame->filename = Py_None;
Py_INCREF(frame->filename);
PyTuple_SET_ITEM(frame_obj, 0, frame->filename);
@@ -983,6 +1216,7 @@ frame_to_pyobject(frame_t *frame)
return frame_obj;
}
+
static PyObject*
traceback_to_pyobject(traceback_t *traceback, _Py_hashtable_t *intern_table)
{
@@ -1021,33 +1255,43 @@ traceback_to_pyobject(traceback_t *traceback, _Py_hashtable_t *intern_table)
return frames;
}
+
static PyObject*
-trace_to_pyobject(trace_t *trace, _Py_hashtable_t *intern_tracebacks)
+trace_to_pyobject(_PyTraceMalloc_domain_t domain, trace_t *trace,
+ _Py_hashtable_t *intern_tracebacks)
{
PyObject *trace_obj = NULL;
- PyObject *size, *traceback;
+ PyObject *obj;
- trace_obj = PyTuple_New(2);
+ trace_obj = PyTuple_New(3);
if (trace_obj == NULL)
return NULL;
- size = PyLong_FromSize_t(trace->size);
- if (size == NULL) {
+ obj = PyLong_FromSize_t(domain);
+ if (obj == NULL) {
Py_DECREF(trace_obj);
return NULL;
}
- PyTuple_SET_ITEM(trace_obj, 0, size);
+ PyTuple_SET_ITEM(trace_obj, 0, obj);
- traceback = traceback_to_pyobject(trace->traceback, intern_tracebacks);
- if (traceback == NULL) {
+ obj = PyLong_FromSize_t(trace->size);
+ if (obj == NULL) {
Py_DECREF(trace_obj);
return NULL;
}
- PyTuple_SET_ITEM(trace_obj, 1, traceback);
+ PyTuple_SET_ITEM(trace_obj, 1, obj);
+
+ obj = traceback_to_pyobject(trace->traceback, intern_tracebacks);
+ if (obj == NULL) {
+ Py_DECREF(trace_obj);
+ return NULL;
+ }
+ PyTuple_SET_ITEM(trace_obj, 2, obj);
return trace_obj;
}
+
typedef struct {
_Py_hashtable_t *traces;
_Py_hashtable_t *tracebacks;
@@ -1055,16 +1299,26 @@ typedef struct {
} get_traces_t;
static int
-tracemalloc_get_traces_fill(_Py_hashtable_entry_t *entry, void *user_data)
+tracemalloc_get_traces_fill(_Py_hashtable_t *traces, _Py_hashtable_entry_t *entry,
+ void *user_data)
{
get_traces_t *get_traces = user_data;
- trace_t *trace;
+ _PyTraceMalloc_domain_t domain;
+ trace_t trace;
PyObject *tracemalloc_obj;
int res;
- trace = (trace_t *)_Py_HASHTABLE_ENTRY_DATA(entry);
+ if (tracemalloc_config.use_domain) {
+ pointer_t key;
+ _Py_HASHTABLE_ENTRY_READ_KEY(traces, entry, key);
+ domain = key.domain;
+ }
+ else {
+ domain = DEFAULT_DOMAIN;
+ }
+ _Py_HASHTABLE_ENTRY_READ_DATA(traces, entry, trace);
- tracemalloc_obj = trace_to_pyobject(trace, get_traces->tracebacks);
+ tracemalloc_obj = trace_to_pyobject(domain, &trace, get_traces->tracebacks);
if (tracemalloc_obj == NULL)
return 1;
@@ -1076,14 +1330,19 @@ tracemalloc_get_traces_fill(_Py_hashtable_entry_t *entry, void *user_data)
return 0;
}
+
static int
-tracemalloc_pyobject_decref_cb(_Py_hashtable_entry_t *entry, void *user_data)
+tracemalloc_pyobject_decref_cb(_Py_hashtable_t *tracebacks,
+ _Py_hashtable_entry_t *entry,
+ void *user_data)
{
- PyObject *obj = (PyObject *)_Py_HASHTABLE_ENTRY_DATA_AS_VOID_P(entry);
+ PyObject *obj;
+ _Py_HASHTABLE_ENTRY_READ_DATA(tracebacks, entry, obj);
Py_DECREF(obj);
return 0;
}
+
PyDoc_STRVAR(tracemalloc_get_traces_doc,
"_get_traces() -> list\n"
"\n"
@@ -1110,7 +1369,8 @@ py_tracemalloc_get_traces(PyObject *self, PyObject *obj)
/* the traceback hash table is used temporarily to intern traceback tuple
of (filename, lineno) tuples */
- get_traces.tracebacks = hashtable_new(sizeof(PyObject *),
+ get_traces.tracebacks = hashtable_new(sizeof(traceback_t *),
+ sizeof(PyObject *),
_Py_hashtable_hash_ptr,
_Py_hashtable_compare_direct);
if (get_traces.tracebacks == NULL) {
@@ -1142,15 +1402,43 @@ error:
finally:
if (get_traces.tracebacks != NULL) {
_Py_hashtable_foreach(get_traces.tracebacks,
- tracemalloc_pyobject_decref_cb, NULL);
+ tracemalloc_pyobject_decref_cb, NULL);
_Py_hashtable_destroy(get_traces.tracebacks);
}
- if (get_traces.traces != NULL)
+ if (get_traces.traces != NULL) {
_Py_hashtable_destroy(get_traces.traces);
+ }
return get_traces.list;
}
+
+static traceback_t*
+tracemalloc_get_traceback(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr)
+{
+ trace_t trace;
+ int found;
+
+ if (!tracemalloc_config.tracing)
+ return NULL;
+
+ TABLES_LOCK();
+ if (tracemalloc_config.use_domain) {
+ pointer_t key = {ptr, domain};
+ found = _Py_HASHTABLE_GET(tracemalloc_traces, key, trace);
+ }
+ else {
+ found = _Py_HASHTABLE_GET(tracemalloc_traces, ptr, trace);
+ }
+ TABLES_UNLOCK();
+
+ if (!found)
+ return NULL;
+
+ return trace.traceback;
+}
+
+
PyDoc_STRVAR(tracemalloc_get_object_traceback_doc,
"_get_object_traceback(obj)\n"
"\n"
@@ -1165,11 +1453,7 @@ py_tracemalloc_get_object_traceback(PyObject *self, PyObject *obj)
{
PyTypeObject *type;
void *ptr;
- trace_t trace;
- int found;
-
- if (!tracemalloc_config.tracing)
- Py_RETURN_NONE;
+ traceback_t *traceback;
type = Py_TYPE(obj);
if (PyType_IS_GC(type))
@@ -1177,16 +1461,48 @@ py_tracemalloc_get_object_traceback(PyObject *self, PyObject *obj)
else
ptr = (void *)obj;
- TABLES_LOCK();
- found = _Py_HASHTABLE_GET(tracemalloc_traces, ptr, trace);
- TABLES_UNLOCK();
-
- if (!found)
+ traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, (Py_uintptr_t)ptr);
+ if (traceback == NULL)
Py_RETURN_NONE;
- return traceback_to_pyobject(trace.traceback, NULL);
+ return traceback_to_pyobject(traceback, NULL);
}
+
+#define PUTS(fd, str) _Py_write_noraise(fd, str, (int)strlen(str))
+
+static void
+_PyMem_DumpFrame(int fd, frame_t * frame)
+{
+ PUTS(fd, " File \"");
+ _Py_DumpASCII(fd, frame->filename);
+ PUTS(fd, "\", line ");
+ _Py_DumpDecimal(fd, frame->lineno);
+ PUTS(fd, "\n");
+}
+
+/* Dump the traceback where a memory block was allocated into file descriptor
+ fd. The function may block on TABLES_LOCK() but it is unlikely. */
+void
+_PyMem_DumpTraceback(int fd, const void *ptr)
+{
+ traceback_t *traceback;
+ int i;
+
+ traceback = tracemalloc_get_traceback(DEFAULT_DOMAIN, (Py_uintptr_t)ptr);
+ if (traceback == NULL)
+ return;
+
+ PUTS(fd, "Memory block allocated at (most recent call first):\n");
+ for (i=0; i < traceback->nframe; i++) {
+ _PyMem_DumpFrame(fd, &traceback->frames[i]);
+ }
+ PUTS(fd, "\n");
+}
+
+#undef PUTS
+
+
PyDoc_STRVAR(tracemalloc_start_doc,
"start(nframe: int=1)\n"
"\n"
@@ -1222,6 +1538,7 @@ PyDoc_STRVAR(tracemalloc_stop_doc,
"Stop tracing Python memory allocations and clear traces\n"
"of memory blocks allocated by Python.");
+
static PyObject*
py_tracemalloc_stop(PyObject *self)
{
@@ -1229,6 +1546,7 @@ py_tracemalloc_stop(PyObject *self)
Py_RETURN_NONE;
}
+
PyDoc_STRVAR(tracemalloc_get_traceback_limit_doc,
"get_traceback_limit() -> int\n"
"\n"
@@ -1244,6 +1562,7 @@ py_tracemalloc_get_traceback_limit(PyObject *self)
return PyLong_FromLong(tracemalloc_config.max_nframe);
}
+
PyDoc_STRVAR(tracemalloc_get_tracemalloc_memory_doc,
"get_tracemalloc_memory() -> int\n"
"\n"
@@ -1267,6 +1586,7 @@ tracemalloc_get_tracemalloc_memory(PyObject *self)
return Py_BuildValue("N", size_obj);
}
+
PyDoc_STRVAR(tracemalloc_get_traced_memory_doc,
"get_traced_memory() -> (int, int)\n"
"\n"
@@ -1292,6 +1612,7 @@ tracemalloc_get_traced_memory(PyObject *self)
return Py_BuildValue("NN", size_obj, peak_size_obj);
}
+
static PyMethodDef module_methods[] = {
{"is_tracing", (PyCFunction)py_tracemalloc_is_tracing,
METH_NOARGS, tracemalloc_is_tracing_doc},
@@ -1342,6 +1663,7 @@ PyInit__tracemalloc(void)
return m;
}
+
static int
parse_sys_xoptions(PyObject *value)
{
@@ -1370,6 +1692,7 @@ parse_sys_xoptions(PyObject *value)
return Py_SAFE_DOWNCAST(nframe, long, int);
}
+
int
_PyTraceMalloc_Init(void)
{
@@ -1428,6 +1751,7 @@ _PyTraceMalloc_Init(void)
return tracemalloc_start(nframe);
}
+
void
_PyTraceMalloc_Fini(void)
{
@@ -1437,3 +1761,59 @@ _PyTraceMalloc_Fini(void)
tracemalloc_deinit();
}
+int
+_PyTraceMalloc_Track(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr,
+ size_t size)
+{
+ int res;
+#ifdef WITH_THREAD
+ PyGILState_STATE gil_state;
+#endif
+
+ if (!tracemalloc_config.tracing) {
+ /* tracemalloc is not tracing: do nothing */
+ return -2;
+ }
+
+#ifdef WITH_THREAD
+ gil_state = PyGILState_Ensure();
+#endif
+
+ TABLES_LOCK();
+ res = tracemalloc_add_trace(domain, ptr, size);
+ TABLES_UNLOCK();
+
+#ifdef WITH_THREAD
+ PyGILState_Release(gil_state);
+#endif
+ return res;
+}
+
+
+int
+_PyTraceMalloc_Untrack(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr)
+{
+ if (!tracemalloc_config.tracing) {
+ /* tracemalloc is not tracing: do nothing */
+ return -2;
+ }
+
+ TABLES_LOCK();
+ tracemalloc_remove_trace(domain, ptr);
+ TABLES_UNLOCK();
+
+ return 0;
+}
+
+
+PyObject*
+_PyTraceMalloc_GetTraceback(_PyTraceMalloc_domain_t domain, Py_uintptr_t ptr)
+{
+ traceback_t *traceback;
+
+ traceback = tracemalloc_get_traceback(domain, ptr);
+ if (traceback == NULL)
+ Py_RETURN_NONE;
+
+ return traceback_to_pyobject(traceback, NULL);
+}
diff --git a/Modules/_winapi.c b/Modules/_winapi.c
index edc6cf4adb..f4da8aaffe 100644
--- a/Modules/_winapi.c
+++ b/Modules/_winapi.c
@@ -175,7 +175,7 @@ class HANDLE_return_converter(CReturnConverter):
self.declare(data)
self.err_occurred_if("_return_value == INVALID_HANDLE_VALUE", data)
data.return_conversion.append(
- 'if (_return_value == NULL)\n Py_RETURN_NONE;\n')
+ 'if (_return_value == NULL) {\n Py_RETURN_NONE;\n}\n')
data.return_conversion.append(
'return_value = HANDLE_TO_PYNUM(_return_value);\n')
@@ -188,7 +188,7 @@ class DWORD_return_converter(CReturnConverter):
data.return_conversion.append(
'return_value = Py_BuildValue("k", _return_value);\n')
[python start generated code]*/
-/*[python end generated code: output=da39a3ee5e6b4b0d input=374076979596ebba]*/
+/*[python end generated code: output=da39a3ee5e6b4b0d input=94819e72d2c6d558]*/
#include "clinic/_winapi.c.h"
@@ -674,7 +674,7 @@ _winapi_CreatePipe_impl(PyObject *module, PyObject *pipe_attrs, DWORD size)
/* helpers for createprocess */
static unsigned long
-getulong(PyObject* obj, char* name)
+getulong(PyObject* obj, const char* name)
{
PyObject* value;
unsigned long ret;
@@ -690,7 +690,7 @@ getulong(PyObject* obj, char* name)
}
static HANDLE
-gethandle(PyObject* obj, char* name)
+gethandle(PyObject* obj, const char* name)
{
PyObject* value;
HANDLE ret;
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index b9f87aeea3..c785872a5f 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -31,7 +31,7 @@ struct arraydescr {
int itemsize;
PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
- char *formats;
+ const char *formats;
int is_integer_type;
int is_signed;
};
@@ -40,7 +40,7 @@ typedef struct arrayobject {
PyObject_VAR_HEAD
char *ob_item;
Py_ssize_t allocated;
- struct arraydescr *ob_descr;
+ const struct arraydescr *ob_descr;
PyObject *weakreflist; /* List of weak references */
int ob_exports; /* Number of exported buffers */
} arrayobject;
@@ -511,7 +511,7 @@ d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
* Don't forget to update typecode_to_mformat_code() if you add a new
* typecode.
*/
-static struct arraydescr descriptors[] = {
+static const struct arraydescr descriptors[] = {
{'b', 1, b_getitem, b_setitem, "b", 1, 1},
{'B', 1, BB_getitem, BB_setitem, "B", 1, 0},
{'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u", 0, 0},
@@ -539,7 +539,7 @@ class array.array "arrayobject *" "&Arraytype"
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ad43d37e942a8854]*/
static PyObject *
-newarrayobject(PyTypeObject *type, Py_ssize_t size, struct arraydescr *descr)
+newarrayobject(PyTypeObject *type, Py_ssize_t size, const struct arraydescr *descr)
{
arrayobject *op;
size_t nbytes;
@@ -1946,7 +1946,7 @@ array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype,
{
PyObject *converted_items;
PyObject *result;
- struct arraydescr *descr;
+ const struct arraydescr *descr;
if (!PyType_Check(arraytype)) {
PyErr_Format(PyExc_TypeError,
@@ -2084,7 +2084,7 @@ array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype,
Py_ssize_t itemcount = Py_SIZE(items) / mf_descr.size;
const unsigned char *memstr =
(unsigned char *)PyBytes_AS_STRING(items);
- struct arraydescr *descr;
+ const struct arraydescr *descr;
/* If possible, try to pack array's items using a data type
* that fits better. This may result in an array with narrower
@@ -2554,7 +2554,7 @@ array_buffer_getbuf(arrayobject *self, Py_buffer *view, int flags)
view->format = NULL;
view->internal = NULL;
if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
- view->format = self->ob_descr->formats;
+ view->format = (char *)self->ob_descr->formats;
#ifdef Py_UNICODE_WIDE
if (self->ob_descr->typecode == 'u') {
view->format = "w";
@@ -2595,7 +2595,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
int c;
PyObject *initial = NULL, *it = NULL;
- struct arraydescr *descr;
+ const struct arraydescr *descr;
if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
return NULL;
@@ -2875,9 +2875,20 @@ array_iter(arrayobject *ao)
static PyObject *
arrayiter_next(arrayiterobject *it)
{
+ arrayobject *ao;
+
+ assert(it != NULL);
assert(PyArrayIter_Check(it));
- if (it->index < Py_SIZE(it->ao))
- return (*it->getitem)(it->ao, it->index++);
+ ao = it->ao;
+ if (ao == NULL) {
+ return NULL;
+ }
+ assert(array_Check(ao));
+ if (it->index < Py_SIZE(ao)) {
+ return (*it->getitem)(ao, it->index++);
+ }
+ it->ao = NULL;
+ Py_DECREF(ao);
return NULL;
}
@@ -2906,8 +2917,11 @@ static PyObject *
array_arrayiterator___reduce___impl(arrayiterobject *self)
/*[clinic end generated code: output=7898a52e8e66e016 input=a062ea1e9951417a]*/
{
- return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
- self->ao, self->index);
+ PyObject *func = _PyObject_GetBuiltin("iter");
+ if (self->ao == NULL) {
+ return Py_BuildValue("N(())", func);
+ }
+ return Py_BuildValue("N(O)n", func, self->ao, self->index);
}
/*[clinic input]
@@ -2987,7 +3001,7 @@ array_modexec(PyObject *m)
char buffer[Py_ARRAY_LENGTH(descriptors)], *p;
PyObject *typecodes;
Py_ssize_t size = 0;
- struct arraydescr *descr;
+ const struct arraydescr *descr;
if (PyType_Ready(&Arraytype) < 0)
return -1;
diff --git a/Modules/audioop.c b/Modules/audioop.c
index 1e131d2476..8ca64c6956 100644
--- a/Modules/audioop.c
+++ b/Modules/audioop.c
@@ -51,13 +51,15 @@ fbound(double val, double minval, double maxval)
#define SEG_SHIFT (4) /* Left shift for segment number. */
#define SEG_MASK (0x70) /* Segment field mask. */
-static PyInt16 seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF,
- 0x1FF, 0x3FF, 0x7FF, 0xFFF};
-static PyInt16 seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF,
- 0x3FF, 0x7FF, 0xFFF, 0x1FFF};
+static const PyInt16 seg_aend[8] = {
+ 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF
+};
+static const PyInt16 seg_uend[8] = {
+ 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF
+};
static PyInt16
-search(PyInt16 val, PyInt16 *table, int size)
+search(PyInt16 val, const PyInt16 *table, int size)
{
int i;
@@ -70,7 +72,7 @@ search(PyInt16 val, PyInt16 *table, int size)
#define st_ulaw2linear16(uc) (_st_ulaw2linear16[uc])
#define st_alaw2linear16(uc) (_st_alaw2linear16[uc])
-static PyInt16 _st_ulaw2linear16[256] = {
+static const PyInt16 _st_ulaw2linear16[256] = {
-32124, -31100, -30076, -29052, -28028, -27004, -25980,
-24956, -23932, -22908, -21884, -20860, -19836, -18812,
-17788, -16764, -15996, -15484, -14972, -14460, -13948,
@@ -176,7 +178,7 @@ st_14linear2ulaw(PyInt16 pcm_val) /* 2's complement (14-bit range) */
}
-static PyInt16 _st_alaw2linear16[256] = {
+static const PyInt16 _st_alaw2linear16[256] = {
-5504, -5248, -6016, -5760, -4480, -4224, -4992,
-4736, -7552, -7296, -8064, -7808, -6528, -6272,
-7040, -6784, -2752, -2624, -3008, -2880, -2240,
@@ -270,12 +272,12 @@ st_linear2alaw(PyInt16 pcm_val) /* 2's complement (13-bit range) */
/* End of code taken from sox */
/* Intel ADPCM step variation table */
-static int indexTable[16] = {
+static const int indexTable[16] = {
-1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8,
};
-static int stepsizeTable[89] = {
+static const int stepsizeTable[89] = {
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
diff --git a/Modules/binascii.c b/Modules/binascii.c
index 13780b2899..623c298767 100644
--- a/Modules/binascii.c
+++ b/Modules/binascii.c
@@ -74,7 +74,7 @@ static PyObject *Incomplete;
#define SKIP 0x7E
#define FAIL 0x7D
-static unsigned char table_a2b_hqx[256] = {
+static const unsigned char table_a2b_hqx[256] = {
/* ^@ ^A ^B ^C ^D ^E ^F ^G */
/* 0*/ FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
/* \b \t \n ^K ^L \r ^N ^O */
@@ -125,10 +125,10 @@ static unsigned char table_a2b_hqx[256] = {
FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL, FAIL,
};
-static unsigned char table_b2a_hqx[] =
+static const unsigned char table_b2a_hqx[] =
"!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr";
-static char table_a2b_base64[] = {
+static const char table_a2b_base64[] = {
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
@@ -144,12 +144,12 @@ static char table_a2b_base64[] = {
/* Max binary chunk size; limited only by available memory */
#define BASE64_MAXBIN ((PY_SSIZE_T_MAX - 3) / 2)
-static unsigned char table_b2a_base64[] =
+static const unsigned char table_b2a_base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-static unsigned short crctab_hqx[256] = {
+static const unsigned short crctab_hqx[256] = {
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
@@ -256,7 +256,8 @@ static PyObject *
binascii_a2b_uu_impl(PyObject *module, Py_buffer *data)
/*[clinic end generated code: output=e027f8e0b0598742 input=7cafeaf73df63d1c]*/
{
- unsigned char *ascii_data, *bin_data;
+ const unsigned char *ascii_data;
+ unsigned char *bin_data;
int leftbits = 0;
unsigned char this_ch;
unsigned int leftchar = 0;
@@ -342,13 +343,15 @@ static PyObject *
binascii_b2a_uu_impl(PyObject *module, Py_buffer *data)
/*[clinic end generated code: output=0070670e52e4aa6b input=00fdf458ce8b465b]*/
{
- unsigned char *ascii_data, *bin_data;
+ unsigned char *ascii_data;
+ const unsigned char *bin_data;
int leftbits = 0;
unsigned char this_ch;
unsigned int leftchar = 0;
- PyObject *rv;
- Py_ssize_t bin_len;
+ Py_ssize_t bin_len, out_len;
+ _PyBytesWriter writer;
+ _PyBytesWriter_Init(&writer);
bin_data = data->buf;
bin_len = data->len;
if ( bin_len > 45 ) {
@@ -358,9 +361,10 @@ binascii_b2a_uu_impl(PyObject *module, Py_buffer *data)
}
/* We're lazy and allocate to much (fixed up later) */
- if ( (rv=PyBytes_FromStringAndSize(NULL, 2 + (bin_len+2)/3*4)) == NULL )
+ out_len = 2 + (bin_len + 2) / 3 * 4;
+ ascii_data = _PyBytesWriter_Alloc(&writer, out_len);
+ if (ascii_data == NULL)
return NULL;
- ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
/* Store the length */
*ascii_data++ = ' ' + (bin_len & 077);
@@ -382,17 +386,12 @@ binascii_b2a_uu_impl(PyObject *module, Py_buffer *data)
}
*ascii_data++ = '\n'; /* Append a courtesy newline */
- if (_PyBytes_Resize(&rv,
- (ascii_data -
- (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
- Py_CLEAR(rv);
- }
- return rv;
+ return _PyBytesWriter_Finish(&writer, ascii_data);
}
static int
-binascii_find_valid(unsigned char *s, Py_ssize_t slen, int num)
+binascii_find_valid(const unsigned char *s, Py_ssize_t slen, int num)
{
/* Finds & returns the (num+1)th
** valid character for base64, or -1 if none.
@@ -429,13 +428,14 @@ static PyObject *
binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
/*[clinic end generated code: output=0628223f19fd3f9b input=5872acf6e1cac243]*/
{
- unsigned char *ascii_data, *bin_data;
+ const unsigned char *ascii_data;
+ unsigned char *bin_data;
int leftbits = 0;
unsigned char this_ch;
unsigned int leftchar = 0;
- PyObject *rv;
Py_ssize_t ascii_len, bin_len;
int quad_pos = 0;
+ _PyBytesWriter writer;
ascii_data = data->buf;
ascii_len = data->len;
@@ -447,11 +447,12 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
+ _PyBytesWriter_Init(&writer);
+
/* Allocate the buffer */
- if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len)) == NULL )
+ bin_data = _PyBytesWriter_Alloc(&writer, bin_len);
+ if (bin_data == NULL)
return NULL;
- bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
- bin_len = 0;
for( ; ascii_len > 0; ascii_len--, ascii_data++) {
this_ch = *ascii_data;
@@ -496,31 +497,17 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
if ( leftbits >= 8 ) {
leftbits -= 8;
*bin_data++ = (leftchar >> leftbits) & 0xff;
- bin_len++;
leftchar &= ((1 << leftbits) - 1);
}
}
if (leftbits != 0) {
PyErr_SetString(Error, "Incorrect padding");
- Py_DECREF(rv);
+ _PyBytesWriter_Dealloc(&writer);
return NULL;
}
- /* And set string size correctly. If the result string is empty
- ** (because the input was all invalid) return the shared empty
- ** string instead; _PyBytes_Resize() won't do this for us.
- */
- if (bin_len > 0) {
- if (_PyBytes_Resize(&rv, bin_len) < 0) {
- Py_CLEAR(rv);
- }
- }
- else {
- Py_DECREF(rv);
- rv = PyBytes_FromStringAndSize("", 0);
- }
- return rv;
+ return _PyBytesWriter_Finish(&writer, bin_data);
}
@@ -528,24 +515,27 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
binascii.b2a_base64
data: Py_buffer
- /
+ *
+ newline: int(c_default="1") = True
Base64-code line of data.
[clinic start generated code]*/
static PyObject *
-binascii_b2a_base64_impl(PyObject *module, Py_buffer *data)
-/*[clinic end generated code: output=4d96663170778dc3 input=14ec4e47371174a9]*/
+binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, int newline)
+/*[clinic end generated code: output=4ad62c8e8485d3b3 input=7b2ea6fa38d8924c]*/
{
- unsigned char *ascii_data, *bin_data;
+ unsigned char *ascii_data;
+ const unsigned char *bin_data;
int leftbits = 0;
unsigned char this_ch;
unsigned int leftchar = 0;
- PyObject *rv;
- Py_ssize_t bin_len;
+ Py_ssize_t bin_len, out_len;
+ _PyBytesWriter writer;
bin_data = data->buf;
bin_len = data->len;
+ _PyBytesWriter_Init(&writer);
assert(bin_len >= 0);
@@ -555,11 +545,14 @@ binascii_b2a_base64_impl(PyObject *module, Py_buffer *data)
}
/* We're lazy and allocate too much (fixed up later).
- "+3" leaves room for up to two pad characters and a trailing
- newline. Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
- if ( (rv=PyBytes_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL )
+ "+2" leaves room for up to two pad characters.
+ Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
+ out_len = bin_len*2 + 2;
+ if (newline)
+ out_len++;
+ ascii_data = _PyBytesWriter_Alloc(&writer, out_len);
+ if (ascii_data == NULL)
return NULL;
- ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
for( ; bin_len > 0 ; bin_len--, bin_data++ ) {
/* Shift the data into our buffer */
@@ -581,14 +574,10 @@ binascii_b2a_base64_impl(PyObject *module, Py_buffer *data)
*ascii_data++ = table_b2a_base64[(leftchar&0xf) << 2];
*ascii_data++ = BASE64_PAD;
}
- *ascii_data++ = '\n'; /* Append a courtesy newline */
+ if (newline)
+ *ascii_data++ = '\n'; /* Append a courtesy newline */
- if (_PyBytes_Resize(&rv,
- (ascii_data -
- (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
- Py_CLEAR(rv);
- }
- return rv;
+ return _PyBytesWriter_Finish(&writer, ascii_data);
}
/*[clinic input]
@@ -604,16 +593,19 @@ static PyObject *
binascii_a2b_hqx_impl(PyObject *module, Py_buffer *data)
/*[clinic end generated code: output=4d6d8c54d54ea1c1 input=0d914c680e0eed55]*/
{
- unsigned char *ascii_data, *bin_data;
+ const unsigned char *ascii_data;
+ unsigned char *bin_data;
int leftbits = 0;
unsigned char this_ch;
unsigned int leftchar = 0;
- PyObject *rv;
+ PyObject *res;
Py_ssize_t len;
int done = 0;
+ _PyBytesWriter writer;
ascii_data = data->buf;
len = data->len;
+ _PyBytesWriter_Init(&writer);
assert(len >= 0);
@@ -623,9 +615,9 @@ binascii_a2b_hqx_impl(PyObject *module, Py_buffer *data)
/* Allocate a string that is too big (fixed later)
Add two to the initial length to prevent interning which
would preclude subsequent resizing. */
- if ( (rv=PyBytes_FromStringAndSize(NULL, len+2)) == NULL )
+ bin_data = _PyBytesWriter_Alloc(&writer, len + 2);
+ if (bin_data == NULL)
return NULL;
- bin_data = (unsigned char *)PyBytes_AS_STRING(rv);
for( ; len > 0 ; len--, ascii_data++ ) {
/* Get the byte and look it up */
@@ -634,7 +626,7 @@ binascii_a2b_hqx_impl(PyObject *module, Py_buffer *data)
continue;
if ( this_ch == FAIL ) {
PyErr_SetString(Error, "Illegal char");
- Py_DECREF(rv);
+ _PyBytesWriter_Dealloc(&writer);
return NULL;
}
if ( this_ch == DONE ) {
@@ -656,21 +648,14 @@ binascii_a2b_hqx_impl(PyObject *module, Py_buffer *data)
if ( leftbits && !done ) {
PyErr_SetString(Incomplete,
"String has incomplete number of bytes");
- Py_DECREF(rv);
+ _PyBytesWriter_Dealloc(&writer);
return NULL;
}
- if (_PyBytes_Resize(&rv,
- (bin_data -
- (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
- Py_CLEAR(rv);
- }
- if (rv) {
- PyObject *rrv = Py_BuildValue("Oi", rv, done);
- Py_DECREF(rv);
- return rrv;
- }
- return NULL;
+ res = _PyBytesWriter_Finish(&writer, bin_data);
+ if (res == NULL)
+ return NULL;
+ return Py_BuildValue("Ni", res, done);
}
@@ -687,11 +672,13 @@ static PyObject *
binascii_rlecode_hqx_impl(PyObject *module, Py_buffer *data)
/*[clinic end generated code: output=393d79338f5f5629 input=e1f1712447a82b09]*/
{
- unsigned char *in_data, *out_data;
- PyObject *rv;
+ const unsigned char *in_data;
+ unsigned char *out_data;
unsigned char ch;
Py_ssize_t in, inend, len;
+ _PyBytesWriter writer;
+ _PyBytesWriter_Init(&writer);
in_data = data->buf;
len = data->len;
@@ -701,9 +688,9 @@ binascii_rlecode_hqx_impl(PyObject *module, Py_buffer *data)
return PyErr_NoMemory();
/* Worst case: output is twice as big as input (fixed later) */
- if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL )
+ out_data = _PyBytesWriter_Alloc(&writer, len * 2 + 2);
+ if (out_data == NULL)
return NULL;
- out_data = (unsigned char *)PyBytes_AS_STRING(rv);
for( in=0; in<len; in++) {
ch = in_data[in];
@@ -729,12 +716,8 @@ binascii_rlecode_hqx_impl(PyObject *module, Py_buffer *data)
}
}
}
- if (_PyBytes_Resize(&rv,
- (out_data -
- (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
- Py_CLEAR(rv);
- }
- return rv;
+
+ return _PyBytesWriter_Finish(&writer, out_data);
}
@@ -751,15 +734,17 @@ static PyObject *
binascii_b2a_hqx_impl(PyObject *module, Py_buffer *data)
/*[clinic end generated code: output=d0aa5a704bc9f7de input=9596ebe019fe12ba]*/
{
- unsigned char *ascii_data, *bin_data;
+ unsigned char *ascii_data;
+ const unsigned char *bin_data;
int leftbits = 0;
unsigned char this_ch;
unsigned int leftchar = 0;
- PyObject *rv;
Py_ssize_t len;
+ _PyBytesWriter writer;
bin_data = data->buf;
len = data->len;
+ _PyBytesWriter_Init(&writer);
assert(len >= 0);
@@ -767,9 +752,9 @@ binascii_b2a_hqx_impl(PyObject *module, Py_buffer *data)
return PyErr_NoMemory();
/* Allocate a buffer that is at least large enough */
- if ( (rv=PyBytes_FromStringAndSize(NULL, len*2+2)) == NULL )
+ ascii_data = _PyBytesWriter_Alloc(&writer, len * 2 + 2);
+ if (ascii_data == NULL)
return NULL;
- ascii_data = (unsigned char *)PyBytes_AS_STRING(rv);
for( ; len > 0 ; len--, bin_data++ ) {
/* Shift into our buffer, and output any 6bits ready */
@@ -786,12 +771,8 @@ binascii_b2a_hqx_impl(PyObject *module, Py_buffer *data)
leftchar <<= (6-leftbits);
*ascii_data++ = table_b2a_hqx[leftchar & 0x3f];
}
- if (_PyBytes_Resize(&rv,
- (ascii_data -
- (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
- Py_CLEAR(rv);
- }
- return rv;
+
+ return _PyBytesWriter_Finish(&writer, ascii_data);
}
@@ -808,13 +789,15 @@ static PyObject *
binascii_rledecode_hqx_impl(PyObject *module, Py_buffer *data)
/*[clinic end generated code: output=9826619565de1c6c input=54cdd49fc014402c]*/
{
- unsigned char *in_data, *out_data;
+ const unsigned char *in_data;
+ unsigned char *out_data;
unsigned char in_byte, in_repeat;
- PyObject *rv;
- Py_ssize_t in_len, out_len, out_len_left;
+ Py_ssize_t in_len;
+ _PyBytesWriter writer;
in_data = data->buf;
in_len = data->len;
+ _PyBytesWriter_Init(&writer);
assert(in_len >= 0);
@@ -825,59 +808,48 @@ binascii_rledecode_hqx_impl(PyObject *module, Py_buffer *data)
return PyErr_NoMemory();
/* Allocate a buffer of reasonable size. Resized when needed */
- out_len = in_len*2;
- if ( (rv=PyBytes_FromStringAndSize(NULL, out_len)) == NULL )
+ out_data = _PyBytesWriter_Alloc(&writer, in_len);
+ if (out_data == NULL)
return NULL;
- out_len_left = out_len;
- out_data = (unsigned char *)PyBytes_AS_STRING(rv);
+
+ /* Use overallocation */
+ writer.overallocate = 1;
/*
** We need two macros here to get/put bytes and handle
** end-of-buffer for input and output strings.
*/
-#define INBYTE(b) \
- do { \
- if ( --in_len < 0 ) { \
- PyErr_SetString(Incomplete, ""); \
- Py_DECREF(rv); \
- return NULL; \
- } \
- b = *in_data++; \
+#define INBYTE(b) \
+ do { \
+ if ( --in_len < 0 ) { \
+ PyErr_SetString(Incomplete, ""); \
+ goto error; \
+ } \
+ b = *in_data++; \
} while(0)
-#define OUTBYTE(b) \
- do { \
- if ( --out_len_left < 0 ) { \
- if ( out_len > PY_SSIZE_T_MAX / 2) return PyErr_NoMemory(); \
- if (_PyBytes_Resize(&rv, 2*out_len) < 0) \
- { Py_XDECREF(rv); return NULL; } \
- out_data = (unsigned char *)PyBytes_AS_STRING(rv) \
- + out_len; \
- out_len_left = out_len-1; \
- out_len = out_len * 2; \
- } \
- *out_data++ = b; \
- } while(0)
-
- /*
- ** Handle first byte separately (since we have to get angry
- ** in case of an orphaned RLE code).
- */
- INBYTE(in_byte);
+ /*
+ ** Handle first byte separately (since we have to get angry
+ ** in case of an orphaned RLE code).
+ */
+ INBYTE(in_byte);
if (in_byte == RUNCHAR) {
INBYTE(in_repeat);
+ /* only 1 byte will be written, but 2 bytes were preallocated:
+ substract 1 byte to prevent overallocation */
+ writer.min_size--;
+
if (in_repeat != 0) {
/* Note Error, not Incomplete (which is at the end
** of the string only). This is a programmer error.
*/
PyErr_SetString(Error, "Orphaned RLE code at start");
- Py_DECREF(rv);
- return NULL;
+ goto error;
}
- OUTBYTE(RUNCHAR);
+ *out_data++ = RUNCHAR;
} else {
- OUTBYTE(in_byte);
+ *out_data++ = in_byte;
}
while( in_len > 0 ) {
@@ -885,26 +857,39 @@ binascii_rledecode_hqx_impl(PyObject *module, Py_buffer *data)
if (in_byte == RUNCHAR) {
INBYTE(in_repeat);
+ /* only 1 byte will be written, but 2 bytes were preallocated:
+ substract 1 byte to prevent overallocation */
+ writer.min_size--;
+
if ( in_repeat == 0 ) {
/* Just an escaped RUNCHAR value */
- OUTBYTE(RUNCHAR);
+ *out_data++ = RUNCHAR;
} else {
/* Pick up value and output a sequence of it */
in_byte = out_data[-1];
+
+ /* enlarge the buffer if needed */
+ if (in_repeat > 1) {
+ /* -1 because we already preallocated 1 byte */
+ out_data = _PyBytesWriter_Prepare(&writer, out_data,
+ in_repeat - 1);
+ if (out_data == NULL)
+ goto error;
+ }
+
while ( --in_repeat > 0 )
- OUTBYTE(in_byte);
+ *out_data++ = in_byte;
}
} else {
/* Normal byte */
- OUTBYTE(in_byte);
+ *out_data++ = in_byte;
}
}
- if (_PyBytes_Resize(&rv,
- (out_data -
- (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
- Py_CLEAR(rv);
- }
- return rv;
+ return _PyBytesWriter_Finish(&writer, out_data);
+
+error:
+ _PyBytesWriter_Dealloc(&writer);
+ return NULL;
}
@@ -922,7 +907,7 @@ static unsigned int
binascii_crc_hqx_impl(PyObject *module, Py_buffer *data, unsigned int crc)
/*[clinic end generated code: output=8ec2a78590d19170 input=add8c53712ccceda]*/
{
- unsigned char *bin_data;
+ const unsigned char *bin_data;
Py_ssize_t len;
crc &= 0xffff;
@@ -1000,7 +985,7 @@ binascii_crc_hqx_impl(PyObject *module, Py_buffer *data, unsigned int crc)
using byte-swap instructions.
********************************************************************/
-static unsigned int crc_32_tab[256] = {
+static const unsigned int crc_32_tab[256] = {
0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U,
0x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U,
0xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U,
@@ -1073,7 +1058,7 @@ binascii_crc32_impl(PyObject *module, Py_buffer *data, unsigned int crc)
#ifdef USE_ZLIB_CRC32
/* This was taken from zlibmodule.c PyZlib_crc32 (but is PY_SSIZE_T_CLEAN) */
{
- Byte *buf;
+ const Byte *buf;
Py_ssize_t len;
int signed_val;
@@ -1084,7 +1069,7 @@ binascii_crc32_impl(PyObject *module, Py_buffer *data, unsigned int crc)
}
#else /* USE_ZLIB_CRC32 */
{ /* By Jim Ahlstrom; All rights transferred to CNRI */
- unsigned char *bin_data;
+ const unsigned char *bin_data;
Py_ssize_t len;
unsigned int result;
@@ -1167,7 +1152,7 @@ static PyObject *
binascii_a2b_hex_impl(PyObject *module, Py_buffer *hexstr)
/*[clinic end generated code: output=0cc1a139af0eeecb input=9e1e7f2f94db24fd]*/
{
- char* argbuf;
+ const char* argbuf;
Py_ssize_t arglen;
PyObject *retval;
char* retbuf;
@@ -1224,7 +1209,7 @@ binascii_unhexlify_impl(PyObject *module, Py_buffer *hexstr)
return binascii_a2b_hex_impl(module, hexstr);
}
-static int table_hex[128] = {
+static const int table_hex[128] = {
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
@@ -1255,7 +1240,8 @@ binascii_a2b_qp_impl(PyObject *module, Py_buffer *data, int header)
{
Py_ssize_t in, out;
char ch;
- unsigned char *ascii_data, *odata;
+ const unsigned char *ascii_data;
+ unsigned char *odata;
Py_ssize_t datalen = 0;
PyObject *rv;
@@ -1361,13 +1347,14 @@ binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs,
/*[clinic end generated code: output=e9884472ebb1a94c input=7f2a9aaa008e92b2]*/
{
Py_ssize_t in, out;
- unsigned char *databuf, *odata;
+ const unsigned char *databuf;
+ unsigned char *odata;
Py_ssize_t datalen = 0, odatalen = 0;
PyObject *rv;
unsigned int linelen = 0;
unsigned char ch;
int crlf = 0;
- unsigned char *p;
+ const unsigned char *p;
databuf = data->buf;
datalen = data->len;
@@ -1376,7 +1363,7 @@ binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs,
/* XXX: this function has the side effect of converting all of
* the end of lines to be the same depending on this detection
* here */
- p = (unsigned char *) memchr(databuf, '\n', datalen);
+ p = (const unsigned char *) memchr(databuf, '\n', datalen);
if ((p != NULL) && (p > databuf) && (*(p-1) == '\r'))
crlf = 1;
diff --git a/Modules/cjkcodecs/clinic/multibytecodec.c.h b/Modules/cjkcodecs/clinic/multibytecodec.c.h
index 8a47ff88a6..0d8a3e0e0d 100644
--- a/Modules/cjkcodecs/clinic/multibytecodec.c.h
+++ b/Modules/cjkcodecs/clinic/multibytecodec.c.h
@@ -30,8 +30,9 @@ _multibytecodec_MultibyteCodec_encode(MultibyteCodecObject *self, PyObject *args
const char *errors = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|z:encode", _keywords,
- &input, &errors))
+ &input, &errors)) {
goto exit;
+ }
return_value = _multibytecodec_MultibyteCodec_encode_impl(self, input, errors);
exit:
@@ -66,14 +67,16 @@ _multibytecodec_MultibyteCodec_decode(MultibyteCodecObject *self, PyObject *args
const char *errors = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|z:decode", _keywords,
- &input, &errors))
+ &input, &errors)) {
goto exit;
+ }
return_value = _multibytecodec_MultibyteCodec_decode_impl(self, &input, errors);
exit:
/* Cleanup for input */
- if (input.obj)
+ if (input.obj) {
PyBuffer_Release(&input);
+ }
return return_value;
}
@@ -100,8 +103,9 @@ _multibytecodec_MultibyteIncrementalEncoder_encode(MultibyteIncrementalEncoderOb
int final = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:encode", _keywords,
- &input, &final))
+ &input, &final)) {
goto exit;
+ }
return_value = _multibytecodec_MultibyteIncrementalEncoder_encode_impl(self, input, final);
exit:
@@ -147,14 +151,16 @@ _multibytecodec_MultibyteIncrementalDecoder_decode(MultibyteIncrementalDecoderOb
int final = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|i:decode", _keywords,
- &input, &final))
+ &input, &final)) {
goto exit;
+ }
return_value = _multibytecodec_MultibyteIncrementalDecoder_decode_impl(self, &input, final);
exit:
/* Cleanup for input */
- if (input.obj)
+ if (input.obj) {
PyBuffer_Release(&input);
+ }
return return_value;
}
@@ -196,8 +202,9 @@ _multibytecodec_MultibyteStreamReader_read(MultibyteStreamReaderObject *self, Py
if (!PyArg_UnpackTuple(args, "read",
0, 1,
- &sizeobj))
+ &sizeobj)) {
goto exit;
+ }
return_value = _multibytecodec_MultibyteStreamReader_read_impl(self, sizeobj);
exit:
@@ -224,8 +231,9 @@ _multibytecodec_MultibyteStreamReader_readline(MultibyteStreamReaderObject *self
if (!PyArg_UnpackTuple(args, "readline",
0, 1,
- &sizeobj))
+ &sizeobj)) {
goto exit;
+ }
return_value = _multibytecodec_MultibyteStreamReader_readline_impl(self, sizeobj);
exit:
@@ -252,8 +260,9 @@ _multibytecodec_MultibyteStreamReader_readlines(MultibyteStreamReaderObject *sel
if (!PyArg_UnpackTuple(args, "readlines",
0, 1,
- &sizehintobj))
+ &sizehintobj)) {
goto exit;
+ }
return_value = _multibytecodec_MultibyteStreamReader_readlines_impl(self, sizehintobj);
exit:
@@ -317,4 +326,4 @@ PyDoc_STRVAR(_multibytecodec___create_codec__doc__,
#define _MULTIBYTECODEC___CREATE_CODEC_METHODDEF \
{"__create_codec", (PyCFunction)_multibytecodec___create_codec, METH_O, _multibytecodec___create_codec__doc__},
-/*[clinic end generated code: output=eebb21e18c3043d1 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=f837bc56b2fa2a4e input=a9049054013a1b77]*/
diff --git a/Modules/clinic/_bz2module.c.h b/Modules/clinic/_bz2module.c.h
index 3ed8303e95..9451fd3b15 100644
--- a/Modules/clinic/_bz2module.c.h
+++ b/Modules/clinic/_bz2module.c.h
@@ -25,14 +25,16 @@ _bz2_BZ2Compressor_compress(BZ2Compressor *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer data = {NULL, NULL};
- if (!PyArg_Parse(arg, "y*:compress", &data))
+ if (!PyArg_Parse(arg, "y*:compress", &data)) {
goto exit;
+ }
return_value = _bz2_BZ2Compressor_compress_impl(self, &data);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -80,11 +82,13 @@ _bz2_BZ2Compressor___init__(PyObject *self, PyObject *args, PyObject *kwargs)
int compresslevel = 9;
if ((Py_TYPE(self) == &BZ2Compressor_Type) &&
- !_PyArg_NoKeywords("BZ2Compressor", kwargs))
+ !_PyArg_NoKeywords("BZ2Compressor", kwargs)) {
goto exit;
+ }
if (!PyArg_ParseTuple(args, "|i:BZ2Compressor",
- &compresslevel))
+ &compresslevel)) {
goto exit;
+ }
return_value = _bz2_BZ2Compressor___init___impl((BZ2Compressor *)self, compresslevel);
exit:
@@ -126,14 +130,16 @@ _bz2_BZ2Decompressor_decompress(BZ2Decompressor *self, PyObject *args, PyObject
Py_ssize_t max_length = -1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|n:decompress", _keywords,
- &data, &max_length))
+ &data, &max_length)) {
goto exit;
+ }
return_value = _bz2_BZ2Decompressor_decompress_impl(self, &data, max_length);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -155,14 +161,16 @@ _bz2_BZ2Decompressor___init__(PyObject *self, PyObject *args, PyObject *kwargs)
int return_value = -1;
if ((Py_TYPE(self) == &BZ2Decompressor_Type) &&
- !_PyArg_NoPositional("BZ2Decompressor", args))
+ !_PyArg_NoPositional("BZ2Decompressor", args)) {
goto exit;
+ }
if ((Py_TYPE(self) == &BZ2Decompressor_Type) &&
- !_PyArg_NoKeywords("BZ2Decompressor", kwargs))
+ !_PyArg_NoKeywords("BZ2Decompressor", kwargs)) {
goto exit;
+ }
return_value = _bz2_BZ2Decompressor___init___impl((BZ2Decompressor *)self);
exit:
return return_value;
}
-/*[clinic end generated code: output=fef29b76b3314fc7 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=71be22f38224fe84 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/_codecsmodule.c.h b/Modules/clinic/_codecsmodule.c.h
index af06c6d9e0..52c61c597a 100644
--- a/Modules/clinic/_codecsmodule.c.h
+++ b/Modules/clinic/_codecsmodule.c.h
@@ -33,8 +33,9 @@ _codecs_lookup(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
const char *encoding;
- if (!PyArg_Parse(arg, "s:lookup", &encoding))
+ if (!PyArg_Parse(arg, "s:lookup", &encoding)) {
goto exit;
+ }
return_value = _codecs_lookup_impl(module, encoding);
exit:
@@ -70,8 +71,9 @@ _codecs_encode(PyObject *module, PyObject *args, PyObject *kwargs)
const char *errors = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|ss:encode", _keywords,
- &obj, &encoding, &errors))
+ &obj, &encoding, &errors)) {
goto exit;
+ }
return_value = _codecs_encode_impl(module, obj, encoding, errors);
exit:
@@ -107,8 +109,9 @@ _codecs_decode(PyObject *module, PyObject *args, PyObject *kwargs)
const char *errors = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|ss:decode", _keywords,
- &obj, &encoding, &errors))
+ &obj, &encoding, &errors)) {
goto exit;
+ }
return_value = _codecs_decode_impl(module, obj, encoding, errors);
exit:
@@ -133,8 +136,9 @@ _codecs__forget_codec(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
const char *encoding;
- if (!PyArg_Parse(arg, "s:_forget_codec", &encoding))
+ if (!PyArg_Parse(arg, "s:_forget_codec", &encoding)) {
goto exit;
+ }
return_value = _codecs__forget_codec_impl(module, encoding);
exit:
@@ -161,14 +165,16 @@ _codecs_escape_decode(PyObject *module, PyObject *args)
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "s*|z:escape_decode",
- &data, &errors))
+ &data, &errors)) {
goto exit;
+ }
return_value = _codecs_escape_decode_impl(module, &data, errors);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -193,8 +199,9 @@ _codecs_escape_encode(PyObject *module, PyObject *args)
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "O!|z:escape_encode",
- &PyBytes_Type, &data, &errors))
+ &PyBytes_Type, &data, &errors)) {
goto exit;
+ }
return_value = _codecs_escape_encode_impl(module, data, errors);
exit:
@@ -221,8 +228,9 @@ _codecs_unicode_internal_decode(PyObject *module, PyObject *args)
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode",
- &obj, &errors))
+ &obj, &errors)) {
goto exit;
+ }
return_value = _codecs_unicode_internal_decode_impl(module, obj, errors);
exit:
@@ -250,14 +258,16 @@ _codecs_utf_7_decode(PyObject *module, PyObject *args)
int final = 0;
if (!PyArg_ParseTuple(args, "y*|zi:utf_7_decode",
- &data, &errors, &final))
+ &data, &errors, &final)) {
goto exit;
+ }
return_value = _codecs_utf_7_decode_impl(module, &data, errors, final);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -283,14 +293,16 @@ _codecs_utf_8_decode(PyObject *module, PyObject *args)
int final = 0;
if (!PyArg_ParseTuple(args, "y*|zi:utf_8_decode",
- &data, &errors, &final))
+ &data, &errors, &final)) {
goto exit;
+ }
return_value = _codecs_utf_8_decode_impl(module, &data, errors, final);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -316,14 +328,16 @@ _codecs_utf_16_decode(PyObject *module, PyObject *args)
int final = 0;
if (!PyArg_ParseTuple(args, "y*|zi:utf_16_decode",
- &data, &errors, &final))
+ &data, &errors, &final)) {
goto exit;
+ }
return_value = _codecs_utf_16_decode_impl(module, &data, errors, final);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -349,14 +363,16 @@ _codecs_utf_16_le_decode(PyObject *module, PyObject *args)
int final = 0;
if (!PyArg_ParseTuple(args, "y*|zi:utf_16_le_decode",
- &data, &errors, &final))
+ &data, &errors, &final)) {
goto exit;
+ }
return_value = _codecs_utf_16_le_decode_impl(module, &data, errors, final);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -382,14 +398,16 @@ _codecs_utf_16_be_decode(PyObject *module, PyObject *args)
int final = 0;
if (!PyArg_ParseTuple(args, "y*|zi:utf_16_be_decode",
- &data, &errors, &final))
+ &data, &errors, &final)) {
goto exit;
+ }
return_value = _codecs_utf_16_be_decode_impl(module, &data, errors, final);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -417,14 +435,16 @@ _codecs_utf_16_ex_decode(PyObject *module, PyObject *args)
int final = 0;
if (!PyArg_ParseTuple(args, "y*|zii:utf_16_ex_decode",
- &data, &errors, &byteorder, &final))
+ &data, &errors, &byteorder, &final)) {
goto exit;
+ }
return_value = _codecs_utf_16_ex_decode_impl(module, &data, errors, byteorder, final);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -450,14 +470,16 @@ _codecs_utf_32_decode(PyObject *module, PyObject *args)
int final = 0;
if (!PyArg_ParseTuple(args, "y*|zi:utf_32_decode",
- &data, &errors, &final))
+ &data, &errors, &final)) {
goto exit;
+ }
return_value = _codecs_utf_32_decode_impl(module, &data, errors, final);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -483,14 +505,16 @@ _codecs_utf_32_le_decode(PyObject *module, PyObject *args)
int final = 0;
if (!PyArg_ParseTuple(args, "y*|zi:utf_32_le_decode",
- &data, &errors, &final))
+ &data, &errors, &final)) {
goto exit;
+ }
return_value = _codecs_utf_32_le_decode_impl(module, &data, errors, final);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -516,14 +540,16 @@ _codecs_utf_32_be_decode(PyObject *module, PyObject *args)
int final = 0;
if (!PyArg_ParseTuple(args, "y*|zi:utf_32_be_decode",
- &data, &errors, &final))
+ &data, &errors, &final)) {
goto exit;
+ }
return_value = _codecs_utf_32_be_decode_impl(module, &data, errors, final);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -551,14 +577,16 @@ _codecs_utf_32_ex_decode(PyObject *module, PyObject *args)
int final = 0;
if (!PyArg_ParseTuple(args, "y*|zii:utf_32_ex_decode",
- &data, &errors, &byteorder, &final))
+ &data, &errors, &byteorder, &final)) {
goto exit;
+ }
return_value = _codecs_utf_32_ex_decode_impl(module, &data, errors, byteorder, final);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -583,14 +611,16 @@ _codecs_unicode_escape_decode(PyObject *module, PyObject *args)
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "s*|z:unicode_escape_decode",
- &data, &errors))
+ &data, &errors)) {
goto exit;
+ }
return_value = _codecs_unicode_escape_decode_impl(module, &data, errors);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -615,14 +645,16 @@ _codecs_raw_unicode_escape_decode(PyObject *module, PyObject *args)
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "s*|z:raw_unicode_escape_decode",
- &data, &errors))
+ &data, &errors)) {
goto exit;
+ }
return_value = _codecs_raw_unicode_escape_decode_impl(module, &data, errors);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -647,14 +679,16 @@ _codecs_latin_1_decode(PyObject *module, PyObject *args)
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "y*|z:latin_1_decode",
- &data, &errors))
+ &data, &errors)) {
goto exit;
+ }
return_value = _codecs_latin_1_decode_impl(module, &data, errors);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -679,14 +713,16 @@ _codecs_ascii_decode(PyObject *module, PyObject *args)
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "y*|z:ascii_decode",
- &data, &errors))
+ &data, &errors)) {
goto exit;
+ }
return_value = _codecs_ascii_decode_impl(module, &data, errors);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -712,14 +748,16 @@ _codecs_charmap_decode(PyObject *module, PyObject *args)
PyObject *mapping = NULL;
if (!PyArg_ParseTuple(args, "y*|zO:charmap_decode",
- &data, &errors, &mapping))
+ &data, &errors, &mapping)) {
goto exit;
+ }
return_value = _codecs_charmap_decode_impl(module, &data, errors, mapping);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -747,14 +785,16 @@ _codecs_mbcs_decode(PyObject *module, PyObject *args)
int final = 0;
if (!PyArg_ParseTuple(args, "y*|zi:mbcs_decode",
- &data, &errors, &final))
+ &data, &errors, &final)) {
goto exit;
+ }
return_value = _codecs_mbcs_decode_impl(module, &data, errors, final);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -785,14 +825,16 @@ _codecs_code_page_decode(PyObject *module, PyObject *args)
int final = 0;
if (!PyArg_ParseTuple(args, "iy*|zi:code_page_decode",
- &codepage, &data, &errors, &final))
+ &codepage, &data, &errors, &final)) {
goto exit;
+ }
return_value = _codecs_code_page_decode_impl(module, codepage, &data, errors, final);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -819,14 +861,16 @@ _codecs_readbuffer_encode(PyObject *module, PyObject *args)
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "s*|z:readbuffer_encode",
- &data, &errors))
+ &data, &errors)) {
goto exit;
+ }
return_value = _codecs_readbuffer_encode_impl(module, &data, errors);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -851,8 +895,9 @@ _codecs_unicode_internal_encode(PyObject *module, PyObject *args)
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
- &obj, &errors))
+ &obj, &errors)) {
goto exit;
+ }
return_value = _codecs_unicode_internal_encode_impl(module, obj, errors);
exit:
@@ -878,9 +923,10 @@ _codecs_utf_7_encode(PyObject *module, PyObject *args)
PyObject *str;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:utf_7_encode",
- &str, &errors))
+ if (!PyArg_ParseTuple(args, "U|z:utf_7_encode",
+ &str, &errors)) {
goto exit;
+ }
return_value = _codecs_utf_7_encode_impl(module, str, errors);
exit:
@@ -906,9 +952,10 @@ _codecs_utf_8_encode(PyObject *module, PyObject *args)
PyObject *str;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:utf_8_encode",
- &str, &errors))
+ if (!PyArg_ParseTuple(args, "U|z:utf_8_encode",
+ &str, &errors)) {
goto exit;
+ }
return_value = _codecs_utf_8_encode_impl(module, str, errors);
exit:
@@ -935,9 +982,10 @@ _codecs_utf_16_encode(PyObject *module, PyObject *args)
const char *errors = NULL;
int byteorder = 0;
- if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode",
- &str, &errors, &byteorder))
+ if (!PyArg_ParseTuple(args, "U|zi:utf_16_encode",
+ &str, &errors, &byteorder)) {
goto exit;
+ }
return_value = _codecs_utf_16_encode_impl(module, str, errors, byteorder);
exit:
@@ -963,9 +1011,10 @@ _codecs_utf_16_le_encode(PyObject *module, PyObject *args)
PyObject *str;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode",
- &str, &errors))
+ if (!PyArg_ParseTuple(args, "U|z:utf_16_le_encode",
+ &str, &errors)) {
goto exit;
+ }
return_value = _codecs_utf_16_le_encode_impl(module, str, errors);
exit:
@@ -991,9 +1040,10 @@ _codecs_utf_16_be_encode(PyObject *module, PyObject *args)
PyObject *str;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode",
- &str, &errors))
+ if (!PyArg_ParseTuple(args, "U|z:utf_16_be_encode",
+ &str, &errors)) {
goto exit;
+ }
return_value = _codecs_utf_16_be_encode_impl(module, str, errors);
exit:
@@ -1020,9 +1070,10 @@ _codecs_utf_32_encode(PyObject *module, PyObject *args)
const char *errors = NULL;
int byteorder = 0;
- if (!PyArg_ParseTuple(args, "O|zi:utf_32_encode",
- &str, &errors, &byteorder))
+ if (!PyArg_ParseTuple(args, "U|zi:utf_32_encode",
+ &str, &errors, &byteorder)) {
goto exit;
+ }
return_value = _codecs_utf_32_encode_impl(module, str, errors, byteorder);
exit:
@@ -1048,9 +1099,10 @@ _codecs_utf_32_le_encode(PyObject *module, PyObject *args)
PyObject *str;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:utf_32_le_encode",
- &str, &errors))
+ if (!PyArg_ParseTuple(args, "U|z:utf_32_le_encode",
+ &str, &errors)) {
goto exit;
+ }
return_value = _codecs_utf_32_le_encode_impl(module, str, errors);
exit:
@@ -1076,9 +1128,10 @@ _codecs_utf_32_be_encode(PyObject *module, PyObject *args)
PyObject *str;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:utf_32_be_encode",
- &str, &errors))
+ if (!PyArg_ParseTuple(args, "U|z:utf_32_be_encode",
+ &str, &errors)) {
goto exit;
+ }
return_value = _codecs_utf_32_be_encode_impl(module, str, errors);
exit:
@@ -1104,9 +1157,10 @@ _codecs_unicode_escape_encode(PyObject *module, PyObject *args)
PyObject *str;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode",
- &str, &errors))
+ if (!PyArg_ParseTuple(args, "U|z:unicode_escape_encode",
+ &str, &errors)) {
goto exit;
+ }
return_value = _codecs_unicode_escape_encode_impl(module, str, errors);
exit:
@@ -1132,9 +1186,10 @@ _codecs_raw_unicode_escape_encode(PyObject *module, PyObject *args)
PyObject *str;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode",
- &str, &errors))
+ if (!PyArg_ParseTuple(args, "U|z:raw_unicode_escape_encode",
+ &str, &errors)) {
goto exit;
+ }
return_value = _codecs_raw_unicode_escape_encode_impl(module, str, errors);
exit:
@@ -1160,9 +1215,10 @@ _codecs_latin_1_encode(PyObject *module, PyObject *args)
PyObject *str;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:latin_1_encode",
- &str, &errors))
+ if (!PyArg_ParseTuple(args, "U|z:latin_1_encode",
+ &str, &errors)) {
goto exit;
+ }
return_value = _codecs_latin_1_encode_impl(module, str, errors);
exit:
@@ -1188,9 +1244,10 @@ _codecs_ascii_encode(PyObject *module, PyObject *args)
PyObject *str;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:ascii_encode",
- &str, &errors))
+ if (!PyArg_ParseTuple(args, "U|z:ascii_encode",
+ &str, &errors)) {
goto exit;
+ }
return_value = _codecs_ascii_encode_impl(module, str, errors);
exit:
@@ -1217,9 +1274,10 @@ _codecs_charmap_encode(PyObject *module, PyObject *args)
const char *errors = NULL;
PyObject *mapping = NULL;
- if (!PyArg_ParseTuple(args, "O|zO:charmap_encode",
- &str, &errors, &mapping))
+ if (!PyArg_ParseTuple(args, "U|zO:charmap_encode",
+ &str, &errors, &mapping)) {
goto exit;
+ }
return_value = _codecs_charmap_encode_impl(module, str, errors, mapping);
exit:
@@ -1243,8 +1301,9 @@ _codecs_charmap_build(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
PyObject *map;
- if (!PyArg_Parse(arg, "U:charmap_build", &map))
+ if (!PyArg_Parse(arg, "U:charmap_build", &map)) {
goto exit;
+ }
return_value = _codecs_charmap_build_impl(module, map);
exit:
@@ -1271,9 +1330,10 @@ _codecs_mbcs_encode(PyObject *module, PyObject *args)
PyObject *str;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "O|z:mbcs_encode",
- &str, &errors))
+ if (!PyArg_ParseTuple(args, "U|z:mbcs_encode",
+ &str, &errors)) {
goto exit;
+ }
return_value = _codecs_mbcs_encode_impl(module, str, errors);
exit:
@@ -1304,9 +1364,10 @@ _codecs_code_page_encode(PyObject *module, PyObject *args)
PyObject *str;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "iO|z:code_page_encode",
- &code_page, &str, &errors))
+ if (!PyArg_ParseTuple(args, "iU|z:code_page_encode",
+ &code_page, &str, &errors)) {
goto exit;
+ }
return_value = _codecs_code_page_encode_impl(module, code_page, str, errors);
exit:
@@ -1340,8 +1401,9 @@ _codecs_register_error(PyObject *module, PyObject *args)
PyObject *handler;
if (!PyArg_ParseTuple(args, "sO:register_error",
- &errors, &handler))
+ &errors, &handler)) {
goto exit;
+ }
return_value = _codecs_register_error_impl(module, errors, handler);
exit:
@@ -1369,8 +1431,9 @@ _codecs_lookup_error(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
const char *name;
- if (!PyArg_Parse(arg, "s:lookup_error", &name))
+ if (!PyArg_Parse(arg, "s:lookup_error", &name)) {
goto exit;
+ }
return_value = _codecs_lookup_error_impl(module, name);
exit:
@@ -1392,4 +1455,4 @@ exit:
#ifndef _CODECS_CODE_PAGE_ENCODE_METHODDEF
#define _CODECS_CODE_PAGE_ENCODE_METHODDEF
#endif /* !defined(_CODECS_CODE_PAGE_ENCODE_METHODDEF) */
-/*[clinic end generated code: output=42fed94e2ab765ba input=a9049054013a1b77]*/
+/*[clinic end generated code: output=6e89ff4423c12a9b input=a9049054013a1b77]*/
diff --git a/Modules/clinic/_cryptmodule.c.h b/Modules/clinic/_cryptmodule.c.h
index a3c371cbaa..412c6fe8a1 100644
--- a/Modules/clinic/_cryptmodule.c.h
+++ b/Modules/clinic/_cryptmodule.c.h
@@ -27,11 +27,12 @@ crypt_crypt(PyObject *module, PyObject *args)
const char *salt;
if (!PyArg_ParseTuple(args, "ss:crypt",
- &word, &salt))
+ &word, &salt)) {
goto exit;
+ }
return_value = crypt_crypt_impl(module, word, salt);
exit:
return return_value;
}
-/*[clinic end generated code: output=e0493a9691537690 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=8dfc88264e662df4 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/_cursesmodule.c.h b/Modules/clinic/_cursesmodule.c.h
index 5e11742499..62ff1c8ae1 100644
--- a/Modules/clinic/_cursesmodule.c.h
+++ b/Modules/clinic/_cursesmodule.c.h
@@ -40,22 +40,26 @@ curses_window_addch(PyCursesWindowObject *self, PyObject *args)
switch (PyTuple_GET_SIZE(args)) {
case 1:
- if (!PyArg_ParseTuple(args, "O:addch", &ch))
+ if (!PyArg_ParseTuple(args, "O:addch", &ch)) {
goto exit;
+ }
break;
case 2:
- if (!PyArg_ParseTuple(args, "Ol:addch", &ch, &attr))
+ if (!PyArg_ParseTuple(args, "Ol:addch", &ch, &attr)) {
goto exit;
+ }
group_right_1 = 1;
break;
case 3:
- if (!PyArg_ParseTuple(args, "iiO:addch", &y, &x, &ch))
+ if (!PyArg_ParseTuple(args, "iiO:addch", &y, &x, &ch)) {
goto exit;
+ }
group_left_1 = 1;
break;
case 4:
- if (!PyArg_ParseTuple(args, "iiOl:addch", &y, &x, &ch, &attr))
+ if (!PyArg_ParseTuple(args, "iiOl:addch", &y, &x, &ch, &attr)) {
goto exit;
+ }
group_right_1 = 1;
group_left_1 = 1;
break;
@@ -68,4 +72,4 @@ curses_window_addch(PyCursesWindowObject *self, PyObject *args)
exit:
return return_value;
}
-/*[clinic end generated code: output=982b1e709577f3ec input=a9049054013a1b77]*/
+/*[clinic end generated code: output=13ffc5f8d79cbfbf input=a9049054013a1b77]*/
diff --git a/Modules/clinic/_datetimemodule.c.h b/Modules/clinic/_datetimemodule.c.h
index 688c903e2f..cf9860b574 100644
--- a/Modules/clinic/_datetimemodule.c.h
+++ b/Modules/clinic/_datetimemodule.c.h
@@ -27,11 +27,12 @@ datetime_datetime_now(PyTypeObject *type, PyObject *args, PyObject *kwargs)
PyObject *tz = Py_None;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:now", _keywords,
- &tz))
+ &tz)) {
goto exit;
+ }
return_value = datetime_datetime_now_impl(type, tz);
exit:
return return_value;
}
-/*[clinic end generated code: output=7f45c670d6e4953a input=a9049054013a1b77]*/
+/*[clinic end generated code: output=a82e6bd057a5dab9 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/_dbmmodule.c.h b/Modules/clinic/_dbmmodule.c.h
index 49cbceb1ef..06cf7e6741 100644
--- a/Modules/clinic/_dbmmodule.c.h
+++ b/Modules/clinic/_dbmmodule.c.h
@@ -60,8 +60,9 @@ _dbm_dbm_get(dbmobject *self, PyObject *args)
PyObject *default_value = NULL;
if (!PyArg_ParseTuple(args, "s#|O:get",
- &key, &key_length, &default_value))
+ &key, &key_length, &default_value)) {
goto exit;
+ }
return_value = _dbm_dbm_get_impl(self, key, key_length, default_value);
exit:
@@ -93,8 +94,9 @@ _dbm_dbm_setdefault(dbmobject *self, PyObject *args)
PyObject *default_value = NULL;
if (!PyArg_ParseTuple(args, "s#|O:setdefault",
- &key, &key_length, &default_value))
+ &key, &key_length, &default_value)) {
goto exit;
+ }
return_value = _dbm_dbm_setdefault_impl(self, key, key_length, default_value);
exit:
@@ -131,11 +133,12 @@ dbmopen(PyObject *module, PyObject *args)
int mode = 438;
if (!PyArg_ParseTuple(args, "s|si:open",
- &filename, &flags, &mode))
+ &filename, &flags, &mode)) {
goto exit;
+ }
return_value = dbmopen_impl(module, filename, flags, mode);
exit:
return return_value;
}
-/*[clinic end generated code: output=fff12f168cdf8b43 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=99adf966ef0475ff input=a9049054013a1b77]*/
diff --git a/Modules/clinic/_elementtree.c.h b/Modules/clinic/_elementtree.c.h
index 86b4c4cec7..266b92f85f 100644
--- a/Modules/clinic/_elementtree.c.h
+++ b/Modules/clinic/_elementtree.c.h
@@ -19,8 +19,9 @@ _elementtree_Element_append(ElementObject *self, PyObject *arg)
PyObject *return_value = NULL;
PyObject *subelement;
- if (!PyArg_Parse(arg, "O!:append", &Element_Type, &subelement))
+ if (!PyArg_Parse(arg, "O!:append", &Element_Type, &subelement)) {
goto exit;
+ }
return_value = _elementtree_Element_append_impl(self, subelement);
exit:
@@ -87,8 +88,9 @@ _elementtree_Element___sizeof__(ElementObject *self, PyObject *Py_UNUSED(ignored
Py_ssize_t _return_value;
_return_value = _elementtree_Element___sizeof___impl(self);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromSsize_t(_return_value);
exit:
@@ -149,8 +151,9 @@ _elementtree_Element_find(ElementObject *self, PyObject *args, PyObject *kwargs)
PyObject *namespaces = Py_None;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:find", _keywords,
- &path, &namespaces))
+ &path, &namespaces)) {
goto exit;
+ }
return_value = _elementtree_Element_find_impl(self, path, namespaces);
exit:
@@ -180,8 +183,9 @@ _elementtree_Element_findtext(ElementObject *self, PyObject *args, PyObject *kwa
PyObject *namespaces = Py_None;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OO:findtext", _keywords,
- &path, &default_value, &namespaces))
+ &path, &default_value, &namespaces)) {
goto exit;
+ }
return_value = _elementtree_Element_findtext_impl(self, path, default_value, namespaces);
exit:
@@ -209,8 +213,9 @@ _elementtree_Element_findall(ElementObject *self, PyObject *args, PyObject *kwar
PyObject *namespaces = Py_None;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:findall", _keywords,
- &path, &namespaces))
+ &path, &namespaces)) {
goto exit;
+ }
return_value = _elementtree_Element_findall_impl(self, path, namespaces);
exit:
@@ -238,8 +243,9 @@ _elementtree_Element_iterfind(ElementObject *self, PyObject *args, PyObject *kwa
PyObject *namespaces = Py_None;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:iterfind", _keywords,
- &path, &namespaces))
+ &path, &namespaces)) {
goto exit;
+ }
return_value = _elementtree_Element_iterfind_impl(self, path, namespaces);
exit:
@@ -267,8 +273,9 @@ _elementtree_Element_get(ElementObject *self, PyObject *args, PyObject *kwargs)
PyObject *default_value = Py_None;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:get", _keywords,
- &key, &default_value))
+ &key, &default_value)) {
goto exit;
+ }
return_value = _elementtree_Element_get_impl(self, key, default_value);
exit:
@@ -311,8 +318,9 @@ _elementtree_Element_iter(ElementObject *self, PyObject *args, PyObject *kwargs)
PyObject *tag = Py_None;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:iter", _keywords,
- &tag))
+ &tag)) {
goto exit;
+ }
return_value = _elementtree_Element_iter_impl(self, tag);
exit:
@@ -356,8 +364,9 @@ _elementtree_Element_insert(ElementObject *self, PyObject *args)
PyObject *subelement;
if (!PyArg_ParseTuple(args, "nO!:insert",
- &index, &Element_Type, &subelement))
+ &index, &Element_Type, &subelement)) {
goto exit;
+ }
return_value = _elementtree_Element_insert_impl(self, index, subelement);
exit:
@@ -419,8 +428,9 @@ _elementtree_Element_makeelement(ElementObject *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "makeelement",
2, 2,
- &tag, &attrib))
+ &tag, &attrib)) {
goto exit;
+ }
return_value = _elementtree_Element_makeelement_impl(self, tag, attrib);
exit:
@@ -444,8 +454,9 @@ _elementtree_Element_remove(ElementObject *self, PyObject *arg)
PyObject *return_value = NULL;
PyObject *subelement;
- if (!PyArg_Parse(arg, "O!:remove", &Element_Type, &subelement))
+ if (!PyArg_Parse(arg, "O!:remove", &Element_Type, &subelement)) {
goto exit;
+ }
return_value = _elementtree_Element_remove_impl(self, subelement);
exit:
@@ -473,8 +484,9 @@ _elementtree_Element_set(ElementObject *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "set",
2, 2,
- &key, &value))
+ &key, &value)) {
goto exit;
+ }
return_value = _elementtree_Element_set_impl(self, key, value);
exit:
@@ -493,8 +505,9 @@ _elementtree_TreeBuilder___init__(PyObject *self, PyObject *args, PyObject *kwar
PyObject *element_factory = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:TreeBuilder", _keywords,
- &element_factory))
+ &element_factory)) {
goto exit;
+ }
return_value = _elementtree_TreeBuilder___init___impl((TreeBuilderObject *)self, element_factory);
exit:
@@ -555,8 +568,9 @@ _elementtree_TreeBuilder_start(TreeBuilderObject *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "start",
1, 2,
- &tag, &attrs))
+ &tag, &attrs)) {
goto exit;
+ }
return_value = _elementtree_TreeBuilder_start_impl(self, tag, attrs);
exit:
@@ -577,8 +591,9 @@ _elementtree_XMLParser___init__(PyObject *self, PyObject *args, PyObject *kwargs
const char *encoding = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOz:XMLParser", _keywords,
- &html, &target, &encoding))
+ &html, &target, &encoding)) {
goto exit;
+ }
return_value = _elementtree_XMLParser___init___impl((XMLParserObject *)self, html, target, encoding);
exit:
@@ -640,8 +655,9 @@ _elementtree_XMLParser_doctype(XMLParserObject *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "doctype",
3, 3,
- &name, &pubid, &system))
+ &name, &pubid, &system)) {
goto exit;
+ }
return_value = _elementtree_XMLParser_doctype_impl(self, name, pubid, system);
exit:
@@ -668,12 +684,14 @@ _elementtree_XMLParser__setevents(XMLParserObject *self, PyObject *args)
PyObject *events_queue;
PyObject *events_to_report = Py_None;
- if (!PyArg_ParseTuple(args, "O!|O:_setevents",
- &PyList_Type, &events_queue, &events_to_report))
+ if (!PyArg_UnpackTuple(args, "_setevents",
+ 1, 2,
+ &events_queue, &events_to_report)) {
goto exit;
+ }
return_value = _elementtree_XMLParser__setevents_impl(self, events_queue, events_to_report);
exit:
return return_value;
}
-/*[clinic end generated code: output=25b8bf7e7f2151ca input=a9049054013a1b77]*/
+/*[clinic end generated code: output=491eb5718c1ae64b input=a9049054013a1b77]*/
diff --git a/Modules/clinic/_gdbmmodule.c.h b/Modules/clinic/_gdbmmodule.c.h
index 2d87cfc760..fdd589c07a 100644
--- a/Modules/clinic/_gdbmmodule.c.h
+++ b/Modules/clinic/_gdbmmodule.c.h
@@ -23,8 +23,9 @@ _gdbm_gdbm_get(dbmobject *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "get",
1, 2,
- &key, &default_value))
+ &key, &default_value)) {
goto exit;
+ }
return_value = _gdbm_gdbm_get_impl(self, key, default_value);
exit:
@@ -53,8 +54,9 @@ _gdbm_gdbm_setdefault(dbmobject *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "setdefault",
1, 2,
- &key, &default_value))
+ &key, &default_value)) {
goto exit;
+ }
return_value = _gdbm_gdbm_setdefault_impl(self, key, default_value);
exit:
@@ -147,8 +149,9 @@ _gdbm_gdbm_nextkey(dbmobject *self, PyObject *arg)
const char *key;
Py_ssize_clean_t key_length;
- if (!PyArg_Parse(arg, "s#:nextkey", &key, &key_length))
+ if (!PyArg_Parse(arg, "s#:nextkey", &key, &key_length)) {
goto exit;
+ }
return_value = _gdbm_gdbm_nextkey_impl(self, key, key_length);
exit:
@@ -242,11 +245,12 @@ dbmopen(PyObject *module, PyObject *args)
int mode = 438;
if (!PyArg_ParseTuple(args, "s|si:open",
- &name, &flags, &mode))
+ &name, &flags, &mode)) {
goto exit;
+ }
return_value = dbmopen_impl(module, name, flags, mode);
exit:
return return_value;
}
-/*[clinic end generated code: output=9ac7a89858a9765f input=a9049054013a1b77]*/
+/*[clinic end generated code: output=ed0f5d4e3d79b80c input=a9049054013a1b77]*/
diff --git a/Modules/clinic/_lzmamodule.c.h b/Modules/clinic/_lzmamodule.c.h
index f8d38ea1fc..899d5c0e33 100644
--- a/Modules/clinic/_lzmamodule.c.h
+++ b/Modules/clinic/_lzmamodule.c.h
@@ -25,14 +25,16 @@ _lzma_LZMACompressor_compress(Compressor *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer data = {NULL, NULL};
- if (!PyArg_Parse(arg, "y*:compress", &data))
+ if (!PyArg_Parse(arg, "y*:compress", &data)) {
goto exit;
+ }
return_value = _lzma_LZMACompressor_compress_impl(self, &data);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -94,14 +96,16 @@ _lzma_LZMADecompressor_decompress(Decompressor *self, PyObject *args, PyObject *
Py_ssize_t max_length = -1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|n:decompress", _keywords,
- &data, &max_length))
+ &data, &max_length)) {
goto exit;
+ }
return_value = _lzma_LZMADecompressor_decompress_impl(self, &data, max_length);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -143,8 +147,9 @@ _lzma_LZMADecompressor___init__(PyObject *self, PyObject *args, PyObject *kwargs
PyObject *filters = Py_None;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iOO:LZMADecompressor", _keywords,
- &format, &memlimit, &filters))
+ &format, &memlimit, &filters)) {
goto exit;
+ }
return_value = _lzma_LZMADecompressor___init___impl((Decompressor *)self, format, memlimit, filters);
exit:
@@ -171,8 +176,9 @@ _lzma_is_check_supported(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
int check_id;
- if (!PyArg_Parse(arg, "i:is_check_supported", &check_id))
+ if (!PyArg_Parse(arg, "i:is_check_supported", &check_id)) {
goto exit;
+ }
return_value = _lzma_is_check_supported_impl(module, check_id);
exit:
@@ -199,8 +205,9 @@ _lzma__encode_filter_properties(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
lzma_filter filter = {LZMA_VLI_UNKNOWN, NULL};
- if (!PyArg_Parse(arg, "O&:_encode_filter_properties", lzma_filter_converter, &filter))
+ if (!PyArg_Parse(arg, "O&:_encode_filter_properties", lzma_filter_converter, &filter)) {
goto exit;
+ }
return_value = _lzma__encode_filter_properties_impl(module, filter);
exit:
@@ -234,15 +241,17 @@ _lzma__decode_filter_properties(PyObject *module, PyObject *args)
Py_buffer encoded_props = {NULL, NULL};
if (!PyArg_ParseTuple(args, "O&y*:_decode_filter_properties",
- lzma_vli_converter, &filter_id, &encoded_props))
+ lzma_vli_converter, &filter_id, &encoded_props)) {
goto exit;
+ }
return_value = _lzma__decode_filter_properties_impl(module, filter_id, &encoded_props);
exit:
/* Cleanup for encoded_props */
- if (encoded_props.obj)
+ if (encoded_props.obj) {
PyBuffer_Release(&encoded_props);
+ }
return return_value;
}
-/*[clinic end generated code: output=fada06020fd318cc input=a9049054013a1b77]*/
+/*[clinic end generated code: output=25bf57a0845d147a input=a9049054013a1b77]*/
diff --git a/Modules/clinic/_opcode.c.h b/Modules/clinic/_opcode.c.h
index a5f644265a..513cbfdad8 100644
--- a/Modules/clinic/_opcode.c.h
+++ b/Modules/clinic/_opcode.c.h
@@ -23,14 +23,16 @@ _opcode_stack_effect(PyObject *module, PyObject *args)
int _return_value;
if (!PyArg_ParseTuple(args, "i|O:stack_effect",
- &opcode, &oparg))
+ &opcode, &oparg)) {
goto exit;
+ }
_return_value = _opcode_stack_effect_impl(module, opcode, oparg);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong((long)_return_value);
exit:
return return_value;
}
-/*[clinic end generated code: output=984d6de140303d10 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=4d91c6a765097853 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/_pickle.c.h b/Modules/clinic/_pickle.c.h
index bd12d2a0c4..0528615bbe 100644
--- a/Modules/clinic/_pickle.c.h
+++ b/Modules/clinic/_pickle.c.h
@@ -53,8 +53,9 @@ _pickle_Pickler___sizeof__(PicklerObject *self, PyObject *Py_UNUSED(ignored))
Py_ssize_t _return_value;
_return_value = _pickle_Pickler___sizeof___impl(self);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromSsize_t(_return_value);
exit:
@@ -98,8 +99,9 @@ _pickle_Pickler___init__(PyObject *self, PyObject *args, PyObject *kwargs)
int fix_imports = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Op:Pickler", _keywords,
- &file, &protocol, &fix_imports))
+ &file, &protocol, &fix_imports)) {
goto exit;
+ }
return_value = _pickle_Pickler___init___impl((PicklerObject *)self, file, protocol, fix_imports);
exit:
@@ -212,8 +214,9 @@ _pickle_Unpickler_find_class(UnpicklerObject *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "find_class",
2, 2,
- &module_name, &global_name))
+ &module_name, &global_name)) {
goto exit;
+ }
return_value = _pickle_Unpickler_find_class_impl(self, module_name, global_name);
exit:
@@ -239,8 +242,9 @@ _pickle_Unpickler___sizeof__(UnpicklerObject *self, PyObject *Py_UNUSED(ignored)
Py_ssize_t _return_value;
_return_value = _pickle_Unpickler___sizeof___impl(self);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromSsize_t(_return_value);
exit:
@@ -288,8 +292,9 @@ _pickle_Unpickler___init__(PyObject *self, PyObject *args, PyObject *kwargs)
const char *errors = "strict";
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|$pss:Unpickler", _keywords,
- &file, &fix_imports, &encoding, &errors))
+ &file, &fix_imports, &encoding, &errors)) {
goto exit;
+ }
return_value = _pickle_Unpickler___init___impl((UnpicklerObject *)self, file, fix_imports, encoding, errors);
exit:
@@ -394,8 +399,9 @@ _pickle_dump(PyObject *module, PyObject *args, PyObject *kwargs)
int fix_imports = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|O$p:dump", _keywords,
- &obj, &file, &protocol, &fix_imports))
+ &obj, &file, &protocol, &fix_imports)) {
goto exit;
+ }
return_value = _pickle_dump_impl(module, obj, file, protocol, fix_imports);
exit:
@@ -437,8 +443,9 @@ _pickle_dumps(PyObject *module, PyObject *args, PyObject *kwargs)
int fix_imports = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O$p:dumps", _keywords,
- &obj, &protocol, &fix_imports))
+ &obj, &protocol, &fix_imports)) {
goto exit;
+ }
return_value = _pickle_dumps_impl(module, obj, protocol, fix_imports);
exit:
@@ -492,8 +499,9 @@ _pickle_load(PyObject *module, PyObject *args, PyObject *kwargs)
const char *errors = "strict";
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|$pss:load", _keywords,
- &file, &fix_imports, &encoding, &errors))
+ &file, &fix_imports, &encoding, &errors)) {
goto exit;
+ }
return_value = _pickle_load_impl(module, file, fix_imports, encoding, errors);
exit:
@@ -538,11 +546,12 @@ _pickle_loads(PyObject *module, PyObject *args, PyObject *kwargs)
const char *errors = "strict";
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|$pss:loads", _keywords,
- &data, &fix_imports, &encoding, &errors))
+ &data, &fix_imports, &encoding, &errors)) {
goto exit;
+ }
return_value = _pickle_loads_impl(module, data, fix_imports, encoding, errors);
exit:
return return_value;
}
-/*[clinic end generated code: output=93657e55d6a748af input=a9049054013a1b77]*/
+/*[clinic end generated code: output=5c5f9149df292ce4 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/_sre.c.h b/Modules/clinic/_sre.c.h
index 3281717218..2a8b220377 100644
--- a/Modules/clinic/_sre.c.h
+++ b/Modules/clinic/_sre.c.h
@@ -20,8 +20,9 @@ _sre_getcodesize(PyObject *module, PyObject *Py_UNUSED(ignored))
int _return_value;
_return_value = _sre_getcodesize_impl(module);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong((long)_return_value);
exit:
@@ -48,11 +49,13 @@ _sre_getlower(PyObject *module, PyObject *args)
int _return_value;
if (!PyArg_ParseTuple(args, "ii:getlower",
- &character, &flags))
+ &character, &flags)) {
goto exit;
+ }
_return_value = _sre_getlower_impl(module, character, flags);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong((long)_return_value);
exit:
@@ -84,8 +87,9 @@ _sre_SRE_Pattern_match(PatternObject *self, PyObject *args, PyObject *kwargs)
PyObject *pattern = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Onn$O:match", _keywords,
- &string, &pos, &endpos, &pattern))
+ &string, &pos, &endpos, &pattern)) {
goto exit;
+ }
return_value = _sre_SRE_Pattern_match_impl(self, string, pos, endpos, pattern);
exit:
@@ -118,8 +122,9 @@ _sre_SRE_Pattern_fullmatch(PatternObject *self, PyObject *args, PyObject *kwargs
PyObject *pattern = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Onn$O:fullmatch", _keywords,
- &string, &pos, &endpos, &pattern))
+ &string, &pos, &endpos, &pattern)) {
goto exit;
+ }
return_value = _sre_SRE_Pattern_fullmatch_impl(self, string, pos, endpos, pattern);
exit:
@@ -154,8 +159,9 @@ _sre_SRE_Pattern_search(PatternObject *self, PyObject *args, PyObject *kwargs)
PyObject *pattern = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Onn$O:search", _keywords,
- &string, &pos, &endpos, &pattern))
+ &string, &pos, &endpos, &pattern)) {
goto exit;
+ }
return_value = _sre_SRE_Pattern_search_impl(self, string, pos, endpos, pattern);
exit:
@@ -188,8 +194,9 @@ _sre_SRE_Pattern_findall(PatternObject *self, PyObject *args, PyObject *kwargs)
PyObject *source = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Onn$O:findall", _keywords,
- &string, &pos, &endpos, &source))
+ &string, &pos, &endpos, &source)) {
goto exit;
+ }
return_value = _sre_SRE_Pattern_findall_impl(self, string, pos, endpos, source);
exit:
@@ -221,8 +228,9 @@ _sre_SRE_Pattern_finditer(PatternObject *self, PyObject *args, PyObject *kwargs)
Py_ssize_t endpos = PY_SSIZE_T_MAX;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|nn:finditer", _keywords,
- &string, &pos, &endpos))
+ &string, &pos, &endpos)) {
goto exit;
+ }
return_value = _sre_SRE_Pattern_finditer_impl(self, string, pos, endpos);
exit:
@@ -251,8 +259,9 @@ _sre_SRE_Pattern_scanner(PatternObject *self, PyObject *args, PyObject *kwargs)
Py_ssize_t endpos = PY_SSIZE_T_MAX;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|nn:scanner", _keywords,
- &string, &pos, &endpos))
+ &string, &pos, &endpos)) {
goto exit;
+ }
return_value = _sre_SRE_Pattern_scanner_impl(self, string, pos, endpos);
exit:
@@ -282,8 +291,9 @@ _sre_SRE_Pattern_split(PatternObject *self, PyObject *args, PyObject *kwargs)
PyObject *source = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|On$O:split", _keywords,
- &string, &maxsplit, &source))
+ &string, &maxsplit, &source)) {
goto exit;
+ }
return_value = _sre_SRE_Pattern_split_impl(self, string, maxsplit, source);
exit:
@@ -313,8 +323,9 @@ _sre_SRE_Pattern_sub(PatternObject *self, PyObject *args, PyObject *kwargs)
Py_ssize_t count = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|n:sub", _keywords,
- &repl, &string, &count))
+ &repl, &string, &count)) {
goto exit;
+ }
return_value = _sre_SRE_Pattern_sub_impl(self, repl, string, count);
exit:
@@ -344,8 +355,9 @@ _sre_SRE_Pattern_subn(PatternObject *self, PyObject *args, PyObject *kwargs)
Py_ssize_t count = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|n:subn", _keywords,
- &repl, &string, &count))
+ &repl, &string, &count)) {
goto exit;
+ }
return_value = _sre_SRE_Pattern_subn_impl(self, repl, string, count);
exit:
@@ -388,8 +400,9 @@ _sre_SRE_Pattern___deepcopy__(PatternObject *self, PyObject *args, PyObject *kwa
PyObject *memo;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:__deepcopy__", _keywords,
- &memo))
+ &memo)) {
goto exit;
+ }
return_value = _sre_SRE_Pattern___deepcopy___impl(self, memo);
exit:
@@ -423,8 +436,9 @@ _sre_compile(PyObject *module, PyObject *args, PyObject *kwargs)
PyObject *indexgroup;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiO!nOO:compile", _keywords,
- &pattern, &flags, &PyList_Type, &code, &groups, &groupindex, &indexgroup))
+ &pattern, &flags, &PyList_Type, &code, &groups, &groupindex, &indexgroup)) {
goto exit;
+ }
return_value = _sre_compile_impl(module, pattern, flags, code, groups, groupindex, indexgroup);
exit:
@@ -451,8 +465,9 @@ _sre_SRE_Match_expand(MatchObject *self, PyObject *args, PyObject *kwargs)
PyObject *template;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:expand", _keywords,
- &template))
+ &template)) {
goto exit;
+ }
return_value = _sre_SRE_Match_expand_impl(self, template);
exit:
@@ -482,8 +497,9 @@ _sre_SRE_Match_groups(MatchObject *self, PyObject *args, PyObject *kwargs)
PyObject *default_value = Py_None;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:groups", _keywords,
- &default_value))
+ &default_value)) {
goto exit;
+ }
return_value = _sre_SRE_Match_groups_impl(self, default_value);
exit:
@@ -513,8 +529,9 @@ _sre_SRE_Match_groupdict(MatchObject *self, PyObject *args, PyObject *kwargs)
PyObject *default_value = Py_None;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:groupdict", _keywords,
- &default_value))
+ &default_value)) {
goto exit;
+ }
return_value = _sre_SRE_Match_groupdict_impl(self, default_value);
exit:
@@ -542,11 +559,13 @@ _sre_SRE_Match_start(MatchObject *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "start",
0, 1,
- &group))
+ &group)) {
goto exit;
+ }
_return_value = _sre_SRE_Match_start_impl(self, group);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromSsize_t(_return_value);
exit:
@@ -574,11 +593,13 @@ _sre_SRE_Match_end(MatchObject *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "end",
0, 1,
- &group))
+ &group)) {
goto exit;
+ }
_return_value = _sre_SRE_Match_end_impl(self, group);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromSsize_t(_return_value);
exit:
@@ -605,8 +626,9 @@ _sre_SRE_Match_span(MatchObject *self, PyObject *args)
if (!PyArg_UnpackTuple(args, "span",
0, 1,
- &group))
+ &group)) {
goto exit;
+ }
return_value = _sre_SRE_Match_span_impl(self, group);
exit:
@@ -649,8 +671,9 @@ _sre_SRE_Match___deepcopy__(MatchObject *self, PyObject *args, PyObject *kwargs)
PyObject *memo;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:__deepcopy__", _keywords,
- &memo))
+ &memo)) {
goto exit;
+ }
return_value = _sre_SRE_Match___deepcopy___impl(self, memo);
exit:
@@ -690,4 +713,4 @@ _sre_SRE_Scanner_search(ScannerObject *self, PyObject *Py_UNUSED(ignored))
{
return _sre_SRE_Scanner_search_impl(self);
}
-/*[clinic end generated code: output=a4ce9e5b748ce532 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=af9455cb54b2a907 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/_ssl.c.h b/Modules/clinic/_ssl.c.h
index 852e3658b6..fd184d5f0f 100644
--- a/Modules/clinic/_ssl.c.h
+++ b/Modules/clinic/_ssl.c.h
@@ -36,8 +36,9 @@ _ssl__test_decode_cert(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
PyObject *path;
- if (!PyArg_Parse(arg, "O&:_test_decode_cert", PyUnicode_FSConverter, &path))
+ if (!PyArg_Parse(arg, "O&:_test_decode_cert", PyUnicode_FSConverter, &path)) {
goto exit;
+ }
return_value = _ssl__test_decode_cert_impl(module, path);
exit:
@@ -71,8 +72,9 @@ _ssl__SSLSocket_peer_certificate(PySSLSocket *self, PyObject *args)
int binary_mode = 0;
if (!PyArg_ParseTuple(args, "|p:peer_certificate",
- &binary_mode))
+ &binary_mode)) {
goto exit;
+ }
return_value = _ssl__SSLSocket_peer_certificate_impl(self, binary_mode);
exit:
@@ -209,14 +211,16 @@ _ssl__SSLSocket_write(PySSLSocket *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer b = {NULL, NULL};
- if (!PyArg_Parse(arg, "y*:write", &b))
+ if (!PyArg_Parse(arg, "y*:write", &b)) {
goto exit;
+ }
return_value = _ssl__SSLSocket_write_impl(self, &b);
exit:
/* Cleanup for b */
- if (b.obj)
+ if (b.obj) {
PyBuffer_Release(&b);
+ }
return return_value;
}
@@ -260,12 +264,14 @@ _ssl__SSLSocket_read(PySSLSocket *self, PyObject *args)
switch (PyTuple_GET_SIZE(args)) {
case 1:
- if (!PyArg_ParseTuple(args, "i:read", &len))
+ if (!PyArg_ParseTuple(args, "i:read", &len)) {
goto exit;
+ }
break;
case 2:
- if (!PyArg_ParseTuple(args, "iw*:read", &len, &buffer))
+ if (!PyArg_ParseTuple(args, "iw*:read", &len, &buffer)) {
goto exit;
+ }
group_right_1 = 1;
break;
default:
@@ -276,8 +282,9 @@ _ssl__SSLSocket_read(PySSLSocket *self, PyObject *args)
exit:
/* Cleanup for buffer */
- if (buffer.obj)
+ if (buffer.obj) {
PyBuffer_Release(&buffer);
+ }
return return_value;
}
@@ -332,11 +339,13 @@ _ssl__SSLContext(PyTypeObject *type, PyObject *args, PyObject *kwargs)
int proto_version;
if ((type == &PySSLContext_Type) &&
- !_PyArg_NoKeywords("_SSLContext", kwargs))
+ !_PyArg_NoKeywords("_SSLContext", kwargs)) {
goto exit;
+ }
if (!PyArg_ParseTuple(args, "i:_SSLContext",
- &proto_version))
+ &proto_version)) {
goto exit;
+ }
return_value = _ssl__SSLContext_impl(type, proto_version);
exit:
@@ -360,8 +369,9 @@ _ssl__SSLContext_set_ciphers(PySSLContext *self, PyObject *arg)
PyObject *return_value = NULL;
const char *cipherlist;
- if (!PyArg_Parse(arg, "s:set_ciphers", &cipherlist))
+ if (!PyArg_Parse(arg, "s:set_ciphers", &cipherlist)) {
goto exit;
+ }
return_value = _ssl__SSLContext_set_ciphers_impl(self, cipherlist);
exit:
@@ -386,14 +396,16 @@ _ssl__SSLContext__set_npn_protocols(PySSLContext *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer protos = {NULL, NULL};
- if (!PyArg_Parse(arg, "y*:_set_npn_protocols", &protos))
+ if (!PyArg_Parse(arg, "y*:_set_npn_protocols", &protos)) {
goto exit;
+ }
return_value = _ssl__SSLContext__set_npn_protocols_impl(self, &protos);
exit:
/* Cleanup for protos */
- if (protos.obj)
+ if (protos.obj) {
PyBuffer_Release(&protos);
+ }
return return_value;
}
@@ -416,14 +428,16 @@ _ssl__SSLContext__set_alpn_protocols(PySSLContext *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer protos = {NULL, NULL};
- if (!PyArg_Parse(arg, "y*:_set_alpn_protocols", &protos))
+ if (!PyArg_Parse(arg, "y*:_set_alpn_protocols", &protos)) {
goto exit;
+ }
return_value = _ssl__SSLContext__set_alpn_protocols_impl(self, &protos);
exit:
/* Cleanup for protos */
- if (protos.obj)
+ if (protos.obj) {
PyBuffer_Release(&protos);
+ }
return return_value;
}
@@ -450,8 +464,9 @@ _ssl__SSLContext_load_cert_chain(PySSLContext *self, PyObject *args, PyObject *k
PyObject *password = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OO:load_cert_chain", _keywords,
- &certfile, &keyfile, &password))
+ &certfile, &keyfile, &password)) {
goto exit;
+ }
return_value = _ssl__SSLContext_load_cert_chain_impl(self, certfile, keyfile, password);
exit:
@@ -482,8 +497,9 @@ _ssl__SSLContext_load_verify_locations(PySSLContext *self, PyObject *args, PyObj
PyObject *cadata = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOO:load_verify_locations", _keywords,
- &cafile, &capath, &cadata))
+ &cafile, &capath, &cadata)) {
goto exit;
+ }
return_value = _ssl__SSLContext_load_verify_locations_impl(self, cafile, capath, cadata);
exit:
@@ -520,8 +536,9 @@ _ssl__SSLContext__wrap_socket(PySSLContext *self, PyObject *args, PyObject *kwar
PyObject *hostname_obj = Py_None;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!i|O:_wrap_socket", _keywords,
- PySocketModule.Sock_Type, &sock, &server_side, &hostname_obj))
+ PySocketModule.Sock_Type, &sock, &server_side, &hostname_obj)) {
goto exit;
+ }
return_value = _ssl__SSLContext__wrap_socket_impl(self, sock, server_side, hostname_obj);
exit:
@@ -553,8 +570,9 @@ _ssl__SSLContext__wrap_bio(PySSLContext *self, PyObject *args, PyObject *kwargs)
PyObject *hostname_obj = Py_None;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!O!i|O:_wrap_bio", _keywords,
- &PySSLMemoryBIO_Type, &incoming, &PySSLMemoryBIO_Type, &outgoing, &server_side, &hostname_obj))
+ &PySSLMemoryBIO_Type, &incoming, &PySSLMemoryBIO_Type, &outgoing, &server_side, &hostname_obj)) {
goto exit;
+ }
return_value = _ssl__SSLContext__wrap_bio_impl(self, incoming, outgoing, server_side, hostname_obj);
exit:
@@ -670,8 +688,9 @@ _ssl__SSLContext_get_ca_certs(PySSLContext *self, PyObject *args, PyObject *kwar
int binary_form = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|p:get_ca_certs", _keywords,
- &binary_form))
+ &binary_form)) {
goto exit;
+ }
return_value = _ssl__SSLContext_get_ca_certs_impl(self, binary_form);
exit:
@@ -687,11 +706,13 @@ _ssl_MemoryBIO(PyTypeObject *type, PyObject *args, PyObject *kwargs)
PyObject *return_value = NULL;
if ((type == &PySSLMemoryBIO_Type) &&
- !_PyArg_NoPositional("MemoryBIO", args))
+ !_PyArg_NoPositional("MemoryBIO", args)) {
goto exit;
+ }
if ((type == &PySSLMemoryBIO_Type) &&
- !_PyArg_NoKeywords("MemoryBIO", kwargs))
+ !_PyArg_NoKeywords("MemoryBIO", kwargs)) {
goto exit;
+ }
return_value = _ssl_MemoryBIO_impl(type);
exit:
@@ -722,8 +743,9 @@ _ssl_MemoryBIO_read(PySSLMemoryBIO *self, PyObject *args)
int len = -1;
if (!PyArg_ParseTuple(args, "|i:read",
- &len))
+ &len)) {
goto exit;
+ }
return_value = _ssl_MemoryBIO_read_impl(self, len);
exit:
@@ -750,14 +772,16 @@ _ssl_MemoryBIO_write(PySSLMemoryBIO *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer b = {NULL, NULL};
- if (!PyArg_Parse(arg, "y*:write", &b))
+ if (!PyArg_Parse(arg, "y*:write", &b)) {
goto exit;
+ }
return_value = _ssl_MemoryBIO_write_impl(self, &b);
exit:
/* Cleanup for b */
- if (b.obj)
+ if (b.obj) {
PyBuffer_Release(&b);
+ }
return return_value;
}
@@ -805,14 +829,16 @@ _ssl_RAND_add(PyObject *module, PyObject *args)
double entropy;
if (!PyArg_ParseTuple(args, "s*d:RAND_add",
- &view, &entropy))
+ &view, &entropy)) {
goto exit;
+ }
return_value = _ssl_RAND_add_impl(module, &view, entropy);
exit:
/* Cleanup for view */
- if (view.obj)
+ if (view.obj) {
PyBuffer_Release(&view);
+ }
return return_value;
}
@@ -835,8 +861,9 @@ _ssl_RAND_bytes(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
int n;
- if (!PyArg_Parse(arg, "i:RAND_bytes", &n))
+ if (!PyArg_Parse(arg, "i:RAND_bytes", &n)) {
goto exit;
+ }
return_value = _ssl_RAND_bytes_impl(module, n);
exit:
@@ -864,8 +891,9 @@ _ssl_RAND_pseudo_bytes(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
int n;
- if (!PyArg_Parse(arg, "i:RAND_pseudo_bytes", &n))
+ if (!PyArg_Parse(arg, "i:RAND_pseudo_bytes", &n)) {
goto exit;
+ }
return_value = _ssl_RAND_pseudo_bytes_impl(module, n);
exit:
@@ -916,8 +944,9 @@ _ssl_RAND_egd(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
PyObject *path;
- if (!PyArg_Parse(arg, "O&:RAND_egd", PyUnicode_FSConverter, &path))
+ if (!PyArg_Parse(arg, "O&:RAND_egd", PyUnicode_FSConverter, &path)) {
goto exit;
+ }
return_value = _ssl_RAND_egd_impl(module, path);
exit:
@@ -970,8 +999,9 @@ _ssl_txt2obj(PyObject *module, PyObject *args, PyObject *kwargs)
int name = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|p:txt2obj", _keywords,
- &txt, &name))
+ &txt, &name)) {
goto exit;
+ }
return_value = _ssl_txt2obj_impl(module, txt, name);
exit:
@@ -996,8 +1026,9 @@ _ssl_nid2obj(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
int nid;
- if (!PyArg_Parse(arg, "i:nid2obj", &nid))
+ if (!PyArg_Parse(arg, "i:nid2obj", &nid)) {
goto exit;
+ }
return_value = _ssl_nid2obj_impl(module, nid);
exit:
@@ -1032,8 +1063,9 @@ _ssl_enum_certificates(PyObject *module, PyObject *args, PyObject *kwargs)
const char *store_name;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:enum_certificates", _keywords,
- &store_name))
+ &store_name)) {
goto exit;
+ }
return_value = _ssl_enum_certificates_impl(module, store_name);
exit:
@@ -1069,8 +1101,9 @@ _ssl_enum_crls(PyObject *module, PyObject *args, PyObject *kwargs)
const char *store_name;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:enum_crls", _keywords,
- &store_name))
+ &store_name)) {
goto exit;
+ }
return_value = _ssl_enum_crls_impl(module, store_name);
exit:
@@ -1102,4 +1135,4 @@ exit:
#ifndef _SSL_ENUM_CRLS_METHODDEF
#define _SSL_ENUM_CRLS_METHODDEF
#endif /* !defined(_SSL_ENUM_CRLS_METHODDEF) */
-/*[clinic end generated code: output=6fb10594d8351dc5 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=02444732c19722b3 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/_tkinter.c.h b/Modules/clinic/_tkinter.c.h
index 77af083c06..edd5380005 100644
--- a/Modules/clinic/_tkinter.c.h
+++ b/Modules/clinic/_tkinter.c.h
@@ -19,8 +19,9 @@ _tkinter_tkapp_eval(TkappObject *self, PyObject *arg)
PyObject *return_value = NULL;
const char *script;
- if (!PyArg_Parse(arg, "s:eval", &script))
+ if (!PyArg_Parse(arg, "s:eval", &script)) {
goto exit;
+ }
return_value = _tkinter_tkapp_eval_impl(self, script);
exit:
@@ -44,8 +45,9 @@ _tkinter_tkapp_evalfile(TkappObject *self, PyObject *arg)
PyObject *return_value = NULL;
const char *fileName;
- if (!PyArg_Parse(arg, "s:evalfile", &fileName))
+ if (!PyArg_Parse(arg, "s:evalfile", &fileName)) {
goto exit;
+ }
return_value = _tkinter_tkapp_evalfile_impl(self, fileName);
exit:
@@ -69,8 +71,9 @@ _tkinter_tkapp_record(TkappObject *self, PyObject *arg)
PyObject *return_value = NULL;
const char *script;
- if (!PyArg_Parse(arg, "s:record", &script))
+ if (!PyArg_Parse(arg, "s:record", &script)) {
goto exit;
+ }
return_value = _tkinter_tkapp_record_impl(self, script);
exit:
@@ -94,8 +97,9 @@ _tkinter_tkapp_adderrinfo(TkappObject *self, PyObject *arg)
PyObject *return_value = NULL;
const char *msg;
- if (!PyArg_Parse(arg, "s:adderrinfo", &msg))
+ if (!PyArg_Parse(arg, "s:adderrinfo", &msg)) {
goto exit;
+ }
return_value = _tkinter_tkapp_adderrinfo_impl(self, msg);
exit:
@@ -143,8 +147,9 @@ _tkinter_tkapp_exprstring(TkappObject *self, PyObject *arg)
PyObject *return_value = NULL;
const char *s;
- if (!PyArg_Parse(arg, "s:exprstring", &s))
+ if (!PyArg_Parse(arg, "s:exprstring", &s)) {
goto exit;
+ }
return_value = _tkinter_tkapp_exprstring_impl(self, s);
exit:
@@ -168,8 +173,9 @@ _tkinter_tkapp_exprlong(TkappObject *self, PyObject *arg)
PyObject *return_value = NULL;
const char *s;
- if (!PyArg_Parse(arg, "s:exprlong", &s))
+ if (!PyArg_Parse(arg, "s:exprlong", &s)) {
goto exit;
+ }
return_value = _tkinter_tkapp_exprlong_impl(self, s);
exit:
@@ -193,8 +199,9 @@ _tkinter_tkapp_exprdouble(TkappObject *self, PyObject *arg)
PyObject *return_value = NULL;
const char *s;
- if (!PyArg_Parse(arg, "s:exprdouble", &s))
+ if (!PyArg_Parse(arg, "s:exprdouble", &s)) {
goto exit;
+ }
return_value = _tkinter_tkapp_exprdouble_impl(self, s);
exit:
@@ -218,8 +225,9 @@ _tkinter_tkapp_exprboolean(TkappObject *self, PyObject *arg)
PyObject *return_value = NULL;
const char *s;
- if (!PyArg_Parse(arg, "s:exprboolean", &s))
+ if (!PyArg_Parse(arg, "s:exprboolean", &s)) {
goto exit;
+ }
return_value = _tkinter_tkapp_exprboolean_impl(self, s);
exit:
@@ -262,8 +270,9 @@ _tkinter_tkapp_createcommand(TkappObject *self, PyObject *args)
PyObject *func;
if (!PyArg_ParseTuple(args, "sO:createcommand",
- &name, &func))
+ &name, &func)) {
goto exit;
+ }
return_value = _tkinter_tkapp_createcommand_impl(self, name, func);
exit:
@@ -287,8 +296,9 @@ _tkinter_tkapp_deletecommand(TkappObject *self, PyObject *arg)
PyObject *return_value = NULL;
const char *name;
- if (!PyArg_Parse(arg, "s:deletecommand", &name))
+ if (!PyArg_Parse(arg, "s:deletecommand", &name)) {
goto exit;
+ }
return_value = _tkinter_tkapp_deletecommand_impl(self, name);
exit:
@@ -318,8 +328,9 @@ _tkinter_tkapp_createfilehandler(TkappObject *self, PyObject *args)
PyObject *func;
if (!PyArg_ParseTuple(args, "OiO:createfilehandler",
- &file, &mask, &func))
+ &file, &mask, &func)) {
goto exit;
+ }
return_value = _tkinter_tkapp_createfilehandler_impl(self, file, mask, func);
exit:
@@ -377,8 +388,9 @@ _tkinter_tkapp_createtimerhandler(TkappObject *self, PyObject *args)
PyObject *func;
if (!PyArg_ParseTuple(args, "iO:createtimerhandler",
- &milliseconds, &func))
+ &milliseconds, &func)) {
goto exit;
+ }
return_value = _tkinter_tkapp_createtimerhandler_impl(self, milliseconds, func);
exit:
@@ -403,8 +415,9 @@ _tkinter_tkapp_mainloop(TkappObject *self, PyObject *args)
int threshold = 0;
if (!PyArg_ParseTuple(args, "|i:mainloop",
- &threshold))
+ &threshold)) {
goto exit;
+ }
return_value = _tkinter_tkapp_mainloop_impl(self, threshold);
exit:
@@ -429,8 +442,9 @@ _tkinter_tkapp_dooneevent(TkappObject *self, PyObject *args)
int flags = 0;
if (!PyArg_ParseTuple(args, "|i:dooneevent",
- &flags))
+ &flags)) {
goto exit;
+ }
return_value = _tkinter_tkapp_dooneevent_impl(self, flags);
exit:
@@ -551,8 +565,9 @@ _tkinter_create(PyObject *module, PyObject *args)
const char *use = NULL;
if (!PyArg_ParseTuple(args, "|zssiiiiz:create",
- &screenName, &baseName, &className, &interactive, &wantobjects, &wantTk, &sync, &use))
+ &screenName, &baseName, &className, &interactive, &wantobjects, &wantTk, &sync, &use)) {
goto exit;
+ }
return_value = _tkinter_create_impl(module, screenName, baseName, className, interactive, wantobjects, wantTk, sync, use);
exit:
@@ -579,8 +594,9 @@ _tkinter_setbusywaitinterval(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
int new_val;
- if (!PyArg_Parse(arg, "i:setbusywaitinterval", &new_val))
+ if (!PyArg_Parse(arg, "i:setbusywaitinterval", &new_val)) {
goto exit;
+ }
return_value = _tkinter_setbusywaitinterval_impl(module, new_val);
exit:
@@ -606,8 +622,9 @@ _tkinter_getbusywaitinterval(PyObject *module, PyObject *Py_UNUSED(ignored))
int _return_value;
_return_value = _tkinter_getbusywaitinterval_impl(module);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong((long)_return_value);
exit:
@@ -621,4 +638,4 @@ exit:
#ifndef _TKINTER_TKAPP_DELETEFILEHANDLER_METHODDEF
#define _TKINTER_TKAPP_DELETEFILEHANDLER_METHODDEF
#endif /* !defined(_TKINTER_TKAPP_DELETEFILEHANDLER_METHODDEF) */
-/*[clinic end generated code: output=f9057c8bf288633d input=a9049054013a1b77]*/
+/*[clinic end generated code: output=836c578b71d69097 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/_weakref.c.h b/Modules/clinic/_weakref.c.h
index b83b33e4a1..c192e72193 100644
--- a/Modules/clinic/_weakref.c.h
+++ b/Modules/clinic/_weakref.c.h
@@ -21,11 +21,12 @@ _weakref_getweakrefcount(PyObject *module, PyObject *object)
Py_ssize_t _return_value;
_return_value = _weakref_getweakrefcount_impl(module, object);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromSsize_t(_return_value);
exit:
return return_value;
}
-/*[clinic end generated code: output=d9086c8576d46933 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=e1ad587147323e19 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/_winapi.c.h b/Modules/clinic/_winapi.c.h
index ac38d374a2..e0c7d6c62d 100644
--- a/Modules/clinic/_winapi.c.h
+++ b/Modules/clinic/_winapi.c.h
@@ -19,8 +19,9 @@ _winapi_Overlapped_GetOverlappedResult(OverlappedObject *self, PyObject *arg)
PyObject *return_value = NULL;
int wait;
- if (!PyArg_Parse(arg, "p:GetOverlappedResult", &wait))
+ if (!PyArg_Parse(arg, "p:GetOverlappedResult", &wait)) {
goto exit;
+ }
return_value = _winapi_Overlapped_GetOverlappedResult_impl(self, wait);
exit:
@@ -79,8 +80,9 @@ _winapi_CloseHandle(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
HANDLE handle;
- if (!PyArg_Parse(arg, "" F_HANDLE ":CloseHandle", &handle))
+ if (!PyArg_Parse(arg, "" F_HANDLE ":CloseHandle", &handle)) {
goto exit;
+ }
return_value = _winapi_CloseHandle_impl(module, handle);
exit:
@@ -108,8 +110,9 @@ _winapi_ConnectNamedPipe(PyObject *module, PyObject *args, PyObject *kwargs)
int use_overlapped = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "" F_HANDLE "|i:ConnectNamedPipe", _keywords,
- &handle, &use_overlapped))
+ &handle, &use_overlapped)) {
goto exit;
+ }
return_value = _winapi_ConnectNamedPipe_impl(module, handle, use_overlapped);
exit:
@@ -147,13 +150,16 @@ _winapi_CreateFile(PyObject *module, PyObject *args)
HANDLE _return_value;
if (!PyArg_ParseTuple(args, "skk" F_POINTER "kk" F_HANDLE ":CreateFile",
- &file_name, &desired_access, &share_mode, &security_attributes, &creation_disposition, &flags_and_attributes, &template_file))
+ &file_name, &desired_access, &share_mode, &security_attributes, &creation_disposition, &flags_and_attributes, &template_file)) {
goto exit;
+ }
_return_value = _winapi_CreateFile_impl(module, file_name, desired_access, share_mode, security_attributes, creation_disposition, flags_and_attributes, template_file);
- if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred())
+ if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred()) {
goto exit;
- if (_return_value == NULL)
+ }
+ if (_return_value == NULL) {
Py_RETURN_NONE;
+ }
return_value = HANDLE_TO_PYNUM(_return_value);
exit:
@@ -180,8 +186,9 @@ _winapi_CreateJunction(PyObject *module, PyObject *args)
LPWSTR dst_path;
if (!PyArg_ParseTuple(args, "uu:CreateJunction",
- &src_path, &dst_path))
+ &src_path, &dst_path)) {
goto exit;
+ }
return_value = _winapi_CreateJunction_impl(module, src_path, dst_path);
exit:
@@ -220,13 +227,16 @@ _winapi_CreateNamedPipe(PyObject *module, PyObject *args)
HANDLE _return_value;
if (!PyArg_ParseTuple(args, "skkkkkk" F_POINTER ":CreateNamedPipe",
- &name, &open_mode, &pipe_mode, &max_instances, &out_buffer_size, &in_buffer_size, &default_timeout, &security_attributes))
+ &name, &open_mode, &pipe_mode, &max_instances, &out_buffer_size, &in_buffer_size, &default_timeout, &security_attributes)) {
goto exit;
+ }
_return_value = _winapi_CreateNamedPipe_impl(module, name, open_mode, pipe_mode, max_instances, out_buffer_size, in_buffer_size, default_timeout, security_attributes);
- if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred())
+ if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred()) {
goto exit;
- if (_return_value == NULL)
+ }
+ if (_return_value == NULL) {
Py_RETURN_NONE;
+ }
return_value = HANDLE_TO_PYNUM(_return_value);
exit:
@@ -258,8 +268,9 @@ _winapi_CreatePipe(PyObject *module, PyObject *args)
DWORD size;
if (!PyArg_ParseTuple(args, "Ok:CreatePipe",
- &pipe_attrs, &size))
+ &pipe_attrs, &size)) {
goto exit;
+ }
return_value = _winapi_CreatePipe_impl(module, pipe_attrs, size);
exit:
@@ -308,8 +319,9 @@ _winapi_CreateProcess(PyObject *module, PyObject *args)
PyObject *startup_info;
if (!PyArg_ParseTuple(args, "ZZOOikOZO:CreateProcess",
- &application_name, &command_line, &proc_attrs, &thread_attrs, &inherit_handles, &creation_flags, &env_mapping, &current_directory, &startup_info))
+ &application_name, &command_line, &proc_attrs, &thread_attrs, &inherit_handles, &creation_flags, &env_mapping, &current_directory, &startup_info)) {
goto exit;
+ }
return_value = _winapi_CreateProcess_impl(module, application_name, command_line, proc_attrs, thread_attrs, inherit_handles, creation_flags, env_mapping, current_directory, startup_info);
exit:
@@ -351,13 +363,16 @@ _winapi_DuplicateHandle(PyObject *module, PyObject *args)
HANDLE _return_value;
if (!PyArg_ParseTuple(args, "" F_HANDLE "" F_HANDLE "" F_HANDLE "ki|k:DuplicateHandle",
- &source_process_handle, &source_handle, &target_process_handle, &desired_access, &inherit_handle, &options))
+ &source_process_handle, &source_handle, &target_process_handle, &desired_access, &inherit_handle, &options)) {
goto exit;
+ }
_return_value = _winapi_DuplicateHandle_impl(module, source_process_handle, source_handle, target_process_handle, desired_access, inherit_handle, options);
- if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred())
+ if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred()) {
goto exit;
- if (_return_value == NULL)
+ }
+ if (_return_value == NULL) {
Py_RETURN_NONE;
+ }
return_value = HANDLE_TO_PYNUM(_return_value);
exit:
@@ -381,8 +396,9 @@ _winapi_ExitProcess(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
UINT ExitCode;
- if (!PyArg_Parse(arg, "I:ExitProcess", &ExitCode))
+ if (!PyArg_Parse(arg, "I:ExitProcess", &ExitCode)) {
goto exit;
+ }
return_value = _winapi_ExitProcess_impl(module, ExitCode);
exit:
@@ -408,10 +424,12 @@ _winapi_GetCurrentProcess(PyObject *module, PyObject *Py_UNUSED(ignored))
HANDLE _return_value;
_return_value = _winapi_GetCurrentProcess_impl(module);
- if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred())
+ if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred()) {
goto exit;
- if (_return_value == NULL)
+ }
+ if (_return_value == NULL) {
Py_RETURN_NONE;
+ }
return_value = HANDLE_TO_PYNUM(_return_value);
exit:
@@ -437,11 +455,13 @@ _winapi_GetExitCodeProcess(PyObject *module, PyObject *arg)
HANDLE process;
DWORD _return_value;
- if (!PyArg_Parse(arg, "" F_HANDLE ":GetExitCodeProcess", &process))
+ if (!PyArg_Parse(arg, "" F_HANDLE ":GetExitCodeProcess", &process)) {
goto exit;
+ }
_return_value = _winapi_GetExitCodeProcess_impl(module, process);
- if ((_return_value == DWORD_MAX) && PyErr_Occurred())
+ if ((_return_value == DWORD_MAX) && PyErr_Occurred()) {
goto exit;
+ }
return_value = Py_BuildValue("k", _return_value);
exit:
@@ -466,8 +486,9 @@ _winapi_GetLastError(PyObject *module, PyObject *Py_UNUSED(ignored))
DWORD _return_value;
_return_value = _winapi_GetLastError_impl(module);
- if ((_return_value == DWORD_MAX) && PyErr_Occurred())
+ if ((_return_value == DWORD_MAX) && PyErr_Occurred()) {
goto exit;
+ }
return_value = Py_BuildValue("k", _return_value);
exit:
@@ -499,8 +520,9 @@ _winapi_GetModuleFileName(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
HMODULE module_handle;
- if (!PyArg_Parse(arg, "" F_HANDLE ":GetModuleFileName", &module_handle))
+ if (!PyArg_Parse(arg, "" F_HANDLE ":GetModuleFileName", &module_handle)) {
goto exit;
+ }
return_value = _winapi_GetModuleFileName_impl(module, module_handle);
exit:
@@ -531,13 +553,16 @@ _winapi_GetStdHandle(PyObject *module, PyObject *arg)
DWORD std_handle;
HANDLE _return_value;
- if (!PyArg_Parse(arg, "k:GetStdHandle", &std_handle))
+ if (!PyArg_Parse(arg, "k:GetStdHandle", &std_handle)) {
goto exit;
+ }
_return_value = _winapi_GetStdHandle_impl(module, std_handle);
- if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred())
+ if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred()) {
goto exit;
- if (_return_value == NULL)
+ }
+ if (_return_value == NULL) {
Py_RETURN_NONE;
+ }
return_value = HANDLE_TO_PYNUM(_return_value);
exit:
@@ -563,8 +588,9 @@ _winapi_GetVersion(PyObject *module, PyObject *Py_UNUSED(ignored))
long _return_value;
_return_value = _winapi_GetVersion_impl(module);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong(_return_value);
exit:
@@ -593,13 +619,16 @@ _winapi_OpenProcess(PyObject *module, PyObject *args)
HANDLE _return_value;
if (!PyArg_ParseTuple(args, "kik:OpenProcess",
- &desired_access, &inherit_handle, &process_id))
+ &desired_access, &inherit_handle, &process_id)) {
goto exit;
+ }
_return_value = _winapi_OpenProcess_impl(module, desired_access, inherit_handle, process_id);
- if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred())
+ if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred()) {
goto exit;
- if (_return_value == NULL)
+ }
+ if (_return_value == NULL) {
Py_RETURN_NONE;
+ }
return_value = HANDLE_TO_PYNUM(_return_value);
exit:
@@ -625,8 +654,9 @@ _winapi_PeekNamedPipe(PyObject *module, PyObject *args)
int size = 0;
if (!PyArg_ParseTuple(args, "" F_HANDLE "|i:PeekNamedPipe",
- &handle, &size))
+ &handle, &size)) {
goto exit;
+ }
return_value = _winapi_PeekNamedPipe_impl(module, handle, size);
exit:
@@ -655,8 +685,9 @@ _winapi_ReadFile(PyObject *module, PyObject *args, PyObject *kwargs)
int use_overlapped = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "" F_HANDLE "i|i:ReadFile", _keywords,
- &handle, &size, &use_overlapped))
+ &handle, &size, &use_overlapped)) {
goto exit;
+ }
return_value = _winapi_ReadFile_impl(module, handle, size, use_overlapped);
exit:
@@ -688,8 +719,9 @@ _winapi_SetNamedPipeHandleState(PyObject *module, PyObject *args)
PyObject *collect_data_timeout;
if (!PyArg_ParseTuple(args, "" F_HANDLE "OOO:SetNamedPipeHandleState",
- &named_pipe, &mode, &max_collection_count, &collect_data_timeout))
+ &named_pipe, &mode, &max_collection_count, &collect_data_timeout)) {
goto exit;
+ }
return_value = _winapi_SetNamedPipeHandleState_impl(module, named_pipe, mode, max_collection_count, collect_data_timeout);
exit:
@@ -717,8 +749,9 @@ _winapi_TerminateProcess(PyObject *module, PyObject *args)
UINT exit_code;
if (!PyArg_ParseTuple(args, "" F_HANDLE "I:TerminateProcess",
- &handle, &exit_code))
+ &handle, &exit_code)) {
goto exit;
+ }
return_value = _winapi_TerminateProcess_impl(module, handle, exit_code);
exit:
@@ -744,8 +777,9 @@ _winapi_WaitNamedPipe(PyObject *module, PyObject *args)
DWORD timeout;
if (!PyArg_ParseTuple(args, "sk:WaitNamedPipe",
- &name, &timeout))
+ &name, &timeout)) {
goto exit;
+ }
return_value = _winapi_WaitNamedPipe_impl(module, name, timeout);
exit:
@@ -774,8 +808,9 @@ _winapi_WaitForMultipleObjects(PyObject *module, PyObject *args)
DWORD milliseconds = INFINITE;
if (!PyArg_ParseTuple(args, "Oi|k:WaitForMultipleObjects",
- &handle_seq, &wait_flag, &milliseconds))
+ &handle_seq, &wait_flag, &milliseconds)) {
goto exit;
+ }
return_value = _winapi_WaitForMultipleObjects_impl(module, handle_seq, wait_flag, milliseconds);
exit:
@@ -808,11 +843,13 @@ _winapi_WaitForSingleObject(PyObject *module, PyObject *args)
long _return_value;
if (!PyArg_ParseTuple(args, "" F_HANDLE "k:WaitForSingleObject",
- &handle, &milliseconds))
+ &handle, &milliseconds)) {
goto exit;
+ }
_return_value = _winapi_WaitForSingleObject_impl(module, handle, milliseconds);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong(_return_value);
exit:
@@ -841,11 +878,12 @@ _winapi_WriteFile(PyObject *module, PyObject *args, PyObject *kwargs)
int use_overlapped = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "" F_HANDLE "O|i:WriteFile", _keywords,
- &handle, &buffer, &use_overlapped))
+ &handle, &buffer, &use_overlapped)) {
goto exit;
+ }
return_value = _winapi_WriteFile_impl(module, handle, buffer, use_overlapped);
exit:
return return_value;
}
-/*[clinic end generated code: output=a4c4b2a9fcb0bea1 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=8032f3371c14749e input=a9049054013a1b77]*/
diff --git a/Modules/clinic/arraymodule.c.h b/Modules/clinic/arraymodule.c.h
index 0c7061a1d2..3b9fcdab0a 100644
--- a/Modules/clinic/arraymodule.c.h
+++ b/Modules/clinic/arraymodule.c.h
@@ -77,8 +77,9 @@ array_array_pop(arrayobject *self, PyObject *args)
Py_ssize_t i = -1;
if (!PyArg_ParseTuple(args, "|n:pop",
- &i))
+ &i)) {
goto exit;
+ }
return_value = array_array_pop_impl(self, i);
exit:
@@ -114,8 +115,9 @@ array_array_insert(arrayobject *self, PyObject *args)
PyObject *v;
if (!PyArg_ParseTuple(args, "nO:insert",
- &i, &v))
+ &i, &v)) {
goto exit;
+ }
return_value = array_array_insert_impl(self, i, v);
exit:
@@ -211,8 +213,9 @@ array_array_fromfile(arrayobject *self, PyObject *args)
Py_ssize_t n;
if (!PyArg_ParseTuple(args, "On:fromfile",
- &f, &n))
+ &f, &n)) {
goto exit;
+ }
return_value = array_array_fromfile_impl(self, f, n);
exit:
@@ -275,14 +278,16 @@ array_array_fromstring(arrayobject *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};
- if (!PyArg_Parse(arg, "s*:fromstring", &buffer))
+ if (!PyArg_Parse(arg, "s*:fromstring", &buffer)) {
goto exit;
+ }
return_value = array_array_fromstring_impl(self, &buffer);
exit:
/* Cleanup for buffer */
- if (buffer.obj)
+ if (buffer.obj) {
PyBuffer_Release(&buffer);
+ }
return return_value;
}
@@ -305,14 +310,16 @@ array_array_frombytes(arrayobject *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer buffer = {NULL, NULL};
- if (!PyArg_Parse(arg, "y*:frombytes", &buffer))
+ if (!PyArg_Parse(arg, "y*:frombytes", &buffer)) {
goto exit;
+ }
return_value = array_array_frombytes_impl(self, &buffer);
exit:
/* Cleanup for buffer */
- if (buffer.obj)
+ if (buffer.obj) {
PyBuffer_Release(&buffer);
+ }
return return_value;
}
@@ -379,8 +386,9 @@ array_array_fromunicode(arrayobject *self, PyObject *arg)
Py_UNICODE *ustr;
Py_ssize_clean_t ustr_length;
- if (!PyArg_Parse(arg, "u#:fromunicode", &ustr, &ustr_length))
+ if (!PyArg_Parse(arg, "u#:fromunicode", &ustr, &ustr_length)) {
goto exit;
+ }
return_value = array_array_fromunicode_impl(self, ustr, ustr_length);
exit:
@@ -453,8 +461,9 @@ array__array_reconstructor(PyObject *module, PyObject *args)
PyObject *items;
if (!PyArg_ParseTuple(args, "OCiO:_array_reconstructor",
- &arraytype, &typecode, &mformat_code, &items))
+ &arraytype, &typecode, &mformat_code, &items)) {
goto exit;
+ }
return_value = array__array_reconstructor_impl(module, arraytype, typecode, mformat_code, items);
exit:
@@ -496,4 +505,4 @@ PyDoc_STRVAR(array_arrayiterator___setstate____doc__,
#define ARRAY_ARRAYITERATOR___SETSTATE___METHODDEF \
{"__setstate__", (PyCFunction)array_arrayiterator___setstate__, METH_O, array_arrayiterator___setstate____doc__},
-/*[clinic end generated code: output=305df3f5796039e4 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=b2054fb764c8cc64 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/audioop.c.h b/Modules/clinic/audioop.c.h
index 62e313b5bf..be4b718843 100644
--- a/Modules/clinic/audioop.c.h
+++ b/Modules/clinic/audioop.c.h
@@ -24,14 +24,16 @@ audioop_getsample(PyObject *module, PyObject *args)
Py_ssize_t index;
if (!PyArg_ParseTuple(args, "y*in:getsample",
- &fragment, &width, &index))
+ &fragment, &width, &index)) {
goto exit;
+ }
return_value = audioop_getsample_impl(module, &fragment, width, index);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -56,14 +58,16 @@ audioop_max(PyObject *module, PyObject *args)
int width;
if (!PyArg_ParseTuple(args, "y*i:max",
- &fragment, &width))
+ &fragment, &width)) {
goto exit;
+ }
return_value = audioop_max_impl(module, &fragment, width);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -88,14 +92,16 @@ audioop_minmax(PyObject *module, PyObject *args)
int width;
if (!PyArg_ParseTuple(args, "y*i:minmax",
- &fragment, &width))
+ &fragment, &width)) {
goto exit;
+ }
return_value = audioop_minmax_impl(module, &fragment, width);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -120,14 +126,16 @@ audioop_avg(PyObject *module, PyObject *args)
int width;
if (!PyArg_ParseTuple(args, "y*i:avg",
- &fragment, &width))
+ &fragment, &width)) {
goto exit;
+ }
return_value = audioop_avg_impl(module, &fragment, width);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -152,14 +160,16 @@ audioop_rms(PyObject *module, PyObject *args)
int width;
if (!PyArg_ParseTuple(args, "y*i:rms",
- &fragment, &width))
+ &fragment, &width)) {
goto exit;
+ }
return_value = audioop_rms_impl(module, &fragment, width);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -185,17 +195,20 @@ audioop_findfit(PyObject *module, PyObject *args)
Py_buffer reference = {NULL, NULL};
if (!PyArg_ParseTuple(args, "y*y*:findfit",
- &fragment, &reference))
+ &fragment, &reference)) {
goto exit;
+ }
return_value = audioop_findfit_impl(module, &fragment, &reference);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
/* Cleanup for reference */
- if (reference.obj)
+ if (reference.obj) {
PyBuffer_Release(&reference);
+ }
return return_value;
}
@@ -221,17 +234,20 @@ audioop_findfactor(PyObject *module, PyObject *args)
Py_buffer reference = {NULL, NULL};
if (!PyArg_ParseTuple(args, "y*y*:findfactor",
- &fragment, &reference))
+ &fragment, &reference)) {
goto exit;
+ }
return_value = audioop_findfactor_impl(module, &fragment, &reference);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
/* Cleanup for reference */
- if (reference.obj)
+ if (reference.obj) {
PyBuffer_Release(&reference);
+ }
return return_value;
}
@@ -257,14 +273,16 @@ audioop_findmax(PyObject *module, PyObject *args)
Py_ssize_t length;
if (!PyArg_ParseTuple(args, "y*n:findmax",
- &fragment, &length))
+ &fragment, &length)) {
goto exit;
+ }
return_value = audioop_findmax_impl(module, &fragment, length);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -289,14 +307,16 @@ audioop_avgpp(PyObject *module, PyObject *args)
int width;
if (!PyArg_ParseTuple(args, "y*i:avgpp",
- &fragment, &width))
+ &fragment, &width)) {
goto exit;
+ }
return_value = audioop_avgpp_impl(module, &fragment, width);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -321,14 +341,16 @@ audioop_maxpp(PyObject *module, PyObject *args)
int width;
if (!PyArg_ParseTuple(args, "y*i:maxpp",
- &fragment, &width))
+ &fragment, &width)) {
goto exit;
+ }
return_value = audioop_maxpp_impl(module, &fragment, width);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -353,14 +375,16 @@ audioop_cross(PyObject *module, PyObject *args)
int width;
if (!PyArg_ParseTuple(args, "y*i:cross",
- &fragment, &width))
+ &fragment, &width)) {
goto exit;
+ }
return_value = audioop_cross_impl(module, &fragment, width);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -387,14 +411,16 @@ audioop_mul(PyObject *module, PyObject *args)
double factor;
if (!PyArg_ParseTuple(args, "y*id:mul",
- &fragment, &width, &factor))
+ &fragment, &width, &factor)) {
goto exit;
+ }
return_value = audioop_mul_impl(module, &fragment, width, factor);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -422,14 +448,16 @@ audioop_tomono(PyObject *module, PyObject *args)
double rfactor;
if (!PyArg_ParseTuple(args, "y*idd:tomono",
- &fragment, &width, &lfactor, &rfactor))
+ &fragment, &width, &lfactor, &rfactor)) {
goto exit;
+ }
return_value = audioop_tomono_impl(module, &fragment, width, lfactor, rfactor);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -457,14 +485,16 @@ audioop_tostereo(PyObject *module, PyObject *args)
double rfactor;
if (!PyArg_ParseTuple(args, "y*idd:tostereo",
- &fragment, &width, &lfactor, &rfactor))
+ &fragment, &width, &lfactor, &rfactor)) {
goto exit;
+ }
return_value = audioop_tostereo_impl(module, &fragment, width, lfactor, rfactor);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -491,17 +521,20 @@ audioop_add(PyObject *module, PyObject *args)
int width;
if (!PyArg_ParseTuple(args, "y*y*i:add",
- &fragment1, &fragment2, &width))
+ &fragment1, &fragment2, &width)) {
goto exit;
+ }
return_value = audioop_add_impl(module, &fragment1, &fragment2, width);
exit:
/* Cleanup for fragment1 */
- if (fragment1.obj)
+ if (fragment1.obj) {
PyBuffer_Release(&fragment1);
+ }
/* Cleanup for fragment2 */
- if (fragment2.obj)
+ if (fragment2.obj) {
PyBuffer_Release(&fragment2);
+ }
return return_value;
}
@@ -527,14 +560,16 @@ audioop_bias(PyObject *module, PyObject *args)
int bias;
if (!PyArg_ParseTuple(args, "y*ii:bias",
- &fragment, &width, &bias))
+ &fragment, &width, &bias)) {
goto exit;
+ }
return_value = audioop_bias_impl(module, &fragment, width, bias);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -559,14 +594,16 @@ audioop_reverse(PyObject *module, PyObject *args)
int width;
if (!PyArg_ParseTuple(args, "y*i:reverse",
- &fragment, &width))
+ &fragment, &width)) {
goto exit;
+ }
return_value = audioop_reverse_impl(module, &fragment, width);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -591,14 +628,16 @@ audioop_byteswap(PyObject *module, PyObject *args)
int width;
if (!PyArg_ParseTuple(args, "y*i:byteswap",
- &fragment, &width))
+ &fragment, &width)) {
goto exit;
+ }
return_value = audioop_byteswap_impl(module, &fragment, width);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -625,14 +664,16 @@ audioop_lin2lin(PyObject *module, PyObject *args)
int newwidth;
if (!PyArg_ParseTuple(args, "y*ii:lin2lin",
- &fragment, &width, &newwidth))
+ &fragment, &width, &newwidth)) {
goto exit;
+ }
return_value = audioop_lin2lin_impl(module, &fragment, width, newwidth);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -666,14 +707,16 @@ audioop_ratecv(PyObject *module, PyObject *args)
int weightB = 0;
if (!PyArg_ParseTuple(args, "y*iiiiO|ii:ratecv",
- &fragment, &width, &nchannels, &inrate, &outrate, &state, &weightA, &weightB))
+ &fragment, &width, &nchannels, &inrate, &outrate, &state, &weightA, &weightB)) {
goto exit;
+ }
return_value = audioop_ratecv_impl(module, &fragment, width, nchannels, inrate, outrate, state, weightA, weightB);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -698,14 +741,16 @@ audioop_lin2ulaw(PyObject *module, PyObject *args)
int width;
if (!PyArg_ParseTuple(args, "y*i:lin2ulaw",
- &fragment, &width))
+ &fragment, &width)) {
goto exit;
+ }
return_value = audioop_lin2ulaw_impl(module, &fragment, width);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -730,14 +775,16 @@ audioop_ulaw2lin(PyObject *module, PyObject *args)
int width;
if (!PyArg_ParseTuple(args, "y*i:ulaw2lin",
- &fragment, &width))
+ &fragment, &width)) {
goto exit;
+ }
return_value = audioop_ulaw2lin_impl(module, &fragment, width);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -762,14 +809,16 @@ audioop_lin2alaw(PyObject *module, PyObject *args)
int width;
if (!PyArg_ParseTuple(args, "y*i:lin2alaw",
- &fragment, &width))
+ &fragment, &width)) {
goto exit;
+ }
return_value = audioop_lin2alaw_impl(module, &fragment, width);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -794,14 +843,16 @@ audioop_alaw2lin(PyObject *module, PyObject *args)
int width;
if (!PyArg_ParseTuple(args, "y*i:alaw2lin",
- &fragment, &width))
+ &fragment, &width)) {
goto exit;
+ }
return_value = audioop_alaw2lin_impl(module, &fragment, width);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -828,14 +879,16 @@ audioop_lin2adpcm(PyObject *module, PyObject *args)
PyObject *state;
if (!PyArg_ParseTuple(args, "y*iO:lin2adpcm",
- &fragment, &width, &state))
+ &fragment, &width, &state)) {
goto exit;
+ }
return_value = audioop_lin2adpcm_impl(module, &fragment, width, state);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
@@ -862,15 +915,17 @@ audioop_adpcm2lin(PyObject *module, PyObject *args)
PyObject *state;
if (!PyArg_ParseTuple(args, "y*iO:adpcm2lin",
- &fragment, &width, &state))
+ &fragment, &width, &state)) {
goto exit;
+ }
return_value = audioop_adpcm2lin_impl(module, &fragment, width, state);
exit:
/* Cleanup for fragment */
- if (fragment.obj)
+ if (fragment.obj) {
PyBuffer_Release(&fragment);
+ }
return return_value;
}
-/*[clinic end generated code: output=385fb09fa21a62c0 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=e0ab74c3fa57c39c input=a9049054013a1b77]*/
diff --git a/Modules/clinic/binascii.c.h b/Modules/clinic/binascii.c.h
index e20cac24ba..68c34f9750 100644
--- a/Modules/clinic/binascii.c.h
+++ b/Modules/clinic/binascii.c.h
@@ -20,8 +20,9 @@ binascii_a2b_uu(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer data = {NULL, NULL};
- if (!PyArg_Parse(arg, "O&:a2b_uu", ascii_buffer_converter, &data))
+ if (!PyArg_Parse(arg, "O&:a2b_uu", ascii_buffer_converter, &data)) {
goto exit;
+ }
return_value = binascii_a2b_uu_impl(module, &data);
exit:
@@ -50,14 +51,16 @@ binascii_b2a_uu(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer data = {NULL, NULL};
- if (!PyArg_Parse(arg, "y*:b2a_uu", &data))
+ if (!PyArg_Parse(arg, "y*:b2a_uu", &data)) {
goto exit;
+ }
return_value = binascii_b2a_uu_impl(module, &data);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -80,8 +83,9 @@ binascii_a2b_base64(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer data = {NULL, NULL};
- if (!PyArg_Parse(arg, "O&:a2b_base64", ascii_buffer_converter, &data))
+ if (!PyArg_Parse(arg, "O&:a2b_base64", ascii_buffer_converter, &data)) {
goto exit;
+ }
return_value = binascii_a2b_base64_impl(module, &data);
exit:
@@ -93,31 +97,36 @@ exit:
}
PyDoc_STRVAR(binascii_b2a_base64__doc__,
-"b2a_base64($module, data, /)\n"
+"b2a_base64($module, /, data, *, newline=True)\n"
"--\n"
"\n"
"Base64-code line of data.");
#define BINASCII_B2A_BASE64_METHODDEF \
- {"b2a_base64", (PyCFunction)binascii_b2a_base64, METH_O, binascii_b2a_base64__doc__},
+ {"b2a_base64", (PyCFunction)binascii_b2a_base64, METH_VARARGS|METH_KEYWORDS, binascii_b2a_base64__doc__},
static PyObject *
-binascii_b2a_base64_impl(PyObject *module, Py_buffer *data);
+binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, int newline);
static PyObject *
-binascii_b2a_base64(PyObject *module, PyObject *arg)
+binascii_b2a_base64(PyObject *module, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
+ static char *_keywords[] = {"data", "newline", NULL};
Py_buffer data = {NULL, NULL};
+ int newline = 1;
- if (!PyArg_Parse(arg, "y*:b2a_base64", &data))
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|$i:b2a_base64", _keywords,
+ &data, &newline)) {
goto exit;
- return_value = binascii_b2a_base64_impl(module, &data);
+ }
+ return_value = binascii_b2a_base64_impl(module, &data, newline);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -140,8 +149,9 @@ binascii_a2b_hqx(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer data = {NULL, NULL};
- if (!PyArg_Parse(arg, "O&:a2b_hqx", ascii_buffer_converter, &data))
+ if (!PyArg_Parse(arg, "O&:a2b_hqx", ascii_buffer_converter, &data)) {
goto exit;
+ }
return_value = binascii_a2b_hqx_impl(module, &data);
exit:
@@ -170,14 +180,16 @@ binascii_rlecode_hqx(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer data = {NULL, NULL};
- if (!PyArg_Parse(arg, "y*:rlecode_hqx", &data))
+ if (!PyArg_Parse(arg, "y*:rlecode_hqx", &data)) {
goto exit;
+ }
return_value = binascii_rlecode_hqx_impl(module, &data);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -200,14 +212,16 @@ binascii_b2a_hqx(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer data = {NULL, NULL};
- if (!PyArg_Parse(arg, "y*:b2a_hqx", &data))
+ if (!PyArg_Parse(arg, "y*:b2a_hqx", &data)) {
goto exit;
+ }
return_value = binascii_b2a_hqx_impl(module, &data);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -230,14 +244,16 @@ binascii_rledecode_hqx(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer data = {NULL, NULL};
- if (!PyArg_Parse(arg, "y*:rledecode_hqx", &data))
+ if (!PyArg_Parse(arg, "y*:rledecode_hqx", &data)) {
goto exit;
+ }
return_value = binascii_rledecode_hqx_impl(module, &data);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -263,17 +279,20 @@ binascii_crc_hqx(PyObject *module, PyObject *args)
unsigned int _return_value;
if (!PyArg_ParseTuple(args, "y*I:crc_hqx",
- &data, &crc))
+ &data, &crc)) {
goto exit;
+ }
_return_value = binascii_crc_hqx_impl(module, &data, crc);
- if ((_return_value == (unsigned int)-1) && PyErr_Occurred())
+ if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -299,17 +318,20 @@ binascii_crc32(PyObject *module, PyObject *args)
unsigned int _return_value;
if (!PyArg_ParseTuple(args, "y*|I:crc32",
- &data, &crc))
+ &data, &crc)) {
goto exit;
+ }
_return_value = binascii_crc32_impl(module, &data, crc);
- if ((_return_value == (unsigned int)-1) && PyErr_Occurred())
+ if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -335,14 +357,16 @@ binascii_b2a_hex(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer data = {NULL, NULL};
- if (!PyArg_Parse(arg, "y*:b2a_hex", &data))
+ if (!PyArg_Parse(arg, "y*:b2a_hex", &data)) {
goto exit;
+ }
return_value = binascii_b2a_hex_impl(module, &data);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -367,14 +391,16 @@ binascii_hexlify(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer data = {NULL, NULL};
- if (!PyArg_Parse(arg, "y*:hexlify", &data))
+ if (!PyArg_Parse(arg, "y*:hexlify", &data)) {
goto exit;
+ }
return_value = binascii_hexlify_impl(module, &data);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -400,8 +426,9 @@ binascii_a2b_hex(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer hexstr = {NULL, NULL};
- if (!PyArg_Parse(arg, "O&:a2b_hex", ascii_buffer_converter, &hexstr))
+ if (!PyArg_Parse(arg, "O&:a2b_hex", ascii_buffer_converter, &hexstr)) {
goto exit;
+ }
return_value = binascii_a2b_hex_impl(module, &hexstr);
exit:
@@ -432,8 +459,9 @@ binascii_unhexlify(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer hexstr = {NULL, NULL};
- if (!PyArg_Parse(arg, "O&:unhexlify", ascii_buffer_converter, &hexstr))
+ if (!PyArg_Parse(arg, "O&:unhexlify", ascii_buffer_converter, &hexstr)) {
goto exit;
+ }
return_value = binascii_unhexlify_impl(module, &hexstr);
exit:
@@ -465,8 +493,9 @@ binascii_a2b_qp(PyObject *module, PyObject *args, PyObject *kwargs)
int header = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i:a2b_qp", _keywords,
- ascii_buffer_converter, &data, &header))
+ ascii_buffer_converter, &data, &header)) {
goto exit;
+ }
return_value = binascii_a2b_qp_impl(module, &data, header);
exit:
@@ -505,15 +534,17 @@ binascii_b2a_qp(PyObject *module, PyObject *args, PyObject *kwargs)
int header = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|iii:b2a_qp", _keywords,
- &data, &quotetabs, &istext, &header))
+ &data, &quotetabs, &istext, &header)) {
goto exit;
+ }
return_value = binascii_b2a_qp_impl(module, &data, quotetabs, istext, header);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
-/*[clinic end generated code: output=51173fc9718a5edc input=a9049054013a1b77]*/
+/*[clinic end generated code: output=d91d1058dc0590e1 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/cmathmodule.c.h b/Modules/clinic/cmathmodule.c.h
index a255353d4a..cba035f150 100644
--- a/Modules/clinic/cmathmodule.c.h
+++ b/Modules/clinic/cmathmodule.c.h
@@ -21,8 +21,9 @@ cmath_acos(PyObject *module, PyObject *arg)
Py_complex z;
Py_complex _return_value;
- if (!PyArg_Parse(arg, "D:acos", &z))
+ if (!PyArg_Parse(arg, "D:acos", &z)) {
goto exit;
+ }
/* modifications for z */
errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
_return_value = cmath_acos_impl(module, z);
@@ -62,8 +63,9 @@ cmath_acosh(PyObject *module, PyObject *arg)
Py_complex z;
Py_complex _return_value;
- if (!PyArg_Parse(arg, "D:acosh", &z))
+ if (!PyArg_Parse(arg, "D:acosh", &z)) {
goto exit;
+ }
/* modifications for z */
errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
_return_value = cmath_acosh_impl(module, z);
@@ -103,8 +105,9 @@ cmath_asin(PyObject *module, PyObject *arg)
Py_complex z;
Py_complex _return_value;
- if (!PyArg_Parse(arg, "D:asin", &z))
+ if (!PyArg_Parse(arg, "D:asin", &z)) {
goto exit;
+ }
/* modifications for z */
errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
_return_value = cmath_asin_impl(module, z);
@@ -144,8 +147,9 @@ cmath_asinh(PyObject *module, PyObject *arg)
Py_complex z;
Py_complex _return_value;
- if (!PyArg_Parse(arg, "D:asinh", &z))
+ if (!PyArg_Parse(arg, "D:asinh", &z)) {
goto exit;
+ }
/* modifications for z */
errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
_return_value = cmath_asinh_impl(module, z);
@@ -185,8 +189,9 @@ cmath_atan(PyObject *module, PyObject *arg)
Py_complex z;
Py_complex _return_value;
- if (!PyArg_Parse(arg, "D:atan", &z))
+ if (!PyArg_Parse(arg, "D:atan", &z)) {
goto exit;
+ }
/* modifications for z */
errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
_return_value = cmath_atan_impl(module, z);
@@ -226,8 +231,9 @@ cmath_atanh(PyObject *module, PyObject *arg)
Py_complex z;
Py_complex _return_value;
- if (!PyArg_Parse(arg, "D:atanh", &z))
+ if (!PyArg_Parse(arg, "D:atanh", &z)) {
goto exit;
+ }
/* modifications for z */
errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
_return_value = cmath_atanh_impl(module, z);
@@ -267,8 +273,9 @@ cmath_cos(PyObject *module, PyObject *arg)
Py_complex z;
Py_complex _return_value;
- if (!PyArg_Parse(arg, "D:cos", &z))
+ if (!PyArg_Parse(arg, "D:cos", &z)) {
goto exit;
+ }
/* modifications for z */
errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
_return_value = cmath_cos_impl(module, z);
@@ -308,8 +315,9 @@ cmath_cosh(PyObject *module, PyObject *arg)
Py_complex z;
Py_complex _return_value;
- if (!PyArg_Parse(arg, "D:cosh", &z))
+ if (!PyArg_Parse(arg, "D:cosh", &z)) {
goto exit;
+ }
/* modifications for z */
errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
_return_value = cmath_cosh_impl(module, z);
@@ -349,8 +357,9 @@ cmath_exp(PyObject *module, PyObject *arg)
Py_complex z;
Py_complex _return_value;
- if (!PyArg_Parse(arg, "D:exp", &z))
+ if (!PyArg_Parse(arg, "D:exp", &z)) {
goto exit;
+ }
/* modifications for z */
errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
_return_value = cmath_exp_impl(module, z);
@@ -390,8 +399,9 @@ cmath_log10(PyObject *module, PyObject *arg)
Py_complex z;
Py_complex _return_value;
- if (!PyArg_Parse(arg, "D:log10", &z))
+ if (!PyArg_Parse(arg, "D:log10", &z)) {
goto exit;
+ }
/* modifications for z */
errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
_return_value = cmath_log10_impl(module, z);
@@ -431,8 +441,9 @@ cmath_sin(PyObject *module, PyObject *arg)
Py_complex z;
Py_complex _return_value;
- if (!PyArg_Parse(arg, "D:sin", &z))
+ if (!PyArg_Parse(arg, "D:sin", &z)) {
goto exit;
+ }
/* modifications for z */
errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
_return_value = cmath_sin_impl(module, z);
@@ -472,8 +483,9 @@ cmath_sinh(PyObject *module, PyObject *arg)
Py_complex z;
Py_complex _return_value;
- if (!PyArg_Parse(arg, "D:sinh", &z))
+ if (!PyArg_Parse(arg, "D:sinh", &z)) {
goto exit;
+ }
/* modifications for z */
errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
_return_value = cmath_sinh_impl(module, z);
@@ -513,8 +525,9 @@ cmath_sqrt(PyObject *module, PyObject *arg)
Py_complex z;
Py_complex _return_value;
- if (!PyArg_Parse(arg, "D:sqrt", &z))
+ if (!PyArg_Parse(arg, "D:sqrt", &z)) {
goto exit;
+ }
/* modifications for z */
errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
_return_value = cmath_sqrt_impl(module, z);
@@ -554,8 +567,9 @@ cmath_tan(PyObject *module, PyObject *arg)
Py_complex z;
Py_complex _return_value;
- if (!PyArg_Parse(arg, "D:tan", &z))
+ if (!PyArg_Parse(arg, "D:tan", &z)) {
goto exit;
+ }
/* modifications for z */
errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
_return_value = cmath_tan_impl(module, z);
@@ -595,8 +609,9 @@ cmath_tanh(PyObject *module, PyObject *arg)
Py_complex z;
Py_complex _return_value;
- if (!PyArg_Parse(arg, "D:tanh", &z))
+ if (!PyArg_Parse(arg, "D:tanh", &z)) {
goto exit;
+ }
/* modifications for z */
errno = 0; PyFPE_START_PROTECT("complex function", goto exit);
_return_value = cmath_tanh_impl(module, z);
@@ -639,8 +654,9 @@ cmath_log(PyObject *module, PyObject *args)
PyObject *y_obj = NULL;
if (!PyArg_ParseTuple(args, "D|O:log",
- &x, &y_obj))
+ &x, &y_obj)) {
goto exit;
+ }
return_value = cmath_log_impl(module, x, y_obj);
exit:
@@ -665,8 +681,9 @@ cmath_phase(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_complex z;
- if (!PyArg_Parse(arg, "D:phase", &z))
+ if (!PyArg_Parse(arg, "D:phase", &z)) {
goto exit;
+ }
return_value = cmath_phase_impl(module, z);
exit:
@@ -693,8 +710,9 @@ cmath_polar(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_complex z;
- if (!PyArg_Parse(arg, "D:polar", &z))
+ if (!PyArg_Parse(arg, "D:polar", &z)) {
goto exit;
+ }
return_value = cmath_polar_impl(module, z);
exit:
@@ -721,8 +739,9 @@ cmath_rect(PyObject *module, PyObject *args)
double phi;
if (!PyArg_ParseTuple(args, "dd:rect",
- &r, &phi))
+ &r, &phi)) {
goto exit;
+ }
return_value = cmath_rect_impl(module, r, phi);
exit:
@@ -747,8 +766,9 @@ cmath_isfinite(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_complex z;
- if (!PyArg_Parse(arg, "D:isfinite", &z))
+ if (!PyArg_Parse(arg, "D:isfinite", &z)) {
goto exit;
+ }
return_value = cmath_isfinite_impl(module, z);
exit:
@@ -773,8 +793,9 @@ cmath_isnan(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_complex z;
- if (!PyArg_Parse(arg, "D:isnan", &z))
+ if (!PyArg_Parse(arg, "D:isnan", &z)) {
goto exit;
+ }
return_value = cmath_isnan_impl(module, z);
exit:
@@ -799,8 +820,9 @@ cmath_isinf(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_complex z;
- if (!PyArg_Parse(arg, "D:isinf", &z))
+ if (!PyArg_Parse(arg, "D:isinf", &z)) {
goto exit;
+ }
return_value = cmath_isinf_impl(module, z);
exit:
@@ -847,14 +869,16 @@ cmath_isclose(PyObject *module, PyObject *args, PyObject *kwargs)
int _return_value;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "DD|$dd:isclose", _keywords,
- &a, &b, &rel_tol, &abs_tol))
+ &a, &b, &rel_tol, &abs_tol)) {
goto exit;
+ }
_return_value = cmath_isclose_impl(module, a, b, rel_tol, abs_tol);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyBool_FromLong((long)_return_value);
exit:
return return_value;
}
-/*[clinic end generated code: output=732194029b7fb1e7 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=0f49dd11b50175bc input=a9049054013a1b77]*/
diff --git a/Modules/clinic/fcntlmodule.c.h b/Modules/clinic/fcntlmodule.c.h
index 67660ebdcb..84a004bb59 100644
--- a/Modules/clinic/fcntlmodule.c.h
+++ b/Modules/clinic/fcntlmodule.c.h
@@ -33,8 +33,9 @@ fcntl_fcntl(PyObject *module, PyObject *args)
PyObject *arg = NULL;
if (!PyArg_ParseTuple(args, "O&i|O:fcntl",
- conv_descriptor, &fd, &code, &arg))
+ conv_descriptor, &fd, &code, &arg)) {
goto exit;
+ }
return_value = fcntl_fcntl_impl(module, fd, code, arg);
exit:
@@ -91,8 +92,9 @@ fcntl_ioctl(PyObject *module, PyObject *args)
int mutate_arg = 1;
if (!PyArg_ParseTuple(args, "O&I|Op:ioctl",
- conv_descriptor, &fd, &code, &ob_arg, &mutate_arg))
+ conv_descriptor, &fd, &code, &ob_arg, &mutate_arg)) {
goto exit;
+ }
return_value = fcntl_ioctl_impl(module, fd, code, ob_arg, mutate_arg);
exit:
@@ -122,8 +124,9 @@ fcntl_flock(PyObject *module, PyObject *args)
int code;
if (!PyArg_ParseTuple(args, "O&i:flock",
- conv_descriptor, &fd, &code))
+ conv_descriptor, &fd, &code)) {
goto exit;
+ }
return_value = fcntl_flock_impl(module, fd, code);
exit:
@@ -175,11 +178,12 @@ fcntl_lockf(PyObject *module, PyObject *args)
int whence = 0;
if (!PyArg_ParseTuple(args, "O&i|OOi:lockf",
- conv_descriptor, &fd, &code, &lenobj, &startobj, &whence))
+ conv_descriptor, &fd, &code, &lenobj, &startobj, &whence)) {
goto exit;
+ }
return_value = fcntl_lockf_impl(module, fd, code, lenobj, startobj, whence);
exit:
return return_value;
}
-/*[clinic end generated code: output=97b1306b864c01c8 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=36cff76a8fb2c9a6 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/grpmodule.c.h b/Modules/clinic/grpmodule.c.h
index 2c47a4236b..82dcd53780 100644
--- a/Modules/clinic/grpmodule.c.h
+++ b/Modules/clinic/grpmodule.c.h
@@ -24,8 +24,9 @@ grp_getgrgid(PyObject *module, PyObject *args, PyObject *kwargs)
PyObject *id;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:getgrgid", _keywords,
- &id))
+ &id)) {
goto exit;
+ }
return_value = grp_getgrgid_impl(module, id);
exit:
@@ -54,8 +55,9 @@ grp_getgrnam(PyObject *module, PyObject *args, PyObject *kwargs)
PyObject *name;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U:getgrnam", _keywords,
- &name))
+ &name)) {
goto exit;
+ }
return_value = grp_getgrnam_impl(module, name);
exit:
@@ -82,4 +84,4 @@ grp_getgrall(PyObject *module, PyObject *Py_UNUSED(ignored))
{
return grp_getgrall_impl(module);
}
-/*[clinic end generated code: output=bee09feefc54a2cb input=a9049054013a1b77]*/
+/*[clinic end generated code: output=8b7502970a29e7f1 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/md5module.c.h b/Modules/clinic/md5module.c.h
index 0bd958ac6e..25dc7bb462 100644
--- a/Modules/clinic/md5module.c.h
+++ b/Modules/clinic/md5module.c.h
@@ -85,11 +85,12 @@ _md5_md5(PyObject *module, PyObject *args, PyObject *kwargs)
PyObject *string = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:md5", _keywords,
- &string))
+ &string)) {
goto exit;
+ }
return_value = _md5_md5_impl(module, string);
exit:
return return_value;
}
-/*[clinic end generated code: output=4cd3cc96e35563d2 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=8f640a98761daffe input=a9049054013a1b77]*/
diff --git a/Modules/clinic/posixmodule.c.h b/Modules/clinic/posixmodule.c.h
index be2e5a6d1a..c550b9ddc7 100644
--- a/Modules/clinic/posixmodule.c.h
+++ b/Modules/clinic/posixmodule.c.h
@@ -42,8 +42,9 @@ os_stat(PyObject *module, PyObject *args, PyObject *kwargs)
int follow_symlinks = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&p:stat", _keywords,
- path_converter, &path, FSTATAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks))
+ path_converter, &path, FSTATAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks)) {
goto exit;
+ }
return_value = os_stat_impl(module, &path, dir_fd, follow_symlinks);
exit:
@@ -77,8 +78,9 @@ os_lstat(PyObject *module, PyObject *args, PyObject *kwargs)
int dir_fd = DEFAULT_DIR_FD;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:lstat", _keywords,
- path_converter, &path, FSTATAT_DIR_FD_CONVERTER, &dir_fd))
+ path_converter, &path, FSTATAT_DIR_FD_CONVERTER, &dir_fd)) {
goto exit;
+ }
return_value = os_lstat_impl(module, &path, dir_fd);
exit:
@@ -140,11 +142,13 @@ os_access(PyObject *module, PyObject *args, PyObject *kwargs)
int _return_value;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&pp:access", _keywords,
- path_converter, &path, &mode, FACCESSAT_DIR_FD_CONVERTER, &dir_fd, &effective_ids, &follow_symlinks))
+ path_converter, &path, &mode, FACCESSAT_DIR_FD_CONVERTER, &dir_fd, &effective_ids, &follow_symlinks)) {
goto exit;
+ }
_return_value = os_access_impl(module, &path, mode, dir_fd, effective_ids, follow_symlinks);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyBool_FromLong((long)_return_value);
exit:
@@ -178,11 +182,13 @@ os_ttyname(PyObject *module, PyObject *arg)
int fd;
char *_return_value;
- if (!PyArg_Parse(arg, "i:ttyname", &fd))
+ if (!PyArg_Parse(arg, "i:ttyname", &fd)) {
goto exit;
+ }
_return_value = os_ttyname_impl(module, fd);
- if (_return_value == NULL)
+ if (_return_value == NULL) {
goto exit;
+ }
return_value = PyUnicode_DecodeFSDefault(_return_value);
exit:
@@ -237,8 +243,9 @@ os_chdir(PyObject *module, PyObject *args, PyObject *kwargs)
path_t path = PATH_T_INITIALIZE("chdir", "path", 0, PATH_HAVE_FCHDIR);
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chdir", _keywords,
- path_converter, &path))
+ path_converter, &path)) {
goto exit;
+ }
return_value = os_chdir_impl(module, &path);
exit:
@@ -273,8 +280,9 @@ os_fchdir(PyObject *module, PyObject *args, PyObject *kwargs)
int fd;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:fchdir", _keywords,
- fildes_converter, &fd))
+ fildes_converter, &fd)) {
goto exit;
+ }
return_value = os_fchdir_impl(module, fd);
exit:
@@ -327,8 +335,9 @@ os_chmod(PyObject *module, PyObject *args, PyObject *kwargs)
int follow_symlinks = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&p:chmod", _keywords,
- path_converter, &path, &mode, FCHMODAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks))
+ path_converter, &path, &mode, FCHMODAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks)) {
goto exit;
+ }
return_value = os_chmod_impl(module, &path, mode, dir_fd, follow_symlinks);
exit:
@@ -363,8 +372,9 @@ os_fchmod(PyObject *module, PyObject *args, PyObject *kwargs)
int mode;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii:fchmod", _keywords,
- &fd, &mode))
+ &fd, &mode)) {
goto exit;
+ }
return_value = os_fchmod_impl(module, fd, mode);
exit:
@@ -399,8 +409,9 @@ os_lchmod(PyObject *module, PyObject *args, PyObject *kwargs)
int mode;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i:lchmod", _keywords,
- path_converter, &path, &mode))
+ path_converter, &path, &mode)) {
goto exit;
+ }
return_value = os_lchmod_impl(module, &path, mode);
exit:
@@ -443,8 +454,9 @@ os_chflags(PyObject *module, PyObject *args, PyObject *kwargs)
int follow_symlinks = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k|p:chflags", _keywords,
- path_converter, &path, &flags, &follow_symlinks))
+ path_converter, &path, &flags, &follow_symlinks)) {
goto exit;
+ }
return_value = os_chflags_impl(module, &path, flags, follow_symlinks);
exit:
@@ -482,8 +494,9 @@ os_lchflags(PyObject *module, PyObject *args, PyObject *kwargs)
unsigned long flags;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k:lchflags", _keywords,
- path_converter, &path, &flags))
+ path_converter, &path, &flags)) {
goto exit;
+ }
return_value = os_lchflags_impl(module, &path, flags);
exit:
@@ -517,8 +530,9 @@ os_chroot(PyObject *module, PyObject *args, PyObject *kwargs)
path_t path = PATH_T_INITIALIZE("chroot", "path", 0, 0);
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chroot", _keywords,
- path_converter, &path))
+ path_converter, &path)) {
goto exit;
+ }
return_value = os_chroot_impl(module, &path);
exit:
@@ -552,8 +566,9 @@ os_fsync(PyObject *module, PyObject *args, PyObject *kwargs)
int fd;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:fsync", _keywords,
- fildes_converter, &fd))
+ fildes_converter, &fd)) {
goto exit;
+ }
return_value = os_fsync_impl(module, fd);
exit:
@@ -606,8 +621,9 @@ os_fdatasync(PyObject *module, PyObject *args, PyObject *kwargs)
int fd;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:fdatasync", _keywords,
- fildes_converter, &fd))
+ fildes_converter, &fd)) {
goto exit;
+ }
return_value = os_fdatasync_impl(module, fd);
exit:
@@ -667,8 +683,9 @@ os_chown(PyObject *module, PyObject *args, PyObject *kwargs)
int follow_symlinks = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&O&|$O&p:chown", _keywords,
- path_converter, &path, _Py_Uid_Converter, &uid, _Py_Gid_Converter, &gid, FCHOWNAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks))
+ path_converter, &path, _Py_Uid_Converter, &uid, _Py_Gid_Converter, &gid, FCHOWNAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks)) {
goto exit;
+ }
return_value = os_chown_impl(module, &path, uid, gid, dir_fd, follow_symlinks);
exit:
@@ -706,8 +723,9 @@ os_fchown(PyObject *module, PyObject *args, PyObject *kwargs)
gid_t gid;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iO&O&:fchown", _keywords,
- &fd, _Py_Uid_Converter, &uid, _Py_Gid_Converter, &gid))
+ &fd, _Py_Uid_Converter, &uid, _Py_Gid_Converter, &gid)) {
goto exit;
+ }
return_value = os_fchown_impl(module, fd, uid, gid);
exit:
@@ -743,8 +761,9 @@ os_lchown(PyObject *module, PyObject *args, PyObject *kwargs)
gid_t gid;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&O&:lchown", _keywords,
- path_converter, &path, _Py_Uid_Converter, &uid, _Py_Gid_Converter, &gid))
+ path_converter, &path, _Py_Uid_Converter, &uid, _Py_Gid_Converter, &gid)) {
goto exit;
+ }
return_value = os_lchown_impl(module, &path, uid, gid);
exit:
@@ -830,8 +849,9 @@ os_link(PyObject *module, PyObject *args, PyObject *kwargs)
int follow_symlinks = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$O&O&p:link", _keywords,
- path_converter, &src, path_converter, &dst, dir_fd_converter, &src_dir_fd, dir_fd_converter, &dst_dir_fd, &follow_symlinks))
+ path_converter, &src, path_converter, &dst, dir_fd_converter, &src_dir_fd, dir_fd_converter, &dst_dir_fd, &follow_symlinks)) {
goto exit;
+ }
return_value = os_link_impl(module, &src, &dst, src_dir_fd, dst_dir_fd, follow_symlinks);
exit:
@@ -876,8 +896,9 @@ os_listdir(PyObject *module, PyObject *args, PyObject *kwargs)
path_t path = PATH_T_INITIALIZE("listdir", "path", 1, PATH_HAVE_FDOPENDIR);
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", _keywords,
- path_converter, &path))
+ path_converter, &path)) {
goto exit;
+ }
return_value = os_listdir_impl(module, &path);
exit:
@@ -906,8 +927,9 @@ os__getfullpathname(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
path_t path = PATH_T_INITIALIZE("_getfullpathname", "path", 0, 0);
- if (!PyArg_Parse(arg, "O&:_getfullpathname", path_converter, &path))
+ if (!PyArg_Parse(arg, "O&:_getfullpathname", path_converter, &path)) {
goto exit;
+ }
return_value = os__getfullpathname_impl(module, &path);
exit:
@@ -939,8 +961,9 @@ os__getfinalpathname(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
PyObject *path;
- if (!PyArg_Parse(arg, "U:_getfinalpathname", &path))
+ if (!PyArg_Parse(arg, "U:_getfinalpathname", &path)) {
goto exit;
+ }
return_value = os__getfinalpathname_impl(module, path);
exit:
@@ -968,8 +991,9 @@ os__isdir(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
path_t path = PATH_T_INITIALIZE("_isdir", "path", 0, 0);
- if (!PyArg_Parse(arg, "O&:_isdir", path_converter, &path))
+ if (!PyArg_Parse(arg, "O&:_isdir", path_converter, &path)) {
goto exit;
+ }
return_value = os__isdir_impl(module, &path);
exit:
@@ -1003,8 +1027,9 @@ os__getvolumepathname(PyObject *module, PyObject *args, PyObject *kwargs)
PyObject *path;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U:_getvolumepathname", _keywords,
- &path))
+ &path)) {
goto exit;
+ }
return_value = os__getvolumepathname_impl(module, path);
exit:
@@ -1042,8 +1067,9 @@ os_mkdir(PyObject *module, PyObject *args, PyObject *kwargs)
int dir_fd = DEFAULT_DIR_FD;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkdir", _keywords,
- path_converter, &path, &mode, MKDIRAT_DIR_FD_CONVERTER, &dir_fd))
+ path_converter, &path, &mode, MKDIRAT_DIR_FD_CONVERTER, &dir_fd)) {
goto exit;
+ }
return_value = os_mkdir_impl(module, &path, mode, dir_fd);
exit:
@@ -1073,8 +1099,9 @@ os_nice(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
int increment;
- if (!PyArg_Parse(arg, "i:nice", &increment))
+ if (!PyArg_Parse(arg, "i:nice", &increment)) {
goto exit;
+ }
return_value = os_nice_impl(module, increment);
exit:
@@ -1106,8 +1133,9 @@ os_getpriority(PyObject *module, PyObject *args, PyObject *kwargs)
int who;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii:getpriority", _keywords,
- &which, &who))
+ &which, &who)) {
goto exit;
+ }
return_value = os_getpriority_impl(module, which, who);
exit:
@@ -1140,8 +1168,9 @@ os_setpriority(PyObject *module, PyObject *args, PyObject *kwargs)
int priority;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iii:setpriority", _keywords,
- &which, &who, &priority))
+ &which, &who, &priority)) {
goto exit;
+ }
return_value = os_setpriority_impl(module, which, who, priority);
exit:
@@ -1180,8 +1209,9 @@ os_rename(PyObject *module, PyObject *args, PyObject *kwargs)
int dst_dir_fd = DEFAULT_DIR_FD;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$O&O&:rename", _keywords,
- path_converter, &src, path_converter, &dst, dir_fd_converter, &src_dir_fd, dir_fd_converter, &dst_dir_fd))
+ path_converter, &src, path_converter, &dst, dir_fd_converter, &src_dir_fd, dir_fd_converter, &dst_dir_fd)) {
goto exit;
+ }
return_value = os_rename_impl(module, &src, &dst, src_dir_fd, dst_dir_fd);
exit:
@@ -1223,8 +1253,9 @@ os_replace(PyObject *module, PyObject *args, PyObject *kwargs)
int dst_dir_fd = DEFAULT_DIR_FD;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$O&O&:replace", _keywords,
- path_converter, &src, path_converter, &dst, dir_fd_converter, &src_dir_fd, dir_fd_converter, &dst_dir_fd))
+ path_converter, &src, path_converter, &dst, dir_fd_converter, &src_dir_fd, dir_fd_converter, &dst_dir_fd)) {
goto exit;
+ }
return_value = os_replace_impl(module, &src, &dst, src_dir_fd, dst_dir_fd);
exit:
@@ -1262,8 +1293,9 @@ os_rmdir(PyObject *module, PyObject *args, PyObject *kwargs)
int dir_fd = DEFAULT_DIR_FD;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:rmdir", _keywords,
- path_converter, &path, UNLINKAT_DIR_FD_CONVERTER, &dir_fd))
+ path_converter, &path, UNLINKAT_DIR_FD_CONVERTER, &dir_fd)) {
goto exit;
+ }
return_value = os_rmdir_impl(module, &path, dir_fd);
exit:
@@ -1296,11 +1328,13 @@ os_system(PyObject *module, PyObject *args, PyObject *kwargs)
long _return_value;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "u:system", _keywords,
- &command))
+ &command)) {
goto exit;
+ }
_return_value = os_system_impl(module, command);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong(_return_value);
exit:
@@ -1332,11 +1366,13 @@ os_system(PyObject *module, PyObject *args, PyObject *kwargs)
long _return_value;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:system", _keywords,
- PyUnicode_FSConverter, &command))
+ PyUnicode_FSConverter, &command)) {
goto exit;
+ }
_return_value = os_system_impl(module, command);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong(_return_value);
exit:
@@ -1366,8 +1402,9 @@ os_umask(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
int mask;
- if (!PyArg_Parse(arg, "i:umask", &mask))
+ if (!PyArg_Parse(arg, "i:umask", &mask)) {
goto exit;
+ }
return_value = os_umask_impl(module, mask);
exit:
@@ -1400,8 +1437,9 @@ os_unlink(PyObject *module, PyObject *args, PyObject *kwargs)
int dir_fd = DEFAULT_DIR_FD;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:unlink", _keywords,
- path_converter, &path, UNLINKAT_DIR_FD_CONVERTER, &dir_fd))
+ path_converter, &path, UNLINKAT_DIR_FD_CONVERTER, &dir_fd)) {
goto exit;
+ }
return_value = os_unlink_impl(module, &path, dir_fd);
exit:
@@ -1437,8 +1475,9 @@ os_remove(PyObject *module, PyObject *args, PyObject *kwargs)
int dir_fd = DEFAULT_DIR_FD;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:remove", _keywords,
- path_converter, &path, UNLINKAT_DIR_FD_CONVERTER, &dir_fd))
+ path_converter, &path, UNLINKAT_DIR_FD_CONVERTER, &dir_fd)) {
goto exit;
+ }
return_value = os_remove_impl(module, &path, dir_fd);
exit:
@@ -1521,8 +1560,9 @@ os_utime(PyObject *module, PyObject *args, PyObject *kwargs)
int follow_symlinks = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|O$OO&p:utime", _keywords,
- path_converter, &path, &times, &ns, FUTIMENSAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks))
+ path_converter, &path, &times, &ns, FUTIMENSAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks)) {
goto exit;
+ }
return_value = os_utime_impl(module, &path, times, ns, dir_fd, follow_symlinks);
exit:
@@ -1552,8 +1592,9 @@ os__exit(PyObject *module, PyObject *args, PyObject *kwargs)
int status;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:_exit", _keywords,
- &status))
+ &status)) {
goto exit;
+ }
return_value = os__exit_impl(module, status);
exit:
@@ -1587,8 +1628,9 @@ os_execv(PyObject *module, PyObject *args)
PyObject *argv;
if (!PyArg_ParseTuple(args, "O&O:execv",
- PyUnicode_FSConverter, &path, &argv))
+ PyUnicode_FSConverter, &path, &argv)) {
goto exit;
+ }
return_value = os_execv_impl(module, path, argv);
exit:
@@ -1631,8 +1673,9 @@ os_execve(PyObject *module, PyObject *args, PyObject *kwargs)
PyObject *env;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&OO:execve", _keywords,
- path_converter, &path, &argv, &env))
+ path_converter, &path, &argv, &env)) {
goto exit;
+ }
return_value = os_execve_impl(module, &path, argv, env);
exit:
@@ -1674,8 +1717,9 @@ os_spawnv(PyObject *module, PyObject *args)
PyObject *argv;
if (!PyArg_ParseTuple(args, "iO&O:spawnv",
- &mode, PyUnicode_FSConverter, &path, &argv))
+ &mode, PyUnicode_FSConverter, &path, &argv)) {
goto exit;
+ }
return_value = os_spawnv_impl(module, mode, path, argv);
exit:
@@ -1721,8 +1765,9 @@ os_spawnve(PyObject *module, PyObject *args)
PyObject *env;
if (!PyArg_ParseTuple(args, "iO&OO:spawnve",
- &mode, PyUnicode_FSConverter, &path, &argv, &env))
+ &mode, PyUnicode_FSConverter, &path, &argv, &env)) {
goto exit;
+ }
return_value = os_spawnve_impl(module, mode, path, argv, env);
exit:
@@ -1804,8 +1849,9 @@ os_sched_get_priority_max(PyObject *module, PyObject *args, PyObject *kwargs)
int policy;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:sched_get_priority_max", _keywords,
- &policy))
+ &policy)) {
goto exit;
+ }
return_value = os_sched_get_priority_max_impl(module, policy);
exit:
@@ -1836,8 +1882,9 @@ os_sched_get_priority_min(PyObject *module, PyObject *args, PyObject *kwargs)
int policy;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:sched_get_priority_min", _keywords,
- &policy))
+ &policy)) {
goto exit;
+ }
return_value = os_sched_get_priority_min_impl(module, policy);
exit:
@@ -1868,8 +1915,9 @@ os_sched_getscheduler(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
pid_t pid;
- if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getscheduler", &pid))
+ if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getscheduler", &pid)) {
goto exit;
+ }
return_value = os_sched_getscheduler_impl(module, pid);
exit:
@@ -1900,8 +1948,9 @@ os_sched_param(PyTypeObject *type, PyObject *args, PyObject *kwargs)
PyObject *sched_priority;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", _keywords,
- &sched_priority))
+ &sched_priority)) {
goto exit;
+ }
return_value = os_sched_param_impl(type, sched_priority);
exit:
@@ -1937,8 +1986,9 @@ os_sched_setscheduler(PyObject *module, PyObject *args)
struct sched_param param;
if (!PyArg_ParseTuple(args, "" _Py_PARSE_PID "iO&:sched_setscheduler",
- &pid, &policy, convert_sched_param, &param))
+ &pid, &policy, convert_sched_param, &param)) {
goto exit;
+ }
return_value = os_sched_setscheduler_impl(module, pid, policy, &param);
exit:
@@ -1970,8 +2020,9 @@ os_sched_getparam(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
pid_t pid;
- if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getparam", &pid))
+ if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getparam", &pid)) {
goto exit;
+ }
return_value = os_sched_getparam_impl(module, pid);
exit:
@@ -2006,8 +2057,9 @@ os_sched_setparam(PyObject *module, PyObject *args)
struct sched_param param;
if (!PyArg_ParseTuple(args, "" _Py_PARSE_PID "O&:sched_setparam",
- &pid, convert_sched_param, &param))
+ &pid, convert_sched_param, &param)) {
goto exit;
+ }
return_value = os_sched_setparam_impl(module, pid, &param);
exit:
@@ -2039,11 +2091,13 @@ os_sched_rr_get_interval(PyObject *module, PyObject *arg)
pid_t pid;
double _return_value;
- if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_rr_get_interval", &pid))
+ if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_rr_get_interval", &pid)) {
goto exit;
+ }
_return_value = os_sched_rr_get_interval_impl(module, pid);
- if ((_return_value == -1.0) && PyErr_Occurred())
+ if ((_return_value == -1.0) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyFloat_FromDouble(_return_value);
exit:
@@ -2098,8 +2152,9 @@ os_sched_setaffinity(PyObject *module, PyObject *args)
PyObject *mask;
if (!PyArg_ParseTuple(args, "" _Py_PARSE_PID "O:sched_setaffinity",
- &pid, &mask))
+ &pid, &mask)) {
goto exit;
+ }
return_value = os_sched_setaffinity_impl(module, pid, mask);
exit:
@@ -2114,7 +2169,7 @@ PyDoc_STRVAR(os_sched_getaffinity__doc__,
"sched_getaffinity($module, pid, /)\n"
"--\n"
"\n"
-"Return the affinity of the process identified by pid.\n"
+"Return the affinity of the process identified by pid (or the current process if zero).\n"
"\n"
"The affinity is returned as a set of CPU identifiers.");
@@ -2130,8 +2185,9 @@ os_sched_getaffinity(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
pid_t pid;
- if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getaffinity", &pid))
+ if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":sched_getaffinity", &pid)) {
goto exit;
+ }
return_value = os_sched_getaffinity_impl(module, pid);
exit:
@@ -2320,8 +2376,9 @@ os_getpgid(PyObject *module, PyObject *args, PyObject *kwargs)
pid_t pid;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "" _Py_PARSE_PID ":getpgid", _keywords,
- &pid))
+ &pid)) {
goto exit;
+ }
return_value = os_getpgid_impl(module, pid);
exit:
@@ -2465,8 +2522,9 @@ os_kill(PyObject *module, PyObject *args)
Py_ssize_t signal;
if (!PyArg_ParseTuple(args, "" _Py_PARSE_PID "n:kill",
- &pid, &signal))
+ &pid, &signal)) {
goto exit;
+ }
return_value = os_kill_impl(module, pid, signal);
exit:
@@ -2497,8 +2555,9 @@ os_killpg(PyObject *module, PyObject *args)
int signal;
if (!PyArg_ParseTuple(args, "" _Py_PARSE_PID "i:killpg",
- &pgid, &signal))
+ &pgid, &signal)) {
goto exit;
+ }
return_value = os_killpg_impl(module, pgid, signal);
exit:
@@ -2527,8 +2586,9 @@ os_plock(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
int op;
- if (!PyArg_Parse(arg, "i:plock", &op))
+ if (!PyArg_Parse(arg, "i:plock", &op)) {
goto exit;
+ }
return_value = os_plock_impl(module, op);
exit:
@@ -2557,8 +2617,9 @@ os_setuid(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
uid_t uid;
- if (!PyArg_Parse(arg, "O&:setuid", _Py_Uid_Converter, &uid))
+ if (!PyArg_Parse(arg, "O&:setuid", _Py_Uid_Converter, &uid)) {
goto exit;
+ }
return_value = os_setuid_impl(module, uid);
exit:
@@ -2587,8 +2648,9 @@ os_seteuid(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
uid_t euid;
- if (!PyArg_Parse(arg, "O&:seteuid", _Py_Uid_Converter, &euid))
+ if (!PyArg_Parse(arg, "O&:seteuid", _Py_Uid_Converter, &euid)) {
goto exit;
+ }
return_value = os_seteuid_impl(module, euid);
exit:
@@ -2617,8 +2679,9 @@ os_setegid(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
gid_t egid;
- if (!PyArg_Parse(arg, "O&:setegid", _Py_Gid_Converter, &egid))
+ if (!PyArg_Parse(arg, "O&:setegid", _Py_Gid_Converter, &egid)) {
goto exit;
+ }
return_value = os_setegid_impl(module, egid);
exit:
@@ -2649,8 +2712,9 @@ os_setreuid(PyObject *module, PyObject *args)
uid_t euid;
if (!PyArg_ParseTuple(args, "O&O&:setreuid",
- _Py_Uid_Converter, &ruid, _Py_Uid_Converter, &euid))
+ _Py_Uid_Converter, &ruid, _Py_Uid_Converter, &euid)) {
goto exit;
+ }
return_value = os_setreuid_impl(module, ruid, euid);
exit:
@@ -2681,8 +2745,9 @@ os_setregid(PyObject *module, PyObject *args)
gid_t egid;
if (!PyArg_ParseTuple(args, "O&O&:setregid",
- _Py_Gid_Converter, &rgid, _Py_Gid_Converter, &egid))
+ _Py_Gid_Converter, &rgid, _Py_Gid_Converter, &egid)) {
goto exit;
+ }
return_value = os_setregid_impl(module, rgid, egid);
exit:
@@ -2711,8 +2776,9 @@ os_setgid(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
gid_t gid;
- if (!PyArg_Parse(arg, "O&:setgid", _Py_Gid_Converter, &gid))
+ if (!PyArg_Parse(arg, "O&:setgid", _Py_Gid_Converter, &gid)) {
goto exit;
+ }
return_value = os_setgid_impl(module, gid);
exit:
@@ -2759,8 +2825,9 @@ os_wait3(PyObject *module, PyObject *args, PyObject *kwargs)
int options;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:wait3", _keywords,
- &options))
+ &options)) {
goto exit;
+ }
return_value = os_wait3_impl(module, options);
exit:
@@ -2795,8 +2862,9 @@ os_wait4(PyObject *module, PyObject *args, PyObject *kwargs)
int options;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "" _Py_PARSE_PID "i:wait4", _keywords,
- &pid, &options))
+ &pid, &options)) {
goto exit;
+ }
return_value = os_wait4_impl(module, pid, options);
exit:
@@ -2839,8 +2907,9 @@ os_waitid(PyObject *module, PyObject *args)
int options;
if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid",
- &idtype, &id, &options))
+ &idtype, &id, &options)) {
goto exit;
+ }
return_value = os_waitid_impl(module, idtype, id, options);
exit:
@@ -2876,8 +2945,9 @@ os_waitpid(PyObject *module, PyObject *args)
int options;
if (!PyArg_ParseTuple(args, "" _Py_PARSE_PID "i:waitpid",
- &pid, &options))
+ &pid, &options)) {
goto exit;
+ }
return_value = os_waitpid_impl(module, pid, options);
exit:
@@ -2913,8 +2983,9 @@ os_waitpid(PyObject *module, PyObject *args)
int options;
if (!PyArg_ParseTuple(args, "" _Py_PARSE_INTPTR "i:waitpid",
- &pid, &options))
+ &pid, &options)) {
goto exit;
+ }
return_value = os_waitpid_impl(module, pid, options);
exit:
@@ -2984,8 +3055,9 @@ os_symlink(PyObject *module, PyObject *args, PyObject *kwargs)
int dir_fd = DEFAULT_DIR_FD;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|p$O&:symlink", _keywords,
- path_converter, &src, path_converter, &dst, &target_is_directory, SYMLINKAT_DIR_FD_CONVERTER, &dir_fd))
+ path_converter, &src, path_converter, &dst, &target_is_directory, SYMLINKAT_DIR_FD_CONVERTER, &dir_fd)) {
goto exit;
+ }
return_value = os_symlink_impl(module, &src, &dst, target_is_directory, dir_fd);
exit:
@@ -3045,8 +3117,9 @@ os_getsid(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
pid_t pid;
- if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":getsid", &pid))
+ if (!PyArg_Parse(arg, "" _Py_PARSE_PID ":getsid", &pid)) {
goto exit;
+ }
return_value = os_getsid_impl(module, pid);
exit:
@@ -3099,8 +3172,9 @@ os_setpgid(PyObject *module, PyObject *args)
pid_t pgrp;
if (!PyArg_ParseTuple(args, "" _Py_PARSE_PID "" _Py_PARSE_PID ":setpgid",
- &pid, &pgrp))
+ &pid, &pgrp)) {
goto exit;
+ }
return_value = os_setpgid_impl(module, pid, pgrp);
exit:
@@ -3129,8 +3203,9 @@ os_tcgetpgrp(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
int fd;
- if (!PyArg_Parse(arg, "i:tcgetpgrp", &fd))
+ if (!PyArg_Parse(arg, "i:tcgetpgrp", &fd)) {
goto exit;
+ }
return_value = os_tcgetpgrp_impl(module, fd);
exit:
@@ -3161,8 +3236,9 @@ os_tcsetpgrp(PyObject *module, PyObject *args)
pid_t pgid;
if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp",
- &fd, &pgid))
+ &fd, &pgid)) {
goto exit;
+ }
return_value = os_tcsetpgrp_impl(module, fd, pgid);
exit:
@@ -3200,11 +3276,13 @@ os_open(PyObject *module, PyObject *args, PyObject *kwargs)
int _return_value;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|i$O&:open", _keywords,
- path_converter, &path, &flags, &mode, OPENAT_DIR_FD_CONVERTER, &dir_fd))
+ path_converter, &path, &flags, &mode, OPENAT_DIR_FD_CONVERTER, &dir_fd)) {
goto exit;
+ }
_return_value = os_open_impl(module, &path, flags, mode, dir_fd);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong((long)_return_value);
exit:
@@ -3234,8 +3312,9 @@ os_close(PyObject *module, PyObject *args, PyObject *kwargs)
int fd;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:close", _keywords,
- &fd))
+ &fd)) {
goto exit;
+ }
return_value = os_close_impl(module, fd);
exit:
@@ -3262,8 +3341,9 @@ os_closerange(PyObject *module, PyObject *args)
int fd_high;
if (!PyArg_ParseTuple(args, "ii:closerange",
- &fd_low, &fd_high))
+ &fd_low, &fd_high)) {
goto exit;
+ }
return_value = os_closerange_impl(module, fd_low, fd_high);
exit:
@@ -3289,11 +3369,13 @@ os_dup(PyObject *module, PyObject *arg)
int fd;
int _return_value;
- if (!PyArg_Parse(arg, "i:dup", &fd))
+ if (!PyArg_Parse(arg, "i:dup", &fd)) {
goto exit;
+ }
_return_value = os_dup_impl(module, fd);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong((long)_return_value);
exit:
@@ -3322,8 +3404,9 @@ os_dup2(PyObject *module, PyObject *args, PyObject *kwargs)
int inheritable = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|p:dup2", _keywords,
- &fd, &fd2, &inheritable))
+ &fd, &fd2, &inheritable)) {
goto exit;
+ }
return_value = os_dup2_impl(module, fd, fd2, inheritable);
exit:
@@ -3360,8 +3443,9 @@ os_lockf(PyObject *module, PyObject *args)
Py_off_t length;
if (!PyArg_ParseTuple(args, "iiO&:lockf",
- &fd, &command, Py_off_t_converter, &length))
+ &fd, &command, Py_off_t_converter, &length)) {
goto exit;
+ }
return_value = os_lockf_impl(module, fd, command, length);
exit:
@@ -3395,11 +3479,13 @@ os_lseek(PyObject *module, PyObject *args)
Py_off_t _return_value;
if (!PyArg_ParseTuple(args, "iO&i:lseek",
- &fd, Py_off_t_converter, &position, &how))
+ &fd, Py_off_t_converter, &position, &how)) {
goto exit;
+ }
_return_value = os_lseek_impl(module, fd, position, how);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromPy_off_t(_return_value);
exit:
@@ -3426,8 +3512,9 @@ os_read(PyObject *module, PyObject *args)
Py_ssize_t length;
if (!PyArg_ParseTuple(args, "in:read",
- &fd, &length))
+ &fd, &length)) {
goto exit;
+ }
return_value = os_read_impl(module, fd, length);
exit:
@@ -3465,11 +3552,13 @@ os_readv(PyObject *module, PyObject *args)
Py_ssize_t _return_value;
if (!PyArg_ParseTuple(args, "iO:readv",
- &fd, &buffers))
+ &fd, &buffers)) {
goto exit;
+ }
_return_value = os_readv_impl(module, fd, buffers);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromSsize_t(_return_value);
exit:
@@ -3504,8 +3593,9 @@ os_pread(PyObject *module, PyObject *args)
Py_off_t offset;
if (!PyArg_ParseTuple(args, "iiO&:pread",
- &fd, &length, Py_off_t_converter, &offset))
+ &fd, &length, Py_off_t_converter, &offset)) {
goto exit;
+ }
return_value = os_pread_impl(module, fd, length, offset);
exit:
@@ -3535,17 +3625,20 @@ os_write(PyObject *module, PyObject *args)
Py_ssize_t _return_value;
if (!PyArg_ParseTuple(args, "iy*:write",
- &fd, &data))
+ &fd, &data)) {
goto exit;
+ }
_return_value = os_write_impl(module, fd, &data);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromSsize_t(_return_value);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -3573,8 +3666,9 @@ os_fstat(PyObject *module, PyObject *args, PyObject *kwargs)
int fd;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:fstat", _keywords,
- &fd))
+ &fd)) {
goto exit;
+ }
return_value = os_fstat_impl(module, fd);
exit:
@@ -3603,11 +3697,13 @@ os_isatty(PyObject *module, PyObject *arg)
int fd;
int _return_value;
- if (!PyArg_Parse(arg, "i:isatty", &fd))
+ if (!PyArg_Parse(arg, "i:isatty", &fd)) {
goto exit;
+ }
_return_value = os_isatty_impl(module, fd);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyBool_FromLong((long)_return_value);
exit:
@@ -3665,8 +3761,9 @@ os_pipe2(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
int flags;
- if (!PyArg_Parse(arg, "i:pipe2", &flags))
+ if (!PyArg_Parse(arg, "i:pipe2", &flags)) {
goto exit;
+ }
return_value = os_pipe2_impl(module, flags);
exit:
@@ -3701,11 +3798,13 @@ os_writev(PyObject *module, PyObject *args)
Py_ssize_t _return_value;
if (!PyArg_ParseTuple(args, "iO:writev",
- &fd, &buffers))
+ &fd, &buffers)) {
goto exit;
+ }
_return_value = os_writev_impl(module, fd, buffers);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromSsize_t(_return_value);
exit:
@@ -3742,17 +3841,20 @@ os_pwrite(PyObject *module, PyObject *args)
Py_ssize_t _return_value;
if (!PyArg_ParseTuple(args, "iy*O&:pwrite",
- &fd, &buffer, Py_off_t_converter, &offset))
+ &fd, &buffer, Py_off_t_converter, &offset)) {
goto exit;
+ }
_return_value = os_pwrite_impl(module, fd, &buffer, offset);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromSsize_t(_return_value);
exit:
/* Cleanup for buffer */
- if (buffer.obj)
+ if (buffer.obj) {
PyBuffer_Release(&buffer);
+ }
return return_value;
}
@@ -3788,8 +3890,9 @@ os_mkfifo(PyObject *module, PyObject *args, PyObject *kwargs)
int dir_fd = DEFAULT_DIR_FD;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkfifo", _keywords,
- path_converter, &path, &mode, MKFIFOAT_DIR_FD_CONVERTER, &dir_fd))
+ path_converter, &path, &mode, MKFIFOAT_DIR_FD_CONVERTER, &dir_fd)) {
goto exit;
+ }
return_value = os_mkfifo_impl(module, &path, mode, dir_fd);
exit:
@@ -3839,8 +3942,9 @@ os_mknod(PyObject *module, PyObject *args, PyObject *kwargs)
int dir_fd = DEFAULT_DIR_FD;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|iO&$O&:mknod", _keywords,
- path_converter, &path, &mode, _Py_Dev_Converter, &device, MKNODAT_DIR_FD_CONVERTER, &dir_fd))
+ path_converter, &path, &mode, _Py_Dev_Converter, &device, MKNODAT_DIR_FD_CONVERTER, &dir_fd)) {
goto exit;
+ }
return_value = os_mknod_impl(module, &path, mode, device, dir_fd);
exit:
@@ -3873,11 +3977,13 @@ os_major(PyObject *module, PyObject *arg)
dev_t device;
unsigned int _return_value;
- if (!PyArg_Parse(arg, "O&:major", _Py_Dev_Converter, &device))
+ if (!PyArg_Parse(arg, "O&:major", _Py_Dev_Converter, &device)) {
goto exit;
+ }
_return_value = os_major_impl(module, device);
- if ((_return_value == (unsigned int)-1) && PyErr_Occurred())
+ if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
exit:
@@ -3907,11 +4013,13 @@ os_minor(PyObject *module, PyObject *arg)
dev_t device;
unsigned int _return_value;
- if (!PyArg_Parse(arg, "O&:minor", _Py_Dev_Converter, &device))
+ if (!PyArg_Parse(arg, "O&:minor", _Py_Dev_Converter, &device)) {
goto exit;
+ }
_return_value = os_minor_impl(module, device);
- if ((_return_value == (unsigned int)-1) && PyErr_Occurred())
+ if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
exit:
@@ -3943,11 +4051,13 @@ os_makedev(PyObject *module, PyObject *args)
dev_t _return_value;
if (!PyArg_ParseTuple(args, "ii:makedev",
- &major, &minor))
+ &major, &minor)) {
goto exit;
+ }
_return_value = os_makedev_impl(module, major, minor);
- if ((_return_value == (dev_t)-1) && PyErr_Occurred())
+ if ((_return_value == (dev_t)-1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = _PyLong_FromDev(_return_value);
exit:
@@ -3978,8 +4088,9 @@ os_ftruncate(PyObject *module, PyObject *args)
Py_off_t length;
if (!PyArg_ParseTuple(args, "iO&:ftruncate",
- &fd, Py_off_t_converter, &length))
+ &fd, Py_off_t_converter, &length)) {
goto exit;
+ }
return_value = os_ftruncate_impl(module, fd, length);
exit:
@@ -4014,8 +4125,9 @@ os_truncate(PyObject *module, PyObject *args, PyObject *kwargs)
Py_off_t length;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:truncate", _keywords,
- path_converter, &path, Py_off_t_converter, &length))
+ path_converter, &path, Py_off_t_converter, &length)) {
goto exit;
+ }
return_value = os_truncate_impl(module, &path, length);
exit:
@@ -4054,8 +4166,9 @@ os_posix_fallocate(PyObject *module, PyObject *args)
Py_off_t length;
if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
- &fd, Py_off_t_converter, &offset, Py_off_t_converter, &length))
+ &fd, Py_off_t_converter, &offset, Py_off_t_converter, &length)) {
goto exit;
+ }
return_value = os_posix_fallocate_impl(module, fd, offset, length);
exit:
@@ -4097,8 +4210,9 @@ os_posix_fadvise(PyObject *module, PyObject *args)
int advice;
if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
- &fd, Py_off_t_converter, &offset, Py_off_t_converter, &length, &advice))
+ &fd, Py_off_t_converter, &offset, Py_off_t_converter, &length, &advice)) {
goto exit;
+ }
return_value = os_posix_fadvise_impl(module, fd, offset, length, advice);
exit:
@@ -4129,8 +4243,9 @@ os_putenv(PyObject *module, PyObject *args)
PyObject *value;
if (!PyArg_ParseTuple(args, "UU:putenv",
- &name, &value))
+ &name, &value)) {
goto exit;
+ }
return_value = os_putenv_impl(module, name, value);
exit:
@@ -4161,8 +4276,9 @@ os_putenv(PyObject *module, PyObject *args)
PyObject *value = NULL;
if (!PyArg_ParseTuple(args, "O&O&:putenv",
- PyUnicode_FSConverter, &name, PyUnicode_FSConverter, &value))
+ PyUnicode_FSConverter, &name, PyUnicode_FSConverter, &value)) {
goto exit;
+ }
return_value = os_putenv_impl(module, name, value);
exit:
@@ -4196,8 +4312,9 @@ os_unsetenv(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
PyObject *name = NULL;
- if (!PyArg_Parse(arg, "O&:unsetenv", PyUnicode_FSConverter, &name))
+ if (!PyArg_Parse(arg, "O&:unsetenv", PyUnicode_FSConverter, &name)) {
goto exit;
+ }
return_value = os_unsetenv_impl(module, name);
exit:
@@ -4227,8 +4344,9 @@ os_strerror(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
int code;
- if (!PyArg_Parse(arg, "i:strerror", &code))
+ if (!PyArg_Parse(arg, "i:strerror", &code)) {
goto exit;
+ }
return_value = os_strerror_impl(module, code);
exit:
@@ -4256,11 +4374,13 @@ os_WCOREDUMP(PyObject *module, PyObject *arg)
int status;
int _return_value;
- if (!PyArg_Parse(arg, "i:WCOREDUMP", &status))
+ if (!PyArg_Parse(arg, "i:WCOREDUMP", &status)) {
goto exit;
+ }
_return_value = os_WCOREDUMP_impl(module, status);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyBool_FromLong((long)_return_value);
exit:
@@ -4295,11 +4415,13 @@ os_WIFCONTINUED(PyObject *module, PyObject *args, PyObject *kwargs)
int _return_value;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:WIFCONTINUED", _keywords,
- &status))
+ &status)) {
goto exit;
+ }
_return_value = os_WIFCONTINUED_impl(module, status);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyBool_FromLong((long)_return_value);
exit:
@@ -4331,11 +4453,13 @@ os_WIFSTOPPED(PyObject *module, PyObject *args, PyObject *kwargs)
int _return_value;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:WIFSTOPPED", _keywords,
- &status))
+ &status)) {
goto exit;
+ }
_return_value = os_WIFSTOPPED_impl(module, status);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyBool_FromLong((long)_return_value);
exit:
@@ -4367,11 +4491,13 @@ os_WIFSIGNALED(PyObject *module, PyObject *args, PyObject *kwargs)
int _return_value;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:WIFSIGNALED", _keywords,
- &status))
+ &status)) {
goto exit;
+ }
_return_value = os_WIFSIGNALED_impl(module, status);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyBool_FromLong((long)_return_value);
exit:
@@ -4403,11 +4529,13 @@ os_WIFEXITED(PyObject *module, PyObject *args, PyObject *kwargs)
int _return_value;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:WIFEXITED", _keywords,
- &status))
+ &status)) {
goto exit;
+ }
_return_value = os_WIFEXITED_impl(module, status);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyBool_FromLong((long)_return_value);
exit:
@@ -4439,11 +4567,13 @@ os_WEXITSTATUS(PyObject *module, PyObject *args, PyObject *kwargs)
int _return_value;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:WEXITSTATUS", _keywords,
- &status))
+ &status)) {
goto exit;
+ }
_return_value = os_WEXITSTATUS_impl(module, status);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong((long)_return_value);
exit:
@@ -4475,11 +4605,13 @@ os_WTERMSIG(PyObject *module, PyObject *args, PyObject *kwargs)
int _return_value;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:WTERMSIG", _keywords,
- &status))
+ &status)) {
goto exit;
+ }
_return_value = os_WTERMSIG_impl(module, status);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong((long)_return_value);
exit:
@@ -4511,11 +4643,13 @@ os_WSTOPSIG(PyObject *module, PyObject *args, PyObject *kwargs)
int _return_value;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:WSTOPSIG", _keywords,
- &status))
+ &status)) {
goto exit;
+ }
_return_value = os_WSTOPSIG_impl(module, status);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong((long)_return_value);
exit:
@@ -4546,8 +4680,9 @@ os_fstatvfs(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
int fd;
- if (!PyArg_Parse(arg, "i:fstatvfs", &fd))
+ if (!PyArg_Parse(arg, "i:fstatvfs", &fd)) {
goto exit;
+ }
return_value = os_fstatvfs_impl(module, fd);
exit:
@@ -4582,8 +4717,9 @@ os_statvfs(PyObject *module, PyObject *args, PyObject *kwargs)
path_t path = PATH_T_INITIALIZE("statvfs", "path", 0, PATH_HAVE_FSTATVFS);
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:statvfs", _keywords,
- path_converter, &path))
+ path_converter, &path)) {
goto exit;
+ }
return_value = os_statvfs_impl(module, &path);
exit:
@@ -4617,8 +4753,9 @@ os__getdiskusage(PyObject *module, PyObject *args, PyObject *kwargs)
Py_UNICODE *path;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "u:_getdiskusage", _keywords,
- &path))
+ &path)) {
goto exit;
+ }
return_value = os__getdiskusage_impl(module, path);
exit:
@@ -4652,11 +4789,13 @@ os_fpathconf(PyObject *module, PyObject *args)
long _return_value;
if (!PyArg_ParseTuple(args, "iO&:fpathconf",
- &fd, conv_path_confname, &name))
+ &fd, conv_path_confname, &name)) {
goto exit;
+ }
_return_value = os_fpathconf_impl(module, fd, name);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong(_return_value);
exit:
@@ -4693,11 +4832,13 @@ os_pathconf(PyObject *module, PyObject *args, PyObject *kwargs)
long _return_value;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:pathconf", _keywords,
- path_converter, &path, conv_path_confname, &name))
+ path_converter, &path, conv_path_confname, &name)) {
goto exit;
+ }
_return_value = os_pathconf_impl(module, &path, name);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong(_return_value);
exit:
@@ -4729,8 +4870,9 @@ os_confstr(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
int name;
- if (!PyArg_Parse(arg, "O&:confstr", conv_confstr_confname, &name))
+ if (!PyArg_Parse(arg, "O&:confstr", conv_confstr_confname, &name)) {
goto exit;
+ }
return_value = os_confstr_impl(module, name);
exit:
@@ -4760,11 +4902,13 @@ os_sysconf(PyObject *module, PyObject *arg)
int name;
long _return_value;
- if (!PyArg_Parse(arg, "O&:sysconf", conv_sysconf_confname, &name))
+ if (!PyArg_Parse(arg, "O&:sysconf", conv_sysconf_confname, &name)) {
goto exit;
+ }
_return_value = os_sysconf_impl(module, name);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong(_return_value);
exit:
@@ -4843,8 +4987,9 @@ os_device_encoding(PyObject *module, PyObject *args, PyObject *kwargs)
int fd;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:device_encoding", _keywords,
- &fd))
+ &fd)) {
goto exit;
+ }
return_value = os_device_encoding_impl(module, fd);
exit:
@@ -4874,8 +5019,9 @@ os_setresuid(PyObject *module, PyObject *args)
uid_t suid;
if (!PyArg_ParseTuple(args, "O&O&O&:setresuid",
- _Py_Uid_Converter, &ruid, _Py_Uid_Converter, &euid, _Py_Uid_Converter, &suid))
+ _Py_Uid_Converter, &ruid, _Py_Uid_Converter, &euid, _Py_Uid_Converter, &suid)) {
goto exit;
+ }
return_value = os_setresuid_impl(module, ruid, euid, suid);
exit:
@@ -4907,8 +5053,9 @@ os_setresgid(PyObject *module, PyObject *args)
gid_t sgid;
if (!PyArg_ParseTuple(args, "O&O&O&:setresgid",
- _Py_Gid_Converter, &rgid, _Py_Gid_Converter, &egid, _Py_Gid_Converter, &sgid))
+ _Py_Gid_Converter, &rgid, _Py_Gid_Converter, &egid, _Py_Gid_Converter, &sgid)) {
goto exit;
+ }
return_value = os_setresgid_impl(module, rgid, egid, sgid);
exit:
@@ -4991,8 +5138,9 @@ os_getxattr(PyObject *module, PyObject *args, PyObject *kwargs)
int follow_symlinks = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", _keywords,
- path_converter, &path, path_converter, &attribute, &follow_symlinks))
+ path_converter, &path, path_converter, &attribute, &follow_symlinks)) {
goto exit;
+ }
return_value = os_getxattr_impl(module, &path, &attribute, follow_symlinks);
exit:
@@ -5039,8 +5187,9 @@ os_setxattr(PyObject *module, PyObject *args, PyObject *kwargs)
int follow_symlinks = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&y*|i$p:setxattr", _keywords,
- path_converter, &path, path_converter, &attribute, &value, &flags, &follow_symlinks))
+ path_converter, &path, path_converter, &attribute, &value, &flags, &follow_symlinks)) {
goto exit;
+ }
return_value = os_setxattr_impl(module, &path, &attribute, &value, flags, follow_symlinks);
exit:
@@ -5049,8 +5198,9 @@ exit:
/* Cleanup for attribute */
path_cleanup(&attribute);
/* Cleanup for value */
- if (value.obj)
+ if (value.obj) {
PyBuffer_Release(&value);
+ }
return return_value;
}
@@ -5087,8 +5237,9 @@ os_removexattr(PyObject *module, PyObject *args, PyObject *kwargs)
int follow_symlinks = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr", _keywords,
- path_converter, &path, path_converter, &attribute, &follow_symlinks))
+ path_converter, &path, path_converter, &attribute, &follow_symlinks)) {
goto exit;
+ }
return_value = os_removexattr_impl(module, &path, &attribute, follow_symlinks);
exit:
@@ -5131,8 +5282,9 @@ os_listxattr(PyObject *module, PyObject *args, PyObject *kwargs)
int follow_symlinks = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", _keywords,
- path_converter, &path, &follow_symlinks))
+ path_converter, &path, &follow_symlinks)) {
goto exit;
+ }
return_value = os_listxattr_impl(module, &path, follow_symlinks);
exit:
@@ -5162,8 +5314,9 @@ os_urandom(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
Py_ssize_t size;
- if (!PyArg_Parse(arg, "n:urandom", &size))
+ if (!PyArg_Parse(arg, "n:urandom", &size)) {
goto exit;
+ }
return_value = os_urandom_impl(module, size);
exit:
@@ -5174,7 +5327,11 @@ PyDoc_STRVAR(os_cpu_count__doc__,
"cpu_count($module, /)\n"
"--\n"
"\n"
-"Return the number of CPUs in the system; return None if indeterminable.");
+"Return the number of CPUs in the system; return None if indeterminable.\n"
+"\n"
+"This number is not equivalent to the number of CPUs the current process can\n"
+"use. The number of usable CPUs can be obtained with\n"
+"``len(os.sched_getaffinity(0))``");
#define OS_CPU_COUNT_METHODDEF \
{"cpu_count", (PyCFunction)os_cpu_count, METH_NOARGS, os_cpu_count__doc__},
@@ -5207,11 +5364,13 @@ os_get_inheritable(PyObject *module, PyObject *arg)
int fd;
int _return_value;
- if (!PyArg_Parse(arg, "i:get_inheritable", &fd))
+ if (!PyArg_Parse(arg, "i:get_inheritable", &fd)) {
goto exit;
+ }
_return_value = os_get_inheritable_impl(module, fd);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyBool_FromLong((long)_return_value);
exit:
@@ -5238,8 +5397,9 @@ os_set_inheritable(PyObject *module, PyObject *args)
int inheritable;
if (!PyArg_ParseTuple(args, "ii:set_inheritable",
- &fd, &inheritable))
+ &fd, &inheritable)) {
goto exit;
+ }
return_value = os_set_inheritable_impl(module, fd, inheritable);
exit:
@@ -5267,11 +5427,13 @@ os_get_handle_inheritable(PyObject *module, PyObject *arg)
Py_intptr_t handle;
int _return_value;
- if (!PyArg_Parse(arg, "" _Py_PARSE_INTPTR ":get_handle_inheritable", &handle))
+ if (!PyArg_Parse(arg, "" _Py_PARSE_INTPTR ":get_handle_inheritable", &handle)) {
goto exit;
+ }
_return_value = os_get_handle_inheritable_impl(module, handle);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyBool_FromLong((long)_return_value);
exit:
@@ -5303,8 +5465,9 @@ os_set_handle_inheritable(PyObject *module, PyObject *args)
int inheritable;
if (!PyArg_ParseTuple(args, "" _Py_PARSE_INTPTR "p:set_handle_inheritable",
- &handle, &inheritable))
+ &handle, &inheritable)) {
goto exit;
+ }
return_value = os_set_handle_inheritable_impl(module, handle, inheritable);
exit:
@@ -5313,6 +5476,39 @@ exit:
#endif /* defined(MS_WINDOWS) */
+PyDoc_STRVAR(os_fspath__doc__,
+"fspath($module, /, path)\n"
+"--\n"
+"\n"
+"Return the file system path representation of the object.\n"
+"\n"
+"If the object is str or bytes, then allow it to pass through as-is. If the\n"
+"object defines __fspath__(), then return the result of that method. All other\n"
+"types raise a TypeError.");
+
+#define OS_FSPATH_METHODDEF \
+ {"fspath", (PyCFunction)os_fspath, METH_VARARGS|METH_KEYWORDS, os_fspath__doc__},
+
+static PyObject *
+os_fspath_impl(PyObject *module, PyObject *path);
+
+static PyObject *
+os_fspath(PyObject *module, PyObject *args, PyObject *kwargs)
+{
+ PyObject *return_value = NULL;
+ static char *_keywords[] = {"path", NULL};
+ PyObject *path;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:fspath", _keywords,
+ &path)) {
+ goto exit;
+ }
+ return_value = os_fspath_impl(module, path);
+
+exit:
+ return return_value;
+}
+
#ifndef OS_TTYNAME_METHODDEF
#define OS_TTYNAME_METHODDEF
#endif /* !defined(OS_TTYNAME_METHODDEF) */
@@ -5784,4 +5980,4 @@ exit:
#ifndef OS_SET_HANDLE_INHERITABLE_METHODDEF
#define OS_SET_HANDLE_INHERITABLE_METHODDEF
#endif /* !defined(OS_SET_HANDLE_INHERITABLE_METHODDEF) */
-/*[clinic end generated code: output=c27221987f987cf3 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=e91e62d8e8f1b6ac input=a9049054013a1b77]*/
diff --git a/Modules/clinic/pwdmodule.c.h b/Modules/clinic/pwdmodule.c.h
index cb191a0b77..f9e0644f26 100644
--- a/Modules/clinic/pwdmodule.c.h
+++ b/Modules/clinic/pwdmodule.c.h
@@ -33,8 +33,9 @@ pwd_getpwnam(PyObject *module, PyObject *arg_)
PyObject *return_value = NULL;
PyObject *arg;
- if (!PyArg_Parse(arg_, "U:getpwnam", &arg))
+ if (!PyArg_Parse(arg_, "U:getpwnam", &arg)) {
goto exit;
+ }
return_value = pwd_getpwnam_impl(module, arg);
exit:
@@ -68,4 +69,4 @@ pwd_getpwall(PyObject *module, PyObject *Py_UNUSED(ignored))
#ifndef PWD_GETPWALL_METHODDEF
#define PWD_GETPWALL_METHODDEF
#endif /* !defined(PWD_GETPWALL_METHODDEF) */
-/*[clinic end generated code: output=d0ea1c5c832f0c1a input=a9049054013a1b77]*/
+/*[clinic end generated code: output=fc41d8d88ec206d8 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/pyexpat.c.h b/Modules/clinic/pyexpat.c.h
index c5b5c71c99..d9cf73dc6e 100644
--- a/Modules/clinic/pyexpat.c.h
+++ b/Modules/clinic/pyexpat.c.h
@@ -25,8 +25,9 @@ pyexpat_xmlparser_Parse(xmlparseobject *self, PyObject *args)
int isfinal = 0;
if (!PyArg_ParseTuple(args, "O|i:Parse",
- &data, &isfinal))
+ &data, &isfinal)) {
goto exit;
+ }
return_value = pyexpat_xmlparser_Parse_impl(self, data, isfinal);
exit:
@@ -60,8 +61,9 @@ pyexpat_xmlparser_SetBase(xmlparseobject *self, PyObject *arg)
PyObject *return_value = NULL;
const char *base;
- if (!PyArg_Parse(arg, "s:SetBase", &base))
+ if (!PyArg_Parse(arg, "s:SetBase", &base)) {
goto exit;
+ }
return_value = pyexpat_xmlparser_SetBase_impl(self, base);
exit:
@@ -129,8 +131,9 @@ pyexpat_xmlparser_ExternalEntityParserCreate(xmlparseobject *self, PyObject *arg
const char *encoding = NULL;
if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
- &context, &encoding))
+ &context, &encoding)) {
goto exit;
+ }
return_value = pyexpat_xmlparser_ExternalEntityParserCreate_impl(self, context, encoding);
exit:
@@ -160,8 +163,9 @@ pyexpat_xmlparser_SetParamEntityParsing(xmlparseobject *self, PyObject *arg)
PyObject *return_value = NULL;
int flag;
- if (!PyArg_Parse(arg, "i:SetParamEntityParsing", &flag))
+ if (!PyArg_Parse(arg, "i:SetParamEntityParsing", &flag)) {
goto exit;
+ }
return_value = pyexpat_xmlparser_SetParamEntityParsing_impl(self, flag);
exit:
@@ -193,8 +197,9 @@ pyexpat_xmlparser_UseForeignDTD(xmlparseobject *self, PyObject *args)
int flag = 1;
if (!PyArg_ParseTuple(args, "|p:UseForeignDTD",
- &flag))
+ &flag)) {
goto exit;
+ }
return_value = pyexpat_xmlparser_UseForeignDTD_impl(self, flag);
exit:
@@ -244,8 +249,9 @@ pyexpat_ParserCreate(PyObject *module, PyObject *args, PyObject *kwargs)
PyObject *intern = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|zzO:ParserCreate", _keywords,
- &encoding, &namespace_separator, &intern))
+ &encoding, &namespace_separator, &intern)) {
goto exit;
+ }
return_value = pyexpat_ParserCreate_impl(module, encoding, namespace_separator, intern);
exit:
@@ -270,8 +276,9 @@ pyexpat_ErrorString(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
long code;
- if (!PyArg_Parse(arg, "l:ErrorString", &code))
+ if (!PyArg_Parse(arg, "l:ErrorString", &code)) {
goto exit;
+ }
return_value = pyexpat_ErrorString_impl(module, code);
exit:
@@ -281,4 +288,4 @@ exit:
#ifndef PYEXPAT_XMLPARSER_USEFOREIGNDTD_METHODDEF
#define PYEXPAT_XMLPARSER_USEFOREIGNDTD_METHODDEF
#endif /* !defined(PYEXPAT_XMLPARSER_USEFOREIGNDTD_METHODDEF) */
-/*[clinic end generated code: output=d479cfab607e9dc8 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=9de21f46734b1311 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/sha1module.c.h b/Modules/clinic/sha1module.c.h
index 5b8db8079b..8030fbde00 100644
--- a/Modules/clinic/sha1module.c.h
+++ b/Modules/clinic/sha1module.c.h
@@ -85,11 +85,12 @@ _sha1_sha1(PyObject *module, PyObject *args, PyObject *kwargs)
PyObject *string = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:sha1", _keywords,
- &string))
+ &string)) {
goto exit;
+ }
return_value = _sha1_sha1_impl(module, string);
exit:
return return_value;
}
-/*[clinic end generated code: output=0b6a194fbb0b94f2 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=475c4cc749ab31b1 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/sha256module.c.h b/Modules/clinic/sha256module.c.h
index 661569c4fe..483109f936 100644
--- a/Modules/clinic/sha256module.c.h
+++ b/Modules/clinic/sha256module.c.h
@@ -85,8 +85,9 @@ _sha256_sha256(PyObject *module, PyObject *args, PyObject *kwargs)
PyObject *string = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:sha256", _keywords,
- &string))
+ &string)) {
goto exit;
+ }
return_value = _sha256_sha256_impl(module, string);
exit:
@@ -113,11 +114,12 @@ _sha256_sha224(PyObject *module, PyObject *args, PyObject *kwargs)
PyObject *string = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:sha224", _keywords,
- &string))
+ &string)) {
goto exit;
+ }
return_value = _sha256_sha224_impl(module, string);
exit:
return return_value;
}
-/*[clinic end generated code: output=5a1fc5480e399f95 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=a41a21c08fcddd70 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/sha512module.c.h b/Modules/clinic/sha512module.c.h
index d64c2a413f..ec5a0c3402 100644
--- a/Modules/clinic/sha512module.c.h
+++ b/Modules/clinic/sha512module.c.h
@@ -103,8 +103,9 @@ _sha512_sha512(PyObject *module, PyObject *args, PyObject *kwargs)
PyObject *string = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:sha512", _keywords,
- &string))
+ &string)) {
goto exit;
+ }
return_value = _sha512_sha512_impl(module, string);
exit:
@@ -135,8 +136,9 @@ _sha512_sha384(PyObject *module, PyObject *args, PyObject *kwargs)
PyObject *string = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:sha384", _keywords,
- &string))
+ &string)) {
goto exit;
+ }
return_value = _sha512_sha384_impl(module, string);
exit:
@@ -168,4 +170,4 @@ exit:
#ifndef _SHA512_SHA384_METHODDEF
#define _SHA512_SHA384_METHODDEF
#endif /* !defined(_SHA512_SHA384_METHODDEF) */
-/*[clinic end generated code: output=bb87f494df50ffc0 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=e314c0f773abd5d7 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/signalmodule.c.h b/Modules/clinic/signalmodule.c.h
index c8f4cd5c41..f8d5bd942b 100644
--- a/Modules/clinic/signalmodule.c.h
+++ b/Modules/clinic/signalmodule.c.h
@@ -23,11 +23,13 @@ signal_alarm(PyObject *module, PyObject *arg)
int seconds;
long _return_value;
- if (!PyArg_Parse(arg, "i:alarm", &seconds))
+ if (!PyArg_Parse(arg, "i:alarm", &seconds)) {
goto exit;
+ }
_return_value = signal_alarm_impl(module, seconds);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong(_return_value);
exit:
@@ -85,8 +87,9 @@ signal_signal(PyObject *module, PyObject *args)
PyObject *handler;
if (!PyArg_ParseTuple(args, "iO:signal",
- &signalnum, &handler))
+ &signalnum, &handler)) {
goto exit;
+ }
return_value = signal_signal_impl(module, signalnum, handler);
exit:
@@ -117,8 +120,9 @@ signal_getsignal(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
int signalnum;
- if (!PyArg_Parse(arg, "i:getsignal", &signalnum))
+ if (!PyArg_Parse(arg, "i:getsignal", &signalnum)) {
goto exit;
+ }
return_value = signal_getsignal_impl(module, signalnum);
exit:
@@ -150,8 +154,9 @@ signal_siginterrupt(PyObject *module, PyObject *args)
int flag;
if (!PyArg_ParseTuple(args, "ii:siginterrupt",
- &signalnum, &flag))
+ &signalnum, &flag)) {
goto exit;
+ }
return_value = signal_siginterrupt_impl(module, signalnum, flag);
exit:
@@ -189,8 +194,9 @@ signal_setitimer(PyObject *module, PyObject *args)
double interval = 0.0;
if (!PyArg_ParseTuple(args, "id|d:setitimer",
- &which, &seconds, &interval))
+ &which, &seconds, &interval)) {
goto exit;
+ }
return_value = signal_setitimer_impl(module, which, seconds, interval);
exit:
@@ -219,8 +225,9 @@ signal_getitimer(PyObject *module, PyObject *arg)
PyObject *return_value = NULL;
int which;
- if (!PyArg_Parse(arg, "i:getitimer", &which))
+ if (!PyArg_Parse(arg, "i:getitimer", &which)) {
goto exit;
+ }
return_value = signal_getitimer_impl(module, which);
exit:
@@ -251,8 +258,9 @@ signal_pthread_sigmask(PyObject *module, PyObject *args)
PyObject *mask;
if (!PyArg_ParseTuple(args, "iO:pthread_sigmask",
- &how, &mask))
+ &how, &mask)) {
goto exit;
+ }
return_value = signal_pthread_sigmask_impl(module, how, mask);
exit:
@@ -344,8 +352,9 @@ signal_sigtimedwait(PyObject *module, PyObject *args)
if (!PyArg_UnpackTuple(args, "sigtimedwait",
2, 2,
- &sigset, &timeout_obj))
+ &sigset, &timeout_obj)) {
goto exit;
+ }
return_value = signal_sigtimedwait_impl(module, sigset, timeout_obj);
exit:
@@ -376,8 +385,9 @@ signal_pthread_kill(PyObject *module, PyObject *args)
int signalnum;
if (!PyArg_ParseTuple(args, "li:pthread_kill",
- &thread_id, &signalnum))
+ &thread_id, &signalnum)) {
goto exit;
+ }
return_value = signal_pthread_kill_impl(module, thread_id, signalnum);
exit:
@@ -429,4 +439,4 @@ exit:
#ifndef SIGNAL_PTHREAD_KILL_METHODDEF
#define SIGNAL_PTHREAD_KILL_METHODDEF
#endif /* !defined(SIGNAL_PTHREAD_KILL_METHODDEF) */
-/*[clinic end generated code: output=dafa598412bfb8d2 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=c6990ef0d0ba72b6 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/spwdmodule.c.h b/Modules/clinic/spwdmodule.c.h
index e26acf1863..b2479ff245 100644
--- a/Modules/clinic/spwdmodule.c.h
+++ b/Modules/clinic/spwdmodule.c.h
@@ -24,8 +24,9 @@ spwd_getspnam(PyObject *module, PyObject *arg_)
PyObject *return_value = NULL;
PyObject *arg;
- if (!PyArg_Parse(arg_, "U:getspnam", &arg))
+ if (!PyArg_Parse(arg_, "U:getspnam", &arg)) {
goto exit;
+ }
return_value = spwd_getspnam_impl(module, arg);
exit:
@@ -65,4 +66,4 @@ spwd_getspall(PyObject *module, PyObject *Py_UNUSED(ignored))
#ifndef SPWD_GETSPALL_METHODDEF
#define SPWD_GETSPALL_METHODDEF
#endif /* !defined(SPWD_GETSPALL_METHODDEF) */
-/*[clinic end generated code: output=510f681b36f54c30 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=07cd8af0afd77fe7 input=a9049054013a1b77]*/
diff --git a/Modules/clinic/unicodedata.c.h b/Modules/clinic/unicodedata.c.h
index d520c1e3dd..d481ccbe64 100644
--- a/Modules/clinic/unicodedata.c.h
+++ b/Modules/clinic/unicodedata.c.h
@@ -27,8 +27,9 @@ unicodedata_UCD_decimal(PyObject *self, PyObject *args)
PyObject *default_value = NULL;
if (!PyArg_ParseTuple(args, "C|O:decimal",
- &chr, &default_value))
+ &chr, &default_value)) {
goto exit;
+ }
return_value = unicodedata_UCD_decimal_impl(self, chr, default_value);
exit:
@@ -59,8 +60,9 @@ unicodedata_UCD_digit(PyObject *self, PyObject *args)
PyObject *default_value = NULL;
if (!PyArg_ParseTuple(args, "C|O:digit",
- &chr, &default_value))
+ &chr, &default_value)) {
goto exit;
+ }
return_value = unicodedata_UCD_digit_impl(self, chr, default_value);
exit:
@@ -92,8 +94,9 @@ unicodedata_UCD_numeric(PyObject *self, PyObject *args)
PyObject *default_value = NULL;
if (!PyArg_ParseTuple(args, "C|O:numeric",
- &chr, &default_value))
+ &chr, &default_value)) {
goto exit;
+ }
return_value = unicodedata_UCD_numeric_impl(self, chr, default_value);
exit:
@@ -118,8 +121,9 @@ unicodedata_UCD_category(PyObject *self, PyObject *arg)
PyObject *return_value = NULL;
int chr;
- if (!PyArg_Parse(arg, "C:category", &chr))
+ if (!PyArg_Parse(arg, "C:category", &chr)) {
goto exit;
+ }
return_value = unicodedata_UCD_category_impl(self, chr);
exit:
@@ -146,8 +150,9 @@ unicodedata_UCD_bidirectional(PyObject *self, PyObject *arg)
PyObject *return_value = NULL;
int chr;
- if (!PyArg_Parse(arg, "C:bidirectional", &chr))
+ if (!PyArg_Parse(arg, "C:bidirectional", &chr)) {
goto exit;
+ }
return_value = unicodedata_UCD_bidirectional_impl(self, chr);
exit:
@@ -175,11 +180,13 @@ unicodedata_UCD_combining(PyObject *self, PyObject *arg)
int chr;
int _return_value;
- if (!PyArg_Parse(arg, "C:combining", &chr))
+ if (!PyArg_Parse(arg, "C:combining", &chr)) {
goto exit;
+ }
_return_value = unicodedata_UCD_combining_impl(self, chr);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong((long)_return_value);
exit:
@@ -208,11 +215,13 @@ unicodedata_UCD_mirrored(PyObject *self, PyObject *arg)
int chr;
int _return_value;
- if (!PyArg_Parse(arg, "C:mirrored", &chr))
+ if (!PyArg_Parse(arg, "C:mirrored", &chr)) {
goto exit;
+ }
_return_value = unicodedata_UCD_mirrored_impl(self, chr);
- if ((_return_value == -1) && PyErr_Occurred())
+ if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
+ }
return_value = PyLong_FromLong((long)_return_value);
exit:
@@ -237,8 +246,9 @@ unicodedata_UCD_east_asian_width(PyObject *self, PyObject *arg)
PyObject *return_value = NULL;
int chr;
- if (!PyArg_Parse(arg, "C:east_asian_width", &chr))
+ if (!PyArg_Parse(arg, "C:east_asian_width", &chr)) {
goto exit;
+ }
return_value = unicodedata_UCD_east_asian_width_impl(self, chr);
exit:
@@ -265,8 +275,9 @@ unicodedata_UCD_decomposition(PyObject *self, PyObject *arg)
PyObject *return_value = NULL;
int chr;
- if (!PyArg_Parse(arg, "C:decomposition", &chr))
+ if (!PyArg_Parse(arg, "C:decomposition", &chr)) {
goto exit;
+ }
return_value = unicodedata_UCD_decomposition_impl(self, chr);
exit:
@@ -296,8 +307,9 @@ unicodedata_UCD_normalize(PyObject *self, PyObject *args)
PyObject *input;
if (!PyArg_ParseTuple(args, "sO!:normalize",
- &form, &PyUnicode_Type, &input))
+ &form, &PyUnicode_Type, &input)) {
goto exit;
+ }
return_value = unicodedata_UCD_normalize_impl(self, form, input);
exit:
@@ -327,8 +339,9 @@ unicodedata_UCD_name(PyObject *self, PyObject *args)
PyObject *default_value = NULL;
if (!PyArg_ParseTuple(args, "C|O:name",
- &chr, &default_value))
+ &chr, &default_value)) {
goto exit;
+ }
return_value = unicodedata_UCD_name_impl(self, chr, default_value);
exit:
@@ -358,11 +371,12 @@ unicodedata_UCD_lookup(PyObject *self, PyObject *arg)
const char *name;
Py_ssize_clean_t name_length;
- if (!PyArg_Parse(arg, "s#:lookup", &name, &name_length))
+ if (!PyArg_Parse(arg, "s#:lookup", &name, &name_length)) {
goto exit;
+ }
return_value = unicodedata_UCD_lookup_impl(self, name, name_length);
exit:
return return_value;
}
-/*[clinic end generated code: output=4f8da33c6bc6efc9 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=5313ce129da87b2f input=a9049054013a1b77]*/
diff --git a/Modules/clinic/zlibmodule.c.h b/Modules/clinic/zlibmodule.c.h
index 71da0273a7..1b2b7fed46 100644
--- a/Modules/clinic/zlibmodule.c.h
+++ b/Modules/clinic/zlibmodule.c.h
@@ -3,38 +3,41 @@ preserve
[clinic start generated code]*/
PyDoc_STRVAR(zlib_compress__doc__,
-"compress($module, bytes, level=Z_DEFAULT_COMPRESSION, /)\n"
+"compress($module, data, /, level=Z_DEFAULT_COMPRESSION)\n"
"--\n"
"\n"
"Returns a bytes object containing compressed data.\n"
"\n"
-" bytes\n"
+" data\n"
" Binary data to be compressed.\n"
" level\n"
-" Compression level, in 0-9.");
+" Compression level, in 0-9 or -1.");
#define ZLIB_COMPRESS_METHODDEF \
- {"compress", (PyCFunction)zlib_compress, METH_VARARGS, zlib_compress__doc__},
+ {"compress", (PyCFunction)zlib_compress, METH_VARARGS|METH_KEYWORDS, zlib_compress__doc__},
static PyObject *
-zlib_compress_impl(PyObject *module, Py_buffer *bytes, int level);
+zlib_compress_impl(PyObject *module, Py_buffer *data, int level);
static PyObject *
-zlib_compress(PyObject *module, PyObject *args)
+zlib_compress(PyObject *module, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
- Py_buffer bytes = {NULL, NULL};
+ static char *_keywords[] = {"", "level", NULL};
+ Py_buffer data = {NULL, NULL};
int level = Z_DEFAULT_COMPRESSION;
- if (!PyArg_ParseTuple(args, "y*|i:compress",
- &bytes, &level))
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|i:compress", _keywords,
+ &data, &level)) {
goto exit;
- return_value = zlib_compress_impl(module, &bytes, level);
+ }
+ return_value = zlib_compress_impl(module, &data, level);
exit:
- /* Cleanup for bytes */
- if (bytes.obj)
- PyBuffer_Release(&bytes);
+ /* Cleanup for data */
+ if (data.obj) {
+ PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -68,14 +71,16 @@ zlib_decompress(PyObject *module, PyObject *args)
unsigned int bufsize = DEF_BUF_SIZE;
if (!PyArg_ParseTuple(args, "y*|iO&:decompress",
- &data, &wbits, capped_uint_converter, &bufsize))
+ &data, &wbits, capped_uint_converter, &bufsize)) {
goto exit;
+ }
return_value = zlib_decompress_impl(module, &data, wbits, bufsize);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -130,14 +135,16 @@ zlib_compressobj(PyObject *module, PyObject *args, PyObject *kwargs)
Py_buffer zdict = {NULL, NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiy*:compressobj", _keywords,
- &level, &method, &wbits, &memLevel, &strategy, &zdict))
+ &level, &method, &wbits, &memLevel, &strategy, &zdict)) {
goto exit;
+ }
return_value = zlib_compressobj_impl(module, level, method, wbits, memLevel, strategy, &zdict);
exit:
/* Cleanup for zdict */
- if (zdict.obj)
+ if (zdict.obj) {
PyBuffer_Release(&zdict);
+ }
return return_value;
}
@@ -169,8 +176,9 @@ zlib_decompressobj(PyObject *module, PyObject *args, PyObject *kwargs)
PyObject *zdict = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:decompressobj", _keywords,
- &wbits, &zdict))
+ &wbits, &zdict)) {
goto exit;
+ }
return_value = zlib_decompressobj_impl(module, wbits, zdict);
exit:
@@ -202,14 +210,16 @@ zlib_Compress_compress(compobject *self, PyObject *arg)
PyObject *return_value = NULL;
Py_buffer data = {NULL, NULL};
- if (!PyArg_Parse(arg, "y*:compress", &data))
+ if (!PyArg_Parse(arg, "y*:compress", &data)) {
goto exit;
+ }
return_value = zlib_Compress_compress_impl(self, &data);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -246,14 +256,16 @@ zlib_Decompress_decompress(compobject *self, PyObject *args)
unsigned int max_length = 0;
if (!PyArg_ParseTuple(args, "y*|O&:decompress",
- &data, capped_uint_converter, &max_length))
+ &data, capped_uint_converter, &max_length)) {
goto exit;
+ }
return_value = zlib_Decompress_decompress_impl(self, &data, max_length);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -283,8 +295,9 @@ zlib_Compress_flush(compobject *self, PyObject *args)
int mode = Z_FINISH;
if (!PyArg_ParseTuple(args, "|i:flush",
- &mode))
+ &mode)) {
goto exit;
+ }
return_value = zlib_Compress_flush_impl(self, mode);
exit:
@@ -357,8 +370,9 @@ zlib_Decompress_flush(compobject *self, PyObject *args)
unsigned int length = DEF_BUF_SIZE;
if (!PyArg_ParseTuple(args, "|O&:flush",
- capped_uint_converter, &length))
+ capped_uint_converter, &length)) {
goto exit;
+ }
return_value = zlib_Decompress_flush_impl(self, length);
exit:
@@ -390,14 +404,16 @@ zlib_adler32(PyObject *module, PyObject *args)
unsigned int value = 1;
if (!PyArg_ParseTuple(args, "y*|I:adler32",
- &data, &value))
+ &data, &value)) {
goto exit;
+ }
return_value = zlib_adler32_impl(module, &data, value);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -427,14 +443,16 @@ zlib_crc32(PyObject *module, PyObject *args)
unsigned int value = 0;
if (!PyArg_ParseTuple(args, "y*|I:crc32",
- &data, &value))
+ &data, &value)) {
goto exit;
+ }
return_value = zlib_crc32_impl(module, &data, value);
exit:
/* Cleanup for data */
- if (data.obj)
+ if (data.obj) {
PyBuffer_Release(&data);
+ }
return return_value;
}
@@ -442,4 +460,4 @@ exit:
#ifndef ZLIB_COMPRESS_COPY_METHODDEF
#define ZLIB_COMPRESS_COPY_METHODDEF
#endif /* !defined(ZLIB_COMPRESS_COPY_METHODDEF) */
-/*[clinic end generated code: output=8545565b1a1822de input=a9049054013a1b77]*/
+/*[clinic end generated code: output=519446af912f4e72 input=a9049054013a1b77]*/
diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c
index f1fda481fc..d6322d0f3f 100644
--- a/Modules/faulthandler.c
+++ b/Modules/faulthandler.c
@@ -119,7 +119,7 @@ static fault_handler_t faulthandler_handlers[] = {
handler fails in faulthandler_fatal_error() */
{SIGSEGV, 0, "Segmentation fault", }
};
-static const unsigned char faulthandler_nsignals = \
+static const size_t faulthandler_nsignals = \
Py_ARRAY_LENGTH(faulthandler_handlers);
#ifdef HAVE_SIGALTSTACK
@@ -202,8 +202,9 @@ faulthandler_get_fileno(PyObject **file_ptr)
static PyThreadState*
get_thread_state(void)
{
- PyThreadState *tstate = PyThreadState_Get();
+ PyThreadState *tstate = _PyThreadState_UncheckedGet();
if (tstate == NULL) {
+ /* just in case but very unlikely... */
PyErr_SetString(PyExc_RuntimeError,
"unable to get the current thread state");
return NULL;
@@ -234,11 +235,12 @@ faulthandler_dump_traceback(int fd, int all_threads,
PyGILState_GetThisThreadState(). */
tstate = PyGILState_GetThisThreadState();
#else
- tstate = PyThreadState_Get();
+ tstate = _PyThreadState_UncheckedGet();
#endif
- if (all_threads)
- _Py_DumpTracebackThreads(fd, interp, tstate);
+ if (all_threads) {
+ (void)_Py_DumpTracebackThreads(fd, NULL, tstate);
+ }
else {
if (tstate != NULL)
_Py_DumpTraceback(fd, tstate);
@@ -272,7 +274,7 @@ faulthandler_dump_traceback_py(PyObject *self,
return NULL;
if (all_threads) {
- errmsg = _Py_DumpTracebackThreads(fd, tstate->interp, tstate);
+ errmsg = _Py_DumpTracebackThreads(fd, NULL, tstate);
if (errmsg != NULL) {
PyErr_SetString(PyExc_RuntimeError, errmsg);
return NULL;
@@ -288,6 +290,19 @@ faulthandler_dump_traceback_py(PyObject *self,
Py_RETURN_NONE;
}
+static void
+faulthandler_disable_fatal_handler(fault_handler_t *handler)
+{
+ if (!handler->enabled)
+ return;
+ handler->enabled = 0;
+#ifdef HAVE_SIGACTION
+ (void)sigaction(handler->signum, &handler->previous, NULL);
+#else
+ (void)signal(handler->signum, handler->previous);
+#endif
+}
+
/* Handler for SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL signals.
@@ -306,7 +321,7 @@ static void
faulthandler_fatal_error(int signum)
{
const int fd = fatal_error.fd;
- unsigned int i;
+ size_t i;
fault_handler_t *handler = NULL;
int save_errno = errno;
@@ -324,12 +339,7 @@ faulthandler_fatal_error(int signum)
}
/* restore the previous handler */
-#ifdef HAVE_SIGACTION
- (void)sigaction(signum, &handler->previous, NULL);
-#else
- (void)signal(signum, handler->previous);
-#endif
- handler->enabled = 0;
+ faulthandler_disable_fatal_handler(handler);
PUTS(fd, "Fatal Python error: ");
PUTS(fd, handler->name);
@@ -351,20 +361,117 @@ faulthandler_fatal_error(int signum)
raise(signum);
}
+#ifdef MS_WINDOWS
+static LONG WINAPI
+faulthandler_exc_handler(struct _EXCEPTION_POINTERS *exc_info)
+{
+ const int fd = fatal_error.fd;
+ DWORD code = exc_info->ExceptionRecord->ExceptionCode;
+ DWORD flags = exc_info->ExceptionRecord->ExceptionFlags;
+
+ /* only log fatal exceptions */
+ if (flags & EXCEPTION_NONCONTINUABLE) {
+ /* call the next exception handler */
+ return EXCEPTION_CONTINUE_SEARCH;
+ }
+
+ PUTS(fd, "Windows fatal exception: ");
+ switch (code)
+ {
+ /* only format most common errors */
+ case EXCEPTION_ACCESS_VIOLATION: PUTS(fd, "access violation"); break;
+ case EXCEPTION_FLT_DIVIDE_BY_ZERO: PUTS(fd, "float divide by zero"); break;
+ case EXCEPTION_FLT_OVERFLOW: PUTS(fd, "float overflow"); break;
+ case EXCEPTION_INT_DIVIDE_BY_ZERO: PUTS(fd, "int divide by zero"); break;
+ case EXCEPTION_INT_OVERFLOW: PUTS(fd, "integer overflow"); break;
+ case EXCEPTION_IN_PAGE_ERROR: PUTS(fd, "page error"); break;
+ case EXCEPTION_STACK_OVERFLOW: PUTS(fd, "stack overflow"); break;
+ default:
+ PUTS(fd, "code ");
+ _Py_DumpDecimal(fd, code);
+ }
+ PUTS(fd, "\n\n");
+
+ if (code == EXCEPTION_ACCESS_VIOLATION) {
+ /* disable signal handler for SIGSEGV */
+ size_t i;
+ for (i=0; i < faulthandler_nsignals; i++) {
+ fault_handler_t *handler = &faulthandler_handlers[i];
+ if (handler->signum == SIGSEGV) {
+ faulthandler_disable_fatal_handler(handler);
+ break;
+ }
+ }
+ }
+
+ faulthandler_dump_traceback(fd, fatal_error.all_threads,
+ fatal_error.interp);
+
+ /* call the next exception handler */
+ return EXCEPTION_CONTINUE_SEARCH;
+}
+#endif
+
/* Install the handler for fatal signals, faulthandler_fatal_error(). */
+static int
+faulthandler_enable(void)
+{
+ size_t i;
+
+ if (fatal_error.enabled) {
+ return 0;
+ }
+ fatal_error.enabled = 1;
+
+ for (i=0; i < faulthandler_nsignals; i++) {
+ fault_handler_t *handler;
+#ifdef HAVE_SIGACTION
+ struct sigaction action;
+#endif
+ int err;
+
+ handler = &faulthandler_handlers[i];
+ assert(!handler->enabled);
+#ifdef HAVE_SIGACTION
+ action.sa_handler = faulthandler_fatal_error;
+ sigemptyset(&action.sa_mask);
+ /* Do not prevent the signal from being received from within
+ its own signal handler */
+ action.sa_flags = SA_NODEFER;
+#ifdef HAVE_SIGALTSTACK
+ if (stack.ss_sp != NULL) {
+ /* Call the signal handler on an alternate signal stack
+ provided by sigaltstack() */
+ action.sa_flags |= SA_ONSTACK;
+ }
+#endif
+ err = sigaction(handler->signum, &action, &handler->previous);
+#else
+ handler->previous = signal(handler->signum,
+ faulthandler_fatal_error);
+ err = (handler->previous == SIG_ERR);
+#endif
+ if (err) {
+ PyErr_SetFromErrno(PyExc_RuntimeError);
+ return -1;
+ }
+
+ handler->enabled = 1;
+ }
+
+#ifdef MS_WINDOWS
+ AddVectoredExceptionHandler(1, faulthandler_exc_handler);
+#endif
+ return 0;
+}
+
static PyObject*
-faulthandler_enable(PyObject *self, PyObject *args, PyObject *kwargs)
+faulthandler_py_enable(PyObject *self, PyObject *args, PyObject *kwargs)
{
static char *kwlist[] = {"file", "all_threads", NULL};
PyObject *file = NULL;
int all_threads = 1;
- unsigned int i;
- fault_handler_t *handler;
-#ifdef HAVE_SIGACTION
- struct sigaction action;
-#endif
- int err;
int fd;
PyThreadState *tstate;
@@ -386,37 +493,10 @@ faulthandler_enable(PyObject *self, PyObject *args, PyObject *kwargs)
fatal_error.all_threads = all_threads;
fatal_error.interp = tstate->interp;
- if (!fatal_error.enabled) {
- fatal_error.enabled = 1;
-
- for (i=0; i < faulthandler_nsignals; i++) {
- handler = &faulthandler_handlers[i];
-#ifdef HAVE_SIGACTION
- action.sa_handler = faulthandler_fatal_error;
- sigemptyset(&action.sa_mask);
- /* Do not prevent the signal from being received from within
- its own signal handler */
- action.sa_flags = SA_NODEFER;
-#ifdef HAVE_SIGALTSTACK
- if (stack.ss_sp != NULL) {
- /* Call the signal handler on an alternate signal stack
- provided by sigaltstack() */
- action.sa_flags |= SA_ONSTACK;
- }
-#endif
- err = sigaction(handler->signum, &action, &handler->previous);
-#else
- handler->previous = signal(handler->signum,
- faulthandler_fatal_error);
- err = (handler->previous == SIG_ERR);
-#endif
- if (err) {
- PyErr_SetFromErrno(PyExc_RuntimeError);
- return NULL;
- }
- handler->enabled = 1;
- }
+ if (faulthandler_enable() < 0) {
+ return NULL;
}
+
Py_RETURN_NONE;
}
@@ -430,14 +510,7 @@ faulthandler_disable(void)
fatal_error.enabled = 0;
for (i=0; i < faulthandler_nsignals; i++) {
handler = &faulthandler_handlers[i];
- if (!handler->enabled)
- continue;
-#ifdef HAVE_SIGACTION
- (void)sigaction(handler->signum, &handler->previous, NULL);
-#else
- (void)signal(handler->signum, handler->previous);
-#endif
- handler->enabled = 0;
+ faulthandler_disable_fatal_handler(handler);
}
}
@@ -469,7 +542,6 @@ faulthandler_thread(void *unused)
{
PyLockStatus st;
const char* errmsg;
- PyThreadState *current;
int ok;
#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
sigset_t set;
@@ -489,12 +561,9 @@ faulthandler_thread(void *unused)
/* Timeout => dump traceback */
assert(st == PY_LOCK_FAILURE);
- /* get the thread holding the GIL, NULL if no thread hold the GIL */
- current = _PyThreadState_UncheckedGet();
-
_Py_write_noraise(thread.fd, thread.header, (int)thread.header_len);
- errmsg = _Py_DumpTracebackThreads(thread.fd, thread.interp, current);
+ errmsg = _Py_DumpTracebackThreads(thread.fd, thread.interp, NULL);
ok = (errmsg == NULL);
if (thread.exit)
@@ -894,7 +963,7 @@ static PyObject *
faulthandler_sigsegv(PyObject *self, PyObject *args)
{
int release_gil = 0;
- if (!PyArg_ParseTuple(args, "|i:_read_null", &release_gil))
+ if (!PyArg_ParseTuple(args, "|i:_sigsegv", &release_gil))
return NULL;
if (release_gil) {
@@ -907,6 +976,49 @@ faulthandler_sigsegv(PyObject *self, PyObject *args)
Py_RETURN_NONE;
}
+#ifdef WITH_THREAD
+static void
+faulthandler_fatal_error_thread(void *plock)
+{
+ PyThread_type_lock *lock = (PyThread_type_lock *)plock;
+
+ Py_FatalError("in new thread");
+
+ /* notify the caller that we are done */
+ PyThread_release_lock(lock);
+}
+
+static PyObject *
+faulthandler_fatal_error_c_thread(PyObject *self, PyObject *args)
+{
+ long thread;
+ PyThread_type_lock lock;
+
+ faulthandler_suppress_crash_report();
+
+ lock = PyThread_allocate_lock();
+ if (lock == NULL)
+ return PyErr_NoMemory();
+
+ PyThread_acquire_lock(lock, WAIT_LOCK);
+
+ thread = PyThread_start_new_thread(faulthandler_fatal_error_thread, lock);
+ if (thread == -1) {
+ PyThread_free_lock(lock);
+ PyErr_SetString(PyExc_RuntimeError, "unable to start the thread");
+ return NULL;
+ }
+
+ /* wait until the thread completes: it will never occur, since Py_FatalError()
+ exits the process immedialty. */
+ PyThread_acquire_lock(lock, WAIT_LOCK);
+ PyThread_release_lock(lock);
+ PyThread_free_lock(lock);
+
+ Py_RETURN_NONE;
+}
+#endif
+
static PyObject *
faulthandler_sigfpe(PyObject *self, PyObject *args)
{
@@ -951,6 +1063,8 @@ faulthandler_fatal_error_py(PyObject *self, PyObject *args)
}
#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
+#define FAULTHANDLER_STACK_OVERFLOW
+
#ifdef __INTEL_COMPILER
/* Issue #23654: Turn off ICC's tail call optimization for the
* stack_overflow generator. ICC turns the recursive tail call into
@@ -994,7 +1108,7 @@ faulthandler_stack_overflow(PyObject *self)
size, depth);
return NULL;
}
-#endif
+#endif /* defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION) */
static int
@@ -1017,12 +1131,25 @@ faulthandler_traverse(PyObject *module, visitproc visit, void *arg)
return 0;
}
+#ifdef MS_WINDOWS
+static PyObject *
+faulthandler_raise_exception(PyObject *self, PyObject *args)
+{
+ unsigned int code, flags = 0;
+ if (!PyArg_ParseTuple(args, "I|I:_raise_exception", &code, &flags))
+ return NULL;
+ faulthandler_suppress_crash_report();
+ RaiseException(code, flags, 0, NULL);
+ Py_RETURN_NONE;
+}
+#endif
+
PyDoc_STRVAR(module_doc,
"faulthandler module.");
static PyMethodDef module_methods[] = {
{"enable",
- (PyCFunction)faulthandler_enable, METH_VARARGS|METH_KEYWORDS,
+ (PyCFunction)faulthandler_py_enable, METH_VARARGS|METH_KEYWORDS,
PyDoc_STR("enable(file=sys.stderr, all_threads=True): "
"enable the fault handler")},
{"disable", (PyCFunction)faulthandler_disable_py, METH_NOARGS,
@@ -1065,16 +1192,25 @@ static PyMethodDef module_methods[] = {
"a SIGSEGV or SIGBUS signal depending on the platform")},
{"_sigsegv", faulthandler_sigsegv, METH_VARARGS,
PyDoc_STR("_sigsegv(release_gil=False): raise a SIGSEGV signal")},
+#ifdef WITH_THREAD
+ {"_fatal_error_c_thread", faulthandler_fatal_error_c_thread, METH_NOARGS,
+ PyDoc_STR("fatal_error_c_thread(): "
+ "call Py_FatalError() in a new C thread.")},
+#endif
{"_sigabrt", faulthandler_sigabrt, METH_NOARGS,
PyDoc_STR("_sigabrt(): raise a SIGABRT signal")},
{"_sigfpe", (PyCFunction)faulthandler_sigfpe, METH_NOARGS,
PyDoc_STR("_sigfpe(): raise a SIGFPE signal")},
{"_fatal_error", faulthandler_fatal_error_py, METH_VARARGS,
PyDoc_STR("_fatal_error(message): call Py_FatalError(message)")},
-#if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)
+#ifdef FAULTHANDLER_STACK_OVERFLOW
{"_stack_overflow", (PyCFunction)faulthandler_stack_overflow, METH_NOARGS,
PyDoc_STR("_stack_overflow(): recursive call to raise a stack overflow")},
#endif
+#ifdef MS_WINDOWS
+ {"_raise_exception", faulthandler_raise_exception, METH_VARARGS,
+ PyDoc_STR("raise_exception(code, flags=0): Call RaiseException(code, flags).")},
+#endif
{NULL, NULL} /* sentinel */
};
@@ -1093,7 +1229,33 @@ static struct PyModuleDef module_def = {
PyMODINIT_FUNC
PyInit_faulthandler(void)
{
- return PyModule_Create(&module_def);
+ PyObject *m = PyModule_Create(&module_def);
+ if (m == NULL)
+ return NULL;
+
+ /* Add constants for unit tests */
+#ifdef MS_WINDOWS
+ /* RaiseException() codes (prefixed by an underscore) */
+ if (PyModule_AddIntConstant(m, "_EXCEPTION_ACCESS_VIOLATION",
+ EXCEPTION_ACCESS_VIOLATION))
+ return NULL;
+ if (PyModule_AddIntConstant(m, "_EXCEPTION_INT_DIVIDE_BY_ZERO",
+ EXCEPTION_INT_DIVIDE_BY_ZERO))
+ return NULL;
+ if (PyModule_AddIntConstant(m, "_EXCEPTION_STACK_OVERFLOW",
+ EXCEPTION_STACK_OVERFLOW))
+ return NULL;
+
+ /* RaiseException() flags (prefixed by an underscore) */
+ if (PyModule_AddIntConstant(m, "_EXCEPTION_NONCONTINUABLE",
+ EXCEPTION_NONCONTINUABLE))
+ return NULL;
+ if (PyModule_AddIntConstant(m, "_EXCEPTION_NONCONTINUABLE_EXCEPTION",
+ EXCEPTION_NONCONTINUABLE_EXCEPTION))
+ return NULL;
+#endif
+
+ return m;
}
/* Call faulthandler.enable() if the PYTHONFAULTHANDLER environment variable
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
index cb7222db89..0c6f4448f0 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -738,7 +738,7 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
}
static void
-debug_cycle(char *msg, PyObject *op)
+debug_cycle(const char *msg, PyObject *op)
{
PySys_FormatStderr("gc: %s <%s %p>\n",
msg, Py_TYPE(op)->tp_name, op);
diff --git a/Modules/getaddrinfo.c b/Modules/getaddrinfo.c
index d8167eaf7c..b6fb53cb3d 100644
--- a/Modules/getaddrinfo.c
+++ b/Modules/getaddrinfo.c
@@ -136,7 +136,7 @@ static int get_addr(const char *, int, struct addrinfo **,
struct addrinfo *, int);
static int str_isnumber(const char *);
-static char *ai_errlist[] = {
+static const char * const ai_errlist[] = {
"success.",
"address family for hostname not supported.", /* EAI_ADDRFAMILY */
"temporary failure in name resolution.", /* EAI_AGAIN */
@@ -198,7 +198,7 @@ if (pai->ai_flags & AI_CANONNAME) {\
#define ERR(err) { error = (err); goto bad; }
-char *
+const char *
gai_strerror(int ecode)
{
if (ecode < 0 || ecode > EAI_MAX)
diff --git a/Modules/getpath.c b/Modules/getpath.c
index 18deb60dfa..65b47a3676 100644
--- a/Modules/getpath.c
+++ b/Modules/getpath.c
@@ -460,8 +460,8 @@ calculate_path(void)
{
extern wchar_t *Py_GetProgramName(void);
- static wchar_t delimiter[2] = {DELIM, '\0'};
- static wchar_t separator[2] = {SEP, '\0'};
+ static const wchar_t delimiter[2] = {DELIM, '\0'};
+ static const wchar_t separator[2] = {SEP, '\0'};
char *_rtpypath = Py_GETENV("PYTHONPATH"); /* XXX use wide version on Windows */
wchar_t *rtpypath = NULL;
wchar_t *home = Py_GetPythonHome();
diff --git a/Modules/grpmodule.c b/Modules/grpmodule.c
index 3a134a0333..9437ae7a5b 100644
--- a/Modules/grpmodule.c
+++ b/Modules/grpmodule.c
@@ -100,14 +100,25 @@ grp_getgrgid_impl(PyObject *module, PyObject *id)
gid_t gid;
struct group *p;
- py_int_id = PyNumber_Long(id);
- if (!py_int_id)
+ if (!_Py_Gid_Converter(id, &gid)) {
+ if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
return NULL;
- if (!_Py_Gid_Converter(py_int_id, &gid)) {
+ }
+ PyErr_Clear();
+ if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
+ "group id must be int, not %.200",
+ id->ob_type->tp_name) < 0) {
+ return NULL;
+ }
+ py_int_id = PyNumber_Long(id);
+ if (!py_int_id)
+ return NULL;
+ if (!_Py_Gid_Converter(py_int_id, &gid)) {
+ Py_DECREF(py_int_id);
+ return NULL;
+ }
Py_DECREF(py_int_id);
- return NULL;
}
- Py_DECREF(py_int_id);
if ((p = getgrgid(gid)) == NULL) {
PyObject *gid_obj = _PyLong_FromGid(gid);
diff --git a/Modules/hashtable.c b/Modules/hashtable.c
index 133f3133ef..b53cc24083 100644
--- a/Modules/hashtable.c
+++ b/Modules/hashtable.c
@@ -1,5 +1,5 @@
-/* The implementation of the hash table (_Py_hashtable_t) is based on the cfuhash
- project:
+/* The implementation of the hash table (_Py_hashtable_t) is based on the
+ cfuhash project:
http://sourceforge.net/projects/libcfu/
Copyright of cfuhash:
@@ -59,7 +59,21 @@
#define ENTRY_NEXT(ENTRY) \
((_Py_hashtable_entry_t *)_Py_SLIST_ITEM_NEXT(ENTRY))
#define HASHTABLE_ITEM_SIZE(HT) \
- (sizeof(_Py_hashtable_entry_t) + (HT)->data_size)
+ (sizeof(_Py_hashtable_entry_t) + (HT)->key_size + (HT)->data_size)
+
+#define ENTRY_READ_PDATA(TABLE, ENTRY, DATA_SIZE, PDATA) \
+ do { \
+ assert((DATA_SIZE) == (TABLE)->data_size); \
+ Py_MEMCPY((PDATA), _Py_HASHTABLE_ENTRY_PDATA(TABLE, (ENTRY)), \
+ (DATA_SIZE)); \
+ } while (0)
+
+#define ENTRY_WRITE_PDATA(TABLE, ENTRY, DATA_SIZE, PDATA) \
+ do { \
+ assert((DATA_SIZE) == (TABLE)->data_size); \
+ Py_MEMCPY((void *)_Py_HASHTABLE_ENTRY_PDATA((TABLE), (ENTRY)), \
+ (PDATA), (DATA_SIZE)); \
+ } while (0)
/* Forward declaration */
static void hashtable_rehash(_Py_hashtable_t *ht);
@@ -70,6 +84,7 @@ _Py_slist_init(_Py_slist_t *list)
list->head = NULL;
}
+
static void
_Py_slist_prepend(_Py_slist_t *list, _Py_slist_item_t *item)
{
@@ -77,6 +92,7 @@ _Py_slist_prepend(_Py_slist_t *list, _Py_slist_item_t *item)
list->head = item;
}
+
static void
_Py_slist_remove(_Py_slist_t *list, _Py_slist_item_t *previous,
_Py_slist_item_t *item)
@@ -87,24 +103,26 @@ _Py_slist_remove(_Py_slist_t *list, _Py_slist_item_t *previous,
list->head = item->next;
}
-Py_uhash_t
-_Py_hashtable_hash_int(const void *key)
-{
- return (Py_uhash_t)key;
-}
Py_uhash_t
-_Py_hashtable_hash_ptr(const void *key)
+_Py_hashtable_hash_ptr(struct _Py_hashtable_t *ht, const void *pkey)
{
- return (Py_uhash_t)_Py_HashPointer((void *)key);
+ void *key;
+
+ _Py_HASHTABLE_READ_KEY(ht, pkey, key);
+ return (Py_uhash_t)_Py_HashPointer(key);
}
+
int
-_Py_hashtable_compare_direct(const void *key, const _Py_hashtable_entry_t *entry)
+_Py_hashtable_compare_direct(_Py_hashtable_t *ht, const void *pkey,
+ const _Py_hashtable_entry_t *entry)
{
- return entry->key == key;
+ const void *pkey2 = _Py_HASHTABLE_ENTRY_PKEY(entry);
+ return (memcmp(pkey, pkey2, ht->key_size) == 0);
}
+
/* makes sure the real size of the buckets array is a power of 2 */
static size_t
round_size(size_t s)
@@ -118,13 +136,12 @@ round_size(size_t s)
return i;
}
+
_Py_hashtable_t *
-_Py_hashtable_new_full(size_t data_size, size_t init_size,
+_Py_hashtable_new_full(size_t key_size, size_t data_size,
+ size_t init_size,
_Py_hashtable_hash_func hash_func,
_Py_hashtable_compare_func compare_func,
- _Py_hashtable_copy_data_func copy_data_func,
- _Py_hashtable_free_data_func free_data_func,
- _Py_hashtable_get_data_size_func get_data_size_func,
_Py_hashtable_allocator_t *allocator)
{
_Py_hashtable_t *ht;
@@ -144,6 +161,7 @@ _Py_hashtable_new_full(size_t data_size, size_t init_size,
ht->num_buckets = round_size(init_size);
ht->entries = 0;
+ ht->key_size = key_size;
ht->data_size = data_size;
buckets_size = ht->num_buckets * sizeof(ht->buckets[0]);
@@ -156,28 +174,27 @@ _Py_hashtable_new_full(size_t data_size, size_t init_size,
ht->hash_func = hash_func;
ht->compare_func = compare_func;
- ht->copy_data_func = copy_data_func;
- ht->free_data_func = free_data_func;
- ht->get_data_size_func = get_data_size_func;
ht->alloc = alloc;
return ht;
}
+
_Py_hashtable_t *
-_Py_hashtable_new(size_t data_size,
+_Py_hashtable_new(size_t key_size, size_t data_size,
_Py_hashtable_hash_func hash_func,
_Py_hashtable_compare_func compare_func)
{
- return _Py_hashtable_new_full(data_size, HASHTABLE_MIN_SIZE,
+ return _Py_hashtable_new_full(key_size, data_size,
+ HASHTABLE_MIN_SIZE,
hash_func, compare_func,
- NULL, NULL, NULL, NULL);
+ NULL);
}
+
size_t
_Py_hashtable_size(_Py_hashtable_t *ht)
{
size_t size;
- size_t hv;
size = sizeof(_Py_hashtable_t);
@@ -187,22 +204,10 @@ _Py_hashtable_size(_Py_hashtable_t *ht)
/* entries */
size += ht->entries * HASHTABLE_ITEM_SIZE(ht);
- /* data linked from entries */
- if (ht->get_data_size_func) {
- for (hv = 0; hv < ht->num_buckets; hv++) {
- _Py_hashtable_entry_t *entry;
-
- for (entry = TABLE_HEAD(ht, hv); entry; entry = ENTRY_NEXT(entry)) {
- void *data;
-
- data = _Py_HASHTABLE_ENTRY_DATA_AS_VOID_P(entry);
- size += ht->get_data_size_func(data);
- }
- }
- }
return size;
}
+
#ifdef Py_DEBUG
void
_Py_hashtable_print_stats(_Py_hashtable_t *ht)
@@ -243,38 +248,45 @@ _Py_hashtable_print_stats(_Py_hashtable_t *ht)
}
#endif
-/* Get an entry. Return NULL if the key does not exist. */
+
_Py_hashtable_entry_t *
-_Py_hashtable_get_entry(_Py_hashtable_t *ht, const void *key)
+_Py_hashtable_get_entry(_Py_hashtable_t *ht,
+ size_t key_size, const void *pkey)
{
Py_uhash_t key_hash;
size_t index;
_Py_hashtable_entry_t *entry;
- key_hash = ht->hash_func(key);
+ assert(key_size == ht->key_size);
+
+ key_hash = ht->hash_func(ht, pkey);
index = key_hash & (ht->num_buckets - 1);
for (entry = TABLE_HEAD(ht, index); entry != NULL; entry = ENTRY_NEXT(entry)) {
- if (entry->key_hash == key_hash && ht->compare_func(key, entry))
+ if (entry->key_hash == key_hash && ht->compare_func(ht, pkey, entry))
break;
}
return entry;
}
+
static int
-_hashtable_pop_entry(_Py_hashtable_t *ht, const void *key, void *data, size_t data_size)
+_Py_hashtable_pop_entry(_Py_hashtable_t *ht, size_t key_size, const void *pkey,
+ void *data, size_t data_size)
{
Py_uhash_t key_hash;
size_t index;
_Py_hashtable_entry_t *entry, *previous;
- key_hash = ht->hash_func(key);
+ assert(key_size == ht->key_size);
+
+ key_hash = ht->hash_func(ht, pkey);
index = key_hash & (ht->num_buckets - 1);
previous = NULL;
for (entry = TABLE_HEAD(ht, index); entry != NULL; entry = ENTRY_NEXT(entry)) {
- if (entry->key_hash == key_hash && ht->compare_func(key, entry))
+ if (entry->key_hash == key_hash && ht->compare_func(ht, pkey, entry))
break;
previous = entry;
}
@@ -287,7 +299,7 @@ _hashtable_pop_entry(_Py_hashtable_t *ht, const void *key, void *data, size_t da
ht->entries--;
if (data != NULL)
- _Py_HASHTABLE_ENTRY_READ_DATA(ht, data, data_size, entry);
+ ENTRY_READ_PDATA(ht, entry, data_size, data);
ht->alloc.free(entry);
if ((float)ht->entries / (float)ht->num_buckets < HASHTABLE_LOW)
@@ -295,26 +307,27 @@ _hashtable_pop_entry(_Py_hashtable_t *ht, const void *key, void *data, size_t da
return 1;
}
-/* Add a new entry to the hash. The key must not be present in the hash table.
- Return 0 on success, -1 on memory error. */
+
int
-_Py_hashtable_set(_Py_hashtable_t *ht, const void *key,
- void *data, size_t data_size)
+_Py_hashtable_set(_Py_hashtable_t *ht, size_t key_size, const void *pkey,
+ size_t data_size, const void *data)
{
Py_uhash_t key_hash;
size_t index;
_Py_hashtable_entry_t *entry;
+ assert(key_size == ht->key_size);
+
assert(data != NULL || data_size == 0);
#ifndef NDEBUG
/* Don't write the assertion on a single line because it is interesting
to know the duplicated entry if the assertion failed. The entry can
be read using a debugger. */
- entry = _Py_hashtable_get_entry(ht, key);
+ entry = _Py_hashtable_get_entry(ht, key_size, pkey);
assert(entry == NULL);
#endif
- key_hash = ht->hash_func(key);
+ key_hash = ht->hash_func(ht, pkey);
index = key_hash & (ht->num_buckets - 1);
entry = ht->alloc.malloc(HASHTABLE_ITEM_SIZE(ht));
@@ -323,11 +336,9 @@ _Py_hashtable_set(_Py_hashtable_t *ht, const void *key,
return -1;
}
- entry->key = (void *)key;
entry->key_hash = key_hash;
-
- assert(data_size == ht->data_size);
- memcpy(_Py_HASHTABLE_ENTRY_DATA(entry), data, data_size);
+ Py_MEMCPY((void *)_Py_HASHTABLE_ENTRY_PKEY(entry), pkey, ht->key_size);
+ ENTRY_WRITE_PDATA(ht, entry, data_size, data);
_Py_slist_prepend(&ht->buckets[index], (_Py_slist_item_t*)entry);
ht->entries++;
@@ -337,48 +348,50 @@ _Py_hashtable_set(_Py_hashtable_t *ht, const void *key,
return 0;
}
-/* Get data from an entry. Copy entry data into data and return 1 if the entry
- exists, return 0 if the entry does not exist. */
+
int
-_Py_hashtable_get(_Py_hashtable_t *ht, const void *key, void *data, size_t data_size)
+_Py_hashtable_get(_Py_hashtable_t *ht, size_t key_size,const void *pkey,
+ size_t data_size, void *data)
{
_Py_hashtable_entry_t *entry;
assert(data != NULL);
- entry = _Py_hashtable_get_entry(ht, key);
+ entry = _Py_hashtable_get_entry(ht, key_size, pkey);
if (entry == NULL)
return 0;
- _Py_HASHTABLE_ENTRY_READ_DATA(ht, data, data_size, entry);
+ ENTRY_READ_PDATA(ht, entry, data_size, data);
return 1;
}
+
int
-_Py_hashtable_pop(_Py_hashtable_t *ht, const void *key, void *data, size_t data_size)
+_Py_hashtable_pop(_Py_hashtable_t *ht, size_t key_size, const void *pkey,
+ size_t data_size, void *data)
{
assert(data != NULL);
- assert(ht->free_data_func == NULL);
- return _hashtable_pop_entry(ht, key, data, data_size);
+ return _Py_hashtable_pop_entry(ht, key_size, pkey, data, data_size);
}
-/* Delete an entry. The entry must exist. */
+
+/* Code commented since the function is not needed in Python */
+#if 0
void
-_Py_hashtable_delete(_Py_hashtable_t *ht, const void *key)
+_Py_hashtable_delete(_Py_hashtable_t *ht, size_t key_size, const void *pkey)
{
#ifndef NDEBUG
- int found = _hashtable_pop_entry(ht, key, NULL, 0);
+ int found = _Py_hashtable_pop_entry(ht, key_size, pkey, NULL, 0);
assert(found);
#else
- (void)_hashtable_pop_entry(ht, key, NULL, 0);
+ (void)_Py_hashtable_pop_entry(ht, key_size, pkey, NULL, 0);
#endif
}
+#endif
+
-/* Prototype for a pointer to a function to be called foreach
- key/value pair in the hash by hashtable_foreach(). Iteration
- stops if a non-zero value is returned. */
int
_Py_hashtable_foreach(_Py_hashtable_t *ht,
- int (*func) (_Py_hashtable_entry_t *entry, void *arg),
+ _Py_hashtable_foreach_func func,
void *arg)
{
_Py_hashtable_entry_t *entry;
@@ -386,7 +399,7 @@ _Py_hashtable_foreach(_Py_hashtable_t *ht,
for (hv = 0; hv < ht->num_buckets; hv++) {
for (entry = TABLE_HEAD(ht, hv); entry; entry = ENTRY_NEXT(entry)) {
- int res = func(entry, arg);
+ int res = func(ht, entry, arg);
if (res)
return res;
}
@@ -394,6 +407,7 @@ _Py_hashtable_foreach(_Py_hashtable_t *ht,
return 0;
}
+
static void
hashtable_rehash(_Py_hashtable_t *ht)
{
@@ -425,7 +439,8 @@ hashtable_rehash(_Py_hashtable_t *ht)
for (entry = BUCKETS_HEAD(old_buckets[bucket]); entry != NULL; entry = next) {
size_t entry_index;
- assert(ht->hash_func(entry->key) == entry->key_hash);
+
+ assert(ht->hash_func(ht, _Py_HASHTABLE_ENTRY_PKEY(entry)) == entry->key_hash);
next = ENTRY_NEXT(entry);
entry_index = entry->key_hash & (new_size - 1);
@@ -436,6 +451,7 @@ hashtable_rehash(_Py_hashtable_t *ht)
ht->alloc.free(old_buckets);
}
+
void
_Py_hashtable_clear(_Py_hashtable_t *ht)
{
@@ -445,8 +461,6 @@ _Py_hashtable_clear(_Py_hashtable_t *ht)
for (i=0; i < ht->num_buckets; i++) {
for (entry = TABLE_HEAD(ht, i); entry != NULL; entry = next) {
next = ENTRY_NEXT(entry);
- if (ht->free_data_func)
- ht->free_data_func(_Py_HASHTABLE_ENTRY_DATA_AS_VOID_P(entry));
ht->alloc.free(entry);
}
_Py_slist_init(&ht->buckets[i]);
@@ -455,6 +469,7 @@ _Py_hashtable_clear(_Py_hashtable_t *ht)
hashtable_rehash(ht);
}
+
void
_Py_hashtable_destroy(_Py_hashtable_t *ht)
{
@@ -464,8 +479,6 @@ _Py_hashtable_destroy(_Py_hashtable_t *ht)
_Py_slist_item_t *entry = ht->buckets[i].head;
while (entry) {
_Py_slist_item_t *entry_next = entry->next;
- if (ht->free_data_func)
- ht->free_data_func(_Py_HASHTABLE_ENTRY_DATA_AS_VOID_P(entry));
ht->alloc.free(entry);
entry = entry_next;
}
@@ -475,39 +488,31 @@ _Py_hashtable_destroy(_Py_hashtable_t *ht)
ht->alloc.free(ht);
}
-/* Return a copy of the hash table */
+
_Py_hashtable_t *
_Py_hashtable_copy(_Py_hashtable_t *src)
{
+ const size_t key_size = src->key_size;
+ const size_t data_size = src->data_size;
_Py_hashtable_t *dst;
_Py_hashtable_entry_t *entry;
size_t bucket;
int err;
- void *data, *new_data;
- dst = _Py_hashtable_new_full(src->data_size, src->num_buckets,
- src->hash_func, src->compare_func,
- src->copy_data_func, src->free_data_func,
- src->get_data_size_func, &src->alloc);
+ dst = _Py_hashtable_new_full(key_size, data_size,
+ src->num_buckets,
+ src->hash_func,
+ src->compare_func,
+ &src->alloc);
if (dst == NULL)
return NULL;
for (bucket=0; bucket < src->num_buckets; bucket++) {
entry = TABLE_HEAD(src, bucket);
for (; entry; entry = ENTRY_NEXT(entry)) {
- if (src->copy_data_func) {
- data = _Py_HASHTABLE_ENTRY_DATA_AS_VOID_P(entry);
- new_data = src->copy_data_func(data);
- if (new_data != NULL)
- err = _Py_hashtable_set(dst, entry->key,
- &new_data, src->data_size);
- else
- err = 1;
- }
- else {
- data = _Py_HASHTABLE_ENTRY_DATA(entry);
- err = _Py_hashtable_set(dst, entry->key, data, src->data_size);
- }
+ const void *pkey = _Py_HASHTABLE_ENTRY_PKEY(entry);
+ const void *pdata = _Py_HASHTABLE_ENTRY_PDATA(src, entry);
+ err = _Py_hashtable_set(dst, key_size, pkey, data_size, pdata);
if (err) {
_Py_hashtable_destroy(dst);
return NULL;
@@ -516,4 +521,3 @@ _Py_hashtable_copy(_Py_hashtable_t *src)
}
return dst;
}
-
diff --git a/Modules/hashtable.h b/Modules/hashtable.h
index a9f9993bfd..18fed096c1 100644
--- a/Modules/hashtable.h
+++ b/Modules/hashtable.h
@@ -1,9 +1,10 @@
#ifndef Py_HASHTABLE_H
#define Py_HASHTABLE_H
-
/* The whole API is private */
#ifndef Py_LIMITED_API
+/* Single linked list */
+
typedef struct _Py_slist_item_s {
struct _Py_slist_item_s *next;
} _Py_slist_item_t;
@@ -16,33 +17,66 @@ typedef struct {
#define _Py_SLIST_HEAD(SLIST) (((_Py_slist_t *)SLIST)->head)
+
+/* _Py_hashtable: table entry */
+
typedef struct {
/* used by _Py_hashtable_t.buckets to link entries */
_Py_slist_item_t _Py_slist_item;
- const void *key;
Py_uhash_t key_hash;
- /* data follows */
+ /* key (key_size bytes) and then data (data_size bytes) follows */
} _Py_hashtable_entry_t;
-#define _Py_HASHTABLE_ENTRY_DATA(ENTRY) \
- ((char *)(ENTRY) + sizeof(_Py_hashtable_entry_t))
+#define _Py_HASHTABLE_ENTRY_PKEY(ENTRY) \
+ ((const void *)((char *)(ENTRY) \
+ + sizeof(_Py_hashtable_entry_t)))
+
+#define _Py_HASHTABLE_ENTRY_PDATA(TABLE, ENTRY) \
+ ((const void *)((char *)(ENTRY) \
+ + sizeof(_Py_hashtable_entry_t) \
+ + (TABLE)->key_size))
+
+/* Get a key value from pkey: use memcpy() rather than a pointer dereference
+ to avoid memory alignment issues. */
+#define _Py_HASHTABLE_READ_KEY(TABLE, PKEY, DST_KEY) \
+ do { \
+ assert(sizeof(DST_KEY) == (TABLE)->key_size); \
+ Py_MEMCPY(&(DST_KEY), (PKEY), sizeof(DST_KEY)); \
+ } while (0)
+
+#define _Py_HASHTABLE_ENTRY_READ_KEY(TABLE, ENTRY, KEY) \
+ do { \
+ assert(sizeof(KEY) == (TABLE)->key_size); \
+ Py_MEMCPY(&(KEY), _Py_HASHTABLE_ENTRY_PKEY(ENTRY), sizeof(KEY)); \
+ } while (0)
-#define _Py_HASHTABLE_ENTRY_DATA_AS_VOID_P(ENTRY) \
- (*(void **)_Py_HASHTABLE_ENTRY_DATA(ENTRY))
+#define _Py_HASHTABLE_ENTRY_READ_DATA(TABLE, ENTRY, DATA) \
+ do { \
+ assert(sizeof(DATA) == (TABLE)->data_size); \
+ Py_MEMCPY(&(DATA), _Py_HASHTABLE_ENTRY_PDATA(TABLE, (ENTRY)), \
+ sizeof(DATA)); \
+ } while (0)
-#define _Py_HASHTABLE_ENTRY_READ_DATA(TABLE, DATA, DATA_SIZE, ENTRY) \
+#define _Py_HASHTABLE_ENTRY_WRITE_DATA(TABLE, ENTRY, DATA) \
do { \
- assert((DATA_SIZE) == (TABLE)->data_size); \
- memcpy(DATA, _Py_HASHTABLE_ENTRY_DATA(ENTRY), DATA_SIZE); \
+ assert(sizeof(DATA) == (TABLE)->data_size); \
+ Py_MEMCPY((void *)_Py_HASHTABLE_ENTRY_PDATA((TABLE), (ENTRY)), \
+ &(DATA), sizeof(DATA)); \
} while (0)
-typedef Py_uhash_t (*_Py_hashtable_hash_func) (const void *key);
-typedef int (*_Py_hashtable_compare_func) (const void *key, const _Py_hashtable_entry_t *he);
-typedef void* (*_Py_hashtable_copy_data_func)(void *data);
-typedef void (*_Py_hashtable_free_data_func)(void *data);
-typedef size_t (*_Py_hashtable_get_data_size_func)(void *data);
+
+/* _Py_hashtable: prototypes */
+
+/* Forward declaration */
+struct _Py_hashtable_t;
+
+typedef Py_uhash_t (*_Py_hashtable_hash_func) (struct _Py_hashtable_t *ht,
+ const void *pkey);
+typedef int (*_Py_hashtable_compare_func) (struct _Py_hashtable_t *ht,
+ const void *pkey,
+ const _Py_hashtable_entry_t *he);
typedef struct {
/* allocate a memory block */
@@ -52,77 +86,126 @@ typedef struct {
void (*free) (void *ptr);
} _Py_hashtable_allocator_t;
-typedef struct {
+
+/* _Py_hashtable: table */
+
+typedef struct _Py_hashtable_t {
size_t num_buckets;
size_t entries; /* Total number of entries in the table. */
_Py_slist_t *buckets;
+ size_t key_size;
size_t data_size;
_Py_hashtable_hash_func hash_func;
_Py_hashtable_compare_func compare_func;
- _Py_hashtable_copy_data_func copy_data_func;
- _Py_hashtable_free_data_func free_data_func;
- _Py_hashtable_get_data_size_func get_data_size_func;
_Py_hashtable_allocator_t alloc;
} _Py_hashtable_t;
-/* hash and compare functions for integers and pointers */
-PyAPI_FUNC(Py_uhash_t) _Py_hashtable_hash_ptr(const void *key);
-PyAPI_FUNC(Py_uhash_t) _Py_hashtable_hash_int(const void *key);
-PyAPI_FUNC(int) _Py_hashtable_compare_direct(const void *key, const _Py_hashtable_entry_t *entry);
+/* hash a pointer (void*) */
+PyAPI_FUNC(Py_uhash_t) _Py_hashtable_hash_ptr(
+ struct _Py_hashtable_t *ht,
+ const void *pkey);
+
+/* comparison using memcmp() */
+PyAPI_FUNC(int) _Py_hashtable_compare_direct(
+ _Py_hashtable_t *ht,
+ const void *pkey,
+ const _Py_hashtable_entry_t *entry);
PyAPI_FUNC(_Py_hashtable_t *) _Py_hashtable_new(
+ size_t key_size,
size_t data_size,
_Py_hashtable_hash_func hash_func,
_Py_hashtable_compare_func compare_func);
+
PyAPI_FUNC(_Py_hashtable_t *) _Py_hashtable_new_full(
+ size_t key_size,
size_t data_size,
size_t init_size,
_Py_hashtable_hash_func hash_func,
_Py_hashtable_compare_func compare_func,
- _Py_hashtable_copy_data_func copy_data_func,
- _Py_hashtable_free_data_func free_data_func,
- _Py_hashtable_get_data_size_func get_data_size_func,
_Py_hashtable_allocator_t *allocator);
+
+PyAPI_FUNC(void) _Py_hashtable_destroy(_Py_hashtable_t *ht);
+
+/* Return a copy of the hash table */
PyAPI_FUNC(_Py_hashtable_t *) _Py_hashtable_copy(_Py_hashtable_t *src);
+
PyAPI_FUNC(void) _Py_hashtable_clear(_Py_hashtable_t *ht);
-PyAPI_FUNC(void) _Py_hashtable_destroy(_Py_hashtable_t *ht);
-typedef int (*_Py_hashtable_foreach_func) (_Py_hashtable_entry_t *entry, void *arg);
+typedef int (*_Py_hashtable_foreach_func) (_Py_hashtable_t *ht,
+ _Py_hashtable_entry_t *entry,
+ void *arg);
+/* Call func() on each entry of the hashtable.
+ Iteration stops if func() result is non-zero, in this case it's the result
+ of the call. Otherwise, the function returns 0. */
PyAPI_FUNC(int) _Py_hashtable_foreach(
_Py_hashtable_t *ht,
- _Py_hashtable_foreach_func func, void *arg);
+ _Py_hashtable_foreach_func func,
+ void *arg);
+
PyAPI_FUNC(size_t) _Py_hashtable_size(_Py_hashtable_t *ht);
-PyAPI_FUNC(_Py_hashtable_entry_t*) _Py_hashtable_get_entry(
- _Py_hashtable_t *ht,
- const void *key);
+/* Add a new entry to the hash. The key must not be present in the hash table.
+ Return 0 on success, -1 on memory error.
+
+ Don't call directly this function,
+ but use _Py_HASHTABLE_SET() and _Py_HASHTABLE_SET_NODATA() macros */
PyAPI_FUNC(int) _Py_hashtable_set(
_Py_hashtable_t *ht,
- const void *key,
- void *data,
- size_t data_size);
+ size_t key_size,
+ const void *pkey,
+ size_t data_size,
+ const void *data);
+
+#define _Py_HASHTABLE_SET(TABLE, KEY, DATA) \
+ _Py_hashtable_set(TABLE, sizeof(KEY), &(KEY), sizeof(DATA), &(DATA))
+
+#define _Py_HASHTABLE_SET_NODATA(TABLE, KEY) \
+ _Py_hashtable_set(TABLE, sizeof(KEY), &(KEY), 0, NULL)
+
+
+/* Get an entry.
+ Return NULL if the key does not exist.
+
+ Don't call directly this function, but use _Py_HASHTABLE_GET_ENTRY()
+ macro */
+PyAPI_FUNC(_Py_hashtable_entry_t*) _Py_hashtable_get_entry(
+ _Py_hashtable_t *ht,
+ size_t key_size,
+ const void *pkey);
+
+#define _Py_HASHTABLE_GET_ENTRY(TABLE, KEY) \
+ _Py_hashtable_get_entry(TABLE, sizeof(KEY), &(KEY))
+
+
+/* Get data from an entry. Copy entry data into data and return 1 if the entry
+ exists, return 0 if the entry does not exist.
+
+ Don't call directly this function, but use _Py_HASHTABLE_GET() macro */
PyAPI_FUNC(int) _Py_hashtable_get(
_Py_hashtable_t *ht,
- const void *key,
- void *data,
- size_t data_size);
+ size_t key_size,
+ const void *pkey,
+ size_t data_size,
+ void *data);
+
+#define _Py_HASHTABLE_GET(TABLE, KEY, DATA) \
+ _Py_hashtable_get(TABLE, sizeof(KEY), &(KEY), sizeof(DATA), &(DATA))
+
+
+/* Don't call directly this function, but use _Py_HASHTABLE_POP() macro */
PyAPI_FUNC(int) _Py_hashtable_pop(
_Py_hashtable_t *ht,
- const void *key,
- void *data,
- size_t data_size);
-PyAPI_FUNC(void) _Py_hashtable_delete(
- _Py_hashtable_t *ht,
- const void *key);
+ size_t key_size,
+ const void *pkey,
+ size_t data_size,
+ void *data);
-#define _Py_HASHTABLE_SET(TABLE, KEY, DATA) \
- _Py_hashtable_set(TABLE, KEY, &(DATA), sizeof(DATA))
+#define _Py_HASHTABLE_POP(TABLE, KEY, DATA) \
+ _Py_hashtable_pop(TABLE, sizeof(KEY), &(KEY), sizeof(DATA), &(DATA))
-#define _Py_HASHTABLE_GET(TABLE, KEY, DATA) \
- _Py_hashtable_get(TABLE, KEY, &(DATA), sizeof(DATA))
#endif /* Py_LIMITED_API */
-
#endif
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 409922a643..c82ad7aaf6 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -1,4 +1,5 @@
+#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "structmember.h"
@@ -7,7 +8,7 @@
*/
-/* groupby object ***********************************************************/
+/* groupby object ************************************************************/
typedef struct {
PyObject_HEAD
@@ -74,7 +75,7 @@ groupby_traverse(groupbyobject *gbo, visitproc visit, void *arg)
static PyObject *
groupby_next(groupbyobject *gbo)
{
- PyObject *newvalue, *newkey, *r, *grouper, *tmp;
+ PyObject *newvalue, *newkey, *r, *grouper;
/* skip to next iteration group */
for (;;) {
@@ -85,8 +86,7 @@ groupby_next(groupbyobject *gbo)
else {
int rcmp;
- rcmp = PyObject_RichCompareBool(gbo->tgtkey,
- gbo->currkey, Py_EQ);
+ rcmp = PyObject_RichCompareBool(gbo->tgtkey, gbo->currkey, Py_EQ);
if (rcmp == -1)
return NULL;
else if (rcmp == 0)
@@ -101,27 +101,19 @@ groupby_next(groupbyobject *gbo)
newkey = newvalue;
Py_INCREF(newvalue);
} else {
- newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc,
- newvalue, NULL);
+ newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc, newvalue, NULL);
if (newkey == NULL) {
Py_DECREF(newvalue);
return NULL;
}
}
- tmp = gbo->currkey;
- gbo->currkey = newkey;
- Py_XDECREF(tmp);
-
- tmp = gbo->currvalue;
- gbo->currvalue = newvalue;
- Py_XDECREF(tmp);
+ Py_XSETREF(gbo->currkey, newkey);
+ Py_XSETREF(gbo->currvalue, newvalue);
}
Py_INCREF(gbo->currkey);
- tmp = gbo->tgtkey;
- gbo->tgtkey = gbo->currkey;
- Py_XDECREF(tmp);
+ Py_XSETREF(gbo->tgtkey, gbo->currkey);
grouper = _grouper_create(gbo, gbo->tgtkey);
if (grouper == NULL)
@@ -173,7 +165,7 @@ static PyMethodDef groupby_methods[] = {
reduce_doc},
{"__setstate__", (PyCFunction)groupby_setstate, METH_O,
setstate_doc},
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
PyDoc_STRVAR(groupby_doc,
@@ -296,8 +288,7 @@ _grouper_next(_grouperobject *igo)
newkey = newvalue;
Py_INCREF(newvalue);
} else {
- newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc,
- newvalue, NULL);
+ newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc, newvalue, NULL);
if (newkey == NULL) {
Py_DECREF(newvalue);
return NULL;
@@ -325,8 +316,7 @@ _grouper_next(_grouperobject *igo)
static PyObject *
_grouper_reduce(_grouperobject *lz)
{
- return Py_BuildValue("O(OO)", Py_TYPE(lz),
- lz->parent, lz->tgtkey);
+ return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->parent, lz->tgtkey);
}
static PyMethodDef _grouper_methods[] = {
@@ -359,7 +349,7 @@ static PyTypeObject _grouper_type = {
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
0, /* tp_doc */
- (traverseproc)_grouper_traverse,/* tp_traverse */
+ (traverseproc)_grouper_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
@@ -380,8 +370,7 @@ static PyTypeObject _grouper_type = {
};
-
-/* tee object and with supporting function and objects ***************/
+/* tee object and with supporting function and objects ***********************/
/* The teedataobject pre-allocates space for LINKCELLS number of objects.
To help the object fit neatly inside cache lines (space for 16 to 32
@@ -396,7 +385,7 @@ static PyTypeObject _grouper_type = {
typedef struct {
PyObject_HEAD
PyObject *it;
- int numread; /* 0 <= numread <= LINKCELLS */
+ int numread; /* 0 <= numread <= LINKCELLS */
PyObject *nextlink;
PyObject *(values[LINKCELLS]);
} teedataobject;
@@ -404,7 +393,7 @@ typedef struct {
typedef struct {
PyObject_HEAD
teedataobject *dataobj;
- int index; /* 0 <= index <= LINKCELLS */
+ int index; /* 0 <= index <= LINKCELLS */
PyObject *weakreflist;
} teeobject;
@@ -461,6 +450,7 @@ static int
teedataobject_traverse(teedataobject *tdo, visitproc visit, void * arg)
{
int i;
+
Py_VISIT(tdo->it);
for (i = 0; i < tdo->numread; i++)
Py_VISIT(tdo->values[i]);
@@ -510,6 +500,7 @@ teedataobject_reduce(teedataobject *tdo)
int i;
/* create a temporary list of already iterated values */
PyObject *values = PyList_New(tdo->numread);
+
if (!values)
return NULL;
for (i=0 ; i<tdo->numread ; i++) {
@@ -577,7 +568,7 @@ static PyMethodDef teedataobject_methods[] = {
PyDoc_STRVAR(teedataobject_doc, "Data container common to multiple tee objects.");
static PyTypeObject teedataobject_type = {
- PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
+ PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */
"itertools._tee_dataobject", /* tp_name */
sizeof(teedataobject), /* tp_basicsize */
0, /* tp_itemsize */
@@ -597,7 +588,7 @@ static PyTypeObject teedataobject_type = {
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
teedataobject_doc, /* tp_doc */
(traverseproc)teedataobject_traverse, /* tp_traverse */
(inquiry)teedataobject_clear, /* tp_clear */
@@ -752,9 +743,9 @@ PyDoc_STRVAR(teeobject_doc,
"Iterator wrapped to make it copyable");
static PyMethodDef tee_methods[] = {
- {"__copy__", (PyCFunction)tee_copy, METH_NOARGS, teecopy_doc},
- {"__reduce__", (PyCFunction)tee_reduce, METH_NOARGS, reduce_doc},
- {"__setstate__", (PyCFunction)tee_setstate, METH_O, setstate_doc},
+ {"__copy__", (PyCFunction)tee_copy, METH_NOARGS, teecopy_doc},
+ {"__reduce__", (PyCFunction)tee_reduce, METH_NOARGS, reduce_doc},
+ {"__setstate__", (PyCFunction)tee_setstate, METH_O, setstate_doc},
{NULL, NULL} /* sentinel */
};
@@ -784,7 +775,7 @@ static PyTypeObject tee_type = {
(traverseproc)tee_traverse, /* tp_traverse */
(inquiry)tee_clear, /* tp_clear */
0, /* tp_richcompare */
- offsetof(teeobject, weakreflist), /* tp_weaklistoffset */
+ offsetof(teeobject, weakreflist), /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)tee_next, /* tp_iternext */
tee_methods, /* tp_methods */
@@ -850,12 +841,13 @@ PyDoc_STRVAR(tee_doc,
"tee(iterable, n=2) --> tuple of n independent iterators.");
-/* cycle object **********************************************************/
+/* cycle object **************************************************************/
typedef struct {
PyObject_HEAD
PyObject *it;
PyObject *saved;
+ Py_ssize_t index;
int firstpass;
} cycleobject;
@@ -895,6 +887,7 @@ cycle_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
lz->it = it;
lz->saved = saved;
+ lz->index = 0;
lz->firstpass = 0;
return (PyObject *)lz;
@@ -904,15 +897,16 @@ static void
cycle_dealloc(cycleobject *lz)
{
PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->saved);
Py_XDECREF(lz->it);
+ Py_XDECREF(lz->saved);
Py_TYPE(lz)->tp_free(lz);
}
static int
cycle_traverse(cycleobject *lz, visitproc visit, void *arg)
{
- Py_VISIT(lz->it);
+ if (lz->it)
+ Py_VISIT(lz->it);
Py_VISIT(lz->saved);
return 0;
}
@@ -921,56 +915,69 @@ static PyObject *
cycle_next(cycleobject *lz)
{
PyObject *item;
- PyObject *it;
- PyObject *tmp;
- while (1) {
+ if (lz->it != NULL) {
item = PyIter_Next(lz->it);
if (item != NULL) {
- if (!lz->firstpass && PyList_Append(lz->saved, item)) {
+ if (lz->firstpass)
+ return item;
+ if (PyList_Append(lz->saved, item)) {
Py_DECREF(item);
return NULL;
}
return item;
}
- if (PyErr_Occurred()) {
- if (PyErr_ExceptionMatches(PyExc_StopIteration))
- PyErr_Clear();
- else
- return NULL;
- }
- if (PyList_Size(lz->saved) == 0)
+ /* Note: StopIteration is already cleared by PyIter_Next() */
+ if (PyErr_Occurred())
return NULL;
- it = PyObject_GetIter(lz->saved);
- if (it == NULL)
- return NULL;
- tmp = lz->it;
- lz->it = it;
- lz->firstpass = 1;
- Py_DECREF(tmp);
+ Py_CLEAR(lz->it);
}
+ if (Py_SIZE(lz->saved) == 0)
+ return NULL;
+ item = PyList_GET_ITEM(lz->saved, lz->index);
+ lz->index++;
+ if (lz->index >= Py_SIZE(lz->saved))
+ lz->index = 0;
+ Py_INCREF(item);
+ return item;
}
static PyObject *
cycle_reduce(cycleobject *lz)
{
- /* Create a new cycle with the iterator tuple, then set
- * the saved state on it.
- */
- return Py_BuildValue("O(O)(Oi)", Py_TYPE(lz),
- lz->it, lz->saved, lz->firstpass);
+ /* Create a new cycle with the iterator tuple, then set the saved state */
+ if (lz->it == NULL) {
+ PyObject *it = PyObject_GetIter(lz->saved);
+ if (it == NULL)
+ return NULL;
+ if (lz->index != 0) {
+ _Py_IDENTIFIER(__setstate__);
+ PyObject *res = _PyObject_CallMethodId(it, &PyId___setstate__,
+ "n", lz->index);
+ if (res == NULL) {
+ Py_DECREF(it);
+ return NULL;
+ }
+ Py_DECREF(res);
+ }
+ return Py_BuildValue("O(N)(Oi)", Py_TYPE(lz), it, lz->saved, 1);
}
+ return Py_BuildValue("O(O)(Oi)", Py_TYPE(lz), lz->it, lz->saved,
+ lz->firstpass);
+}
static PyObject *
cycle_setstate(cycleobject *lz, PyObject *state)
{
PyObject *saved=NULL;
int firstpass;
- if (!PyArg_ParseTuple(state, "Oi", &saved, &firstpass))
+
+ if (!PyArg_ParseTuple(state, "O!i", &PyList_Type, &saved, &firstpass))
return NULL;
- Py_XINCREF(saved);
+ Py_INCREF(saved);
Py_XSETREF(lz->saved, saved);
lz->firstpass = firstpass != 0;
+ lz->index = 0;
Py_RETURN_NONE;
}
@@ -1039,7 +1046,7 @@ typedef struct {
PyObject_HEAD
PyObject *func;
PyObject *it;
- long start;
+ long start;
} dropwhileobject;
static PyTypeObject dropwhile_type;
@@ -1129,8 +1136,7 @@ dropwhile_next(dropwhileobject *lz)
static PyObject *
dropwhile_reduce(dropwhileobject *lz)
{
- return Py_BuildValue("O(OO)l", Py_TYPE(lz),
- lz->func, lz->it, lz->start);
+ return Py_BuildValue("O(OO)l", Py_TYPE(lz), lz->func, lz->it, lz->start);
}
static PyObject *
@@ -1181,13 +1187,13 @@ static PyTypeObject dropwhile_type = {
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE, /* tp_flags */
dropwhile_doc, /* tp_doc */
- (traverseproc)dropwhile_traverse, /* tp_traverse */
+ (traverseproc)dropwhile_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)dropwhile_next, /* tp_iternext */
- dropwhile_methods, /* tp_methods */
+ dropwhile_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
@@ -1208,7 +1214,7 @@ typedef struct {
PyObject_HEAD
PyObject *func;
PyObject *it;
- long stop;
+ long stop;
} takewhileobject;
static PyTypeObject takewhile_type;
@@ -1283,7 +1289,7 @@ takewhile_next(takewhileobject *lz)
}
ok = PyObject_IsTrue(good);
Py_DECREF(good);
- if (ok == 1)
+ if (ok > 0)
return item;
Py_DECREF(item);
if (ok == 0)
@@ -1294,14 +1300,14 @@ takewhile_next(takewhileobject *lz)
static PyObject *
takewhile_reduce(takewhileobject *lz)
{
- return Py_BuildValue("O(OO)l", Py_TYPE(lz),
- lz->func, lz->it, lz->stop);
+ return Py_BuildValue("O(OO)l", Py_TYPE(lz), lz->func, lz->it, lz->stop);
}
static PyObject *
takewhile_reduce_setstate(takewhileobject *lz, PyObject *state)
{
int stop = PyObject_IsTrue(state);
+
if (stop < 0)
return NULL;
lz->stop = stop;
@@ -1345,7 +1351,7 @@ static PyTypeObject takewhile_type = {
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE, /* tp_flags */
takewhile_doc, /* tp_doc */
- (traverseproc)takewhile_traverse, /* tp_traverse */
+ (traverseproc)takewhile_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
@@ -1366,7 +1372,7 @@ static PyTypeObject takewhile_type = {
};
-/* islice object ************************************************************/
+/* islice object *************************************************************/
typedef struct {
PyObject_HEAD
@@ -1402,7 +1408,8 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (PyErr_Occurred())
PyErr_Clear();
PyErr_SetString(PyExc_ValueError,
- "Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.");
+ "Stop argument for islice() must be None or "
+ "an integer: 0 <= x <= sys.maxsize.");
return NULL;
}
}
@@ -1417,14 +1424,16 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (PyErr_Occurred())
PyErr_Clear();
PyErr_SetString(PyExc_ValueError,
- "Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.");
+ "Stop argument for islice() must be None or "
+ "an integer: 0 <= x <= sys.maxsize.");
return NULL;
}
}
}
if (start<0 || stop<-1) {
PyErr_SetString(PyExc_ValueError,
- "Indices for islice() must be None or an integer: 0 <= x <= sys.maxsize.");
+ "Indices for islice() must be None or "
+ "an integer: 0 <= x <= sys.maxsize.");
return NULL;
}
@@ -1521,6 +1530,7 @@ islice_reduce(isliceobject *lz)
* then 'setstate' with the next and count
*/
PyObject *stop;
+
if (lz->it == NULL) {
PyObject *empty_list;
PyObject *empty_it;
@@ -1550,6 +1560,7 @@ static PyObject *
islice_setstate(isliceobject *lz, PyObject *state)
{
Py_ssize_t cnt = PyLong_AsSsize_t(state);
+
if (cnt == -1 && PyErr_Occurred())
return NULL;
lz->cnt = cnt;
@@ -1764,7 +1775,7 @@ static PyTypeObject starmap_type = {
};
-/* chain object ************************************************************/
+/* chain object **************************************************************/
typedef struct {
PyObject_HEAD
@@ -1840,32 +1851,32 @@ chain_next(chainobject *lz)
PyObject *item;
if (lz->source == NULL)
- return NULL; /* already stopped */
+ return NULL; /* already stopped */
if (lz->active == NULL) {
PyObject *iterable = PyIter_Next(lz->source);
if (iterable == NULL) {
Py_CLEAR(lz->source);
- return NULL; /* no more input sources */
+ return NULL; /* no more input sources */
}
lz->active = PyObject_GetIter(iterable);
Py_DECREF(iterable);
if (lz->active == NULL) {
Py_CLEAR(lz->source);
- return NULL; /* input not iterable */
+ return NULL; /* input not iterable */
}
}
- item = PyIter_Next(lz->active);
+ item = (*Py_TYPE(lz->active)->tp_iternext)(lz->active);
if (item != NULL)
return item;
if (PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_StopIteration))
PyErr_Clear();
else
- return NULL; /* input raised an exception */
+ return NULL; /* input raised an exception */
}
Py_CLEAR(lz->active);
- return chain_next(lz); /* recurse and use next active */
+ return chain_next(lz); /* recurse and use next active */
}
static PyObject *
@@ -1891,6 +1902,7 @@ static PyObject *
chain_setstate(chainobject *lz, PyObject *state)
{
PyObject *source, *active=NULL;
+
if (! PyArg_ParseTuple(state, "O|O", &source, &active))
return NULL;
@@ -1915,13 +1927,13 @@ Alternate chain() contructor taking a single iterable argument\n\
that evaluates lazily.");
static PyMethodDef chain_methods[] = {
- {"from_iterable", (PyCFunction) chain_new_from_iterable, METH_O | METH_CLASS,
- chain_from_iterable_doc},
+ {"from_iterable", (PyCFunction) chain_new_from_iterable, METH_O | METH_CLASS,
+ chain_from_iterable_doc},
{"__reduce__", (PyCFunction)chain_reduce, METH_NOARGS,
reduce_doc},
{"__setstate__", (PyCFunction)chain_setstate, METH_O,
setstate_doc},
- {NULL, NULL} /* sentinel */
+ {NULL, NULL} /* sentinel */
};
static PyTypeObject chain_type = {
@@ -1973,10 +1985,10 @@ static PyTypeObject chain_type = {
typedef struct {
PyObject_HEAD
- PyObject *pools; /* tuple of pool tuples */
- Py_ssize_t *indices; /* one index per pool */
- PyObject *result; /* most recently returned result tuple */
- int stopped; /* set to 1 when the product iterator is exhausted */
+ PyObject *pools; /* tuple of pool tuples */
+ Py_ssize_t *indices; /* one index per pool */
+ PyObject *result; /* most recently returned result tuple */
+ int stopped; /* set to 1 when the iterator is exhausted */
} productobject;
static PyTypeObject product_type;
@@ -1995,7 +2007,8 @@ product_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PyObject *tmpargs = PyTuple_New(0);
if (tmpargs == NULL)
return NULL;
- if (!PyArg_ParseTupleAndKeywords(tmpargs, kwds, "|n:product", kwlist, &repeat)) {
+ if (!PyArg_ParseTupleAndKeywords(tmpargs, kwds, "|n:product",
+ kwlist, &repeat)) {
Py_DECREF(tmpargs);
return NULL;
}
@@ -2284,7 +2297,7 @@ product((0,1), (0,1), (0,1)) --> (0,0,0) (0,0,1) (0,1,0) (0,1,1) (1,0,0) ...");
static PyTypeObject product_type = {
PyVarObject_HEAD_INIT(NULL, 0)
"itertools.product", /* tp_name */
- sizeof(productobject), /* tp_basicsize */
+ sizeof(productobject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)product_dealloc, /* tp_dealloc */
@@ -2326,15 +2339,15 @@ static PyTypeObject product_type = {
};
-/* combinations object ************************************************************/
+/* combinations object *******************************************************/
typedef struct {
PyObject_HEAD
- PyObject *pool; /* input converted to a tuple */
- Py_ssize_t *indices; /* one index per result element */
- PyObject *result; /* most recently returned result tuple */
- Py_ssize_t r; /* size of result tuple */
- int stopped; /* set to 1 when the combinations iterator is exhausted */
+ PyObject *pool; /* input converted to a tuple */
+ Py_ssize_t *indices; /* one index per result element */
+ PyObject *result; /* most recently returned result tuple */
+ Py_ssize_t r; /* size of result tuple */
+ int stopped; /* set to 1 when the iterator is exhausted */
} combinationsobject;
static PyTypeObject combinations_type;
@@ -2544,17 +2557,16 @@ combinations_setstate(combinationsobject *lz, PyObject *state)
Py_ssize_t i;
Py_ssize_t n = PyTuple_GET_SIZE(lz->pool);
- if (!PyTuple_Check(state) || PyTuple_GET_SIZE(state) != lz->r)
- {
+ if (!PyTuple_Check(state) || PyTuple_GET_SIZE(state) != lz->r) {
PyErr_SetString(PyExc_ValueError, "invalid arguments");
return NULL;
}
- for (i=0; i<lz->r; i++)
- {
+ for (i=0; i<lz->r; i++) {
Py_ssize_t max;
PyObject* indexObject = PyTuple_GET_ITEM(state, i);
Py_ssize_t index = PyLong_AsSsize_t(indexObject);
+
if (index == -1 && PyErr_Occurred())
return NULL; /* not an integer */
max = i + n - lz->r;
@@ -2640,7 +2652,7 @@ static PyTypeObject combinations_type = {
};
-/* combinations with replacement object *******************************************/
+/* combinations with replacement object **************************************/
/* Equivalent to:
@@ -2670,11 +2682,11 @@ static PyTypeObject combinations_type = {
*/
typedef struct {
PyObject_HEAD
- PyObject *pool; /* input converted to a tuple */
+ PyObject *pool; /* input converted to a tuple */
Py_ssize_t *indices; /* one index per result element */
PyObject *result; /* most recently returned result tuple */
- Py_ssize_t r; /* size of result tuple */
- int stopped; /* set to 1 when the cwr iterator is exhausted */
+ Py_ssize_t r; /* size of result tuple */
+ int stopped; /* set to 1 when the cwr iterator is exhausted */
} cwrobject;
static PyTypeObject cwr_type;
@@ -2691,8 +2703,9 @@ cwr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_ssize_t i;
static char *kwargs[] = {"iterable", "r", NULL};
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:combinations_with_replacement", kwargs,
- &iterable, &r))
+ if (!PyArg_ParseTupleAndKeywords(args, kwds,
+ "On:combinations_with_replacement",
+ kwargs, &iterable, &r))
return NULL;
pool = PySequence_Tuple(iterable);
@@ -2857,8 +2870,7 @@ cwr_reduce(cwrobject *lz)
indices = PyTuple_New(lz->r);
if (!indices)
return NULL;
- for (i=0; i<lz->r; i++)
- {
+ for (i=0; i<lz->r; i++) {
PyObject* index = PyLong_FromSsize_t(lz->indices[i]);
if (!index) {
Py_DECREF(indices);
@@ -2884,10 +2896,10 @@ cwr_setstate(cwrobject *lz, PyObject *state)
}
n = PyTuple_GET_SIZE(lz->pool);
- for (i=0; i<lz->r; i++)
- {
+ for (i=0; i<lz->r; i++) {
PyObject* indexObject = PyTuple_GET_ITEM(state, i);
Py_ssize_t index = PyLong_AsSsize_t(indexObject);
+
if (index < 0 && PyErr_Occurred())
return NULL; /* not an integer */
/* clamp the index */
@@ -2971,7 +2983,7 @@ static PyTypeObject cwr_type = {
};
-/* permutations object ************************************************************
+/* permutations object ********************************************************
def permutations(iterable, r=None):
'permutations(range(3), 2) --> (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)'
@@ -2998,12 +3010,12 @@ def permutations(iterable, r=None):
typedef struct {
PyObject_HEAD
- PyObject *pool; /* input converted to a tuple */
- Py_ssize_t *indices; /* one index per element in the pool */
- Py_ssize_t *cycles; /* one rollover counter per element in the result */
- PyObject *result; /* most recently returned result tuple */
- Py_ssize_t r; /* size of result tuple */
- int stopped; /* set to 1 when the permutations iterator is exhausted */
+ PyObject *pool; /* input converted to a tuple */
+ Py_ssize_t *indices; /* one index per element in the pool */
+ Py_ssize_t *cycles; /* one rollover counter per element in the result */
+ PyObject *result; /* most recently returned result tuple */
+ Py_ssize_t r; /* size of result tuple */
+ int stopped; /* set to 1 when the iterator is exhausted */
} permutationsobject;
static PyTypeObject permutations_type;
@@ -3218,7 +3230,7 @@ permutations_reduce(permutationsobject *po)
indices = PyTuple_New(n);
if (indices == NULL)
goto err;
- for (i=0; i<n; i++){
+ for (i=0; i<n; i++) {
PyObject* index = PyLong_FromSsize_t(po->indices[i]);
if (!index)
goto err;
@@ -3228,8 +3240,7 @@ permutations_reduce(permutationsobject *po)
cycles = PyTuple_New(po->r);
if (cycles == NULL)
goto err;
- for (i=0; i<po->r; i++)
- {
+ for (i=0 ; i<po->r ; i++) {
PyObject* index = PyLong_FromSsize_t(po->cycles[i]);
if (!index)
goto err;
@@ -3257,15 +3268,12 @@ permutations_setstate(permutationsobject *po, PyObject *state)
return NULL;
n = PyTuple_GET_SIZE(po->pool);
- if (PyTuple_GET_SIZE(indices) != n ||
- PyTuple_GET_SIZE(cycles) != po->r)
- {
+ if (PyTuple_GET_SIZE(indices) != n || PyTuple_GET_SIZE(cycles) != po->r) {
PyErr_SetString(PyExc_ValueError, "invalid arguments");
return NULL;
}
- for (i=0; i<n; i++)
- {
+ for (i=0; i<n; i++) {
PyObject* indexObject = PyTuple_GET_ITEM(indices, i);
Py_ssize_t index = PyLong_AsSsize_t(indexObject);
if (index < 0 && PyErr_Occurred())
@@ -3278,8 +3286,7 @@ permutations_setstate(permutationsobject *po, PyObject *state)
po->indices[i] = index;
}
- for (i=0; i<po->r; i++)
- {
+ for (i=0; i<po->r; i++) {
PyObject* indexObject = PyTuple_GET_ITEM(cycles, i);
Py_ssize_t index = PyLong_AsSsize_t(indexObject);
if (index < 0 && PyErr_Occurred())
@@ -3320,11 +3327,11 @@ permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)");
static PyTypeObject permutations_type = {
PyVarObject_HEAD_INIT(NULL, 0)
- "itertools.permutations", /* tp_name */
+ "itertools.permutations", /* tp_name */
sizeof(permutationsobject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
- (destructor)permutations_dealloc, /* tp_dealloc */
+ (destructor)permutations_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
@@ -3341,13 +3348,13 @@ static PyTypeObject permutations_type = {
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE, /* tp_flags */
- permutations_doc, /* tp_doc */
- (traverseproc)permutations_traverse, /* tp_traverse */
+ permutations_doc, /* tp_doc */
+ (traverseproc)permutations_traverse,/* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
- (iternextfunc)permutations_next, /* tp_iternext */
+ (iternextfunc)permutations_next, /* tp_iternext */
permuations_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
@@ -3358,11 +3365,11 @@ static PyTypeObject permutations_type = {
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
- permutations_new, /* tp_new */
+ permutations_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
};
-/* accumulate object ************************************************************/
+/* accumulate object ********************************************************/
typedef struct {
PyObject_HEAD
@@ -3429,9 +3436,9 @@ accumulate_traverse(accumulateobject *lz, visitproc visit, void *arg)
static PyObject *
accumulate_next(accumulateobject *lz)
{
- PyObject *val, *oldtotal, *newtotal;
+ PyObject *val, *newtotal;
- val = PyIter_Next(lz->it);
+ val = (*Py_TYPE(lz->it)->tp_iternext)(lz->it);
if (val == NULL)
return NULL;
@@ -3449,11 +3456,8 @@ accumulate_next(accumulateobject *lz)
if (newtotal == NULL)
return NULL;
- oldtotal = lz->total;
- lz->total = newtotal;
- Py_DECREF(oldtotal);
-
Py_INCREF(newtotal);
+ Py_SETREF(lz->total, newtotal);
return newtotal;
}
@@ -3480,7 +3484,7 @@ accumulate_reduce(accumulateobject *lz)
return Py_BuildValue("O(OO)O", Py_TYPE(lz),
lz->it, lz->binop?lz->binop:Py_None,
lz->total?lz->total:Py_None);
- }
+}
static PyObject *
accumulate_setstate(accumulateobject *lz, PyObject *state)
@@ -3642,7 +3646,7 @@ compress_next(compressobject *lz)
ok = PyObject_IsTrue(selector);
Py_DECREF(selector);
- if (ok == 1)
+ if (ok > 0)
return datum;
Py_DECREF(datum);
if (ok < 0)
@@ -3655,7 +3659,7 @@ compress_reduce(compressobject *lz)
{
return Py_BuildValue("O(OO)", Py_TYPE(lz),
lz->data, lz->selectors);
- }
+}
static PyMethodDef compress_methods[] = {
{"__reduce__", (PyCFunction)compress_reduce, METH_NOARGS,
@@ -3674,44 +3678,44 @@ static PyTypeObject compress_type = {
PyVarObject_HEAD_INIT(NULL, 0)
"itertools.compress", /* tp_name */
sizeof(compressobject), /* tp_basicsize */
- 0, /* tp_itemsize */
+ 0, /* tp_itemsize */
/* methods */
(destructor)compress_dealloc, /* tp_dealloc */
- 0, /* tp_print */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_reserved */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
+ 0, /* tp_print */
+ 0, /* tp_getattr */
+ 0, /* tp_setattr */
+ 0, /* tp_reserved */
+ 0, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_sequence */
+ 0, /* tp_as_mapping */
+ 0, /* tp_hash */
+ 0, /* tp_call */
+ 0, /* tp_str */
+ PyObject_GenericGetAttr, /* tp_getattro */
+ 0, /* tp_setattro */
+ 0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- compress_doc, /* tp_doc */
- (traverseproc)compress_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
+ compress_doc, /* tp_doc */
+ (traverseproc)compress_traverse, /* tp_traverse */
+ 0, /* tp_clear */
+ 0, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ PyObject_SelfIter, /* tp_iter */
(iternextfunc)compress_next, /* tp_iternext */
- compress_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 */
- 0, /* tp_init */
- 0, /* tp_alloc */
- compress_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
+ compress_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 */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ compress_new, /* tp_new */
+ PyObject_GC_Del, /* tp_free */
};
@@ -3792,8 +3796,7 @@ filterfalse_next(filterfalseobject *lz)
ok = PyObject_IsTrue(item);
} else {
PyObject *good;
- good = PyObject_CallFunctionObjArgs(lz->func,
- item, NULL);
+ good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
if (good == NULL) {
Py_DECREF(item);
return NULL;
@@ -3812,9 +3815,8 @@ filterfalse_next(filterfalseobject *lz)
static PyObject *
filterfalse_reduce(filterfalseobject *lz)
{
- return Py_BuildValue("O(OO)", Py_TYPE(lz),
- lz->func, lz->it);
- }
+ return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->func, lz->it);
+}
static PyMethodDef filterfalse_methods[] = {
{"__reduce__", (PyCFunction)filterfalse_reduce, METH_NOARGS,
@@ -3834,7 +3836,7 @@ static PyTypeObject filterfalse_type = {
sizeof(filterfalseobject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
- (destructor)filterfalse_dealloc, /* tp_dealloc */
+ (destructor)filterfalse_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
@@ -3852,7 +3854,7 @@ static PyTypeObject filterfalse_type = {
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE, /* tp_flags */
filterfalse_doc, /* tp_doc */
- (traverseproc)filterfalse_traverse, /* tp_traverse */
+ (traverseproc)filterfalse_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
@@ -4091,15 +4093,15 @@ static PyTypeObject count_type = {
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
count_doc, /* tp_doc */
- (traverseproc)count_traverse, /* tp_traverse */
+ (traverseproc)count_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)count_next, /* tp_iternext */
- count_methods, /* tp_methods */
+ count_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
@@ -4265,9 +4267,7 @@ static PyTypeObject repeat_type = {
PyObject_GC_Del, /* tp_free */
};
-/* ziplongest object ************************************************************/
-
-#include "Python.h"
+/* ziplongest object *********************************************************/
typedef struct {
PyObject_HEAD
@@ -4306,7 +4306,7 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
ittuple = PyTuple_New(tuplesize);
if (ittuple == NULL)
return NULL;
- for (i=0; i < tuplesize; ++i) {
+ for (i=0; i < tuplesize; i++) {
PyObject *item = PyTuple_GET_ITEM(args, i);
PyObject *it = PyObject_GetIter(item);
if (it == NULL) {
@@ -4447,6 +4447,7 @@ zip_longest_reduce(ziplongestobject *lz)
*/
int i;
PyObject *args = PyTuple_New(PyTuple_GET_SIZE(lz->ittuple));
+
if (args == NULL)
return NULL;
for (i=0; i<PyTuple_GET_SIZE(lz->ittuple); i++) {
@@ -4497,7 +4498,7 @@ static PyTypeObject ziplongest_type = {
sizeof(ziplongestobject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
- (destructor)zip_longest_dealloc, /* tp_dealloc */
+ (destructor)zip_longest_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
@@ -4514,8 +4515,8 @@ static PyTypeObject ziplongest_type = {
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE, /* tp_flags */
- zip_longest_doc, /* tp_doc */
- (traverseproc)zip_longest_traverse, /* tp_traverse */
+ zip_longest_doc, /* tp_doc */
+ (traverseproc)zip_longest_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
@@ -4531,7 +4532,7 @@ static PyTypeObject ziplongest_type = {
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
- zip_longest_new, /* tp_new */
+ zip_longest_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
};
diff --git a/Modules/main.c b/Modules/main.c
index e4c955e011..b6dcdd0a30 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -42,11 +42,11 @@ static int orig_argc;
#define PROGRAM_OPTS BASE_OPTS
/* Short usage message (with %s for argv0) */
-static char *usage_line =
+static const char usage_line[] =
"usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
/* Long usage message, split into parts < 512 bytes */
-static char *usage_1 = "\
+static const char usage_1[] = "\
Options and arguments (and corresponding environment variables):\n\
-b : issue warnings about str(bytes_instance), str(bytearray_instance)\n\
and comparing bytes/bytearray with str. (-bb: issue errors)\n\
@@ -56,7 +56,7 @@ Options and arguments (and corresponding environment variables):\n\
-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
-h : print this help message and exit (also --help)\n\
";
-static char *usage_2 = "\
+static const char usage_2[] = "\
-i : inspect interactively after running script; forces a prompt even\n\
if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
-I : isolate Python from the user's environment (implies -E and -s)\n\
@@ -67,7 +67,7 @@ static char *usage_2 = "\
-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
-S : don't imply 'import site' on initialization\n\
";
-static char *usage_3 = "\
+static const char usage_3[] = "\
-u : unbuffered binary stdout and stderr, stdin always buffered;\n\
also PYTHONUNBUFFERED=x\n\
see man page for details on internal buffering relating to '-u'\n\
@@ -79,7 +79,7 @@ static char *usage_3 = "\
-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
-X opt : set implementation-specific option\n\
";
-static char *usage_4 = "\
+static const char usage_4[] = "\
file : program read from script file\n\
- : program read from stdin (default; interactive mode if a tty)\n\
arg ...: arguments passed to program in sys.argv[1:]\n\n\
@@ -88,22 +88,23 @@ PYTHONSTARTUP: file executed on interactive startup (no default)\n\
PYTHONPATH : '%lc'-separated list of directories prefixed to the\n\
default module search path. The result is sys.path.\n\
";
-static char *usage_5 =
+static const char usage_5[] =
"PYTHONHOME : alternate <prefix> directory (or <prefix>%lc<exec_prefix>).\n"
" The default module search path uses %s.\n"
"PYTHONCASEOK : ignore case in 'import' statements (Windows).\n"
"PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n"
-"PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.\n\
-";
-static char *usage_6 = "\
-PYTHONHASHSEED: if this variable is set to 'random', a random value is used\n\
- to seed the hashes of str, bytes and datetime objects. It can also be\n\
- set to an integer in the range [0,4294967295] to get hash values with a\n\
- predictable seed.\n\
-";
+"PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.\n";
+static const char usage_6[] =
+"PYTHONHASHSEED: if this variable is set to 'random', a random value is used\n"
+" to seed the hashes of str, bytes and datetime objects. It can also be\n"
+" set to an integer in the range [0,4294967295] to get hash values with a\n"
+" predictable seed.\n"
+"PYTHONMALLOC: set the Python memory allocators and/or install debug hooks\n"
+" on Python memory allocators. Use PYTHONMALLOC=debug to install debug\n"
+" hooks.\n";
static int
-usage(int exitcode, wchar_t* program)
+usage(int exitcode, const wchar_t* program)
{
FILE *f = exitcode ? stderr : stdout;
@@ -341,6 +342,7 @@ Py_Main(int argc, wchar_t **argv)
int help = 0;
int version = 0;
int saw_unbuffered_flag = 0;
+ char *opt;
PyCompilerFlags cf;
PyObject *warning_option = NULL;
PyObject *warning_options = NULL;
@@ -365,6 +367,13 @@ Py_Main(int argc, wchar_t **argv)
}
}
+ opt = Py_GETENV("PYTHONMALLOC");
+ if (_PyMem_SetupAllocators(opt) < 0) {
+ fprintf(stderr,
+ "Error in PYTHONMALLOC: unknown allocator \"%s\"!\n", opt);
+ exit(1);
+ }
+
Py_HashRandomizationFlag = 1;
_PyRandom_Init();
@@ -654,7 +663,7 @@ Py_Main(int argc, wchar_t **argv)
Py_SetProgramName(wbuf);
/* Don't free wbuf, the argument to Py_SetProgramName
- * must remain valid until the Py_Finalize is called.
+ * must remain valid until Py_FinalizeEx is called.
*/
} else {
Py_SetProgramName(argv[0]);
@@ -785,7 +794,11 @@ Py_Main(int argc, wchar_t **argv)
sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
}
- Py_Finalize();
+ if (Py_FinalizeEx() < 0) {
+ /* Value unlikely to be confused with a non-error exit status or
+ other special meaning */
+ sts = 120;
+ }
#ifdef __INSURE__
/* Insure++ is a memory analysis tool that aids in discovering
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index 7ebf8e84c2..cf049010ea 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -875,7 +875,7 @@ math_1_to_int(PyObject *arg, double (*func) (double), int can_overflow)
}
static PyObject *
-math_2(PyObject *args, double (*func) (double, double), char *funcname)
+math_2(PyObject *args, double (*func) (double, double), const char *funcname)
{
PyObject *ox, *oy;
double x, y, r;
@@ -1672,7 +1672,7 @@ PyDoc_STRVAR(math_modf_doc,
in that int is larger than PY_SSIZE_T_MAX. */
static PyObject*
-loghelper(PyObject* arg, double (*func)(double), char *funcname)
+loghelper(PyObject* arg, double (*func)(double), const char *funcname)
{
/* If it is int, do it ourselves. */
if (PyLong_Check(arg)) {
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index bb98a99427..50cec2498d 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -389,6 +389,7 @@ mmap_write_method(mmap_object *self,
PyObject *args)
{
Py_buffer data;
+ PyObject *result;
CHECK_VALID(NULL);
if (!PyArg_ParseTuple(args, "y*:write", &data))
@@ -406,9 +407,9 @@ mmap_write_method(mmap_object *self,
}
memcpy(self->data + self->pos, data.buf, data.len);
self->pos = self->pos + data.len;
+ result = PyLong_FromSsize_t(data.len);
PyBuffer_Release(&data);
- Py_INCREF(Py_None);
- return Py_None;
+ return result;
}
static PyObject *
diff --git a/Modules/nismodule.c b/Modules/nismodule.c
index 64eb5dbc3d..b6a855c577 100644
--- a/Modules/nismodule.c
+++ b/Modules/nismodule.c
@@ -76,11 +76,7 @@ nis_mapname (char *map, int *pfix)
*pfix = 0;
for (i=0; aliases[i].alias != 0L; i++) {
- if (!strcmp (aliases[i].alias, map)) {
- *pfix = aliases[i].fix;
- return aliases[i].map;
- }
- if (!strcmp (aliases[i].map, map)) {
+ if (!strcmp (aliases[i].alias, map) || !strcmp (aliases[i].map, map)) {
*pfix = aliases[i].fix;
return aliases[i].map;
}
diff --git a/Modules/ossaudiodev.c b/Modules/ossaudiodev.c
index d2fd5c81d1..2b7d71f64f 100644
--- a/Modules/ossaudiodev.c
+++ b/Modules/ossaudiodev.c
@@ -31,7 +31,11 @@
#endif
#include <sys/ioctl.h>
+#ifdef __ANDROID__
+#include <linux/soundcard.h>
+#else
#include <sys/soundcard.h>
+#endif
#if defined(linux)
diff --git a/Modules/parsermodule.c b/Modules/parsermodule.c
index 6471b8ee99..82770a52ff 100644
--- a/Modules/parsermodule.c
+++ b/Modules/parsermodule.c
@@ -53,7 +53,7 @@ extern grammar _PyParser_Grammar; /* From graminit.c */
/* String constants used to initialize module attributes.
*
*/
-static char parser_copyright_string[] =
+static const char parser_copyright_string[] =
"Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
Virginia, USA. Portions copyright 1991-1995 by Stichting Mathematisch\n\
@@ -63,7 +63,7 @@ Centrum, Amsterdam, The Netherlands.";
PyDoc_STRVAR(parser_doc_string,
"This is an interface to Python's internal parser.");
-static char parser_version_string[] = "0.5";
+static const char parser_version_string[] = "0.5";
typedef PyObject* (*SeqMaker) (Py_ssize_t length);
@@ -578,13 +578,13 @@ parser_issuite(PyST_Object *self, PyObject *args, PyObject *kw)
}
-/* err_string(char* message)
+/* err_string(const char* message)
*
* Sets the error string for an exception of type ParserError.
*
*/
static void
-err_string(char *message)
+err_string(const char *message)
{
PyErr_SetString(parser_error, message);
}
@@ -597,7 +597,7 @@ err_string(char *message)
*
*/
static PyObject*
-parser_do_parse(PyObject *args, PyObject *kw, char *argspec, int type)
+parser_do_parse(PyObject *args, PyObject *kw, const char *argspec, int type)
{
char* string = 0;
PyObject* res = 0;
@@ -670,9 +670,75 @@ parser_suite(PyST_Object *self, PyObject *args, PyObject *kw)
static node* build_node_tree(PyObject *tuple);
-static int validate_expr_tree(node *tree);
-static int validate_file_input(node *tree);
-static int validate_encoding_decl(node *tree);
+
+static int
+validate_node(node *tree)
+{
+ int type = TYPE(tree);
+ int nch = NCH(tree);
+ dfa *nt_dfa;
+ state *dfa_state;
+ int pos, arc;
+
+ assert(ISNONTERMINAL(type));
+ type -= NT_OFFSET;
+ if (type >= _PyParser_Grammar.g_ndfas) {
+ PyErr_Format(parser_error, "Unrecognized node type %d.", TYPE(tree));
+ return 0;
+ }
+ nt_dfa = &_PyParser_Grammar.g_dfa[type];
+ REQ(tree, nt_dfa->d_type);
+
+ /* Run the DFA for this nonterminal. */
+ dfa_state = &nt_dfa->d_state[nt_dfa->d_initial];
+ for (pos = 0; pos < nch; ++pos) {
+ node *ch = CHILD(tree, pos);
+ int ch_type = TYPE(ch);
+ for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
+ short a_label = dfa_state->s_arc[arc].a_lbl;
+ assert(a_label < _PyParser_Grammar.g_ll.ll_nlabels);
+ if (_PyParser_Grammar.g_ll.ll_label[a_label].lb_type == ch_type) {
+ /* The child is acceptable; if non-terminal, validate it recursively. */
+ if (ISNONTERMINAL(ch_type) && !validate_node(ch))
+ return 0;
+
+ /* Update the state, and move on to the next child. */
+ dfa_state = &nt_dfa->d_state[dfa_state->s_arc[arc].a_arrow];
+ goto arc_found;
+ }
+ }
+ /* What would this state have accepted? */
+ {
+ short a_label = dfa_state->s_arc->a_lbl;
+ int next_type;
+ if (!a_label) /* Wouldn't accept any more children */
+ goto illegal_num_children;
+
+ next_type = _PyParser_Grammar.g_ll.ll_label[a_label].lb_type;
+ if (ISNONTERMINAL(next_type))
+ PyErr_Format(parser_error, "Expected node type %d, got %d.",
+ next_type, ch_type);
+ else
+ PyErr_Format(parser_error, "Illegal terminal: expected %s.",
+ _PyParser_TokenNames[next_type]);
+ return 0;
+ }
+
+arc_found:
+ continue;
+ }
+ /* Are we in a final state? If so, return 1 for successful validation. */
+ for (arc = 0; arc < dfa_state->s_narcs; ++arc) {
+ if (!dfa_state->s_arc[arc].a_lbl) {
+ return 1;
+ }
+ }
+
+illegal_num_children:
+ PyErr_Format(parser_error,
+ "Illegal number of children for %s node.", nt_dfa->d_name);
+ return 0;
+}
/* PyObject* parser_tuple2st(PyObject* self, PyObject* args)
*
@@ -681,7 +747,7 @@ static int validate_encoding_decl(node *tree);
* tuple can be validated. It does this by checking the first code of the
* tuple, and, if acceptable, builds the internal representation. If this
* step succeeds, the internal representation is validated as fully as
- * possible with the various validate_*() routines defined below.
+ * possible with the recursive validate_node() routine defined above.
*
* This function must be changed if support is to be added for PyST_FRAGMENT
* ST objects.
@@ -710,33 +776,35 @@ parser_tuple2st(PyST_Object *self, PyObject *args, PyObject *kw)
*/
tree = build_node_tree(tuple);
if (tree != 0) {
- int start_sym = TYPE(tree);
- if (start_sym == eval_input) {
+ node *validation_root = tree;
+ int tree_type = 0;
+ switch (TYPE(tree)) {
+ case eval_input:
/* Might be an eval form. */
- if (validate_expr_tree(tree))
- st = parser_newstobject(tree, PyST_EXPR);
- else
- PyNode_Free(tree);
- }
- else if (start_sym == file_input) {
- /* This looks like an exec form so far. */
- if (validate_file_input(tree))
- st = parser_newstobject(tree, PyST_SUITE);
- else
- PyNode_Free(tree);
- }
- else if (start_sym == encoding_decl) {
+ tree_type = PyST_EXPR;
+ break;
+ case encoding_decl:
/* This looks like an encoding_decl so far. */
- if (validate_encoding_decl(tree))
- st = parser_newstobject(tree, PyST_SUITE);
- else
- PyNode_Free(tree);
- }
- else {
+ if (NCH(tree) != 1)
+ err_string("Error Parsing encoding_decl");
+ validation_root = CHILD(tree, 0);
+ /* Fall through */
+ case file_input:
+ /* This looks like an exec form so far. */
+
+ tree_type = PyST_SUITE;
+ break;
+ default:
/* This is a fragment, at best. */
PyNode_Free(tree);
err_string("parse tree does not use a valid start symbol");
+ return (0);
}
+
+ if (validate_node(validation_root))
+ st = parser_newstobject(tree, tree_type);
+ else
+ PyNode_Free(tree);
}
/* Make sure we raise an exception on all errors. We should never
* get this, but we'd do well to be sure something is done.
@@ -981,2433 +1049,6 @@ build_node_tree(PyObject *tuple)
}
-/*
- * Validation routines used within the validation section:
- */
-static int validate_terminal(node *terminal, int type, char *string);
-
-#define validate_ampersand(ch) validate_terminal(ch, AMPER, "&")
-#define validate_circumflex(ch) validate_terminal(ch, CIRCUMFLEX, "^")
-#define validate_colon(ch) validate_terminal(ch, COLON, ":")
-#define validate_comma(ch) validate_terminal(ch, COMMA, ",")
-#define validate_dedent(ch) validate_terminal(ch, DEDENT, "")
-#define validate_equal(ch) validate_terminal(ch, EQUAL, "=")
-#define validate_indent(ch) validate_terminal(ch, INDENT, (char*)NULL)
-#define validate_lparen(ch) validate_terminal(ch, LPAR, "(")
-#define validate_newline(ch) validate_terminal(ch, NEWLINE, (char*)NULL)
-#define validate_rparen(ch) validate_terminal(ch, RPAR, ")")
-#define validate_semi(ch) validate_terminal(ch, SEMI, ";")
-#define validate_star(ch) validate_terminal(ch, STAR, "*")
-#define validate_vbar(ch) validate_terminal(ch, VBAR, "|")
-#define validate_doublestar(ch) validate_terminal(ch, DOUBLESTAR, "**")
-#define validate_dot(ch) validate_terminal(ch, DOT, ".")
-#define validate_at(ch) validate_terminal(ch, AT, "@")
-#define validate_rarrow(ch) validate_terminal(ch, RARROW, "->")
-#define validate_name(ch, str) validate_terminal(ch, NAME, str)
-
-#define VALIDATER(n) static int validate_##n(node *tree)
-
-VALIDATER(node); VALIDATER(small_stmt);
-VALIDATER(class); VALIDATER(node);
-VALIDATER(parameters); VALIDATER(suite);
-VALIDATER(testlist); VALIDATER(varargslist);
-VALIDATER(vfpdef);
-VALIDATER(stmt); VALIDATER(simple_stmt);
-VALIDATER(expr_stmt); VALIDATER(power);
-VALIDATER(del_stmt);
-VALIDATER(return_stmt); VALIDATER(raise_stmt);
-VALIDATER(import_stmt); VALIDATER(import_stmt);
-VALIDATER(import_name); VALIDATER(yield_stmt);
-VALIDATER(global_stmt); VALIDATER(nonlocal_stmt);
-VALIDATER(assert_stmt);
-VALIDATER(compound_stmt); VALIDATER(test_or_star_expr);
-VALIDATER(while); VALIDATER(for);
-VALIDATER(try); VALIDATER(except_clause);
-VALIDATER(test); VALIDATER(and_test);
-VALIDATER(not_test); VALIDATER(comparison);
-VALIDATER(comp_op);
-VALIDATER(star_expr); VALIDATER(expr);
-VALIDATER(xor_expr); VALIDATER(and_expr);
-VALIDATER(shift_expr); VALIDATER(arith_expr);
-VALIDATER(term); VALIDATER(factor);
-VALIDATER(atom); VALIDATER(lambdef);
-VALIDATER(trailer); VALIDATER(subscript);
-VALIDATER(subscriptlist); VALIDATER(sliceop);
-VALIDATER(exprlist); VALIDATER(dictorsetmaker);
-VALIDATER(arglist); VALIDATER(argument);
-VALIDATER(comp_for);
-VALIDATER(comp_iter); VALIDATER(comp_if);
-VALIDATER(testlist_comp); VALIDATER(yield_expr);
-VALIDATER(or_test);
-VALIDATER(test_nocond); VALIDATER(lambdef_nocond);
-VALIDATER(yield_arg);
-VALIDATER(async_funcdef); VALIDATER(async_stmt);
-VALIDATER(atom_expr);
-
-#undef VALIDATER
-
-#define is_even(n) (((n) & 1) == 0)
-#define is_odd(n) (((n) & 1) == 1)
-
-
-static int
-validate_ntype(node *n, int t)
-{
- if (TYPE(n) != t) {
- PyErr_Format(parser_error, "Expected node type %d, got %d.",
- t, TYPE(n));
- return 0;
- }
- return 1;
-}
-
-
-/* Verifies that the number of child nodes is exactly 'num', raising
- * an exception if it isn't. The exception message does not indicate
- * the exact number of nodes, allowing this to be used to raise the
- * "right" exception when the wrong number of nodes is present in a
- * specific variant of a statement's syntax. This is commonly used
- * in that fashion.
- */
-static int
-validate_numnodes(node *n, int num, const char *const name)
-{
- if (NCH(n) != num) {
- PyErr_Format(parser_error,
- "Illegal number of children for %s node.", name);
- return 0;
- }
- return 1;
-}
-
-
-static int
-validate_terminal(node *terminal, int type, char *string)
-{
- int res = (validate_ntype(terminal, type)
- && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
-
- if (!res && !PyErr_Occurred()) {
- PyErr_Format(parser_error,
- "Illegal terminal: expected \"%s\"", string);
- }
- return (res);
-}
-
-/* X (',' X) [','] */
-static int
-validate_repeating_list_variable(node *tree,
- int list_node_type,
- int (*validate_child_func_inc)(node *, int *),
- int *pos,
- const char *const list_node_type_name)
-{
- int nch = NCH(tree);
- int res = (nch && validate_ntype(tree, list_node_type));
-
- if (!res && !PyErr_Occurred()) {
- /* Unconditionally raise. */
- (void) validate_numnodes(tree, 1, list_node_type_name);
- }
- else {
- for ( ; res && *pos < nch; ) {
- res = validate_child_func_inc(tree, pos);
- if (!res || *pos >= nch)
- break;
- res = validate_comma(CHILD(tree, (*pos)++));
- }
- }
- return res;
-}
-
-/* X (',' X) [','] */
-static int
-validate_repeating_list(node *tree,
- int list_node_type,
- int (*validate_child_func)(node *),
- const char *const list_node_type_name)
-{
- int nch = NCH(tree);
- int res = (nch && validate_ntype(tree, list_node_type));
- int pos = 0;
-
- if (!res && !PyErr_Occurred()) {
- /* Unconditionally raise. */
- (void) validate_numnodes(tree, 1, list_node_type_name);
- }
- else {
- for ( ; res && pos < nch; ) {
- res = validate_child_func(CHILD(tree, pos++));
- if (!res || pos >= nch)
- break;
- res = validate_comma(CHILD(tree, pos++));
- }
- }
- return res;
-}
-
-
-/* validate_class()
- *
- * classdef:
- * 'class' NAME ['(' testlist ')'] ':' suite
- */
-static int
-validate_class(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, classdef) &&
- ((nch == 4) || (nch == 6) || (nch == 7)));
-
- if (res) {
- res = (validate_name(CHILD(tree, 0), "class")
- && validate_ntype(CHILD(tree, 1), NAME)
- && validate_colon(CHILD(tree, nch - 2))
- && validate_suite(CHILD(tree, nch - 1)));
- }
- else {
- (void) validate_numnodes(tree, 4, "class");
- }
-
- if (res) {
- if (nch == 7) {
- res = ((validate_lparen(CHILD(tree, 2)) &&
- validate_arglist(CHILD(tree, 3)) &&
- validate_rparen(CHILD(tree, 4))));
- }
- else if (nch == 6) {
- res = (validate_lparen(CHILD(tree,2)) &&
- validate_rparen(CHILD(tree,3)));
- }
- }
- return (res);
-}
-
-
-/* if_stmt:
- * 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
- */
-static int
-validate_if(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, if_stmt)
- && (nch >= 4)
- && validate_name(CHILD(tree, 0), "if")
- && validate_test(CHILD(tree, 1))
- && validate_colon(CHILD(tree, 2))
- && validate_suite(CHILD(tree, 3)));
-
- if (res && ((nch % 4) == 3)) {
- /* ... 'else' ':' suite */
- res = (validate_name(CHILD(tree, nch - 3), "else")
- && validate_colon(CHILD(tree, nch - 2))
- && validate_suite(CHILD(tree, nch - 1)));
- nch -= 3;
- }
- else if (!res && !PyErr_Occurred())
- (void) validate_numnodes(tree, 4, "if");
- if ((nch % 4) != 0)
- /* Will catch the case for nch < 4 */
- res = validate_numnodes(tree, 0, "if");
- else if (res && (nch > 4)) {
- /* ... ('elif' test ':' suite)+ ... */
- int j = 4;
- while ((j < nch) && res) {
- res = (validate_name(CHILD(tree, j), "elif")
- && validate_colon(CHILD(tree, j + 2))
- && validate_test(CHILD(tree, j + 1))
- && validate_suite(CHILD(tree, j + 3)));
- j += 4;
- }
- }
- return (res);
-}
-
-
-/* parameters:
- * '(' [varargslist] ')'
- *
- */
-static int
-validate_parameters(node *tree)
-{
- int nch = NCH(tree);
- int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
-
- if (res) {
- res = (validate_lparen(CHILD(tree, 0))
- && validate_rparen(CHILD(tree, nch - 1)));
- if (res && (nch == 3))
- res = validate_varargslist(CHILD(tree, 1));
- }
- else {
- (void) validate_numnodes(tree, 2, "parameters");
- }
- return (res);
-}
-
-
-/* validate_suite()
- *
- * suite:
- * simple_stmt
- * | NEWLINE INDENT stmt+ DEDENT
- */
-static int
-validate_suite(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
-
- if (res && (nch == 1))
- res = validate_simple_stmt(CHILD(tree, 0));
- else if (res) {
- /* NEWLINE INDENT stmt+ DEDENT */
- res = (validate_newline(CHILD(tree, 0))
- && validate_indent(CHILD(tree, 1))
- && validate_stmt(CHILD(tree, 2))
- && validate_dedent(CHILD(tree, nch - 1)));
-
- if (res && (nch > 4)) {
- int i = 3;
- --nch; /* forget the DEDENT */
- for ( ; res && (i < nch); ++i)
- res = validate_stmt(CHILD(tree, i));
- }
- else if (nch < 4)
- res = validate_numnodes(tree, 4, "suite");
- }
- return (res);
-}
-
-
-static int
-validate_testlist(node *tree)
-{
- return (validate_repeating_list(tree, testlist,
- validate_test, "testlist"));
-}
-
-static int
-validate_testlist_star_expr(node *tl)
-{
- return (validate_repeating_list(tl, testlist_star_expr, validate_test_or_star_expr,
- "testlist"));
-}
-
-
-/* validate either vfpdef or tfpdef.
- * vfpdef: NAME
- * tfpdef: NAME [':' test]
- */
-static int
-validate_vfpdef(node *tree)
-{
- int nch = NCH(tree);
- if (TYPE(tree) == vfpdef) {
- return nch == 1 && validate_name(CHILD(tree, 0), NULL);
- }
- else if (TYPE(tree) == tfpdef) {
- if (nch == 1) {
- return validate_name(CHILD(tree, 0), NULL);
- }
- else if (nch == 3) {
- return validate_name(CHILD(tree, 0), NULL) &&
- validate_colon(CHILD(tree, 1)) &&
- validate_test(CHILD(tree, 2));
- }
- }
- return 0;
-}
-
-/* '*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef
- * ..or tfpdef in place of vfpdef. vfpdef: NAME; tfpdef: NAME [':' test]
- */
-static int
-validate_varargslist_trailer(node *tree, int start)
-{
- int nch = NCH(tree);
- int res = 0;
-
- if (nch <= start) {
- err_string("expected variable argument trailer for varargslist");
- return 0;
- }
- if (TYPE(CHILD(tree, start)) == STAR) {
- /*
- * '*' [vfpdef]
- */
- res = validate_star(CHILD(tree, start++));
- if (res && start < nch && (TYPE(CHILD(tree, start)) == vfpdef ||
- TYPE(CHILD(tree, start)) == tfpdef))
- res = validate_vfpdef(CHILD(tree, start++));
- /*
- * (',' vfpdef ['=' test])*
- */
- while (res && start + 1 < nch && (
- TYPE(CHILD(tree, start + 1)) == vfpdef ||
- TYPE(CHILD(tree, start + 1)) == tfpdef)) {
- res = (validate_comma(CHILD(tree, start++))
- && validate_vfpdef(CHILD(tree, start++)));
- if (res && start + 1 < nch && TYPE(CHILD(tree, start)) == EQUAL)
- res = (validate_equal(CHILD(tree, start++))
- && validate_test(CHILD(tree, start++)));
- }
- /*
- * [',' '**' vfpdef]
- */
- if (res && start + 2 < nch && TYPE(CHILD(tree, start+1)) == DOUBLESTAR)
- res = (validate_comma(CHILD(tree, start++))
- && validate_doublestar(CHILD(tree, start++))
- && validate_vfpdef(CHILD(tree, start++)));
- }
- else if (TYPE(CHILD(tree, start)) == DOUBLESTAR) {
- /*
- * '**' vfpdef
- */
- if (start + 1 < nch)
- res = (validate_doublestar(CHILD(tree, start++))
- && validate_vfpdef(CHILD(tree, start++)));
- else {
- res = 0;
- err_string("expected vfpdef after ** in varargslist trailer");
- }
- }
- else {
- res = 0;
- err_string("expected * or ** in varargslist trailer");
- }
-
- if (res && start != nch) {
- res = 0;
- err_string("unexpected extra children in varargslist trailer");
- }
- return res;
-}
-
-
-/* validate_varargslist()
- *
- * Validate typedargslist or varargslist.
- *
- * typedargslist: ((tfpdef ['=' test] ',')*
- * ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
- * '**' tfpdef)
- * | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
- * tfpdef: NAME [':' test]
- * varargslist: ((vfpdef ['=' test] ',')*
- * ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
- * '**' vfpdef)
- * | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
- * vfpdef: NAME
- *
- */
-static int
-validate_varargslist(node *tree)
-{
- int nch = NCH(tree);
- int res = (TYPE(tree) == varargslist ||
- TYPE(tree) == typedargslist) &&
- (nch != 0);
- int sym;
- node *ch;
- int i = 0;
-
- if (!res)
- return 0;
- if (nch < 1) {
- err_string("varargslist missing child nodes");
- return 0;
- }
- while (i < nch) {
- ch = CHILD(tree, i);
- sym = TYPE(ch);
- if (sym == vfpdef || sym == tfpdef) {
- /* validate (vfpdef ['=' test] ',')+ */
- res = validate_vfpdef(ch);
- ++i;
- if (res && (i+2 <= nch) && TYPE(CHILD(tree, i)) == EQUAL) {
- res = (validate_equal(CHILD(tree, i))
- && validate_test(CHILD(tree, i+1)));
- if (res)
- i += 2;
- }
- if (res && i < nch) {
- res = validate_comma(CHILD(tree, i));
- ++i;
- }
- } else if (sym == DOUBLESTAR || sym == STAR) {
- res = validate_varargslist_trailer(tree, i);
- break;
- } else {
- res = 0;
- err_string("illegal formation for varargslist");
- }
- }
- return res;
-}
-
-
-/* comp_iter: comp_for | comp_if
- */
-static int
-validate_comp_iter(node *tree)
-{
- int res = (validate_ntype(tree, comp_iter)
- && validate_numnodes(tree, 1, "comp_iter"));
- if (res && TYPE(CHILD(tree, 0)) == comp_for)
- res = validate_comp_for(CHILD(tree, 0));
- else
- res = validate_comp_if(CHILD(tree, 0));
-
- return res;
-}
-
-/* comp_for: 'for' exprlist 'in' test [comp_iter]
- */
-static int
-validate_comp_for(node *tree)
-{
- int nch = NCH(tree);
- int res;
-
- if (nch == 5)
- res = validate_comp_iter(CHILD(tree, 4));
- else
- res = validate_numnodes(tree, 4, "comp_for");
-
- if (res)
- res = (validate_name(CHILD(tree, 0), "for")
- && validate_exprlist(CHILD(tree, 1))
- && validate_name(CHILD(tree, 2), "in")
- && validate_or_test(CHILD(tree, 3)));
-
- return res;
-}
-
-/* comp_if: 'if' test_nocond [comp_iter]
- */
-static int
-validate_comp_if(node *tree)
-{
- int nch = NCH(tree);
- int res;
-
- if (nch == 3)
- res = validate_comp_iter(CHILD(tree, 2));
- else
- res = validate_numnodes(tree, 2, "comp_if");
-
- if (res)
- res = (validate_name(CHILD(tree, 0), "if")
- && validate_test_nocond(CHILD(tree, 1)));
-
- return res;
-}
-
-
-/* simple_stmt | compound_stmt
- *
- */
-static int
-validate_stmt(node *tree)
-{
- int res = (validate_ntype(tree, stmt)
- && validate_numnodes(tree, 1, "stmt"));
-
- if (res) {
- tree = CHILD(tree, 0);
-
- if (TYPE(tree) == simple_stmt)
- res = validate_simple_stmt(tree);
- else
- res = validate_compound_stmt(tree);
- }
- return (res);
-}
-
-
-/* small_stmt (';' small_stmt)* [';'] NEWLINE
- *
- */
-static int
-validate_simple_stmt(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, simple_stmt)
- && (nch >= 2)
- && validate_small_stmt(CHILD(tree, 0))
- && validate_newline(CHILD(tree, nch - 1)));
-
- if (nch < 2)
- res = validate_numnodes(tree, 2, "simple_stmt");
- --nch; /* forget the NEWLINE */
- if (res && is_even(nch))
- res = validate_semi(CHILD(tree, --nch));
- if (res && (nch > 2)) {
- int i;
-
- for (i = 1; res && (i < nch); i += 2)
- res = (validate_semi(CHILD(tree, i))
- && validate_small_stmt(CHILD(tree, i + 1)));
- }
- return (res);
-}
-
-
-static int
-validate_small_stmt(node *tree)
-{
- int nch = NCH(tree);
- int res = validate_numnodes(tree, 1, "small_stmt");
-
- if (res) {
- int ntype = TYPE(CHILD(tree, 0));
-
- if ( (ntype == expr_stmt)
- || (ntype == del_stmt)
- || (ntype == pass_stmt)
- || (ntype == flow_stmt)
- || (ntype == import_stmt)
- || (ntype == global_stmt)
- || (ntype == nonlocal_stmt)
- || (ntype == assert_stmt))
- res = validate_node(CHILD(tree, 0));
- else {
- res = 0;
- err_string("illegal small_stmt child type");
- }
- }
- else if (nch == 1) {
- res = 0;
- PyErr_Format(parser_error,
- "Unrecognized child node of small_stmt: %d.",
- TYPE(CHILD(tree, 0)));
- }
- return (res);
-}
-
-
-/* compound_stmt:
- * if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated
- */
-static int
-validate_compound_stmt(node *tree)
-{
- int res = (validate_ntype(tree, compound_stmt)
- && validate_numnodes(tree, 1, "compound_stmt"));
- int ntype;
-
- if (!res)
- return (0);
-
- tree = CHILD(tree, 0);
- ntype = TYPE(tree);
- if ( (ntype == if_stmt)
- || (ntype == while_stmt)
- || (ntype == for_stmt)
- || (ntype == try_stmt)
- || (ntype == with_stmt)
- || (ntype == funcdef)
- || (ntype == async_stmt)
- || (ntype == classdef)
- || (ntype == decorated))
- res = validate_node(tree);
- else {
- res = 0;
- PyErr_Format(parser_error,
- "Illegal compound statement type: %d.", TYPE(tree));
- }
- return (res);
-}
-
-static int
-validate_yield_or_testlist(node *tree, int tse)
-{
- if (TYPE(tree) == yield_expr) {
- return validate_yield_expr(tree);
- }
- else {
- if (tse)
- return validate_testlist_star_expr(tree);
- else
- return validate_testlist(tree);
- }
-}
-
-static int
-validate_expr_stmt(node *tree)
-{
- int j;
- int nch = NCH(tree);
- int res = (validate_ntype(tree, expr_stmt)
- && is_odd(nch)
- && validate_testlist_star_expr(CHILD(tree, 0)));
-
- if (res && nch == 3
- && TYPE(CHILD(tree, 1)) == augassign) {
- res = validate_numnodes(CHILD(tree, 1), 1, "augassign")
- && validate_yield_or_testlist(CHILD(tree, 2), 0);
-
- if (res) {
- char *s = STR(CHILD(CHILD(tree, 1), 0));
-
- res = (strcmp(s, "+=") == 0
- || strcmp(s, "-=") == 0
- || strcmp(s, "*=") == 0
- || strcmp(s, "/=") == 0
- || strcmp(s, "//=") == 0
- || strcmp(s, "%=") == 0
- || strcmp(s, "&=") == 0
- || strcmp(s, "|=") == 0
- || strcmp(s, "^=") == 0
- || strcmp(s, "<<=") == 0
- || strcmp(s, ">>=") == 0
- || strcmp(s, "**=") == 0);
- if (!res)
- err_string("illegal augmented assignment operator");
- }
- }
- else {
- for (j = 1; res && (j < nch); j += 2)
- res = validate_equal(CHILD(tree, j))
- && validate_yield_or_testlist(CHILD(tree, j + 1), 1);
- }
- return (res);
-}
-
-
-static int
-validate_del_stmt(node *tree)
-{
- return (validate_numnodes(tree, 2, "del_stmt")
- && validate_name(CHILD(tree, 0), "del")
- && validate_exprlist(CHILD(tree, 1)));
-}
-
-
-static int
-validate_return_stmt(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, return_stmt)
- && ((nch == 1) || (nch == 2))
- && validate_name(CHILD(tree, 0), "return"));
-
- if (res && (nch == 2))
- res = validate_testlist(CHILD(tree, 1));
-
- return (res);
-}
-
-
-/*
- * raise_stmt:
- *
- * 'raise' [test ['from' test]]
- */
-static int
-validate_raise_stmt(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, raise_stmt)
- && ((nch == 1) || (nch == 2) || (nch == 4)));
-
- if (!res && !PyErr_Occurred())
- (void) validate_numnodes(tree, 2, "raise");
-
- if (res) {
- res = validate_name(CHILD(tree, 0), "raise");
- if (res && (nch >= 2))
- res = validate_test(CHILD(tree, 1));
- if (res && (nch == 4)) {
- res = (validate_name(CHILD(tree, 2), "from")
- && validate_test(CHILD(tree, 3)));
- }
- }
- return (res);
-}
-
-
-/* yield_expr: 'yield' [yield_arg]
- */
-static int
-validate_yield_expr(node *tree)
-{
- int nch = NCH(tree);
- if (nch < 1 || nch > 2)
- return 0;
- if (!validate_ntype(tree, yield_expr))
- return 0;
- if (!validate_name(CHILD(tree, 0), "yield"))
- return 0;
- if (nch == 2) {
- if (!validate_yield_arg(CHILD(tree, 1)))
- return 0;
- }
- return 1;
-}
-
-/* yield_arg: 'from' test | testlist
- */
-static int
-validate_yield_arg(node *tree)
-{
- int nch = NCH(tree);
- if (!validate_ntype(tree, yield_arg))
- return 0;
- switch (nch) {
- case 1:
- if (!validate_testlist(CHILD(tree, nch - 1)))
- return 0;
- break;
- case 2:
- if (!validate_name(CHILD(tree, 0), "from"))
- return 0;
- if (!validate_test(CHILD(tree, 1)))
- return 0;
- break;
- default:
- return 0;
- }
- return 1;
-}
-
-/* yield_stmt: yield_expr
- */
-static int
-validate_yield_stmt(node *tree)
-{
- return (validate_ntype(tree, yield_stmt)
- && validate_numnodes(tree, 1, "yield_stmt")
- && validate_yield_expr(CHILD(tree, 0)));
-}
-
-
-static int
-validate_import_as_name(node *tree)
-{
- int nch = NCH(tree);
- int ok = validate_ntype(tree, import_as_name);
-
- if (ok) {
- if (nch == 1)
- ok = validate_name(CHILD(tree, 0), NULL);
- else if (nch == 3)
- ok = (validate_name(CHILD(tree, 0), NULL)
- && validate_name(CHILD(tree, 1), "as")
- && validate_name(CHILD(tree, 2), NULL));
- else
- ok = validate_numnodes(tree, 3, "import_as_name");
- }
- return ok;
-}
-
-
-/* dotted_name: NAME ("." NAME)*
- */
-static int
-validate_dotted_name(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, dotted_name)
- && is_odd(nch)
- && validate_name(CHILD(tree, 0), NULL));
- int i;
-
- for (i = 1; res && (i < nch); i += 2) {
- res = (validate_dot(CHILD(tree, i))
- && validate_name(CHILD(tree, i+1), NULL));
- }
- return res;
-}
-
-
-/* dotted_as_name: dotted_name [NAME NAME]
- */
-static int
-validate_dotted_as_name(node *tree)
-{
- int nch = NCH(tree);
- int res = validate_ntype(tree, dotted_as_name);
-
- if (res) {
- if (nch == 1)
- res = validate_dotted_name(CHILD(tree, 0));
- else if (nch == 3)
- res = (validate_dotted_name(CHILD(tree, 0))
- && validate_name(CHILD(tree, 1), "as")
- && validate_name(CHILD(tree, 2), NULL));
- else {
- res = 0;
- err_string("illegal number of children for dotted_as_name");
- }
- }
- return res;
-}
-
-
-/* dotted_as_name (',' dotted_as_name)* */
-static int
-validate_dotted_as_names(node *tree)
-{
- int nch = NCH(tree);
- int res = is_odd(nch) && validate_dotted_as_name(CHILD(tree, 0));
- int i;
-
- for (i = 1; res && (i < nch); i += 2)
- res = (validate_comma(CHILD(tree, i))
- && validate_dotted_as_name(CHILD(tree, i + 1)));
- return (res);
-}
-
-
-/* import_as_name (',' import_as_name)* [','] */
-static int
-validate_import_as_names(node *tree)
-{
- int nch = NCH(tree);
- int res = validate_import_as_name(CHILD(tree, 0));
- int i;
-
- for (i = 1; res && (i + 1 < nch); i += 2)
- res = (validate_comma(CHILD(tree, i))
- && validate_import_as_name(CHILD(tree, i + 1)));
- return (res);
-}
-
-
-/* 'import' dotted_as_names */
-static int
-validate_import_name(node *tree)
-{
- return (validate_ntype(tree, import_name)
- && validate_numnodes(tree, 2, "import_name")
- && validate_name(CHILD(tree, 0), "import")
- && validate_dotted_as_names(CHILD(tree, 1)));
-}
-
-/* Helper function to count the number of leading dots (or ellipsis tokens) in
- * 'from ...module import name'
- */
-static int
-count_from_dots(node *tree)
-{
- int i;
- for (i = 1; i < NCH(tree); i++)
- if (TYPE(CHILD(tree, i)) != DOT && TYPE(CHILD(tree, i)) != ELLIPSIS)
- break;
- return i - 1;
-}
-
-/* import_from: ('from' ('.'* dotted_name | '.'+)
- * 'import' ('*' | '(' import_as_names ')' | import_as_names))
- */
-static int
-validate_import_from(node *tree)
-{
- int nch = NCH(tree);
- int ndots = count_from_dots(tree);
- int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
- int offset = ndots + havename;
- int res = validate_ntype(tree, import_from)
- && (offset >= 1)
- && (nch >= 3 + offset)
- && validate_name(CHILD(tree, 0), "from")
- && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
- && validate_name(CHILD(tree, offset + 1), "import");
-
- if (res && TYPE(CHILD(tree, offset + 2)) == LPAR)
- res = ((nch == offset + 5)
- && validate_lparen(CHILD(tree, offset + 2))
- && validate_import_as_names(CHILD(tree, offset + 3))
- && validate_rparen(CHILD(tree, offset + 4)));
- else if (res && TYPE(CHILD(tree, offset + 2)) != STAR)
- res = validate_import_as_names(CHILD(tree, offset + 2));
- return (res);
-}
-
-
-/* import_stmt: import_name | import_from */
-static int
-validate_import_stmt(node *tree)
-{
- int nch = NCH(tree);
- int res = validate_numnodes(tree, 1, "import_stmt");
-
- if (res) {
- int ntype = TYPE(CHILD(tree, 0));
-
- if (ntype == import_name || ntype == import_from)
- res = validate_node(CHILD(tree, 0));
- else {
- res = 0;
- err_string("illegal import_stmt child type");
- }
- }
- else if (nch == 1) {
- res = 0;
- PyErr_Format(parser_error,
- "Unrecognized child node of import_stmt: %d.",
- TYPE(CHILD(tree, 0)));
- }
- return (res);
-}
-
-
-/* global_stmt:
- *
- * 'global' NAME (',' NAME)*
- */
-static int
-validate_global_stmt(node *tree)
-{
- int j;
- int nch = NCH(tree);
- int res = (validate_ntype(tree, global_stmt)
- && is_even(nch) && (nch >= 2));
-
- if (!res && !PyErr_Occurred())
- err_string("illegal global statement");
-
- if (res)
- res = (validate_name(CHILD(tree, 0), "global")
- && validate_ntype(CHILD(tree, 1), NAME));
- for (j = 2; res && (j < nch); j += 2)
- res = (validate_comma(CHILD(tree, j))
- && validate_ntype(CHILD(tree, j + 1), NAME));
-
- return (res);
-}
-
-/* nonlocal_stmt:
- *
- * 'nonlocal' NAME (',' NAME)*
- */
-static int
-validate_nonlocal_stmt(node *tree)
-{
- int j;
- int nch = NCH(tree);
- int res = (validate_ntype(tree, nonlocal_stmt)
- && is_even(nch) && (nch >= 2));
-
- if (!res && !PyErr_Occurred())
- err_string("illegal nonlocal statement");
-
- if (res)
- res = (validate_name(CHILD(tree, 0), "nonlocal")
- && validate_ntype(CHILD(tree, 1), NAME));
- for (j = 2; res && (j < nch); j += 2)
- res = (validate_comma(CHILD(tree, j))
- && validate_ntype(CHILD(tree, j + 1), NAME));
-
- return res;
-}
-
-/* assert_stmt:
- *
- * 'assert' test [',' test]
- */
-static int
-validate_assert_stmt(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, assert_stmt)
- && ((nch == 2) || (nch == 4))
- && (validate_name(CHILD(tree, 0), "assert"))
- && validate_test(CHILD(tree, 1)));
-
- if (!res && !PyErr_Occurred())
- err_string("illegal assert statement");
- if (res && (nch > 2))
- res = (validate_comma(CHILD(tree, 2))
- && validate_test(CHILD(tree, 3)));
-
- return (res);
-}
-
-
-static int
-validate_while(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, while_stmt)
- && ((nch == 4) || (nch == 7))
- && validate_name(CHILD(tree, 0), "while")
- && validate_test(CHILD(tree, 1))
- && validate_colon(CHILD(tree, 2))
- && validate_suite(CHILD(tree, 3)));
-
- if (res && (nch == 7))
- res = (validate_name(CHILD(tree, 4), "else")
- && validate_colon(CHILD(tree, 5))
- && validate_suite(CHILD(tree, 6)));
-
- return (res);
-}
-
-
-static int
-validate_for(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, for_stmt)
- && ((nch == 6) || (nch == 9))
- && validate_name(CHILD(tree, 0), "for")
- && validate_exprlist(CHILD(tree, 1))
- && validate_name(CHILD(tree, 2), "in")
- && validate_testlist(CHILD(tree, 3))
- && validate_colon(CHILD(tree, 4))
- && validate_suite(CHILD(tree, 5)));
-
- if (res && (nch == 9))
- res = (validate_name(CHILD(tree, 6), "else")
- && validate_colon(CHILD(tree, 7))
- && validate_suite(CHILD(tree, 8)));
-
- return (res);
-}
-
-
-/* try_stmt:
- * 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
- ['finally' ':' suite]
- * | 'try' ':' suite 'finally' ':' suite
- *
- */
-static int
-validate_try(node *tree)
-{
- int nch = NCH(tree);
- int pos = 3;
- int res = (validate_ntype(tree, try_stmt)
- && (nch >= 6) && ((nch % 3) == 0));
-
- if (res)
- res = (validate_name(CHILD(tree, 0), "try")
- && validate_colon(CHILD(tree, 1))
- && validate_suite(CHILD(tree, 2))
- && validate_colon(CHILD(tree, nch - 2))
- && validate_suite(CHILD(tree, nch - 1)));
- else if (!PyErr_Occurred()) {
- const char* name = "except";
- if (TYPE(CHILD(tree, nch - 3)) != except_clause)
- name = STR(CHILD(tree, nch - 3));
-
- PyErr_Format(parser_error,
- "Illegal number of children for try/%s node.", name);
- }
- /* Handle try/finally statement */
- if (res && (TYPE(CHILD(tree, pos)) == NAME) &&
- (strcmp(STR(CHILD(tree, pos)), "finally") == 0)) {
- res = (validate_numnodes(tree, 6, "try/finally")
- && validate_colon(CHILD(tree, 4))
- && validate_suite(CHILD(tree, 5)));
- return (res);
- }
- /* try/except statement: skip past except_clause sections */
- while (res && pos < nch && (TYPE(CHILD(tree, pos)) == except_clause)) {
- res = (validate_except_clause(CHILD(tree, pos))
- && validate_colon(CHILD(tree, pos + 1))
- && validate_suite(CHILD(tree, pos + 2)));
- pos += 3;
- }
- /* skip else clause */
- if (res && pos < nch && (TYPE(CHILD(tree, pos)) == NAME) &&
- (strcmp(STR(CHILD(tree, pos)), "else") == 0)) {
- res = (validate_colon(CHILD(tree, pos + 1))
- && validate_suite(CHILD(tree, pos + 2)));
- pos += 3;
- }
- if (res && pos < nch) {
- /* last clause must be a finally */
- res = (validate_name(CHILD(tree, pos), "finally")
- && validate_numnodes(tree, pos + 3, "try/except/finally")
- && validate_colon(CHILD(tree, pos + 1))
- && validate_suite(CHILD(tree, pos + 2)));
- }
- return (res);
-}
-
-
-static int
-validate_except_clause(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, except_clause)
- && ((nch == 1) || (nch == 2) || (nch == 4))
- && validate_name(CHILD(tree, 0), "except"));
-
- if (res && (nch > 1))
- res = validate_test(CHILD(tree, 1));
- if (res && (nch == 4))
- res = (validate_name(CHILD(tree, 2), "as")
- && validate_ntype(CHILD(tree, 3), NAME));
-
- return (res);
-}
-
-
-static int
-validate_test(node *tree)
-{
- int nch = NCH(tree);
- int res = validate_ntype(tree, test) && is_odd(nch);
-
- if (res && (TYPE(CHILD(tree, 0)) == lambdef))
- res = ((nch == 1)
- && validate_lambdef(CHILD(tree, 0)));
- else if (res) {
- res = validate_or_test(CHILD(tree, 0));
- res = (res && (nch == 1 || (nch == 5 &&
- validate_name(CHILD(tree, 1), "if") &&
- validate_or_test(CHILD(tree, 2)) &&
- validate_name(CHILD(tree, 3), "else") &&
- validate_test(CHILD(tree, 4)))));
- }
- return (res);
-}
-
-static int
-validate_test_nocond(node *tree)
-{
- int nch = NCH(tree);
- int res = validate_ntype(tree, test_nocond) && (nch == 1);
-
- if (res && (TYPE(CHILD(tree, 0)) == lambdef_nocond))
- res = (validate_lambdef_nocond(CHILD(tree, 0)));
- else if (res) {
- res = (validate_or_test(CHILD(tree, 0)));
- }
- return (res);
-}
-
-static int
-validate_or_test(node *tree)
-{
- int nch = NCH(tree);
- int res = validate_ntype(tree, or_test) && is_odd(nch);
-
- if (res) {
- int pos;
- res = validate_and_test(CHILD(tree, 0));
- for (pos = 1; res && (pos < nch); pos += 2)
- res = (validate_name(CHILD(tree, pos), "or")
- && validate_and_test(CHILD(tree, pos + 1)));
- }
- return (res);
-}
-
-
-static int
-validate_and_test(node *tree)
-{
- int pos;
- int nch = NCH(tree);
- int res = (validate_ntype(tree, and_test)
- && is_odd(nch)
- && validate_not_test(CHILD(tree, 0)));
-
- for (pos = 1; res && (pos < nch); pos += 2)
- res = (validate_name(CHILD(tree, pos), "and")
- && validate_not_test(CHILD(tree, 0)));
-
- return (res);
-}
-
-
-static int
-validate_not_test(node *tree)
-{
- int nch = NCH(tree);
- int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
-
- if (res) {
- if (nch == 2)
- res = (validate_name(CHILD(tree, 0), "not")
- && validate_not_test(CHILD(tree, 1)));
- else if (nch == 1)
- res = validate_comparison(CHILD(tree, 0));
- }
- return (res);
-}
-
-
-static int
-validate_comparison(node *tree)
-{
- int pos;
- int nch = NCH(tree);
- int res = (validate_ntype(tree, comparison)
- && is_odd(nch)
- && validate_expr(CHILD(tree, 0)));
-
- for (pos = 1; res && (pos < nch); pos += 2)
- res = (validate_comp_op(CHILD(tree, pos))
- && validate_expr(CHILD(tree, pos + 1)));
-
- return (res);
-}
-
-
-static int
-validate_comp_op(node *tree)
-{
- int res = 0;
- int nch = NCH(tree);
-
- if (!validate_ntype(tree, comp_op))
- return (0);
- if (nch == 1) {
- /*
- * Only child will be a terminal with a well-defined symbolic name
- * or a NAME with a string of either 'is' or 'in'
- */
- tree = CHILD(tree, 0);
- switch (TYPE(tree)) {
- case LESS:
- case GREATER:
- case EQEQUAL:
- case EQUAL:
- case LESSEQUAL:
- case GREATEREQUAL:
- case NOTEQUAL:
- res = 1;
- break;
- case NAME:
- res = ((strcmp(STR(tree), "in") == 0)
- || (strcmp(STR(tree), "is") == 0));
- if (!res) {
- PyErr_Format(parser_error,
- "illegal operator '%s'", STR(tree));
- }
- break;
- default:
- err_string("illegal comparison operator type");
- break;
- }
- }
- else if ((res = validate_numnodes(tree, 2, "comp_op")) != 0) {
- res = (validate_ntype(CHILD(tree, 0), NAME)
- && validate_ntype(CHILD(tree, 1), NAME)
- && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
- && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
- || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
- && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
- if (!res && !PyErr_Occurred())
- err_string("unknown comparison operator");
- }
- return (res);
-}
-
-
-static int
-validate_star_expr(node *tree)
-{
- int res = validate_ntype(tree, star_expr);
- if (!res) return res;
- if (!validate_numnodes(tree, 2, "star_expr"))
- return 0;
- return validate_ntype(CHILD(tree, 0), STAR) && \
- validate_expr(CHILD(tree, 1));
-}
-
-
-static int
-validate_expr(node *tree)
-{
- int j;
- int nch = NCH(tree);
- int res = (validate_ntype(tree, expr)
- && is_odd(nch)
- && validate_xor_expr(CHILD(tree, 0)));
-
- for (j = 2; res && (j < nch); j += 2)
- res = (validate_xor_expr(CHILD(tree, j))
- && validate_vbar(CHILD(tree, j - 1)));
-
- return (res);
-}
-
-
-static int
-validate_xor_expr(node *tree)
-{
- int j;
- int nch = NCH(tree);
- int res = (validate_ntype(tree, xor_expr)
- && is_odd(nch)
- && validate_and_expr(CHILD(tree, 0)));
-
- for (j = 2; res && (j < nch); j += 2)
- res = (validate_circumflex(CHILD(tree, j - 1))
- && validate_and_expr(CHILD(tree, j)));
-
- return (res);
-}
-
-
-static int
-validate_and_expr(node *tree)
-{
- int pos;
- int nch = NCH(tree);
- int res = (validate_ntype(tree, and_expr)
- && is_odd(nch)
- && validate_shift_expr(CHILD(tree, 0)));
-
- for (pos = 1; res && (pos < nch); pos += 2)
- res = (validate_ampersand(CHILD(tree, pos))
- && validate_shift_expr(CHILD(tree, pos + 1)));
-
- return (res);
-}
-
-
-static int
-validate_chain_two_ops(node *tree, int (*termvalid)(node *), int op1, int op2)
- {
- int pos = 1;
- int nch = NCH(tree);
- int res = (is_odd(nch)
- && (*termvalid)(CHILD(tree, 0)));
-
- for ( ; res && (pos < nch); pos += 2) {
- if (TYPE(CHILD(tree, pos)) != op1)
- res = validate_ntype(CHILD(tree, pos), op2);
- if (res)
- res = (*termvalid)(CHILD(tree, pos + 1));
- }
- return (res);
-}
-
-
-static int
-validate_shift_expr(node *tree)
-{
- return (validate_ntype(tree, shift_expr)
- && validate_chain_two_ops(tree, validate_arith_expr,
- LEFTSHIFT, RIGHTSHIFT));
-}
-
-
-static int
-validate_arith_expr(node *tree)
-{
- return (validate_ntype(tree, arith_expr)
- && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
-}
-
-
-static int
-validate_term(node *tree)
-{
- int pos = 1;
- int nch = NCH(tree);
- int res = (validate_ntype(tree, term)
- && is_odd(nch)
- && validate_factor(CHILD(tree, 0)));
-
- for ( ; res && (pos < nch); pos += 2)
- res = (((TYPE(CHILD(tree, pos)) == STAR)
- || (TYPE(CHILD(tree, pos)) == SLASH)
- || (TYPE(CHILD(tree, pos)) == DOUBLESLASH)
- || (TYPE(CHILD(tree, pos)) == PERCENT))
- && validate_factor(CHILD(tree, pos + 1)));
-
- return (res);
-}
-
-
-/* factor:
- *
- * factor: ('+'|'-'|'~') factor | power
- */
-static int
-validate_factor(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, factor)
- && (((nch == 2)
- && ((TYPE(CHILD(tree, 0)) == PLUS)
- || (TYPE(CHILD(tree, 0)) == MINUS)
- || (TYPE(CHILD(tree, 0)) == TILDE))
- && validate_factor(CHILD(tree, 1)))
- || ((nch == 1)
- && validate_power(CHILD(tree, 0)))));
- return (res);
-}
-
-
-/* power:
- *
- * power: atom_expr trailer* ['**' factor]
- */
-static int
-validate_power(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, power) && (nch >= 1)
- && validate_atom_expr(CHILD(tree, 0)));
-
- if (nch > 1) {
- if (nch != 3) {
- err_string("illegal number of nodes for 'power'");
- return (0);
- }
- res = (validate_doublestar(CHILD(tree, 1))
- && validate_factor(CHILD(tree, 2)));
- }
-
- return (res);
-}
-
-
-/* atom_expr:
- *
- * atom_expr: [AWAIT] atom trailer*
- */
-static int
-validate_atom_expr(node *tree)
-{
- int start = 0;
- int nch = NCH(tree);
- int res;
- int pos;
-
- res = validate_ntype(tree, atom_expr) && (nch >= 1);
- if (!res) {
- return (res);
- }
-
- if (TYPE(CHILD(tree, 0)) == AWAIT) {
- start = 1;
- if (nch < 2) {
- err_string("illegal number of nodes for 'atom_expr'");
- return (0);
- }
- }
-
- res = validate_atom(CHILD(tree, start));
- if (res) {
- pos = start + 1;
- while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
- res = validate_trailer(CHILD(tree, pos++));
- }
-
- return (res);
-}
-
-
-static int
-validate_atom(node *tree)
-{
- int pos;
- int nch = NCH(tree);
- int res = validate_ntype(tree, atom);
-
- if (res && nch < 1)
- res = validate_numnodes(tree, nch+1, "atom");
- if (res) {
- switch (TYPE(CHILD(tree, 0))) {
- case LPAR:
- res = ((nch <= 3)
- && (validate_rparen(CHILD(tree, nch - 1))));
-
- if (res && (nch == 3)) {
- if (TYPE(CHILD(tree, 1))==yield_expr)
- res = validate_yield_expr(CHILD(tree, 1));
- else
- res = validate_testlist_comp(CHILD(tree, 1));
- }
- break;
- case LSQB:
- if (nch == 2)
- res = validate_ntype(CHILD(tree, 1), RSQB);
- else if (nch == 3)
- res = (validate_testlist_comp(CHILD(tree, 1))
- && validate_ntype(CHILD(tree, 2), RSQB));
- else {
- res = 0;
- err_string("illegal list display atom");
- }
- break;
- case LBRACE:
- res = ((nch <= 3)
- && validate_ntype(CHILD(tree, nch - 1), RBRACE));
-
- if (res && (nch == 3))
- res = validate_dictorsetmaker(CHILD(tree, 1));
- break;
- case NAME:
- case NUMBER:
- case ELLIPSIS:
- res = (nch == 1);
- break;
- case STRING:
- for (pos = 1; res && (pos < nch); ++pos)
- res = validate_ntype(CHILD(tree, pos), STRING);
- break;
- default:
- res = 0;
- break;
- }
- }
- return (res);
-}
-
-
-/* testlist_comp:
- * (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
- */
-static int
-validate_testlist_comp(node *tree)
-{
- int nch = NCH(tree);
- int ok;
-
- if (nch == 0) {
- err_string("missing child nodes of testlist_comp");
- return 0;
- }
-
- if (nch == 2 && TYPE(CHILD(tree, 1)) == comp_for) {
- ok = (validate_test(CHILD(tree, 0))
- && validate_comp_for(CHILD(tree, 1)));
- }
- else {
- ok = validate_repeating_list(tree,
- testlist_comp,
- validate_test_or_star_expr,
- "testlist_comp");
- }
- return ok;
-}
-
-/* decorator:
- * '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
- */
-static int
-validate_decorator(node *tree)
-{
- int ok;
- int nch = NCH(tree);
- ok = (validate_ntype(tree, decorator) &&
- (nch == 3 || nch == 5 || nch == 6) &&
- validate_at(CHILD(tree, 0)) &&
- validate_dotted_name(CHILD(tree, 1)) &&
- validate_newline(RCHILD(tree, -1)));
-
- if (ok && nch != 3) {
- ok = (validate_lparen(CHILD(tree, 2)) &&
- validate_rparen(RCHILD(tree, -2)));
-
- if (ok && nch == 6)
- ok = validate_arglist(CHILD(tree, 3));
- }
-
- return ok;
-}
-
-/* decorators:
- * decorator+
- */
-static int
-validate_decorators(node *tree)
-{
- int i, nch, ok;
- nch = NCH(tree);
- ok = validate_ntype(tree, decorators) && nch >= 1;
-
- for (i = 0; ok && i < nch; ++i)
- ok = validate_decorator(CHILD(tree, i));
-
- return ok;
-}
-
-/* with_item:
- * test ['as' expr]
- */
-static int
-validate_with_item(node *tree)
-{
- int nch = NCH(tree);
- int ok = (validate_ntype(tree, with_item)
- && (nch == 1 || nch == 3)
- && validate_test(CHILD(tree, 0)));
- if (ok && nch == 3)
- ok = (validate_name(CHILD(tree, 1), "as")
- && validate_expr(CHILD(tree, 2)));
- return ok;
-}
-
-/* with_stmt:
- * 0 1 ... -2 -1
- * 'with' with_item (',' with_item)* ':' suite
- */
-static int
-validate_with_stmt(node *tree)
-{
- int i;
- int nch = NCH(tree);
- int ok = (validate_ntype(tree, with_stmt)
- && (nch % 2 == 0)
- && validate_name(CHILD(tree, 0), "with")
- && validate_colon(RCHILD(tree, -2))
- && validate_suite(RCHILD(tree, -1)));
- for (i = 1; ok && i < nch - 2; i += 2)
- ok = validate_with_item(CHILD(tree, i));
- return ok;
-}
-
-/* funcdef: 'def' NAME parameters ['->' test] ':' suite */
-
-static int
-validate_funcdef(node *tree)
-{
- int nch = NCH(tree);
- int res = validate_ntype(tree, funcdef);
- if (res) {
- if (nch == 5) {
- res = (validate_name(CHILD(tree, 0), "def")
- && validate_ntype(CHILD(tree, 1), NAME)
- && validate_parameters(CHILD(tree, 2))
- && validate_colon(CHILD(tree, 3))
- && validate_suite(CHILD(tree, 4)));
- }
- else if (nch == 7) {
- res = (validate_name(CHILD(tree, 0), "def")
- && validate_ntype(CHILD(tree, 1), NAME)
- && validate_parameters(CHILD(tree, 2))
- && validate_rarrow(CHILD(tree, 3))
- && validate_test(CHILD(tree, 4))
- && validate_colon(CHILD(tree, 5))
- && validate_suite(CHILD(tree, 6)));
- }
- else {
- res = 0;
- err_string("illegal number of children for funcdef");
- }
- }
- return res;
-}
-
-/* async_funcdef: ASYNC funcdef */
-
-static int
-validate_async_funcdef(node *tree)
-{
- int nch = NCH(tree);
- int res = validate_ntype(tree, async_funcdef);
- if (res) {
- if (nch == 2) {
- res = (validate_ntype(CHILD(tree, 0), ASYNC)
- && validate_funcdef(CHILD(tree, 1)));
- }
- else {
- res = 0;
- err_string("illegal number of children for async_funcdef");
- }
- }
- return res;
-}
-
-
-/* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
-
-static int
-validate_async_stmt(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, async_stmt)
- && validate_ntype(CHILD(tree, 0), ASYNC));
-
- if (nch != 2) {
- res = 0;
- err_string("illegal number of children for async_stmt");
- } else {
- if (TYPE(CHILD(tree, 1)) == funcdef) {
- res = validate_funcdef(CHILD(tree, 1));
- }
- else if (TYPE(CHILD(tree, 1)) == with_stmt) {
- res = validate_with_stmt(CHILD(tree, 1));
- }
- else if (TYPE(CHILD(tree, 1)) == for_stmt) {
- res = validate_for(CHILD(tree, 1));
- }
- }
-
- return res;
-}
-
-
-
-/* decorated
- * decorators (classdef | funcdef)
- */
-static int
-validate_decorated(node *tree)
-{
- int nch = NCH(tree);
- int ok = (validate_ntype(tree, decorated)
- && (nch == 2)
- && validate_decorators(RCHILD(tree, -2)));
- if (TYPE(RCHILD(tree, -1)) == funcdef)
- ok = ok && validate_funcdef(RCHILD(tree, -1));
- else
- ok = ok && validate_class(RCHILD(tree, -1));
- return ok;
-}
-
-static int
-validate_lambdef(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, lambdef)
- && ((nch == 3) || (nch == 4))
- && validate_name(CHILD(tree, 0), "lambda")
- && validate_colon(CHILD(tree, nch - 2))
- && validate_test(CHILD(tree, nch - 1)));
-
- if (res && (nch == 4))
- res = validate_varargslist(CHILD(tree, 1));
- else if (!res && !PyErr_Occurred())
- (void) validate_numnodes(tree, 3, "lambdef");
-
- return (res);
-}
-
-
-static int
-validate_lambdef_nocond(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, lambdef_nocond)
- && ((nch == 3) || (nch == 4))
- && validate_name(CHILD(tree, 0), "lambda")
- && validate_colon(CHILD(tree, nch - 2))
- && validate_test(CHILD(tree, nch - 1)));
-
- if (res && (nch == 4))
- res = validate_varargslist(CHILD(tree, 1));
- else if (!res && !PyErr_Occurred())
- (void) validate_numnodes(tree, 3, "lambdef_nocond");
-
- return (res);
-}
-
-
-/* arglist:
- *
- * (argument ',')* (argument [','] | '*' test [',' '**' test] | '**' test)
- */
-static int
-validate_arglist(node *tree)
-{
- int nch = NCH(tree);
- int i = 0;
- int ok = 1;
-
- if (nch <= 0)
- /* raise the right error from having an invalid number of children */
- return validate_numnodes(tree, nch + 1, "arglist");
-
- if (nch > 1) {
- for (i=0; i<nch; i++) {
- if (TYPE(CHILD(tree, i)) == argument) {
- node *ch = CHILD(tree, i);
- if (NCH(ch) == 2 && TYPE(CHILD(ch, 1)) == comp_for) {
- err_string("need '(', ')' for generator expression");
- return 0;
- }
- }
- }
- }
-
- while (ok && nch-i >= 2) {
- /* skip leading (argument ',') */
- ok = (validate_argument(CHILD(tree, i))
- && validate_comma(CHILD(tree, i+1)));
- if (ok)
- i += 2;
- else
- PyErr_Clear();
- }
- ok = 1;
- if (nch-i > 0) {
- int sym = TYPE(CHILD(tree, i));
-
- if (sym == argument) {
- ok = validate_argument(CHILD(tree, i));
- if (ok && i+1 != nch) {
- err_string("illegal arglist specification"
- " (extra stuff on end)");
- ok = 0;
- }
- }
- else {
- err_string("illegal arglist specification");
- ok = 0;
- }
- }
- return (ok);
-}
-
-
-
-/* argument: ( test [comp_for] |
- * test '=' test |
- * '**' test |
- * '*' test )
- */
-static int
-validate_argument(node *tree)
-{
- int nch = NCH(tree);
- int res = (validate_ntype(tree, argument)
- && ((nch == 1) || (nch == 2) || (nch == 3)));
-
- if (res) {
- if (TYPE(CHILD(tree, 0)) == DOUBLESTAR) {
- res = validate_test(CHILD(tree, 1));
- }
- else if (TYPE(CHILD(tree, 0)) == STAR) {
- res = validate_test(CHILD(tree, 1));
- }
- else if (nch == 1) {
- res = validate_test(CHILD(tree, 0));
- }
- else if (nch == 2) {
- res = (validate_test(CHILD(tree, 0))
- && validate_comp_for(CHILD(tree, 1)));
- }
- else if (res && (nch == 3)) {
- res = (validate_test(CHILD(tree, 0))
- && validate_equal(CHILD(tree, 1))
- && validate_test(CHILD(tree, 2)));
- }
- }
- return (res);
-}
-
-
-
-/* trailer:
- *
- * '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
- */
-static int
-validate_trailer(node *tree)
-{
- int nch = NCH(tree);
- int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
-
- if (res) {
- switch (TYPE(CHILD(tree, 0))) {
- case LPAR:
- res = validate_rparen(CHILD(tree, nch - 1));
- if (res && (nch == 3))
- res = validate_arglist(CHILD(tree, 1));
- break;
- case LSQB:
- res = (validate_numnodes(tree, 3, "trailer")
- && validate_subscriptlist(CHILD(tree, 1))
- && validate_ntype(CHILD(tree, 2), RSQB));
- break;
- case DOT:
- res = (validate_numnodes(tree, 2, "trailer")
- && validate_ntype(CHILD(tree, 1), NAME));
- break;
- default:
- res = 0;
- break;
- }
- }
- else {
- (void) validate_numnodes(tree, 2, "trailer");
- }
- return (res);
-}
-
-
-/* subscriptlist:
- *
- * subscript (',' subscript)* [',']
- */
-static int
-validate_subscriptlist(node *tree)
-{
- return (validate_repeating_list(tree, subscriptlist,
- validate_subscript, "subscriptlist"));
-}
-
-
-/* subscript:
- *
- * '.' '.' '.' | test | [test] ':' [test] [sliceop]
- */
-static int
-validate_subscript(node *tree)
-{
- int offset = 0;
- int nch = NCH(tree);
- int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
-
- if (!res) {
- if (!PyErr_Occurred())
- err_string("invalid number of arguments for subscript node");
- return (0);
- }
- if (TYPE(CHILD(tree, 0)) == DOT)
- /* take care of ('.' '.' '.') possibility */
- return (validate_numnodes(tree, 3, "subscript")
- && validate_dot(CHILD(tree, 0))
- && validate_dot(CHILD(tree, 1))
- && validate_dot(CHILD(tree, 2)));
- if (nch == 1) {
- if (TYPE(CHILD(tree, 0)) == test)
- res = validate_test(CHILD(tree, 0));
- else
- res = validate_colon(CHILD(tree, 0));
- return (res);
- }
- /* Must be [test] ':' [test] [sliceop],
- * but at least one of the optional components will
- * be present, but we don't know which yet.
- */
- if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
- res = validate_test(CHILD(tree, 0));
- offset = 1;
- }
- if (res)
- res = validate_colon(CHILD(tree, offset));
- if (res) {
- int rem = nch - ++offset;
- if (rem) {
- if (TYPE(CHILD(tree, offset)) == test) {
- res = validate_test(CHILD(tree, offset));
- ++offset;
- --rem;
- }
- if (res && rem)
- res = validate_sliceop(CHILD(tree, offset));
- }
- }
- return (res);
-}
-
-
-static int
-validate_sliceop(node *tree)
-{
- int nch = NCH(tree);
- int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
- && validate_ntype(tree, sliceop);
- if (!res && !PyErr_Occurred()) {
- res = validate_numnodes(tree, 1, "sliceop");
- }
- if (res)
- res = validate_colon(CHILD(tree, 0));
- if (res && (nch == 2))
- res = validate_test(CHILD(tree, 1));
-
- return (res);
-}
-
-
-static int
-validate_test_or_star_expr(node *n)
-{
- if (TYPE(n) == test)
- return validate_test(n);
- return validate_star_expr(n);
-}
-
-static int
-validate_expr_or_star_expr(node *n)
-{
- if (TYPE(n) == expr)
- return validate_expr(n);
- return validate_star_expr(n);
-}
-
-
-static int
-validate_exprlist(node *tree)
-{
- return (validate_repeating_list(tree, exprlist,
- validate_expr_or_star_expr, "exprlist"));
-}
-
-/* Incrementing validate functions returns nonzero iff success (like other
- * validate functions, and advance *i by the length of the matched pattern. */
-
-/* test ':' test */
-static int
-validate_test_colon_test_inc(node *tree, int *i)
-{
- return (validate_test(CHILD(tree, (*i)++))
- && validate_colon(CHILD(tree, (*i)++))
- && validate_test(CHILD(tree, (*i)++)));
-}
-
-/* test ':' test | '**' expr */
-static int
-validate_dict_element_inc(node *tree, int *i)
-{
- int nch = NCH(tree);
- int res = 0;
- if (nch - *i >= 2) {
- if (TYPE(CHILD(tree, *i+1)) == COLON) {
- /* test ':' test */
- res = validate_test_colon_test_inc(tree, i);
- } else {
- /* '**' expr */
- res = (validate_doublestar(CHILD(tree, (*i)++))
- && validate_expr(CHILD(tree, (*i)++)));
- }
- }
- return res;
-}
-
-/*
- * dictorsetmaker:
- *
- * ( ((test ':' test | '**' expr)
- * (comp_for | (',' (test ':' test | '**' expr))* [','])) |
- * ((test | '*' test)
- * (comp_for | (',' (test | '*' test))* [','])) )
- */
-static int
-validate_dictorsetmaker(node *tree)
-{
- int nch = NCH(tree);
- int res;
- int i = 0;
-
- res = validate_ntype(tree, dictorsetmaker);
- if (!res)
- return 0;
-
- if (nch - i < 1) {
- /* Unconditionally raise. */
- (void) validate_numnodes(tree, 1, "dictorsetmaker");
- return 0;
- }
-
- if (nch - i >= 2
- && ((TYPE(CHILD(tree, i+1)) == COLON) ||
- (TYPE(CHILD(tree, i)) == DOUBLESTAR))) {
- /* Dictionary display or dictionary comprehension. */
- if (nch - i >= 4 && TYPE(CHILD(tree, i+3)) == comp_for) {
- /* Dictionary comprehension. */
- res = (validate_test_colon_test_inc(tree, &i)
- && validate_comp_for(CHILD(tree, i++)));
- if (!res)
- return 0;
- } else {
- /* Dictionary display. */
- return validate_repeating_list_variable(
- tree,
- dictorsetmaker,
- validate_dict_element_inc,
- &i,
- "dictorsetmaker");
- }
- } else {
- /* Set display or set comprehension. */
- if (nch - i >= 2 && TYPE(CHILD(tree, i + 1)) == comp_for) {
- /* Set comprehension. */
- res = (validate_test(CHILD(tree, i++))
- && validate_comp_for(CHILD(tree, i++)));
- if (!res)
- return 0;
- } else {
- /* Set display. */
- return validate_repeating_list(tree,
- dictorsetmaker,
- validate_test_or_star_expr,
- "dictorsetmaker");
- }
- }
-
- if (nch - i > 0) {
- err_string("Illegal trailing nodes for dictorsetmaker.");
- return 0;
- }
-
- return 1;
-}
-
-
-static int
-validate_eval_input(node *tree)
-{
- int pos;
- int nch = NCH(tree);
- int res = (validate_ntype(tree, eval_input)
- && (nch >= 2)
- && validate_testlist(CHILD(tree, 0))
- && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
-
- for (pos = 1; res && (pos < (nch - 1)); ++pos)
- res = validate_ntype(CHILD(tree, pos), NEWLINE);
-
- return (res);
-}
-
-
-static int
-validate_node(node *tree)
-{
- int nch = 0; /* num. children on current node */
- int res = 1; /* result value */
- node* next = 0; /* node to process after this one */
-
- while (res && (tree != 0)) {
- nch = NCH(tree);
- next = 0;
- switch (TYPE(tree)) {
- /*
- * Definition nodes.
- */
- case async_funcdef:
- res = validate_async_funcdef(tree);
- break;
- case async_stmt:
- res = validate_async_stmt(tree);
- break;
- case funcdef:
- res = validate_funcdef(tree);
- break;
- case with_stmt:
- res = validate_with_stmt(tree);
- break;
- case classdef:
- res = validate_class(tree);
- break;
- case decorated:
- res = validate_decorated(tree);
- break;
- /*
- * "Trivial" parse tree nodes.
- * (Why did I call these trivial?)
- */
- case stmt:
- res = validate_stmt(tree);
- break;
- case small_stmt:
- /*
- * expr_stmt | del_stmt | pass_stmt | flow_stmt |
- * import_stmt | global_stmt | nonlocal_stmt | assert_stmt
- */
- res = validate_small_stmt(tree);
- break;
- case flow_stmt:
- res = (validate_numnodes(tree, 1, "flow_stmt")
- && ((TYPE(CHILD(tree, 0)) == break_stmt)
- || (TYPE(CHILD(tree, 0)) == continue_stmt)
- || (TYPE(CHILD(tree, 0)) == yield_stmt)
- || (TYPE(CHILD(tree, 0)) == return_stmt)
- || (TYPE(CHILD(tree, 0)) == raise_stmt)));
- if (res)
- next = CHILD(tree, 0);
- else if (nch == 1)
- err_string("illegal flow_stmt type");
- break;
- case yield_stmt:
- res = validate_yield_stmt(tree);
- break;
- /*
- * Compound statements.
- */
- case simple_stmt:
- res = validate_simple_stmt(tree);
- break;
- case compound_stmt:
- res = validate_compound_stmt(tree);
- break;
- /*
- * Fundamental statements.
- */
- case expr_stmt:
- res = validate_expr_stmt(tree);
- break;
- case del_stmt:
- res = validate_del_stmt(tree);
- break;
- case pass_stmt:
- res = (validate_numnodes(tree, 1, "pass")
- && validate_name(CHILD(tree, 0), "pass"));
- break;
- case break_stmt:
- res = (validate_numnodes(tree, 1, "break")
- && validate_name(CHILD(tree, 0), "break"));
- break;
- case continue_stmt:
- res = (validate_numnodes(tree, 1, "continue")
- && validate_name(CHILD(tree, 0), "continue"));
- break;
- case return_stmt:
- res = validate_return_stmt(tree);
- break;
- case raise_stmt:
- res = validate_raise_stmt(tree);
- break;
- case import_stmt:
- res = validate_import_stmt(tree);
- break;
- case import_name:
- res = validate_import_name(tree);
- break;
- case import_from:
- res = validate_import_from(tree);
- break;
- case global_stmt:
- res = validate_global_stmt(tree);
- break;
- case nonlocal_stmt:
- res = validate_nonlocal_stmt(tree);
- break;
- case assert_stmt:
- res = validate_assert_stmt(tree);
- break;
- case if_stmt:
- res = validate_if(tree);
- break;
- case while_stmt:
- res = validate_while(tree);
- break;
- case for_stmt:
- res = validate_for(tree);
- break;
- case try_stmt:
- res = validate_try(tree);
- break;
- case suite:
- res = validate_suite(tree);
- break;
- /*
- * Expression nodes.
- */
- case testlist:
- res = validate_testlist(tree);
- break;
- case yield_expr:
- res = validate_yield_expr(tree);
- break;
- case test:
- res = validate_test(tree);
- break;
- case and_test:
- res = validate_and_test(tree);
- break;
- case not_test:
- res = validate_not_test(tree);
- break;
- case comparison:
- res = validate_comparison(tree);
- break;
- case exprlist:
- res = validate_exprlist(tree);
- break;
- case comp_op:
- res = validate_comp_op(tree);
- break;
- case expr:
- res = validate_expr(tree);
- break;
- case xor_expr:
- res = validate_xor_expr(tree);
- break;
- case and_expr:
- res = validate_and_expr(tree);
- break;
- case shift_expr:
- res = validate_shift_expr(tree);
- break;
- case arith_expr:
- res = validate_arith_expr(tree);
- break;
- case term:
- res = validate_term(tree);
- break;
- case factor:
- res = validate_factor(tree);
- break;
- case power:
- res = validate_power(tree);
- break;
- case atom:
- res = validate_atom(tree);
- break;
-
- default:
- /* Hopefully never reached! */
- err_string("unrecognized node type");
- res = 0;
- break;
- }
- tree = next;
- }
- return (res);
-}
-
-
-static int
-validate_expr_tree(node *tree)
-{
- int res = validate_eval_input(tree);
-
- if (!res && !PyErr_Occurred())
- err_string("could not validate expression tuple");
-
- return (res);
-}
-
-
-/* file_input:
- * (NEWLINE | stmt)* ENDMARKER
- */
-static int
-validate_file_input(node *tree)
-{
- int j;
- int nch = NCH(tree) - 1;
- int res = ((nch >= 0)
- && validate_ntype(CHILD(tree, nch), ENDMARKER));
-
- for (j = 0; res && (j < nch); ++j) {
- if (TYPE(CHILD(tree, j)) == stmt)
- res = validate_stmt(CHILD(tree, j));
- else
- res = validate_newline(CHILD(tree, j));
- }
- /* This stays in to prevent any internal failures from getting to the
- * user. Hopefully, this won't be needed. If a user reports getting
- * this, we have some debugging to do.
- */
- if (!res && !PyErr_Occurred())
- err_string("VALIDATION FAILURE: report this to the maintainer!");
-
- return (res);
-}
-
-static int
-validate_encoding_decl(node *tree)
-{
- int nch = NCH(tree);
- int res = ((nch == 1)
- && validate_file_input(CHILD(tree, 0)));
-
- if (!res && !PyErr_Occurred())
- err_string("Error Parsing encoding_decl");
-
- return res;
-}
-
static PyObject*
pickle_constructor = NULL;
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 3c3f80660c..7f16a83e78 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -32,6 +32,12 @@
#include "winreparse.h"
#endif
+/* On android API level 21, 'AT_EACCESS' is not declared although
+ * HAVE_FACCESSAT is defined. */
+#ifdef __ANDROID__
+#undef HAVE_FACCESSAT
+#endif
+
#include <stdio.h> /* needed for ctermid() */
#ifdef __cplusplus
@@ -671,21 +677,20 @@ _Py_Dev_Converter(PyObject *obj, void *p)
#endif
static int
-_fd_converter(PyObject *o, int *p, const char *allowed)
+_fd_converter(PyObject *o, int *p)
{
int overflow;
long long_value;
PyObject *index = PyNumber_Index(o);
if (index == NULL) {
- PyErr_Format(PyExc_TypeError,
- "argument should be %s, not %.200s",
- allowed, Py_TYPE(o)->tp_name);
return 0;
}
+ assert(PyLong_Check(index));
long_value = PyLong_AsLongAndOverflow(index, &overflow);
Py_DECREF(index);
+ assert(!PyErr_Occurred());
if (overflow > 0 || long_value > INT_MAX) {
PyErr_SetString(PyExc_OverflowError,
"fd is greater than maximum");
@@ -708,7 +713,15 @@ dir_fd_converter(PyObject *o, void *p)
*(int *)p = DEFAULT_DIR_FD;
return 1;
}
- return _fd_converter(o, (int *)p, "integer");
+ else if (PyIndex_Check(o)) {
+ return _fd_converter(o, (int *)p);
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "argument should be integer or None, not %.200s",
+ Py_TYPE(o)->tp_name);
+ return 0;
+ }
}
@@ -799,8 +812,8 @@ typedef struct {
const char *argument_name;
int nullable;
int allow_fd;
- wchar_t *wide;
- char *narrow;
+ const wchar_t *wide;
+ const char *narrow;
int fd;
Py_ssize_t length;
PyObject *object;
@@ -818,11 +831,12 @@ path_cleanup(path_t *path) {
}
static int
-path_converter(PyObject *o, void *p) {
+path_converter(PyObject *o, void *p)
+{
path_t *path = (path_t *)p;
- PyObject *unicode, *bytes;
+ PyObject *bytes;
Py_ssize_t length;
- char *narrow;
+ const char *narrow;
#define FORMAT_EXCEPTION(exc, fmt) \
PyErr_Format(exc, "%s%s" fmt, \
@@ -839,12 +853,7 @@ path_converter(PyObject *o, void *p) {
/* ensure it's always safe to call path_cleanup() */
path->cleanup = NULL;
- if (o == Py_None) {
- if (!path->nullable) {
- FORMAT_EXCEPTION(PyExc_TypeError,
- "can't specify None for %s argument");
- return 0;
- }
+ if ((o == Py_None) && path->nullable) {
path->wide = NULL;
path->narrow = NULL;
path->length = 0;
@@ -853,24 +862,20 @@ path_converter(PyObject *o, void *p) {
return 1;
}
- unicode = PyUnicode_FromObject(o);
- if (unicode) {
+ if (PyUnicode_Check(o)) {
#ifdef MS_WINDOWS
- wchar_t *wide;
+ const wchar_t *wide;
- wide = PyUnicode_AsUnicodeAndSize(unicode, &length);
+ wide = PyUnicode_AsUnicodeAndSize(o, &length);
if (!wide) {
- Py_DECREF(unicode);
return 0;
}
if (length > 32767) {
FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
- Py_DECREF(unicode);
return 0;
}
if (wcslen(wide) != length) {
- FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character");
- Py_DECREF(unicode);
+ FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
return 0;
}
@@ -879,51 +884,46 @@ path_converter(PyObject *o, void *p) {
path->length = length;
path->object = o;
path->fd = -1;
- path->cleanup = unicode;
- return Py_CLEANUP_SUPPORTED;
+ return 1;
#else
- int converted = PyUnicode_FSConverter(unicode, &bytes);
- Py_DECREF(unicode);
- if (!converted)
- bytes = NULL;
+ if (!PyUnicode_FSConverter(o, &bytes)) {
+ return 0;
+ }
#endif
}
- else {
- PyErr_Clear();
- if (PyObject_CheckBuffer(o))
- bytes = PyBytes_FromObject(o);
- else
- bytes = NULL;
+ else if (PyObject_CheckBuffer(o)) {
+#ifdef MS_WINDOWS
+ if (win32_warn_bytes_api()) {
+ return 0;
+ }
+#endif
+ bytes = PyBytes_FromObject(o);
if (!bytes) {
- PyErr_Clear();
- if (path->allow_fd) {
- int fd;
- int result = _fd_converter(o, &fd,
- "string, bytes or integer");
- if (result) {
- path->wide = NULL;
- path->narrow = NULL;
- path->length = 0;
- path->object = o;
- path->fd = fd;
- return result;
- }
- }
+ return 0;
}
}
-
- if (!bytes) {
- if (!PyErr_Occurred())
- FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter");
- return 0;
+ else if (path->allow_fd && PyIndex_Check(o)) {
+ if (!_fd_converter(o, &path->fd)) {
+ return 0;
+ }
+ path->wide = NULL;
+ path->narrow = NULL;
+ path->length = 0;
+ path->object = o;
+ return 1;
}
-
-#ifdef MS_WINDOWS
- if (win32_warn_bytes_api()) {
- Py_DECREF(bytes);
+ else {
+ PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
+ path->function_name ? path->function_name : "",
+ path->function_name ? ": " : "",
+ path->argument_name ? path->argument_name : "path",
+ path->allow_fd && path->nullable ? "string, bytes, integer or None" :
+ path->allow_fd ? "string, bytes or integer" :
+ path->nullable ? "string, bytes or None" :
+ "string or bytes",
+ Py_TYPE(o)->tp_name);
return 0;
}
-#endif
length = PyBytes_GET_SIZE(bytes);
#ifdef MS_WINDOWS
@@ -951,7 +951,8 @@ path_converter(PyObject *o, void *p) {
}
static void
-argument_unavailable_error(char *function_name, char *argument_name) {
+argument_unavailable_error(const char *function_name, const char *argument_name)
+{
PyErr_Format(PyExc_NotImplementedError,
"%s%s%s unavailable on this platform",
(function_name != NULL) ? function_name : "",
@@ -974,7 +975,8 @@ dir_fd_unavailable(PyObject *o, void *p)
}
static int
-fd_specified(char *function_name, int fd) {
+fd_specified(const char *function_name, int fd)
+{
if (fd == -1)
return 0;
@@ -983,7 +985,8 @@ fd_specified(char *function_name, int fd) {
}
static int
-follow_symlinks_specified(char *function_name, int follow_symlinks) {
+follow_symlinks_specified(const char *function_name, int follow_symlinks)
+{
if (follow_symlinks)
return 0;
@@ -992,7 +995,8 @@ follow_symlinks_specified(char *function_name, int follow_symlinks) {
}
static int
-path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) {
+path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
+{
if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) {
PyErr_Format(PyExc_ValueError,
"%s: can't specify dir_fd without matching path",
@@ -1003,7 +1007,8 @@ path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) {
}
static int
-dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) {
+dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
+{
if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
PyErr_Format(PyExc_ValueError,
"%s: can't specify both dir_fd and fd",
@@ -1014,8 +1019,9 @@ dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) {
}
static int
-fd_and_follow_symlinks_invalid(char *function_name, int fd,
- int follow_symlinks) {
+fd_and_follow_symlinks_invalid(const char *function_name, int fd,
+ int follow_symlinks)
+{
if ((fd > 0) && (!follow_symlinks)) {
PyErr_Format(PyExc_ValueError,
"%s: cannot use fd and follow_symlinks together",
@@ -1026,8 +1032,9 @@ fd_and_follow_symlinks_invalid(char *function_name, int fd,
}
static int
-dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd,
- int follow_symlinks) {
+dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
+ int follow_symlinks)
+{
if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
PyErr_Format(PyExc_ValueError,
"%s: cannot use dir_fd and follow_symlinks together",
@@ -1159,7 +1166,7 @@ convertenviron(void)
for (e = _wenviron; *e != NULL; e++) {
PyObject *k;
PyObject *v;
- wchar_t *p = wcschr(*e, L'=');
+ const wchar_t *p = wcschr(*e, L'=');
if (p == NULL)
continue;
k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
@@ -1187,7 +1194,7 @@ convertenviron(void)
for (e = environ; *e != NULL; e++) {
PyObject *k;
PyObject *v;
- char *p = strchr(*e, '=');
+ const char *p = strchr(*e, '=');
if (p == NULL)
continue;
k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
@@ -1222,7 +1229,7 @@ posix_error(void)
#ifdef MS_WINDOWS
static PyObject *
-win32_error(char* function, const char* filename)
+win32_error(const char* function, const char* filename)
{
/* XXX We should pass the function name along in the future.
(winreg.c also wants to pass the function name.)
@@ -1237,7 +1244,7 @@ win32_error(char* function, const char* filename)
}
static PyObject *
-win32_error_object(char* function, PyObject* filename)
+win32_error_object(const char* function, PyObject* filename)
{
/* XXX - see win32_error for comments on 'function' */
errno = GetLastError();
@@ -1458,7 +1465,7 @@ get_target_path(HANDLE hdl, wchar_t **target_path)
if(!buf_size)
return FALSE;
- buf = PyMem_New(wchar_t, buf_size+1);
+ buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t));
if (!buf) {
SetLastError(ERROR_OUTOFMEMORY);
return FALSE;
@@ -1468,12 +1475,12 @@ get_target_path(HANDLE hdl, wchar_t **target_path)
buf, buf_size, VOLUME_NAME_DOS);
if(!result_length) {
- PyMem_Free(buf);
+ PyMem_RawFree(buf);
return FALSE;
}
if(!CloseHandle(hdl)) {
- PyMem_Free(buf);
+ PyMem_RawFree(buf);
return FALSE;
}
@@ -1558,7 +1565,7 @@ win32_xstat_impl(const char *path, struct _Py_stat_struct *result,
return -1;
code = win32_xstat_impl_w(target_path, result, FALSE);
- PyMem_Free(target_path);
+ PyMem_RawFree(target_path);
return code;
}
} else
@@ -1648,7 +1655,7 @@ win32_xstat_impl_w(const wchar_t *path, struct _Py_stat_struct *result,
return -1;
code = win32_xstat_impl_w(target_path, result, FALSE);
- PyMem_Free(target_path);
+ PyMem_RawFree(target_path);
return code;
}
} else
@@ -2102,7 +2109,7 @@ _pystat_fromstructstat(STRUCT_STAT *st)
static PyObject *
-posix_do_stat(char *function_name, path_t *path,
+posix_do_stat(const char *function_name, path_t *path,
int dir_fd, int follow_symlinks)
{
STRUCT_STAT st;
@@ -3314,12 +3321,22 @@ posix_getcwd(int use_bytes)
Py_BEGIN_ALLOW_THREADS
do {
buflen += chunk;
+#ifdef MS_WINDOWS
+ if (buflen > INT_MAX) {
+ PyErr_NoMemory();
+ break;
+ }
+#endif
tmpbuf = PyMem_RawRealloc(buf, buflen);
if (tmpbuf == NULL)
break;
buf = tmpbuf;
+#ifdef MS_WINDOWS
+ cwd = getcwd(buf, (int)buflen);
+#else
cwd = getcwd(buf, buflen);
+#endif
} while (cwd == NULL && errno == ERANGE);
Py_END_ALLOW_THREADS
@@ -3461,15 +3478,13 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
BOOL result;
WIN32_FIND_DATA FileData;
char namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
- char *bufptr = namebuf;
/* only claim to have space for MAX_PATH */
Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
- PyObject *po = NULL;
wchar_t *wnamebuf = NULL;
if (!path->narrow) {
WIN32_FIND_DATAW wFileData;
- wchar_t *po_wchars;
+ const wchar_t *po_wchars;
if (!path->wide) { /* Default arg: "." */
po_wchars = L".";
@@ -3635,7 +3650,7 @@ _posix_listdir(path_t *path, PyObject *list)
else
#endif
{
- char *name;
+ const char *name;
if (path->narrow) {
name = path->narrow;
/* only return bytes if they specified a bytes object */
@@ -3817,7 +3832,7 @@ os__getfinalpathname_impl(PyObject *module, PyObject *path)
wchar_t *target_path;
int result_length;
PyObject *result;
- wchar_t *path_wchar;
+ const wchar_t *path_wchar;
path_wchar = PyUnicode_AsUnicode(path);
if (path_wchar == NULL)
@@ -3906,7 +3921,8 @@ os__getvolumepathname_impl(PyObject *module, PyObject *path)
/*[clinic end generated code: output=cbdcbd1059ceef4c input=7eacadc40acbda6b]*/
{
PyObject *result;
- wchar_t *path_wchar, *mountpath=NULL;
+ const wchar_t *path_wchar;
+ wchar_t *mountpath=NULL;
size_t buflen;
BOOL ret;
@@ -4104,7 +4120,7 @@ os_setpriority_impl(PyObject *module, int which, int who, int priority)
static PyObject *
internal_rename(path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd, int is_replace)
{
- char *function_name = is_replace ? "replace" : "rename";
+ const char *function_name = is_replace ? "replace" : "rename";
int dir_fd_specified;
#ifdef MS_WINDOWS
@@ -4284,7 +4300,7 @@ os_system_impl(PyObject *module, PyObject *command)
/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
{
long result;
- char *bytes = PyBytes_AsString(command);
+ const char *bytes = PyBytes_AsString(command);
Py_BEGIN_ALLOW_THREADS
result = system(bytes);
Py_END_ALLOW_THREADS
@@ -4564,7 +4580,7 @@ typedef struct {
#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
static int
-utime_dir_fd(utime_t *ut, int dir_fd, char *path, int follow_symlinks)
+utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
{
#ifdef HAVE_UTIMENSAT
int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
@@ -4606,14 +4622,14 @@ utime_fd(utime_t *ut, int fd)
#define PATH_UTIME_HAVE_FD 0
#endif
+#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
+# define UTIME_HAVE_NOFOLLOW_SYMLINKS
+#endif
-#define UTIME_HAVE_NOFOLLOW_SYMLINKS \
- (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES))
-
-#if UTIME_HAVE_NOFOLLOW_SYMLINKS
+#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
static int
-utime_nofollow_symlinks(utime_t *ut, char *path)
+utime_nofollow_symlinks(utime_t *ut, const char *path)
{
#ifdef HAVE_UTIMENSAT
UTIME_TO_TIMESPEC;
@@ -4629,7 +4645,7 @@ utime_nofollow_symlinks(utime_t *ut, char *path)
#ifndef MS_WINDOWS
static int
-utime_default(utime_t *ut, char *path)
+utime_default(utime_t *ut, const char *path)
{
#ifdef HAVE_UTIMENSAT
UTIME_TO_TIMESPEC;
@@ -4772,7 +4788,7 @@ os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
utime.now = 1;
}
-#if !UTIME_HAVE_NOFOLLOW_SYMLINKS
+#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
if (follow_symlinks_specified("utime", follow_symlinks))
goto exit;
#endif
@@ -4826,7 +4842,7 @@ os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
#else /* MS_WINDOWS */
Py_BEGIN_ALLOW_THREADS
-#if UTIME_HAVE_NOFOLLOW_SYMLINKS
+#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
result = utime_nofollow_symlinks(&utime, path->narrow);
else
@@ -4923,7 +4939,8 @@ parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
Py_ssize_t i, pos, envc;
PyObject *keys=NULL, *vals=NULL;
PyObject *key, *val, *key2, *val2;
- char *p, *k, *v;
+ char *p;
+ const char *k, *v;
size_t len;
i = PyMapping_Size(env);
@@ -5038,7 +5055,7 @@ static PyObject *
os_execv_impl(PyObject *module, PyObject *path, PyObject *argv)
/*[clinic end generated code: output=b21dc34deeb5b004 input=96041559925e5229]*/
{
- char *path_char;
+ const char *path_char;
char **argvlist;
Py_ssize_t argc;
@@ -5158,7 +5175,7 @@ static PyObject *
os_spawnv_impl(PyObject *module, int mode, PyObject *path, PyObject *argv)
/*[clinic end generated code: output=c427c0ce40f10638 input=042c91dfc1e6debc]*/
{
- char *path_char;
+ const char *path_char;
char **argvlist;
int i;
Py_ssize_t argc;
@@ -5236,7 +5253,7 @@ os_spawnve_impl(PyObject *module, int mode, PyObject *path, PyObject *argv,
PyObject *env)
/*[clinic end generated code: output=ebcfa5f7ba2f4219 input=02362fd937963f8f]*/
{
- char *path_char;
+ const char *path_char;
char **argvlist;
char **envlist;
PyObject *res = NULL;
@@ -5756,14 +5773,14 @@ os.sched_getaffinity
pid: pid_t
/
-Return the affinity of the process identified by pid.
+Return the affinity of the process identified by pid (or the current process if zero).
The affinity is returned as a set of CPU identifiers.
[clinic start generated code]*/
static PyObject *
os_sched_getaffinity_impl(PyObject *module, pid_t pid)
-/*[clinic end generated code: output=f726f2c193c17a4f input=eaf161936874b8a1]*/
+/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
{
int cpu, ncpus, count;
size_t setsize;
@@ -5924,7 +5941,7 @@ os_openpty_impl(PyObject *module)
if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
goto posix_error;
-#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
+#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
#ifndef __hpux
@@ -6249,7 +6266,7 @@ static PyObject *
posix_initgroups(PyObject *self, PyObject *args)
{
PyObject *oname;
- char *username;
+ const char *username;
int res;
#ifdef __APPLE__
int gid;
@@ -7123,16 +7140,16 @@ exit:
static PyObject *
win_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
{
- wchar_t *path;
+ const wchar_t *path;
DWORD n_bytes_returned;
DWORD io_result;
PyObject *po, *result;
- int dir_fd;
+ int dir_fd;
HANDLE reparse_point_handle;
char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
- wchar_t *print_name;
+ const wchar_t *print_name;
static char *keywords[] = {"path", "dir_fd", NULL};
@@ -7200,8 +7217,8 @@ win_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
#if defined(MS_WINDOWS)
/* Grab CreateSymbolicLinkW dynamically from kernel32 */
-static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL;
-static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL;
+static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD) = NULL;
+static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPCSTR, LPCSTR, DWORD) = NULL;
static int
check_CreateSymbolicLink(void)
@@ -7306,7 +7323,7 @@ _joinA(char *dest_path, const char *root, const char *rest)
/* Return True if the path at src relative to dest is a directory */
static int
-_check_dirW(WCHAR *src, WCHAR *dest)
+_check_dirW(LPCWSTR src, LPCWSTR dest)
{
WIN32_FILE_ATTRIBUTE_DATA src_info;
WCHAR dest_parent[MAX_PATH];
@@ -7325,7 +7342,7 @@ _check_dirW(WCHAR *src, WCHAR *dest)
/* Return True if the path at src relative to dest is a directory */
static int
-_check_dirA(char *src, char *dest)
+_check_dirA(LPCSTR src, LPCSTR dest)
{
WIN32_FILE_ATTRIBUTE_DATA src_info;
char dest_parent[MAX_PATH];
@@ -9013,7 +9030,7 @@ static PyObject *
os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
{
- wchar_t *env;
+ const wchar_t *env;
PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
if (unicode == NULL) {
@@ -9059,8 +9076,8 @@ os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
{
PyObject *bytes = NULL;
char *env;
- char *name_string = PyBytes_AsString(name);
- char *value_string = PyBytes_AsString(value);
+ const char *name_string = PyBytes_AsString(name);
+ const char *value_string = PyBytes_AsString(value);
bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
if (bytes == NULL) {
@@ -9479,8 +9496,8 @@ os__getdiskusage_impl(PyObject *module, Py_UNICODE *path)
* sufficiently pervasive that it's not worth the loss of readability.
*/
struct constdef {
- char *name;
- long value;
+ const char *name;
+ int value;
};
static int
@@ -9488,7 +9505,10 @@ conv_confname(PyObject *arg, int *valuep, struct constdef *table,
size_t tablesize)
{
if (PyLong_Check(arg)) {
- *valuep = PyLong_AS_LONG(arg);
+ int value = _PyLong_AsInt(arg);
+ if (value == -1 && PyErr_Occurred())
+ return 0;
+ *valuep = value;
return 1;
}
else {
@@ -10449,7 +10469,7 @@ cmp_constdefs(const void *v1, const void *v2)
static int
setup_confname_table(struct constdef *table, size_t tablesize,
- char *tablename, PyObject *module)
+ const char *tablename, PyObject *module)
{
PyObject *d = NULL;
size_t i;
@@ -10576,9 +10596,9 @@ static PyObject *
win32_startfile(PyObject *self, PyObject *args)
{
PyObject *ofilepath;
- char *filepath;
- char *operation = NULL;
- wchar_t *wpath, *woperation;
+ const char *filepath;
+ const char *operation = NULL;
+ const wchar_t *wpath, *woperation;
HINSTANCE rc;
PyObject *unipath, *uoperation = NULL;
@@ -10819,7 +10839,7 @@ os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
for (i = 0; ; i++) {
void *ptr;
ssize_t result;
- static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
+ static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Py_ssize_t buffer_size = buffer_sizes[i];
if (!buffer_size) {
path_error(path);
@@ -10983,9 +11003,9 @@ os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
name = path->narrow ? path->narrow : ".";
for (i = 0; ; i++) {
- char *start, *trace, *end;
+ const char *start, *trace, *end;
ssize_t length;
- static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
+ static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Py_ssize_t buffer_size = buffer_sizes[i];
if (!buffer_size) {
/* ERANGE */
@@ -11197,11 +11217,15 @@ get_terminal_size(PyObject *self, PyObject *args)
os.cpu_count
Return the number of CPUs in the system; return None if indeterminable.
+
+This number is not equivalent to the number of CPUs the current process can
+use. The number of usable CPUs can be obtained with
+``len(os.sched_getaffinity(0))``
[clinic start generated code]*/
static PyObject *
os_cpu_count_impl(PyObject *module)
-/*[clinic end generated code: output=5fc29463c3936a9c input=d55e2f8f3823a628]*/
+/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
{
int ncpu = 0;
#ifdef MS_WINDOWS
@@ -11458,7 +11482,7 @@ DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
struct _Py_stat_struct st;
#ifdef MS_WINDOWS
- wchar_t *path;
+ const wchar_t *path;
path = PyUnicode_AsUnicode(self->path);
if (!path)
@@ -11475,7 +11499,7 @@ DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
}
#else /* POSIX */
PyObject *bytes;
- char *path;
+ const char *path;
if (!PyUnicode_FSConverter(self->path, &bytes))
return NULL;
@@ -11659,7 +11683,7 @@ DirEntry_inode(DirEntry *self)
{
#ifdef MS_WINDOWS
if (!self->got_file_index) {
- wchar_t *path;
+ const wchar_t *path;
struct _Py_stat_struct stat;
path = PyUnicode_AsUnicode(self->path);
@@ -11690,6 +11714,13 @@ DirEntry_repr(DirEntry *self)
return PyUnicode_FromFormat("<DirEntry %R>", self->name);
}
+static PyObject *
+DirEntry_fspath(DirEntry *self)
+{
+ Py_INCREF(self->path);
+ return self->path;
+}
+
static PyMemberDef DirEntry_members[] = {
{"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
"the entry's base filename, relative to scandir() \"path\" argument"},
@@ -11714,6 +11745,9 @@ static PyMethodDef DirEntry_methods[] = {
{"inode", (PyCFunction)DirEntry_inode, METH_NOARGS,
"return inode of the entry; cached per entry",
},
+ {"__fspath__", (PyCFunction)DirEntry_fspath, METH_NOARGS,
+ "returns the path for the entry",
+ },
{NULL}
};
@@ -11753,7 +11787,7 @@ static PyTypeObject DirEntryType = {
#ifdef MS_WINDOWS
static wchar_t *
-join_path_filenameW(wchar_t *path_wide, wchar_t* filename)
+join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
{
Py_ssize_t path_len;
Py_ssize_t size;
@@ -11828,7 +11862,7 @@ error:
#else /* POSIX */
static char *
-join_path_filename(char *path_narrow, char* filename, Py_ssize_t filename_len)
+join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
{
Py_ssize_t path_len;
Py_ssize_t size;
@@ -11860,7 +11894,7 @@ join_path_filename(char *path_narrow, char* filename, Py_ssize_t filename_len)
}
static PyObject *
-DirEntry_from_posix_info(path_t *path, char *name, Py_ssize_t name_len,
+DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
ino_t d_ino
#ifdef HAVE_DIRENT_D_TYPE
, unsigned char d_type
@@ -11923,8 +11957,14 @@ typedef struct {
#ifdef MS_WINDOWS
+static int
+ScandirIterator_is_closed(ScandirIterator *iterator)
+{
+ return iterator->handle == INVALID_HANDLE_VALUE;
+}
+
static void
-ScandirIterator_close(ScandirIterator *iterator)
+ScandirIterator_closedir(ScandirIterator *iterator)
{
HANDLE handle = iterator->handle;
@@ -11944,7 +11984,7 @@ ScandirIterator_iternext(ScandirIterator *iterator)
BOOL success;
PyObject *entry;
- /* Happens if the iterator is iterated twice */
+ /* Happens if the iterator is iterated twice, or closed explicitly */
if (iterator->handle == INVALID_HANDLE_VALUE)
return NULL;
@@ -11975,14 +12015,20 @@ ScandirIterator_iternext(ScandirIterator *iterator)
}
/* Error or no more files */
- ScandirIterator_close(iterator);
+ ScandirIterator_closedir(iterator);
return NULL;
}
#else /* POSIX */
+static int
+ScandirIterator_is_closed(ScandirIterator *iterator)
+{
+ return !iterator->dirp;
+}
+
static void
-ScandirIterator_close(ScandirIterator *iterator)
+ScandirIterator_closedir(ScandirIterator *iterator)
{
DIR *dirp = iterator->dirp;
@@ -12004,7 +12050,7 @@ ScandirIterator_iternext(ScandirIterator *iterator)
int is_dot;
PyObject *entry;
- /* Happens if the iterator is iterated twice */
+ /* Happens if the iterator is iterated twice, or closed explicitly */
if (!iterator->dirp)
return NULL;
@@ -12041,21 +12087,76 @@ ScandirIterator_iternext(ScandirIterator *iterator)
}
/* Error or no more files */
- ScandirIterator_close(iterator);
+ ScandirIterator_closedir(iterator);
return NULL;
}
#endif
+static PyObject *
+ScandirIterator_close(ScandirIterator *self, PyObject *args)
+{
+ ScandirIterator_closedir(self);
+ Py_RETURN_NONE;
+}
+
+static PyObject *
+ScandirIterator_enter(PyObject *self, PyObject *args)
+{
+ Py_INCREF(self);
+ return self;
+}
+
+static PyObject *
+ScandirIterator_exit(ScandirIterator *self, PyObject *args)
+{
+ ScandirIterator_closedir(self);
+ Py_RETURN_NONE;
+}
+
static void
-ScandirIterator_dealloc(ScandirIterator *iterator)
+ScandirIterator_finalize(ScandirIterator *iterator)
{
- ScandirIterator_close(iterator);
- Py_XDECREF(iterator->path.object);
+ PyObject *error_type, *error_value, *error_traceback;
+
+ /* Save the current exception, if any. */
+ PyErr_Fetch(&error_type, &error_value, &error_traceback);
+
+ if (!ScandirIterator_is_closed(iterator)) {
+ ScandirIterator_closedir(iterator);
+
+ if (PyErr_ResourceWarning((PyObject *)iterator, 1,
+ "unclosed scandir iterator %R", iterator)) {
+ /* Spurious errors can appear at shutdown */
+ if (PyErr_ExceptionMatches(PyExc_Warning)) {
+ PyErr_WriteUnraisable((PyObject *) iterator);
+ }
+ }
+ }
+
+ Py_CLEAR(iterator->path.object);
path_cleanup(&iterator->path);
+
+ /* Restore the saved exception. */
+ PyErr_Restore(error_type, error_value, error_traceback);
+}
+
+static void
+ScandirIterator_dealloc(ScandirIterator *iterator)
+{
+ if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
+ return;
+
Py_TYPE(iterator)->tp_free((PyObject *)iterator);
}
+static PyMethodDef ScandirIterator_methods[] = {
+ {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
+ {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
+ {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
+ {NULL}
+};
+
static PyTypeObject ScandirIteratorType = {
PyVarObject_HEAD_INIT(NULL, 0)
MODNAME ".ScandirIterator", /* tp_name */
@@ -12077,7 +12178,8 @@ static PyTypeObject ScandirIteratorType = {
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
+ Py_TPFLAGS_DEFAULT
+ | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
@@ -12085,6 +12187,27 @@ static PyTypeObject ScandirIteratorType = {
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)ScandirIterator_iternext, /* tp_iternext */
+ ScandirIterator_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 */
+ 0, /* tp_init */
+ 0, /* tp_alloc */
+ 0, /* tp_new */
+ 0, /* tp_free */
+ 0, /* tp_is_gc */
+ 0, /* tp_bases */
+ 0, /* tp_mro */
+ 0, /* tp_cache */
+ 0, /* tp_subclasses */
+ 0, /* tp_weaklist */
+ 0, /* tp_del */
+ 0, /* tp_version_tag */
+ (destructor)ScandirIterator_finalize, /* tp_finalize */
};
static PyObject *
@@ -12095,7 +12218,7 @@ posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs)
#ifdef MS_WINDOWS
wchar_t *path_strW;
#else
- char *path;
+ const char *path;
#endif
iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
@@ -12167,6 +12290,69 @@ error:
return NULL;
}
+/*
+ Return the file system path representation of the object.
+
+ If the object is str or bytes, then allow it to pass through with
+ an incremented refcount. If the object defines __fspath__(), then
+ return the result of that method. All other types raise a TypeError.
+*/
+PyObject *
+PyOS_FSPath(PyObject *path)
+{
+ _Py_IDENTIFIER(__fspath__);
+ PyObject *func = NULL;
+ PyObject *path_repr = NULL;
+
+ if (PyUnicode_Check(path) || PyBytes_Check(path)) {
+ Py_INCREF(path);
+ return path;
+ }
+
+ func = _PyObject_LookupSpecial(path, &PyId___fspath__);
+ if (NULL == func) {
+ return PyErr_Format(PyExc_TypeError,
+ "expected str, bytes or os.PathLike object, "
+ "not %.200s",
+ Py_TYPE(path)->tp_name);
+ }
+
+ path_repr = PyObject_CallFunctionObjArgs(func, NULL);
+ Py_DECREF(func);
+ if (NULL == path_repr) {
+ return NULL;
+ }
+
+ if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
+ PyErr_Format(PyExc_TypeError,
+ "expected %.200s.__fspath__() to return str or bytes, "
+ "not %.200s", Py_TYPE(path)->tp_name,
+ Py_TYPE(path_repr)->tp_name);
+ Py_DECREF(path_repr);
+ return NULL;
+ }
+
+ return path_repr;
+}
+
+/*[clinic input]
+os.fspath
+
+ path: object
+
+Return the file system path representation of the object.
+
+If the object is str or bytes, then allow it to pass through as-is. If the
+object defines __fspath__(), then return the result of that method. All other
+types raise a TypeError.
+[clinic start generated code]*/
+
+static PyObject *
+os_fspath_impl(PyObject *module, PyObject *path)
+/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
+{
+ return PyOS_FSPath(path);
+}
#include "clinic/posixmodule.c.h"
@@ -12367,6 +12553,7 @@ static PyMethodDef posix_methods[] = {
{"scandir", (PyCFunction)posix_scandir,
METH_VARARGS | METH_KEYWORDS,
posix_scandir__doc__},
+ OS_FSPATH_METHODDEF
{NULL, NULL} /* Sentinel */
};
@@ -12378,7 +12565,6 @@ enable_symlink()
HANDLE tok;
TOKEN_PRIVILEGES tok_priv;
LUID luid;
- int meth_idx = 0;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
return 0;
@@ -12481,12 +12667,14 @@ all_ins(PyObject *m)
#ifdef O_LARGEFILE
if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
#endif
+#ifndef __GNU__
#ifdef O_SHLOCK
if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
#endif
#ifdef O_EXLOCK
if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
#endif
+#endif
#ifdef O_EXEC
if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
#endif
@@ -12783,25 +12971,25 @@ all_ins(PyObject *m)
if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
#endif
-#ifdef RTLD_LAZY
+#if HAVE_DECL_RTLD_LAZY
if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
#endif
-#ifdef RTLD_NOW
+#if HAVE_DECL_RTLD_NOW
if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
#endif
-#ifdef RTLD_GLOBAL
+#if HAVE_DECL_RTLD_GLOBAL
if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
#endif
-#ifdef RTLD_LOCAL
+#if HAVE_DECL_RTLD_LOCAL
if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
#endif
-#ifdef RTLD_NODELETE
+#if HAVE_DECL_RTLD_NODELETE
if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
#endif
-#ifdef RTLD_NOLOAD
+#if HAVE_DECL_RTLD_NOLOAD
if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
#endif
-#ifdef RTLD_DEEPBIND
+#if HAVE_DECL_RTLD_DEEPBIND
if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
#endif
@@ -12822,7 +13010,7 @@ static struct PyModuleDef posixmodule = {
};
-static char *have_functions[] = {
+static const char * const have_functions[] = {
#ifdef HAVE_FACCESSAT
"HAVE_FACCESSAT",
@@ -12957,7 +13145,7 @@ INITFUNC(void)
{
PyObject *m, *v;
PyObject *list;
- char **trace;
+ const char * const *trace;
#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
win32_can_symlink = enable_symlink();
@@ -13132,6 +13320,7 @@ INITFUNC(void)
Py_DECREF(unicode);
}
PyModule_AddObject(m, "_have_functions", list);
+ PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType);
initialized = 1;
diff --git a/Modules/pwdmodule.c b/Modules/pwdmodule.c
index 7416cf71d5..784e9d0349 100644
--- a/Modules/pwdmodule.c
+++ b/Modules/pwdmodule.c
@@ -75,10 +75,18 @@ mkpwent(struct passwd *p)
#define SETS(i,val) sets(v, i, val)
SETS(setIndex++, p->pw_name);
+#if defined(HAVE_STRUCT_PASSWD_PW_PASSWD) && !defined(__ANDROID__)
SETS(setIndex++, p->pw_passwd);
+#else
+ SETS(setIndex++, "");
+#endif
PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid));
PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid));
+#if defined(HAVE_STRUCT_PASSWD_PW_GECOS)
SETS(setIndex++, p->pw_gecos);
+#else
+ SETS(setIndex++, "");
+#endif
SETS(setIndex++, p->pw_dir);
SETS(setIndex++, p->pw_shell);
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
index eb322c20d4..dc97e9d2bf 100644
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -91,7 +91,7 @@ static struct HandlerInfo handler_info[64];
* false on an exception.
*/
static int
-set_error_attr(PyObject *err, char *name, int value)
+set_error_attr(PyObject *err, const char *name, int value)
{
PyObject *v = PyLong_FromLong(value);
@@ -218,7 +218,7 @@ flag_error(xmlparseobject *self)
}
static PyObject*
-call_with_frame(char *funcname, int lineno, PyObject* func, PyObject* args,
+call_with_frame(const char *funcname, int lineno, PyObject* func, PyObject* args,
xmlparseobject *self)
{
PyObject *res;
@@ -747,7 +747,8 @@ pyexpat_xmlparser_Parse_impl(xmlparseobject *self, PyObject *data,
s += MAX_CHUNK_SIZE;
slen -= MAX_CHUNK_SIZE;
}
- assert(MAX_CHUNK_SIZE < INT_MAX && slen < INT_MAX);
+ Py_BUILD_ASSERT(MAX_CHUNK_SIZE <= INT_MAX);
+ assert(slen <= INT_MAX);
rc = XML_Parse(self->itself, s, (int)slen, isfinal);
done:
@@ -765,7 +766,7 @@ readinst(char *buf, int buf_size, PyObject *meth)
{
PyObject *str;
Py_ssize_t len;
- char *ptr;
+ const char *ptr;
str = PyObject_CallFunction(meth, "n", buf_size);
if (str == NULL)
@@ -1225,12 +1226,8 @@ xmlparse_dealloc(xmlparseobject *self)
self->itself = NULL;
if (self->handlers != NULL) {
- PyObject *temp;
- for (i = 0; handler_info[i].name != NULL; i++) {
- temp = self->handlers[i];
- self->handlers[i] = NULL;
- Py_XDECREF(temp);
- }
+ for (i = 0; handler_info[i].name != NULL; i++)
+ Py_CLEAR(self->handlers[i]);
PyMem_Free(self->handlers);
self->handlers = NULL;
}
@@ -1344,7 +1341,6 @@ sethandler(xmlparseobject *self, PyObject *name, PyObject* v)
int handlernum = handlername2int(name);
if (handlernum >= 0) {
xmlhandler c_handler = NULL;
- PyObject *temp = self->handlers[handlernum];
if (v == Py_None) {
/* If this is the character data handler, and a character
@@ -1366,8 +1362,7 @@ sethandler(xmlparseobject *self, PyObject *name, PyObject* v)
Py_INCREF(v);
c_handler = handler_info[handlernum].handler;
}
- self->handlers[handlernum] = v;
- Py_XDECREF(temp);
+ Py_XSETREF(self->handlers[handlernum], v);
handler_info[handlernum].setter(self->itself, c_handler);
return 1;
}
@@ -1897,15 +1892,12 @@ static void
clear_handlers(xmlparseobject *self, int initial)
{
int i = 0;
- PyObject *temp;
for (; handler_info[i].name != NULL; i++) {
if (initial)
self->handlers[i] = NULL;
else {
- temp = self->handlers[i];
- self->handlers[i] = NULL;
- Py_XDECREF(temp);
+ Py_CLEAR(self->handlers[i]);
handler_info[i].setter(self->itself, NULL);
}
}
diff --git a/Modules/readline.c b/Modules/readline.c
index 91f7cca73e..f8876e0560 100644
--- a/Modules/readline.c
+++ b/Modules/readline.c
@@ -343,10 +343,8 @@ set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
Py_CLEAR(*hook_var);
}
else if (PyCallable_Check(function)) {
- PyObject *tmp = *hook_var;
Py_INCREF(function);
- *hook_var = function;
- Py_XDECREF(tmp);
+ Py_XSETREF(*hook_var, function);
}
else {
PyErr_Format(PyExc_TypeError,
@@ -604,6 +602,24 @@ PyDoc_STRVAR(doc_add_history,
"add_history(string) -> None\n\
add an item to the history buffer");
+static int should_auto_add_history = 1;
+
+/* Enable or disable automatic history */
+
+static PyObject *
+py_set_auto_history(PyObject *self, PyObject *args)
+{
+ if (!PyArg_ParseTuple(args, "p:set_auto_history",
+ &should_auto_add_history)) {
+ return NULL;
+ }
+ Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(doc_set_auto_history,
+"set_auto_history(enabled) -> None\n\
+Enables or disables automatic history.");
+
/* Get the tab-completion word-delimiters that readline uses */
@@ -822,6 +838,7 @@ static struct PyMethodDef readline_methods[] =
{"set_completer_delims", set_completer_delims,
METH_O, doc_set_completer_delims},
+ {"set_auto_history", py_set_auto_history, METH_VARARGS, doc_set_auto_history},
{"add_history", py_add_history, METH_O, doc_add_history},
{"remove_history_item", py_remove_history, METH_VARARGS, doc_remove_history},
{"replace_history_item", py_replace_history, METH_VARARGS, doc_replace_history},
@@ -857,7 +874,7 @@ on_hook(PyObject *func)
if (r == Py_None)
result = 0;
else {
- result = PyLong_AsLong(r);
+ result = _PyLong_AsInt(r);
if (result == -1 && PyErr_Occurred())
goto error;
}
@@ -1324,7 +1341,7 @@ call_readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
/* we have a valid line */
n = strlen(p);
- if (n > 0) {
+ if (should_auto_add_history && n > 0) {
const char *line;
int length = _py_get_history_length();
if (length > 0)
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index 00324a51bf..a6b63d22e5 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -1842,7 +1842,7 @@ kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds)
PyObject *pfd;
static char *kwlist[] = {"ident", "filter", "flags", "fflags",
"data", "udata", NULL};
- static char *fmt = "O|hHI" DATA_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent";
+ static const char fmt[] = "O|hHI" DATA_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent";
EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */
@@ -2127,7 +2127,7 @@ kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
if (_PyTime_FromSecondsObject(&timeout,
otimeout, _PyTime_ROUND_CEILING) < 0) {
PyErr_Format(PyExc_TypeError,
- "timeout argument must be an number "
+ "timeout argument must be a number "
"or None, got %.200s",
Py_TYPE(otimeout)->tp_name);
return NULL;
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 8f571a2eb4..f7fe218c45 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -163,7 +163,11 @@ if_indextoname(index) -- return the corresponding interface name\n\
# include <sys/uio.h>
#endif
-#ifndef WITH_THREAD
+#if !defined(WITH_THREAD)
+# undef HAVE_GETHOSTBYNAME_R
+#endif
+
+#if defined(__ANDROID__) && __ANDROID_API__ < 23
# undef HAVE_GETHOSTBYNAME_R
#endif
@@ -926,7 +930,7 @@ static PyThread_type_lock netdb_lock;
an error occurred; then an exception is raised. */
static int
-setipaddr(char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
+setipaddr(const char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int af)
{
struct addrinfo hints, *res;
int error;
@@ -1107,7 +1111,7 @@ makeipaddr(struct sockaddr *addr, int addrlen)
an error occurred. */
static int
-setbdaddr(char *name, bdaddr_t *bdaddr)
+setbdaddr(const char *name, bdaddr_t *bdaddr)
{
unsigned int b0, b1, b2, b3, b4, b5;
char ch;
@@ -1401,7 +1405,7 @@ static int
idna_converter(PyObject *obj, struct maybe_idna *data)
{
size_t len;
- PyObject *obj2, *obj3;
+ PyObject *obj2;
if (obj == NULL) {
idna_cleanup(data);
return 1;
@@ -1416,31 +1420,27 @@ idna_converter(PyObject *obj, struct maybe_idna *data)
data->buf = PyByteArray_AsString(obj);
len = PyByteArray_Size(obj);
}
- else if (PyUnicode_Check(obj) && PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) {
- data->buf = PyUnicode_DATA(obj);
- len = PyUnicode_GET_LENGTH(obj);
- }
- else {
- obj2 = PyUnicode_FromObject(obj);
- if (!obj2) {
- PyErr_Format(PyExc_TypeError, "string or unicode text buffer expected, not %s",
- obj->ob_type->tp_name);
- return 0;
- }
- obj3 = PyUnicode_AsEncodedString(obj2, "idna", NULL);
- Py_DECREF(obj2);
- if (!obj3) {
- PyErr_SetString(PyExc_TypeError, "encoding of hostname failed");
- return 0;
+ else if (PyUnicode_Check(obj)) {
+ if (PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) {
+ data->buf = PyUnicode_DATA(obj);
+ len = PyUnicode_GET_LENGTH(obj);
}
- if (!PyBytes_Check(obj3)) {
- Py_DECREF(obj3);
- PyErr_SetString(PyExc_TypeError, "encoding of hostname failed to return bytes");
- return 0;
+ else {
+ obj2 = PyUnicode_AsEncodedString(obj, "idna", NULL);
+ if (!obj2) {
+ PyErr_SetString(PyExc_TypeError, "encoding of hostname failed");
+ return 0;
+ }
+ assert(PyBytes_Check(obj2));
+ data->obj = obj2;
+ data->buf = PyBytes_AS_STRING(obj2);
+ len = PyBytes_GET_SIZE(obj2);
}
- data->obj = obj3;
- data->buf = PyBytes_AS_STRING(obj3);
- len = PyBytes_GET_SIZE(obj3);
+ }
+ else {
+ PyErr_Format(PyExc_TypeError, "str, bytes or bytearray expected, not %s",
+ obj->ob_type->tp_name);
+ return 0;
}
if (strlen(data->buf) != len) {
Py_CLEAR(data->obj);
@@ -2458,13 +2458,26 @@ sock_setsockopt(PySocketSockObject *s, PyObject *args)
if (!PyArg_ParseTuple(args, "iiy*:setsockopt",
&level, &optname, &optval))
return NULL;
+#ifdef MS_WINDOWS
+ if (optval.len > INT_MAX) {
+ PyBuffer_Release(&optval);
+ PyErr_Format(PyExc_OverflowError,
+ "socket option is larger than %i bytes",
+ INT_MAX);
+ return NULL;
+ }
+ res = setsockopt(s->sock_fd, level, optname,
+ optval.buf, (int)optval.len);
+#else
res = setsockopt(s->sock_fd, level, optname, optval.buf, optval.len);
+#endif
PyBuffer_Release(&optval);
}
- if (res < 0)
+ if (res < 0) {
return s->errorhandler();
- Py_INCREF(Py_None);
- return Py_None;
+ }
+
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(setsockopt_doc,
@@ -2563,16 +2576,22 @@ static PyObject *
sock_close(PySocketSockObject *s)
{
SOCKET_T fd;
+ int res;
- /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
- * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
- * for more details.
- */
- if ((fd = s->sock_fd) != -1) {
+ fd = s->sock_fd;
+ if (fd != -1) {
s->sock_fd = -1;
+
+ /* We do not want to retry upon EINTR: see
+ http://lwn.net/Articles/576478/ and
+ http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
+ for more details. */
Py_BEGIN_ALLOW_THREADS
- (void) SOCKETCLOSE(fd);
+ res = SOCKETCLOSE(fd);
Py_END_ALLOW_THREADS
+ if (res < 0) {
+ return s->errorhandler();
+ }
}
Py_INCREF(Py_None);
return Py_None;
@@ -4034,6 +4053,17 @@ sock_ioctl(PySocketSockObject *s, PyObject *arg)
return set_error();
}
return PyLong_FromUnsignedLong(recv); }
+#if defined(SIO_LOOPBACK_FAST_PATH)
+ case SIO_LOOPBACK_FAST_PATH: {
+ unsigned int option;
+ if (!PyArg_ParseTuple(arg, "kI:ioctl", &cmd, &option))
+ return NULL;
+ if (WSAIoctl(s->sock_fd, cmd, &option, sizeof(option),
+ NULL, 0, &recv, NULL, NULL) == SOCKET_ERROR) {
+ return set_error();
+ }
+ return PyLong_FromUnsignedLong(recv); }
+#endif
default:
PyErr_Format(PyExc_ValueError, "invalid ioctl command %d", cmd);
return NULL;
@@ -4044,7 +4074,8 @@ PyDoc_STRVAR(sock_ioctl_doc,
\n\
Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are\n\
SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.\n\
-SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval).");
+SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval).\n\
+SIO_LOOPBACK_FAST_PATH: 'option' is a boolean value, and is disabled by default");
#endif
#if defined(MS_WINDOWS)
@@ -4163,22 +4194,45 @@ static PyGetSetDef sock_getsetlist[] = {
First close the file description. */
static void
-sock_dealloc(PySocketSockObject *s)
+sock_finalize(PySocketSockObject *s)
{
+ SOCKET_T fd;
+ PyObject *error_type, *error_value, *error_traceback;
+
+ /* Save the current exception, if any. */
+ PyErr_Fetch(&error_type, &error_value, &error_traceback);
+
if (s->sock_fd != -1) {
- PyObject *exc, *val, *tb;
- Py_ssize_t old_refcount = Py_REFCNT(s);
- ++Py_REFCNT(s);
- PyErr_Fetch(&exc, &val, &tb);
- if (PyErr_WarnFormat(PyExc_ResourceWarning, 1,
- "unclosed %R", s))
+ if (PyErr_ResourceWarning((PyObject *)s, 1, "unclosed %R", s)) {
/* Spurious errors can appear at shutdown */
- if (PyErr_ExceptionMatches(PyExc_Warning))
- PyErr_WriteUnraisable((PyObject *) s);
- PyErr_Restore(exc, val, tb);
- (void) SOCKETCLOSE(s->sock_fd);
- Py_REFCNT(s) = old_refcount;
+ if (PyErr_ExceptionMatches(PyExc_Warning)) {
+ PyErr_WriteUnraisable((PyObject *)s);
+ }
+ }
+
+ /* Only close the socket *after* logging the ResourceWarning warning
+ to allow the logger to call socket methods like
+ socket.getsockname(). If the socket is closed before, socket
+ methods fails with the EBADF error. */
+ fd = s->sock_fd;
+ s->sock_fd = -1;
+
+ /* We do not want to retry upon EINTR: see sock_close() */
+ Py_BEGIN_ALLOW_THREADS
+ (void) SOCKETCLOSE(fd);
+ Py_END_ALLOW_THREADS
}
+
+ /* Restore the saved exception. */
+ PyErr_Restore(error_type, error_value, error_traceback);
+}
+
+static void
+sock_dealloc(PySocketSockObject *s)
+{
+ if (PyObject_CallFinalizerFromDealloc((PyObject *)s) < 0)
+ return;
+
Py_TYPE(s)->tp_free((PyObject *)s);
}
@@ -4395,7 +4449,8 @@ static PyTypeObject sock_type = {
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
+ | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
sock_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
@@ -4415,6 +4470,15 @@ static PyTypeObject sock_type = {
PyType_GenericAlloc, /* tp_alloc */
sock_new, /* tp_new */
PyObject_Del, /* tp_free */
+ 0, /* tp_is_gc */
+ 0, /* tp_bases */
+ 0, /* tp_mro */
+ 0, /* tp_cache */
+ 0, /* tp_subclasses */
+ 0, /* tp_weaklist */
+ 0, /* tp_del */
+ 0, /* tp_version_tag */
+ (destructor)sock_finalize, /* tp_finalize */
};
@@ -5450,10 +5514,6 @@ socket_inet_ntop(PyObject *self, PyObject *args)
} else {
return PyUnicode_FromString(retval);
}
-
- /* NOTREACHED */
- PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop");
- return NULL;
}
#elif defined(MS_WINDOWS)
@@ -6187,9 +6247,6 @@ PyInit__socket(void)
PyModule_AddIntMacro(m, AF_UNSPEC);
#endif
PyModule_AddIntMacro(m, AF_INET);
-#ifdef AF_INET6
- PyModule_AddIntMacro(m, AF_INET6);
-#endif /* AF_INET6 */
#if defined(AF_UNIX)
PyModule_AddIntMacro(m, AF_UNIX);
#endif /* AF_UNIX */
@@ -6481,9 +6538,11 @@ PyInit__socket(void)
#ifdef SO_OOBINLINE
PyModule_AddIntMacro(m, SO_OOBINLINE);
#endif
+#ifndef __GNU__
#ifdef SO_REUSEPORT
PyModule_AddIntMacro(m, SO_REUSEPORT);
#endif
+#endif
#ifdef SO_SNDBUF
PyModule_AddIntMacro(m, SO_SNDBUF);
#endif
@@ -7224,8 +7283,16 @@ PyInit__socket(void)
#ifdef SIO_RCVALL
{
- DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS};
- const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS"};
+ DWORD codes[] = {SIO_RCVALL, SIO_KEEPALIVE_VALS,
+#if defined(SIO_LOOPBACK_FAST_PATH)
+ SIO_LOOPBACK_FAST_PATH
+#endif
+ };
+ const char *names[] = {"SIO_RCVALL", "SIO_KEEPALIVE_VALS",
+#if defined(SIO_LOOPBACK_FAST_PATH)
+ "SIO_LOOPBACK_FAST_PATH"
+#endif
+ };
int i;
for(i = 0; i<Py_ARRAY_LENGTH(codes); ++i) {
PyObject *tmp;
diff --git a/Modules/spwdmodule.c b/Modules/spwdmodule.c
index 4b9f3cd9b7..556a715a24 100644
--- a/Modules/spwdmodule.c
+++ b/Modules/spwdmodule.c
@@ -137,7 +137,10 @@ spwd_getspnam_impl(PyObject *module, PyObject *arg)
if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
goto out;
if ((p = getspnam(name)) == NULL) {
- PyErr_SetString(PyExc_KeyError, "getspnam(): name not found");
+ if (errno != 0)
+ PyErr_SetFromErrno(PyExc_OSError);
+ else
+ PyErr_SetString(PyExc_KeyError, "getspnam(): name not found");
goto out;
}
retval = mkspent(p);
diff --git a/Modules/sre_lib.h b/Modules/sre_lib.h
index 128c71e340..78f7ac745e 100644
--- a/Modules/sre_lib.h
+++ b/Modules/sre_lib.h
@@ -367,14 +367,12 @@ SRE(info)(SRE_STATE* state, SRE_CODE* pattern)
#define RETURN_ON_FAILURE(i) \
do { RETURN_ON_ERROR(i); if (i == 0) RETURN_FAILURE; } while (0)
-#define SFY(x) #x
-
#define DATA_STACK_ALLOC(state, type, ptr) \
do { \
alloc_pos = state->data_stack_base; \
TRACE(("allocating %s in %" PY_FORMAT_SIZE_T "d " \
"(%" PY_FORMAT_SIZE_T "d)\n", \
- SFY(type), alloc_pos, sizeof(type))); \
+ Py_STRINGIFY(type), alloc_pos, sizeof(type))); \
if (sizeof(type) > state->data_stack_size - alloc_pos) { \
int j = data_stack_grow(state, sizeof(type)); \
if (j < 0) return j; \
@@ -387,7 +385,7 @@ do { \
#define DATA_STACK_LOOKUP_AT(state, type, ptr, pos) \
do { \
- TRACE(("looking up %s at %" PY_FORMAT_SIZE_T "d\n", SFY(type), pos)); \
+ TRACE(("looking up %s at %" PY_FORMAT_SIZE_T "d\n", Py_STRINGIFY(type), pos)); \
ptr = (type*)(state->data_stack+pos); \
} while (0)
@@ -1256,7 +1254,32 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
prefix, prefix_len, prefix_skip));
TRACE(("charset = %p\n", charset));
-#if defined(USE_FAST_SEARCH)
+ if (prefix_len == 1) {
+ /* pattern starts with a literal character */
+ SRE_CHAR c = (SRE_CHAR) prefix[0];
+#if SIZEOF_SRE_CHAR < 4
+ if ((SRE_CODE) c != prefix[0])
+ return 0; /* literal can't match: doesn't fit in char width */
+#endif
+ end = (SRE_CHAR *)state->end;
+ while (ptr < end) {
+ while (*ptr != c) {
+ if (++ptr >= end)
+ return 0;
+ }
+ TRACE(("|%p|%p|SEARCH LITERAL\n", pattern, ptr));
+ state->start = ptr;
+ state->ptr = ptr + prefix_skip;
+ if (flags & SRE_INFO_LITERAL)
+ return 1; /* we got all of it */
+ status = SRE(match)(state, pattern + 2*prefix_skip, 0);
+ if (status != 0)
+ return status;
+ ++ptr;
+ }
+ return 0;
+ }
+
if (prefix_len > 1) {
/* pattern starts with a known prefix. use the overlap
table to skip forward as fast as we possibly can */
@@ -1305,32 +1328,8 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
}
return 0;
}
-#endif
- if (pattern[0] == SRE_OP_LITERAL) {
- /* pattern starts with a literal character. this is used
- for short prefixes, and if fast search is disabled */
- SRE_CHAR c = (SRE_CHAR) pattern[1];
-#if SIZEOF_SRE_CHAR < 4
- if ((SRE_CODE) c != pattern[1])
- return 0; /* literal can't match: doesn't fit in char width */
-#endif
- end = (SRE_CHAR *)state->end;
- while (ptr < end) {
- while (*ptr != c) {
- if (++ptr >= end)
- return 0;
- }
- TRACE(("|%p|%p|SEARCH LITERAL\n", pattern, ptr));
- state->start = ptr;
- state->ptr = ++ptr;
- if (flags & SRE_INFO_LITERAL)
- return 1; /* we got all of it */
- status = SRE(match)(state, pattern + 2, 0);
- if (status != 0)
- break;
- }
- } else if (charset) {
+ if (charset) {
/* pattern starts with a character from a known set */
end = (SRE_CHAR *)state->end;
for (;;) {
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index d2caacdc6d..94746444cf 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -311,7 +311,7 @@ tmtotuple(struct tm *p)
Returns non-zero on success (parallels PyArg_ParseTuple).
*/
static int
-parse_time_t_args(PyObject *args, char *format, time_t *pwhen)
+parse_time_t_args(PyObject *args, const char *format, time_t *pwhen)
{
PyObject *ot = NULL;
time_t whent;
@@ -732,10 +732,10 @@ _asctime(struct tm *timeptr)
{
/* Inspired by Open Group reference implementation available at
* http://pubs.opengroup.org/onlinepubs/009695399/functions/asctime.html */
- static char wday_name[7][4] = {
+ static const char wday_name[7][4] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
- static char mon_name[12][4] = {
+ static const char mon_name[12][4] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
@@ -1034,6 +1034,7 @@ py_process_time(_Py_clock_info_t *info)
}
#endif
+ /* Currently, Python 3 requires clock() to build: see issue #22624 */
return floatclock(info);
#endif
}
diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c
index fe4e90822a..7d518fa257 100644
--- a/Modules/unicodedata.c
+++ b/Modules/unicodedata.c
@@ -884,7 +884,7 @@ _gethash(const char *s, int len, int scale)
return h;
}
-static char *hangul_syllables[][3] = {
+static const char * const hangul_syllables[][3] = {
{ "G", "A", "" },
{ "GG", "AE", "G" },
{ "N", "YA", "GG" },
@@ -1057,7 +1057,7 @@ find_syllable(const char *str, int *len, int *pos, int count, int column)
int i, len1;
*len = -1;
for (i = 0; i < count; i++) {
- char *s = hangul_syllables[i][column];
+ const char *s = hangul_syllables[i][column];
len1 = Py_SAFE_DOWNCAST(strlen(s), size_t, int);
if (len1 <= *len)
continue;
diff --git a/Modules/xxlimited.c b/Modules/xxlimited.c
index 40c176063d..c1a9be9b89 100644
--- a/Modules/xxlimited.c
+++ b/Modules/xxlimited.c
@@ -89,7 +89,7 @@ Xxo_getattro(XxoObject *self, PyObject *name)
}
static int
-Xxo_setattr(XxoObject *self, char *name, PyObject *v)
+Xxo_setattr(XxoObject *self, const char *name, PyObject *v)
{
if (self->x_attr == NULL) {
self->x_attr = PyDict_New();
diff --git a/Modules/xxmodule.c b/Modules/xxmodule.c
index 85230d9c97..076440703f 100644
--- a/Modules/xxmodule.c
+++ b/Modules/xxmodule.c
@@ -76,7 +76,7 @@ Xxo_getattro(XxoObject *self, PyObject *name)
}
static int
-Xxo_setattr(XxoObject *self, char *name, PyObject *v)
+Xxo_setattr(XxoObject *self, const char *name, PyObject *v)
{
if (self->x_attr == NULL) {
self->x_attr = PyDict_New();
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index ab5b65d264..28f7f151ac 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -57,7 +57,7 @@ typedef struct
} compobject;
static void
-zlib_error(z_stream zst, int err, char *msg)
+zlib_error(z_stream zst, int err, const char *msg)
{
const char *zmsg = Z_NULL;
/* In case of a version mismatch, zst.msg won't be initialized.
@@ -141,18 +141,18 @@ PyZlib_Free(voidpf ctx, void *ptr)
/*[clinic input]
zlib.compress
- bytes: Py_buffer
+ data: Py_buffer
Binary data to be compressed.
- level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
- Compression level, in 0-9.
/
+ level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
+ Compression level, in 0-9 or -1.
Returns a bytes object containing compressed data.
[clinic start generated code]*/
static PyObject *
-zlib_compress_impl(PyObject *module, Py_buffer *bytes, int level)
-/*[clinic end generated code: output=ae64c2c3076321a0 input=be3abe9934bda4b3]*/
+zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
+/*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
{
PyObject *ReturnVal = NULL;
Byte *input, *output = NULL;
@@ -160,13 +160,13 @@ zlib_compress_impl(PyObject *module, Py_buffer *bytes, int level)
int err;
z_stream zst;
- if ((size_t)bytes->len > UINT_MAX) {
+ if ((size_t)data->len > UINT_MAX) {
PyErr_SetString(PyExc_OverflowError,
"Size does not fit in an unsigned int");
goto error;
}
- input = bytes->buf;
- length = (unsigned int)bytes->len;
+ input = data->buf;
+ length = (unsigned int)data->len;
zst.avail_out = length + length/1000 + 12 + 1;
@@ -1352,7 +1352,7 @@ PyDoc_STRVAR(zlib_module_documentation,
"zlib library, which is based on GNU zip.\n"
"\n"
"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
-"compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
+"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
"compressobj([level[, ...]]) -- Return a compressor object.\n"
"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"