diff options
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/Setup.config.in | 2 | ||||
-rw-r--r-- | Modules/_codecsmodule.c | 16 | ||||
-rw-r--r-- | Modules/_collectionsmodule.c | 50 | ||||
-rw-r--r-- | Modules/_datetimemodule.c | 38 | ||||
-rw-r--r-- | Modules/_decimal/docstrings.h | 860 | ||||
-rw-r--r-- | Modules/_heapqmodule.c | 299 | ||||
-rw-r--r-- | Modules/_io/textio.c | 34 | ||||
-rw-r--r-- | Modules/_operator.c | 4 | ||||
-rw-r--r-- | Modules/_ssl.c | 4 | ||||
-rw-r--r-- | Modules/_struct.c | 9 | ||||
-rw-r--r-- | Modules/_testcapimodule.c | 180 | ||||
-rw-r--r-- | Modules/_testembed.c | 4 | ||||
-rw-r--r-- | Modules/_tracemalloc.c | 60 | ||||
-rw-r--r-- | Modules/_winapi.c | 137 | ||||
-rw-r--r-- | Modules/gcmodule.c | 24 | ||||
-rw-r--r-- | Modules/main.c | 19 | ||||
-rw-r--r-- | Modules/mathmodule.c | 16 | ||||
-rw-r--r-- | Modules/posixmodule.c | 45 | ||||
-rw-r--r-- | Modules/signalmodule.c | 6 | ||||
-rw-r--r-- | Modules/winreparse.h | 53 |
20 files changed, 1110 insertions, 750 deletions
diff --git a/Modules/Setup.config.in b/Modules/Setup.config.in index 5ac2404..adac030 100644 --- a/Modules/Setup.config.in +++ b/Modules/Setup.config.in @@ -7,7 +7,7 @@ @USE_THREAD_MODULE@_thread _threadmodule.c # The signal module -@USE_SIGNAL_MODULE@signal signalmodule.c +@USE_SIGNAL_MODULE@_signal signalmodule.c # The rest of the modules previously listed in this file are built # by the setup.py script in Python 2.1 and later. diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c index 0b093ab..1b21300 100644 --- a/Modules/_codecsmodule.c +++ b/Modules/_codecsmodule.c @@ -89,13 +89,15 @@ a ValueError. Other possible values are 'ignore', 'replace' and\n\ codecs.register_error that can handle ValueErrors."); static PyObject * -codec_encode(PyObject *self, PyObject *args) +codec_encode(PyObject *self, PyObject *args, PyObject *kwargs) { + static char *kwlist[] = {"obj", "encoding", "errors", NULL}; const char *encoding = NULL; const char *errors = NULL; PyObject *v; - if (!PyArg_ParseTuple(args, "O|ss:encode", &v, &encoding, &errors)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|ss:encode", kwlist, + &v, &encoding, &errors)) return NULL; if (encoding == NULL) @@ -116,13 +118,15 @@ as well as any other name registered with codecs.register_error that is\n\ able to handle ValueErrors."); static PyObject * -codec_decode(PyObject *self, PyObject *args) +codec_decode(PyObject *self, PyObject *args, PyObject *kwargs) { + static char *kwlist[] = {"obj", "encoding", "errors", NULL}; const char *encoding = NULL; const char *errors = NULL; PyObject *v; - if (!PyArg_ParseTuple(args, "O|ss:decode", &v, &encoding, &errors)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|ss:decode", kwlist, + &v, &encoding, &errors)) return NULL; if (encoding == NULL) @@ -1120,9 +1124,9 @@ static PyMethodDef _codecs_functions[] = { register__doc__}, {"lookup", codec_lookup, METH_VARARGS, lookup__doc__}, - {"encode", codec_encode, METH_VARARGS, + {"encode", (PyCFunction)codec_encode, METH_VARARGS|METH_KEYWORDS, encode__doc__}, - {"decode", codec_decode, METH_VARARGS, + {"decode", (PyCFunction)codec_decode, METH_VARARGS|METH_KEYWORDS, decode__doc__}, {"escape_encode", escape_encode, METH_VARARGS}, {"escape_decode", escape_decode, METH_VARARGS}, diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index c1aa9a3..c6c8666 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -3,7 +3,7 @@ /* collections module implementation of a deque() datatype Written and maintained by Raymond D. Hettinger <python@rcn.com> - Copyright (c) 2004-2013 Python Software Foundation. + Copyright (c) 2004-2014 Python Software Foundation. All rights reserved. */ @@ -145,6 +145,12 @@ typedef struct { static PyTypeObject deque_type; +/* 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) { @@ -454,6 +460,31 @@ deque_inplace_concat(dequeobject *deque, PyObject *other) return (PyObject *)deque; } +/* The rotate() method is part of the public API and is used internally +as a primitive for other methods. + +Rotation by 1 or -1 is a common case, so any optimizations for high +volume rotations should take care not to penalize the common case. + +Conceptually, a rotate by one is equivalent to a pop on one side and an +append on the other. However, a pop/append pair is unnecessarily slow +because it requires a incref/decref pair for an object located randomly +in memory. It is better to just move the object pointer from one block +to the next without changing the reference count. + +When moving batches of pointers, it is tempting to use memcpy() but that +proved to be slower than a simple loop for a variety of reasons. +Memcpy() cannot know in advance that we're copying pointers instead of +bytes, that the source and destination are pointer aligned and +non-overlapping, that moving just one pointer is a common case, that we +never need to move more than BLOCKLEN pointers, and that at least one +pointer is always moved. + +For high volume rotations, newblock() and freeblock() are never called +more than once. Previously emptied blocks are immediately reused as a +destination block. If a block is left-over at the end, it is freed. +*/ + static int _deque_rotate(dequeobject *deque, Py_ssize_t n) { @@ -1800,18 +1831,29 @@ _count_elements(PyObject *self, PyObject *args) if (mapping_get != NULL && mapping_get == dict_get && mapping_setitem != NULL && mapping_setitem == dict_setitem) { while (1) { + Py_hash_t hash; + key = PyIter_Next(it); if (key == NULL) break; - oldval = PyDict_GetItem(mapping, key); + + if (!PyUnicode_CheckExact(key) || + (hash = ((PyASCIIObject *) key)->hash) == -1) + { + hash = PyObject_Hash(key); + if (hash == -1) + goto done; + } + + oldval = _PyDict_GetItem_KnownHash(mapping, key, hash); if (oldval == NULL) { - if (PyDict_SetItem(mapping, key, one) == -1) + if (_PyDict_SetItem_KnownHash(mapping, key, one, hash) == -1) break; } else { newval = PyNumber_Add(oldval, one); if (newval == NULL) break; - if (PyDict_SetItem(mapping, key, newval) == -1) + if (_PyDict_SetItem_KnownHash(mapping, key, newval, hash) == -1) break; Py_CLEAR(newval); } diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 496ff34..04d9b5d 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -3805,29 +3805,6 @@ time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw) return clone; } -static int -time_bool(PyObject *self) -{ - PyObject *offset, *tzinfo; - int offsecs = 0; - - if (TIME_GET_SECOND(self) || TIME_GET_MICROSECOND(self)) { - /* Since utcoffset is in whole minutes, nothing can - * alter the conclusion that this is nonzero. - */ - return 1; - } - tzinfo = GET_TIME_TZINFO(self); - if (tzinfo != Py_None) { - offset = call_utcoffset(tzinfo, Py_None); - if (offset == NULL) - return -1; - offsecs = GET_TD_DAYS(offset)*86400 + GET_TD_SECONDS(offset); - Py_DECREF(offset); - } - return (TIME_GET_MINUTE(self)*60 - offsecs + TIME_GET_HOUR(self)*3600) != 0; -} - /* Pickle support, a simple use of __reduce__. */ /* Let basestate be the non-tzinfo data string. @@ -3895,19 +3872,6 @@ PyDoc_STR("time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time All arguments are optional. tzinfo may be None, or an instance of\n\ a tzinfo subclass. The remaining arguments may be ints.\n"); -static PyNumberMethods time_as_number = { - 0, /* nb_add */ - 0, /* nb_subtract */ - 0, /* nb_multiply */ - 0, /* nb_remainder */ - 0, /* nb_divmod */ - 0, /* nb_power */ - 0, /* nb_negative */ - 0, /* nb_positive */ - 0, /* nb_absolute */ - (inquiry)time_bool, /* nb_bool */ -}; - static PyTypeObject PyDateTime_TimeType = { PyVarObject_HEAD_INIT(NULL, 0) "datetime.time", /* tp_name */ @@ -3919,7 +3883,7 @@ static PyTypeObject PyDateTime_TimeType = { 0, /* tp_setattr */ 0, /* tp_reserved */ (reprfunc)time_repr, /* tp_repr */ - &time_as_number, /* tp_as_number */ + 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ (hashfunc)time_hash, /* tp_hash */ diff --git a/Modules/_decimal/docstrings.h b/Modules/_decimal/docstrings.h index a6490b9..71029a9 100644 --- a/Modules/_decimal/docstrings.h +++ b/Modules/_decimal/docstrings.h @@ -19,26 +19,30 @@ PyDoc_STRVAR(doc__decimal, "C decimal arithmetic module"); -PyDoc_STRVAR(doc_getcontext,"\n\ -getcontext() - Get the current default context.\n\ +PyDoc_STRVAR(doc_getcontext, +"getcontext($module, /)\n--\n\n\ +Get the current default context.\n\ \n"); -PyDoc_STRVAR(doc_setcontext,"\n\ -setcontext(c) - Set a new default context.\n\ +PyDoc_STRVAR(doc_setcontext, +"setcontext($module, context, /)\n--\n\n\ +Set a new default context.\n\ \n"); -PyDoc_STRVAR(doc_localcontext,"\n\ -localcontext(ctx=None) - Return a context manager that will set the default\n\ -context to a copy of ctx on entry to the with-statement and restore the\n\ -previous default context when exiting the with-statement. If no context is\n\ -specified, a copy of the current default context is used.\n\ +PyDoc_STRVAR(doc_localcontext, +"localcontext($module, /, ctx=None)\n--\n\n\ +Return a context manager that will set the default context to a copy of ctx\n\ +on entry to the with-statement and restore the previous default context when\n\ +exiting the with-statement. If no context is specified, a copy of the current\n\ +default context is used.\n\ \n"); #ifdef EXTRA_FUNCTIONALITY -PyDoc_STRVAR(doc_ieee_context,"\n\ -IEEEContext(bits) - Return a context object initialized to the proper values for\n\ -one of the IEEE interchange formats. The argument must be a multiple of 32 and\n\ -less than IEEE_CONTEXT_MAX_BITS. For the most common values, the constants\n\ +PyDoc_STRVAR(doc_ieee_context, +"IEEEContext($module, bits, /)\n--\n\n\ +Return a context object initialized to the proper values for one of the\n\ +IEEE interchange formats. The argument must be a multiple of 32 and less\n\ +than IEEE_CONTEXT_MAX_BITS. For the most common values, the constants\n\ DECIMAL32, DECIMAL64 and DECIMAL128 are provided.\n\ \n"); #endif @@ -48,32 +52,34 @@ DECIMAL32, DECIMAL64 and DECIMAL128 are provided.\n\ /* Decimal Object and Methods */ /******************************************************************************/ -PyDoc_STRVAR(doc_decimal,"\n\ -Decimal(value=\"0\", context=None): Construct a new Decimal object.\n\ -value can be an integer, string, tuple, or another Decimal object.\n\ -If no value is given, return Decimal('0'). The context does not affect\n\ -the conversion and is only passed to determine if the InvalidOperation\n\ -trap is active.\n\ +PyDoc_STRVAR(doc_decimal, +"Decimal(value=\"0\", context=None)\n--\n\n\ +Construct a new Decimal object. 'value' can be an integer, string, tuple,\n\ +or another Decimal object. If no value is given, return Decimal('0'). The\n\ +context does not affect the conversion and is only passed to determine if\n\ +the InvalidOperation trap is active.\n\ \n"); -PyDoc_STRVAR(doc_adjusted,"\n\ -adjusted() - Return the adjusted exponent of the number.\n\ -\n\ -Defined as exp + digits - 1.\n\ +PyDoc_STRVAR(doc_adjusted, +"adjusted($self, /)\n--\n\n\ +Return the adjusted exponent of the number. Defined as exp + digits - 1.\n\ \n"); -PyDoc_STRVAR(doc_as_tuple,"\n\ -as_tuple() - Return a tuple representation of the number.\n\ +PyDoc_STRVAR(doc_as_tuple, +"as_tuple($self, /)\n--\n\n\ +Return a tuple representation of the number.\n\ \n"); -PyDoc_STRVAR(doc_canonical,"\n\ -canonical() - Return the canonical encoding of the argument. Currently,\n\ -the encoding of a Decimal instance is always canonical, so this operation\n\ -returns its argument unchanged.\n\ +PyDoc_STRVAR(doc_canonical, +"canonical($self, /)\n--\n\n\ +Return the canonical encoding of the argument. Currently, the encoding\n\ +of a Decimal instance is always canonical, so this operation returns its\n\ +argument unchanged.\n\ \n"); -PyDoc_STRVAR(doc_compare,"\n\ -compare(other, context=None) - Compare self to other. Return a decimal value:\n\ +PyDoc_STRVAR(doc_compare, +"compare($self, /, other, context=None)\n--\n\n\ +Compare self to other. Return a decimal value:\n\ \n\ a or b is a NaN ==> Decimal('NaN')\n\ a < b ==> Decimal('-1')\n\ @@ -81,17 +87,18 @@ compare(other, context=None) - Compare self to other. Return a decimal value:\n\ a > b ==> Decimal('1')\n\ \n"); -PyDoc_STRVAR(doc_compare_signal,"\n\ -compare_signal(other, context=None) - Identical to compare, except that\n\ -all NaNs signal.\n\ +PyDoc_STRVAR(doc_compare_signal, +"compare_signal($self, /, other, context=None)\n--\n\n\ +Identical to compare, except that all NaNs signal.\n\ \n"); -PyDoc_STRVAR(doc_compare_total,"\n\ -compare_total(other, context=None) - Compare two operands using their\n\ -abstract representation rather than their numerical value. Similar to the\n\ -compare() method, but the result gives a total ordering on Decimal instances.\n\ -Two Decimal instances with the same numeric value but different representations\n\ -compare unequal in this ordering:\n\ +PyDoc_STRVAR(doc_compare_total, +"compare_total($self, /, other, context=None)\n--\n\n\ +Compare two operands using their abstract representation rather than\n\ +their numerical value. Similar to the compare() method, but the result\n\ +gives a total ordering on Decimal instances. Two Decimal instances with\n\ +the same numeric value but different representations compare unequal\n\ +in this ordering:\n\ \n\ >>> Decimal('12.0').compare_total(Decimal('12'))\n\ Decimal('-1')\n\ @@ -107,36 +114,39 @@ and no rounding is performed. As an exception, the C version may raise\n\ InvalidOperation if the second operand cannot be converted exactly.\n\ \n"); -PyDoc_STRVAR(doc_compare_total_mag,"\n\ -compare_total_mag(other, context=None) - Compare two operands using their\n\ -abstract representation rather than their value as in compare_total(), but\n\ -ignoring the sign of each operand. x.compare_total_mag(y) is equivalent to\n\ -x.copy_abs().compare_total(y.copy_abs()).\n\ +PyDoc_STRVAR(doc_compare_total_mag, +"compare_total_mag($self, /, other, context=None)\n--\n\n\ +Compare two operands using their abstract representation rather than their\n\ +value as in compare_total(), but ignoring the sign of each operand.\n\ +\n\ +x.compare_total_mag(y) is equivalent to x.copy_abs().compare_total(y.copy_abs()).\n\ \n\ This operation is unaffected by context and is quiet: no flags are changed\n\ and no rounding is performed. As an exception, the C version may raise\n\ InvalidOperation if the second operand cannot be converted exactly.\n\ \n"); -PyDoc_STRVAR(doc_conjugate,"\n\ -conjugate() - Return self.\n\ +PyDoc_STRVAR(doc_conjugate, +"conjugate($self, /)\n--\n\n\ +Return self.\n\ \n"); -PyDoc_STRVAR(doc_copy_abs,"\n\ -copy_abs() - Return the absolute value of the argument. This operation\n\ -is unaffected by context and is quiet: no flags are changed and no rounding\n\ -is performed.\n\ +PyDoc_STRVAR(doc_copy_abs, +"copy_abs($self, /)\n--\n\n\ +Return the absolute value of the argument. This operation is unaffected by\n\ +context and is quiet: no flags are changed and no rounding is performed.\n\ \n"); -PyDoc_STRVAR(doc_copy_negate,"\n\ -copy_negate() - Return the negation of the argument. This operation is\n\ -unaffected by context and is quiet: no flags are changed and no rounding\n\ -is performed.\n\ +PyDoc_STRVAR(doc_copy_negate, +"copy_negate($self, /)\n--\n\n\ +Return the negation of the argument. This operation is unaffected by context\n\ +and is quiet: no flags are changed and no rounding is performed.\n\ \n"); -PyDoc_STRVAR(doc_copy_sign,"\n\ -copy_sign(other, context=None) - Return a copy of the first operand with\n\ -the sign set to be the same as the sign of the second operand. For example:\n\ +PyDoc_STRVAR(doc_copy_sign, +"copy_sign($self, /, other, context=None)\n--\n\n\ +Return a copy of the first operand with the sign set to be the same as the\n\ +sign of the second operand. For example:\n\ \n\ >>> Decimal('2.3').copy_sign(Decimal('-1.5'))\n\ Decimal('-2.3')\n\ @@ -146,14 +156,16 @@ and no rounding is performed. As an exception, the C version may raise\n\ InvalidOperation if the second operand cannot be converted exactly.\n\ \n"); -PyDoc_STRVAR(doc_exp,"\n\ -exp(context=None) - Return the value of the (natural) exponential function\n\ -e**x at the given number. The function always uses the ROUND_HALF_EVEN mode\n\ -and the result is correctly rounded.\n\ +PyDoc_STRVAR(doc_exp, +"exp($self, /, context=None)\n--\n\n\ +Return the value of the (natural) exponential function e**x at the given\n\ +number. The function always uses the ROUND_HALF_EVEN mode and the result\n\ +is correctly rounded.\n\ \n"); -PyDoc_STRVAR(doc_from_float,"\n\ -from_float(f) - Class method that converts a float to a decimal number, exactly.\n\ +PyDoc_STRVAR(doc_from_float, +"from_float($type, f, /)\n--\n\n\ +Class method that converts a float to a decimal number, exactly.\n\ Since 0.1 is not exactly representable in binary floating point,\n\ Decimal.from_float(0.1) is not the same as Decimal('0.1').\n\ \n\ @@ -168,155 +180,176 @@ Decimal.from_float(0.1) is not the same as Decimal('0.1').\n\ \n\ \n"); -PyDoc_STRVAR(doc_fma,"\n\ -fma(other, third, context=None) - Fused multiply-add. Return self*other+third\n\ -with no rounding of the intermediate product self*other.\n\ +PyDoc_STRVAR(doc_fma, +"fma($self, /, other, third, context=None)\n--\n\n\ +Fused multiply-add. Return self*other+third with no rounding of the\n\ +intermediate product self*other.\n\ \n\ >>> Decimal(2).fma(3, 5)\n\ Decimal('11')\n\ \n\ \n"); -PyDoc_STRVAR(doc_is_canonical,"\n\ -is_canonical() - Return True if the argument is canonical and False otherwise.\n\ -Currently, a Decimal instance is always canonical, so this operation always\n\ -returns True.\n\ +PyDoc_STRVAR(doc_is_canonical, +"is_canonical($self, /)\n--\n\n\ +Return True if the argument is canonical and False otherwise. Currently,\n\ +a Decimal instance is always canonical, so this operation always returns\n\ +True.\n\ \n"); -PyDoc_STRVAR(doc_is_finite,"\n\ -is_finite() - Return True if the argument is a finite number, and False if the\n\ -argument is infinite or a NaN.\n\ +PyDoc_STRVAR(doc_is_finite, +"is_finite($self, /)\n--\n\n\ +Return True if the argument is a finite number, and False if the argument\n\ +is infinite or a NaN.\n\ \n"); -PyDoc_STRVAR(doc_is_infinite,"\n\ -is_infinite() - Return True if the argument is either positive or negative\n\ -infinity and False otherwise.\n\ +PyDoc_STRVAR(doc_is_infinite, +"is_infinite($self, /)\n--\n\n\ +Return True if the argument is either positive or negative infinity and\n\ +False otherwise.\n\ \n"); -PyDoc_STRVAR(doc_is_nan,"\n\ -is_nan() - Return True if the argument is a (quiet or signaling) NaN and\n\ -False otherwise.\n\ +PyDoc_STRVAR(doc_is_nan, +"is_nan($self, /)\n--\n\n\ +Return True if the argument is a (quiet or signaling) NaN and False\n\ +otherwise.\n\ \n"); -PyDoc_STRVAR(doc_is_normal,"\n\ -is_normal(context=None) - Return True if the argument is a normal finite\n\ -non-zero number with an adjusted exponent greater than or equal to Emin.\n\ -Return False if the argument is zero, subnormal, infinite or a NaN.\n\ +PyDoc_STRVAR(doc_is_normal, +"is_normal($self, /, context=None)\n--\n\n\ +Return True if the argument is a normal finite non-zero number with an\n\ +adjusted exponent greater than or equal to Emin. Return False if the\n\ +argument is zero, subnormal, infinite or a NaN.\n\ \n"); -PyDoc_STRVAR(doc_is_qnan,"\n\ -is_qnan() - Return True if the argument is a quiet NaN, and False otherwise.\n\ +PyDoc_STRVAR(doc_is_qnan, +"is_qnan($self, /)\n--\n\n\ +Return True if the argument is a quiet NaN, and False otherwise.\n\ \n"); -PyDoc_STRVAR(doc_is_signed,"\n\ -is_signed() - Return True if the argument has a negative sign and\n\ -False otherwise. Note that both zeros and NaNs can carry signs.\n\ +PyDoc_STRVAR(doc_is_signed, +"is_signed($self, /)\n--\n\n\ +Return True if the argument has a negative sign and False otherwise.\n\ +Note that both zeros and NaNs can carry signs.\n\ \n"); -PyDoc_STRVAR(doc_is_snan,"\n\ -is_snan() - Return True if the argument is a signaling NaN and False otherwise.\n\ +PyDoc_STRVAR(doc_is_snan, +"is_snan($self, /)\n--\n\n\ +Return True if the argument is a signaling NaN and False otherwise.\n\ \n"); -PyDoc_STRVAR(doc_is_subnormal,"\n\ -is_subnormal(context=None) - Return True if the argument is subnormal, and\n\ -False otherwise. A number is subnormal if it is non-zero, finite, and has an\n\ -adjusted exponent less than Emin.\n\ +PyDoc_STRVAR(doc_is_subnormal, +"is_subnormal($self, /, context=None)\n--\n\n\ +Return True if the argument is subnormal, and False otherwise. A number is\n\ +subnormal if it is non-zero, finite, and has an adjusted exponent less\n\ +than Emin.\n\ \n"); -PyDoc_STRVAR(doc_is_zero,"\n\ -is_zero() - Return True if the argument is a (positive or negative) zero and\n\ -False otherwise.\n\ +PyDoc_STRVAR(doc_is_zero, +"is_zero($self, /)\n--\n\n\ +Return True if the argument is a (positive or negative) zero and False\n\ +otherwise.\n\ \n"); -PyDoc_STRVAR(doc_ln,"\n\ -ln(context=None) - Return the natural (base e) logarithm of the operand.\n\ -The function always uses the ROUND_HALF_EVEN mode and the result is\n\ -correctly rounded.\n\ +PyDoc_STRVAR(doc_ln, +"ln($self, /, context=None)\n--\n\n\ +Return the natural (base e) logarithm of the operand. The function always\n\ +uses the ROUND_HALF_EVEN mode and the result is correctly rounded.\n\ \n"); -PyDoc_STRVAR(doc_log10,"\n\ -log10(context=None) - Return the base ten logarithm of the operand.\n\ -The function always uses the ROUND_HALF_EVEN mode and the result is\n\ -correctly rounded.\n\ +PyDoc_STRVAR(doc_log10, +"log10($self, /, context=None)\n--\n\n\ +Return the base ten logarithm of the operand. The function always uses the\n\ +ROUND_HALF_EVEN mode and the result is correctly rounded.\n\ \n"); -PyDoc_STRVAR(doc_logb,"\n\ -logb(context=None) - For a non-zero number, return the adjusted exponent\n\ -of the operand as a Decimal instance. If the operand is a zero, then\n\ -Decimal('-Infinity') is returned and the DivisionByZero condition is\n\ -raised. If the operand is an infinity then Decimal('Infinity') is returned.\n\ +PyDoc_STRVAR(doc_logb, +"logb($self, /, context=None)\n--\n\n\ +For a non-zero number, return the adjusted exponent of the operand as a\n\ +Decimal instance. If the operand is a zero, then Decimal('-Infinity') is\n\ +returned and the DivisionByZero condition is raised. If the operand is\n\ +an infinity then Decimal('Infinity') is returned.\n\ \n"); -PyDoc_STRVAR(doc_logical_and,"\n\ -logical_and(other, context=None) - Return the digit-wise and of the two\n\ -(logical) operands.\n\ +PyDoc_STRVAR(doc_logical_and, +"logical_and($self, /, other, context=None)\n--\n\n\ +Return the digit-wise 'and' of the two (logical) operands.\n\ \n"); -PyDoc_STRVAR(doc_logical_invert,"\n\ -logical_invert(context=None) - Return the digit-wise inversion of the\n\ -(logical) operand.\n\ +PyDoc_STRVAR(doc_logical_invert, +"logical_invert($self, /, context=None)\n--\n\n\ +Return the digit-wise inversion of the (logical) operand.\n\ \n"); -PyDoc_STRVAR(doc_logical_or,"\n\ -logical_or(other, context=None) - Return the digit-wise or of the two\n\ -(logical) operands.\n\ +PyDoc_STRVAR(doc_logical_or, +"logical_or($self, /, other, context=None)\n--\n\n\ +Return the digit-wise 'or' of the two (logical) operands.\n\ \n"); -PyDoc_STRVAR(doc_logical_xor,"\n\ -logical_xor(other, context=None) - Return the digit-wise exclusive or of the\n\ -two (logical) operands.\n\ +PyDoc_STRVAR(doc_logical_xor, +"logical_xor($self, /, other, context=None)\n--\n\n\ +Return the digit-wise 'exclusive or' of the two (logical) operands.\n\ \n"); -PyDoc_STRVAR(doc_max,"\n\ -max(other, context=None) - Maximum of self and other. If one operand is a\n\ -quiet NaN and the other is numeric, the numeric operand is returned.\n\ +PyDoc_STRVAR(doc_max, +"max($self, /, other, context=None)\n--\n\n\ +Maximum of self and other. If one operand is a quiet NaN and the other is\n\ +numeric, the numeric operand is returned.\n\ \n"); -PyDoc_STRVAR(doc_max_mag,"\n\ -max_mag(other, context=None) - Similar to the max() method, but the\n\ -comparison is done using the absolute values of the operands.\n\ +PyDoc_STRVAR(doc_max_mag, +"max_mag($self, /, other, context=None)\n--\n\n\ +Similar to the max() method, but the comparison is done using the absolute\n\ +values of the operands.\n\ \n"); -PyDoc_STRVAR(doc_min,"\n\ -min(other, context=None) - Minimum of self and other. If one operand is a\n\ -quiet NaN and the other is numeric, the numeric operand is returned.\n\ +PyDoc_STRVAR(doc_min, +"min($self, /, other, context=None)\n--\n\n\ +Minimum of self and other. If one operand is a quiet NaN and the other is\n\ +numeric, the numeric operand is returned.\n\ \n"); -PyDoc_STRVAR(doc_min_mag,"\n\ -min_mag(other, context=None) - Similar to the min() method, but the\n\ -comparison is done using the absolute values of the operands.\n\ +PyDoc_STRVAR(doc_min_mag, +"min_mag($self, /, other, context=None)\n--\n\n\ +Similar to the min() method, but the comparison is done using the absolute\n\ +values of the operands.\n\ \n"); -PyDoc_STRVAR(doc_next_minus,"\n\ -next_minus(context=None) - Return the largest number representable in the\n\ -given context (or in the current default context if no context is given) that\n\ -is smaller than the given operand.\n\ +PyDoc_STRVAR(doc_next_minus, +"next_minus($self, /, context=None)\n--\n\n\ +Return the largest number representable in the given context (or in the\n\ +current default context if no context is given) that is smaller than the\n\ +given operand.\n\ \n"); -PyDoc_STRVAR(doc_next_plus,"\n\ -next_plus(context=None) - Return the smallest number representable in the\n\ -given context (or in the current default context if no context is given) that\n\ -is larger than the given operand.\n\ +PyDoc_STRVAR(doc_next_plus, +"next_plus($self, /, context=None)\n--\n\n\ +Return the smallest number representable in the given context (or in the\n\ +current default context if no context is given) that is larger than the\n\ +given operand.\n\ \n"); -PyDoc_STRVAR(doc_next_toward,"\n\ -next_toward(other, context=None) - If the two operands are unequal, return\n\ -the number closest to the first operand in the direction of the second operand.\n\ -If both operands are numerically equal, return a copy of the first operand\n\ -with the sign set to be the same as the sign of the second operand.\n\ +PyDoc_STRVAR(doc_next_toward, +"next_toward($self, /, other, context=None)\n--\n\n\ +If the two operands are unequal, return the number closest to the first\n\ +operand in the direction of the second operand. If both operands are\n\ +numerically equal, return a copy of the first operand with the sign set\n\ +to be the same as the sign of the second operand.\n\ \n"); -PyDoc_STRVAR(doc_normalize,"\n\ -normalize(context=None) - Normalize the number by stripping the rightmost\n\ -trailing zeros and converting any result equal to Decimal('0') to Decimal('0e0').\n\ -Used for producing canonical values for members of an equivalence class. For\n\ -example, Decimal('32.100') and Decimal('0.321000e+2') both normalize to the\n\ -equivalent value Decimal('32.1').\n\ +PyDoc_STRVAR(doc_normalize, +"normalize($self, /, context=None)\n--\n\n\ +Normalize the number by stripping the rightmost trailing zeros and\n\ +converting any result equal to Decimal('0') to Decimal('0e0'). Used\n\ +for producing canonical values for members of an equivalence class.\n\ +For example, Decimal('32.100') and Decimal('0.321000e+2') both normalize\n\ +to the equivalent value Decimal('32.1').\n\ \n"); -PyDoc_STRVAR(doc_number_class,"\n\ -number_class(context=None) - Return a string describing the class of the\n\ -operand. The returned value is one of the following ten strings:\n\ +PyDoc_STRVAR(doc_number_class, +"number_class($self, /, context=None)\n--\n\n\ +Return a string describing the class of the operand. The returned value\n\ +is one of the following ten strings:\n\ \n\ * '-Infinity', indicating that the operand is negative infinity.\n\ * '-Normal', indicating that the operand is a negative normal number.\n\ @@ -331,9 +364,10 @@ operand. The returned value is one of the following ten strings:\n\ \n\ \n"); -PyDoc_STRVAR(doc_quantize,"\n\ -quantize(exp, rounding=None, context=None) - Return a value equal to the\n\ -first operand after rounding and having the exponent of the second operand.\n\ +PyDoc_STRVAR(doc_quantize, +"quantize($self, /, exp, rounding=None, context=None)\n--\n\n\ +Return a value equal to the first operand after rounding and having the\n\ +exponent of the second operand.\n\ \n\ >>> Decimal('1.41421356').quantize(Decimal('1.000'))\n\ Decimal('1.414')\n\ @@ -352,93 +386,98 @@ rounding argument if given, else by the given context argument; if neither\n\ argument is given, the rounding mode of the current thread's context is used.\n\ \n"); -PyDoc_STRVAR(doc_radix,"\n\ -radix() - Return Decimal(10), the radix (base) in which the Decimal class does\n\ +PyDoc_STRVAR(doc_radix, +"radix($self, /)\n--\n\n\ +Return Decimal(10), the radix (base) in which the Decimal class does\n\ all its arithmetic. Included for compatibility with the specification.\n\ \n"); -PyDoc_STRVAR(doc_remainder_near,"\n\ -remainder_near(other, context=None) - Return the remainder from dividing\n\ -self by other. This differs from self % other in that the sign of the\n\ -remainder is chosen so as to minimize its absolute value. More precisely, the\n\ -return value is self - n * other where n is the integer nearest to the exact\n\ -value of self / other, and if two integers are equally near then the even one\n\ -is chosen.\n\ +PyDoc_STRVAR(doc_remainder_near, +"remainder_near($self, /, other, context=None)\n--\n\n\ +Return the remainder from dividing self by other. This differs from\n\ +self % other in that the sign of the remainder is chosen so as to minimize\n\ +its absolute value. More precisely, the return value is self - n * other\n\ +where n is the integer nearest to the exact value of self / other, and\n\ +if two integers are equally near then the even one is chosen.\n\ \n\ If the result is zero then its sign will be the sign of self.\n\ \n"); -PyDoc_STRVAR(doc_rotate,"\n\ -rotate(other, context=None) - Return the result of rotating the digits of the\n\ -first operand by an amount specified by the second operand. The second operand\n\ -must be an integer in the range -precision through precision. The absolute\n\ -value of the second operand gives the number of places to rotate. If the second\n\ -operand is positive then rotation is to the left; otherwise rotation is to the\n\ -right. The coefficient of the first operand is padded on the left with zeros to\n\ +PyDoc_STRVAR(doc_rotate, +"rotate($self, /, other, context=None)\n--\n\n\ +Return the result of rotating the digits of the first operand by an amount\n\ +specified by the second operand. The second operand must be an integer in\n\ +the range -precision through precision. The absolute value of the second\n\ +operand gives the number of places to rotate. If the second operand is\n\ +positive then rotation is to the left; otherwise rotation is to the right.\n\ +The coefficient of the first operand is padded on the left with zeros to\n\ length precision if necessary. The sign and exponent of the first operand are\n\ unchanged.\n\ \n"); -PyDoc_STRVAR(doc_same_quantum,"\n\ -same_quantum(other, context=None) - Test whether self and other have the\n\ -same exponent or whether both are NaN.\n\ +PyDoc_STRVAR(doc_same_quantum, +"same_quantum($self, /, other, context=None)\n--\n\n\ +Test whether self and other have the same exponent or whether both are NaN.\n\ \n\ This operation is unaffected by context and is quiet: no flags are changed\n\ and no rounding is performed. As an exception, the C version may raise\n\ InvalidOperation if the second operand cannot be converted exactly.\n\ \n"); -PyDoc_STRVAR(doc_scaleb,"\n\ -scaleb(other, context=None) - Return the first operand with the exponent\n\ -adjusted the second. Equivalently, return the first operand multiplied by\n\ -10**other. The second operand must be an integer.\n\ +PyDoc_STRVAR(doc_scaleb, +"scaleb($self, /, other, context=None)\n--\n\n\ +Return the first operand with the exponent adjusted the second. Equivalently,\n\ +return the first operand multiplied by 10**other. The second operand must be\n\ +an integer.\n\ \n"); -PyDoc_STRVAR(doc_shift,"\n\ -shift(other, context=None) - Return the result of shifting the digits of\n\ -the first operand by an amount specified by the second operand. The second\n\ -operand must be an integer in the range -precision through precision. The\n\ -absolute value of the second operand gives the number of places to shift.\n\ -If the second operand is positive, then the shift is to the left; otherwise\n\ -the shift is to the right. Digits shifted into the coefficient are zeros.\n\ -The sign and exponent of the first operand are unchanged.\n\ +PyDoc_STRVAR(doc_shift, +"shift($self, /, other, context=None)\n--\n\n\ +Return the result of shifting the digits of the first operand by an amount\n\ +specified by the second operand. The second operand must be an integer in\n\ +the range -precision through precision. The absolute value of the second\n\ +operand gives the number of places to shift. If the second operand is\n\ +positive, then the shift is to the left; otherwise the shift is to the\n\ +right. Digits shifted into the coefficient are zeros. The sign and exponent\n\ +of the first operand are unchanged.\n\ \n"); -PyDoc_STRVAR(doc_sqrt,"\n\ -sqrt(context=None) - Return the square root of the argument to full precision.\n\ -The result is correctly rounded using the ROUND_HALF_EVEN rounding mode.\n\ +PyDoc_STRVAR(doc_sqrt, +"sqrt($self, /, context=None)\n--\n\n\ +Return the square root of the argument to full precision. The result is\n\ +correctly rounded using the ROUND_HALF_EVEN rounding mode.\n\ \n"); -PyDoc_STRVAR(doc_to_eng_string,"\n\ -to_eng_string(context=None) - Convert to an engineering-type string.\n\ -Engineering notation has an exponent which is a multiple of 3, so there\n\ -are up to 3 digits left of the decimal place. For example, Decimal('123E+1')\n\ -is converted to Decimal('1.23E+3').\n\ +PyDoc_STRVAR(doc_to_eng_string, +"to_eng_string($self, /, context=None)\n--\n\n\ +Convert to an engineering-type string. Engineering notation has an exponent\n\ +which is a multiple of 3, so there are up to 3 digits left of the decimal\n\ +place. For example, Decimal('123E+1') is converted to Decimal('1.23E+3').\n\ \n\ The value of context.capitals determines whether the exponent sign is lower\n\ or upper case. Otherwise, the context does not affect the operation.\n\ \n"); -PyDoc_STRVAR(doc_to_integral,"\n\ -to_integral(rounding=None, context=None) - Identical to the\n\ -to_integral_value() method. The to_integral() name has been kept\n\ -for compatibility with older versions.\n\ +PyDoc_STRVAR(doc_to_integral, +"to_integral($self, /, rounding=None, context=None)\n--\n\n\ +Identical to the to_integral_value() method. The to_integral() name has been\n\ +kept for compatibility with older versions.\n\ \n"); -PyDoc_STRVAR(doc_to_integral_exact,"\n\ -to_integral_exact(rounding=None, context=None) - Round to the nearest\n\ -integer, signaling Inexact or Rounded as appropriate if rounding occurs.\n\ -The rounding mode is determined by the rounding parameter if given, else\n\ -by the given context. If neither parameter is given, then the rounding mode\n\ -of the current default context is used.\n\ +PyDoc_STRVAR(doc_to_integral_exact, +"to_integral_exact($self, /, rounding=None, context=None)\n--\n\n\ +Round to the nearest integer, signaling Inexact or Rounded as appropriate if\n\ +rounding occurs. The rounding mode is determined by the rounding parameter\n\ +if given, else by the given context. If neither parameter is given, then the\n\ +rounding mode of the current default context is used.\n\ \n"); -PyDoc_STRVAR(doc_to_integral_value,"\n\ -to_integral_value(rounding=None, context=None) - Round to the nearest\n\ -integer without signaling Inexact or Rounded. The rounding mode is determined\n\ -by the rounding parameter if given, else by the given context. If neither\n\ -parameter is given, then the rounding mode of the current default context is\n\ -used.\n\ +PyDoc_STRVAR(doc_to_integral_value, +"to_integral_value($self, /, rounding=None, context=None)\n--\n\n\ +Round to the nearest integer without signaling Inexact or Rounded. The\n\ +rounding mode is determined by the rounding parameter if given, else by\n\ +the given context. If neither parameter is given, then the rounding mode\n\ +of the current default context is used.\n\ \n"); @@ -446,9 +485,10 @@ used.\n\ /* Context Object and Methods */ /******************************************************************************/ -PyDoc_STRVAR(doc_context,"\n\ +PyDoc_STRVAR(doc_context, +"Context(prec=None, rounding=None, Emin=None, Emax=None, capitals=None, clamp=None, flags=None, traps=None)\n--\n\n\ The context affects almost all operations and controls rounding,\n\ -Over/Underflow, raising of exceptions and much more. A new context\n\ +Over/Underflow, raising of exceptions and much more. A new context\n\ can be constructed as follows:\n\ \n\ >>> c = Context(prec=28, Emin=-425000000, Emax=425000000,\n\ @@ -460,308 +500,372 @@ can be constructed as follows:\n\ \n"); #ifdef EXTRA_FUNCTIONALITY -PyDoc_STRVAR(doc_ctx_apply,"\n\ -apply(x) - Apply self to Decimal x.\n\ +PyDoc_STRVAR(doc_ctx_apply, +"apply($self, x, /)\n--\n\n\ +Apply self to Decimal x.\n\ \n"); #endif -PyDoc_STRVAR(doc_ctx_clear_flags,"\n\ -clear_flags() - Reset all flags to False.\n\ +PyDoc_STRVAR(doc_ctx_clear_flags, +"clear_flags($self, /)\n--\n\n\ +Reset all flags to False.\n\ \n"); -PyDoc_STRVAR(doc_ctx_clear_traps,"\n\ -clear_traps() - Set all traps to False.\n\ +PyDoc_STRVAR(doc_ctx_clear_traps, +"clear_traps($self, /)\n--\n\n\ +Set all traps to False.\n\ \n"); -PyDoc_STRVAR(doc_ctx_copy,"\n\ -copy() - Return a duplicate of the context with all flags cleared.\n\ +PyDoc_STRVAR(doc_ctx_copy, +"copy($self, /)\n--\n\n\ +Return a duplicate of the context with all flags cleared.\n\ \n"); -PyDoc_STRVAR(doc_ctx_copy_decimal,"\n\ -copy_decimal(x) - Return a copy of Decimal x.\n\ +PyDoc_STRVAR(doc_ctx_copy_decimal, +"copy_decimal($self, x, /)\n--\n\n\ +Return a copy of Decimal x.\n\ \n"); -PyDoc_STRVAR(doc_ctx_create_decimal,"\n\ -create_decimal(x) - Create a new Decimal instance from x, using self as the\n\ -context. Unlike the Decimal constructor, this function observes the context\n\ -limits.\n\ +PyDoc_STRVAR(doc_ctx_create_decimal, +"create_decimal($self, num=\"0\", /)\n--\n\n\ +Create a new Decimal instance from num, using self as the context. Unlike the\n\ +Decimal constructor, this function observes the context limits.\n\ \n"); -PyDoc_STRVAR(doc_ctx_create_decimal_from_float,"\n\ -create_decimal_from_float(f) - Create a new Decimal instance from float f.\n\ -Unlike the Decimal.from_float() class method, this function observes the\n\ -context limits.\n\ +PyDoc_STRVAR(doc_ctx_create_decimal_from_float, +"create_decimal_from_float($self, f, /)\n--\n\n\ +Create a new Decimal instance from float f. Unlike the Decimal.from_float()\n\ +class method, this function observes the context limits.\n\ \n"); -PyDoc_STRVAR(doc_ctx_Etiny,"\n\ -Etiny() - Return a value equal to Emin - prec + 1, which is the minimum\n\ -exponent value for subnormal results. When underflow occurs, the exponent\n\ -is set to Etiny.\n\ +PyDoc_STRVAR(doc_ctx_Etiny, +"Etiny($self, /)\n--\n\n\ +Return a value equal to Emin - prec + 1, which is the minimum exponent value\n\ +for subnormal results. When underflow occurs, the exponent is set to Etiny.\n\ \n"); -PyDoc_STRVAR(doc_ctx_Etop,"\n\ -Etop() - Return a value equal to Emax - prec + 1. This is the maximum exponent\n\ -if the _clamp field of the context is set to 1 (IEEE clamp mode). Etop() must\n\ -not be negative.\n\ +PyDoc_STRVAR(doc_ctx_Etop, +"Etop($self, /)\n--\n\n\ +Return a value equal to Emax - prec + 1. This is the maximum exponent\n\ +if the _clamp field of the context is set to 1 (IEEE clamp mode). Etop()\n\ +must not be negative.\n\ \n"); -PyDoc_STRVAR(doc_ctx_abs,"\n\ -abs(x) - Return the absolute value of x.\n\ +PyDoc_STRVAR(doc_ctx_abs, +"abs($self, x, /)\n--\n\n\ +Return the absolute value of x.\n\ \n"); -PyDoc_STRVAR(doc_ctx_add,"\n\ -add(x, y) - Return the sum of x and y.\n\ +PyDoc_STRVAR(doc_ctx_add, +"add($self, x, y, /)\n--\n\n\ +Return the sum of x and y.\n\ \n"); -PyDoc_STRVAR(doc_ctx_canonical,"\n\ -canonical(x) - Return a new instance of x.\n\ +PyDoc_STRVAR(doc_ctx_canonical, +"canonical($self, x, /)\n--\n\n\ +Return a new instance of x.\n\ \n"); -PyDoc_STRVAR(doc_ctx_compare,"\n\ -compare(x, y) - Compare x and y numerically.\n\ +PyDoc_STRVAR(doc_ctx_compare, +"compare($self, x, y, /)\n--\n\n\ +Compare x and y numerically.\n\ \n"); -PyDoc_STRVAR(doc_ctx_compare_signal,"\n\ -compare_signal(x, y) - Compare x and y numerically. All NaNs signal.\n\ +PyDoc_STRVAR(doc_ctx_compare_signal, +"compare_signal($self, x, y, /)\n--\n\n\ +Compare x and y numerically. All NaNs signal.\n\ \n"); -PyDoc_STRVAR(doc_ctx_compare_total,"\n\ -compare_total(x, y) - Compare x and y using their abstract representation.\n\ +PyDoc_STRVAR(doc_ctx_compare_total, +"compare_total($self, x, y, /)\n--\n\n\ +Compare x and y using their abstract representation.\n\ \n"); -PyDoc_STRVAR(doc_ctx_compare_total_mag,"\n\ -compare_total_mag(x, y) - Compare x and y using their abstract representation,\n\ -ignoring sign.\n\ +PyDoc_STRVAR(doc_ctx_compare_total_mag, +"compare_total_mag($self, x, y, /)\n--\n\n\ +Compare x and y using their abstract representation, ignoring sign.\n\ \n"); -PyDoc_STRVAR(doc_ctx_copy_abs,"\n\ -copy_abs(x) - Return a copy of x with the sign set to 0.\n\ +PyDoc_STRVAR(doc_ctx_copy_abs, +"copy_abs($self, x, /)\n--\n\n\ +Return a copy of x with the sign set to 0.\n\ \n"); -PyDoc_STRVAR(doc_ctx_copy_negate,"\n\ -copy_negate(x) - Return a copy of x with the sign inverted.\n\ +PyDoc_STRVAR(doc_ctx_copy_negate, +"copy_negate($self, x, /)\n--\n\n\ +Return a copy of x with the sign inverted.\n\ \n"); -PyDoc_STRVAR(doc_ctx_copy_sign,"\n\ -copy_sign(x, y) - Copy the sign from y to x.\n\ +PyDoc_STRVAR(doc_ctx_copy_sign, +"copy_sign($self, x, y, /)\n--\n\n\ +Copy the sign from y to x.\n\ \n"); -PyDoc_STRVAR(doc_ctx_divide,"\n\ -divide(x, y) - Return x divided by y.\n\ +PyDoc_STRVAR(doc_ctx_divide, +"divide($self, x, y, /)\n--\n\n\ +Return x divided by y.\n\ \n"); -PyDoc_STRVAR(doc_ctx_divide_int,"\n\ -divide_int(x, y) - Return x divided by y, truncated to an integer.\n\ +PyDoc_STRVAR(doc_ctx_divide_int, +"divide_int($self, x, y, /)\n--\n\n\ +Return x divided by y, truncated to an integer.\n\ \n"); -PyDoc_STRVAR(doc_ctx_divmod,"\n\ -divmod(x, y) - Return quotient and remainder of the division x / y.\n\ +PyDoc_STRVAR(doc_ctx_divmod, +"divmod($self, x, y, /)\n--\n\n\ +Return quotient and remainder of the division x / y.\n\ \n"); -PyDoc_STRVAR(doc_ctx_exp,"\n\ -exp(x) - Return e ** x.\n\ +PyDoc_STRVAR(doc_ctx_exp, +"exp($self, x, /)\n--\n\n\ +Return e ** x.\n\ \n"); -PyDoc_STRVAR(doc_ctx_fma,"\n\ -fma(x, y, z) - Return x multiplied by y, plus z.\n\ +PyDoc_STRVAR(doc_ctx_fma, +"fma($self, x, y, z, /)\n--\n\n\ +Return x multiplied by y, plus z.\n\ \n"); -PyDoc_STRVAR(doc_ctx_is_canonical,"\n\ -is_canonical(x) - Return True if x is canonical, False otherwise.\n\ +PyDoc_STRVAR(doc_ctx_is_canonical, +"is_canonical($self, x, /)\n--\n\n\ +Return True if x is canonical, False otherwise.\n\ \n"); -PyDoc_STRVAR(doc_ctx_is_finite,"\n\ -is_finite(x) - Return True if x is finite, False otherwise.\n\ +PyDoc_STRVAR(doc_ctx_is_finite, +"is_finite($self, x, /)\n--\n\n\ +Return True if x is finite, False otherwise.\n\ \n"); -PyDoc_STRVAR(doc_ctx_is_infinite,"\n\ -is_infinite(x) - Return True if x is infinite, False otherwise.\n\ +PyDoc_STRVAR(doc_ctx_is_infinite, +"is_infinite($self, x, /)\n--\n\n\ +Return True if x is infinite, False otherwise.\n\ \n"); -PyDoc_STRVAR(doc_ctx_is_nan,"\n\ -is_nan(x) - Return True if x is a qNaN or sNaN, False otherwise.\n\ +PyDoc_STRVAR(doc_ctx_is_nan, +"is_nan($self, x, /)\n--\n\n\ +Return True if x is a qNaN or sNaN, False otherwise.\n\ \n"); -PyDoc_STRVAR(doc_ctx_is_normal,"\n\ -is_normal(x) - Return True if x is a normal number, False otherwise.\n\ +PyDoc_STRVAR(doc_ctx_is_normal, +"is_normal($self, x, /)\n--\n\n\ +Return True if x is a normal number, False otherwise.\n\ \n"); -PyDoc_STRVAR(doc_ctx_is_qnan,"\n\ -is_qnan(x) - Return True if x is a quiet NaN, False otherwise.\n\ +PyDoc_STRVAR(doc_ctx_is_qnan, +"is_qnan($self, x, /)\n--\n\n\ +Return True if x is a quiet NaN, False otherwise.\n\ \n"); -PyDoc_STRVAR(doc_ctx_is_signed,"\n\ -is_signed(x) - Return True if x is negative, False otherwise.\n\ +PyDoc_STRVAR(doc_ctx_is_signed, +"is_signed($self, x, /)\n--\n\n\ +Return True if x is negative, False otherwise.\n\ \n"); -PyDoc_STRVAR(doc_ctx_is_snan,"\n\ -is_snan() - Return True if x is a signaling NaN, False otherwise.\n\ +PyDoc_STRVAR(doc_ctx_is_snan, +"is_snan($self, x, /)\n--\n\n\ +Return True if x is a signaling NaN, False otherwise.\n\ \n"); -PyDoc_STRVAR(doc_ctx_is_subnormal,"\n\ -is_subnormal(x) - Return True if x is subnormal, False otherwise.\n\ +PyDoc_STRVAR(doc_ctx_is_subnormal, +"is_subnormal($self, x, /)\n--\n\n\ +Return True if x is subnormal, False otherwise.\n\ \n"); -PyDoc_STRVAR(doc_ctx_is_zero,"\n\ -is_zero(x) - Return True if x is a zero, False otherwise.\n\ +PyDoc_STRVAR(doc_ctx_is_zero, +"is_zero($self, x, /)\n--\n\n\ +Return True if x is a zero, False otherwise.\n\ \n"); -PyDoc_STRVAR(doc_ctx_ln,"\n\ -ln(x) - Return the natural (base e) logarithm of x.\n\ +PyDoc_STRVAR(doc_ctx_ln, +"ln($self, x, /)\n--\n\n\ +Return the natural (base e) logarithm of x.\n\ \n"); -PyDoc_STRVAR(doc_ctx_log10,"\n\ -log10(x) - Return the base 10 logarithm of x.\n\ +PyDoc_STRVAR(doc_ctx_log10, +"log10($self, x, /)\n--\n\n\ +Return the base 10 logarithm of x.\n\ \n"); -PyDoc_STRVAR(doc_ctx_logb,"\n\ -logb(x) - Return the exponent of the magnitude of the operand's MSD.\n\ +PyDoc_STRVAR(doc_ctx_logb, +"logb($self, x, /)\n--\n\n\ +Return the exponent of the magnitude of the operand's MSD.\n\ \n"); -PyDoc_STRVAR(doc_ctx_logical_and,"\n\ -logical_and(x, y) - Digit-wise and of x and y.\n\ +PyDoc_STRVAR(doc_ctx_logical_and, +"logical_and($self, x, y, /)\n--\n\n\ +Digit-wise and of x and y.\n\ \n"); -PyDoc_STRVAR(doc_ctx_logical_invert,"\n\ -logical_invert(x) - Invert all digits of x.\n\ +PyDoc_STRVAR(doc_ctx_logical_invert, +"logical_invert($self, x, /)\n--\n\n\ +Invert all digits of x.\n\ \n"); -PyDoc_STRVAR(doc_ctx_logical_or,"\n\ -logical_or(x, y) - Digit-wise or of x and y.\n\ +PyDoc_STRVAR(doc_ctx_logical_or, +"logical_or($self, x, y, /)\n--\n\n\ +Digit-wise or of x and y.\n\ \n"); -PyDoc_STRVAR(doc_ctx_logical_xor,"\n\ -logical_xor(x, y) - Digit-wise xor of x and y.\n\ +PyDoc_STRVAR(doc_ctx_logical_xor, +"logical_xor($self, x, y, /)\n--\n\n\ +Digit-wise xor of x and y.\n\ \n"); -PyDoc_STRVAR(doc_ctx_max,"\n\ -max(x, y) - Compare the values numerically and return the maximum.\n\ +PyDoc_STRVAR(doc_ctx_max, +"max($self, x, y, /)\n--\n\n\ +Compare the values numerically and return the maximum.\n\ \n"); -PyDoc_STRVAR(doc_ctx_max_mag,"\n\ -max_mag(x, y) - Compare the values numerically with their sign ignored.\n\ +PyDoc_STRVAR(doc_ctx_max_mag, +"max_mag($self, x, y, /)\n--\n\n\ +Compare the values numerically with their sign ignored.\n\ \n"); -PyDoc_STRVAR(doc_ctx_min,"\n\ -min(x, y) - Compare the values numerically and return the minimum.\n\ +PyDoc_STRVAR(doc_ctx_min, +"min($self, x, y, /)\n--\n\n\ +Compare the values numerically and return the minimum.\n\ \n"); -PyDoc_STRVAR(doc_ctx_min_mag,"\n\ -min_mag(x, y) - Compare the values numerically with their sign ignored.\n\ +PyDoc_STRVAR(doc_ctx_min_mag, +"min_mag($self, x, y, /)\n--\n\n\ +Compare the values numerically with their sign ignored.\n\ \n"); -PyDoc_STRVAR(doc_ctx_minus,"\n\ -minus(x) - Minus corresponds to the unary prefix minus operator in Python,\n\ -but applies the context to the result.\n\ +PyDoc_STRVAR(doc_ctx_minus, +"minus($self, x, /)\n--\n\n\ +Minus corresponds to the unary prefix minus operator in Python, but applies\n\ +the context to the result.\n\ \n"); -PyDoc_STRVAR(doc_ctx_multiply,"\n\ -multiply(x, y) - Return the product of x and y.\n\ +PyDoc_STRVAR(doc_ctx_multiply, +"multiply($self, x, y, /)\n--\n\n\ +Return the product of x and y.\n\ \n"); -PyDoc_STRVAR(doc_ctx_next_minus,"\n\ -next_minus(x) - Return the largest representable number smaller than x.\n\ +PyDoc_STRVAR(doc_ctx_next_minus, +"next_minus($self, x, /)\n--\n\n\ +Return the largest representable number smaller than x.\n\ \n"); -PyDoc_STRVAR(doc_ctx_next_plus,"\n\ -next_plus(x) - Return the smallest representable number larger than x.\n\ +PyDoc_STRVAR(doc_ctx_next_plus, +"next_plus($self, x, /)\n--\n\n\ +Return the smallest representable number larger than x.\n\ \n"); -PyDoc_STRVAR(doc_ctx_next_toward,"\n\ -next_toward(x) - Return the number closest to x, in the direction towards y.\n\ +PyDoc_STRVAR(doc_ctx_next_toward, +"next_toward($self, x, y, /)\n--\n\n\ +Return the number closest to x, in the direction towards y.\n\ \n"); -PyDoc_STRVAR(doc_ctx_normalize,"\n\ -normalize(x) - Reduce x to its simplest form. Alias for reduce(x).\n\ +PyDoc_STRVAR(doc_ctx_normalize, +"normalize($self, x, /)\n--\n\n\ +Reduce x to its simplest form. Alias for reduce(x).\n\ \n"); -PyDoc_STRVAR(doc_ctx_number_class,"\n\ -number_class(x) - Return an indication of the class of x.\n\ +PyDoc_STRVAR(doc_ctx_number_class, +"number_class($self, x, /)\n--\n\n\ +Return an indication of the class of x.\n\ \n"); -PyDoc_STRVAR(doc_ctx_plus,"\n\ -plus(x) - Plus corresponds to the unary prefix plus operator in Python,\n\ -but applies the context to the result.\n\ +PyDoc_STRVAR(doc_ctx_plus, +"plus($self, x, /)\n--\n\n\ +Plus corresponds to the unary prefix plus operator in Python, but applies\n\ +the context to the result.\n\ \n"); -PyDoc_STRVAR(doc_ctx_power,"\n\ -power(x, y) - Compute x**y. If x is negative, then y must be integral.\n\ -The result will be inexact unless y is integral and the result is finite\n\ -and can be expressed exactly in 'precision' digits. In the Python version\n\ -the result is always correctly rounded, in the C version the result is\n\ -almost always correctly rounded.\n\ +PyDoc_STRVAR(doc_ctx_power, +"power($self, /, a, b, modulo=None)\n--\n\n\ +Compute a**b. If 'a' is negative, then 'b' must be integral. The result\n\ +will be inexact unless 'a' is integral and the result is finite and can\n\ +be expressed exactly in 'precision' digits. In the Python version the\n\ +result is always correctly rounded, in the C version the result is almost\n\ +always correctly rounded.\n\ \n\ -power(x, y, m) - Compute (x**y) % m. The following restrictions hold:\n\ +If modulo is given, compute (a**b) % modulo. The following restrictions\n\ +hold:\n\ \n\ * all three arguments must be integral\n\ - * y must be nonnegative\n\ - * at least one of x or y must be nonzero\n\ - * m must be nonzero and less than 10**prec in absolute value\n\ + * 'b' must be nonnegative\n\ + * at least one of 'a' or 'b' must be nonzero\n\ + * modulo must be nonzero and less than 10**prec in absolute value\n\ \n\ \n"); -PyDoc_STRVAR(doc_ctx_quantize,"\n\ -quantize(x, y) - Return a value equal to x (rounded), having the exponent of y.\n\ +PyDoc_STRVAR(doc_ctx_quantize, +"quantize($self, x, y, /)\n--\n\n\ +Return a value equal to x (rounded), having the exponent of y.\n\ \n"); -PyDoc_STRVAR(doc_ctx_radix,"\n\ -radix() - Return 10.\n\ +PyDoc_STRVAR(doc_ctx_radix, +"radix($self, /)\n--\n\n\ +Return 10.\n\ \n"); -PyDoc_STRVAR(doc_ctx_remainder,"\n\ -remainder(x, y) - Return the remainder from integer division. The sign of\n\ -the result, if non-zero, is the same as that of the original dividend.\n\ +PyDoc_STRVAR(doc_ctx_remainder, +"remainder($self, x, y, /)\n--\n\n\ +Return the remainder from integer division. The sign of the result,\n\ +if non-zero, is the same as that of the original dividend.\n\ \n"); -PyDoc_STRVAR(doc_ctx_remainder_near,"\n\ -remainder_near(x, y) - Return x - y * n, where n is the integer nearest the\n\ -exact value of x / y (if the result is 0 then its sign will be the sign of x).\n\ +PyDoc_STRVAR(doc_ctx_remainder_near, +"remainder_near($self, x, y, /)\n--\n\n\ +Return x - y * n, where n is the integer nearest the exact value of x / y\n\ +(if the result is 0 then its sign will be the sign of x).\n\ \n"); -PyDoc_STRVAR(doc_ctx_rotate,"\n\ -rotate(x, y) - Return a copy of x, rotated by y places.\n\ +PyDoc_STRVAR(doc_ctx_rotate, +"rotate($self, x, y, /)\n--\n\n\ +Return a copy of x, rotated by y places.\n\ \n"); -PyDoc_STRVAR(doc_ctx_same_quantum,"\n\ -same_quantum(x, y) - Return True if the two operands have the same exponent.\n\ +PyDoc_STRVAR(doc_ctx_same_quantum, +"same_quantum($self, x, y, /)\n--\n\n\ +Return True if the two operands have the same exponent.\n\ \n"); -PyDoc_STRVAR(doc_ctx_scaleb,"\n\ -scaleb(x, y) - Return the first operand after adding the second value\n\ -to its exp.\n\ +PyDoc_STRVAR(doc_ctx_scaleb, +"scaleb($self, x, y, /)\n--\n\n\ +Return the first operand after adding the second value to its exp.\n\ \n"); -PyDoc_STRVAR(doc_ctx_shift,"\n\ -shift(x, y) - Return a copy of x, shifted by y places.\n\ +PyDoc_STRVAR(doc_ctx_shift, +"shift($self, x, y, /)\n--\n\n\ +Return a copy of x, shifted by y places.\n\ \n"); -PyDoc_STRVAR(doc_ctx_sqrt,"\n\ -sqrt(x) - Square root of a non-negative number to context precision.\n\ +PyDoc_STRVAR(doc_ctx_sqrt, +"sqrt($self, x, /)\n--\n\n\ +Square root of a non-negative number to context precision.\n\ \n"); -PyDoc_STRVAR(doc_ctx_subtract,"\n\ -subtract(x, y) - Return the difference between x and y.\n\ +PyDoc_STRVAR(doc_ctx_subtract, +"subtract($self, x, y, /)\n--\n\n\ +Return the difference between x and y.\n\ \n"); -PyDoc_STRVAR(doc_ctx_to_eng_string,"\n\ -to_eng_string(x) - Convert a number to a string, using engineering notation.\n\ +PyDoc_STRVAR(doc_ctx_to_eng_string, +"to_eng_string($self, x, /)\n--\n\n\ +Convert a number to a string, using engineering notation.\n\ \n"); -PyDoc_STRVAR(doc_ctx_to_integral,"\n\ -to_integral(x) - Identical to to_integral_value(x).\n\ +PyDoc_STRVAR(doc_ctx_to_integral, +"to_integral($self, x, /)\n--\n\n\ +Identical to to_integral_value(x).\n\ \n"); -PyDoc_STRVAR(doc_ctx_to_integral_exact,"\n\ -to_integral_exact(x) - Round to an integer. Signal if the result is\n\ -rounded or inexact.\n\ +PyDoc_STRVAR(doc_ctx_to_integral_exact, +"to_integral_exact($self, x, /)\n--\n\n\ +Round to an integer. Signal if the result is rounded or inexact.\n\ \n"); -PyDoc_STRVAR(doc_ctx_to_integral_value,"\n\ -to_integral_value(x) - Round to an integer.\n\ +PyDoc_STRVAR(doc_ctx_to_integral_value, +"to_integral_value($self, x, /)\n--\n\n\ +Round to an integer.\n\ \n"); -PyDoc_STRVAR(doc_ctx_to_sci_string,"\n\ -to_sci_string(x) - Convert a number to a string using scientific notation.\n\ +PyDoc_STRVAR(doc_ctx_to_sci_string, +"to_sci_string($self, x, /)\n--\n\n\ +Convert a number to a string using scientific notation.\n\ \n"); diff --git a/Modules/_heapqmodule.c b/Modules/_heapqmodule.c index eee56a0..ad190df 100644 --- a/Modules/_heapqmodule.c +++ b/Modules/_heapqmodule.c @@ -11,10 +11,9 @@ annotated by François Pinard, and converted to C by Raymond Hettinger. static int _siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos) { - PyObject *newitem, *parent, *olditem; + PyObject *newitem, *parent; + Py_ssize_t parentpos, size; int cmp; - Py_ssize_t parentpos; - Py_ssize_t size; assert(PyList_Check(heap)); size = PyList_GET_SIZE(heap); @@ -23,39 +22,28 @@ _siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos) return -1; } - newitem = PyList_GET_ITEM(heap, pos); - Py_INCREF(newitem); /* Follow the path to the root, moving parents down until finding a place newitem fits. */ - while (pos > startpos){ + newitem = PyList_GET_ITEM(heap, pos); + while (pos > startpos) { parentpos = (pos - 1) >> 1; parent = PyList_GET_ITEM(heap, parentpos); cmp = PyObject_RichCompareBool(newitem, parent, Py_LT); - if (cmp == -1) { - Py_DECREF(newitem); + if (cmp == -1) return -1; - } if (size != PyList_GET_SIZE(heap)) { - Py_DECREF(newitem); PyErr_SetString(PyExc_RuntimeError, "list changed size during iteration"); return -1; } if (cmp == 0) break; - Py_INCREF(parent); - olditem = PyList_GET_ITEM(heap, pos); + parent = PyList_GET_ITEM(heap, parentpos); + newitem = PyList_GET_ITEM(heap, pos); + PyList_SET_ITEM(heap, parentpos, newitem); PyList_SET_ITEM(heap, pos, parent); - Py_DECREF(olditem); pos = parentpos; - if (size != PyList_GET_SIZE(heap)) { - PyErr_SetString(PyExc_RuntimeError, - "list changed size during iteration"); - return -1; - } } - Py_DECREF(PyList_GET_ITEM(heap, pos)); - PyList_SET_ITEM(heap, pos, newitem); return 0; } @@ -63,20 +51,16 @@ static int _siftup(PyListObject *heap, Py_ssize_t pos) { Py_ssize_t startpos, endpos, childpos, rightpos, limit; + PyObject *tmp1, *tmp2; int cmp; - PyObject *newitem, *tmp, *olditem; - Py_ssize_t size; assert(PyList_Check(heap)); - size = PyList_GET_SIZE(heap); - endpos = size; + endpos = PyList_GET_SIZE(heap); startpos = pos; if (pos >= endpos) { PyErr_SetString(PyExc_IndexError, "index out of range"); return -1; } - newitem = PyList_GET_ITEM(heap, pos); - Py_INCREF(newitem); /* Bubble up the smaller child until hitting a leaf. */ limit = endpos / 2; /* smallest pos that has no child */ @@ -89,37 +73,24 @@ _siftup(PyListObject *heap, Py_ssize_t pos) PyList_GET_ITEM(heap, childpos), PyList_GET_ITEM(heap, rightpos), Py_LT); - if (cmp == -1) { - Py_DECREF(newitem); + if (cmp == -1) return -1; - } if (cmp == 0) childpos = rightpos; - } - if (size != PyList_GET_SIZE(heap)) { - Py_DECREF(newitem); - PyErr_SetString(PyExc_RuntimeError, - "list changed size during iteration"); - return -1; + if (endpos != PyList_GET_SIZE(heap)) { + PyErr_SetString(PyExc_RuntimeError, + "list changed size during iteration"); + return -1; + } } /* Move the smaller child up. */ - tmp = PyList_GET_ITEM(heap, childpos); - Py_INCREF(tmp); - olditem = PyList_GET_ITEM(heap, pos); - PyList_SET_ITEM(heap, pos, tmp); - Py_DECREF(olditem); + tmp1 = PyList_GET_ITEM(heap, childpos); + tmp2 = PyList_GET_ITEM(heap, pos); + PyList_SET_ITEM(heap, childpos, tmp2); + PyList_SET_ITEM(heap, pos, tmp1); pos = childpos; - if (size != PyList_GET_SIZE(heap)) { - PyErr_SetString(PyExc_RuntimeError, - "list changed size during iteration"); - return -1; - } } - - /* The leaf at pos is empty now. Put newitem there, and bubble - it up to its final resting place (by sifting its parents down). */ - Py_DECREF(PyList_GET_ITEM(heap, pos)); - PyList_SET_ITEM(heap, pos, newitem); + /* Bubble it up to its final resting place (by sifting its parents down). */ return _siftdown(heap, startpos, pos); } @@ -296,123 +267,42 @@ heapify(PyObject *self, PyObject *heap) PyDoc_STRVAR(heapify_doc, "Transform list into a heap, in-place, in O(len(heap)) time."); -static PyObject * -nlargest(PyObject *self, PyObject *args) -{ - PyObject *heap=NULL, *elem, *iterable, *sol, *it, *oldelem; - Py_ssize_t i, n; - int cmp; - - if (!PyArg_ParseTuple(args, "nO:nlargest", &n, &iterable)) - return NULL; - - it = PyObject_GetIter(iterable); - if (it == NULL) - return NULL; - - heap = PyList_New(0); - if (heap == NULL) - goto fail; - - for (i=0 ; i<n ; i++ ){ - elem = PyIter_Next(it); - if (elem == NULL) { - if (PyErr_Occurred()) - goto fail; - else - goto sortit; - } - if (PyList_Append(heap, elem) == -1) { - Py_DECREF(elem); - goto fail; - } - Py_DECREF(elem); - } - if (PyList_GET_SIZE(heap) == 0) - goto sortit; - - for (i=n/2-1 ; i>=0 ; i--) - if(_siftup((PyListObject *)heap, i) == -1) - goto fail; - - sol = PyList_GET_ITEM(heap, 0); - while (1) { - elem = PyIter_Next(it); - if (elem == NULL) { - if (PyErr_Occurred()) - goto fail; - else - goto sortit; - } - cmp = PyObject_RichCompareBool(sol, elem, Py_LT); - if (cmp == -1) { - Py_DECREF(elem); - goto fail; - } - if (cmp == 0) { - Py_DECREF(elem); - continue; - } - oldelem = PyList_GET_ITEM(heap, 0); - PyList_SET_ITEM(heap, 0, elem); - Py_DECREF(oldelem); - if (_siftup((PyListObject *)heap, 0) == -1) - goto fail; - sol = PyList_GET_ITEM(heap, 0); - } -sortit: - if (PyList_Sort(heap) == -1) - goto fail; - if (PyList_Reverse(heap) == -1) - goto fail; - Py_DECREF(it); - return heap; - -fail: - Py_DECREF(it); - Py_XDECREF(heap); - return NULL; -} - -PyDoc_STRVAR(nlargest_doc, -"Find the n largest elements in a dataset.\n\ -\n\ -Equivalent to: sorted(iterable, reverse=True)[:n]\n"); - static int _siftdownmax(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos) { PyObject *newitem, *parent; + Py_ssize_t parentpos, size; int cmp; - Py_ssize_t parentpos; assert(PyList_Check(heap)); - if (pos >= PyList_GET_SIZE(heap)) { + size = PyList_GET_SIZE(heap); + if (pos >= size) { PyErr_SetString(PyExc_IndexError, "index out of range"); return -1; } - newitem = PyList_GET_ITEM(heap, pos); - Py_INCREF(newitem); /* Follow the path to the root, moving parents down until finding a place newitem fits. */ - while (pos > startpos){ + newitem = PyList_GET_ITEM(heap, pos); + while (pos > startpos) { parentpos = (pos - 1) >> 1; parent = PyList_GET_ITEM(heap, parentpos); cmp = PyObject_RichCompareBool(parent, newitem, Py_LT); - if (cmp == -1) { - Py_DECREF(newitem); + if (cmp == -1) + return -1; + if (size != PyList_GET_SIZE(heap)) { + PyErr_SetString(PyExc_RuntimeError, + "list changed size during iteration"); return -1; } if (cmp == 0) break; - Py_INCREF(parent); - Py_DECREF(PyList_GET_ITEM(heap, pos)); + parent = PyList_GET_ITEM(heap, parentpos); + newitem = PyList_GET_ITEM(heap, pos); + PyList_SET_ITEM(heap, parentpos, newitem); PyList_SET_ITEM(heap, pos, parent); pos = parentpos; } - Py_DECREF(PyList_GET_ITEM(heap, pos)); - PyList_SET_ITEM(heap, pos, newitem); return 0; } @@ -420,8 +310,8 @@ static int _siftupmax(PyListObject *heap, Py_ssize_t pos) { Py_ssize_t startpos, endpos, childpos, rightpos, limit; + PyObject *tmp1, *tmp2; int cmp; - PyObject *newitem, *tmp; assert(PyList_Check(heap)); endpos = PyList_GET_SIZE(heap); @@ -430,8 +320,6 @@ _siftupmax(PyListObject *heap, Py_ssize_t pos) PyErr_SetString(PyExc_IndexError, "index out of range"); return -1; } - newitem = PyList_GET_ITEM(heap, pos); - Py_INCREF(newitem); /* Bubble up the smaller child until hitting a leaf. */ limit = endpos / 2; /* smallest pos that has no child */ @@ -444,111 +332,56 @@ _siftupmax(PyListObject *heap, Py_ssize_t pos) PyList_GET_ITEM(heap, rightpos), PyList_GET_ITEM(heap, childpos), Py_LT); - if (cmp == -1) { - Py_DECREF(newitem); + if (cmp == -1) return -1; - } if (cmp == 0) childpos = rightpos; + if (endpos != PyList_GET_SIZE(heap)) { + PyErr_SetString(PyExc_RuntimeError, + "list changed size during iteration"); + return -1; + } } /* Move the smaller child up. */ - tmp = PyList_GET_ITEM(heap, childpos); - Py_INCREF(tmp); - Py_DECREF(PyList_GET_ITEM(heap, pos)); - PyList_SET_ITEM(heap, pos, tmp); + tmp1 = PyList_GET_ITEM(heap, childpos); + tmp2 = PyList_GET_ITEM(heap, pos); + PyList_SET_ITEM(heap, childpos, tmp2); + PyList_SET_ITEM(heap, pos, tmp1); pos = childpos; } - - /* The leaf at pos is empty now. Put newitem there, and bubble - it up to its final resting place (by sifting its parents down). */ - Py_DECREF(PyList_GET_ITEM(heap, pos)); - PyList_SET_ITEM(heap, pos, newitem); + /* Bubble it up to its final resting place (by sifting its parents down). */ return _siftdownmax(heap, startpos, pos); } static PyObject * -nsmallest(PyObject *self, PyObject *args) +_heapreplace_max(PyObject *self, PyObject *args) { - PyObject *heap=NULL, *elem, *iterable, *los, *it, *oldelem; - Py_ssize_t i, n; - int cmp; + PyObject *heap, *item, *returnitem; - if (!PyArg_ParseTuple(args, "nO:nsmallest", &n, &iterable)) + if (!PyArg_UnpackTuple(args, "_heapreplace_max", 2, 2, &heap, &item)) return NULL; - it = PyObject_GetIter(iterable); - if (it == NULL) + if (!PyList_Check(heap)) { + PyErr_SetString(PyExc_TypeError, "heap argument must be a list"); return NULL; - - heap = PyList_New(0); - if (heap == NULL) - goto fail; - - for (i=0 ; i<n ; i++ ){ - elem = PyIter_Next(it); - if (elem == NULL) { - if (PyErr_Occurred()) - goto fail; - else - goto sortit; - } - if (PyList_Append(heap, elem) == -1) { - Py_DECREF(elem); - goto fail; - } - Py_DECREF(elem); } - n = PyList_GET_SIZE(heap); - if (n == 0) - goto sortit; - for (i=n/2-1 ; i>=0 ; i--) - if(_siftupmax((PyListObject *)heap, i) == -1) - goto fail; - - los = PyList_GET_ITEM(heap, 0); - while (1) { - elem = PyIter_Next(it); - if (elem == NULL) { - if (PyErr_Occurred()) - goto fail; - else - goto sortit; - } - cmp = PyObject_RichCompareBool(elem, los, Py_LT); - if (cmp == -1) { - Py_DECREF(elem); - goto fail; - } - if (cmp == 0) { - Py_DECREF(elem); - continue; - } - - oldelem = PyList_GET_ITEM(heap, 0); - PyList_SET_ITEM(heap, 0, elem); - Py_DECREF(oldelem); - if (_siftupmax((PyListObject *)heap, 0) == -1) - goto fail; - los = PyList_GET_ITEM(heap, 0); + if (PyList_GET_SIZE(heap) < 1) { + PyErr_SetString(PyExc_IndexError, "index out of range"); + return NULL; } -sortit: - if (PyList_Sort(heap) == -1) - goto fail; - Py_DECREF(it); - return heap; - -fail: - Py_DECREF(it); - Py_XDECREF(heap); - return NULL; + returnitem = PyList_GET_ITEM(heap, 0); + Py_INCREF(item); + PyList_SET_ITEM(heap, 0, item); + if (_siftupmax((PyListObject *)heap, 0) == -1) { + Py_DECREF(returnitem); + return NULL; + } + return returnitem; } -PyDoc_STRVAR(nsmallest_doc, -"Find the n smallest elements in a dataset.\n\ -\n\ -Equivalent to: sorted(iterable)[:n]\n"); +PyDoc_STRVAR(heapreplace_max_doc, "Maxheap variant of heapreplace"); static PyMethodDef heapq_methods[] = { {"heappush", (PyCFunction)heappush, @@ -561,10 +394,8 @@ static PyMethodDef heapq_methods[] = { METH_VARARGS, heapreplace_doc}, {"heapify", (PyCFunction)heapify, METH_O, heapify_doc}, - {"nlargest", (PyCFunction)nlargest, - METH_VARARGS, nlargest_doc}, - {"nsmallest", (PyCFunction)nsmallest, - METH_VARARGS, nsmallest_doc}, + {"_heapreplace_max",(PyCFunction)_heapreplace_max, + METH_VARARGS, heapreplace_max_doc}, {NULL, NULL} /* sentinel */ }; diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index ba5789d..24c7b45 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -1440,6 +1440,7 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint) PyObject *dec_buffer = NULL; PyObject *dec_flags = NULL; PyObject *input_chunk = NULL; + Py_buffer input_chunk_buf; PyObject *decoded_chars, *chunk_size; Py_ssize_t nbytes, nchars; int eof; @@ -1471,6 +1472,15 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint) Py_DECREF(state); return -1; } + + if (!PyBytes_Check(dec_buffer)) { + PyErr_Format(PyExc_TypeError, + "decoder getstate() should have returned a bytes " + "object, not '%.200s'", + Py_TYPE(dec_buffer)->tp_name); + Py_DECREF(state); + return -1; + } Py_INCREF(dec_buffer); Py_INCREF(dec_flags); Py_DECREF(state); @@ -1483,23 +1493,24 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint) chunk_size = PyLong_FromSsize_t(Py_MAX(self->chunk_size, size_hint)); if (chunk_size == NULL) goto fail; + input_chunk = PyObject_CallMethodObjArgs(self->buffer, (self->has_read1 ? _PyIO_str_read1: _PyIO_str_read), chunk_size, NULL); Py_DECREF(chunk_size); if (input_chunk == NULL) goto fail; - if (!PyBytes_Check(input_chunk)) { + + if (PyObject_GetBuffer(input_chunk, &input_chunk_buf, 0) != 0) { PyErr_Format(PyExc_TypeError, - "underlying %s() should have returned a bytes object, " + "underlying %s() should have returned a bytes-like object, " "not '%.200s'", (self->has_read1 ? "read1": "read"), Py_TYPE(input_chunk)->tp_name); goto fail; } - nbytes = PyBytes_Size(input_chunk); + nbytes = input_chunk_buf.len; eof = (nbytes == 0); - if (Py_TYPE(self->decoder) == &PyIncrementalNewlineDecoder_Type) { decoded_chars = _PyIncrementalNewlineDecoder_decode( self->decoder, input_chunk, eof); @@ -1508,6 +1519,7 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint) decoded_chars = PyObject_CallMethodObjArgs(self->decoder, _PyIO_str_decode, input_chunk, eof ? Py_True : Py_False, NULL); } + PyBuffer_Release(&input_chunk_buf); if (check_decoded(decoded_chars) < 0) goto fail; @@ -1524,18 +1536,12 @@ textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint) /* At the snapshot point, len(dec_buffer) bytes before the read, the * next input to be decoded is dec_buffer + input_chunk. */ - PyObject *next_input = PyNumber_Add(dec_buffer, input_chunk); - if (next_input == NULL) - goto fail; - if (!PyBytes_Check(next_input)) { - PyErr_Format(PyExc_TypeError, - "decoder getstate() should have returned a bytes " - "object, not '%.200s'", - Py_TYPE(next_input)->tp_name); - Py_DECREF(next_input); + PyObject *next_input = dec_buffer; + PyBytes_Concat(&next_input, input_chunk); + if (next_input == NULL) { + dec_buffer = NULL; /* Reference lost to PyBytes_Concat */ goto fail; } - Py_DECREF(dec_buffer); Py_CLEAR(self->snapshot); self->snapshot = Py_BuildValue("NN", dec_flags, next_input); } diff --git a/Modules/_operator.c b/Modules/_operator.c index adeb99e..9c5c0d2 100644 --- a/Modules/_operator.c +++ b/Modules/_operator.c @@ -69,6 +69,7 @@ spami(truth , PyObject_IsTrue) spam2(op_add , PyNumber_Add) spam2(op_sub , PyNumber_Subtract) spam2(op_mul , PyNumber_Multiply) +spam2(op_matmul , PyNumber_MatrixMultiply) spam2(op_floordiv , PyNumber_FloorDivide) spam2(op_truediv , PyNumber_TrueDivide) spam2(op_mod , PyNumber_Remainder) @@ -86,6 +87,7 @@ spam2(op_or_ , PyNumber_Or) spam2(op_iadd , PyNumber_InPlaceAdd) spam2(op_isub , PyNumber_InPlaceSubtract) spam2(op_imul , PyNumber_InPlaceMultiply) +spam2(op_imatmul , PyNumber_InPlaceMatrixMultiply) spam2(op_ifloordiv , PyNumber_InPlaceFloorDivide) spam2(op_itruediv , PyNumber_InPlaceTrueDivide) spam2(op_imod , PyNumber_InPlaceRemainder) @@ -343,6 +345,7 @@ spam2o(index, "index(a) -- Same as a.__index__()") spam2(add, "add(a, b) -- Same as a + b.") spam2(sub, "sub(a, b) -- Same as a - b.") spam2(mul, "mul(a, b) -- Same as a * b.") +spam2(matmul, "matmul(a, b) -- Same as a @ b.") spam2(floordiv, "floordiv(a, b) -- Same as a // b.") spam2(truediv, "truediv(a, b) -- Same as a / b.") spam2(mod, "mod(a, b) -- Same as a % b.") @@ -360,6 +363,7 @@ spam2(or_, "or_(a, b) -- Same as a | b.") spam2(iadd, "a = iadd(a, b) -- Same as a += b.") spam2(isub, "a = isub(a, b) -- Same as a -= b.") spam2(imul, "a = imul(a, b) -- Same as a *= b.") +spam2(imatmul, "a = imatmul(a, b) -- Same as a @= b.") spam2(ifloordiv, "a = ifloordiv(a, b) -- Same as a //= b.") spam2(itruediv, "a = itruediv(a, b) -- Same as a /= b") spam2(imod, "a = imod(a, b) -- Same as a %= b.") diff --git a/Modules/_ssl.c b/Modules/_ssl.c index 5031476..3b7226d 100644 --- a/Modules/_ssl.c +++ b/Modules/_ssl.c @@ -249,10 +249,8 @@ typedef enum { } timeout_state; /* Wrap error strings with filename and line # */ -#define STRINGIFY1(x) #x -#define STRINGIFY2(x) STRINGIFY1(x) #define ERRSTR1(x,y,z) (x ":" y ": " z) -#define ERRSTR(x) ERRSTR1("_ssl.c", STRINGIFY2(__LINE__), x) +#define ERRSTR(x) ERRSTR1("_ssl.c", Py_STRINGIFY(__LINE__), x) /* diff --git a/Modules/_struct.c b/Modules/_struct.c index 1de94e4..4941fc8 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -85,8 +85,6 @@ typedef struct { char c; _Bool x; } s_bool; #define BOOL_ALIGN 0 #endif -#define STRINGIFY(x) #x - #ifdef __powerc #pragma options align=reset #endif @@ -546,8 +544,8 @@ np_short(char *p, PyObject *v, const formatdef *f) return -1; if (x < SHRT_MIN || x > SHRT_MAX){ PyErr_SetString(StructError, - "short format requires " STRINGIFY(SHRT_MIN) - " <= number <= " STRINGIFY(SHRT_MAX)); + "short format requires " Py_STRINGIFY(SHRT_MIN) + " <= number <= " Py_STRINGIFY(SHRT_MAX)); return -1; } y = (short)x; @@ -564,7 +562,8 @@ np_ushort(char *p, PyObject *v, const formatdef *f) return -1; if (x < 0 || x > USHRT_MAX){ PyErr_SetString(StructError, - "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX)); + "ushort format requires 0 <= number <= " + Py_STRINGIFY(USHRT_MAX)); return -1; } y = (unsigned short)x; diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index db2376d..a755004 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -2710,6 +2710,20 @@ test_pymem_alloc0(PyObject *self) { void *ptr; + ptr = PyMem_RawMalloc(0); + if (ptr == NULL) { + PyErr_SetString(PyExc_RuntimeError, "PyMem_RawMalloc(0) returns NULL"); + return NULL; + } + PyMem_RawFree(ptr); + + ptr = PyMem_RawCalloc(0, 0); + if (ptr == NULL) { + PyErr_SetString(PyExc_RuntimeError, "PyMem_RawCalloc(0, 0) returns NULL"); + return NULL; + } + PyMem_RawFree(ptr); + ptr = PyMem_Malloc(0); if (ptr == NULL) { PyErr_SetString(PyExc_RuntimeError, "PyMem_Malloc(0) returns NULL"); @@ -2717,6 +2731,13 @@ test_pymem_alloc0(PyObject *self) } PyMem_Free(ptr); + ptr = PyMem_Calloc(0, 0); + if (ptr == NULL) { + PyErr_SetString(PyExc_RuntimeError, "PyMem_Calloc(0, 0) returns NULL"); + return NULL; + } + PyMem_Free(ptr); + ptr = PyObject_Malloc(0); if (ptr == NULL) { PyErr_SetString(PyExc_RuntimeError, "PyObject_Malloc(0) returns NULL"); @@ -2724,6 +2745,13 @@ test_pymem_alloc0(PyObject *self) } PyObject_Free(ptr); + ptr = PyObject_Calloc(0, 0); + if (ptr == NULL) { + PyErr_SetString(PyExc_RuntimeError, "PyObject_Calloc(0, 0) returns NULL"); + return NULL; + } + PyObject_Free(ptr); + Py_RETURN_NONE; } @@ -2731,6 +2759,8 @@ typedef struct { PyMemAllocator alloc; size_t malloc_size; + size_t calloc_nelem; + size_t calloc_elsize; void *realloc_ptr; size_t realloc_new_size; void *free_ptr; @@ -2743,6 +2773,14 @@ static void* hook_malloc (void* ctx, size_t size) return hook->alloc.malloc(hook->alloc.ctx, size); } +static void* hook_calloc (void* ctx, size_t nelem, size_t elsize) +{ + alloc_hook_t *hook = (alloc_hook_t *)ctx; + hook->calloc_nelem = nelem; + hook->calloc_elsize = elsize; + return hook->alloc.calloc(hook->alloc.ctx, nelem, elsize); +} + static void* hook_realloc (void* ctx, void* ptr, size_t new_size) { alloc_hook_t *hook = (alloc_hook_t *)ctx; @@ -2765,16 +2803,14 @@ test_setallocators(PyMemAllocatorDomain domain) const char *error_msg; alloc_hook_t hook; PyMemAllocator alloc; - size_t size, size2; + size_t size, size2, nelem, elsize; void *ptr, *ptr2; - hook.malloc_size = 0; - hook.realloc_ptr = NULL; - hook.realloc_new_size = 0; - hook.free_ptr = NULL; + memset(&hook, 0, sizeof(hook)); alloc.ctx = &hook; alloc.malloc = &hook_malloc; + alloc.calloc = &hook_calloc; alloc.realloc = &hook_realloc; alloc.free = &hook_free; PyMem_GetAllocator(domain, &hook.alloc); @@ -2831,6 +2867,33 @@ test_setallocators(PyMemAllocatorDomain domain) goto fail; } + nelem = 2; + elsize = 5; + switch(domain) + { + case PYMEM_DOMAIN_RAW: ptr = PyMem_RawCalloc(nelem, elsize); break; + case PYMEM_DOMAIN_MEM: ptr = PyMem_Calloc(nelem, elsize); break; + case PYMEM_DOMAIN_OBJ: ptr = PyObject_Calloc(nelem, elsize); break; + default: ptr = NULL; break; + } + + if (ptr == NULL) { + error_msg = "calloc failed"; + goto fail; + } + + if (hook.calloc_nelem != nelem || hook.calloc_elsize != elsize) { + error_msg = "calloc invalid nelem or elsize"; + goto fail; + } + + switch(domain) + { + case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr); break; + case PYMEM_DOMAIN_MEM: PyMem_Free(ptr); break; + case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr); break; + } + Py_INCREF(Py_None); res = Py_None; goto finally; @@ -3298,6 +3361,109 @@ static PyTypeObject test_structmembersType = { }; +typedef struct { + PyObject_HEAD +} matmulObject; + +static PyObject * +matmulType_matmul(PyObject *self, PyObject *other) +{ + return Py_BuildValue("(sOO)", "matmul", self, other); +} + +static PyObject * +matmulType_imatmul(PyObject *self, PyObject *other) +{ + return Py_BuildValue("(sOO)", "imatmul", self, other); +} + +static void +matmulType_dealloc(PyObject *self) +{ + Py_TYPE(self)->tp_free(self); +} + +static PyNumberMethods matmulType_as_number = { + 0, /* nb_add */ + 0, /* nb_subtract */ + 0, /* nb_multiply */ + 0, /* nb_remainde r*/ + 0, /* nb_divmod */ + 0, /* nb_power */ + 0, /* nb_negative */ + 0, /* tp_positive */ + 0, /* tp_absolute */ + 0, /* tp_bool */ + 0, /* nb_invert */ + 0, /* nb_lshift */ + 0, /* nb_rshift */ + 0, /* nb_and */ + 0, /* nb_xor */ + 0, /* nb_or */ + 0, /* nb_int */ + 0, /* nb_reserved */ + 0, /* nb_float */ + 0, /* nb_inplace_add */ + 0, /* nb_inplace_subtract */ + 0, /* nb_inplace_multiply */ + 0, /* nb_inplace_remainder */ + 0, /* nb_inplace_power */ + 0, /* nb_inplace_lshift */ + 0, /* nb_inplace_rshift */ + 0, /* nb_inplace_and */ + 0, /* nb_inplace_xor */ + 0, /* nb_inplace_or */ + 0, /* nb_floor_divide */ + 0, /* nb_true_divide */ + 0, /* nb_inplace_floor_divide */ + 0, /* nb_inplace_true_divide */ + 0, /* nb_index */ + matmulType_matmul, /* nb_matrix_multiply */ + matmulType_imatmul /* nb_matrix_inplace_multiply */ +}; + +static PyTypeObject matmulType = { + PyVarObject_HEAD_INIT(NULL, 0) + "matmulType", + sizeof(matmulObject), /* tp_basicsize */ + 0, /* tp_itemsize */ + matmulType_dealloc, /* destructor tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + &matmulType_as_number, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + PyObject_GenericGetAttr, /* tp_getattro */ + PyObject_GenericSetAttr, /* tp_setattro */ + 0, /* tp_as_buffer */ + 0, /* tp_flags */ + "C level type with matrix operations defined", + 0, /* traverseproc tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + 0, /* tp_methods */ + 0, /* tp_members */ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + PyType_GenericNew, /* tp_new */ + PyObject_Del, /* tp_free */ +}; + static struct PyModuleDef _testcapimodule = { PyModuleDef_HEAD_INIT, @@ -3327,6 +3493,10 @@ PyInit__testcapi(void) /* don't use a name starting with "test", since we don't want test_capi to automatically call this */ PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); + if (PyType_Ready(&matmulType) < 0) + return NULL; + Py_INCREF(&matmulType); + PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType); PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX)); PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN)); diff --git a/Modules/_testembed.c b/Modules/_testembed.c index a21d251..39ff097 100644 --- a/Modules/_testembed.c +++ b/Modules/_testembed.c @@ -109,11 +109,11 @@ static void test_forced_io_encoding(void) printf("--- Use defaults ---\n"); check_stdio_details(NULL, NULL); printf("--- Set errors only ---\n"); - check_stdio_details(NULL, "surrogateescape"); + check_stdio_details(NULL, "ignore"); printf("--- Set encoding only ---\n"); check_stdio_details("latin-1", NULL); printf("--- Set encoding and errors ---\n"); - check_stdio_details("latin-1", "surrogateescape"); + check_stdio_details("latin-1", "replace"); /* Check calling after initialization fails */ Py_Initialize(); diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c index 780e8ed..429b209 100644 --- a/Modules/_tracemalloc.c +++ b/Modules/_tracemalloc.c @@ -16,9 +16,6 @@ static void raw_free(void *ptr); # define TRACE_DEBUG #endif -#define _STR(VAL) #VAL -#define STR(VAL) _STR(VAL) - /* Protected by the GIL */ static struct { PyMemAllocator mem; @@ -476,17 +473,22 @@ tracemalloc_remove_trace(void *ptr) } static void* -tracemalloc_malloc(void *ctx, size_t size) +tracemalloc_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize) { PyMemAllocator *alloc = (PyMemAllocator *)ctx; void *ptr; - ptr = alloc->malloc(alloc->ctx, size); + assert(nelem <= PY_SIZE_MAX / elsize); + + if (use_calloc) + ptr = alloc->calloc(alloc->ctx, nelem, elsize); + else + ptr = alloc->malloc(alloc->ctx, nelem * elsize); if (ptr == NULL) return NULL; TABLES_LOCK(); - if (tracemalloc_add_trace(ptr, size) < 0) { + if (tracemalloc_add_trace(ptr, nelem * elsize) < 0) { /* Failed to allocate a trace for the new memory block */ TABLES_UNLOCK(); alloc->free(alloc->ctx, ptr); @@ -560,13 +562,16 @@ tracemalloc_free(void *ctx, void *ptr) } static void* -tracemalloc_malloc_gil(void *ctx, size_t size) +tracemalloc_alloc_gil(int use_calloc, void *ctx, size_t nelem, size_t elsize) { void *ptr; if (get_reentrant()) { PyMemAllocator *alloc = (PyMemAllocator *)ctx; - return alloc->malloc(alloc->ctx, size); + if (use_calloc) + return alloc->calloc(alloc->ctx, nelem, elsize); + else + return alloc->malloc(alloc->ctx, nelem * elsize); } /* Ignore reentrant call. PyObjet_Malloc() calls PyMem_Malloc() for @@ -574,13 +579,25 @@ tracemalloc_malloc_gil(void *ctx, size_t size) allocation twice. */ set_reentrant(1); - ptr = tracemalloc_malloc(ctx, size); + ptr = tracemalloc_alloc(use_calloc, ctx, nelem, elsize); set_reentrant(0); 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) { void *ptr2; @@ -614,7 +631,7 @@ tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size) #ifdef TRACE_RAW_MALLOC static void* -tracemalloc_raw_malloc(void *ctx, size_t size) +tracemalloc_raw_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize) { #ifdef WITH_THREAD PyGILState_STATE gil_state; @@ -623,7 +640,10 @@ tracemalloc_raw_malloc(void *ctx, size_t size) if (get_reentrant()) { PyMemAllocator *alloc = (PyMemAllocator *)ctx; - return alloc->malloc(alloc->ctx, size); + if (use_calloc) + return alloc->calloc(alloc->ctx, nelem, elsize); + else + return alloc->malloc(alloc->ctx, nelem * elsize); } /* Ignore reentrant call. PyGILState_Ensure() may call PyMem_RawMalloc() @@ -633,10 +653,10 @@ tracemalloc_raw_malloc(void *ctx, size_t size) #ifdef WITH_THREAD gil_state = PyGILState_Ensure(); - ptr = tracemalloc_malloc(ctx, size); + ptr = tracemalloc_alloc(use_calloc, ctx, nelem, elsize); PyGILState_Release(gil_state); #else - ptr = tracemalloc_malloc(ctx, size); + ptr = tracemalloc_alloc(use_calloc, ctx, nelem, elsize); #endif set_reentrant(0); @@ -644,6 +664,18 @@ tracemalloc_raw_malloc(void *ctx, size_t size) } 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) { #ifdef WITH_THREAD @@ -856,6 +888,7 @@ tracemalloc_start(int max_nframe) #ifdef TRACE_RAW_MALLOC alloc.malloc = tracemalloc_raw_malloc; + alloc.calloc = tracemalloc_raw_calloc; alloc.realloc = tracemalloc_raw_realloc; alloc.free = tracemalloc_free; @@ -865,6 +898,7 @@ tracemalloc_start(int max_nframe) #endif alloc.malloc = tracemalloc_malloc_gil; + alloc.calloc = tracemalloc_calloc_gil; alloc.realloc = tracemalloc_realloc_gil; alloc.free = tracemalloc_free; diff --git a/Modules/_winapi.c b/Modules/_winapi.c index b755178..f118436 100644 --- a/Modules/_winapi.c +++ b/Modules/_winapi.c @@ -40,6 +40,7 @@ #define WINDOWS_LEAN_AND_MEAN #include "windows.h" #include <crtdbg.h> +#include "winreparse.h" #if defined(MS_WIN32) && !defined(MS_WIN64) #define HANDLE_TO_PYNUM(handle) \ @@ -401,6 +402,140 @@ winapi_CreateFile(PyObject *self, PyObject *args) } static PyObject * +winapi_CreateJunction(PyObject *self, PyObject *args) +{ + /* Input arguments */ + LPWSTR src_path = NULL; + LPWSTR dst_path = NULL; + + /* Privilege adjustment */ + HANDLE token = NULL; + TOKEN_PRIVILEGES tp; + + /* Reparse data buffer */ + const USHORT prefix_len = 4; + USHORT print_len = 0; + USHORT rdb_size = 0; + PREPARSE_DATA_BUFFER rdb = NULL; + + /* Junction point creation */ + HANDLE junction = NULL; + DWORD ret = 0; + + if (!PyArg_ParseTuple(args, "uu", &src_path, &dst_path)) + return NULL; + + if (src_path == NULL || dst_path == NULL) + return PyErr_SetFromWindowsErr(ERROR_INVALID_PARAMETER); + + if (wcsncmp(src_path, L"\\??\\", prefix_len) == 0) + return PyErr_SetFromWindowsErr(ERROR_INVALID_PARAMETER); + + /* Adjust privileges to allow rewriting directory entry as a + junction point. */ + if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token)) + goto cleanup; + + if (!LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &tp.Privileges[0].Luid)) + goto cleanup; + + tp.PrivilegeCount = 1; + tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; + if (!AdjustTokenPrivileges(token, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), + NULL, NULL)) + goto cleanup; + + if (GetFileAttributesW(src_path) == INVALID_FILE_ATTRIBUTES) + goto cleanup; + + /* Store the absolute link target path length in print_len. */ + print_len = (USHORT)GetFullPathNameW(src_path, 0, NULL, NULL); + if (print_len == 0) + goto cleanup; + + /* NUL terminator should not be part of print_len. */ + --print_len; + + /* REPARSE_DATA_BUFFER usage is heavily under-documented, especially for + junction points. Here's what I've learned along the way: + - A junction point has two components: a print name and a substitute + name. They both describe the link target, but the substitute name is + the physical target and the print name is shown in directory listings. + - The print name must be a native name, prefixed with "\??\". + - Both names are stored after each other in the same buffer (the + PathBuffer) and both must be NUL-terminated. + - There are four members defining their respective offset and length + inside PathBuffer: SubstituteNameOffset, SubstituteNameLength, + PrintNameOffset and PrintNameLength. + - The total size we need to allocate for the REPARSE_DATA_BUFFER, thus, + is the sum of: + - the fixed header size (REPARSE_DATA_BUFFER_HEADER_SIZE) + - the size of the MountPointReparseBuffer member without the PathBuffer + - the size of the prefix ("\??\") in bytes + - 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 + + 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); + 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->MountPointReparseBuffer.SubstituteNameOffset = 0; + rdb->MountPointReparseBuffer.SubstituteNameLength = + (prefix_len + print_len) * sizeof(WCHAR); + rdb->MountPointReparseBuffer.PrintNameOffset = + rdb->MountPointReparseBuffer.SubstituteNameLength + sizeof(WCHAR); + rdb->MountPointReparseBuffer.PrintNameLength = print_len * sizeof(WCHAR); + + /* Store the full native path of link target at the substitute name + offset (0). */ + wcscpy(rdb->MountPointReparseBuffer.PathBuffer, L"\\??\\"); + if (GetFullPathNameW(src_path, print_len + 1, + rdb->MountPointReparseBuffer.PathBuffer + prefix_len, + NULL) == 0) + goto cleanup; + + /* Copy everything but the native prefix to the print name offset. */ + wcscpy(rdb->MountPointReparseBuffer.PathBuffer + + prefix_len + print_len + 1, + rdb->MountPointReparseBuffer.PathBuffer + prefix_len); + + /* Create a directory for the junction point. */ + if (!CreateDirectoryW(dst_path, NULL)) + goto cleanup; + + junction = CreateFileW(dst_path, GENERIC_READ | GENERIC_WRITE, 0, NULL, + OPEN_EXISTING, + FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL); + if (junction == INVALID_HANDLE_VALUE) + goto cleanup; + + /* Make the directory entry a junction point. */ + if (!DeviceIoControl(junction, FSCTL_SET_REPARSE_POINT, rdb, rdb_size, + NULL, 0, &ret, NULL)) + goto cleanup; + +cleanup: + ret = GetLastError(); + + CloseHandle(token); + CloseHandle(junction); + PyMem_RawFree(rdb); + + if (ret != 0) + return PyErr_SetFromWindowsErr(ret); + + Py_RETURN_NONE; +} + +static PyObject * winapi_CreateNamedPipe(PyObject *self, PyObject *args) { LPCTSTR lpName; @@ -1225,6 +1360,8 @@ static PyMethodDef winapi_functions[] = { METH_VARARGS | METH_KEYWORDS, ""}, {"CreateFile", winapi_CreateFile, METH_VARARGS, ""}, + {"CreateJunction", winapi_CreateJunction, METH_VARARGS, + ""}, {"CreateNamedPipe", winapi_CreateNamedPipe, METH_VARARGS, ""}, {"CreatePipe", winapi_CreatePipe, METH_VARARGS, diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c index 9bb3666..e1b693f 100644 --- a/Modules/gcmodule.c +++ b/Modules/gcmodule.c @@ -1715,15 +1715,19 @@ PyObject_GC_UnTrack(void *op) _PyObject_GC_UNTRACK(op); } -PyObject * -_PyObject_GC_Malloc(size_t basicsize) +static PyObject * +_PyObject_GC_Alloc(int use_calloc, size_t basicsize) { PyObject *op; PyGC_Head *g; + size_t size; if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) return PyErr_NoMemory(); - g = (PyGC_Head *)PyObject_MALLOC( - sizeof(PyGC_Head) + basicsize); + size = sizeof(PyGC_Head) + basicsize; + if (use_calloc) + g = (PyGC_Head *)PyObject_Calloc(1, size); + else + g = (PyGC_Head *)PyObject_Malloc(size); if (g == NULL) return PyErr_NoMemory(); g->gc.gc_refs = 0; @@ -1743,6 +1747,18 @@ _PyObject_GC_Malloc(size_t basicsize) } PyObject * +_PyObject_GC_Malloc(size_t basicsize) +{ + return _PyObject_GC_Alloc(0, basicsize); +} + +PyObject * +_PyObject_GC_Calloc(size_t basicsize) +{ + return _PyObject_GC_Alloc(1, basicsize); +} + +PyObject * _PyObject_GC_New(PyTypeObject *tp) { PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp)); diff --git a/Modules/main.c b/Modules/main.c index 87a21d7..1c25326 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -343,6 +343,8 @@ Py_Main(int argc, wchar_t **argv) int version = 0; int saw_unbuffered_flag = 0; PyCompilerFlags cf; + PyObject *warning_option = NULL; + PyObject *warning_options = NULL; cf.cf_flags = 0; @@ -465,7 +467,15 @@ Py_Main(int argc, wchar_t **argv) break; case 'W': - PySys_AddWarnOption(_PyOS_optarg); + if (warning_options == NULL) + warning_options = PyList_New(0); + if (warning_options == NULL) + Py_FatalError("failure in handling of -W argument"); + warning_option = PyUnicode_FromWideChar(_PyOS_optarg, -1); + if (warning_option == NULL) + Py_FatalError("failure in handling of -W argument"); + PyList_Append(warning_options, warning_option); + Py_DECREF(warning_option); break; case 'X': @@ -559,6 +569,12 @@ Py_Main(int argc, wchar_t **argv) PyMem_RawFree(buf); } #endif + if (warning_options != NULL) { + Py_ssize_t i; + for (i = 0; i < PyList_GET_SIZE(warning_options); i++) { + PySys_AddWarnOptionUnicode(PyList_GET_ITEM(warning_options, i)); + } + } if (command == NULL && module == NULL && _PyOS_optind < argc && wcscmp(argv[_PyOS_optind], L"-") != 0) @@ -652,6 +668,7 @@ Py_Main(int argc, wchar_t **argv) Py_SetProgramName(argv[0]); #endif Py_Initialize(); + Py_XDECREF(warning_options); if (!Py_QuietFlag && (Py_VerboseFlag || (command == NULL && filename == NULL && diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 7f094ff..7f525ea 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -1408,6 +1408,7 @@ static PyObject * math_factorial(PyObject *self, PyObject *arg) { long x; + int overflow; PyObject *result, *odd_part, *two_valuation; if (PyFloat_Check(arg)) { @@ -1421,15 +1422,22 @@ math_factorial(PyObject *self, PyObject *arg) lx = PyLong_FromDouble(dx); if (lx == NULL) return NULL; - x = PyLong_AsLong(lx); + x = PyLong_AsLongAndOverflow(lx, &overflow); Py_DECREF(lx); } else - x = PyLong_AsLong(arg); + x = PyLong_AsLongAndOverflow(arg, &overflow); - if (x == -1 && PyErr_Occurred()) + if (x == -1 && PyErr_Occurred()) { + return NULL; + } + else if (overflow == 1) { + PyErr_Format(PyExc_OverflowError, + "factorial() argument should not exceed %ld", + LONG_MAX); return NULL; - if (x < 0) { + } + else if (overflow == -1 || x < 0) { PyErr_SetString(PyExc_ValueError, "factorial() not defined for negative values"); return NULL; diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 8cd5485..916be81 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -27,6 +27,8 @@ #include "Python.h" #ifndef MS_WINDOWS #include "posixmodule.h" +#else +#include "winreparse.h" #endif #ifdef __cplusplus @@ -301,6 +303,9 @@ extern int lstat(const char *, struct stat *); #ifndef IO_REPARSE_TAG_SYMLINK #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) #endif +#ifndef IO_REPARSE_TAG_MOUNT_POINT +#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) +#endif #include "osdefs.h" #include <malloc.h> #include <windows.h> @@ -1109,41 +1114,6 @@ _PyVerify_fd_dup2(int fd1, int fd2) #endif #ifdef MS_WINDOWS -/* The following structure was copied from - http://msdn.microsoft.com/en-us/library/ms791514.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 { - ULONG ReparseTag; - USHORT ReparseDataLength; - USHORT Reserved; - union { - struct { - USHORT SubstituteNameOffset; - USHORT SubstituteNameLength; - USHORT PrintNameOffset; - USHORT PrintNameLength; - ULONG Flags; - WCHAR PathBuffer[1]; - } SymbolicLinkReparseBuffer; - - struct { - USHORT SubstituteNameOffset; - USHORT SubstituteNameLength; - USHORT PrintNameOffset; - USHORT PrintNameLength; - WCHAR PathBuffer[1]; - } MountPointReparseBuffer; - - struct { - UCHAR DataBuffer[1]; - } GenericReparseBuffer; - }; -} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; - -#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ - GenericReparseBuffer) -#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) static int win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) @@ -4492,7 +4462,10 @@ BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) find_data_handle = FindFirstFileW(lpFileName, &find_data); if(find_data_handle != INVALID_HANDLE_VALUE) { - is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK; + /* IO_REPARSE_TAG_SYMLINK if it is a symlink and + IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ + is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || + find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; FindClose(find_data_handle); } } diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index fedaddf..cf4ba61 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -956,7 +956,7 @@ the first is the signal number, the second is the interrupted stack frame."); static struct PyModuleDef signalmodule = { PyModuleDef_HEAD_INIT, - "signal", + "_signal", module_doc, -1, signal_methods, @@ -967,7 +967,7 @@ static struct PyModuleDef signalmodule = { }; PyMODINIT_FUNC -PyInit_signal(void) +PyInit__signal(void) { PyObject *m, *d, *x; int i; @@ -1380,7 +1380,7 @@ PyErr_SetInterrupt(void) void PyOS_InitInterrupts(void) { - PyObject *m = PyImport_ImportModule("signal"); + PyObject *m = PyImport_ImportModule("_signal"); if (m) { Py_DECREF(m); } diff --git a/Modules/winreparse.h b/Modules/winreparse.h new file mode 100644 index 0000000..66f7775 --- /dev/null +++ b/Modules/winreparse.h @@ -0,0 +1,53 @@ +#ifndef Py_WINREPARSE_H +#define Py_WINREPARSE_H + +#ifdef MS_WINDOWS +#include <Windows.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/* 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 { + ULONG ReparseTag; + USHORT ReparseDataLength; + USHORT Reserved; + union { + struct { + USHORT SubstituteNameOffset; + USHORT SubstituteNameLength; + USHORT PrintNameOffset; + USHORT PrintNameLength; + ULONG Flags; + WCHAR PathBuffer[1]; + } SymbolicLinkReparseBuffer; + + struct { + USHORT SubstituteNameOffset; + USHORT SubstituteNameLength; + USHORT PrintNameOffset; + USHORT PrintNameLength; + WCHAR PathBuffer[1]; + } MountPointReparseBuffer; + + struct { + UCHAR DataBuffer[1]; + } GenericReparseBuffer; + }; +} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; + +#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\ + GenericReparseBuffer) +#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 ) + +#ifdef __cplusplus +} +#endif + +#endif /* MS_WINDOWS */ + +#endif /* !Py_WINREPARSE_H */ |