diff options
Diffstat (limited to 'Modules')
129 files changed, 6071 insertions, 6043 deletions
diff --git a/Modules/_bisectmodule.c b/Modules/_bisectmodule.c index 02b55d1..22ddbf2 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 e3e0eb1..f4077fa 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 9b1194e..4e75995 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 10fbcfe..3410dfe 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 b428279..7a78541 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) @@ -518,15 +518,13 @@ static PyTypeObject Dialect_Type = { static PyObject * _call_dialect(PyObject *dialect_inst, PyObject *kwargs) { - PyObject *ctor_args; - PyObject *dialect; - - ctor_args = Py_BuildValue(dialect_inst ? "(O)" : "()", dialect_inst); - if (ctor_args == NULL) - return NULL; - dialect = PyObject_Call((PyObject *)&Dialect_Type, ctor_args, kwargs); - Py_DECREF(ctor_args); - return dialect; + PyObject *type = (PyObject *)&Dialect_Type; + if (dialect_inst) { + return _PyObject_FastCallDict(type, &dialect_inst, 1, kwargs); + } + else { + return _PyObject_FastCallDict(type, NULL, 0, kwargs); + } } /* @@ -1648,7 +1646,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 b20039f..cd5b4aa 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 91413d7..b958f30 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 30e3a96..a276fcd 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 0d3f724..b06ba8a 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 228f497..18ef335 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 f419cb2..7e95af7 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -9,6 +9,12 @@ #ifdef MS_WINDOWS # include <winsock2.h> /* struct timeval */ +static struct tm *localtime_r(const time_t *timep, struct tm *result) +{ + if (localtime_s(result, timep) == 0) + return result; + return NULL; +} #endif /* Differentiate between building the core module and building extension @@ -56,6 +62,7 @@ class datetime.datetime "PyDateTime_DateTime *" "&PyDateTime_DateTimeType" #define DATE_GET_MINUTE PyDateTime_DATE_GET_MINUTE #define DATE_GET_SECOND PyDateTime_DATE_GET_SECOND #define DATE_GET_MICROSECOND PyDateTime_DATE_GET_MICROSECOND +#define DATE_GET_FOLD PyDateTime_DATE_GET_FOLD /* Date accessors for date and datetime. */ #define SET_YEAR(o, v) (((o)->data[0] = ((v) & 0xff00) >> 8), \ @@ -71,12 +78,14 @@ class datetime.datetime "PyDateTime_DateTime *" "&PyDateTime_DateTimeType" (((o)->data[7] = ((v) & 0xff0000) >> 16), \ ((o)->data[8] = ((v) & 0x00ff00) >> 8), \ ((o)->data[9] = ((v) & 0x0000ff))) +#define DATE_SET_FOLD(o, v) (PyDateTime_DATE_GET_FOLD(o) = (v)) /* Time accessors for time. */ #define TIME_GET_HOUR PyDateTime_TIME_GET_HOUR #define TIME_GET_MINUTE PyDateTime_TIME_GET_MINUTE #define TIME_GET_SECOND PyDateTime_TIME_GET_SECOND #define TIME_GET_MICROSECOND PyDateTime_TIME_GET_MICROSECOND +#define TIME_GET_FOLD PyDateTime_TIME_GET_FOLD #define TIME_SET_HOUR(o, v) (PyDateTime_TIME_GET_HOUR(o) = (v)) #define TIME_SET_MINUTE(o, v) (PyDateTime_TIME_GET_MINUTE(o) = (v)) #define TIME_SET_SECOND(o, v) (PyDateTime_TIME_GET_SECOND(o) = (v)) @@ -84,6 +93,7 @@ class datetime.datetime "PyDateTime_DateTime *" "&PyDateTime_DateTimeType" (((o)->data[3] = ((v) & 0xff0000) >> 16), \ ((o)->data[4] = ((v) & 0x00ff00) >> 8), \ ((o)->data[5] = ((v) & 0x0000ff))) +#define TIME_SET_FOLD(o, v) (PyDateTime_TIME_GET_FOLD(o) = (v)) /* Delta accessors for timedelta. */ #define GET_TD_DAYS(o) (((PyDateTime_Delta *)(o))->days) @@ -184,12 +194,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 }; @@ -417,7 +427,7 @@ check_date_args(int year, int month, int day) * aren't, raise ValueError and return -1. */ static int -check_time_args(int h, int m, int s, int us) +check_time_args(int h, int m, int s, int us, int fold) { if (h < 0 || h > 23) { PyErr_SetString(PyExc_ValueError, @@ -439,6 +449,11 @@ check_time_args(int h, int m, int s, int us) "microsecond must be in 0..999999"); return -1; } + if (fold != 0 && fold != 1) { + PyErr_SetString(PyExc_ValueError, + "fold must be either 0 or 1"); + return -1; + } return 0; } @@ -674,8 +689,8 @@ new_date_ex(int year, int month, int day, PyTypeObject *type) /* Create a datetime instance with no range checking. */ static PyObject * -new_datetime_ex(int year, int month, int day, int hour, int minute, - int second, int usecond, PyObject *tzinfo, PyTypeObject *type) +new_datetime_ex2(int year, int month, int day, int hour, int minute, + int second, int usecond, PyObject *tzinfo, int fold, PyTypeObject *type) { PyDateTime_DateTime *self; char aware = tzinfo != Py_None; @@ -692,18 +707,27 @@ new_datetime_ex(int year, int month, int day, int hour, int minute, Py_INCREF(tzinfo); self->tzinfo = tzinfo; } + DATE_SET_FOLD(self, fold); } return (PyObject *)self; } -#define new_datetime(y, m, d, hh, mm, ss, us, tzinfo) \ - new_datetime_ex(y, m, d, hh, mm, ss, us, tzinfo, \ +static PyObject * +new_datetime_ex(int year, int month, int day, int hour, int minute, + int second, int usecond, PyObject *tzinfo, PyTypeObject *type) +{ + return new_datetime_ex2(year, month, day, hour, minute, second, usecond, + tzinfo, 0, type); +} + +#define new_datetime(y, m, d, hh, mm, ss, us, tzinfo, fold) \ + new_datetime_ex2(y, m, d, hh, mm, ss, us, tzinfo, fold, \ &PyDateTime_DateTimeType) /* Create a time instance with no range checking. */ static PyObject * -new_time_ex(int hour, int minute, int second, int usecond, - PyObject *tzinfo, PyTypeObject *type) +new_time_ex2(int hour, int minute, int second, int usecond, + PyObject *tzinfo, int fold, PyTypeObject *type) { PyDateTime_Time *self; char aware = tzinfo != Py_None; @@ -720,12 +744,20 @@ new_time_ex(int hour, int minute, int second, int usecond, Py_INCREF(tzinfo); self->tzinfo = tzinfo; } + TIME_SET_FOLD(self, fold); } return (PyObject *)self; } -#define new_time(hh, mm, ss, us, tzinfo) \ - new_time_ex(hh, mm, ss, us, tzinfo, &PyDateTime_TimeType) +static PyObject * +new_time_ex(int hour, int minute, int second, int usecond, + PyObject *tzinfo, PyTypeObject *type) +{ + return new_time_ex2(hour, minute, second, usecond, tzinfo, 0, type); +} + +#define new_time(hh, mm, ss, us, tzinfo, fold) \ + new_time_ex2(hh, mm, ss, us, tzinfo, fold, &PyDateTime_TimeType) /* Create a timedelta instance. Normalize the members iff normalize is * true. Passing false is a speed optimization, if you know for sure @@ -873,7 +905,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; @@ -887,10 +919,10 @@ call_tzinfo_method(PyObject *tzinfo, char *name, PyObject *tzinfoarg) if (offset == Py_None || offset == NULL) return offset; if (PyDelta_Check(offset)) { - if (GET_TD_MICROSECONDS(offset) != 0 || GET_TD_SECONDS(offset) % 60 != 0) { + if (GET_TD_MICROSECONDS(offset) != 0) { Py_DECREF(offset); PyErr_Format(PyExc_ValueError, "offset must be a timedelta" - " representing a whole number of minutes"); + " representing a whole number of seconds"); return NULL; } if ((GET_TD_DAYS(offset) == -1 && GET_TD_SECONDS(offset) == 0) || @@ -1002,6 +1034,30 @@ append_keyword_tzinfo(PyObject *repr, PyObject *tzinfo) return repr; } +/* repr is like "someclass(arg1, arg2)". If fold isn't 0, + * stuff + * ", fold=" + repr(tzinfo) + * before the closing ")". + */ +static PyObject * +append_keyword_fold(PyObject *repr, int fold) +{ + PyObject *temp; + + assert(PyUnicode_Check(repr)); + if (fold == 0) + return repr; + /* Get rid of the trailing ')'. */ + assert(PyUnicode_READ_CHAR(repr, PyUnicode_GET_LENGTH(repr)-1) == ')'); + temp = PyUnicode_Substring(repr, 0, PyUnicode_GET_LENGTH(repr) - 1); + Py_DECREF(repr); + if (temp == NULL) + return NULL; + repr = PyUnicode_FromFormat("%U, fold=%d)", temp, fold); + Py_DECREF(temp); + return repr; +} + /* --------------------------------------------------------------------------- * String format helpers. */ @@ -1009,10 +1065,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 +1113,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; } @@ -1072,10 +1126,11 @@ format_utcoffset(char *buf, size_t buflen, const char *sep, Py_DECREF(offset); minutes = divmod(seconds, 60, &seconds); hours = divmod(minutes, 60, &minutes); - assert(seconds == 0); - /* XXX ignore sub-minute data, currently not allowed. */ - PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); - + if (seconds == 0) + PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); + else + PyOS_snprintf(buf, buflen, "%c%02d%s%02d%s%02d", sign, hours, + sep, minutes, sep, seconds); return 0; } @@ -2307,7 +2362,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 +2941,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 +3102,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; } @@ -3080,37 +3133,34 @@ Fail: static PyObject * tzinfo_reduce(PyObject *self) { - PyObject *args, *state, *tmp; + PyObject *args, *state; PyObject *getinitargs, *getstate; _Py_IDENTIFIER(__getinitargs__); _Py_IDENTIFIER(__getstate__); - tmp = PyTuple_New(0); - if (tmp == NULL) - return NULL; - getinitargs = _PyObject_GetAttrId(self, &PyId___getinitargs__); if (getinitargs != NULL) { - args = PyObject_CallObject(getinitargs, tmp); + args = _PyObject_CallNoArg(getinitargs); Py_DECREF(getinitargs); if (args == NULL) { - Py_DECREF(tmp); return NULL; } } else { PyErr_Clear(); - args = tmp; - Py_INCREF(args); + + args = PyTuple_New(0); + if (args == NULL) { + return NULL; + } } getstate = _PyObject_GetAttrId(self, &PyId___getstate__); if (getstate != NULL) { - state = PyObject_CallObject(getstate, tmp); + state = _PyObject_CallNoArg(getstate); Py_DECREF(getstate); if (state == NULL) { Py_DECREF(args); - Py_DECREF(tmp); return NULL; } } @@ -3119,13 +3169,12 @@ tzinfo_reduce(PyObject *self) PyErr_Clear(); state = Py_None; dictptr = _PyObject_GetDictPtr(self); - if (dictptr && *dictptr && PyDict_Size(*dictptr)) + if (dictptr && *dictptr && PyDict_Size(*dictptr)) { state = *dictptr; + } Py_INCREF(state); } - Py_DECREF(tmp); - if (state == Py_None) { Py_DECREF(state); return Py_BuildValue("(ON)", Py_TYPE(self), args); @@ -3155,7 +3204,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 +3336,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 +3436,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 = { @@ -3466,12 +3520,19 @@ time_tzinfo(PyDateTime_Time *self, void *unused) return result; } +static PyObject * +time_fold(PyDateTime_Time *self, void *unused) +{ + return PyLong_FromLong(TIME_GET_FOLD(self)); +} + static PyGetSetDef time_getset[] = { {"hour", (getter)time_hour}, {"minute", (getter)time_minute}, {"second", (getter)py_time_second}, {"microsecond", (getter)time_microsecond}, - {"tzinfo", (getter)time_tzinfo}, + {"tzinfo", (getter)time_tzinfo}, + {"fold", (getter)time_fold}, {NULL} }; @@ -3480,7 +3541,7 @@ static PyGetSetDef time_getset[] = { */ static char *time_kws[] = {"hour", "minute", "second", "microsecond", - "tzinfo", NULL}; + "tzinfo", "fold", NULL}; static PyObject * time_new(PyTypeObject *type, PyObject *args, PyObject *kw) @@ -3492,13 +3553,14 @@ time_new(PyTypeObject *type, PyObject *args, PyObject *kw) int second = 0; int usecond = 0; PyObject *tzinfo = Py_None; + int fold = 0; /* Check for invocation from pickle with __getstate__ state */ if (PyTuple_GET_SIZE(args) >= 1 && PyTuple_GET_SIZE(args) <= 2 && PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && PyBytes_GET_SIZE(state) == _PyDateTime_TIME_DATASIZE && - ((unsigned char) (PyBytes_AS_STRING(state)[0])) < 24) + (0x7F & ((unsigned char) (PyBytes_AS_STRING(state)[0]))) < 24) { PyDateTime_Time *me; char aware; @@ -3523,19 +3585,26 @@ time_new(PyTypeObject *type, PyObject *args, PyObject *kw) Py_INCREF(tzinfo); me->tzinfo = tzinfo; } + if (pdata[0] & (1 << 7)) { + me->data[0] -= 128; + me->fold = 1; + } + else { + me->fold = 0; + } } return (PyObject *)me; } - if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO", time_kws, + if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i", time_kws, &hour, &minute, &second, &usecond, - &tzinfo)) { - if (check_time_args(hour, minute, second, usecond) < 0) + &tzinfo, &fold)) { + if (check_time_args(hour, minute, second, usecond, fold) < 0) return NULL; if (check_tzinfo_subclass(tzinfo) < 0) return NULL; - self = new_time_ex(hour, minute, second, usecond, tzinfo, - type); + self = new_time_ex2(hour, minute, second, usecond, tzinfo, fold, + type); } return self; } @@ -3585,6 +3654,7 @@ time_repr(PyDateTime_Time *self) int m = TIME_GET_MINUTE(self); int s = TIME_GET_SECOND(self); int us = TIME_GET_MICROSECOND(self); + int fold = TIME_GET_FOLD(self); PyObject *result = NULL; if (us) @@ -3597,6 +3667,8 @@ time_repr(PyDateTime_Time *self) result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m); if (result != NULL && HASTZINFO(self)) result = append_keyword_tzinfo(result, self->tzinfo); + if (result != NULL && fold) + result = append_keyword_fold(result, fold); return result; } @@ -3607,23 +3679,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, ×pec)) + 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; @@ -3750,9 +3855,23 @@ static Py_hash_t time_hash(PyDateTime_Time *self) { if (self->hashcode == -1) { - PyObject *offset; - - offset = time_utcoffset((PyObject *)self, NULL); + PyObject *offset, *self0; + if (DATE_GET_FOLD(self)) { + self0 = new_time_ex2(DATE_GET_HOUR(self), + DATE_GET_MINUTE(self), + DATE_GET_SECOND(self), + DATE_GET_MICROSECOND(self), + HASTZINFO(self) ? self->tzinfo : Py_None, + 0, Py_TYPE(self)); + if (self0 == NULL) + return -1; + } + else { + self0 = (PyObject *)self; + Py_INCREF(self0); + } + offset = time_utcoffset(self0, NULL); + Py_DECREF(self0); if (offset == NULL) return -1; @@ -3798,15 +3917,24 @@ time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) int ss = TIME_GET_SECOND(self); int us = TIME_GET_MICROSECOND(self); PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; + int fold = TIME_GET_FOLD(self); - if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO:replace", + if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO$i:replace", time_kws, - &hh, &mm, &ss, &us, &tzinfo)) + &hh, &mm, &ss, &us, &tzinfo, &fold)) return NULL; tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo); if (tuple == NULL) return NULL; clone = time_new(Py_TYPE(self), tuple, NULL); + if (clone != NULL) { + if (fold != 0 && fold != 1) { + PyErr_SetString(PyExc_ValueError, + "fold must be either 0 or 1"); + return NULL; + } + TIME_SET_FOLD(clone, fold); + } Py_DECREF(tuple); return clone; } @@ -3819,7 +3947,7 @@ time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) * __getstate__ isn't exposed. */ static PyObject * -time_getstate(PyDateTime_Time *self) +time_getstate(PyDateTime_Time *self, int proto) { PyObject *basestate; PyObject *result = NULL; @@ -3827,6 +3955,9 @@ time_getstate(PyDateTime_Time *self) basestate = PyBytes_FromStringAndSize((char *)self->data, _PyDateTime_TIME_DATASIZE); if (basestate != NULL) { + if (proto > 3 && TIME_GET_FOLD(self)) + /* Set the first bit of the first byte */ + PyBytes_AS_STRING(basestate)[0] |= (1 << 7); if (! HASTZINFO(self) || self->tzinfo == Py_None) result = PyTuple_Pack(1, basestate); else @@ -3837,16 +3968,21 @@ time_getstate(PyDateTime_Time *self) } static PyObject * -time_reduce(PyDateTime_Time *self, PyObject *arg) +time_reduce(PyDateTime_Time *self, PyObject *args) { - return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self)); + int proto = 0; + if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) + return NULL; + + return Py_BuildValue("(ON)", Py_TYPE(self), time_getstate(self, proto)); } 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.")}, @@ -3866,13 +4002,13 @@ static PyMethodDef time_methods[] = { {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS, PyDoc_STR("Return time with new specified fields.")}, - {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS, - PyDoc_STR("__reduce__() -> (cls, state)")}, + {"__reduce_ex__", (PyCFunction)time_reduce, METH_VARARGS, + PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, {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\ @@ -3960,12 +4096,19 @@ datetime_tzinfo(PyDateTime_DateTime *self, void *unused) return result; } +static PyObject * +datetime_fold(PyDateTime_DateTime *self, void *unused) +{ + return PyLong_FromLong(DATE_GET_FOLD(self)); +} + static PyGetSetDef datetime_getset[] = { {"hour", (getter)datetime_hour}, {"minute", (getter)datetime_minute}, {"second", (getter)datetime_second}, {"microsecond", (getter)datetime_microsecond}, - {"tzinfo", (getter)datetime_tzinfo}, + {"tzinfo", (getter)datetime_tzinfo}, + {"fold", (getter)datetime_fold}, {NULL} }; @@ -3975,7 +4118,7 @@ static PyGetSetDef datetime_getset[] = { static char *datetime_kws[] = { "year", "month", "day", "hour", "minute", "second", - "microsecond", "tzinfo", NULL + "microsecond", "tzinfo", "fold", NULL }; static PyObject * @@ -3990,6 +4133,7 @@ datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) int minute = 0; int second = 0; int usecond = 0; + int fold = 0; PyObject *tzinfo = Py_None; /* Check for invocation from pickle with __getstate__ state */ @@ -3997,7 +4141,7 @@ datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) PyTuple_GET_SIZE(args) <= 2 && PyBytes_Check(state = PyTuple_GET_ITEM(args, 0)) && PyBytes_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE && - MONTH_IS_SANE(PyBytes_AS_STRING(state)[2])) + MONTH_IS_SANE(PyBytes_AS_STRING(state)[2] & 0x7F)) { PyDateTime_DateTime *me; char aware; @@ -4022,22 +4166,29 @@ datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) Py_INCREF(tzinfo); me->tzinfo = tzinfo; } + if (pdata[2] & (1 << 7)) { + me->data[2] -= 128; + me->fold = 1; + } + else { + me->fold = 0; + } } return (PyObject *)me; } - if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO", datetime_kws, + if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO$i", datetime_kws, &year, &month, &day, &hour, &minute, - &second, &usecond, &tzinfo)) { + &second, &usecond, &tzinfo, &fold)) { if (check_date_args(year, month, day) < 0) return NULL; - if (check_time_args(hour, minute, second, usecond) < 0) + if (check_time_args(hour, minute, second, usecond, fold) < 0) return NULL; if (check_tzinfo_subclass(tzinfo) < 0) return NULL; - self = new_datetime_ex(year, month, day, + self = new_datetime_ex2(year, month, day, hour, minute, second, usecond, - tzinfo, type); + tzinfo, fold, type); } return self; } @@ -4045,6 +4196,45 @@ datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw) /* TM_FUNC is the shared type of localtime() and gmtime(). */ typedef struct tm *(*TM_FUNC)(const time_t *timer); +/* As of version 2015f max fold in IANA database is + * 23 hours at 1969-09-30 13:00:00 in Kwajalein. */ +static PY_LONG_LONG max_fold_seconds = 24 * 3600; +/* NB: date(1970,1,1).toordinal() == 719163 */ +static PY_LONG_LONG epoch = Py_LL(719163) * 24 * 60 * 60; + +static PY_LONG_LONG +utc_to_seconds(int year, int month, int day, + int hour, int minute, int second) +{ + PY_LONG_LONG ordinal = ymd_to_ord(year, month, day); + return ((ordinal * 24 + hour) * 60 + minute) * 60 + second; +} + +static PY_LONG_LONG +local(PY_LONG_LONG u) +{ + struct tm local_time; + time_t t; + u -= epoch; + t = u; + if (t != u) { + PyErr_SetString(PyExc_OverflowError, + "timestamp out of range for platform time_t"); + return -1; + } + /* XXX: add bounds checking */ + if (localtime_r(&t, &local_time) == NULL) { + PyErr_SetFromErrno(PyExc_OSError); + return -1; + } + return utc_to_seconds(local_time.tm_year + 1900, + local_time.tm_mon + 1, + local_time.tm_mday, + local_time.tm_hour, + local_time.tm_min, + local_time.tm_sec); +} + /* Internal helper. * Build datetime from a time_t and a distinct count of microseconds. * Pass localtime or gmtime for f, to control the interpretation of timet. @@ -4054,6 +4244,7 @@ datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us, PyObject *tzinfo) { struct tm *tm; + int year, month, day, hour, minute, second, fold = 0; tm = f(&timet); if (tm == NULL) { @@ -4064,61 +4255,40 @@ datetime_from_timet_and_us(PyObject *cls, TM_FUNC f, time_t timet, int us, return PyErr_SetFromErrno(PyExc_OSError); } + year = tm->tm_year + 1900; + month = tm->tm_mon + 1; + day = tm->tm_mday; + hour = tm->tm_hour; + minute = tm->tm_min; /* The platform localtime/gmtime may insert leap seconds, * indicated by tm->tm_sec > 59. We don't care about them, * except to the extent that passing them on to the datetime * constructor would raise ValueError for a reason that * made no sense to the user. */ - if (tm->tm_sec > 59) - tm->tm_sec = 59; - return PyObject_CallFunction(cls, "iiiiiiiO", - tm->tm_year + 1900, - tm->tm_mon + 1, - tm->tm_mday, - tm->tm_hour, - tm->tm_min, - tm->tm_sec, - 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; -} + second = Py_MIN(59, tm->tm_sec); -/* 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; + if (tzinfo == Py_None && f == localtime) { + PY_LONG_LONG probe_seconds, result_seconds, transition; + + result_seconds = utc_to_seconds(year, month, day, + hour, minute, second); + /* Probe max_fold_seconds to detect a fold. */ + probe_seconds = local(epoch + timet - max_fold_seconds); + if (probe_seconds == -1) + return NULL; + transition = result_seconds - probe_seconds - max_fold_seconds; + if (transition < 0) { + probe_seconds = local(epoch + timet + transition); + if (probe_seconds == -1) + return NULL; + if (probe_seconds == result_seconds) + fold = 1; + } + } + return new_datetime_ex2(year, month, day, hour, + minute, second, us, tzinfo, fold, + (PyTypeObject *)cls); } /* Internal helper. @@ -4129,32 +4299,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 +4360,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 +4379,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, ×tamp, &tzinfo)) return NULL; if (check_tzinfo_subclass(tzinfo) < 0) @@ -4243,10 +4395,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 +4404,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", ×tamp)) + if (PyArg_ParseTuple(args, "O:utcfromtimestamp", ×tamp)) result = datetime_from_timestamp(cls, gmtime, timestamp, Py_None); return result; @@ -4288,27 +4437,32 @@ datetime_strptime(PyObject *cls, PyObject *args) static PyObject * datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) { - static char *keywords[] = {"date", "time", NULL}; + static char *keywords[] = {"date", "time", "tzinfo", NULL}; PyObject *date; PyObject *time; + PyObject *tzinfo = NULL; PyObject *result = NULL; - if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!:combine", keywords, + if (PyArg_ParseTupleAndKeywords(args, kw, "O!O!|O:combine", keywords, &PyDateTime_DateType, &date, - &PyDateTime_TimeType, &time)) { - PyObject *tzinfo = Py_None; - - if (HASTZINFO(time)) - tzinfo = ((PyDateTime_Time *)time)->tzinfo; + &PyDateTime_TimeType, &time, &tzinfo)) { + if (tzinfo == NULL) { + if (HASTZINFO(time)) + tzinfo = ((PyDateTime_Time *)time)->tzinfo; + else + tzinfo = Py_None; + } result = PyObject_CallFunction(cls, "iiiiiiiO", - GET_YEAR(date), - GET_MONTH(date), - GET_DAY(date), - TIME_GET_HOUR(time), - TIME_GET_MINUTE(time), - TIME_GET_SECOND(time), - TIME_GET_MICROSECOND(time), - tzinfo); + GET_YEAR(date), + GET_MONTH(date), + GET_DAY(date), + TIME_GET_HOUR(time), + TIME_GET_MINUTE(time), + TIME_GET_SECOND(time), + TIME_GET_MICROSECOND(time), + tzinfo); + if (result) + DATE_SET_FOLD(result, TIME_GET_FOLD(time)); } return result; } @@ -4376,7 +4530,7 @@ add_datetime_timedelta(PyDateTime_DateTime *date, PyDateTime_Delta *delta, else return new_datetime(year, month, day, hour, minute, second, microsecond, - HASTZINFO(date) ? date->tzinfo : Py_None); + HASTZINFO(date) ? date->tzinfo : Py_None, 0); } static PyObject * @@ -4469,9 +4623,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); } } @@ -4521,6 +4673,8 @@ datetime_repr(PyDateTime_DateTime *self) GET_YEAR(self), GET_MONTH(self), GET_DAY(self), DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); } + if (baserepr != NULL && DATE_GET_FOLD(self) != 0) + baserepr = append_keyword_fold(baserepr, DATE_GET_FOLD(self)); if (baserepr == NULL || ! HASTZINFO(self)) return baserepr; return append_keyword_tzinfo(baserepr, self->tzinfo); @@ -4536,25 +4690,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, ×pec)) 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; @@ -4581,6 +4765,70 @@ datetime_ctime(PyDateTime_DateTime *self) /* Miscellaneous methods. */ static PyObject * +flip_fold(PyObject *dt) +{ + return new_datetime_ex2(GET_YEAR(dt), + GET_MONTH(dt), + GET_DAY(dt), + DATE_GET_HOUR(dt), + DATE_GET_MINUTE(dt), + DATE_GET_SECOND(dt), + DATE_GET_MICROSECOND(dt), + HASTZINFO(dt) ? + ((PyDateTime_DateTime *)dt)->tzinfo : Py_None, + !DATE_GET_FOLD(dt), + Py_TYPE(dt)); +} + +static PyObject * +get_flip_fold_offset(PyObject *dt) +{ + PyObject *result, *flip_dt; + + flip_dt = flip_fold(dt); + if (flip_dt == NULL) + return NULL; + result = datetime_utcoffset(flip_dt, NULL); + Py_DECREF(flip_dt); + return result; +} + +/* PEP 495 exception: Whenever one or both of the operands in + * inter-zone comparison is such that its utcoffset() depends + * on the value of its fold fold attribute, the result is False. + * + * Return 1 if exception applies, 0 if not, and -1 on error. + */ +static int +pep495_eq_exception(PyObject *self, PyObject *other, + PyObject *offset_self, PyObject *offset_other) +{ + int result = 0; + PyObject *flip_offset; + + flip_offset = get_flip_fold_offset(self); + if (flip_offset == NULL) + return -1; + if (flip_offset != offset_self && + delta_cmp(flip_offset, offset_self)) + { + result = 1; + goto done; + } + Py_DECREF(flip_offset); + + flip_offset = get_flip_fold_offset(other); + if (flip_offset == NULL) + return -1; + if (flip_offset != offset_other && + delta_cmp(flip_offset, offset_other)) + result = 1; + done: + Py_DECREF(flip_offset); + return result; +} + +static PyObject * datetime_richcompare(PyObject *self, PyObject *other, int op) { PyObject *result = NULL; @@ -4627,6 +4875,13 @@ datetime_richcompare(PyObject *self, PyObject *other, int op) diff = memcmp(((PyDateTime_DateTime *)self)->data, ((PyDateTime_DateTime *)other)->data, _PyDateTime_DATETIME_DATASIZE); + if ((op == Py_EQ || op == Py_NE) && diff == 0) { + int ex = pep495_eq_exception(self, other, offset1, offset2); + if (ex == -1) + goto done; + if (ex) + diff = 1; + } result = diff_to_bool(diff, op); } else if (offset1 != Py_None && offset2 != Py_None) { @@ -4642,6 +4897,13 @@ datetime_richcompare(PyObject *self, PyObject *other, int op) diff = GET_TD_SECONDS(delta) | GET_TD_MICROSECONDS(delta); Py_DECREF(delta); + if ((op == Py_EQ || op == Py_NE) && diff == 0) { + int ex = pep495_eq_exception(self, other, offset1, offset2); + if (ex == -1) + goto done; + if (ex) + diff = 1; + } result = diff_to_bool(diff, op); } else if (op == Py_EQ) { @@ -4667,9 +4929,26 @@ static Py_hash_t datetime_hash(PyDateTime_DateTime *self) { if (self->hashcode == -1) { - PyObject *offset; - - offset = datetime_utcoffset((PyObject *)self, NULL); + PyObject *offset, *self0; + if (DATE_GET_FOLD(self)) { + self0 = new_datetime_ex2(GET_YEAR(self), + GET_MONTH(self), + GET_DAY(self), + DATE_GET_HOUR(self), + DATE_GET_MINUTE(self), + DATE_GET_SECOND(self), + DATE_GET_MICROSECOND(self), + HASTZINFO(self) ? self->tzinfo : Py_None, + 0, Py_TYPE(self)); + if (self0 == NULL) + return -1; + } + else { + self0 = (PyObject *)self; + Py_INCREF(self0); + } + offset = datetime_utcoffset(self0, NULL); + Py_DECREF(self0); if (offset == NULL) return -1; @@ -4723,76 +5002,78 @@ datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) int ss = DATE_GET_SECOND(self); int us = DATE_GET_MICROSECOND(self); PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None; + int fold = DATE_GET_FOLD(self); - if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO:replace", + if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO$i:replace", datetime_kws, &y, &m, &d, &hh, &mm, &ss, &us, - &tzinfo)) + &tzinfo, &fold)) return NULL; tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo); if (tuple == NULL) return NULL; clone = datetime_new(Py_TYPE(self), tuple, NULL); + + if (clone != NULL) { + if (fold != 0 && fold != 1) { + PyErr_SetString(PyExc_ValueError, + "fold must be either 0 or 1"); + return NULL; + } + DATE_SET_FOLD(clone, fold); + } Py_DECREF(tuple); return clone; } static PyObject * -local_timezone(PyDateTime_DateTime *utc_time) +local_timezone_from_timestamp(time_t timestamp) { PyObject *result = NULL; - struct tm *timep; - time_t timestamp; PyObject *delta; - PyObject *one_second; - PyObject *seconds; + struct tm *local_time_tm; PyObject *nameo = NULL; const char *zone = NULL; - delta = new_delta(ymd_to_ord(GET_YEAR(utc_time), GET_MONTH(utc_time), - GET_DAY(utc_time)) - 719163, - 60 * (60 * DATE_GET_HOUR(utc_time) + - DATE_GET_MINUTE(utc_time)) + - DATE_GET_SECOND(utc_time), - 0, 0); - if (delta == NULL) - return NULL; - one_second = new_delta(0, 1, 0, 0); - if (one_second == NULL) - goto error; - seconds = divide_timedelta_timedelta((PyDateTime_Delta *)delta, - (PyDateTime_Delta *)one_second); - Py_DECREF(one_second); - if (seconds == NULL) - goto error; - Py_DECREF(delta); - timestamp = PyLong_AsLong(seconds); - Py_DECREF(seconds); - if (timestamp == -1 && PyErr_Occurred()) - return NULL; - timep = localtime(×tamp); + local_time_tm = localtime(×tamp); #ifdef HAVE_STRUCT_TM_TM_ZONE - zone = timep->tm_zone; - delta = new_delta(0, timep->tm_gmtoff, 0, 1); + zone = local_time_tm->tm_zone; + delta = new_delta(0, local_time_tm->tm_gmtoff, 0, 1); #else /* HAVE_STRUCT_TM_TM_ZONE */ { - PyObject *local_time; - local_time = new_datetime(timep->tm_year + 1900, timep->tm_mon + 1, - timep->tm_mday, timep->tm_hour, timep->tm_min, - timep->tm_sec, DATE_GET_MICROSECOND(utc_time), - utc_time->tzinfo); - if (local_time == NULL) - goto error; - delta = datetime_subtract(local_time, (PyObject*)utc_time); - /* XXX: before relying on tzname, we should compare delta - to the offset implied by timezone/altzone */ - if (daylight && timep->tm_isdst >= 0) - zone = tzname[timep->tm_isdst % 2]; - else - zone = tzname[0]; + PyObject *local_time, *utc_time; + struct tm *utc_time_tm; + char buf[100]; + strftime(buf, sizeof(buf), "%Z", local_time_tm); + zone = buf; + local_time = new_datetime(local_time_tm->tm_year + 1900, + local_time_tm->tm_mon + 1, + local_time_tm->tm_mday, + local_time_tm->tm_hour, + local_time_tm->tm_min, + local_time_tm->tm_sec, 0, Py_None, 0); + if (local_time == NULL) { + return NULL; + } + utc_time_tm = gmtime(×tamp); + utc_time = new_datetime(utc_time_tm->tm_year + 1900, + utc_time_tm->tm_mon + 1, + utc_time_tm->tm_mday, + utc_time_tm->tm_hour, + utc_time_tm->tm_min, + utc_time_tm->tm_sec, 0, Py_None, 0); + if (utc_time == NULL) { + Py_DECREF(local_time); + return NULL; + } + delta = datetime_subtract(local_time, utc_time); Py_DECREF(local_time); + Py_DECREF(utc_time); } #endif /* HAVE_STRUCT_TM_TM_ZONE */ + if (delta == NULL) { + return NULL; + } if (zone != NULL) { nameo = PyUnicode_DecodeLocale(zone, "surrogateescape"); if (nameo == NULL) @@ -4805,12 +5086,65 @@ local_timezone(PyDateTime_DateTime *utc_time) return result; } +static PyObject * +local_timezone(PyDateTime_DateTime *utc_time) +{ + time_t timestamp; + PyObject *delta; + PyObject *one_second; + PyObject *seconds; + + delta = datetime_subtract((PyObject *)utc_time, PyDateTime_Epoch); + if (delta == NULL) + return NULL; + one_second = new_delta(0, 1, 0, 0); + if (one_second == NULL) { + Py_DECREF(delta); + return NULL; + } + seconds = divide_timedelta_timedelta((PyDateTime_Delta *)delta, + (PyDateTime_Delta *)one_second); + Py_DECREF(one_second); + Py_DECREF(delta); + if (seconds == NULL) + return NULL; + timestamp = _PyLong_AsTime_t(seconds); + Py_DECREF(seconds); + if (timestamp == -1 && PyErr_Occurred()) + return NULL; + return local_timezone_from_timestamp(timestamp); +} + +static PY_LONG_LONG +local_to_seconds(int year, int month, int day, + int hour, int minute, int second, int fold); + +static PyObject * +local_timezone_from_local(PyDateTime_DateTime *local_dt) +{ + PY_LONG_LONG seconds; + time_t timestamp; + seconds = local_to_seconds(GET_YEAR(local_dt), + GET_MONTH(local_dt), + GET_DAY(local_dt), + DATE_GET_HOUR(local_dt), + DATE_GET_MINUTE(local_dt), + DATE_GET_SECOND(local_dt), + DATE_GET_FOLD(local_dt)); + if (seconds == -1) + return NULL; + /* XXX: add bounds check */ + timestamp = seconds - epoch; + return local_timezone_from_timestamp(timestamp); +} + static PyDateTime_DateTime * datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) { PyDateTime_DateTime *result; PyObject *offset; PyObject *temp; + PyObject *self_tzinfo; PyObject *tzinfo = Py_None; static char *keywords[] = {"tz", NULL}; @@ -4821,27 +5155,27 @@ datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) if (check_tzinfo_subclass(tzinfo) == -1) return NULL; - if (!HASTZINFO(self) || self->tzinfo == Py_None) - goto NeedAware; + if (!HASTZINFO(self) || self->tzinfo == Py_None) { + self_tzinfo = local_timezone_from_local(self); + if (self_tzinfo == NULL) + return NULL; + } else { + self_tzinfo = self->tzinfo; + Py_INCREF(self_tzinfo); + } /* Conversion to self's own time zone is a NOP. */ - if (self->tzinfo == tzinfo) { + if (self_tzinfo == tzinfo) { + Py_DECREF(self_tzinfo); Py_INCREF(self); return self; } /* Convert self to UTC. */ - offset = datetime_utcoffset((PyObject *)self, NULL); + offset = call_utcoffset(self_tzinfo, (PyObject *)self); + Py_DECREF(self_tzinfo); if (offset == NULL) return NULL; - if (offset == Py_None) { - Py_DECREF(offset); - NeedAware: - PyErr_SetString(PyExc_ValueError, "astimezone() cannot be applied to " - "a naive datetime"); - return NULL; - } - /* result = self - offset */ result = (PyDateTime_DateTime *)add_datetime_timedelta(self, (PyDateTime_Delta *)offset, -1); @@ -4849,6 +5183,32 @@ datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw) if (result == NULL) return NULL; + /* Make sure result is aware and UTC. */ + if (!HASTZINFO(result)) { + temp = (PyObject *)result; + result = (PyDateTime_DateTime *) + new_datetime_ex2(GET_YEAR(result), + GET_MONTH(result), + GET_DAY(result), + DATE_GET_HOUR(result), + DATE_GET_MINUTE(result), + DATE_GET_SECOND(result), + DATE_GET_MICROSECOND(result), + PyDateTime_TimeZone_UTC, + DATE_GET_FOLD(result), + Py_TYPE(result)); + Py_DECREF(temp); + if (result == NULL) + return NULL; + } + else { + /* Result is already aware - just replace tzinfo. */ + temp = result->tzinfo; + result->tzinfo = PyDateTime_TimeZone_UTC; + Py_INCREF(result->tzinfo); + Py_DECREF(temp); + } + /* Attach new tzinfo and let fromutc() do the rest. */ temp = result->tzinfo; if (tzinfo == Py_None) { @@ -4896,6 +5256,56 @@ datetime_timetuple(PyDateTime_DateTime *self) dstflag); } +static PY_LONG_LONG +local_to_seconds(int year, int month, int day, + int hour, int minute, int second, int fold) +{ + PY_LONG_LONG t, a, b, u1, u2, t1, t2, lt; + t = utc_to_seconds(year, month, day, hour, minute, second); + /* Our goal is to solve t = local(u) for u. */ + lt = local(t); + if (lt == -1) + return -1; + a = lt - t; + u1 = t - a; + t1 = local(u1); + if (t1 == -1) + return -1; + if (t1 == t) { + /* We found one solution, but it may not be the one we need. + * Look for an earlier solution (if `fold` is 0), or a + * later one (if `fold` is 1). */ + if (fold) + u2 = u1 + max_fold_seconds; + else + u2 = u1 - max_fold_seconds; + lt = local(u2); + if (lt == -1) + return -1; + b = lt - u2; + if (a == b) + return u1; + } + else { + b = t1 - u1; + assert(a != b); + } + u2 = t - b; + t2 = local(u2); + if (t2 == -1) + return -1; + if (t2 == t) + return u2; + if (t1 == t) + return u1; + /* We have found both offsets a and b, but neither t - a nor t - b is + * a solution. This means t is in the gap. */ + return fold?Py_MIN(u1, u2):Py_MAX(u1, u2); +} + +/* date(1970,1,1).toordinal() == 719163 */ +#define EPOCH_SECONDS (719163LL * 24 * 60 * 60) + static PyObject * datetime_timestamp(PyDateTime_DateTime *self) { @@ -4910,33 +5320,18 @@ datetime_timestamp(PyDateTime_DateTime *self) Py_DECREF(delta); } else { - struct tm time; - time_t timestamp; - memset((void *) &time, '\0', sizeof(struct tm)); - time.tm_year = GET_YEAR(self) - 1900; - time.tm_mon = GET_MONTH(self) - 1; - time.tm_mday = GET_DAY(self); - time.tm_hour = DATE_GET_HOUR(self); - time.tm_min = DATE_GET_MINUTE(self); - time.tm_sec = DATE_GET_SECOND(self); - time.tm_wday = -1; - time.tm_isdst = -1; - timestamp = mktime(&time); - if (timestamp == (time_t)(-1) -#ifndef _AIX - /* Return value of -1 does not necessarily mean an error, - * but tm_wday cannot remain set to -1 if mktime succeeded. */ - && time.tm_wday == -1 -#else - /* on AIX, tm_wday is always sets, even on error */ -#endif - ) - { - PyErr_SetString(PyExc_OverflowError, - "timestamp out of range"); + PY_LONG_LONG seconds; + seconds = local_to_seconds(GET_YEAR(self), + GET_MONTH(self), + GET_DAY(self), + DATE_GET_HOUR(self), + DATE_GET_MINUTE(self), + DATE_GET_SECOND(self), + DATE_GET_FOLD(self)); + if (seconds == -1) return NULL; - } - result = PyFloat_FromDouble(timestamp + DATE_GET_MICROSECOND(self) / 1e6); + result = PyFloat_FromDouble(seconds - EPOCH_SECONDS + + DATE_GET_MICROSECOND(self) / 1e6); } return result; } @@ -4956,7 +5351,8 @@ datetime_gettime(PyDateTime_DateTime *self) DATE_GET_MINUTE(self), DATE_GET_SECOND(self), DATE_GET_MICROSECOND(self), - Py_None); + Py_None, + DATE_GET_FOLD(self)); } static PyObject * @@ -4966,7 +5362,8 @@ datetime_gettimetz(PyDateTime_DateTime *self) DATE_GET_MINUTE(self), DATE_GET_SECOND(self), DATE_GET_MICROSECOND(self), - GET_DT_TZINFO(self)); + GET_DT_TZINFO(self), + DATE_GET_FOLD(self)); } static PyObject * @@ -5018,7 +5415,7 @@ datetime_utctimetuple(PyDateTime_DateTime *self) * __getstate__ isn't exposed. */ static PyObject * -datetime_getstate(PyDateTime_DateTime *self) +datetime_getstate(PyDateTime_DateTime *self, int proto) { PyObject *basestate; PyObject *result = NULL; @@ -5026,6 +5423,9 @@ datetime_getstate(PyDateTime_DateTime *self) basestate = PyBytes_FromStringAndSize((char *)self->data, _PyDateTime_DATETIME_DATASIZE); if (basestate != NULL) { + if (proto > 3 && DATE_GET_FOLD(self)) + /* Set the first bit of the third byte */ + PyBytes_AS_STRING(basestate)[2] |= (1 << 7); if (! HASTZINFO(self) || self->tzinfo == Py_None) result = PyTuple_Pack(1, basestate); else @@ -5036,9 +5436,13 @@ datetime_getstate(PyDateTime_DateTime *self) } static PyObject * -datetime_reduce(PyDateTime_DateTime *self, PyObject *arg) +datetime_reduce(PyDateTime_DateTime *self, PyObject *args) { - return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self)); + int proto = 0; + if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) + return NULL; + + return Py_BuildValue("(ON)", Py_TYPE(self), datetime_getstate(self, proto)); } static PyMethodDef datetime_methods[] = { @@ -5093,9 +5497,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).")}, @@ -5112,13 +5519,13 @@ static PyMethodDef datetime_methods[] = { {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS, PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, - {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS, - PyDoc_STR("__reduce__() -> (cls, state)")}, + {"__reduce_ex__", (PyCFunction)datetime_reduce, METH_VARARGS, + PyDoc_STR("__reduce_ex__(proto) -> (cls, state)")}, {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\ @@ -5201,7 +5608,9 @@ static PyDateTime_CAPI CAPI = { new_time_ex, new_delta_ex, datetime_fromtimestamp, - date_fromtimestamp + date_fromtimestamp, + new_datetime_ex2, + new_time_ex2 }; @@ -5282,12 +5691,12 @@ PyInit__datetime(void) /* time values */ d = PyDateTime_TimeType.tp_dict; - x = new_time(0, 0, 0, 0, Py_None); + x = new_time(0, 0, 0, 0, Py_None, 0); if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) return NULL; Py_DECREF(x); - x = new_time(23, 59, 59, 999999, Py_None); + x = new_time(23, 59, 59, 999999, Py_None, 0); if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) return NULL; Py_DECREF(x); @@ -5300,12 +5709,12 @@ PyInit__datetime(void) /* datetime values */ d = PyDateTime_DateTimeType.tp_dict; - x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None); + x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None, 0); if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) return NULL; Py_DECREF(x); - x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None); + x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None, 0); if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) return NULL; Py_DECREF(x); @@ -5347,7 +5756,7 @@ PyInit__datetime(void) /* Epoch */ PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0, - PyDateTime_TimeZone_UTC); + PyDateTime_TimeZone_UTC, 0); if (PyDateTime_Epoch == NULL) return NULL; @@ -5382,19 +5791,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 ea8790b..804978a 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 e15941a..3ba8e35 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 71029a9..f7fd6e7 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 35de6b8..dfe1523 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 a45a429..3aadfb0 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 0f41fe5..a854e09 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 9c98d1a..efd7119 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 593f9f5..328ab92 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 5ca7413..56e4887 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 ab7d5bd..f907531 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 85ffca2..721293a 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,53 @@ _elementtree_Element___deepcopy__(ElementObject *self, PyObject *memo) return NULL; } +LOCAL(PyObject *) +deepcopy(PyObject *object, PyObject *memo) +{ + /* do a deep copy of the given object */ + elementtreestate *st; + PyObject *stack[2]; + + /* 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; + } + + stack[0] = object; + stack[1] = memo; + return _PyObject_FastCall(st->deepcopy_obj, stack, 2); +} + + /*[clinic input] _elementtree.Element.__sizeof__ -> Py_ssize_t @@ -1892,92 +1916,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 +2019,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 +2042,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 +2058,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 +2069,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 +2104,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 +2240,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 +2253,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 +2291,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 +2326,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 +2345,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 +2372,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 +2453,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 +2489,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 +2958,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 +2968,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 +3049,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 +3072,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 +3561,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 +3571,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 +3589,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 +3725,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 +3761,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 +3775,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/_functoolsmodule.c b/Modules/_functoolsmodule.c index d785c49..848a03c 100644 --- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -128,44 +128,60 @@ partial_call(partialobject *pto, PyObject *args, PyObject *kw) { PyObject *ret; PyObject *argappl, *kwappl; + PyObject **stack; + Py_ssize_t nargs; assert (PyCallable_Check(pto->fn)); assert (PyTuple_Check(pto->args)); assert (PyDict_Check(pto->kw)); if (PyTuple_GET_SIZE(pto->args) == 0) { - argappl = args; - Py_INCREF(args); - } else if (PyTuple_GET_SIZE(args) == 0) { - argappl = pto->args; - Py_INCREF(pto->args); - } else { + stack = &PyTuple_GET_ITEM(args, 0); + nargs = PyTuple_GET_SIZE(args); + argappl = NULL; + } + else if (PyTuple_GET_SIZE(args) == 0) { + stack = &PyTuple_GET_ITEM(pto->args, 0); + nargs = PyTuple_GET_SIZE(pto->args); + argappl = NULL; + } + else { + stack = NULL; argappl = PySequence_Concat(pto->args, args); - if (argappl == NULL) + if (argappl == NULL) { return NULL; + } + assert(PyTuple_Check(argappl)); } if (PyDict_Size(pto->kw) == 0) { kwappl = kw; Py_XINCREF(kwappl); - } else { + } + else { kwappl = PyDict_Copy(pto->kw); if (kwappl == NULL) { - Py_DECREF(argappl); + Py_XDECREF(argappl); return NULL; } + if (kw != NULL) { if (PyDict_Merge(kwappl, kw, 1) != 0) { - Py_DECREF(argappl); + Py_XDECREF(argappl); Py_DECREF(kwappl); return NULL; } } } - ret = PyObject_Call(pto->fn, argappl, kwappl); - Py_DECREF(argappl); + if (stack) { + ret = _PyObject_FastCallDict(pto->fn, stack, nargs, kwappl); + } + else { + ret = PyObject_Call(pto->fn, argappl, kwappl); + Py_DECREF(argappl); + } Py_XDECREF(kwappl); return ret; } @@ -461,12 +477,12 @@ static PyObject * keyobject_richcompare(PyObject *ko, PyObject *other, int op) { PyObject *res; - PyObject *args; PyObject *x; PyObject *y; PyObject *compare; PyObject *answer; static PyObject *zero; + PyObject* stack[2]; if (zero == NULL) { zero = PyLong_FromLong(0); @@ -490,17 +506,13 @@ keyobject_richcompare(PyObject *ko, PyObject *other, int op) /* Call the user's comparison function and translate the 3-way * result into true or false (or error). */ - args = PyTuple_New(2); - if (args == NULL) - return NULL; - Py_INCREF(x); - Py_INCREF(y); - PyTuple_SET_ITEM(args, 0, x); - PyTuple_SET_ITEM(args, 1, y); - res = PyObject_Call(compare, args, NULL); - Py_DECREF(args); - if (res == NULL) + stack[0] = x; + stack[1] = y; + res = _PyObject_FastCall(compare, stack, 2); + if (res == NULL) { return NULL; + } + answer = PyObject_RichCompare(res, zero, op); Py_DECREF(res); return answer; diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c index b840bf5..daae71d 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 136abf5..b499e1f 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 65c955a..ec700f6 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 0c6eae2..3c48ff3 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 6d67751..cbe7425 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 9e5d78b..a1ba121 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 6c88e32..50891c5 100644 --- a/Modules/_io/clinic/_iomodule.c.h +++ b/Modules/_io/clinic/_iomodule.c.h @@ -138,7 +138,8 @@ static PyObject * _io_open(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"file", "mode", "buffering", "encoding", "errors", "newline", "closefd", "opener", NULL}; + static const char * const _keywords[] = {"file", "mode", "buffering", "encoding", "errors", "newline", "closefd", "opener", NULL}; + static _PyArg_Parser _parser = {"O|sizzziO:open", _keywords, 0}; PyObject *file; const char *mode = "r"; int buffering = -1; @@ -148,12 +149,13 @@ _io_open(PyObject *module, PyObject *args, PyObject *kwargs) int closefd = 1; PyObject *opener = Py_None; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|sizzziO:open", _keywords, - &file, &mode, &buffering, &encoding, &errors, &newline, &closefd, &opener)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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=14769629391a3130 input=a9049054013a1b77]*/ diff --git a/Modules/_io/clinic/bufferedio.c.h b/Modules/_io/clinic/bufferedio.c.h index 437e275..58144a4 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: @@ -310,13 +324,15 @@ static int _io_BufferedReader___init__(PyObject *self, PyObject *args, PyObject *kwargs) { int return_value = -1; - static char *_keywords[] = {"raw", "buffer_size", NULL}; + static const char * const _keywords[] = {"raw", "buffer_size", NULL}; + static _PyArg_Parser _parser = {"O|n:BufferedReader", _keywords, 0}; PyObject *raw; Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|n:BufferedReader", _keywords, - &raw, &buffer_size)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &raw, &buffer_size)) { goto exit; + } return_value = _io_BufferedReader___init___impl((buffered *)self, raw, buffer_size); exit: @@ -341,13 +357,15 @@ static int _io_BufferedWriter___init__(PyObject *self, PyObject *args, PyObject *kwargs) { int return_value = -1; - static char *_keywords[] = {"raw", "buffer_size", NULL}; + static const char * const _keywords[] = {"raw", "buffer_size", NULL}; + static _PyArg_Parser _parser = {"O|n:BufferedWriter", _keywords, 0}; PyObject *raw; Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|n:BufferedWriter", _keywords, - &raw, &buffer_size)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &raw, &buffer_size)) { goto exit; + } return_value = _io_BufferedWriter___init___impl((buffered *)self, raw, buffer_size); exit: @@ -371,14 +389,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 +430,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: @@ -439,16 +461,18 @@ static int _io_BufferedRandom___init__(PyObject *self, PyObject *args, PyObject *kwargs) { int return_value = -1; - static char *_keywords[] = {"raw", "buffer_size", NULL}; + static const char * const _keywords[] = {"raw", "buffer_size", NULL}; + static _PyArg_Parser _parser = {"O|n:BufferedRandom", _keywords, 0}; PyObject *raw; Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|n:BufferedRandom", _keywords, - &raw, &buffer_size)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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=a956f394ecde4cf9 input=a9049054013a1b77]*/ diff --git a/Modules/_io/clinic/bytesio.c.h b/Modules/_io/clinic/bytesio.c.h index 5f2abb0..c64ce5c 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: @@ -408,15 +415,17 @@ static int _io_BytesIO___init__(PyObject *self, PyObject *args, PyObject *kwargs) { int return_value = -1; - static char *_keywords[] = {"initial_bytes", NULL}; + static const char * const _keywords[] = {"initial_bytes", NULL}; + static _PyArg_Parser _parser = {"|O:BytesIO", _keywords, 0}; PyObject *initvalue = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:BytesIO", _keywords, - &initvalue)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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=6382e8eb578eea64 input=a9049054013a1b77]*/ diff --git a/Modules/_io/clinic/fileio.c.h b/Modules/_io/clinic/fileio.c.h index 1042008..908fe0f 100644 --- a/Modules/_io/clinic/fileio.c.h +++ b/Modules/_io/clinic/fileio.c.h @@ -49,15 +49,17 @@ static int _io_FileIO___init__(PyObject *self, PyObject *args, PyObject *kwargs) { int return_value = -1; - static char *_keywords[] = {"file", "mode", "closefd", "opener", NULL}; + static const char * const _keywords[] = {"file", "mode", "closefd", "opener", NULL}; + static _PyArg_Parser _parser = {"O|siO:FileIO", _keywords, 0}; PyObject *nameobj; const char *mode = "r"; int closefd = 1; PyObject *opener = Py_None; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|siO:FileIO", _keywords, - &nameobj, &mode, &closefd, &opener)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &nameobj, &mode, &closefd, &opener)) { goto exit; + } return_value = _io_FileIO___init___impl((fileio *)self, nameobj, mode, closefd, opener); exit: @@ -154,14 +156,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 +214,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 +245,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 +287,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 +341,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 +373,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=51924bc0ee11d58e input=a9049054013a1b77]*/ diff --git a/Modules/_io/clinic/iobase.c.h b/Modules/_io/clinic/iobase.c.h index 9762f11..edbf73a 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 a8e32a3..d2c05d7 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: @@ -217,13 +221,15 @@ static int _io_StringIO___init__(PyObject *self, PyObject *args, PyObject *kwargs) { int return_value = -1; - static char *_keywords[] = {"initial_value", "newline", NULL}; + static const char * const _keywords[] = {"initial_value", "newline", NULL}; + static _PyArg_Parser _parser = {"|OO:StringIO", _keywords, 0}; PyObject *value = NULL; PyObject *newline_obj = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO:StringIO", _keywords, - &value, &newline_obj)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &value, &newline_obj)) { goto exit; + } return_value = _io_StringIO___init___impl((stringio *)self, value, newline_obj); exit: @@ -283,4 +289,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=5dd5c2a213e75405 input=a9049054013a1b77]*/ diff --git a/Modules/_io/clinic/textio.c.h b/Modules/_io/clinic/textio.c.h index dc7e8c7..6c40819 100644 --- a/Modules/_io/clinic/textio.c.h +++ b/Modules/_io/clinic/textio.c.h @@ -24,14 +24,16 @@ static int _io_IncrementalNewlineDecoder___init__(PyObject *self, PyObject *args, PyObject *kwargs) { int return_value = -1; - static char *_keywords[] = {"decoder", "translate", "errors", NULL}; + static const char * const _keywords[] = {"decoder", "translate", "errors", NULL}; + static _PyArg_Parser _parser = {"Oi|O:IncrementalNewlineDecoder", _keywords, 0}; PyObject *decoder; int translate; PyObject *errors = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|O:IncrementalNewlineDecoder", _keywords, - &decoder, &translate, &errors)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &decoder, &translate, &errors)) { goto exit; + } return_value = _io_IncrementalNewlineDecoder___init___impl((nldecoder_object *)self, decoder, translate, errors); exit: @@ -54,13 +56,15 @@ static PyObject * _io_IncrementalNewlineDecoder_decode(nldecoder_object *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"input", "final", NULL}; + static const char * const _keywords[] = {"input", "final", NULL}; + static _PyArg_Parser _parser = {"O|i:decode", _keywords, 0}; PyObject *input; int final = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:decode", _keywords, - &input, &final)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &input, &final)) { goto exit; + } return_value = _io_IncrementalNewlineDecoder_decode_impl(self, input, final); exit: @@ -153,7 +157,8 @@ static int _io_TextIOWrapper___init__(PyObject *self, PyObject *args, PyObject *kwargs) { int return_value = -1; - static char *_keywords[] = {"buffer", "encoding", "errors", "newline", "line_buffering", "write_through", NULL}; + static const char * const _keywords[] = {"buffer", "encoding", "errors", "newline", "line_buffering", "write_through", NULL}; + static _PyArg_Parser _parser = {"O|zzzii:TextIOWrapper", _keywords, 0}; PyObject *buffer; const char *encoding = NULL; const char *errors = NULL; @@ -161,9 +166,10 @@ _io_TextIOWrapper___init__(PyObject *self, PyObject *args, PyObject *kwargs) int line_buffering = 0; int write_through = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|zzzii:TextIOWrapper", _keywords, - &buffer, &encoding, &errors, &newline, &line_buffering, &write_through)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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 +210,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 +237,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 +264,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 +292,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 +337,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 +464,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=7ec624a9bf6393f5 input=a9049054013a1b77]*/ diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index 919cf50..12e5156 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 212b0dd..f07a0ca 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 06b4144..ecf6dc1 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 063caa6..96c8e7b 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 f82af34..d3dbf98 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 b1d6add..a92fb10 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 66e534f..ccfb513 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 bc01ffe..ce4e17f 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 4ae638e..d92a8bf 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/_operator.c b/Modules/_operator.c index dfff1d2..fb8eafc 100644 --- a/Modules/_operator.c +++ b/Modules/_operator.c @@ -1111,6 +1111,8 @@ methodcaller_reduce(methodcallerobject *mc) PyObject *functools; PyObject *partial; PyObject *constructor; + PyObject *newargs[2]; + _Py_IDENTIFIER(partial); functools = PyImport_ImportModule("functools"); if (!functools) @@ -1119,17 +1121,11 @@ methodcaller_reduce(methodcallerobject *mc) Py_DECREF(functools); if (!partial) return NULL; - newargs = PyTuple_New(2); - if (newargs == NULL) { - Py_DECREF(partial); - return NULL; - } - Py_INCREF(Py_TYPE(mc)); - PyTuple_SET_ITEM(newargs, 0, (PyObject *)Py_TYPE(mc)); - Py_INCREF(mc->name); - PyTuple_SET_ITEM(newargs, 1, mc->name); - constructor = PyObject_Call(partial, newargs, mc->kwds); - Py_DECREF(newargs); + + newargs[0] = (PyObject *)Py_TYPE(mc); + newargs[1] = mc->name; + constructor = _PyObject_FastCallDict(partial, newargs, 2, mc->kwds); + Py_DECREF(partial); return Py_BuildValue("NO", constructor, mc->args); } diff --git a/Modules/_pickle.c b/Modules/_pickle.c index 3c21b6a..f029ed6 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; } @@ -331,43 +345,20 @@ static PyObject * _Pickle_FastCall(PyObject *func, PyObject *obj) { PyObject *result; - PyObject *arg_tuple = PyTuple_New(1); - - /* Note: this function used to reuse the argument tuple. This used to give - a slight performance boost with older pickle implementations where many - unbuffered reads occurred (thus needing many function calls). - However, this optimization was removed because it was too complicated - to get right. It abused the C API for tuples to mutate them which led - to subtle reference counting and concurrency bugs. Furthermore, the - introduction of protocol 4 and the prefetching optimization via peek() - significantly reduced the number of function calls we do. Thus, the - benefits became marginal at best. */ - - if (arg_tuple == NULL) { - Py_DECREF(obj); - return NULL; - } - PyTuple_SET_ITEM(arg_tuple, 0, obj); - result = PyObject_Call(func, arg_tuple, NULL); - Py_CLEAR(arg_tuple); + result = _PyObject_CallArg1(func, obj); + Py_DECREF(obj); return result; } /*************************************************************************/ -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 +389,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 +408,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 +444,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 +462,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 +495,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 +866,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); @@ -1134,9 +1140,7 @@ _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) return -1; if (n == READ_WHOLE_LINE) { - PyObject *empty_tuple = PyTuple_New(0); - data = PyObject_Call(self->readline, empty_tuple, NULL); - Py_DECREF(empty_tuple); + data = _PyObject_CallNoArg(self->readline); } else { PyObject *len; @@ -1174,21 +1178,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 +1191,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 +1211,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 +2107,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 +2147,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 +2165,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 +3550,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 +3596,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) { @@ -3877,10 +3937,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save) /* Check for a __reduce__ method. */ reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__); if (reduce_func != NULL) { - PyObject *empty_tuple = PyTuple_New(0); - reduce_value = PyObject_Call(reduce_func, empty_tuple, - NULL); - Py_DECREF(empty_tuple); + reduce_value = _PyObject_CallNoArg(reduce_func); } else { PyErr_Format(st->PicklingError, @@ -4397,7 +4454,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 +4483,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 +4494,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 +4577,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 +5050,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 +5132,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 +5206,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 +5529,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 +5559,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 +5745,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 +5774,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 +5793,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 +5812,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 +5827,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 +5877,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 +5900,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 +5954,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 +6010,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 +6147,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 +6231,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 +6937,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 +6949,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 8bedab5..0ca0aa5 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 95ad4a4..fd6b230 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 66b6e34..68be458 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 3689a4e..62c5893 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 fcc1695..1841c768 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; } /* @@ -860,8 +859,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; } } @@ -892,8 +890,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; } } @@ -1028,8 +1025,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; } } @@ -1058,8 +1054,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) @@ -1086,8 +1081,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 @@ -1110,8 +1104,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; } } @@ -1134,8 +1127,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 @@ -1625,7 +1617,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 e1676de..98eb961 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++) { @@ -698,7 +697,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); @@ -913,8 +914,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; @@ -995,8 +995,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) @@ -1012,8 +1011,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[] = { @@ -1049,7 +1047,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 7a7e860..7cd6d2a 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 09b5835..0a62f62 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; @@ -1059,7 +1056,6 @@ pattern_subx(PatternObject* self, PyObject* ptemplate, PyObject* string, PyObject* joiner; PyObject* item; PyObject* filter; - PyObject* args; PyObject* match; void* ptr; Py_ssize_t status; @@ -1161,13 +1157,7 @@ pattern_subx(PatternObject* self, PyObject* ptemplate, PyObject* string, match = pattern_new_match(self, &state, 1); if (!match) goto error; - args = PyTuple_Pack(1, match); - if (!args) { - Py_DECREF(match); - goto error; - } - item = PyObject_CallObject(filter, args); - Py_DECREF(args); + item = _PyObject_CallArg1(filter, match); Py_DECREF(match); if (!item) goto error; @@ -2052,8 +2042,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 391034e..e2c4386 100644 --- a/Modules/_ssl.c +++ b/Modules/_ssl.c @@ -382,7 +382,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; @@ -464,7 +464,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 f965541..df81900 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 43db8a8..13d3ccc 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 3810e94..5d661f7 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 41da997..4daa34e 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 316f932..42771e3 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 796ac0f..e3329c7 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 edc6cf4..91d4f01 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" @@ -486,7 +486,7 @@ _winapi_CreateJunction_impl(PyObject *module, LPWSTR src_path, const USHORT prefix_len = 4; USHORT print_len = 0; USHORT rdb_size = 0; - PREPARSE_DATA_BUFFER rdb = NULL; + _Py_PREPARSE_DATA_BUFFER rdb = NULL; /* Junction point creation */ HANDLE junction = NULL; @@ -542,18 +542,18 @@ _winapi_CreateJunction_impl(PyObject *module, LPWSTR src_path, - the size of the print name in bytes - the size of the substitute name in bytes - the size of two NUL terminators in bytes */ - rdb_size = REPARSE_DATA_BUFFER_HEADER_SIZE + + rdb_size = _Py_REPARSE_DATA_BUFFER_HEADER_SIZE + sizeof(rdb->MountPointReparseBuffer) - sizeof(rdb->MountPointReparseBuffer.PathBuffer) + /* Two +1's for NUL terminators. */ (prefix_len + print_len + 1 + print_len + 1) * sizeof(WCHAR); - rdb = (PREPARSE_DATA_BUFFER)PyMem_RawMalloc(rdb_size); + rdb = (_Py_PREPARSE_DATA_BUFFER)PyMem_RawMalloc(rdb_size); if (rdb == NULL) goto cleanup; memset(rdb, 0, rdb_size); rdb->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT; - rdb->ReparseDataLength = rdb_size - REPARSE_DATA_BUFFER_HEADER_SIZE; + rdb->ReparseDataLength = rdb_size - _Py_REPARSE_DATA_BUFFER_HEADER_SIZE; rdb->MountPointReparseBuffer.SubstituteNameOffset = 0; rdb->MountPointReparseBuffer.SubstituteNameLength = (prefix_len + print_len) * sizeof(WCHAR); @@ -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 b9f87ae..4d9a23f 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; @@ -846,37 +846,10 @@ array_repeat(arrayobject *a, Py_ssize_t n) } static int -array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) +array_del_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh) { char *item; - Py_ssize_t n; /* Size of replacement array */ Py_ssize_t d; /* Change in size */ -#define b ((arrayobject *)v) - if (v == NULL) - n = 0; - else if (array_Check(v)) { - n = Py_SIZE(b); - if (a == b) { - /* Special case "a[i:j] = a" -- copy b first */ - int ret; - v = array_slice(b, 0, n); - if (!v) - return -1; - ret = array_ass_slice(a, ilow, ihigh, v); - Py_DECREF(v); - return ret; - } - if (b->ob_descr != a->ob_descr) { - PyErr_BadArgument(); - return -1; - } - } - else { - PyErr_Format(PyExc_TypeError, - "can only assign array (not \"%.200s\") to array slice", - Py_TYPE(v)->tp_name); - return -1; - } if (ilow < 0) ilow = 0; else if (ilow > Py_SIZE(a)) @@ -888,7 +861,7 @@ array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) else if (ihigh > Py_SIZE(a)) ihigh = Py_SIZE(a); item = a->ob_item; - d = n - (ihigh-ilow); + d = ihigh-ilow; /* Issue #4509: If the array has exported buffers and the slice assignment would change the size of the array, fail early to make sure we don't modify it. */ @@ -897,25 +870,14 @@ array_ass_slice(arrayobject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) "cannot resize an array that is exporting buffers"); return -1; } - if (d < 0) { /* Delete -d items */ - memmove(item + (ihigh+d)*a->ob_descr->itemsize, + if (d > 0) { /* Delete d items */ + memmove(item + (ihigh-d)*a->ob_descr->itemsize, item + ihigh*a->ob_descr->itemsize, (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize); - if (array_resize(a, Py_SIZE(a) + d) == -1) + if (array_resize(a, Py_SIZE(a) - d) == -1) return -1; } - else if (d > 0) { /* Insert d items */ - if (array_resize(a, Py_SIZE(a) + d)) - return -1; - memmove(item + (ihigh+d)*a->ob_descr->itemsize, - item + ihigh*a->ob_descr->itemsize, - (Py_SIZE(a)-ihigh)*a->ob_descr->itemsize); - } - if (n > 0) - memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item, - n*b->ob_descr->itemsize); return 0; -#undef b } static int @@ -927,7 +889,7 @@ array_ass_item(arrayobject *a, Py_ssize_t i, PyObject *v) return -1; } if (v == NULL) - return array_ass_slice(a, i, i+1, v); + return array_del_slice(a, i, i+1); return (*a->ob_descr->setitem)(a, i, v); } @@ -1155,8 +1117,7 @@ array_array_remove(arrayobject *self, PyObject *v) cmp = PyObject_RichCompareBool(selfi, v, Py_EQ); Py_DECREF(selfi); if (cmp > 0) { - if (array_ass_slice(self, i, i+1, - (PyObject *)NULL) != 0) + if (array_del_slice(self, i, i+1) != 0) return NULL; Py_INCREF(Py_None); return Py_None; @@ -1199,7 +1160,7 @@ array_array_pop_impl(arrayobject *self, Py_ssize_t i) v = getarrayitem((PyObject *)self, i); if (v == NULL) return NULL; - if (array_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) { + if (array_del_slice(self, i, i+1) != 0) { Py_DECREF(v); return NULL; } @@ -1946,7 +1907,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 +2045,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 +2515,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 +2556,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 +2836,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 +2878,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 +2962,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 1e131d2..ed1eca3 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, @@ -444,7 +446,9 @@ audioop_max_impl(PyObject *module, Py_buffer *fragment, int width) return NULL; for (i = 0; i < fragment->len; i += width) { int val = GETRAWSAMPLE(width, fragment->buf, i); - if (val < 0) absval = (-val); + /* Cast to unsigned before negating. Unsigned overflow is well- + defined, but signed overflow is not. */ + if (val < 0) absval = -(unsigned int)val; else absval = val; if (absval > max) max = absval; } diff --git a/Modules/binascii.c b/Modules/binascii.c index f0b47d8..50b09fe 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 8a47ff8..c9c58cd 100644 --- a/Modules/cjkcodecs/clinic/multibytecodec.c.h +++ b/Modules/cjkcodecs/clinic/multibytecodec.c.h @@ -25,13 +25,15 @@ static PyObject * _multibytecodec_MultibyteCodec_encode(MultibyteCodecObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"input", "errors", NULL}; + static const char * const _keywords[] = {"input", "errors", NULL}; + static _PyArg_Parser _parser = {"O|z:encode", _keywords, 0}; PyObject *input; const char *errors = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|z:encode", _keywords, - &input, &errors)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &input, &errors)) { goto exit; + } return_value = _multibytecodec_MultibyteCodec_encode_impl(self, input, errors); exit: @@ -61,19 +63,22 @@ static PyObject * _multibytecodec_MultibyteCodec_decode(MultibyteCodecObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"input", "errors", NULL}; + static const char * const _keywords[] = {"input", "errors", NULL}; + static _PyArg_Parser _parser = {"y*|z:decode", _keywords, 0}; Py_buffer input = {NULL, NULL}; const char *errors = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|z:decode", _keywords, - &input, &errors)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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; } @@ -95,13 +100,15 @@ static PyObject * _multibytecodec_MultibyteIncrementalEncoder_encode(MultibyteIncrementalEncoderObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"input", "final", NULL}; + static const char * const _keywords[] = {"input", "final", NULL}; + static _PyArg_Parser _parser = {"O|i:encode", _keywords, 0}; PyObject *input; int final = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:encode", _keywords, - &input, &final)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &input, &final)) { goto exit; + } return_value = _multibytecodec_MultibyteIncrementalEncoder_encode_impl(self, input, final); exit: @@ -142,19 +149,22 @@ static PyObject * _multibytecodec_MultibyteIncrementalDecoder_decode(MultibyteIncrementalDecoderObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"input", "final", NULL}; + static const char * const _keywords[] = {"input", "final", NULL}; + static _PyArg_Parser _parser = {"y*|i:decode", _keywords, 0}; Py_buffer input = {NULL, NULL}; int final = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|i:decode", _keywords, - &input, &final)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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 +206,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 +235,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 +264,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 +330,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=8e86fa162c85230b input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_bz2module.c.h b/Modules/clinic/_bz2module.c.h index 3ed8303e..c4032ea 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: @@ -121,19 +125,22 @@ static PyObject * _bz2_BZ2Decompressor_decompress(BZ2Decompressor *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"data", "max_length", NULL}; + static const char * const _keywords[] = {"data", "max_length", NULL}; + static _PyArg_Parser _parser = {"y*|n:decompress", _keywords, 0}; Py_buffer data = {NULL, NULL}; Py_ssize_t max_length = -1; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|n:decompress", _keywords, - &data, &max_length)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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 +162,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=40e5ef049f9e719b input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_codecsmodule.c.h b/Modules/clinic/_codecsmodule.c.h index af06c6d..090b1ef 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: @@ -64,14 +65,16 @@ static PyObject * _codecs_encode(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"obj", "encoding", "errors", NULL}; + static const char * const _keywords[] = {"obj", "encoding", "errors", NULL}; + static _PyArg_Parser _parser = {"O|ss:encode", _keywords, 0}; PyObject *obj; const char *encoding = NULL; const char *errors = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|ss:encode", _keywords, - &obj, &encoding, &errors)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &obj, &encoding, &errors)) { goto exit; + } return_value = _codecs_encode_impl(module, obj, encoding, errors); exit: @@ -101,14 +104,16 @@ static PyObject * _codecs_decode(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"obj", "encoding", "errors", NULL}; + static const char * const _keywords[] = {"obj", "encoding", "errors", NULL}; + static _PyArg_Parser _parser = {"O|ss:decode", _keywords, 0}; PyObject *obj; const char *encoding = NULL; const char *errors = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|ss:decode", _keywords, - &obj, &encoding, &errors)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &obj, &encoding, &errors)) { goto exit; + } return_value = _codecs_decode_impl(module, obj, encoding, errors); exit: @@ -133,8 +138,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 +167,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 +201,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 +230,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 +260,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 +295,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 +330,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 +365,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 +400,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 +437,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 +472,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 +507,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 +542,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 +579,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 +613,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 +647,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 +681,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 +715,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 +750,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 +787,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 +827,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 +863,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 +897,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 +925,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 +954,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 +984,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 +1013,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 +1042,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 +1072,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 +1101,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 +1130,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 +1159,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 +1188,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 +1217,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 +1246,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 +1276,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 +1303,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 +1332,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 +1366,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 +1403,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 +1433,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 +1457,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=0221e4eece62c905 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_cryptmodule.c.h b/Modules/clinic/_cryptmodule.c.h index a3c371c..412c6fe 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 5e11742..62ff1c8 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 688c903..cf09207 100644 --- a/Modules/clinic/_datetimemodule.c.h +++ b/Modules/clinic/_datetimemodule.c.h @@ -23,15 +23,17 @@ static PyObject * datetime_datetime_now(PyTypeObject *type, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"tz", NULL}; + static const char * const _keywords[] = {"tz", NULL}; + static _PyArg_Parser _parser = {"|O:now", _keywords, 0}; PyObject *tz = Py_None; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:now", _keywords, - &tz)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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=61f85af5637df8b5 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_dbmmodule.c.h b/Modules/clinic/_dbmmodule.c.h index 49cbceb..06cf7e6 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 86b4c4c..c91dfbf 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: @@ -144,13 +146,15 @@ static PyObject * _elementtree_Element_find(ElementObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "namespaces", NULL}; + static const char * const _keywords[] = {"path", "namespaces", NULL}; + static _PyArg_Parser _parser = {"O|O:find", _keywords, 0}; PyObject *path; PyObject *namespaces = Py_None; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:find", _keywords, - &path, &namespaces)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &path, &namespaces)) { goto exit; + } return_value = _elementtree_Element_find_impl(self, path, namespaces); exit: @@ -174,14 +178,16 @@ static PyObject * _elementtree_Element_findtext(ElementObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "default", "namespaces", NULL}; + static const char * const _keywords[] = {"path", "default", "namespaces", NULL}; + static _PyArg_Parser _parser = {"O|OO:findtext", _keywords, 0}; PyObject *path; PyObject *default_value = Py_None; PyObject *namespaces = Py_None; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OO:findtext", _keywords, - &path, &default_value, &namespaces)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &path, &default_value, &namespaces)) { goto exit; + } return_value = _elementtree_Element_findtext_impl(self, path, default_value, namespaces); exit: @@ -204,13 +210,15 @@ static PyObject * _elementtree_Element_findall(ElementObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "namespaces", NULL}; + static const char * const _keywords[] = {"path", "namespaces", NULL}; + static _PyArg_Parser _parser = {"O|O:findall", _keywords, 0}; PyObject *path; PyObject *namespaces = Py_None; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:findall", _keywords, - &path, &namespaces)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &path, &namespaces)) { goto exit; + } return_value = _elementtree_Element_findall_impl(self, path, namespaces); exit: @@ -233,13 +241,15 @@ static PyObject * _elementtree_Element_iterfind(ElementObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "namespaces", NULL}; + static const char * const _keywords[] = {"path", "namespaces", NULL}; + static _PyArg_Parser _parser = {"O|O:iterfind", _keywords, 0}; PyObject *path; PyObject *namespaces = Py_None; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:iterfind", _keywords, - &path, &namespaces)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &path, &namespaces)) { goto exit; + } return_value = _elementtree_Element_iterfind_impl(self, path, namespaces); exit: @@ -262,13 +272,15 @@ static PyObject * _elementtree_Element_get(ElementObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"key", "default", NULL}; + static const char * const _keywords[] = {"key", "default", NULL}; + static _PyArg_Parser _parser = {"O|O:get", _keywords, 0}; PyObject *key; PyObject *default_value = Py_None; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:get", _keywords, - &key, &default_value)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &key, &default_value)) { goto exit; + } return_value = _elementtree_Element_get_impl(self, key, default_value); exit: @@ -307,12 +319,14 @@ static PyObject * _elementtree_Element_iter(ElementObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"tag", NULL}; + static const char * const _keywords[] = {"tag", NULL}; + static _PyArg_Parser _parser = {"|O:iter", _keywords, 0}; PyObject *tag = Py_None; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:iter", _keywords, - &tag)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &tag)) { goto exit; + } return_value = _elementtree_Element_iter_impl(self, tag); exit: @@ -356,8 +370,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 +434,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 +460,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 +490,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: @@ -489,12 +507,14 @@ static int _elementtree_TreeBuilder___init__(PyObject *self, PyObject *args, PyObject *kwargs) { int return_value = -1; - static char *_keywords[] = {"element_factory", NULL}; + static const char * const _keywords[] = {"element_factory", NULL}; + static _PyArg_Parser _parser = {"|O:TreeBuilder", _keywords, 0}; PyObject *element_factory = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:TreeBuilder", _keywords, - &element_factory)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &element_factory)) { goto exit; + } return_value = _elementtree_TreeBuilder___init___impl((TreeBuilderObject *)self, element_factory); exit: @@ -555,8 +575,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: @@ -571,14 +592,16 @@ static int _elementtree_XMLParser___init__(PyObject *self, PyObject *args, PyObject *kwargs) { int return_value = -1; - static char *_keywords[] = {"html", "target", "encoding", NULL}; + static const char * const _keywords[] = {"html", "target", "encoding", NULL}; + static _PyArg_Parser _parser = {"|OOz:XMLParser", _keywords, 0}; PyObject *html = NULL; PyObject *target = NULL; const char *encoding = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOz:XMLParser", _keywords, - &html, &target, &encoding)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &html, &target, &encoding)) { goto exit; + } return_value = _elementtree_XMLParser___init___impl((XMLParserObject *)self, html, target, encoding); exit: @@ -640,8 +663,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 +692,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=4c5e94c28a009ce6 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_gdbmmodule.c.h b/Modules/clinic/_gdbmmodule.c.h index 2d87cfc..fdd589c 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 f8d38ea..c2ac89a 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; } @@ -89,19 +91,22 @@ static PyObject * _lzma_LZMADecompressor_decompress(Decompressor *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"data", "max_length", NULL}; + static const char * const _keywords[] = {"data", "max_length", NULL}; + static _PyArg_Parser _parser = {"y*|n:decompress", _keywords, 0}; Py_buffer data = {NULL, NULL}; Py_ssize_t max_length = -1; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|n:decompress", _keywords, - &data, &max_length)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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; } @@ -137,14 +142,16 @@ static int _lzma_LZMADecompressor___init__(PyObject *self, PyObject *args, PyObject *kwargs) { int return_value = -1; - static char *_keywords[] = {"format", "memlimit", "filters", NULL}; + static const char * const _keywords[] = {"format", "memlimit", "filters", NULL}; + static _PyArg_Parser _parser = {"|iOO:LZMADecompressor", _keywords, 0}; int format = FORMAT_AUTO; PyObject *memlimit = Py_None; PyObject *filters = Py_None; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iOO:LZMADecompressor", _keywords, - &format, &memlimit, &filters)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &format, &memlimit, &filters)) { goto exit; + } return_value = _lzma_LZMADecompressor___init___impl((Decompressor *)self, format, memlimit, filters); exit: @@ -171,8 +178,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 +207,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 +243,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=9434583fe111c771 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_opcode.c.h b/Modules/clinic/_opcode.c.h index a5f6442..513cbfd 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 bd12d2a..b8eec33 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: @@ -92,14 +93,16 @@ static int _pickle_Pickler___init__(PyObject *self, PyObject *args, PyObject *kwargs) { int return_value = -1; - static char *_keywords[] = {"file", "protocol", "fix_imports", NULL}; + static const char * const _keywords[] = {"file", "protocol", "fix_imports", NULL}; + static _PyArg_Parser _parser = {"O|Op:Pickler", _keywords, 0}; PyObject *file; PyObject *protocol = NULL; int fix_imports = 1; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Op:Pickler", _keywords, - &file, &protocol, &fix_imports)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &file, &protocol, &fix_imports)) { goto exit; + } return_value = _pickle_Pickler___init___impl((PicklerObject *)self, file, protocol, fix_imports); exit: @@ -212,8 +215,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 +243,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: @@ -281,15 +286,17 @@ static int _pickle_Unpickler___init__(PyObject *self, PyObject *args, PyObject *kwargs) { int return_value = -1; - static char *_keywords[] = {"file", "fix_imports", "encoding", "errors", NULL}; + static const char * const _keywords[] = {"file", "fix_imports", "encoding", "errors", NULL}; + static _PyArg_Parser _parser = {"O|$pss:Unpickler", _keywords, 0}; PyObject *file; int fix_imports = 1; const char *encoding = "ASCII"; const char *errors = "strict"; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|$pss:Unpickler", _keywords, - &file, &fix_imports, &encoding, &errors)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &file, &fix_imports, &encoding, &errors)) { goto exit; + } return_value = _pickle_Unpickler___init___impl((UnpicklerObject *)self, file, fix_imports, encoding, errors); exit: @@ -387,15 +394,17 @@ static PyObject * _pickle_dump(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"obj", "file", "protocol", "fix_imports", NULL}; + static const char * const _keywords[] = {"obj", "file", "protocol", "fix_imports", NULL}; + static _PyArg_Parser _parser = {"OO|O$p:dump", _keywords, 0}; PyObject *obj; PyObject *file; PyObject *protocol = NULL; int fix_imports = 1; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|O$p:dump", _keywords, - &obj, &file, &protocol, &fix_imports)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &obj, &file, &protocol, &fix_imports)) { goto exit; + } return_value = _pickle_dump_impl(module, obj, file, protocol, fix_imports); exit: @@ -431,14 +440,16 @@ static PyObject * _pickle_dumps(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"obj", "protocol", "fix_imports", NULL}; + static const char * const _keywords[] = {"obj", "protocol", "fix_imports", NULL}; + static _PyArg_Parser _parser = {"O|O$p:dumps", _keywords, 0}; PyObject *obj; PyObject *protocol = NULL; int fix_imports = 1; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O$p:dumps", _keywords, - &obj, &protocol, &fix_imports)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &obj, &protocol, &fix_imports)) { goto exit; + } return_value = _pickle_dumps_impl(module, obj, protocol, fix_imports); exit: @@ -485,15 +496,17 @@ static PyObject * _pickle_load(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"file", "fix_imports", "encoding", "errors", NULL}; + static const char * const _keywords[] = {"file", "fix_imports", "encoding", "errors", NULL}; + static _PyArg_Parser _parser = {"O|$pss:load", _keywords, 0}; PyObject *file; int fix_imports = 1; const char *encoding = "ASCII"; const char *errors = "strict"; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|$pss:load", _keywords, - &file, &fix_imports, &encoding, &errors)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &file, &fix_imports, &encoding, &errors)) { goto exit; + } return_value = _pickle_load_impl(module, file, fix_imports, encoding, errors); exit: @@ -531,18 +544,20 @@ static PyObject * _pickle_loads(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"data", "fix_imports", "encoding", "errors", NULL}; + static const char * const _keywords[] = {"data", "fix_imports", "encoding", "errors", NULL}; + static _PyArg_Parser _parser = {"O|$pss:loads", _keywords, 0}; PyObject *data; int fix_imports = 1; const char *encoding = "ASCII"; const char *errors = "strict"; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|$pss:loads", _keywords, - &data, &fix_imports, &encoding, &errors)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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=50f9127109673c98 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_sre.c.h b/Modules/clinic/_sre.c.h index 3281717..9aba13e 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: @@ -77,15 +80,17 @@ static PyObject * _sre_SRE_Pattern_match(PatternObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"string", "pos", "endpos", "pattern", NULL}; + static const char * const _keywords[] = {"string", "pos", "endpos", "pattern", NULL}; + static _PyArg_Parser _parser = {"|Onn$O:match", _keywords, 0}; PyObject *string = NULL; Py_ssize_t pos = 0; Py_ssize_t endpos = PY_SSIZE_T_MAX; PyObject *pattern = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Onn$O:match", _keywords, - &string, &pos, &endpos, &pattern)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &string, &pos, &endpos, &pattern)) { goto exit; + } return_value = _sre_SRE_Pattern_match_impl(self, string, pos, endpos, pattern); exit: @@ -111,15 +116,17 @@ static PyObject * _sre_SRE_Pattern_fullmatch(PatternObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"string", "pos", "endpos", "pattern", NULL}; + static const char * const _keywords[] = {"string", "pos", "endpos", "pattern", NULL}; + static _PyArg_Parser _parser = {"|Onn$O:fullmatch", _keywords, 0}; PyObject *string = NULL; Py_ssize_t pos = 0; Py_ssize_t endpos = PY_SSIZE_T_MAX; PyObject *pattern = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Onn$O:fullmatch", _keywords, - &string, &pos, &endpos, &pattern)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &string, &pos, &endpos, &pattern)) { goto exit; + } return_value = _sre_SRE_Pattern_fullmatch_impl(self, string, pos, endpos, pattern); exit: @@ -147,15 +154,17 @@ static PyObject * _sre_SRE_Pattern_search(PatternObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"string", "pos", "endpos", "pattern", NULL}; + static const char * const _keywords[] = {"string", "pos", "endpos", "pattern", NULL}; + static _PyArg_Parser _parser = {"|Onn$O:search", _keywords, 0}; PyObject *string = NULL; Py_ssize_t pos = 0; Py_ssize_t endpos = PY_SSIZE_T_MAX; PyObject *pattern = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Onn$O:search", _keywords, - &string, &pos, &endpos, &pattern)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &string, &pos, &endpos, &pattern)) { goto exit; + } return_value = _sre_SRE_Pattern_search_impl(self, string, pos, endpos, pattern); exit: @@ -181,15 +190,17 @@ static PyObject * _sre_SRE_Pattern_findall(PatternObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"string", "pos", "endpos", "source", NULL}; + static const char * const _keywords[] = {"string", "pos", "endpos", "source", NULL}; + static _PyArg_Parser _parser = {"|Onn$O:findall", _keywords, 0}; PyObject *string = NULL; Py_ssize_t pos = 0; Py_ssize_t endpos = PY_SSIZE_T_MAX; PyObject *source = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Onn$O:findall", _keywords, - &string, &pos, &endpos, &source)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &string, &pos, &endpos, &source)) { goto exit; + } return_value = _sre_SRE_Pattern_findall_impl(self, string, pos, endpos, source); exit: @@ -215,14 +226,16 @@ static PyObject * _sre_SRE_Pattern_finditer(PatternObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"string", "pos", "endpos", NULL}; + static const char * const _keywords[] = {"string", "pos", "endpos", NULL}; + static _PyArg_Parser _parser = {"O|nn:finditer", _keywords, 0}; PyObject *string; Py_ssize_t pos = 0; Py_ssize_t endpos = PY_SSIZE_T_MAX; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|nn:finditer", _keywords, - &string, &pos, &endpos)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &string, &pos, &endpos)) { goto exit; + } return_value = _sre_SRE_Pattern_finditer_impl(self, string, pos, endpos); exit: @@ -245,14 +258,16 @@ static PyObject * _sre_SRE_Pattern_scanner(PatternObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"string", "pos", "endpos", NULL}; + static const char * const _keywords[] = {"string", "pos", "endpos", NULL}; + static _PyArg_Parser _parser = {"O|nn:scanner", _keywords, 0}; PyObject *string; Py_ssize_t pos = 0; Py_ssize_t endpos = PY_SSIZE_T_MAX; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|nn:scanner", _keywords, - &string, &pos, &endpos)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &string, &pos, &endpos)) { goto exit; + } return_value = _sre_SRE_Pattern_scanner_impl(self, string, pos, endpos); exit: @@ -276,14 +291,16 @@ static PyObject * _sre_SRE_Pattern_split(PatternObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"string", "maxsplit", "source", NULL}; + static const char * const _keywords[] = {"string", "maxsplit", "source", NULL}; + static _PyArg_Parser _parser = {"|On$O:split", _keywords, 0}; PyObject *string = NULL; Py_ssize_t maxsplit = 0; PyObject *source = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|On$O:split", _keywords, - &string, &maxsplit, &source)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &string, &maxsplit, &source)) { goto exit; + } return_value = _sre_SRE_Pattern_split_impl(self, string, maxsplit, source); exit: @@ -307,14 +324,16 @@ static PyObject * _sre_SRE_Pattern_sub(PatternObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"repl", "string", "count", NULL}; + static const char * const _keywords[] = {"repl", "string", "count", NULL}; + static _PyArg_Parser _parser = {"OO|n:sub", _keywords, 0}; PyObject *repl; PyObject *string; Py_ssize_t count = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|n:sub", _keywords, - &repl, &string, &count)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &repl, &string, &count)) { goto exit; + } return_value = _sre_SRE_Pattern_sub_impl(self, repl, string, count); exit: @@ -338,14 +357,16 @@ static PyObject * _sre_SRE_Pattern_subn(PatternObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"repl", "string", "count", NULL}; + static const char * const _keywords[] = {"repl", "string", "count", NULL}; + static _PyArg_Parser _parser = {"OO|n:subn", _keywords, 0}; PyObject *repl; PyObject *string; Py_ssize_t count = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|n:subn", _keywords, - &repl, &string, &count)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &repl, &string, &count)) { goto exit; + } return_value = _sre_SRE_Pattern_subn_impl(self, repl, string, count); exit: @@ -384,12 +405,14 @@ static PyObject * _sre_SRE_Pattern___deepcopy__(PatternObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"memo", NULL}; + static const char * const _keywords[] = {"memo", NULL}; + static _PyArg_Parser _parser = {"O:__deepcopy__", _keywords, 0}; PyObject *memo; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:__deepcopy__", _keywords, - &memo)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &memo)) { goto exit; + } return_value = _sre_SRE_Pattern___deepcopy___impl(self, memo); exit: @@ -414,7 +437,8 @@ static PyObject * _sre_compile(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"pattern", "flags", "code", "groups", "groupindex", "indexgroup", NULL}; + static const char * const _keywords[] = {"pattern", "flags", "code", "groups", "groupindex", "indexgroup", NULL}; + static _PyArg_Parser _parser = {"OiO!nOO:compile", _keywords, 0}; PyObject *pattern; int flags; PyObject *code; @@ -422,9 +446,10 @@ _sre_compile(PyObject *module, PyObject *args, PyObject *kwargs) PyObject *groupindex; PyObject *indexgroup; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiO!nOO:compile", _keywords, - &pattern, &flags, &PyList_Type, &code, &groups, &groupindex, &indexgroup)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &pattern, &flags, &PyList_Type, &code, &groups, &groupindex, &indexgroup)) { goto exit; + } return_value = _sre_compile_impl(module, pattern, flags, code, groups, groupindex, indexgroup); exit: @@ -447,12 +472,14 @@ static PyObject * _sre_SRE_Match_expand(MatchObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"template", NULL}; + static const char * const _keywords[] = {"template", NULL}; + static _PyArg_Parser _parser = {"O:expand", _keywords, 0}; PyObject *template; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:expand", _keywords, - &template)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &template)) { goto exit; + } return_value = _sre_SRE_Match_expand_impl(self, template); exit: @@ -478,12 +505,14 @@ static PyObject * _sre_SRE_Match_groups(MatchObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"default", NULL}; + static const char * const _keywords[] = {"default", NULL}; + static _PyArg_Parser _parser = {"|O:groups", _keywords, 0}; PyObject *default_value = Py_None; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:groups", _keywords, - &default_value)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &default_value)) { goto exit; + } return_value = _sre_SRE_Match_groups_impl(self, default_value); exit: @@ -509,12 +538,14 @@ static PyObject * _sre_SRE_Match_groupdict(MatchObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"default", NULL}; + static const char * const _keywords[] = {"default", NULL}; + static _PyArg_Parser _parser = {"|O:groupdict", _keywords, 0}; PyObject *default_value = Py_None; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:groupdict", _keywords, - &default_value)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &default_value)) { goto exit; + } return_value = _sre_SRE_Match_groupdict_impl(self, default_value); exit: @@ -542,11 +573,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 +607,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 +640,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: @@ -645,12 +681,14 @@ static PyObject * _sre_SRE_Match___deepcopy__(MatchObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"memo", NULL}; + static const char * const _keywords[] = {"memo", NULL}; + static _PyArg_Parser _parser = {"O:__deepcopy__", _keywords, 0}; PyObject *memo; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:__deepcopy__", _keywords, - &memo)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &memo)) { goto exit; + } return_value = _sre_SRE_Match___deepcopy___impl(self, memo); exit: @@ -690,4 +728,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=2cbc2b1482738e54 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_ssl.c.h b/Modules/clinic/_ssl.c.h index 852e365..0bdc35e 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; } @@ -444,14 +458,16 @@ static PyObject * _ssl__SSLContext_load_cert_chain(PySSLContext *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"certfile", "keyfile", "password", NULL}; + static const char * const _keywords[] = {"certfile", "keyfile", "password", NULL}; + static _PyArg_Parser _parser = {"O|OO:load_cert_chain", _keywords, 0}; PyObject *certfile; PyObject *keyfile = NULL; PyObject *password = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OO:load_cert_chain", _keywords, - &certfile, &keyfile, &password)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &certfile, &keyfile, &password)) { goto exit; + } return_value = _ssl__SSLContext_load_cert_chain_impl(self, certfile, keyfile, password); exit: @@ -476,14 +492,16 @@ static PyObject * _ssl__SSLContext_load_verify_locations(PySSLContext *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"cafile", "capath", "cadata", NULL}; + static const char * const _keywords[] = {"cafile", "capath", "cadata", NULL}; + static _PyArg_Parser _parser = {"|OOO:load_verify_locations", _keywords, 0}; PyObject *cafile = NULL; PyObject *capath = NULL; PyObject *cadata = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOO:load_verify_locations", _keywords, - &cafile, &capath, &cadata)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &cafile, &capath, &cadata)) { goto exit; + } return_value = _ssl__SSLContext_load_verify_locations_impl(self, cafile, capath, cadata); exit: @@ -514,14 +532,16 @@ static PyObject * _ssl__SSLContext__wrap_socket(PySSLContext *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"sock", "server_side", "server_hostname", NULL}; + static const char * const _keywords[] = {"sock", "server_side", "server_hostname", NULL}; + static _PyArg_Parser _parser = {"O!i|O:_wrap_socket", _keywords, 0}; PyObject *sock; int server_side; PyObject *hostname_obj = Py_None; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!i|O:_wrap_socket", _keywords, - PySocketModule.Sock_Type, &sock, &server_side, &hostname_obj)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + PySocketModule.Sock_Type, &sock, &server_side, &hostname_obj)) { goto exit; + } return_value = _ssl__SSLContext__wrap_socket_impl(self, sock, server_side, hostname_obj); exit: @@ -546,15 +566,17 @@ static PyObject * _ssl__SSLContext__wrap_bio(PySSLContext *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"incoming", "outgoing", "server_side", "server_hostname", NULL}; + static const char * const _keywords[] = {"incoming", "outgoing", "server_side", "server_hostname", NULL}; + static _PyArg_Parser _parser = {"O!O!i|O:_wrap_bio", _keywords, 0}; PySSLMemoryBIO *incoming; PySSLMemoryBIO *outgoing; int server_side; 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)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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: @@ -666,12 +688,14 @@ static PyObject * _ssl__SSLContext_get_ca_certs(PySSLContext *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"binary_form", NULL}; + static const char * const _keywords[] = {"binary_form", NULL}; + static _PyArg_Parser _parser = {"|p:get_ca_certs", _keywords, 0}; int binary_form = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|p:get_ca_certs", _keywords, - &binary_form)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &binary_form)) { goto exit; + } return_value = _ssl__SSLContext_get_ca_certs_impl(self, binary_form); exit: @@ -687,11 +711,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 +748,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 +777,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 +834,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 +866,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 +896,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 +949,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: @@ -965,13 +999,15 @@ static PyObject * _ssl_txt2obj(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"txt", "name", NULL}; + static const char * const _keywords[] = {"txt", "name", NULL}; + static _PyArg_Parser _parser = {"s|p:txt2obj", _keywords, 0}; const char *txt; int name = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|p:txt2obj", _keywords, - &txt, &name)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &txt, &name)) { goto exit; + } return_value = _ssl_txt2obj_impl(module, txt, name); exit: @@ -996,8 +1032,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: @@ -1028,12 +1065,14 @@ static PyObject * _ssl_enum_certificates(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"store_name", NULL}; + static const char * const _keywords[] = {"store_name", NULL}; + static _PyArg_Parser _parser = {"s:enum_certificates", _keywords, 0}; const char *store_name; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:enum_certificates", _keywords, - &store_name)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &store_name)) { goto exit; + } return_value = _ssl_enum_certificates_impl(module, store_name); exit: @@ -1065,12 +1104,14 @@ static PyObject * _ssl_enum_crls(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"store_name", NULL}; + static const char * const _keywords[] = {"store_name", NULL}; + static _PyArg_Parser _parser = {"s:enum_crls", _keywords, 0}; const char *store_name; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:enum_crls", _keywords, - &store_name)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &store_name)) { goto exit; + } return_value = _ssl_enum_crls_impl(module, store_name); exit: @@ -1102,4 +1143,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=6057f95343369849 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_tkinter.c.h b/Modules/clinic/_tkinter.c.h index 77af083..edd5380 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 b83b33e..c192e72 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 ac38d37..44d48a9 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: @@ -103,13 +105,15 @@ static PyObject * _winapi_ConnectNamedPipe(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"handle", "overlapped", NULL}; + static const char * const _keywords[] = {"handle", "overlapped", NULL}; + static _PyArg_Parser _parser = {"" F_HANDLE "|i:ConnectNamedPipe", _keywords, 0}; HANDLE handle; int use_overlapped = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "" F_HANDLE "|i:ConnectNamedPipe", _keywords, - &handle, &use_overlapped)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &handle, &use_overlapped)) { goto exit; + } return_value = _winapi_ConnectNamedPipe_impl(module, handle, use_overlapped); exit: @@ -147,13 +151,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 +187,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 +228,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 +269,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 +320,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, ¤t_directory, &startup_info)) + &application_name, &command_line, &proc_attrs, &thread_attrs, &inherit_handles, &creation_flags, &env_mapping, ¤t_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 +364,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 +397,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 +425,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 +456,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 +487,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 +521,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 +554,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 +589,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 +620,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 +655,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: @@ -649,14 +680,16 @@ static PyObject * _winapi_ReadFile(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"handle", "size", "overlapped", NULL}; + static const char * const _keywords[] = {"handle", "size", "overlapped", NULL}; + static _PyArg_Parser _parser = {"" F_HANDLE "i|i:ReadFile", _keywords, 0}; HANDLE handle; int size; int use_overlapped = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "" F_HANDLE "i|i:ReadFile", _keywords, - &handle, &size, &use_overlapped)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &handle, &size, &use_overlapped)) { goto exit; + } return_value = _winapi_ReadFile_impl(module, handle, size, use_overlapped); exit: @@ -688,8 +721,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 +751,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 +779,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 +810,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 +845,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: @@ -835,17 +874,19 @@ static PyObject * _winapi_WriteFile(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"handle", "buffer", "overlapped", NULL}; + static const char * const _keywords[] = {"handle", "buffer", "overlapped", NULL}; + static _PyArg_Parser _parser = {"" F_HANDLE "O|i:WriteFile", _keywords, 0}; HANDLE handle; PyObject *buffer; int use_overlapped = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "" F_HANDLE "O|i:WriteFile", _keywords, - &handle, &buffer, &use_overlapped)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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=4bfccfb32ab726e8 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/arraymodule.c.h b/Modules/clinic/arraymodule.c.h index 0c7061a..3b9fcda 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 62e313b5..be4b718 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 e20cac2..0ee7f57 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,37 @@ 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 const char * const _keywords[] = {"data", "newline", NULL}; + static _PyArg_Parser _parser = {"y*|$i:b2a_base64", _keywords, 0}; Py_buffer data = {NULL, NULL}; + int newline = 1; - if (!PyArg_Parse(arg, "y*:b2a_base64", &data)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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 +150,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 +181,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 +213,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 +245,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 +280,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 +319,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 +358,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 +392,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 +427,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 +460,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: @@ -460,13 +489,15 @@ static PyObject * binascii_a2b_qp(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"data", "header", NULL}; + static const char * const _keywords[] = {"data", "header", NULL}; + static _PyArg_Parser _parser = {"O&|i:a2b_qp", _keywords, 0}; Py_buffer data = {NULL, NULL}; int header = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i:a2b_qp", _keywords, - ascii_buffer_converter, &data, &header)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + ascii_buffer_converter, &data, &header)) { goto exit; + } return_value = binascii_a2b_qp_impl(module, &data, header); exit: @@ -498,22 +529,25 @@ static PyObject * binascii_b2a_qp(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"data", "quotetabs", "istext", "header", NULL}; + static const char * const _keywords[] = {"data", "quotetabs", "istext", "header", NULL}; + static _PyArg_Parser _parser = {"y*|iii:b2a_qp", _keywords, 0}; Py_buffer data = {NULL, NULL}; int quotetabs = 0; int istext = 1; int header = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|iii:b2a_qp", _keywords, - &data, "etabs, &istext, &header)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &data, "etabs, &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=12611b05d8bf4a9c input=a9049054013a1b77]*/ diff --git a/Modules/clinic/cmathmodule.c.h b/Modules/clinic/cmathmodule.c.h index a255353..e46c314 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: @@ -839,22 +861,25 @@ static PyObject * cmath_isclose(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL}; + static const char * const _keywords[] = {"a", "b", "rel_tol", "abs_tol", NULL}; + static _PyArg_Parser _parser = {"DD|$dd:isclose", _keywords, 0}; Py_complex a; Py_complex b; double rel_tol = 1e-09; double abs_tol = 0.0; int _return_value; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "DD|$dd:isclose", _keywords, - &a, &b, &rel_tol, &abs_tol)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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=aa2e77ca9fc26928 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/fcntlmodule.c.h b/Modules/clinic/fcntlmodule.c.h index 67660eb..84a004b 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 2c47a42..9c9d595 100644 --- a/Modules/clinic/grpmodule.c.h +++ b/Modules/clinic/grpmodule.c.h @@ -20,12 +20,14 @@ static PyObject * grp_getgrgid(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"id", NULL}; + static const char * const _keywords[] = {"id", NULL}; + static _PyArg_Parser _parser = {"O:getgrgid", _keywords, 0}; PyObject *id; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:getgrgid", _keywords, - &id)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &id)) { goto exit; + } return_value = grp_getgrgid_impl(module, id); exit: @@ -50,12 +52,14 @@ static PyObject * grp_getgrnam(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"name", NULL}; + static const char * const _keywords[] = {"name", NULL}; + static _PyArg_Parser _parser = {"U:getgrnam", _keywords, 0}; PyObject *name; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U:getgrnam", _keywords, - &name)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &name)) { goto exit; + } return_value = grp_getgrnam_impl(module, name); exit: @@ -82,4 +86,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=c06081097b7fffe7 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/md5module.c.h b/Modules/clinic/md5module.c.h index 0bd958a..aeb1c41 100644 --- a/Modules/clinic/md5module.c.h +++ b/Modules/clinic/md5module.c.h @@ -81,15 +81,17 @@ static PyObject * _md5_md5(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"string", NULL}; + static const char * const _keywords[] = {"string", NULL}; + static _PyArg_Parser _parser = {"|O:md5", _keywords, 0}; PyObject *string = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:md5", _keywords, - &string)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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=f86fc2f3f21831e2 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/posixmodule.c.h b/Modules/clinic/posixmodule.c.h index be2e5a6..26dedd1 100644 --- a/Modules/clinic/posixmodule.c.h +++ b/Modules/clinic/posixmodule.c.h @@ -36,14 +36,16 @@ static PyObject * os_stat(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "dir_fd", "follow_symlinks", NULL}; + static const char * const _keywords[] = {"path", "dir_fd", "follow_symlinks", NULL}; + static _PyArg_Parser _parser = {"O&|$O&p:stat", _keywords, 0}; path_t path = PATH_T_INITIALIZE("stat", "path", 0, 1); int dir_fd = DEFAULT_DIR_FD; 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)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + 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: @@ -72,13 +74,15 @@ static PyObject * os_lstat(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "dir_fd", NULL}; + static const char * const _keywords[] = {"path", "dir_fd", NULL}; + static _PyArg_Parser _parser = {"O&|$O&:lstat", _keywords, 0}; path_t path = PATH_T_INITIALIZE("lstat", "path", 0, 0); int dir_fd = DEFAULT_DIR_FD; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:lstat", _keywords, - path_converter, &path, FSTATAT_DIR_FD_CONVERTER, &dir_fd)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path, FSTATAT_DIR_FD_CONVERTER, &dir_fd)) { goto exit; + } return_value = os_lstat_impl(module, &path, dir_fd); exit: @@ -131,7 +135,8 @@ static PyObject * os_access(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "mode", "dir_fd", "effective_ids", "follow_symlinks", NULL}; + static const char * const _keywords[] = {"path", "mode", "dir_fd", "effective_ids", "follow_symlinks", NULL}; + static _PyArg_Parser _parser = {"O&i|$O&pp:access", _keywords, 0}; path_t path = PATH_T_INITIALIZE("access", "path", 0, 1); int mode; int dir_fd = DEFAULT_DIR_FD; @@ -139,12 +144,14 @@ os_access(PyObject *module, PyObject *args, PyObject *kwargs) int follow_symlinks = 1; 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)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + 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 +185,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: @@ -233,12 +242,14 @@ static PyObject * os_chdir(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", NULL}; + static const char * const _keywords[] = {"path", NULL}; + static _PyArg_Parser _parser = {"O&:chdir", _keywords, 0}; path_t path = PATH_T_INITIALIZE("chdir", "path", 0, PATH_HAVE_FCHDIR); - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chdir", _keywords, - path_converter, &path)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path)) { goto exit; + } return_value = os_chdir_impl(module, &path); exit: @@ -269,12 +280,14 @@ static PyObject * os_fchdir(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"fd", NULL}; + static const char * const _keywords[] = {"fd", NULL}; + static _PyArg_Parser _parser = {"O&:fchdir", _keywords, 0}; int fd; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:fchdir", _keywords, - fildes_converter, &fd)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + fildes_converter, &fd)) { goto exit; + } return_value = os_fchdir_impl(module, fd); exit: @@ -320,15 +333,17 @@ static PyObject * os_chmod(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "mode", "dir_fd", "follow_symlinks", NULL}; + static const char * const _keywords[] = {"path", "mode", "dir_fd", "follow_symlinks", NULL}; + static _PyArg_Parser _parser = {"O&i|$O&p:chmod", _keywords, 0}; path_t path = PATH_T_INITIALIZE("chmod", "path", 0, PATH_HAVE_FCHMOD); int mode; int dir_fd = DEFAULT_DIR_FD; 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)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + 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: @@ -358,13 +373,15 @@ static PyObject * os_fchmod(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"fd", "mode", NULL}; + static const char * const _keywords[] = {"fd", "mode", NULL}; + static _PyArg_Parser _parser = {"ii:fchmod", _keywords, 0}; int fd; int mode; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii:fchmod", _keywords, - &fd, &mode)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &fd, &mode)) { goto exit; + } return_value = os_fchmod_impl(module, fd, mode); exit: @@ -394,13 +411,15 @@ static PyObject * os_lchmod(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "mode", NULL}; + static const char * const _keywords[] = {"path", "mode", NULL}; + static _PyArg_Parser _parser = {"O&i:lchmod", _keywords, 0}; path_t path = PATH_T_INITIALIZE("lchmod", "path", 0, 0); int mode; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i:lchmod", _keywords, - path_converter, &path, &mode)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path, &mode)) { goto exit; + } return_value = os_lchmod_impl(module, &path, mode); exit: @@ -437,14 +456,16 @@ static PyObject * os_chflags(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "flags", "follow_symlinks", NULL}; + static const char * const _keywords[] = {"path", "flags", "follow_symlinks", NULL}; + static _PyArg_Parser _parser = {"O&k|p:chflags", _keywords, 0}; path_t path = PATH_T_INITIALIZE("chflags", "path", 0, 0); unsigned long flags; int follow_symlinks = 1; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k|p:chflags", _keywords, - path_converter, &path, &flags, &follow_symlinks)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path, &flags, &follow_symlinks)) { goto exit; + } return_value = os_chflags_impl(module, &path, flags, follow_symlinks); exit: @@ -477,13 +498,15 @@ static PyObject * os_lchflags(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "flags", NULL}; + static const char * const _keywords[] = {"path", "flags", NULL}; + static _PyArg_Parser _parser = {"O&k:lchflags", _keywords, 0}; path_t path = PATH_T_INITIALIZE("lchflags", "path", 0, 0); unsigned long flags; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k:lchflags", _keywords, - path_converter, &path, &flags)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path, &flags)) { goto exit; + } return_value = os_lchflags_impl(module, &path, flags); exit: @@ -513,12 +536,14 @@ static PyObject * os_chroot(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", NULL}; + static const char * const _keywords[] = {"path", NULL}; + static _PyArg_Parser _parser = {"O&:chroot", _keywords, 0}; path_t path = PATH_T_INITIALIZE("chroot", "path", 0, 0); - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chroot", _keywords, - path_converter, &path)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path)) { goto exit; + } return_value = os_chroot_impl(module, &path); exit: @@ -548,12 +573,14 @@ static PyObject * os_fsync(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"fd", NULL}; + static const char * const _keywords[] = {"fd", NULL}; + static _PyArg_Parser _parser = {"O&:fsync", _keywords, 0}; int fd; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:fsync", _keywords, - fildes_converter, &fd)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + fildes_converter, &fd)) { goto exit; + } return_value = os_fsync_impl(module, fd); exit: @@ -602,12 +629,14 @@ static PyObject * os_fdatasync(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"fd", NULL}; + static const char * const _keywords[] = {"fd", NULL}; + static _PyArg_Parser _parser = {"O&:fdatasync", _keywords, 0}; int fd; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:fdatasync", _keywords, - fildes_converter, &fd)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + fildes_converter, &fd)) { goto exit; + } return_value = os_fdatasync_impl(module, fd); exit: @@ -659,16 +688,18 @@ static PyObject * os_chown(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "uid", "gid", "dir_fd", "follow_symlinks", NULL}; + static const char * const _keywords[] = {"path", "uid", "gid", "dir_fd", "follow_symlinks", NULL}; + static _PyArg_Parser _parser = {"O&O&O&|$O&p:chown", _keywords, 0}; path_t path = PATH_T_INITIALIZE("chown", "path", 0, PATH_HAVE_FCHOWN); uid_t uid; gid_t gid; int dir_fd = DEFAULT_DIR_FD; 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)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + 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: @@ -700,14 +731,16 @@ static PyObject * os_fchown(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"fd", "uid", "gid", NULL}; + static const char * const _keywords[] = {"fd", "uid", "gid", NULL}; + static _PyArg_Parser _parser = {"iO&O&:fchown", _keywords, 0}; int fd; uid_t uid; gid_t gid; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iO&O&:fchown", _keywords, - &fd, _Py_Uid_Converter, &uid, _Py_Gid_Converter, &gid)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &fd, _Py_Uid_Converter, &uid, _Py_Gid_Converter, &gid)) { goto exit; + } return_value = os_fchown_impl(module, fd, uid, gid); exit: @@ -737,14 +770,16 @@ static PyObject * os_lchown(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "uid", "gid", NULL}; + static const char * const _keywords[] = {"path", "uid", "gid", NULL}; + static _PyArg_Parser _parser = {"O&O&O&:lchown", _keywords, 0}; path_t path = PATH_T_INITIALIZE("lchown", "path", 0, 0); uid_t uid; gid_t gid; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&O&:lchown", _keywords, - path_converter, &path, _Py_Uid_Converter, &uid, _Py_Gid_Converter, &gid)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path, _Py_Uid_Converter, &uid, _Py_Gid_Converter, &gid)) { goto exit; + } return_value = os_lchown_impl(module, &path, uid, gid); exit: @@ -822,16 +857,18 @@ static PyObject * os_link(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", "follow_symlinks", NULL}; + static const char * const _keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", "follow_symlinks", NULL}; + static _PyArg_Parser _parser = {"O&O&|$O&O&p:link", _keywords, 0}; path_t src = PATH_T_INITIALIZE("link", "src", 0, 0); path_t dst = PATH_T_INITIALIZE("link", "dst", 0, 0); int src_dir_fd = DEFAULT_DIR_FD; int dst_dir_fd = DEFAULT_DIR_FD; 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)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + 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: @@ -872,12 +909,14 @@ static PyObject * os_listdir(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", NULL}; + static const char * const _keywords[] = {"path", NULL}; + static _PyArg_Parser _parser = {"|O&:listdir", _keywords, 0}; path_t path = PATH_T_INITIALIZE("listdir", "path", 1, PATH_HAVE_FDOPENDIR); - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", _keywords, - path_converter, &path)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path)) { goto exit; + } return_value = os_listdir_impl(module, &path); exit: @@ -906,8 +945,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 +979,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 +1009,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: @@ -999,12 +1041,14 @@ static PyObject * os__getvolumepathname(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", NULL}; + static const char * const _keywords[] = {"path", NULL}; + static _PyArg_Parser _parser = {"U:_getvolumepathname", _keywords, 0}; PyObject *path; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U:_getvolumepathname", _keywords, - &path)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &path)) { goto exit; + } return_value = os__getvolumepathname_impl(module, path); exit: @@ -1036,14 +1080,16 @@ static PyObject * os_mkdir(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "mode", "dir_fd", NULL}; + static const char * const _keywords[] = {"path", "mode", "dir_fd", NULL}; + static _PyArg_Parser _parser = {"O&|i$O&:mkdir", _keywords, 0}; path_t path = PATH_T_INITIALIZE("mkdir", "path", 0, 0); int mode = 511; 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)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + 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 +1119,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: @@ -1101,13 +1148,15 @@ static PyObject * os_getpriority(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"which", "who", NULL}; + static const char * const _keywords[] = {"which", "who", NULL}; + static _PyArg_Parser _parser = {"ii:getpriority", _keywords, 0}; int which; int who; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii:getpriority", _keywords, - &which, &who)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &which, &who)) { goto exit; + } return_value = os_getpriority_impl(module, which, who); exit: @@ -1134,14 +1183,16 @@ static PyObject * os_setpriority(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"which", "who", "priority", NULL}; + static const char * const _keywords[] = {"which", "who", "priority", NULL}; + static _PyArg_Parser _parser = {"iii:setpriority", _keywords, 0}; int which; int who; int priority; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iii:setpriority", _keywords, - &which, &who, &priority)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &which, &who, &priority)) { goto exit; + } return_value = os_setpriority_impl(module, which, who, priority); exit: @@ -1173,15 +1224,17 @@ static PyObject * os_rename(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL}; + static const char * const _keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL}; + static _PyArg_Parser _parser = {"O&O&|$O&O&:rename", _keywords, 0}; path_t src = PATH_T_INITIALIZE("rename", "src", 0, 0); path_t dst = PATH_T_INITIALIZE("rename", "dst", 0, 0); int src_dir_fd = DEFAULT_DIR_FD; 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)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + 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: @@ -1216,15 +1269,17 @@ static PyObject * os_replace(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL}; + static const char * const _keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL}; + static _PyArg_Parser _parser = {"O&O&|$O&O&:replace", _keywords, 0}; path_t src = PATH_T_INITIALIZE("replace", "src", 0, 0); path_t dst = PATH_T_INITIALIZE("replace", "dst", 0, 0); int src_dir_fd = DEFAULT_DIR_FD; 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)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + 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: @@ -1257,13 +1312,15 @@ static PyObject * os_rmdir(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "dir_fd", NULL}; + static const char * const _keywords[] = {"path", "dir_fd", NULL}; + static _PyArg_Parser _parser = {"O&|$O&:rmdir", _keywords, 0}; path_t path = PATH_T_INITIALIZE("rmdir", "path", 0, 0); int dir_fd = DEFAULT_DIR_FD; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:rmdir", _keywords, - path_converter, &path, UNLINKAT_DIR_FD_CONVERTER, &dir_fd)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path, UNLINKAT_DIR_FD_CONVERTER, &dir_fd)) { goto exit; + } return_value = os_rmdir_impl(module, &path, dir_fd); exit: @@ -1291,16 +1348,19 @@ static PyObject * os_system(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"command", NULL}; + static const char * const _keywords[] = {"command", NULL}; + static _PyArg_Parser _parser = {"u:system", _keywords, 0}; Py_UNICODE *command; long _return_value; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "u:system", _keywords, - &command)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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: @@ -1327,16 +1387,19 @@ static PyObject * os_system(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"command", NULL}; + static const char * const _keywords[] = {"command", NULL}; + static _PyArg_Parser _parser = {"O&:system", _keywords, 0}; PyObject *command = NULL; long _return_value; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:system", _keywords, - PyUnicode_FSConverter, &command)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + 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 +1429,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: @@ -1395,13 +1459,15 @@ static PyObject * os_unlink(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "dir_fd", NULL}; + static const char * const _keywords[] = {"path", "dir_fd", NULL}; + static _PyArg_Parser _parser = {"O&|$O&:unlink", _keywords, 0}; path_t path = PATH_T_INITIALIZE("unlink", "path", 0, 0); int dir_fd = DEFAULT_DIR_FD; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:unlink", _keywords, - path_converter, &path, UNLINKAT_DIR_FD_CONVERTER, &dir_fd)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path, UNLINKAT_DIR_FD_CONVERTER, &dir_fd)) { goto exit; + } return_value = os_unlink_impl(module, &path, dir_fd); exit: @@ -1432,13 +1498,15 @@ static PyObject * os_remove(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "dir_fd", NULL}; + static const char * const _keywords[] = {"path", "dir_fd", NULL}; + static _PyArg_Parser _parser = {"O&|$O&:remove", _keywords, 0}; path_t path = PATH_T_INITIALIZE("remove", "path", 0, 0); int dir_fd = DEFAULT_DIR_FD; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:remove", _keywords, - path_converter, &path, UNLINKAT_DIR_FD_CONVERTER, &dir_fd)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path, UNLINKAT_DIR_FD_CONVERTER, &dir_fd)) { goto exit; + } return_value = os_remove_impl(module, &path, dir_fd); exit: @@ -1513,16 +1581,18 @@ static PyObject * os_utime(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "times", "ns", "dir_fd", "follow_symlinks", NULL}; + static const char * const _keywords[] = {"path", "times", "ns", "dir_fd", "follow_symlinks", NULL}; + static _PyArg_Parser _parser = {"O&|O$OO&p:utime", _keywords, 0}; path_t path = PATH_T_INITIALIZE("utime", "path", 0, PATH_UTIME_HAVE_FD); PyObject *times = NULL; PyObject *ns = NULL; int dir_fd = DEFAULT_DIR_FD; int follow_symlinks = 1; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|O$OO&p:utime", _keywords, - path_converter, &path, ×, &ns, FUTIMENSAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path, ×, &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: @@ -1548,12 +1618,14 @@ static PyObject * os__exit(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"status", NULL}; + static const char * const _keywords[] = {"status", NULL}; + static _PyArg_Parser _parser = {"i:_exit", _keywords, 0}; int status; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:_exit", _keywords, - &status)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &status)) { goto exit; + } return_value = os__exit_impl(module, status); exit: @@ -1587,8 +1659,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: @@ -1625,14 +1698,16 @@ static PyObject * os_execve(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "argv", "env", NULL}; + static const char * const _keywords[] = {"path", "argv", "env", NULL}; + static _PyArg_Parser _parser = {"O&OO:execve", _keywords, 0}; path_t path = PATH_T_INITIALIZE("execve", "path", 0, PATH_HAVE_FEXECVE); PyObject *argv; PyObject *env; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&OO:execve", _keywords, - path_converter, &path, &argv, &env)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path, &argv, &env)) { goto exit; + } return_value = os_execve_impl(module, &path, argv, env); exit: @@ -1674,8 +1749,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 +1797,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: @@ -1800,12 +1877,14 @@ static PyObject * os_sched_get_priority_max(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"policy", NULL}; + static const char * const _keywords[] = {"policy", NULL}; + static _PyArg_Parser _parser = {"i:sched_get_priority_max", _keywords, 0}; int policy; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:sched_get_priority_max", _keywords, - &policy)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &policy)) { goto exit; + } return_value = os_sched_get_priority_max_impl(module, policy); exit: @@ -1832,12 +1911,14 @@ static PyObject * os_sched_get_priority_min(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"policy", NULL}; + static const char * const _keywords[] = {"policy", NULL}; + static _PyArg_Parser _parser = {"i:sched_get_priority_min", _keywords, 0}; int policy; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:sched_get_priority_min", _keywords, - &policy)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &policy)) { goto exit; + } return_value = os_sched_get_priority_min_impl(module, policy); exit: @@ -1868,8 +1949,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: @@ -1896,12 +1978,14 @@ static PyObject * os_sched_param(PyTypeObject *type, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"sched_priority", NULL}; + static const char * const _keywords[] = {"sched_priority", NULL}; + static _PyArg_Parser _parser = {"O:sched_param", _keywords, 0}; PyObject *sched_priority; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", _keywords, - &sched_priority)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &sched_priority)) { goto exit; + } return_value = os_sched_param_impl(type, sched_priority); exit: @@ -1937,8 +2021,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, ¶m)) + &pid, &policy, convert_sched_param, ¶m)) { goto exit; + } return_value = os_sched_setscheduler_impl(module, pid, policy, ¶m); exit: @@ -1970,8 +2055,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 +2092,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, ¶m)) + &pid, convert_sched_param, ¶m)) { goto exit; + } return_value = os_sched_setparam_impl(module, pid, ¶m); exit: @@ -2039,11 +2126,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 +2187,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 +2204,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 +2220,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: @@ -2316,12 +2407,14 @@ static PyObject * os_getpgid(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"pid", NULL}; + static const char * const _keywords[] = {"pid", NULL}; + static _PyArg_Parser _parser = {"" _Py_PARSE_PID ":getpgid", _keywords, 0}; pid_t pid; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "" _Py_PARSE_PID ":getpgid", _keywords, - &pid)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &pid)) { goto exit; + } return_value = os_getpgid_impl(module, pid); exit: @@ -2465,8 +2558,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 +2591,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 +2622,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 +2653,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 +2684,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 +2715,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 +2748,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 +2781,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 +2812,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: @@ -2755,12 +2857,14 @@ static PyObject * os_wait3(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"options", NULL}; + static const char * const _keywords[] = {"options", NULL}; + static _PyArg_Parser _parser = {"i:wait3", _keywords, 0}; int options; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:wait3", _keywords, - &options)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &options)) { goto exit; + } return_value = os_wait3_impl(module, options); exit: @@ -2790,13 +2894,15 @@ static PyObject * os_wait4(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"pid", "options", NULL}; + static const char * const _keywords[] = {"pid", "options", NULL}; + static _PyArg_Parser _parser = {"" _Py_PARSE_PID "i:wait4", _keywords, 0}; pid_t pid; int options; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "" _Py_PARSE_PID "i:wait4", _keywords, - &pid, &options)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &pid, &options)) { goto exit; + } return_value = os_wait4_impl(module, pid, options); exit: @@ -2839,8 +2945,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 +2983,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 +3021,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: @@ -2977,15 +3086,17 @@ static PyObject * os_symlink(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"src", "dst", "target_is_directory", "dir_fd", NULL}; + static const char * const _keywords[] = {"src", "dst", "target_is_directory", "dir_fd", NULL}; + static _PyArg_Parser _parser = {"O&O&|p$O&:symlink", _keywords, 0}; path_t src = PATH_T_INITIALIZE("symlink", "src", 0, 0); path_t dst = PATH_T_INITIALIZE("symlink", "dst", 0, 0); int target_is_directory = 0; 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)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + 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 +3156,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 +3211,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 +3242,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 +3275,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: @@ -3192,19 +3307,22 @@ static PyObject * os_open(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "flags", "mode", "dir_fd", NULL}; + static const char * const _keywords[] = {"path", "flags", "mode", "dir_fd", NULL}; + static _PyArg_Parser _parser = {"O&i|i$O&:open", _keywords, 0}; path_t path = PATH_T_INITIALIZE("open", "path", 0, 0); int flags; int mode = 511; int dir_fd = DEFAULT_DIR_FD; 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)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + 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: @@ -3230,12 +3348,14 @@ static PyObject * os_close(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"fd", NULL}; + static const char * const _keywords[] = {"fd", NULL}; + static _PyArg_Parser _parser = {"i:close", _keywords, 0}; int fd; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:close", _keywords, - &fd)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &fd)) { goto exit; + } return_value = os_close_impl(module, fd); exit: @@ -3262,8 +3382,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 +3410,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: @@ -3316,14 +3439,16 @@ static PyObject * os_dup2(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"fd", "fd2", "inheritable", NULL}; + static const char * const _keywords[] = {"fd", "fd2", "inheritable", NULL}; + static _PyArg_Parser _parser = {"ii|p:dup2", _keywords, 0}; int fd; int fd2; int inheritable = 1; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|p:dup2", _keywords, - &fd, &fd2, &inheritable)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &fd, &fd2, &inheritable)) { goto exit; + } return_value = os_dup2_impl(module, fd, fd2, inheritable); exit: @@ -3360,8 +3485,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 +3521,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 +3554,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 +3594,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 +3635,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 +3667,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; } @@ -3569,12 +3704,14 @@ static PyObject * os_fstat(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"fd", NULL}; + static const char * const _keywords[] = {"fd", NULL}; + static _PyArg_Parser _parser = {"i:fstat", _keywords, 0}; int fd; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:fstat", _keywords, - &fd)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &fd)) { goto exit; + } return_value = os_fstat_impl(module, fd); exit: @@ -3603,11 +3740,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 +3804,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 +3841,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 +3884,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; } @@ -3782,14 +3927,16 @@ static PyObject * os_mkfifo(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "mode", "dir_fd", NULL}; + static const char * const _keywords[] = {"path", "mode", "dir_fd", NULL}; + static _PyArg_Parser _parser = {"O&|i$O&:mkfifo", _keywords, 0}; path_t path = PATH_T_INITIALIZE("mkfifo", "path", 0, 0); int mode = 438; 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)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path, &mode, MKFIFOAT_DIR_FD_CONVERTER, &dir_fd)) { goto exit; + } return_value = os_mkfifo_impl(module, &path, mode, dir_fd); exit: @@ -3832,15 +3979,17 @@ static PyObject * os_mknod(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "mode", "device", "dir_fd", NULL}; + static const char * const _keywords[] = {"path", "mode", "device", "dir_fd", NULL}; + static _PyArg_Parser _parser = {"O&|iO&$O&:mknod", _keywords, 0}; path_t path = PATH_T_INITIALIZE("mknod", "path", 0, 0); int mode = 384; dev_t device = 0; 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)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + 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 +4022,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 +4058,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 +4096,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 +4133,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: @@ -4009,13 +4165,15 @@ static PyObject * os_truncate(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "length", NULL}; + static const char * const _keywords[] = {"path", "length", NULL}; + static _PyArg_Parser _parser = {"O&O&:truncate", _keywords, 0}; path_t path = PATH_T_INITIALIZE("truncate", "path", 0, PATH_HAVE_FTRUNCATE); Py_off_t length; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:truncate", _keywords, - path_converter, &path, Py_off_t_converter, &length)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path, Py_off_t_converter, &length)) { goto exit; + } return_value = os_truncate_impl(module, &path, length); exit: @@ -4054,8 +4212,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 +4256,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 +4289,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 +4322,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 +4358,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 +4390,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 +4420,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: @@ -4290,16 +4456,19 @@ static PyObject * os_WIFCONTINUED(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"status", NULL}; + static const char * const _keywords[] = {"status", NULL}; + static _PyArg_Parser _parser = {"i:WIFCONTINUED", _keywords, 0}; int status; int _return_value; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:WIFCONTINUED", _keywords, - &status)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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: @@ -4326,16 +4495,19 @@ static PyObject * os_WIFSTOPPED(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"status", NULL}; + static const char * const _keywords[] = {"status", NULL}; + static _PyArg_Parser _parser = {"i:WIFSTOPPED", _keywords, 0}; int status; int _return_value; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:WIFSTOPPED", _keywords, - &status)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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: @@ -4362,16 +4534,19 @@ static PyObject * os_WIFSIGNALED(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"status", NULL}; + static const char * const _keywords[] = {"status", NULL}; + static _PyArg_Parser _parser = {"i:WIFSIGNALED", _keywords, 0}; int status; int _return_value; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:WIFSIGNALED", _keywords, - &status)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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: @@ -4398,16 +4573,19 @@ static PyObject * os_WIFEXITED(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"status", NULL}; + static const char * const _keywords[] = {"status", NULL}; + static _PyArg_Parser _parser = {"i:WIFEXITED", _keywords, 0}; int status; int _return_value; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:WIFEXITED", _keywords, - &status)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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: @@ -4434,16 +4612,19 @@ static PyObject * os_WEXITSTATUS(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"status", NULL}; + static const char * const _keywords[] = {"status", NULL}; + static _PyArg_Parser _parser = {"i:WEXITSTATUS", _keywords, 0}; int status; int _return_value; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:WEXITSTATUS", _keywords, - &status)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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: @@ -4470,16 +4651,19 @@ static PyObject * os_WTERMSIG(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"status", NULL}; + static const char * const _keywords[] = {"status", NULL}; + static _PyArg_Parser _parser = {"i:WTERMSIG", _keywords, 0}; int status; int _return_value; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:WTERMSIG", _keywords, - &status)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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: @@ -4506,16 +4690,19 @@ static PyObject * os_WSTOPSIG(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"status", NULL}; + static const char * const _keywords[] = {"status", NULL}; + static _PyArg_Parser _parser = {"i:WSTOPSIG", _keywords, 0}; int status; int _return_value; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:WSTOPSIG", _keywords, - &status)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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 +4733,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: @@ -4578,12 +4766,14 @@ static PyObject * os_statvfs(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", NULL}; + static const char * const _keywords[] = {"path", NULL}; + static _PyArg_Parser _parser = {"O&:statvfs", _keywords, 0}; path_t path = PATH_T_INITIALIZE("statvfs", "path", 0, PATH_HAVE_FSTATVFS); - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:statvfs", _keywords, - path_converter, &path)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path)) { goto exit; + } return_value = os_statvfs_impl(module, &path); exit: @@ -4613,12 +4803,14 @@ static PyObject * os__getdiskusage(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", NULL}; + static const char * const _keywords[] = {"path", NULL}; + static _PyArg_Parser _parser = {"u:_getdiskusage", _keywords, 0}; Py_UNICODE *path; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "u:_getdiskusage", _keywords, - &path)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &path)) { goto exit; + } return_value = os__getdiskusage_impl(module, path); exit: @@ -4652,11 +4844,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: @@ -4687,17 +4881,20 @@ static PyObject * os_pathconf(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "name", NULL}; + static const char * const _keywords[] = {"path", "name", NULL}; + static _PyArg_Parser _parser = {"O&O&:pathconf", _keywords, 0}; path_t path = PATH_T_INITIALIZE("pathconf", "path", 0, PATH_HAVE_FPATHCONF); int name; long _return_value; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:pathconf", _keywords, - path_converter, &path, conv_path_confname, &name)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + 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 +4926,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 +4958,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: @@ -4839,12 +5039,14 @@ static PyObject * os_device_encoding(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"fd", NULL}; + static const char * const _keywords[] = {"fd", NULL}; + static _PyArg_Parser _parser = {"i:device_encoding", _keywords, 0}; int fd; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:device_encoding", _keywords, - &fd)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &fd)) { goto exit; + } return_value = os_device_encoding_impl(module, fd); exit: @@ -4874,8 +5076,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 +5110,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: @@ -4985,14 +5189,16 @@ static PyObject * os_getxattr(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "attribute", "follow_symlinks", NULL}; + static const char * const _keywords[] = {"path", "attribute", "follow_symlinks", NULL}; + static _PyArg_Parser _parser = {"O&O&|$p:getxattr", _keywords, 0}; path_t path = PATH_T_INITIALIZE("getxattr", "path", 0, 1); path_t attribute = PATH_T_INITIALIZE("getxattr", "attribute", 0, 0); int follow_symlinks = 1; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", _keywords, - path_converter, &path, path_converter, &attribute, &follow_symlinks)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path, path_converter, &attribute, &follow_symlinks)) { goto exit; + } return_value = os_getxattr_impl(module, &path, &attribute, follow_symlinks); exit: @@ -5031,16 +5237,18 @@ static PyObject * os_setxattr(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "attribute", "value", "flags", "follow_symlinks", NULL}; + static const char * const _keywords[] = {"path", "attribute", "value", "flags", "follow_symlinks", NULL}; + static _PyArg_Parser _parser = {"O&O&y*|i$p:setxattr", _keywords, 0}; path_t path = PATH_T_INITIALIZE("setxattr", "path", 0, 1); path_t attribute = PATH_T_INITIALIZE("setxattr", "attribute", 0, 0); Py_buffer value = {NULL, NULL}; int flags = 0; 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)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + 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 +5257,9 @@ exit: /* Cleanup for attribute */ path_cleanup(&attribute); /* Cleanup for value */ - if (value.obj) + if (value.obj) { PyBuffer_Release(&value); + } return return_value; } @@ -5081,14 +5290,16 @@ static PyObject * os_removexattr(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "attribute", "follow_symlinks", NULL}; + static const char * const _keywords[] = {"path", "attribute", "follow_symlinks", NULL}; + static _PyArg_Parser _parser = {"O&O&|$p:removexattr", _keywords, 0}; path_t path = PATH_T_INITIALIZE("removexattr", "path", 0, 1); path_t attribute = PATH_T_INITIALIZE("removexattr", "attribute", 0, 0); int follow_symlinks = 1; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr", _keywords, - path_converter, &path, path_converter, &attribute, &follow_symlinks)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path, path_converter, &attribute, &follow_symlinks)) { goto exit; + } return_value = os_removexattr_impl(module, &path, &attribute, follow_symlinks); exit: @@ -5126,13 +5337,15 @@ static PyObject * os_listxattr(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"path", "follow_symlinks", NULL}; + static const char * const _keywords[] = {"path", "follow_symlinks", NULL}; + static _PyArg_Parser _parser = {"|O&$p:listxattr", _keywords, 0}; path_t path = PATH_T_INITIALIZE("listxattr", "path", 1, 1); int follow_symlinks = 1; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", _keywords, - path_converter, &path, &follow_symlinks)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + path_converter, &path, &follow_symlinks)) { goto exit; + } return_value = os_listxattr_impl(module, &path, follow_symlinks); exit: @@ -5162,8 +5375,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 +5388,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 +5425,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 +5458,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 +5488,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 +5526,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 +5537,40 @@ 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 const char * const _keywords[] = {"path", NULL}; + static _PyArg_Parser _parser = {"O:fspath", _keywords, 0}; + PyObject *path; + + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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 +6042,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=97180b6734421a7d input=a9049054013a1b77]*/ diff --git a/Modules/clinic/pwdmodule.c.h b/Modules/clinic/pwdmodule.c.h index cb191a0..f9e0644 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 c5b5c71..8a03f67 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: @@ -238,14 +243,16 @@ static PyObject * pyexpat_ParserCreate(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"encoding", "namespace_separator", "intern", NULL}; + static const char * const _keywords[] = {"encoding", "namespace_separator", "intern", NULL}; + static _PyArg_Parser _parser = {"|zzO:ParserCreate", _keywords, 0}; const char *encoding = NULL; const char *namespace_separator = NULL; PyObject *intern = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|zzO:ParserCreate", _keywords, - &encoding, &namespace_separator, &intern)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &encoding, &namespace_separator, &intern)) { goto exit; + } return_value = pyexpat_ParserCreate_impl(module, encoding, namespace_separator, intern); exit: @@ -270,8 +277,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 +289,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=93cfe662f2bc48e5 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/sha1module.c.h b/Modules/clinic/sha1module.c.h index 5b8db80..1972762 100644 --- a/Modules/clinic/sha1module.c.h +++ b/Modules/clinic/sha1module.c.h @@ -81,15 +81,17 @@ static PyObject * _sha1_sha1(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"string", NULL}; + static const char * const _keywords[] = {"string", NULL}; + static _PyArg_Parser _parser = {"|O:sha1", _keywords, 0}; PyObject *string = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:sha1", _keywords, - &string)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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=549a5d08c248337d input=a9049054013a1b77]*/ diff --git a/Modules/clinic/sha256module.c.h b/Modules/clinic/sha256module.c.h index 661569c..4239ab8 100644 --- a/Modules/clinic/sha256module.c.h +++ b/Modules/clinic/sha256module.c.h @@ -81,12 +81,14 @@ static PyObject * _sha256_sha256(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"string", NULL}; + static const char * const _keywords[] = {"string", NULL}; + static _PyArg_Parser _parser = {"|O:sha256", _keywords, 0}; PyObject *string = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:sha256", _keywords, - &string)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &string)) { goto exit; + } return_value = _sha256_sha256_impl(module, string); exit: @@ -109,15 +111,17 @@ static PyObject * _sha256_sha224(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"string", NULL}; + static const char * const _keywords[] = {"string", NULL}; + static _PyArg_Parser _parser = {"|O:sha224", _keywords, 0}; PyObject *string = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:sha224", _keywords, - &string)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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=a1296ba6d0780051 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/sha512module.c.h b/Modules/clinic/sha512module.c.h index d64c2a4..78f7743 100644 --- a/Modules/clinic/sha512module.c.h +++ b/Modules/clinic/sha512module.c.h @@ -99,12 +99,14 @@ static PyObject * _sha512_sha512(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"string", NULL}; + static const char * const _keywords[] = {"string", NULL}; + static _PyArg_Parser _parser = {"|O:sha512", _keywords, 0}; PyObject *string = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:sha512", _keywords, - &string)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &string)) { goto exit; + } return_value = _sha512_sha512_impl(module, string); exit: @@ -131,12 +133,14 @@ static PyObject * _sha512_sha384(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"string", NULL}; + static const char * const _keywords[] = {"string", NULL}; + static _PyArg_Parser _parser = {"|O:sha384", _keywords, 0}; PyObject *string = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:sha384", _keywords, - &string)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &string)) { goto exit; + } return_value = _sha512_sha384_impl(module, string); exit: @@ -168,4 +172,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=cf0da76cb603d1bf input=a9049054013a1b77]*/ diff --git a/Modules/clinic/signalmodule.c.h b/Modules/clinic/signalmodule.c.h index c8f4cd5..f8d5bd9 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 e26acf1..b2479ff 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 d520c1e..d481ccb 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 b1af7ce..172308a 100644 --- a/Modules/clinic/zlibmodule.c.h +++ b/Modules/clinic/zlibmodule.c.h @@ -3,44 +3,48 @@ 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 const char * const _keywords[] = {"", "level", NULL}; + static _PyArg_Parser _parser = {"y*|i:compress", _keywords, 0}; + Py_buffer data = {NULL, NULL}; int level = Z_DEFAULT_COMPRESSION; - if (!PyArg_ParseTuple(args, "y*|i:compress", - &bytes, &level)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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; } PyDoc_STRVAR(zlib_decompress__doc__, -"decompress($module, data, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE, /)\n" +"decompress($module, data, /, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE)\n" "--\n" "\n" "Returns a bytes object containing the uncompressed data.\n" @@ -53,29 +57,33 @@ PyDoc_STRVAR(zlib_decompress__doc__, " The initial output buffer size."); #define ZLIB_DECOMPRESS_METHODDEF \ - {"decompress", (PyCFunction)zlib_decompress, METH_VARARGS, zlib_decompress__doc__}, + {"decompress", (PyCFunction)zlib_decompress, METH_VARARGS|METH_KEYWORDS, zlib_decompress__doc__}, static PyObject * zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits, Py_ssize_t bufsize); static PyObject * -zlib_decompress(PyObject *module, PyObject *args) +zlib_decompress(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; + static const char * const _keywords[] = {"", "wbits", "bufsize", NULL}; + static _PyArg_Parser _parser = {"y*|iO&:decompress", _keywords, 0}; Py_buffer data = {NULL, NULL}; int wbits = MAX_WBITS; Py_ssize_t bufsize = DEF_BUF_SIZE; - if (!PyArg_ParseTuple(args, "y*|iO&:decompress", - &data, &wbits, ssize_t_converter, &bufsize)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &data, &wbits, ssize_t_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; } @@ -121,7 +129,8 @@ static PyObject * zlib_compressobj(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"level", "method", "wbits", "memLevel", "strategy", "zdict", NULL}; + static const char * const _keywords[] = {"level", "method", "wbits", "memLevel", "strategy", "zdict", NULL}; + static _PyArg_Parser _parser = {"|iiiiiy*:compressobj", _keywords, 0}; int level = Z_DEFAULT_COMPRESSION; int method = DEFLATED; int wbits = MAX_WBITS; @@ -129,15 +138,17 @@ zlib_compressobj(PyObject *module, PyObject *args, PyObject *kwargs) int strategy = Z_DEFAULT_STRATEGY; Py_buffer zdict = {NULL, NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiy*:compressobj", _keywords, - &level, &method, &wbits, &memLevel, &strategy, &zdict)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &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; } @@ -164,13 +175,15 @@ static PyObject * zlib_decompressobj(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; - static char *_keywords[] = {"wbits", "zdict", NULL}; + static const char * const _keywords[] = {"wbits", "zdict", NULL}; + static _PyArg_Parser _parser = {"|iO:decompressobj", _keywords, 0}; int wbits = MAX_WBITS; PyObject *zdict = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:decompressobj", _keywords, - &wbits, &zdict)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &wbits, &zdict)) { goto exit; + } return_value = zlib_decompressobj_impl(module, wbits, zdict); exit: @@ -202,20 +215,22 @@ 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; } PyDoc_STRVAR(zlib_Decompress_decompress__doc__, -"decompress($self, data, max_length=0, /)\n" +"decompress($self, data, /, max_length=0)\n" "--\n" "\n" "Return a bytes object containing the decompressed version of the data.\n" @@ -232,28 +247,32 @@ PyDoc_STRVAR(zlib_Decompress_decompress__doc__, "Call the flush() method to clear these buffers."); #define ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF \ - {"decompress", (PyCFunction)zlib_Decompress_decompress, METH_VARARGS, zlib_Decompress_decompress__doc__}, + {"decompress", (PyCFunction)zlib_Decompress_decompress, METH_VARARGS|METH_KEYWORDS, zlib_Decompress_decompress__doc__}, static PyObject * zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, Py_ssize_t max_length); static PyObject * -zlib_Decompress_decompress(compobject *self, PyObject *args) +zlib_Decompress_decompress(compobject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; + static const char * const _keywords[] = {"", "max_length", NULL}; + static _PyArg_Parser _parser = {"y*|O&:decompress", _keywords, 0}; Py_buffer data = {NULL, NULL}; Py_ssize_t max_length = 0; - if (!PyArg_ParseTuple(args, "y*|O&:decompress", - &data, ssize_t_converter, &max_length)) + if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser, + &data, ssize_t_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 +302,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 +377,9 @@ zlib_Decompress_flush(compobject *self, PyObject *args) Py_ssize_t length = DEF_BUF_SIZE; if (!PyArg_ParseTuple(args, "|O&:flush", - ssize_t_converter, &length)) + ssize_t_converter, &length)) { goto exit; + } return_value = zlib_Decompress_flush_impl(self, length); exit: @@ -390,14 +411,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 +450,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 +467,4 @@ exit: #ifndef ZLIB_COMPRESS_COPY_METHODDEF #define ZLIB_COMPRESS_COPY_METHODDEF #endif /* !defined(ZLIB_COMPRESS_COPY_METHODDEF) */ -/*[clinic end generated code: output=7711ef02d1d5776c input=a9049054013a1b77]*/ +/*[clinic end generated code: output=48911ef429b65903 input=a9049054013a1b77]*/ diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c index cba42a7..0e7d4db 100644 --- a/Modules/cmathmodule.c +++ b/Modules/cmathmodule.c @@ -1239,6 +1239,7 @@ PyInit_cmath(void) PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI)); PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E)); + PyModule_AddObject(m, "tau", PyFloat_FromDouble(Py_MATH_TAU)); /* 2pi */ /* initialize special value tables */ diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c index f1fda48..d6322d0 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 cb7222d..0c6f444 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 d8167ea..b6fb53c 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 18deb60..65b47a3 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 3a134a0..9437ae7 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 133f313..b53cc24 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 a9f9993..18fed09 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 6ff241e..c966376 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() constructor 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 e4c955e..b6dcdd0 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 7ebf8e8..43aa229 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)) { @@ -2144,6 +2144,7 @@ PyInit_math(void) PyModule_AddObject(m, "pi", PyFloat_FromDouble(Py_MATH_PI)); PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E)); + PyModule_AddObject(m, "tau", PyFloat_FromDouble(Py_MATH_TAU)); /* 2pi */ PyModule_AddObject(m, "inf", PyFloat_FromDouble(m_inf())); #if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN) PyModule_AddObject(m, "nan", PyFloat_FromDouble(m_nan())); diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index bb98a99..50cec24 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 64eb5db..b6a855c 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 d2fd5c8..2b7d71f 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 6471b8e..82770a5 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 b854caf..10d6bcb 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,67 @@ 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 (PyBytes_Check(o)) { +#ifdef MS_WINDOWS + if (win32_warn_bytes_api()) { + return 0; + } +#endif + bytes = o; + Py_INCREF(bytes); + } + else if (PyObject_CheckBuffer(o)) { + if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, + "%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; + } +#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 @@ -946,12 +967,19 @@ path_converter(PyObject *o, void *p) { path->length = length; path->object = o; path->fd = -1; - path->cleanup = bytes; - return Py_CLEANUP_SUPPORTED; + if (bytes == o) { + Py_DECREF(bytes); + return 1; + } + else { + path->cleanup = bytes; + return Py_CLEANUP_SUPPORTED; + } } 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 +1002,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 +1012,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 +1022,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 +1034,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 +1046,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 +1059,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", @@ -1099,8 +1133,8 @@ _PyVerify_fd_dup2(int fd1, int fd2) static int win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) { - char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; - REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; + char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; + _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; DWORD n_bytes_returned; if (0 == DeviceIoControl( @@ -1159,7 +1193,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 +1221,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 +1256,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 +1271,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 +1492,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 +1502,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 +1592,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 +1682,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 +2136,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 +3348,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 +3505,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 +3677,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 +3859,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) @@ -3908,7 +3950,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; @@ -4106,7 +4149,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 @@ -4286,7 +4329,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 @@ -4566,7 +4609,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; @@ -4608,14 +4651,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; @@ -4631,7 +4674,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; @@ -4774,7 +4817,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 @@ -4828,7 +4871,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 @@ -4925,7 +4968,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); @@ -5040,7 +5084,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; @@ -5160,7 +5204,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; @@ -5238,7 +5282,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; @@ -5758,14 +5802,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; @@ -5926,7 +5970,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 @@ -6251,7 +6295,7 @@ static PyObject * posix_initgroups(PyObject *self, PyObject *args) { PyObject *oname; - char *username; + const char *username; int res; #ifdef __APPLE__ int gid; @@ -7125,16 +7169,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; + char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; + _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; + const wchar_t *print_name; static char *keywords[] = {"path", "dir_fd", NULL}; @@ -7202,8 +7246,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) @@ -7308,7 +7352,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]; @@ -7327,7 +7371,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]; @@ -9015,7 +9059,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) { @@ -9061,8 +9105,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) { @@ -9481,8 +9525,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 @@ -9490,7 +9534,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 { @@ -10451,7 +10498,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; @@ -10578,9 +10625,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; @@ -10821,7 +10868,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); @@ -10985,9 +11032,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 */ @@ -11199,11 +11246,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 @@ -11460,7 +11511,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) @@ -11477,7 +11528,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; @@ -11661,7 +11712,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); @@ -11692,6 +11743,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"}, @@ -11716,6 +11774,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} }; @@ -11755,7 +11816,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; @@ -11830,7 +11891,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; @@ -11862,7 +11923,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 @@ -11925,8 +11986,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; @@ -11946,7 +12013,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; @@ -11977,14 +12044,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; @@ -12006,7 +12079,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; @@ -12043,21 +12116,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 */ @@ -12079,7 +12207,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 */ @@ -12087,6 +12216,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 * @@ -12097,7 +12247,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); @@ -12169,6 +12319,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" @@ -12369,6 +12582,7 @@ static PyMethodDef posix_methods[] = { {"scandir", (PyCFunction)posix_scandir, METH_VARARGS | METH_KEYWORDS, posix_scandir__doc__}, + OS_FSPATH_METHODDEF {NULL, NULL} /* Sentinel */ }; @@ -12380,7 +12594,6 @@ enable_symlink() HANDLE tok; TOKEN_PRIVILEGES tok_priv; LUID luid; - int meth_idx = 0; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) return 0; @@ -12483,12 +12696,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 @@ -12791,25 +13006,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 @@ -12830,7 +13045,7 @@ static struct PyModuleDef posixmodule = { }; -static char *have_functions[] = { +static const char * const have_functions[] = { #ifdef HAVE_FACCESSAT "HAVE_FACCESSAT", @@ -12965,7 +13180,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(); @@ -13141,6 +13356,9 @@ INITFUNC(void) } PyModule_AddObject(m, "_have_functions", list); + Py_INCREF((PyObject *) &DirEntryType); + PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType); + initialized = 1; return m; diff --git a/Modules/pwdmodule.c b/Modules/pwdmodule.c index 7416cf7..784e9d0 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 eb322c2..dc97e9d 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 91f7cca..f8876e0 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 00324a5..80e7873 100644 --- a/Modules/selectmodule.c +++ b/Modules/selectmodule.c @@ -4,6 +4,10 @@ have any value except INVALID_SOCKET. */ +#if defined(HAVE_POLL_H) && !defined(_GNU_SOURCE) +#define _GNU_SOURCE +#endif + #include "Python.h" #include <structmember.h> @@ -1842,7 +1846,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 +2131,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; @@ -2452,6 +2456,10 @@ PyInit_select(void) #ifdef POLLMSG PyModule_AddIntMacro(m, POLLMSG); #endif +#ifdef POLLRDHUP + /* Kernel 2.6.17+ */ + PyModule_AddIntMacro(m, POLLRDHUP); +#endif } #endif /* HAVE_POLL */ @@ -2473,12 +2481,18 @@ PyInit_select(void) PyModule_AddIntMacro(m, EPOLLPRI); PyModule_AddIntMacro(m, EPOLLERR); PyModule_AddIntMacro(m, EPOLLHUP); +#ifdef EPOLLRDHUP + /* Kernel 2.6.17 */ + PyModule_AddIntMacro(m, EPOLLRDHUP); +#endif PyModule_AddIntMacro(m, EPOLLET); #ifdef EPOLLONESHOT /* Kernel 2.6.2+ */ PyModule_AddIntMacro(m, EPOLLONESHOT); #endif - /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */ +#ifdef EPOLLEXCLUSIVE + PyModule_AddIntMacro(m, EPOLLEXCLUSIVE); +#endif #ifdef EPOLLRDNORM PyModule_AddIntMacro(m, EPOLLRDNORM); diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index b34981c..d896cc0 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,17 +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. - */ fd = s->sock_fd; if (fd != INVALID_SOCKET) { s->sock_fd = INVALID_SOCKET; + + /* 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; @@ -4035,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; @@ -4045,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) @@ -4164,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 != INVALID_SOCKET) { - 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 = INVALID_SOCKET; + + /* 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); } @@ -4396,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 */ @@ -4416,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 */ }; @@ -5451,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) @@ -6188,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 */ @@ -6439,7 +6495,10 @@ PyInit__socket(void) PyModule_AddIntMacro(m, SOCK_STREAM); PyModule_AddIntMacro(m, SOCK_DGRAM); /* We have incomplete socket support. */ +#ifdef SOCK_RAW + /* SOCK_RAW is marked as optional in the POSIX specification */ PyModule_AddIntMacro(m, SOCK_RAW); +#endif PyModule_AddIntMacro(m, SOCK_SEQPACKET); #if defined(SOCK_RDM) PyModule_AddIntMacro(m, SOCK_RDM); @@ -6482,9 +6541,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 @@ -6521,6 +6582,12 @@ PyInit__socket(void) #ifdef LOCAL_PEERCRED PyModule_AddIntMacro(m, LOCAL_PEERCRED); #endif +#ifdef SO_PASSSEC + PyModule_AddIntMacro(m, SO_PASSSEC); +#endif +#ifdef SO_PEERSEC + PyModule_AddIntMacro(m, SO_PEERSEC); +#endif #ifdef SO_BINDTODEVICE PyModule_AddIntMacro(m, SO_BINDTODEVICE); #endif @@ -6530,6 +6597,12 @@ PyInit__socket(void) #ifdef SO_MARK PyModule_AddIntMacro(m, SO_MARK); #endif +#ifdef SO_DOMAIN + PyModule_AddIntMacro(m, SO_DOMAIN); +#endif +#ifdef SO_PROTOCOL + PyModule_AddIntMacro(m, SO_PROTOCOL); +#endif /* Maximum number of connections for "listen" */ #ifdef SOMAXCONN @@ -7225,8 +7298,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 4b9f3cd..556a715 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 128c71e..78f7ac7 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 d2caacd..9474644 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 fe4e908..7d518fa 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/winreparse.h b/Modules/winreparse.h index 66f7775..28049c9 100644 --- a/Modules/winreparse.h +++ b/Modules/winreparse.h @@ -2,7 +2,7 @@ #define Py_WINREPARSE_H #ifdef MS_WINDOWS -#include <Windows.h> +#include <windows.h> #ifdef __cplusplus extern "C" { @@ -10,9 +10,10 @@ extern "C" { /* The following structure was copied from http://msdn.microsoft.com/en-us/library/ff552012.aspx as the required - include doesn't seem to be present in the Windows SDK (at least as included - with Visual Studio Express). */ -typedef struct _REPARSE_DATA_BUFFER { + include km\ntifs.h isn't present in the Windows SDK (at least as included + with Visual Studio Express). Use unique names to avoid conflicting with + the structure as defined by Min GW. */ +typedef struct { ULONG ReparseTag; USHORT ReparseDataLength; USHORT Reserved; @@ -38,11 +39,11 @@ typedef struct _REPARSE_DATA_BUFFER { UCHAR DataBuffer[1]; } GenericReparseBuffer; }; -} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; +} _Py_REPARSE_DATA_BUFFER, *_Py_PREPARSE_DATA_BUFFER; -#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ - GenericReparseBuffer) -#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) +#define _Py_REPARSE_DATA_BUFFER_HEADER_SIZE \ + FIELD_OFFSET(_Py_REPARSE_DATA_BUFFER, GenericReparseBuffer) +#define _Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) #ifdef __cplusplus } diff --git a/Modules/xxlimited.c b/Modules/xxlimited.c index 40c1760..c1a9be9 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 85230d9..0764407 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 fccb616..4cded31 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. @@ -199,18 +199,18 @@ arrange_output_buffer(z_stream *zst, PyObject **buffer, Py_ssize_t length) /*[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 *RetVal = NULL; Byte *ibuf; @@ -218,8 +218,8 @@ zlib_compress_impl(PyObject *module, Py_buffer *bytes, int level) int err, flush; z_stream zst; - ibuf = bytes->buf; - ibuflen = bytes->len; + ibuf = data->buf; + ibuflen = data->len; zst.opaque = NULL; zst.zalloc = PyZlib_Malloc; @@ -318,11 +318,11 @@ zlib.decompress data: Py_buffer Compressed data. + / wbits: int(c_default="MAX_WBITS") = MAX_WBITS The window buffer size and container format. bufsize: ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE The initial output buffer size. - / Returns a bytes object containing the uncompressed data. [clinic start generated code]*/ @@ -330,7 +330,7 @@ Returns a bytes object containing the uncompressed data. static PyObject * zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits, Py_ssize_t bufsize) -/*[clinic end generated code: output=77c7e35111dc8c42 input=c13dd2c5696cd17f]*/ +/*[clinic end generated code: output=77c7e35111dc8c42 input=21960936208e9a5b]*/ { PyObject *RetVal = NULL; Byte *ibuf; @@ -750,11 +750,11 @@ zlib.Decompress.decompress data: Py_buffer The binary data to decompress. + / max_length: ssize_t = 0 The maximum allowable length of the decompressed data. Unconsumed input data will be stored in the unconsumed_tail attribute. - / Return a bytes object containing the decompressed version of the data. @@ -766,7 +766,7 @@ Call the flush() method to clear these buffers. static PyObject * zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, Py_ssize_t max_length) -/*[clinic end generated code: output=6e5173c74e710352 input=d6de9b53c4566b8a]*/ +/*[clinic end generated code: output=6e5173c74e710352 input=b85a212a012b770a]*/ { int err = Z_OK; Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE, hard_limit; @@ -1333,7 +1333,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" |