diff -r d0e2437136f5 Objects/longobject.c --- a/Objects/longobject.c Sun Jan 19 03:01:23 2014 -0800 +++ b/Objects/longobject.c Tue Jan 21 16:48:07 2014 +0800 @@ -9,6 +9,11 @@ #include #include +/*[clinic input] +class long +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + #ifndef NSMALLPOSINTS #define NSMALLPOSINTS 257 #endif @@ -4436,16 +4441,45 @@ return PyLong_FromLong(1L); } +/*[clinic input] +long.__format__ + + format_spec: unicode + / +[clinic start generated code]*/ + +PyDoc_STRVAR(long___format____doc__, +"__format__(format_spec)"); + +#define LONG___FORMAT___METHODDEF \ + {"__format__", (PyCFunction)long___format__, METH_VARARGS, long___format____doc__}, + static PyObject * -long__format__(PyObject *self, PyObject *args) +long___format___impl(PyObject *self, PyObject *format_spec); + +static PyObject * +long___format__(PyObject *self, PyObject *args) { + PyObject *return_value = NULL; PyObject *format_spec; + + if (!PyArg_ParseTuple(args, + "U:__format__", + &format_spec)) + goto exit; + return_value = long___format___impl(self, format_spec); + +exit: + return return_value; +} + +static PyObject * +long___format___impl(PyObject *self, PyObject *format_spec) +/*[clinic end generated code: checksum=c2e97db2f7502ce804bb35c004f02f08dd4dc080]*/ +{ _PyUnicodeWriter writer; int ret; - if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) - return NULL; - _PyUnicodeWriter_Init(&writer); ret = _PyLong_FormatAdvancedWriter( &writer, @@ -4704,32 +4738,86 @@ } #endif +/*[clinic input] +long.to_bytes + + self: self(type="PyLongObject *") + length: Py_ssize_t + byteorder: unicode + * + signed: object(c_default="NULL") = False + +Return an array of bytes representing an integer. + +The integer is represented using length bytes. An OverflowError is +raised if the integer is not representable with the given number of +bytes. + +The byteorder argument determines 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. + +The signed keyword-only argument 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. +[clinic start generated code]*/ + +PyDoc_STRVAR(long_to_bytes__doc__, +"to_bytes(length, byteorder, *, signed=False)\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."); + +#define LONG_TO_BYTES_METHODDEF \ + {"to_bytes", (PyCFunction)long_to_bytes, METH_VARARGS|METH_KEYWORDS, long_to_bytes__doc__}, static PyObject * -long_to_bytes(PyLongObject *v, PyObject *args, PyObject *kwds) +long_to_bytes_impl(PyLongObject *self, Py_ssize_t length, PyObject *byteorder, PyObject *signed_value); + +static PyObject * +long_to_bytes(PyObject *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; + PyObject *signed_value = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "nU|$O:to_bytes", _keywords, + &length, &byteorder, &signed_value)) + goto exit; + return_value = long_to_bytes_impl((PyLongObject *)self, length, byteorder, signed_value); + +exit: + return return_value; +} + +static PyObject * +long_to_bytes_impl(PyLongObject *self, Py_ssize_t length, PyObject *byteorder, PyObject *signed_value) +/*[clinic end generated code: checksum=31d1c538e6012b7ce6aa727342a536efe87c4d4b]*/ +{ 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,8 +4825,8 @@ return NULL; } - if (is_signed_obj != NULL) { - int cmp = PyObject_IsTrue(is_signed_obj); + if (signed_value != NULL) { + int cmp = PyObject_IsTrue(signed_value); if (cmp < 0) return NULL; is_signed = cmp ? 1 : 0; @@ -4759,7 +4847,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 +4856,86 @@ 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 +long.from_bytes + + self: self(type="PyTypeObject *") + bytes: object + byteorder: unicode + * + signed: object(c_default="NULL") = False + +Return the integer represented by the given array of bytes. + +The bytes argument 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. + +The byteorder argument determines 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. + +The signed keyword-only argument indicates whether two's complement is +used to represent the integer. +[clinic start generated code]*/ + +PyDoc_STRVAR(long_from_bytes__doc__, +"from_bytes(bytes, byteorder, *, signed=False)\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."); + +#define LONG_FROM_BYTES_METHODDEF \ + {"from_bytes", (PyCFunction)long_from_bytes, METH_VARARGS|METH_KEYWORDS|METH_CLASS, long_from_bytes__doc__}, static PyObject * -long_from_bytes(PyTypeObject *type, PyObject *args, PyObject *kwds) +long_from_bytes_impl(PyTypeObject *cls, PyObject *bytes, PyObject *byteorder, PyObject *signed_value); + +static PyObject * +long_from_bytes(PyTypeObject *cls, 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; + PyObject *byteorder; + PyObject *signed_value = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "OU|$O:from_bytes", _keywords, + &bytes, &byteorder, &signed_value)) + goto exit; + return_value = long_from_bytes_impl(cls, bytes, byteorder, signed_value); + +exit: + return return_value; +} + +static PyObject * +long_from_bytes_impl(PyTypeObject *cls, PyObject *bytes, PyObject *byteorder, PyObject *signed_value) +/*[clinic end generated code: checksum=3036e218decd4619c90b31301841203c48ef06b6]*/ +{ int little_endian; int is_signed; - PyObject *obj; - PyObject *bytes; + PyObject *internal_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")) + + 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,8 +4943,8 @@ return NULL; } - if (is_signed_obj != NULL) { - int cmp = PyObject_IsTrue(is_signed_obj); + if (signed_value != NULL) { + int cmp = PyObject_IsTrue(signed_value); if (cmp < 0) return NULL; is_signed = cmp ? 1 : 0; @@ -4832,24 +4955,24 @@ is_signed = 0; } - bytes = PyObject_Bytes(obj); - if (bytes == NULL) + internal_bytes = PyObject_Bytes(bytes); + if (internal_bytes == NULL) return NULL; long_obj = _PyLong_FromByteArray( - (unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes), + (unsigned char *)PyBytes_AS_STRING(internal_bytes), Py_SIZE(internal_bytes), little_endian, is_signed); - Py_DECREF(bytes); + Py_DECREF(internal_bytes); /* If from_bytes() was used on subclass, allocate new subclass * instance, initialize it with decoded int value and return it. */ - if (type != &PyLong_Type && PyType_IsSubtype(type, &PyLong_Type)) { + if (cls != &PyLong_Type && PyType_IsSubtype(cls, &PyLong_Type)) { PyLongObject *newobj; int i; Py_ssize_t n = ABS(Py_SIZE(long_obj)); - newobj = (PyLongObject *)type->tp_alloc(type, n); + newobj = (PyLongObject *)cls->tp_alloc(cls, n); if (newobj == NULL) { Py_DECREF(long_obj); return NULL; @@ -4867,24 +4990,6 @@ 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."}, @@ -4894,10 +4999,8 @@ {"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}, + LONG_TO_BYTES_METHODDEF + LONG_FROM_BYTES_METHODDEF {"__trunc__", (PyCFunction)long_long, METH_NOARGS, "Truncating an Integral returns itself."}, {"__floor__", (PyCFunction)long_long, METH_NOARGS, @@ -4908,7 +5011,7 @@ "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}, + LONG___FORMAT___METHODDEF {"__sizeof__", (PyCFunction)long_sizeof, METH_NOARGS, "Returns size in memory, in bytes"}, {NULL, NULL} /* sentinel */