]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Python/Python-2.7.10/Objects/intobject.c
AppPkg/Applications/Python/Python-2.7.10: Initial Checkin part 3/5.
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / Objects / intobject.c
diff --git a/AppPkg/Applications/Python/Python-2.7.10/Objects/intobject.c b/AppPkg/Applications/Python/Python-2.7.10/Objects/intobject.c
new file mode 100644 (file)
index 0000000..93732b0
--- /dev/null
@@ -0,0 +1,1581 @@
+\r
+/* Integer object implementation */\r
+\r
+#include "Python.h"\r
+#include <ctype.h>\r
+#include <float.h>\r
+\r
+static PyObject *int_int(PyIntObject *v);\r
+\r
+long\r
+PyInt_GetMax(void)\r
+{\r
+    return LONG_MAX;            /* To initialize sys.maxint */\r
+}\r
+\r
+/* Integers are quite normal objects, to make object handling uniform.\r
+   (Using odd pointers to represent integers would save much space\r
+   but require extra checks for this special case throughout the code.)\r
+   Since a typical Python program spends much of its time allocating\r
+   and deallocating integers, these operations should be very fast.\r
+   Therefore we use a dedicated allocation scheme with a much lower\r
+   overhead (in space and time) than straight malloc(): a simple\r
+   dedicated free list, filled when necessary with memory from malloc().\r
+\r
+   block_list is a singly-linked list of all PyIntBlocks ever allocated,\r
+   linked via their next members.  PyIntBlocks are never returned to the\r
+   system before shutdown (PyInt_Fini).\r
+\r
+   free_list is a singly-linked list of available PyIntObjects, linked\r
+   via abuse of their ob_type members.\r
+*/\r
+\r
+#define BLOCK_SIZE      1000    /* 1K less typical malloc overhead */\r
+#define BHEAD_SIZE      8       /* Enough for a 64-bit pointer */\r
+#define N_INTOBJECTS    ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))\r
+\r
+struct _intblock {\r
+    struct _intblock *next;\r
+    PyIntObject objects[N_INTOBJECTS];\r
+};\r
+\r
+typedef struct _intblock PyIntBlock;\r
+\r
+static PyIntBlock *block_list = NULL;\r
+static PyIntObject *free_list = NULL;\r
+\r
+static PyIntObject *\r
+fill_free_list(void)\r
+{\r
+    PyIntObject *p, *q;\r
+    /* Python's object allocator isn't appropriate for large blocks. */\r
+    p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));\r
+    if (p == NULL)\r
+        return (PyIntObject *) PyErr_NoMemory();\r
+    ((PyIntBlock *)p)->next = block_list;\r
+    block_list = (PyIntBlock *)p;\r
+    /* Link the int objects together, from rear to front, then return\r
+       the address of the last int object in the block. */\r
+    p = &((PyIntBlock *)p)->objects[0];\r
+    q = p + N_INTOBJECTS;\r
+    while (--q > p)\r
+        Py_TYPE(q) = (struct _typeobject *)(q-1);\r
+    Py_TYPE(q) = NULL;\r
+    return p + N_INTOBJECTS - 1;\r
+}\r
+\r
+#ifndef NSMALLPOSINTS\r
+#define NSMALLPOSINTS           257\r
+#endif\r
+#ifndef NSMALLNEGINTS\r
+#define NSMALLNEGINTS           5\r
+#endif\r
+#if NSMALLNEGINTS + NSMALLPOSINTS > 0\r
+/* References to small integers are saved in this array so that they\r
+   can be shared.\r
+   The integers that are saved are those in the range\r
+   -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).\r
+*/\r
+static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];\r
+#endif\r
+#ifdef COUNT_ALLOCS\r
+Py_ssize_t quick_int_allocs;\r
+Py_ssize_t quick_neg_int_allocs;\r
+#endif\r
+\r
+PyObject *\r
+PyInt_FromLong(long ival)\r
+{\r
+    register PyIntObject *v;\r
+#if NSMALLNEGINTS + NSMALLPOSINTS > 0\r
+    if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {\r
+        v = small_ints[ival + NSMALLNEGINTS];\r
+        Py_INCREF(v);\r
+#ifdef COUNT_ALLOCS\r
+        if (ival >= 0)\r
+            quick_int_allocs++;\r
+        else\r
+            quick_neg_int_allocs++;\r
+#endif\r
+        return (PyObject *) v;\r
+    }\r
+#endif\r
+    if (free_list == NULL) {\r
+        if ((free_list = fill_free_list()) == NULL)\r
+            return NULL;\r
+    }\r
+    /* Inline PyObject_New */\r
+    v = free_list;\r
+    free_list = (PyIntObject *)Py_TYPE(v);\r
+    PyObject_INIT(v, &PyInt_Type);\r
+    v->ob_ival = ival;\r
+    return (PyObject *) v;\r
+}\r
+\r
+PyObject *\r
+PyInt_FromSize_t(size_t ival)\r
+{\r
+    if (ival <= LONG_MAX)\r
+        return PyInt_FromLong((long)ival);\r
+    return _PyLong_FromSize_t(ival);\r
+}\r
+\r
+PyObject *\r
+PyInt_FromSsize_t(Py_ssize_t ival)\r
+{\r
+    if (ival >= LONG_MIN && ival <= LONG_MAX)\r
+        return PyInt_FromLong((long)ival);\r
+    return _PyLong_FromSsize_t(ival);\r
+}\r
+\r
+static void\r
+int_dealloc(PyIntObject *v)\r
+{\r
+    if (PyInt_CheckExact(v)) {\r
+        Py_TYPE(v) = (struct _typeobject *)free_list;\r
+        free_list = v;\r
+    }\r
+    else\r
+        Py_TYPE(v)->tp_free((PyObject *)v);\r
+}\r
+\r
+static void\r
+int_free(PyIntObject *v)\r
+{\r
+    Py_TYPE(v) = (struct _typeobject *)free_list;\r
+    free_list = v;\r
+}\r
+\r
+long\r
+PyInt_AsLong(register PyObject *op)\r
+{\r
+    PyNumberMethods *nb;\r
+    PyIntObject *io;\r
+    long val;\r
+\r
+    if (op && PyInt_Check(op))\r
+        return PyInt_AS_LONG((PyIntObject*) op);\r
+\r
+    if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||\r
+        nb->nb_int == NULL) {\r
+        PyErr_SetString(PyExc_TypeError, "an integer is required");\r
+        return -1;\r
+    }\r
+\r
+    io = (PyIntObject*) (*nb->nb_int) (op);\r
+    if (io == NULL)\r
+        return -1;\r
+    if (!PyInt_Check(io)) {\r
+        if (PyLong_Check(io)) {\r
+            /* got a long? => retry int conversion */\r
+            val = PyLong_AsLong((PyObject *)io);\r
+            Py_DECREF(io);\r
+            if ((val == -1) && PyErr_Occurred())\r
+                return -1;\r
+            return val;\r
+        }\r
+        else\r
+        {\r
+            Py_DECREF(io);\r
+            PyErr_SetString(PyExc_TypeError,\r
+                        "__int__ method should return an integer");\r
+            return -1;\r
+        }\r
+    }\r
+\r
+    val = PyInt_AS_LONG(io);\r
+    Py_DECREF(io);\r
+\r
+    return val;\r
+}\r
+\r
+int\r
+_PyInt_AsInt(PyObject *obj)\r
+{\r
+    long result = PyInt_AsLong(obj);\r
+    if (result == -1 && PyErr_Occurred())\r
+        return -1;\r
+    if (result > INT_MAX || result < INT_MIN) {\r
+        PyErr_SetString(PyExc_OverflowError,\r
+                        "Python int too large to convert to C int");\r
+        return -1;\r
+    }\r
+    return (int)result;\r
+}\r
+\r
+Py_ssize_t\r
+PyInt_AsSsize_t(register PyObject *op)\r
+{\r
+#if SIZEOF_SIZE_T != SIZEOF_LONG\r
+    PyNumberMethods *nb;\r
+    PyObject *io;\r
+    Py_ssize_t val;\r
+#endif\r
+\r
+    if (op == NULL) {\r
+        PyErr_SetString(PyExc_TypeError, "an integer is required");\r
+        return -1;\r
+    }\r
+\r
+    if (PyInt_Check(op))\r
+        return PyInt_AS_LONG((PyIntObject*) op);\r
+    if (PyLong_Check(op))\r
+        return _PyLong_AsSsize_t(op);\r
+#if SIZEOF_SIZE_T == SIZEOF_LONG\r
+    return PyInt_AsLong(op);\r
+#else\r
+\r
+    if ((nb = Py_TYPE(op)->tp_as_number) == NULL ||\r
+        (nb->nb_int == NULL && nb->nb_long == 0)) {\r
+        PyErr_SetString(PyExc_TypeError, "an integer is required");\r
+        return -1;\r
+    }\r
+\r
+    if (nb->nb_long != 0)\r
+        io = (*nb->nb_long)(op);\r
+    else\r
+        io = (*nb->nb_int)(op);\r
+    if (io == NULL)\r
+        return -1;\r
+    if (!PyInt_Check(io)) {\r
+        if (PyLong_Check(io)) {\r
+            /* got a long? => retry int conversion */\r
+            val = _PyLong_AsSsize_t(io);\r
+            Py_DECREF(io);\r
+            if ((val == -1) && PyErr_Occurred())\r
+                return -1;\r
+            return val;\r
+        }\r
+        else\r
+        {\r
+            Py_DECREF(io);\r
+            PyErr_SetString(PyExc_TypeError,\r
+                        "__int__ method should return an integer");\r
+            return -1;\r
+        }\r
+    }\r
+\r
+    val = PyInt_AS_LONG(io);\r
+    Py_DECREF(io);\r
+\r
+    return val;\r
+#endif\r
+}\r
+\r
+unsigned long\r
+PyInt_AsUnsignedLongMask(register PyObject *op)\r
+{\r
+    PyNumberMethods *nb;\r
+    PyIntObject *io;\r
+    unsigned long val;\r
+\r
+    if (op && PyInt_Check(op))\r
+        return PyInt_AS_LONG((PyIntObject*) op);\r
+    if (op && PyLong_Check(op))\r
+        return PyLong_AsUnsignedLongMask(op);\r
+\r
+    if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||\r
+        nb->nb_int == NULL) {\r
+        PyErr_SetString(PyExc_TypeError, "an integer is required");\r
+        return (unsigned long)-1;\r
+    }\r
+\r
+    io = (PyIntObject*) (*nb->nb_int) (op);\r
+    if (io == NULL)\r
+        return (unsigned long)-1;\r
+    if (!PyInt_Check(io)) {\r
+        if (PyLong_Check(io)) {\r
+            val = PyLong_AsUnsignedLongMask((PyObject *)io);\r
+            Py_DECREF(io);\r
+            if (PyErr_Occurred())\r
+                return (unsigned long)-1;\r
+            return val;\r
+        }\r
+        else\r
+        {\r
+            Py_DECREF(io);\r
+            PyErr_SetString(PyExc_TypeError,\r
+                        "__int__ method should return an integer");\r
+            return (unsigned long)-1;\r
+        }\r
+    }\r
+\r
+    val = PyInt_AS_LONG(io);\r
+    Py_DECREF(io);\r
+\r
+    return val;\r
+}\r
+\r
+#ifdef HAVE_LONG_LONG\r
+unsigned PY_LONG_LONG\r
+PyInt_AsUnsignedLongLongMask(register PyObject *op)\r
+{\r
+    PyNumberMethods *nb;\r
+    PyIntObject *io;\r
+    unsigned PY_LONG_LONG val;\r
+\r
+    if (op && PyInt_Check(op))\r
+        return PyInt_AS_LONG((PyIntObject*) op);\r
+    if (op && PyLong_Check(op))\r
+        return PyLong_AsUnsignedLongLongMask(op);\r
+\r
+    if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||\r
+        nb->nb_int == NULL) {\r
+        PyErr_SetString(PyExc_TypeError, "an integer is required");\r
+        return (unsigned PY_LONG_LONG)-1;\r
+    }\r
+\r
+    io = (PyIntObject*) (*nb->nb_int) (op);\r
+    if (io == NULL)\r
+        return (unsigned PY_LONG_LONG)-1;\r
+    if (!PyInt_Check(io)) {\r
+        if (PyLong_Check(io)) {\r
+            val = PyLong_AsUnsignedLongLongMask((PyObject *)io);\r
+            Py_DECREF(io);\r
+            if (PyErr_Occurred())\r
+                return (unsigned PY_LONG_LONG)-1;\r
+            return val;\r
+        }\r
+        else\r
+        {\r
+            Py_DECREF(io);\r
+            PyErr_SetString(PyExc_TypeError,\r
+                        "__int__ method should return an integer");\r
+            return (unsigned PY_LONG_LONG)-1;\r
+        }\r
+    }\r
+\r
+    val = PyInt_AS_LONG(io);\r
+    Py_DECREF(io);\r
+\r
+    return val;\r
+}\r
+#endif\r
+\r
+PyObject *\r
+PyInt_FromString(char *s, char **pend, int base)\r
+{\r
+    char *end;\r
+    long x;\r
+    Py_ssize_t slen;\r
+    PyObject *sobj, *srepr;\r
+\r
+    if ((base != 0 && base < 2) || base > 36) {\r
+        PyErr_SetString(PyExc_ValueError,\r
+                        "int() base must be >= 2 and <= 36");\r
+        return NULL;\r
+    }\r
+\r
+    while (*s && isspace(Py_CHARMASK(*s)))\r
+        s++;\r
+    errno = 0;\r
+    if (base == 0 && s[0] == '0') {\r
+        x = (long) PyOS_strtoul(s, &end, base);\r
+        if (x < 0)\r
+            return PyLong_FromString(s, pend, base);\r
+    }\r
+    else\r
+        x = PyOS_strtol(s, &end, base);\r
+    if (end == s || !isalnum(Py_CHARMASK(end[-1])))\r
+        goto bad;\r
+    while (*end && isspace(Py_CHARMASK(*end)))\r
+        end++;\r
+    if (*end != '\0') {\r
+  bad:\r
+        slen = strlen(s) < 200 ? strlen(s) : 200;\r
+        sobj = PyString_FromStringAndSize(s, slen);\r
+        if (sobj == NULL)\r
+            return NULL;\r
+        srepr = PyObject_Repr(sobj);\r
+        Py_DECREF(sobj);\r
+        if (srepr == NULL)\r
+            return NULL;\r
+        PyErr_Format(PyExc_ValueError,\r
+                     "invalid literal for int() with base %d: %s",\r
+                     base, PyString_AS_STRING(srepr));\r
+        Py_DECREF(srepr);\r
+        return NULL;\r
+    }\r
+    else if (errno != 0)\r
+        return PyLong_FromString(s, pend, base);\r
+    if (pend)\r
+        *pend = end;\r
+    return PyInt_FromLong(x);\r
+}\r
+\r
+#ifdef Py_USING_UNICODE\r
+PyObject *\r
+PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base)\r
+{\r
+    PyObject *result;\r
+    char *buffer = (char *)PyMem_MALLOC(length+1);\r
+\r
+    if (buffer == NULL)\r
+        return PyErr_NoMemory();\r
+\r
+    if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {\r
+        PyMem_FREE(buffer);\r
+        return NULL;\r
+    }\r
+    result = PyInt_FromString(buffer, NULL, base);\r
+    PyMem_FREE(buffer);\r
+    return result;\r
+}\r
+#endif\r
+\r
+/* Methods */\r
+\r
+/* Integers are seen as the "smallest" of all numeric types and thus\r
+   don't have any knowledge about conversion of other types to\r
+   integers. */\r
+\r
+#define CONVERT_TO_LONG(obj, lng)               \\r
+    if (PyInt_Check(obj)) {                     \\r
+        lng = PyInt_AS_LONG(obj);               \\r
+    }                                           \\r
+    else {                                      \\r
+        Py_INCREF(Py_NotImplemented);           \\r
+        return Py_NotImplemented;               \\r
+    }\r
+\r
+/* ARGSUSED */\r
+static int\r
+int_print(PyIntObject *v, FILE *fp, int flags)\r
+     /* flags -- not used but required by interface */\r
+{\r
+    long int_val = v->ob_ival;\r
+    Py_BEGIN_ALLOW_THREADS\r
+    fprintf(fp, "%ld", int_val);\r
+    Py_END_ALLOW_THREADS\r
+    return 0;\r
+}\r
+\r
+static int\r
+int_compare(PyIntObject *v, PyIntObject *w)\r
+{\r
+    register long i = v->ob_ival;\r
+    register long j = w->ob_ival;\r
+    return (i < j) ? -1 : (i > j) ? 1 : 0;\r
+}\r
+\r
+static long\r
+int_hash(PyIntObject *v)\r
+{\r
+    /* XXX If this is changed, you also need to change the way\r
+       Python's long, float and complex types are hashed. */\r
+    long x = v -> ob_ival;\r
+    if (x == -1)\r
+        x = -2;\r
+    return x;\r
+}\r
+\r
+static PyObject *\r
+int_add(PyIntObject *v, PyIntObject *w)\r
+{\r
+    register long a, b, x;\r
+    CONVERT_TO_LONG(v, a);\r
+    CONVERT_TO_LONG(w, b);\r
+    /* casts in the line below avoid undefined behaviour on overflow */\r
+    x = (long)((unsigned long)a + b);\r
+    if ((x^a) >= 0 || (x^b) >= 0)\r
+        return PyInt_FromLong(x);\r
+    return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);\r
+}\r
+\r
+static PyObject *\r
+int_sub(PyIntObject *v, PyIntObject *w)\r
+{\r
+    register long a, b, x;\r
+    CONVERT_TO_LONG(v, a);\r
+    CONVERT_TO_LONG(w, b);\r
+    /* casts in the line below avoid undefined behaviour on overflow */\r
+    x = (long)((unsigned long)a - b);\r
+    if ((x^a) >= 0 || (x^~b) >= 0)\r
+        return PyInt_FromLong(x);\r
+    return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,\r
+                                                 (PyObject *)w);\r
+}\r
+\r
+/*\r
+Integer overflow checking for * is painful:  Python tried a couple ways, but\r
+they didn't work on all platforms, or failed in endcases (a product of\r
+-sys.maxint-1 has been a particular pain).\r
+\r
+Here's another way:\r
+\r
+The native long product x*y is either exactly right or *way* off, being\r
+just the last n bits of the true product, where n is the number of bits\r
+in a long (the delivered product is the true product plus i*2**n for\r
+some integer i).\r
+\r
+The native double product (double)x * (double)y is subject to three\r
+rounding errors:  on a sizeof(long)==8 box, each cast to double can lose\r
+info, and even on a sizeof(long)==4 box, the multiplication can lose info.\r
+But, unlike the native long product, it's not in *range* trouble:  even\r
+if sizeof(long)==32 (256-bit longs), the product easily fits in the\r
+dynamic range of a double.  So the leading 50 (or so) bits of the double\r
+product are correct.\r
+\r
+We check these two ways against each other, and declare victory if they're\r
+approximately the same.  Else, because the native long product is the only\r
+one that can lose catastrophic amounts of information, it's the native long\r
+product that must have overflowed.\r
+*/\r
+\r
+static PyObject *\r
+int_mul(PyObject *v, PyObject *w)\r
+{\r
+    long a, b;\r
+    long longprod;                      /* a*b in native long arithmetic */\r
+    double doubled_longprod;            /* (double)longprod */\r
+    double doubleprod;                  /* (double)a * (double)b */\r
+\r
+    CONVERT_TO_LONG(v, a);\r
+    CONVERT_TO_LONG(w, b);\r
+    /* casts in the next line avoid undefined behaviour on overflow */\r
+    longprod = (long)((unsigned long)a * b);\r
+    doubleprod = (double)a * (double)b;\r
+    doubled_longprod = (double)longprod;\r
+\r
+    /* Fast path for normal case:  small multiplicands, and no info\r
+       is lost in either method. */\r
+    if (doubled_longprod == doubleprod)\r
+        return PyInt_FromLong(longprod);\r
+\r
+    /* Somebody somewhere lost info.  Close enough, or way off?  Note\r
+       that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).\r
+       The difference either is or isn't significant compared to the\r
+       true value (of which doubleprod is a good approximation).\r
+    */\r
+    {\r
+        const double diff = doubled_longprod - doubleprod;\r
+        const double absdiff = diff >= 0.0 ? diff : -diff;\r
+        const double absprod = doubleprod >= 0.0 ? doubleprod :\r
+                              -doubleprod;\r
+        /* absdiff/absprod <= 1/32 iff\r
+           32 * absdiff <= absprod -- 5 good bits is "close enough" */\r
+        if (32.0 * absdiff <= absprod)\r
+            return PyInt_FromLong(longprod);\r
+        else\r
+            return PyLong_Type.tp_as_number->nb_multiply(v, w);\r
+    }\r
+}\r
+\r
+/* Integer overflow checking for unary negation: on a 2's-complement\r
+ * box, -x overflows iff x is the most negative long.  In this case we\r
+ * get -x == x.  However, -x is undefined (by C) if x /is/ the most\r
+ * negative long (it's a signed overflow case), and some compilers care.\r
+ * So we cast x to unsigned long first.  However, then other compilers\r
+ * warn about applying unary minus to an unsigned operand.  Hence the\r
+ * weird "0-".\r
+ */\r
+#define UNARY_NEG_WOULD_OVERFLOW(x)     \\r
+    ((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x))\r
+\r
+/* Return type of i_divmod */\r
+enum divmod_result {\r
+    DIVMOD_OK,                  /* Correct result */\r
+    DIVMOD_OVERFLOW,            /* Overflow, try again using longs */\r
+    DIVMOD_ERROR                /* Exception raised */\r
+};\r
+\r
+static enum divmod_result\r
+i_divmod(register long x, register long y,\r
+         long *p_xdivy, long *p_xmody)\r
+{\r
+    long xdivy, xmody;\r
+\r
+    if (y == 0) {\r
+        PyErr_SetString(PyExc_ZeroDivisionError,\r
+                        "integer division or modulo by zero");\r
+        return DIVMOD_ERROR;\r
+    }\r
+    /* (-sys.maxint-1)/-1 is the only overflow case. */\r
+    if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x))\r
+        return DIVMOD_OVERFLOW;\r
+    xdivy = x / y;\r
+    /* xdiv*y can overflow on platforms where x/y gives floor(x/y)\r
+     * for x and y with differing signs. (This is unusual\r
+     * behaviour, and C99 prohibits it, but it's allowed by C89;\r
+     * for an example of overflow, take x = LONG_MIN, y = 5 or x =\r
+     * LONG_MAX, y = -5.)  However, x - xdivy*y is always\r
+     * representable as a long, since it lies strictly between\r
+     * -abs(y) and abs(y).  We add casts to avoid intermediate\r
+     * overflow.\r
+     */\r
+    xmody = (long)(x - (unsigned long)xdivy * y);\r
+    /* If the signs of x and y differ, and the remainder is non-0,\r
+     * C89 doesn't define whether xdivy is now the floor or the\r
+     * ceiling of the infinitely precise quotient.  We want the floor,\r
+     * and we have it iff the remainder's sign matches y's.\r
+     */\r
+    if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {\r
+        xmody += y;\r
+        --xdivy;\r
+        assert(xmody && ((y ^ xmody) >= 0));\r
+    }\r
+    *p_xdivy = xdivy;\r
+    *p_xmody = xmody;\r
+    return DIVMOD_OK;\r
+}\r
+\r
+static PyObject *\r
+int_div(PyIntObject *x, PyIntObject *y)\r
+{\r
+    long xi, yi;\r
+    long d, m;\r
+    CONVERT_TO_LONG(x, xi);\r
+    CONVERT_TO_LONG(y, yi);\r
+    switch (i_divmod(xi, yi, &d, &m)) {\r
+    case DIVMOD_OK:\r
+        return PyInt_FromLong(d);\r
+    case DIVMOD_OVERFLOW:\r
+        return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,\r
+                                                   (PyObject *)y);\r
+    default:\r
+        return NULL;\r
+    }\r
+}\r
+\r
+static PyObject *\r
+int_classic_div(PyIntObject *x, PyIntObject *y)\r
+{\r
+    long xi, yi;\r
+    long d, m;\r
+    CONVERT_TO_LONG(x, xi);\r
+    CONVERT_TO_LONG(y, yi);\r
+    if (Py_DivisionWarningFlag &&\r
+        PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)\r
+        return NULL;\r
+    switch (i_divmod(xi, yi, &d, &m)) {\r
+    case DIVMOD_OK:\r
+        return PyInt_FromLong(d);\r
+    case DIVMOD_OVERFLOW:\r
+        return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,\r
+                                                   (PyObject *)y);\r
+    default:\r
+        return NULL;\r
+    }\r
+}\r
+\r
+static PyObject *\r
+int_true_divide(PyIntObject *x, PyIntObject *y)\r
+{\r
+    long xi, yi;\r
+    /* If they aren't both ints, give someone else a chance.  In\r
+       particular, this lets int/long get handled by longs, which\r
+       underflows to 0 gracefully if the long is too big to convert\r
+       to float. */\r
+    CONVERT_TO_LONG(x, xi);\r
+    CONVERT_TO_LONG(y, yi);\r
+    if (yi == 0) {\r
+        PyErr_SetString(PyExc_ZeroDivisionError,\r
+                        "division by zero");\r
+        return NULL;\r
+    }\r
+    if (xi == 0)\r
+        return PyFloat_FromDouble(yi < 0 ? -0.0 : 0.0);\r
+\r
+#define WIDTH_OF_ULONG (CHAR_BIT*SIZEOF_LONG)\r
+#if DBL_MANT_DIG < WIDTH_OF_ULONG\r
+    if ((xi >= 0 ? 0UL + xi : 0UL - xi) >> DBL_MANT_DIG ||\r
+        (yi >= 0 ? 0UL + yi : 0UL - yi) >> DBL_MANT_DIG)\r
+        /* Large x or y.  Use long integer arithmetic. */\r
+        return PyLong_Type.tp_as_number->nb_true_divide(\r
+            (PyObject *)x, (PyObject *)y);\r
+    else\r
+#endif\r
+        /* Both ints can be exactly represented as doubles.  Do a\r
+           floating-point division. */\r
+        return PyFloat_FromDouble((double)xi / (double)yi);\r
+}\r
+\r
+static PyObject *\r
+int_mod(PyIntObject *x, PyIntObject *y)\r
+{\r
+    long xi, yi;\r
+    long d, m;\r
+    CONVERT_TO_LONG(x, xi);\r
+    CONVERT_TO_LONG(y, yi);\r
+    switch (i_divmod(xi, yi, &d, &m)) {\r
+    case DIVMOD_OK:\r
+        return PyInt_FromLong(m);\r
+    case DIVMOD_OVERFLOW:\r
+        return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,\r
+                                                      (PyObject *)y);\r
+    default:\r
+        return NULL;\r
+    }\r
+}\r
+\r
+static PyObject *\r
+int_divmod(PyIntObject *x, PyIntObject *y)\r
+{\r
+    long xi, yi;\r
+    long d, m;\r
+    CONVERT_TO_LONG(x, xi);\r
+    CONVERT_TO_LONG(y, yi);\r
+    switch (i_divmod(xi, yi, &d, &m)) {\r
+    case DIVMOD_OK:\r
+        return Py_BuildValue("(ll)", d, m);\r
+    case DIVMOD_OVERFLOW:\r
+        return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,\r
+                                                   (PyObject *)y);\r
+    default:\r
+        return NULL;\r
+    }\r
+}\r
+\r
+static PyObject *\r
+int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)\r
+{\r
+    register long iv, iw, iz=0, ix, temp, prev;\r
+    CONVERT_TO_LONG(v, iv);\r
+    CONVERT_TO_LONG(w, iw);\r
+    if (iw < 0) {\r
+        if ((PyObject *)z != Py_None) {\r
+            PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "\r
+                 "cannot be negative when 3rd argument specified");\r
+            return NULL;\r
+        }\r
+        /* Return a float.  This works because we know that\r
+           this calls float_pow() which converts its\r
+           arguments to double. */\r
+        return PyFloat_Type.tp_as_number->nb_power(\r
+            (PyObject *)v, (PyObject *)w, (PyObject *)z);\r
+    }\r
+    if ((PyObject *)z != Py_None) {\r
+        CONVERT_TO_LONG(z, iz);\r
+        if (iz == 0) {\r
+            PyErr_SetString(PyExc_ValueError,\r
+                            "pow() 3rd argument cannot be 0");\r
+            return NULL;\r
+        }\r
+    }\r
+    /*\r
+     * XXX: The original exponentiation code stopped looping\r
+     * when temp hit zero; this code will continue onwards\r
+     * unnecessarily, but at least it won't cause any errors.\r
+     * Hopefully the speed improvement from the fast exponentiation\r
+     * will compensate for the slight inefficiency.\r
+     * XXX: Better handling of overflows is desperately needed.\r
+     */\r
+    temp = iv;\r
+    ix = 1;\r
+    while (iw > 0) {\r
+        prev = ix;              /* Save value for overflow check */\r
+        if (iw & 1) {\r
+            /*\r
+             * The (unsigned long) cast below ensures that the multiplication\r
+             * is interpreted as an unsigned operation rather than a signed one\r
+             * (C99 6.3.1.8p1), thus avoiding the perils of undefined behaviour\r
+             * from signed arithmetic overflow (C99 6.5p5).  See issue #12973.\r
+             */\r
+            ix = (unsigned long)ix * temp;\r
+            if (temp == 0)\r
+                break; /* Avoid ix / 0 */\r
+            if (ix / temp != prev) {\r
+                return PyLong_Type.tp_as_number->nb_power(\r
+                    (PyObject *)v,\r
+                    (PyObject *)w,\r
+                    (PyObject *)z);\r
+            }\r
+        }\r
+        iw >>= 1;               /* Shift exponent down by 1 bit */\r
+        if (iw==0) break;\r
+        prev = temp;\r
+        temp = (unsigned long)temp * temp;  /* Square the value of temp */\r
+        if (prev != 0 && temp / prev != prev) {\r
+            return PyLong_Type.tp_as_number->nb_power(\r
+                (PyObject *)v, (PyObject *)w, (PyObject *)z);\r
+        }\r
+        if (iz) {\r
+            /* If we did a multiplication, perform a modulo */\r
+            ix = ix % iz;\r
+            temp = temp % iz;\r
+        }\r
+    }\r
+    if (iz) {\r
+        long div, mod;\r
+        switch (i_divmod(ix, iz, &div, &mod)) {\r
+        case DIVMOD_OK:\r
+            ix = mod;\r
+            break;\r
+        case DIVMOD_OVERFLOW:\r
+            return PyLong_Type.tp_as_number->nb_power(\r
+                (PyObject *)v, (PyObject *)w, (PyObject *)z);\r
+        default:\r
+            return NULL;\r
+        }\r
+    }\r
+    return PyInt_FromLong(ix);\r
+}\r
+\r
+static PyObject *\r
+int_neg(PyIntObject *v)\r
+{\r
+    register long a;\r
+    a = v->ob_ival;\r
+    /* check for overflow */\r
+    if (UNARY_NEG_WOULD_OVERFLOW(a)) {\r
+        PyObject *o = PyLong_FromLong(a);\r
+        if (o != NULL) {\r
+            PyObject *result = PyNumber_Negative(o);\r
+            Py_DECREF(o);\r
+            return result;\r
+        }\r
+        return NULL;\r
+    }\r
+    return PyInt_FromLong(-a);\r
+}\r
+\r
+static PyObject *\r
+int_abs(PyIntObject *v)\r
+{\r
+    if (v->ob_ival >= 0)\r
+        return int_int(v);\r
+    else\r
+        return int_neg(v);\r
+}\r
+\r
+static int\r
+int_nonzero(PyIntObject *v)\r
+{\r
+    return v->ob_ival != 0;\r
+}\r
+\r
+static PyObject *\r
+int_invert(PyIntObject *v)\r
+{\r
+    return PyInt_FromLong(~v->ob_ival);\r
+}\r
+\r
+static PyObject *\r
+int_lshift(PyIntObject *v, PyIntObject *w)\r
+{\r
+    long a, b, c;\r
+    PyObject *vv, *ww, *result;\r
+\r
+    CONVERT_TO_LONG(v, a);\r
+    CONVERT_TO_LONG(w, b);\r
+    if (b < 0) {\r
+        PyErr_SetString(PyExc_ValueError, "negative shift count");\r
+        return NULL;\r
+    }\r
+    if (a == 0 || b == 0)\r
+        return int_int(v);\r
+    if (b >= LONG_BIT) {\r
+        vv = PyLong_FromLong(PyInt_AS_LONG(v));\r
+        if (vv == NULL)\r
+            return NULL;\r
+        ww = PyLong_FromLong(PyInt_AS_LONG(w));\r
+        if (ww == NULL) {\r
+            Py_DECREF(vv);\r
+            return NULL;\r
+        }\r
+        result = PyNumber_Lshift(vv, ww);\r
+        Py_DECREF(vv);\r
+        Py_DECREF(ww);\r
+        return result;\r
+    }\r
+    c = a << b;\r
+    if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {\r
+        vv = PyLong_FromLong(PyInt_AS_LONG(v));\r
+        if (vv == NULL)\r
+            return NULL;\r
+        ww = PyLong_FromLong(PyInt_AS_LONG(w));\r
+        if (ww == NULL) {\r
+            Py_DECREF(vv);\r
+            return NULL;\r
+        }\r
+        result = PyNumber_Lshift(vv, ww);\r
+        Py_DECREF(vv);\r
+        Py_DECREF(ww);\r
+        return result;\r
+    }\r
+    return PyInt_FromLong(c);\r
+}\r
+\r
+static PyObject *\r
+int_rshift(PyIntObject *v, PyIntObject *w)\r
+{\r
+    register long a, b;\r
+    CONVERT_TO_LONG(v, a);\r
+    CONVERT_TO_LONG(w, b);\r
+    if (b < 0) {\r
+        PyErr_SetString(PyExc_ValueError, "negative shift count");\r
+        return NULL;\r
+    }\r
+    if (a == 0 || b == 0)\r
+        return int_int(v);\r
+    if (b >= LONG_BIT) {\r
+        if (a < 0)\r
+            a = -1;\r
+        else\r
+            a = 0;\r
+    }\r
+    else {\r
+        a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);\r
+    }\r
+    return PyInt_FromLong(a);\r
+}\r
+\r
+static PyObject *\r
+int_and(PyIntObject *v, PyIntObject *w)\r
+{\r
+    register long a, b;\r
+    CONVERT_TO_LONG(v, a);\r
+    CONVERT_TO_LONG(w, b);\r
+    return PyInt_FromLong(a & b);\r
+}\r
+\r
+static PyObject *\r
+int_xor(PyIntObject *v, PyIntObject *w)\r
+{\r
+    register long a, b;\r
+    CONVERT_TO_LONG(v, a);\r
+    CONVERT_TO_LONG(w, b);\r
+    return PyInt_FromLong(a ^ b);\r
+}\r
+\r
+static PyObject *\r
+int_or(PyIntObject *v, PyIntObject *w)\r
+{\r
+    register long a, b;\r
+    CONVERT_TO_LONG(v, a);\r
+    CONVERT_TO_LONG(w, b);\r
+    return PyInt_FromLong(a | b);\r
+}\r
+\r
+static int\r
+int_coerce(PyObject **pv, PyObject **pw)\r
+{\r
+    if (PyInt_Check(*pw)) {\r
+        Py_INCREF(*pv);\r
+        Py_INCREF(*pw);\r
+        return 0;\r
+    }\r
+    return 1; /* Can't do it */\r
+}\r
+\r
+static PyObject *\r
+int_int(PyIntObject *v)\r
+{\r
+    if (PyInt_CheckExact(v))\r
+        Py_INCREF(v);\r
+    else\r
+        v = (PyIntObject *)PyInt_FromLong(v->ob_ival);\r
+    return (PyObject *)v;\r
+}\r
+\r
+static PyObject *\r
+int_long(PyIntObject *v)\r
+{\r
+    return PyLong_FromLong((v -> ob_ival));\r
+}\r
+\r
+static const unsigned char BitLengthTable[32] = {\r
+    0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,\r
+    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5\r
+};\r
+\r
+static int\r
+bits_in_ulong(unsigned long d)\r
+{\r
+    int d_bits = 0;\r
+    while (d >= 32) {\r
+        d_bits += 6;\r
+        d >>= 6;\r
+    }\r
+    d_bits += (int)BitLengthTable[d];\r
+    return d_bits;\r
+}\r
+\r
+#if 8*SIZEOF_LONG-1 <= DBL_MANT_DIG\r
+/* Every Python int can be exactly represented as a float. */\r
+\r
+static PyObject *\r
+int_float(PyIntObject *v)\r
+{\r
+    return PyFloat_FromDouble((double)(v -> ob_ival));\r
+}\r
+\r
+#else\r
+/* Here not all Python ints are exactly representable as floats, so we may\r
+   have to round.  We do this manually, since the C standards don't specify\r
+   whether converting an integer to a float rounds up or down */\r
+\r
+static PyObject *\r
+int_float(PyIntObject *v)\r
+{\r
+    unsigned long abs_ival, lsb;\r
+    int round_up;\r
+\r
+    if (v->ob_ival < 0)\r
+        abs_ival = 0U-(unsigned long)v->ob_ival;\r
+    else\r
+        abs_ival = (unsigned long)v->ob_ival;\r
+    if (abs_ival < (1L << DBL_MANT_DIG))\r
+        /* small integer;  no need to round */\r
+        return PyFloat_FromDouble((double)v->ob_ival);\r
+\r
+    /* Round abs_ival to MANT_DIG significant bits, using the\r
+       round-half-to-even rule.  abs_ival & lsb picks out the 'rounding'\r
+       bit: the first bit after the most significant MANT_DIG bits of\r
+       abs_ival.  We round up if this bit is set, provided that either:\r
+\r
+         (1) abs_ival isn't exactly halfway between two floats, in which\r
+         case at least one of the bits following the rounding bit must be\r
+         set; i.e., abs_ival & lsb-1 != 0, or:\r
+\r
+         (2) the resulting rounded value has least significant bit 0; or\r
+         in other words the bit above the rounding bit is set (this is the\r
+         'to-even' bit of round-half-to-even); i.e., abs_ival & 2*lsb != 0\r
+\r
+       The condition "(1) or (2)" equates to abs_ival & 3*lsb-1 != 0. */\r
+\r
+    lsb = 1L << (bits_in_ulong(abs_ival)-DBL_MANT_DIG-1);\r
+    round_up = (abs_ival & lsb) && (abs_ival & (3*lsb-1));\r
+    abs_ival &= -2*lsb;\r
+    if (round_up)\r
+        abs_ival += 2*lsb;\r
+    return PyFloat_FromDouble(v->ob_ival < 0 ?\r
+                              -(double)abs_ival :\r
+                  (double)abs_ival);\r
+}\r
+\r
+#endif\r
+\r
+static PyObject *\r
+int_oct(PyIntObject *v)\r
+{\r
+    return _PyInt_Format(v, 8, 0);\r
+}\r
+\r
+static PyObject *\r
+int_hex(PyIntObject *v)\r
+{\r
+    return _PyInt_Format(v, 16, 0);\r
+}\r
+\r
+static PyObject *\r
+int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);\r
+\r
+static PyObject *\r
+int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
+{\r
+    PyObject *x = NULL;\r
+    int base = -909;\r
+    static char *kwlist[] = {"x", "base", 0};\r
+\r
+    if (type != &PyInt_Type)\r
+        return int_subtype_new(type, args, kwds); /* Wimp out */\r
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,\r
+                                     &x, &base))\r
+        return NULL;\r
+    if (x == NULL) {\r
+        if (base != -909) {\r
+            PyErr_SetString(PyExc_TypeError,\r
+                            "int() missing string argument");\r
+            return NULL;\r
+        }\r
+        return PyInt_FromLong(0L);\r
+    }\r
+    if (base == -909)\r
+        return PyNumber_Int(x);\r
+    if (PyString_Check(x)) {\r
+        /* Since PyInt_FromString doesn't have a length parameter,\r
+         * check here for possible NULs in the string. */\r
+        char *string = PyString_AS_STRING(x);\r
+        if (strlen(string) != PyString_Size(x)) {\r
+            /* create a repr() of the input string,\r
+             * just like PyInt_FromString does */\r
+            PyObject *srepr;\r
+            srepr = PyObject_Repr(x);\r
+            if (srepr == NULL)\r
+                return NULL;\r
+            PyErr_Format(PyExc_ValueError,\r
+                 "invalid literal for int() with base %d: %s",\r
+                 base, PyString_AS_STRING(srepr));\r
+            Py_DECREF(srepr);\r
+            return NULL;\r
+        }\r
+        return PyInt_FromString(string, NULL, base);\r
+    }\r
+#ifdef Py_USING_UNICODE\r
+    if (PyUnicode_Check(x))\r
+        return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),\r
+                                 PyUnicode_GET_SIZE(x),\r
+                                 base);\r
+#endif\r
+    PyErr_SetString(PyExc_TypeError,\r
+                    "int() can't convert non-string with explicit base");\r
+    return NULL;\r
+}\r
+\r
+/* Wimpy, slow approach to tp_new calls for subtypes of int:\r
+   first create a regular int from whatever arguments we got,\r
+   then allocate a subtype instance and initialize its ob_ival\r
+   from the regular int.  The regular int is then thrown away.\r
+*/\r
+static PyObject *\r
+int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
+{\r
+    PyObject *tmp, *newobj;\r
+    long ival;\r
+\r
+    assert(PyType_IsSubtype(type, &PyInt_Type));\r
+    tmp = int_new(&PyInt_Type, args, kwds);\r
+    if (tmp == NULL)\r
+        return NULL;\r
+    if (!PyInt_Check(tmp)) {\r
+        ival = PyLong_AsLong(tmp);\r
+        if (ival == -1 && PyErr_Occurred()) {\r
+            Py_DECREF(tmp);\r
+            return NULL;\r
+        }\r
+    } else {\r
+        ival = ((PyIntObject *)tmp)->ob_ival;\r
+    }\r
+\r
+    newobj = type->tp_alloc(type, 0);\r
+    if (newobj == NULL) {\r
+        Py_DECREF(tmp);\r
+        return NULL;\r
+    }\r
+    ((PyIntObject *)newobj)->ob_ival = ival;\r
+    Py_DECREF(tmp);\r
+    return newobj;\r
+}\r
+\r
+static PyObject *\r
+int_getnewargs(PyIntObject *v)\r
+{\r
+    return Py_BuildValue("(l)", v->ob_ival);\r
+}\r
+\r
+static PyObject *\r
+int_get0(PyIntObject *v, void *context) {\r
+    return PyInt_FromLong(0L);\r
+}\r
+\r
+static PyObject *\r
+int_get1(PyIntObject *v, void *context) {\r
+    return PyInt_FromLong(1L);\r
+}\r
+\r
+/* Convert an integer to a decimal string.  On many platforms, this\r
+   will be significantly faster than the general arbitrary-base\r
+   conversion machinery in _PyInt_Format, thanks to optimization\r
+   opportunities offered by division by a compile-time constant. */\r
+static PyObject *\r
+int_to_decimal_string(PyIntObject *v) {\r
+    char buf[sizeof(long)*CHAR_BIT/3+6], *p, *bufend;\r
+    long n = v->ob_ival;\r
+    unsigned long absn;\r
+    p = bufend = buf + sizeof(buf);\r
+    absn = n < 0 ? 0UL - n : n;\r
+    do {\r
+        *--p = '0' + (char)(absn % 10);\r
+        absn /= 10;\r
+    } while (absn);\r
+    if (n < 0)\r
+        *--p = '-';\r
+    return PyString_FromStringAndSize(p, bufend - p);\r
+}\r
+\r
+/* Convert an integer to the given base.  Returns a string.\r
+   If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'.\r
+   If newstyle is zero, then use the pre-2.6 behavior of octal having\r
+   a leading "0" */\r
+PyAPI_FUNC(PyObject*)\r
+_PyInt_Format(PyIntObject *v, int base, int newstyle)\r
+{\r
+    /* There are no doubt many, many ways to optimize this, using code\r
+       similar to _PyLong_Format */\r
+    long n = v->ob_ival;\r
+    int  negative = n < 0;\r
+    int is_zero = n == 0;\r
+\r
+    /* For the reasoning behind this size, see\r
+       http://c-faq.com/misc/hexio.html. Then, add a few bytes for\r
+       the possible sign and prefix "0[box]" */\r
+    char buf[sizeof(n)*CHAR_BIT+6];\r
+\r
+    /* Start by pointing to the end of the buffer.  We fill in from\r
+       the back forward. */\r
+    char* p = &buf[sizeof(buf)];\r
+\r
+    assert(base >= 2 && base <= 36);\r
+\r
+    /* Special case base 10, for speed */\r
+    if (base == 10)\r
+        return int_to_decimal_string(v);\r
+\r
+    do {\r
+        /* I'd use i_divmod, except it doesn't produce the results\r
+           I want when n is negative.  So just duplicate the salient\r
+           part here. */\r
+        long div = n / base;\r
+        long mod = n - div * base;\r
+\r
+        /* convert abs(mod) to the right character in [0-9, a-z] */\r
+        char cdigit = (char)(mod < 0 ? -mod : mod);\r
+        cdigit += (cdigit < 10) ? '0' : 'a'-10;\r
+        *--p = cdigit;\r
+\r
+        n = div;\r
+    } while(n);\r
+\r
+    if (base == 2) {\r
+        *--p = 'b';\r
+        *--p = '0';\r
+    }\r
+    else if (base == 8) {\r
+        if (newstyle) {\r
+            *--p = 'o';\r
+            *--p = '0';\r
+        }\r
+        else\r
+            if (!is_zero)\r
+                *--p = '0';\r
+    }\r
+    else if (base == 16) {\r
+        *--p = 'x';\r
+        *--p = '0';\r
+    }\r
+    else {\r
+        *--p = '#';\r
+        *--p = '0' + base%10;\r
+        if (base > 10)\r
+            *--p = '0' + base/10;\r
+    }\r
+    if (negative)\r
+        *--p = '-';\r
+\r
+    return PyString_FromStringAndSize(p, &buf[sizeof(buf)] - p);\r
+}\r
+\r
+static PyObject *\r
+int__format__(PyObject *self, PyObject *args)\r
+{\r
+    PyObject *format_spec;\r
+\r
+    if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))\r
+        return NULL;\r
+    if (PyBytes_Check(format_spec))\r
+        return _PyInt_FormatAdvanced(self,\r
+                                     PyBytes_AS_STRING(format_spec),\r
+                                     PyBytes_GET_SIZE(format_spec));\r
+    if (PyUnicode_Check(format_spec)) {\r
+        /* Convert format_spec to a str */\r
+        PyObject *result;\r
+        PyObject *str_spec = PyObject_Str(format_spec);\r
+\r
+        if (str_spec == NULL)\r
+            return NULL;\r
+\r
+        result = _PyInt_FormatAdvanced(self,\r
+                                       PyBytes_AS_STRING(str_spec),\r
+                                       PyBytes_GET_SIZE(str_spec));\r
+\r
+        Py_DECREF(str_spec);\r
+        return result;\r
+    }\r
+    PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode");\r
+    return NULL;\r
+}\r
+\r
+static PyObject *\r
+int_bit_length(PyIntObject *v)\r
+{\r
+    unsigned long n;\r
+\r
+    if (v->ob_ival < 0)\r
+        /* avoid undefined behaviour when v->ob_ival == -LONG_MAX-1 */\r
+        n = 0U-(unsigned long)v->ob_ival;\r
+    else\r
+        n = (unsigned long)v->ob_ival;\r
+\r
+    return PyInt_FromLong(bits_in_ulong(n));\r
+}\r
+\r
+PyDoc_STRVAR(int_bit_length_doc,\r
+"int.bit_length() -> int\n\\r
+\n\\r
+Number of bits necessary to represent self in binary.\n\\r
+>>> bin(37)\n\\r
+'0b100101'\n\\r
+>>> (37).bit_length()\n\\r
+6");\r
+\r
+#if 0\r
+static PyObject *\r
+int_is_finite(PyObject *v)\r
+{\r
+    Py_RETURN_TRUE;\r
+}\r
+#endif\r
+\r
+static PyMethodDef int_methods[] = {\r
+    {"conjugate",       (PyCFunction)int_int,   METH_NOARGS,\r
+     "Returns self, the complex conjugate of any int."},\r
+    {"bit_length", (PyCFunction)int_bit_length, METH_NOARGS,\r
+     int_bit_length_doc},\r
+#if 0\r
+    {"is_finite",       (PyCFunction)int_is_finite,     METH_NOARGS,\r
+     "Returns always True."},\r
+#endif\r
+    {"__trunc__",       (PyCFunction)int_int,   METH_NOARGS,\r
+     "Truncating an Integral returns itself."},\r
+    {"__getnewargs__",          (PyCFunction)int_getnewargs,    METH_NOARGS},\r
+    {"__format__", (PyCFunction)int__format__, METH_VARARGS},\r
+    {NULL,              NULL}           /* sentinel */\r
+};\r
+\r
+static PyGetSetDef int_getset[] = {\r
+    {"real",\r
+     (getter)int_int, (setter)NULL,\r
+     "the real part of a complex number",\r
+     NULL},\r
+    {"imag",\r
+     (getter)int_get0, (setter)NULL,\r
+     "the imaginary part of a complex number",\r
+     NULL},\r
+    {"numerator",\r
+     (getter)int_int, (setter)NULL,\r
+     "the numerator of a rational number in lowest terms",\r
+     NULL},\r
+    {"denominator",\r
+     (getter)int_get1, (setter)NULL,\r
+     "the denominator of a rational number in lowest terms",\r
+     NULL},\r
+    {NULL}  /* Sentinel */\r
+};\r
+\r
+PyDoc_STRVAR(int_doc,\r
+"int(x=0) -> int or long\n\\r
+int(x, base=10) -> int or long\n\\r
+\n\\r
+Convert a number or string to an integer, or return 0 if no arguments\n\\r
+are given.  If x is floating point, the conversion truncates towards zero.\n\\r
+If x is outside the integer range, the function returns a long instead.\n\\r
+\n\\r
+If x is not a number or if base is given, then x must be a string or\n\\r
+Unicode object representing an integer literal in the given base.  The\n\\r
+literal can be preceded by '+' or '-' and be surrounded by whitespace.\n\\r
+The base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means to\n\\r
+interpret the base from the string as an integer literal.\n\\r
+>>> int('0b100', base=0)\n\\r
+4");\r
+\r
+static PyNumberMethods int_as_number = {\r
+    (binaryfunc)int_add,        /*nb_add*/\r
+    (binaryfunc)int_sub,        /*nb_subtract*/\r
+    (binaryfunc)int_mul,        /*nb_multiply*/\r
+    (binaryfunc)int_classic_div, /*nb_divide*/\r
+    (binaryfunc)int_mod,        /*nb_remainder*/\r
+    (binaryfunc)int_divmod,     /*nb_divmod*/\r
+    (ternaryfunc)int_pow,       /*nb_power*/\r
+    (unaryfunc)int_neg,         /*nb_negative*/\r
+    (unaryfunc)int_int,         /*nb_positive*/\r
+    (unaryfunc)int_abs,         /*nb_absolute*/\r
+    (inquiry)int_nonzero,       /*nb_nonzero*/\r
+    (unaryfunc)int_invert,      /*nb_invert*/\r
+    (binaryfunc)int_lshift,     /*nb_lshift*/\r
+    (binaryfunc)int_rshift,     /*nb_rshift*/\r
+    (binaryfunc)int_and,        /*nb_and*/\r
+    (binaryfunc)int_xor,        /*nb_xor*/\r
+    (binaryfunc)int_or,         /*nb_or*/\r
+    int_coerce,                 /*nb_coerce*/\r
+    (unaryfunc)int_int,         /*nb_int*/\r
+    (unaryfunc)int_long,        /*nb_long*/\r
+    (unaryfunc)int_float,       /*nb_float*/\r
+    (unaryfunc)int_oct,         /*nb_oct*/\r
+    (unaryfunc)int_hex,         /*nb_hex*/\r
+    0,                          /*nb_inplace_add*/\r
+    0,                          /*nb_inplace_subtract*/\r
+    0,                          /*nb_inplace_multiply*/\r
+    0,                          /*nb_inplace_divide*/\r
+    0,                          /*nb_inplace_remainder*/\r
+    0,                          /*nb_inplace_power*/\r
+    0,                          /*nb_inplace_lshift*/\r
+    0,                          /*nb_inplace_rshift*/\r
+    0,                          /*nb_inplace_and*/\r
+    0,                          /*nb_inplace_xor*/\r
+    0,                          /*nb_inplace_or*/\r
+    (binaryfunc)int_div,        /* nb_floor_divide */\r
+    (binaryfunc)int_true_divide, /* nb_true_divide */\r
+    0,                          /* nb_inplace_floor_divide */\r
+    0,                          /* nb_inplace_true_divide */\r
+    (unaryfunc)int_int,         /* nb_index */\r
+};\r
+\r
+PyTypeObject PyInt_Type = {\r
+    PyVarObject_HEAD_INIT(&PyType_Type, 0)\r
+    "int",\r
+    sizeof(PyIntObject),\r
+    0,\r
+    (destructor)int_dealloc,                    /* tp_dealloc */\r
+    (printfunc)int_print,                       /* tp_print */\r
+    0,                                          /* tp_getattr */\r
+    0,                                          /* tp_setattr */\r
+    (cmpfunc)int_compare,                       /* tp_compare */\r
+    (reprfunc)int_to_decimal_string,            /* tp_repr */\r
+    &int_as_number,                             /* tp_as_number */\r
+    0,                                          /* tp_as_sequence */\r
+    0,                                          /* tp_as_mapping */\r
+    (hashfunc)int_hash,                         /* tp_hash */\r
+    0,                                          /* tp_call */\r
+    (reprfunc)int_to_decimal_string,            /* tp_str */\r
+    PyObject_GenericGetAttr,                    /* tp_getattro */\r
+    0,                                          /* tp_setattro */\r
+    0,                                          /* tp_as_buffer */\r
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |\r
+        Py_TPFLAGS_BASETYPE | Py_TPFLAGS_INT_SUBCLASS,          /* tp_flags */\r
+    int_doc,                                    /* tp_doc */\r
+    0,                                          /* tp_traverse */\r
+    0,                                          /* tp_clear */\r
+    0,                                          /* tp_richcompare */\r
+    0,                                          /* tp_weaklistoffset */\r
+    0,                                          /* tp_iter */\r
+    0,                                          /* tp_iternext */\r
+    int_methods,                                /* tp_methods */\r
+    0,                                          /* tp_members */\r
+    int_getset,                                 /* tp_getset */\r
+    0,                                          /* tp_base */\r
+    0,                                          /* tp_dict */\r
+    0,                                          /* tp_descr_get */\r
+    0,                                          /* tp_descr_set */\r
+    0,                                          /* tp_dictoffset */\r
+    0,                                          /* tp_init */\r
+    0,                                          /* tp_alloc */\r
+    int_new,                                    /* tp_new */\r
+    (freefunc)int_free,                         /* tp_free */\r
+};\r
+\r
+int\r
+_PyInt_Init(void)\r
+{\r
+    PyIntObject *v;\r
+    int ival;\r
+#if NSMALLNEGINTS + NSMALLPOSINTS > 0\r
+    for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {\r
+          if (!free_list && (free_list = fill_free_list()) == NULL)\r
+                    return 0;\r
+        /* PyObject_New is inlined */\r
+        v = free_list;\r
+        free_list = (PyIntObject *)Py_TYPE(v);\r
+        PyObject_INIT(v, &PyInt_Type);\r
+        v->ob_ival = ival;\r
+        small_ints[ival + NSMALLNEGINTS] = v;\r
+    }\r
+#endif\r
+    return 1;\r
+}\r
+\r
+int\r
+PyInt_ClearFreeList(void)\r
+{\r
+    PyIntObject *p;\r
+    PyIntBlock *list, *next;\r
+    int i;\r
+    int u;                      /* remaining unfreed ints per block */\r
+    int freelist_size = 0;\r
+\r
+    list = block_list;\r
+    block_list = NULL;\r
+    free_list = NULL;\r
+    while (list != NULL) {\r
+        u = 0;\r
+        for (i = 0, p = &list->objects[0];\r
+             i < N_INTOBJECTS;\r
+             i++, p++) {\r
+            if (PyInt_CheckExact(p) && p->ob_refcnt != 0)\r
+                u++;\r
+        }\r
+        next = list->next;\r
+        if (u) {\r
+            list->next = block_list;\r
+            block_list = list;\r
+            for (i = 0, p = &list->objects[0];\r
+                 i < N_INTOBJECTS;\r
+                 i++, p++) {\r
+                if (!PyInt_CheckExact(p) ||\r
+                    p->ob_refcnt == 0) {\r
+                    Py_TYPE(p) = (struct _typeobject *)\r
+                        free_list;\r
+                    free_list = p;\r
+                }\r
+#if NSMALLNEGINTS + NSMALLPOSINTS > 0\r
+                else if (-NSMALLNEGINTS <= p->ob_ival &&\r
+                         p->ob_ival < NSMALLPOSINTS &&\r
+                         small_ints[p->ob_ival +\r
+                                    NSMALLNEGINTS] == NULL) {\r
+                    Py_INCREF(p);\r
+                    small_ints[p->ob_ival +\r
+                               NSMALLNEGINTS] = p;\r
+                }\r
+#endif\r
+            }\r
+        }\r
+        else {\r
+            PyMem_FREE(list);\r
+        }\r
+        freelist_size += u;\r
+        list = next;\r
+    }\r
+\r
+    return freelist_size;\r
+}\r
+\r
+void\r
+PyInt_Fini(void)\r
+{\r
+    PyIntObject *p;\r
+    PyIntBlock *list;\r
+    int i;\r
+    int u;                      /* total unfreed ints per block */\r
+\r
+#if NSMALLNEGINTS + NSMALLPOSINTS > 0\r
+    PyIntObject **q;\r
+\r
+    i = NSMALLNEGINTS + NSMALLPOSINTS;\r
+    q = small_ints;\r
+    while (--i >= 0) {\r
+        Py_XDECREF(*q);\r
+        *q++ = NULL;\r
+    }\r
+#endif\r
+    u = PyInt_ClearFreeList();\r
+    if (!Py_VerboseFlag)\r
+        return;\r
+    fprintf(stderr, "# cleanup ints");\r
+    if (!u) {\r
+        fprintf(stderr, "\n");\r
+    }\r
+    else {\r
+        fprintf(stderr,\r
+            ": %d unfreed int%s\n",\r
+            u, u == 1 ? "" : "s");\r
+    }\r
+    if (Py_VerboseFlag > 1) {\r
+        list = block_list;\r
+        while (list != NULL) {\r
+            for (i = 0, p = &list->objects[0];\r
+                 i < N_INTOBJECTS;\r
+                 i++, p++) {\r
+                if (PyInt_CheckExact(p) && p->ob_refcnt != 0)\r
+                    /* XXX(twouters) cast refcount to\r
+                       long until %zd is universally\r
+                       available\r
+                     */\r
+                    fprintf(stderr,\r
+                "#   <int at %p, refcnt=%ld, val=%ld>\n",\r
+                                p, (long)p->ob_refcnt,\r
+                                p->ob_ival);\r
+            }\r
+            list = list->next;\r
+        }\r
+    }\r
+}\r