diff -r b541ecd32115 Objects/longobject.c --- a/Objects/longobject.c Fri Feb 07 16:11:17 2014 -0800 +++ b/Objects/longobject.c Sat Feb 08 17:11:02 2014 +0800 @@ -9,6 +9,11 @@ #include #include +/*[clinic input] +class int "PyLongObject *" "&PyLong_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=38820563aa9844c2]*/ + #ifndef NSMALLPOSINTS #define NSMALLPOSINTS 257 #endif @@ -2370,7 +2375,6 @@ /* forward */ static PyLongObject *x_divrem (PyLongObject *, PyLongObject *, PyLongObject **); -static PyObject *long_long(PyObject *v); /* Int division with remainder, top-level routine */ @@ -4026,13 +4030,46 @@ return (PyObject *)z; } +/*[clinic input] +int.conjugate + +Returns self, the complex conjugate of any int. +[clinic start generated code]*/ + +PyDoc_STRVAR(int_conjugate__doc__, +"sig=($self)\n" +"Returns self, the complex conjugate of any int."); + +#define INT_CONJUGATE_METHODDEF \ + {"conjugate", (PyCFunction)int_conjugate, METH_NOARGS, int_conjugate__doc__}, + +static PyObject * +int_conjugate_impl(PyLongObject *self); + +static PyObject * +int_conjugate(PyLongObject *self, PyObject *Py_UNUSED(ignored)) +{ + return int_conjugate_impl(self); +} + +static PyObject * +int_conjugate_impl(PyLongObject *self) +/*[clinic end generated code: output=b83eb9a4df01bf27 input=778243cb5bb83eee]*/ +{ + if (PyLong_CheckExact((PyObject *)self)) + Py_INCREF(self); + else + self = (PyLongObject *)_PyLong_Copy(self); + return (PyObject *)self; +} + static PyObject * long_abs(PyLongObject *v) { if (Py_SIZE(v) < 0) return long_neg(v); else - return long_long((PyObject *)v); + return int_conjugate_impl(v); } static int @@ -4317,16 +4354,6 @@ } static PyObject * -long_long(PyObject *v) -{ - if (PyLong_CheckExact(v)) - Py_INCREF(v); - else - v = _PyLong_Copy((PyLongObject *)v); - return v; -} - -static PyObject * long_float(PyObject *v) { double result; @@ -4420,10 +4447,30 @@ return (PyObject *)newobj; } +/*[clinic input] +int.__getnewargs__ +[clinic start generated code]*/ + +PyDoc_STRVAR(int___getnewargs____doc__, +"sig=($self)"); + +#define INT___GETNEWARGS___METHODDEF \ + {"__getnewargs__", (PyCFunction)int___getnewargs__, METH_NOARGS, int___getnewargs____doc__}, + static PyObject * -long_getnewargs(PyLongObject *v) +int___getnewargs___impl(PyLongObject *self); + +static PyObject * +int___getnewargs__(PyLongObject *self, PyObject *Py_UNUSED(ignored)) { - return Py_BuildValue("(N)", _PyLong_Copy(v)); + return int___getnewargs___impl(self); +} + +static PyObject * +int___getnewargs___impl(PyLongObject *self) +/*[clinic end generated code: output=7347c3a373c70569 input=5904770ab1fb8c75]*/ +{ + return Py_BuildValue("(N)", _PyLong_Copy(self)); } static PyObject * @@ -4436,20 +4483,49 @@ return PyLong_FromLong(1L); } +/*[clinic input] +int.__format__ + + format_spec: unicode + / +[clinic start generated code]*/ + +PyDoc_STRVAR(int___format____doc__, +"sig=($self, format_spec)"); + +#define INT___FORMAT___METHODDEF \ + {"__format__", (PyCFunction)int___format__, METH_VARARGS, int___format____doc__}, + static PyObject * -long__format__(PyObject *self, PyObject *args) +int___format___impl(PyLongObject *self, PyObject *format_spec); + +static PyObject * +int___format__(PyLongObject *self, PyObject *args) { + PyObject *return_value = NULL; PyObject *format_spec; + + if (!PyArg_ParseTuple(args, + "U:__format__", + &format_spec)) + goto exit; + return_value = int___format___impl(self, format_spec); + +exit: + return return_value; +} + +static PyObject * +int___format___impl(PyLongObject *self, PyObject *format_spec) +/*[clinic end generated code: output=35d079476d6c9ef7 input=e31944a9b3e428b7]*/ +{ _PyUnicodeWriter writer; int ret; - if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) - return NULL; - _PyUnicodeWriter_Init(&writer); ret = _PyLong_FormatAdvancedWriter( &writer, - self, + (PyObject *)self, format_spec, 0, PyUnicode_GET_LENGTH(format_spec)); if (ret == -1) { _PyUnicodeWriter_Dealloc(&writer); @@ -4577,7 +4653,7 @@ if (!PyArg_ParseTuple(args, "|O", &o_ndigits)) return NULL; if (o_ndigits == NULL) - return long_long(self); + return int_conjugate_impl((PyLongObject *)self); ndigits = PyNumber_Index(o_ndigits); if (ndigits == NULL) @@ -4586,7 +4662,7 @@ /* if ndigits >= 0 then no rounding is necessary; return self unchanged */ if (Py_SIZE(ndigits) >= 0) { Py_DECREF(ndigits); - return long_long(self); + return int_conjugate_impl((PyLongObject *)self); } /* result = self - divmod_near(self, 10 ** -ndigits)[1] */ @@ -4623,30 +4699,95 @@ return result; } +/*[clinic input] +int.__sizeof__ -> Py_ssize_t + +Returns size in memory, in bytes. +[clinic start generated code]*/ + +PyDoc_STRVAR(int___sizeof____doc__, +"sig=($self)\n" +"Returns size in memory, in bytes."); + +#define INT___SIZEOF___METHODDEF \ + {"__sizeof__", (PyCFunction)int___sizeof__, METH_NOARGS, int___sizeof____doc__}, + +static Py_ssize_t +int___sizeof___impl(PyLongObject *self); + static PyObject * -long_sizeof(PyLongObject *v) +int___sizeof__(PyLongObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + Py_ssize_t _return_value; + + _return_value = int___sizeof___impl(self); + if ((_return_value == -1) && PyErr_Occurred()) + goto exit; + return_value = PyLong_FromSsize_t(_return_value); + +exit: + return return_value; +} + +static Py_ssize_t +int___sizeof___impl(PyLongObject *self) +/*[clinic end generated code: output=ccf4eb9eb878d6e8 input=9b51620c76fc4507]*/ { Py_ssize_t res; - res = offsetof(PyLongObject, ob_digit) + ABS(Py_SIZE(v))*sizeof(digit); - return PyLong_FromSsize_t(res); + res = offsetof(PyLongObject, ob_digit) + ABS(Py_SIZE(self))*sizeof(digit); + return res; } +/*[clinic input] +int.bit_length + +Number of bits necessary to represent self in binary. + +>>> bin(37) +'0b100101' +>>> (37).bit_length() +6 +[clinic start generated code]*/ + +PyDoc_STRVAR(int_bit_length__doc__, +"sig=($self)\n" +"Number of bits necessary to represent self in binary.\n" +"\n" +">>> bin(37)\n" +"\'0b100101\'\n" +">>> (37).bit_length()\n" +"6"); + +#define INT_BIT_LENGTH_METHODDEF \ + {"bit_length", (PyCFunction)int_bit_length, METH_NOARGS, int_bit_length__doc__}, + static PyObject * -long_bit_length(PyLongObject *v) +int_bit_length_impl(PyLongObject *self); + +static PyObject * +int_bit_length(PyLongObject *self, PyObject *Py_UNUSED(ignored)) +{ + return int_bit_length_impl(self); +} + +static PyObject * +int_bit_length_impl(PyLongObject *self) +/*[clinic end generated code: output=5efd591e16afde6d input=e4eb7a587e849a32]*/ { PyLongObject *result, *x, *y; Py_ssize_t ndigits, msd_bits = 0; digit msd; - assert(v != NULL); - assert(PyLong_Check(v)); - - ndigits = ABS(Py_SIZE(v)); + assert(self != NULL); + assert(PyLong_Check(self)); + + ndigits = ABS(Py_SIZE(self)); if (ndigits == 0) return PyLong_FromLong(0); - msd = v->ob_digit[ndigits-1]; + msd = self->ob_digit[ndigits-1]; while (msd >= 32) { msd_bits += 6; msd >>= 6; @@ -4687,49 +4828,122 @@ return NULL; } -PyDoc_STRVAR(long_bit_length_doc, -"int.bit_length() -> int\n\ -\n\ -Number of bits necessary to represent self in binary.\n\ ->>> bin(37)\n\ -'0b100101'\n\ ->>> (37).bit_length()\n\ -6"); - #if 0 +/*[clinic input] +int.is_finite + +Returns True. +[clinic start generated code]*/ + +PyDoc_STRVAR(int_is_finite__doc__, +"sig=($self)\n" +"Returns True."); + +#define INT_IS_FINITE_METHODDEF \ + {"is_finite", (PyCFunction)int_is_finite, METH_NOARGS, int_is_finite__doc__}, + static PyObject * -long_is_finite(PyObject *v) +int_is_finite_impl(PyLongObject *self); + +static PyObject * +int_is_finite(PyLongObject *self, PyObject *Py_UNUSED(ignored)) +{ + return int_is_finite_impl(self); +} + +static PyObject * +int_is_finite_impl(PyLongObject *self) +/*[clinic end generated code: output=9b4752bd49130ff7 input=843cc8cb0a87d8f8]*/ { Py_RETURN_TRUE; } #endif +/*[clinic input] +dump buffer +[clinic start generated code]*/ + +#ifndef INT_IS_FINITE_METHODDEF + #define INT_IS_FINITE_METHODDEF +#endif /* !defined(INT_IS_FINITE_METHODDEF) */ +/*[clinic end generated code: output=f019cfebd6c4f8f7 input=524ce2e021e4eba6]*/ + +/*[clinic input] +int.to_bytes + + length: Py_ssize_t + Length of bytes object to use for bytes representation. An OverflowError + is raised if the integer is not representable with the given number of + bytes. + byteorder: unicode + The byte order used to represent the integer. If byteorder is 'big', + the most significant byte is at the beginning of the byte array. If + byteorder is 'little', the most significant byte is at the end of the + byte array. To request the native byte order of the host system, use + `sys.byteorder' as the byte order value. + * + signed as is_signed: bool = False + Determines whether two's complement is used to represent the integer. + If signed is False and a negative integer is given, an OverflowError + is raised. + +Return an array of bytes representing an integer. +[clinic start generated code]*/ + +PyDoc_STRVAR(int_to_bytes__doc__, +"sig=($self, length, byteorder, *, signed=False)\n" +"Return an array of bytes representing an integer.\n" +"\n" +" length\n" +" Length of bytes object to use for bytes representation. An OverflowError\n" +" is raised if the integer is not representable with the given number of\n" +" bytes.\n" +" byteorder\n" +" The byte order used to represent the integer. If byteorder is \'big\',\n" +" the most significant byte is at the beginning of the byte array. If\n" +" byteorder is \'little\', the most significant byte is at the end of the\n" +" byte array. To request the native byte order of the host system, use\n" +" `sys.byteorder\' as the byte order value.\n" +" signed\n" +" Determines whether two\'s complement is used to represent the integer.\n" +" If signed is False and a negative integer is given, an OverflowError\n" +" is raised."); + +#define INT_TO_BYTES_METHODDEF \ + {"to_bytes", (PyCFunction)int_to_bytes, METH_VARARGS|METH_KEYWORDS, int_to_bytes__doc__}, static PyObject * -long_to_bytes(PyLongObject *v, PyObject *args, PyObject *kwds) +int_to_bytes_impl(PyLongObject *self, Py_ssize_t length, PyObject *byteorder, int is_signed); + +static PyObject * +int_to_bytes(PyLongObject *self, PyObject *args, PyObject *kwargs) { - PyObject *byteorder_str; - PyObject *is_signed_obj = NULL; + PyObject *return_value = NULL; + static char *_keywords[] = {"length", "byteorder", "signed", NULL}; Py_ssize_t length; + PyObject *byteorder; + int is_signed = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "nU|$p:to_bytes", _keywords, + &length, &byteorder, &is_signed)) + goto exit; + return_value = int_to_bytes_impl(self, length, byteorder, is_signed); + +exit: + return return_value; +} + +static PyObject * +int_to_bytes_impl(PyLongObject *self, Py_ssize_t length, PyObject *byteorder, int is_signed) +/*[clinic end generated code: output=c5de8e3f29ab4826 input=f4d35f063e083ec0]*/ +{ int little_endian; - int is_signed; PyObject *bytes; - static char *kwlist[] = {"length", "byteorder", "signed", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "nU|O:to_bytes", kwlist, - &length, &byteorder_str, - &is_signed_obj)) - return NULL; - - if (args != NULL && Py_SIZE(args) > 2) { - PyErr_SetString(PyExc_TypeError, - "'signed' is a keyword-only argument"); - return NULL; - } - - if (!PyUnicode_CompareWithASCIIString(byteorder_str, "little")) + + if (!PyUnicode_CompareWithASCIIString(byteorder, "little")) little_endian = 1; - else if (!PyUnicode_CompareWithASCIIString(byteorder_str, "big")) + else if (!PyUnicode_CompareWithASCIIString(byteorder, "big")) little_endian = 0; else { PyErr_SetString(PyExc_ValueError, @@ -4737,18 +4951,6 @@ return NULL; } - if (is_signed_obj != NULL) { - int cmp = PyObject_IsTrue(is_signed_obj); - if (cmp < 0) - return NULL; - is_signed = cmp ? 1 : 0; - } - else { - /* If the signed argument was omitted, use False as the - default. */ - is_signed = 0; - } - if (length < 0) { PyErr_SetString(PyExc_ValueError, "length argument must be non-negative"); @@ -4759,7 +4961,7 @@ if (bytes == NULL) return NULL; - if (_PyLong_AsByteArray(v, (unsigned char *)PyBytes_AS_STRING(bytes), + if (_PyLong_AsByteArray(self, (unsigned char *)PyBytes_AS_STRING(bytes), length, little_endian, is_signed) < 0) { Py_DECREF(bytes); return NULL; @@ -4768,51 +4970,81 @@ return bytes; } -PyDoc_STRVAR(long_to_bytes_doc, -"int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\ -\n\ -Return an array of bytes representing an integer.\n\ -\n\ -The integer is represented using length bytes. An OverflowError is\n\ -raised if the integer is not representable with the given number of\n\ -bytes.\n\ -\n\ -The byteorder argument determines the byte order used to represent the\n\ -integer. If byteorder is 'big', the most significant byte is at the\n\ -beginning of the byte array. If byteorder is 'little', the most\n\ -significant byte is at the end of the byte array. To request the native\n\ -byte order of the host system, use `sys.byteorder' as the byte order value.\n\ -\n\ -The signed keyword-only argument determines whether two's complement is\n\ -used to represent the integer. If signed is False and a negative integer\n\ -is given, an OverflowError is raised."); +/*[clinic input] +@classmethod +int.from_bytes + + bytes as bytes_obj: object + Bytes that will be converted to integer. The bytes must either support + the buffer protocol or be an iterable object producing bytes. Bytes + and bytearray are examples of built-in objects that support the buffer + protocol. + byteorder: unicode + The byte order used to represent the integer. If byteorder is 'big', + the most significant byte is at the beginning of the byte array. If + byteorder is 'little', the most significant byte is at the end of the + byte array. To request the native byte order of the host system, use + `sys.byteorder' as the byte order value. + * + signed as is_signed: bool = False + Indicates whether two's complement is used to represent the integer. + +Return the integer represented by the given array of bytes. +[clinic start generated code]*/ + +PyDoc_STRVAR(int_from_bytes__doc__, +"sig=($type, bytes, byteorder, *, signed=False)\n" +"Return the integer represented by the given array of bytes.\n" +"\n" +" bytes\n" +" Bytes that will be converted to integer. The bytes must either support\n" +" the buffer protocol or be an iterable object producing bytes. Bytes\n" +" and bytearray are examples of built-in objects that support the buffer\n" +" protocol.\n" +" byteorder\n" +" The byte order used to represent the integer. If byteorder is \'big\',\n" +" the most significant byte is at the beginning of the byte array. If\n" +" byteorder is \'little\', the most significant byte is at the end of the\n" +" byte array. To request the native byte order of the host system, use\n" +" `sys.byteorder\' as the byte order value.\n" +" signed\n" +" Indicates whether two\'s complement is used to represent the integer."); + +#define INT_FROM_BYTES_METHODDEF \ + {"from_bytes", (PyCFunction)int_from_bytes, METH_VARARGS|METH_KEYWORDS|METH_CLASS, int_from_bytes__doc__}, static PyObject * -long_from_bytes(PyTypeObject *type, PyObject *args, PyObject *kwds) +int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj, PyObject *byteorder, int is_signed); + +static PyObject * +int_from_bytes(PyTypeObject *type, PyObject *args, PyObject *kwargs) { - PyObject *byteorder_str; - PyObject *is_signed_obj = NULL; + PyObject *return_value = NULL; + static char *_keywords[] = {"bytes", "byteorder", "signed", NULL}; + PyObject *bytes_obj; + PyObject *byteorder; + int is_signed = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "OU|$p:from_bytes", _keywords, + &bytes_obj, &byteorder, &is_signed)) + goto exit; + return_value = int_from_bytes_impl(type, bytes_obj, byteorder, is_signed); + +exit: + return return_value; +} + +static PyObject * +int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj, PyObject *byteorder, int is_signed) +/*[clinic end generated code: output=ba07e001e59cc5f4 input=5f0a6b7fbdb4cb16]*/ +{ int little_endian; - int is_signed; - PyObject *obj; - PyObject *bytes; - PyObject *long_obj; - static char *kwlist[] = {"bytes", "byteorder", "signed", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "OU|O:from_bytes", kwlist, - &obj, &byteorder_str, - &is_signed_obj)) - return NULL; - - if (args != NULL && Py_SIZE(args) > 2) { - PyErr_SetString(PyExc_TypeError, - "'signed' is a keyword-only argument"); - return NULL; - } - - if (!PyUnicode_CompareWithASCIIString(byteorder_str, "little")) + PyObject *long_obj, *bytes; + + if (!PyUnicode_CompareWithASCIIString(byteorder, "little")) little_endian = 1; - else if (!PyUnicode_CompareWithASCIIString(byteorder_str, "big")) + else if (!PyUnicode_CompareWithASCIIString(byteorder, "big")) little_endian = 0; else { PyErr_SetString(PyExc_ValueError, @@ -4820,19 +5052,7 @@ return NULL; } - if (is_signed_obj != NULL) { - int cmp = PyObject_IsTrue(is_signed_obj); - if (cmp < 0) - return NULL; - is_signed = cmp ? 1 : 0; - } - else { - /* If the signed argument was omitted, use False as the - default. */ - is_signed = 0; - } - - bytes = PyObject_Bytes(obj); + bytes = PyObject_Bytes(bytes_obj); if (bytes == NULL) return NULL; @@ -4867,56 +5087,30 @@ return long_obj; } -PyDoc_STRVAR(long_from_bytes_doc, -"int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\ -\n\ -Return the integer represented by the given array of bytes.\n\ -\n\ -The bytes argument must either support the buffer protocol or be an\n\ -iterable object producing bytes. Bytes and bytearray are examples of\n\ -built-in objects that support the buffer protocol.\n\ -\n\ -The byteorder argument determines the byte order used to represent the\n\ -integer. If byteorder is 'big', the most significant byte is at the\n\ -beginning of the byte array. If byteorder is 'little', the most\n\ -significant byte is at the end of the byte array. To request the native\n\ -byte order of the host system, use `sys.byteorder' as the byte order value.\n\ -\n\ -The signed keyword-only argument indicates whether two's complement is\n\ -used to represent the integer."); - static PyMethodDef long_methods[] = { - {"conjugate", (PyCFunction)long_long, METH_NOARGS, - "Returns self, the complex conjugate of any int."}, - {"bit_length", (PyCFunction)long_bit_length, METH_NOARGS, - long_bit_length_doc}, -#if 0 - {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS, - "Returns always True."}, -#endif - {"to_bytes", (PyCFunction)long_to_bytes, - METH_VARARGS|METH_KEYWORDS, long_to_bytes_doc}, - {"from_bytes", (PyCFunction)long_from_bytes, - METH_VARARGS|METH_KEYWORDS|METH_CLASS, long_from_bytes_doc}, - {"__trunc__", (PyCFunction)long_long, METH_NOARGS, + INT_CONJUGATE_METHODDEF + INT_BIT_LENGTH_METHODDEF + INT_IS_FINITE_METHODDEF + INT_TO_BYTES_METHODDEF + INT_FROM_BYTES_METHODDEF + {"__trunc__", (PyCFunction)int_conjugate, METH_NOARGS, "Truncating an Integral returns itself."}, - {"__floor__", (PyCFunction)long_long, METH_NOARGS, + {"__floor__", (PyCFunction)int_conjugate, METH_NOARGS, "Flooring an Integral returns itself."}, - {"__ceil__", (PyCFunction)long_long, METH_NOARGS, + {"__ceil__", (PyCFunction)int_conjugate, METH_NOARGS, "Ceiling of an Integral returns itself."}, {"__round__", (PyCFunction)long_round, METH_VARARGS, "Rounding an Integral returns itself.\n" "Rounding with an ndigits argument also returns an integer."}, - {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS}, - {"__format__", (PyCFunction)long__format__, METH_VARARGS}, - {"__sizeof__", (PyCFunction)long_sizeof, METH_NOARGS, - "Returns size in memory, in bytes"}, + INT___GETNEWARGS___METHODDEF + INT___FORMAT___METHODDEF + INT___SIZEOF___METHODDEF {NULL, NULL} /* sentinel */ }; static PyGetSetDef long_getset[] = { {"real", - (getter)long_long, (setter)NULL, + (getter)int_conjugate, (setter)NULL, "the real part of a complex number", NULL}, {"imag", @@ -4924,7 +5118,7 @@ "the imaginary part of a complex number", NULL}, {"numerator", - (getter)long_long, (setter)NULL, + (getter)int_conjugate, (setter)NULL, "the numerator of a rational number in lowest terms", NULL}, {"denominator", @@ -4958,7 +5152,7 @@ long_divmod, /*nb_divmod*/ long_pow, /*nb_power*/ (unaryfunc)long_neg, /*nb_negative*/ - (unaryfunc)long_long, /*tp_positive*/ + (unaryfunc)int_conjugate, /*tp_positive*/ (unaryfunc)long_abs, /*tp_absolute*/ (inquiry)long_bool, /*tp_bool*/ (unaryfunc)long_invert, /*nb_invert*/ @@ -4967,7 +5161,7 @@ long_and, /*nb_and*/ long_xor, /*nb_xor*/ long_or, /*nb_or*/ - long_long, /*nb_int*/ + (unaryfunc)int_conjugate, /*nb_int*/ 0, /*nb_reserved*/ long_float, /*nb_float*/ 0, /* nb_inplace_add */ @@ -4984,7 +5178,7 @@ long_true_divide, /* nb_true_divide */ 0, /* nb_inplace_floor_divide */ 0, /* nb_inplace_true_divide */ - long_long, /* nb_index */ + (unaryfunc)int_conjugate, /* nb_index */ }; PyTypeObject PyLong_Type = {