2 /* Integer object implementation */
10 return LONG_MAX
; /* To initialize sys.maxint */
13 /* Integers are quite normal objects, to make object handling uniform.
14 (Using odd pointers to represent integers would save much space
15 but require extra checks for this special case throughout the code.)
16 Since a typical Python program spends much of its time allocating
17 and deallocating integers, these operations should be very fast.
18 Therefore we use a dedicated allocation scheme with a much lower
19 overhead (in space and time) than straight malloc(): a simple
20 dedicated free list, filled when necessary with memory from malloc().
22 block_list is a singly-linked list of all PyIntBlocks ever allocated,
23 linked via their next members. PyIntBlocks are never returned to the
24 system before shutdown (PyInt_Fini).
26 free_list is a singly-linked list of available PyIntObjects, linked
27 via abuse of their ob_type members.
30 #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
31 #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
32 #define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
35 struct _intblock
*next
;
36 PyIntObject objects
[N_INTOBJECTS
];
39 typedef struct _intblock PyIntBlock
;
41 static PyIntBlock
*block_list
= NULL
;
42 static PyIntObject
*free_list
= NULL
;
48 /* Python's object allocator isn't appropriate for large blocks. */
49 p
= (PyIntObject
*) PyMem_MALLOC(sizeof(PyIntBlock
));
51 return (PyIntObject
*) PyErr_NoMemory();
52 ((PyIntBlock
*)p
)->next
= block_list
;
53 block_list
= (PyIntBlock
*)p
;
54 /* Link the int objects together, from rear to front, then return
55 the address of the last int object in the block. */
56 p
= &((PyIntBlock
*)p
)->objects
[0];
59 q
->ob_type
= (struct _typeobject
*)(q
-1);
61 return p
+ N_INTOBJECTS
- 1;
65 #define NSMALLPOSINTS 100
68 #define NSMALLNEGINTS 5
70 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
71 /* References to small integers are saved in this array so that they
73 The integers that are saved are those in the range
74 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
76 static PyIntObject
*small_ints
[NSMALLNEGINTS
+ NSMALLPOSINTS
];
79 int quick_int_allocs
, quick_neg_int_allocs
;
83 PyInt_FromLong(long ival
)
85 register PyIntObject
*v
;
86 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
87 if (-NSMALLNEGINTS
<= ival
&& ival
< NSMALLPOSINTS
) {
88 v
= small_ints
[ival
+ NSMALLNEGINTS
];
94 quick_neg_int_allocs
++;
96 return (PyObject
*) v
;
99 if (free_list
== NULL
) {
100 if ((free_list
= fill_free_list()) == NULL
)
103 /* Inline PyObject_New */
105 free_list
= (PyIntObject
*)v
->ob_type
;
106 PyObject_INIT(v
, &PyInt_Type
);
108 return (PyObject
*) v
;
112 int_dealloc(PyIntObject
*v
)
114 if (PyInt_CheckExact(v
)) {
115 v
->ob_type
= (struct _typeobject
*)free_list
;
119 v
->ob_type
->tp_free((PyObject
*)v
);
123 int_free(PyIntObject
*v
)
125 v
->ob_type
= (struct _typeobject
*)free_list
;
130 PyInt_AsLong(register PyObject
*op
)
136 if (op
&& PyInt_Check(op
))
137 return PyInt_AS_LONG((PyIntObject
*) op
);
139 if (op
== NULL
|| (nb
= op
->ob_type
->tp_as_number
) == NULL
||
140 nb
->nb_int
== NULL
) {
141 PyErr_SetString(PyExc_TypeError
, "an integer is required");
145 io
= (PyIntObject
*) (*nb
->nb_int
) (op
);
148 if (!PyInt_Check(io
)) {
149 if (PyLong_Check(io
)) {
150 /* got a long? => retry int conversion */
151 val
= PyLong_AsLong((PyObject
*)io
);
153 if ((val
== -1) && PyErr_Occurred())
160 PyErr_SetString(PyExc_TypeError
,
161 "nb_int should return int object");
166 val
= PyInt_AS_LONG(io
);
173 PyInt_AsUnsignedLongMask(register PyObject
*op
)
179 if (op
&& PyInt_Check(op
))
180 return PyInt_AS_LONG((PyIntObject
*) op
);
181 if (op
&& PyLong_Check(op
))
182 return PyLong_AsUnsignedLongMask(op
);
184 if (op
== NULL
|| (nb
= op
->ob_type
->tp_as_number
) == NULL
||
185 nb
->nb_int
== NULL
) {
186 PyErr_SetString(PyExc_TypeError
, "an integer is required");
190 io
= (PyIntObject
*) (*nb
->nb_int
) (op
);
193 if (!PyInt_Check(io
)) {
194 if (PyLong_Check(io
)) {
195 val
= PyLong_AsUnsignedLongMask((PyObject
*)io
);
197 if (PyErr_Occurred())
204 PyErr_SetString(PyExc_TypeError
,
205 "nb_int should return int object");
210 val
= PyInt_AS_LONG(io
);
216 #ifdef HAVE_LONG_LONG
217 unsigned PY_LONG_LONG
218 PyInt_AsUnsignedLongLongMask(register PyObject
*op
)
222 unsigned PY_LONG_LONG val
;
224 if (op
&& PyInt_Check(op
))
225 return PyInt_AS_LONG((PyIntObject
*) op
);
226 if (op
&& PyLong_Check(op
))
227 return PyLong_AsUnsignedLongLongMask(op
);
229 if (op
== NULL
|| (nb
= op
->ob_type
->tp_as_number
) == NULL
||
230 nb
->nb_int
== NULL
) {
231 PyErr_SetString(PyExc_TypeError
, "an integer is required");
235 io
= (PyIntObject
*) (*nb
->nb_int
) (op
);
238 if (!PyInt_Check(io
)) {
239 if (PyLong_Check(io
)) {
240 val
= PyLong_AsUnsignedLongLongMask((PyObject
*)io
);
242 if (PyErr_Occurred())
249 PyErr_SetString(PyExc_TypeError
,
250 "nb_int should return int object");
255 val
= PyInt_AS_LONG(io
);
263 PyInt_FromString(char *s
, char **pend
, int base
)
267 char buffer
[256]; /* For errors */
269 if ((base
!= 0 && base
< 2) || base
> 36) {
270 PyErr_SetString(PyExc_ValueError
,
271 "int() base must be >= 2 and <= 36");
275 while (*s
&& isspace(Py_CHARMASK(*s
)))
278 if (base
== 0 && s
[0] == '0') {
279 x
= (long) PyOS_strtoul(s
, &end
, base
);
281 return PyLong_FromString(s
, pend
, base
);
284 x
= PyOS_strtol(s
, &end
, base
);
285 if (end
== s
|| !isalnum(Py_CHARMASK(end
[-1])))
287 while (*end
&& isspace(Py_CHARMASK(*end
)))
291 PyOS_snprintf(buffer
, sizeof(buffer
),
292 "invalid literal for int(): %.200s", s
);
293 PyErr_SetString(PyExc_ValueError
, buffer
);
297 return PyLong_FromString(s
, pend
, base
);
300 return PyInt_FromLong(x
);
303 #ifdef Py_USING_UNICODE
305 PyInt_FromUnicode(Py_UNICODE
*s
, int length
, int base
)
308 char *buffer
= PyMem_MALLOC(length
+1);
313 if (PyUnicode_EncodeDecimal(s
, length
, buffer
, NULL
)) {
317 result
= PyInt_FromString(buffer
, NULL
, base
);
325 /* Integers are seen as the "smallest" of all numeric types and thus
326 don't have any knowledge about conversion of other types to
329 #define CONVERT_TO_LONG(obj, lng) \
330 if (PyInt_Check(obj)) { \
331 lng = PyInt_AS_LONG(obj); \
334 Py_INCREF(Py_NotImplemented); \
335 return Py_NotImplemented; \
340 int_print(PyIntObject
*v
, FILE *fp
, int flags
)
341 /* flags -- not used but required by interface */
343 fprintf(fp
, "%ld", v
->ob_ival
);
348 int_repr(PyIntObject
*v
)
351 PyOS_snprintf(buf
, sizeof(buf
), "%ld", v
->ob_ival
);
352 return PyString_FromString(buf
);
356 int_compare(PyIntObject
*v
, PyIntObject
*w
)
358 register long i
= v
->ob_ival
;
359 register long j
= w
->ob_ival
;
360 return (i
< j
) ? -1 : (i
> j
) ? 1 : 0;
364 int_hash(PyIntObject
*v
)
366 /* XXX If this is changed, you also need to change the way
367 Python's long, float and complex types are hashed. */
368 long x
= v
-> ob_ival
;
375 int_add(PyIntObject
*v
, PyIntObject
*w
)
377 register long a
, b
, x
;
378 CONVERT_TO_LONG(v
, a
);
379 CONVERT_TO_LONG(w
, b
);
381 if ((x
^a
) >= 0 || (x
^b
) >= 0)
382 return PyInt_FromLong(x
);
383 return PyLong_Type
.tp_as_number
->nb_add((PyObject
*)v
, (PyObject
*)w
);
387 int_sub(PyIntObject
*v
, PyIntObject
*w
)
389 register long a
, b
, x
;
390 CONVERT_TO_LONG(v
, a
);
391 CONVERT_TO_LONG(w
, b
);
393 if ((x
^a
) >= 0 || (x
^~b
) >= 0)
394 return PyInt_FromLong(x
);
395 return PyLong_Type
.tp_as_number
->nb_subtract((PyObject
*)v
,
400 Integer overflow checking for * is painful: Python tried a couple ways, but
401 they didn't work on all platforms, or failed in endcases (a product of
402 -sys.maxint-1 has been a particular pain).
406 The native long product x*y is either exactly right or *way* off, being
407 just the last n bits of the true product, where n is the number of bits
408 in a long (the delivered product is the true product plus i*2**n for
411 The native double product (double)x * (double)y is subject to three
412 rounding errors: on a sizeof(long)==8 box, each cast to double can lose
413 info, and even on a sizeof(long)==4 box, the multiplication can lose info.
414 But, unlike the native long product, it's not in *range* trouble: even
415 if sizeof(long)==32 (256-bit longs), the product easily fits in the
416 dynamic range of a double. So the leading 50 (or so) bits of the double
419 We check these two ways against each other, and declare victory if they're
420 approximately the same. Else, because the native long product is the only
421 one that can lose catastrophic amounts of information, it's the native long
422 product that must have overflowed.
426 int_mul(PyObject
*v
, PyObject
*w
)
429 long longprod
; /* a*b in native long arithmetic */
430 double doubled_longprod
; /* (double)longprod */
431 double doubleprod
; /* (double)a * (double)b */
433 CONVERT_TO_LONG(v
, a
);
434 CONVERT_TO_LONG(w
, b
);
436 doubleprod
= (double)a
* (double)b
;
437 doubled_longprod
= (double)longprod
;
439 /* Fast path for normal case: small multiplicands, and no info
440 is lost in either method. */
441 if (doubled_longprod
== doubleprod
)
442 return PyInt_FromLong(longprod
);
444 /* Somebody somewhere lost info. Close enough, or way off? Note
445 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
446 The difference either is or isn't significant compared to the
447 true value (of which doubleprod is a good approximation).
450 const double diff
= doubled_longprod
- doubleprod
;
451 const double absdiff
= diff
>= 0.0 ? diff
: -diff
;
452 const double absprod
= doubleprod
>= 0.0 ? doubleprod
:
454 /* absdiff/absprod <= 1/32 iff
455 32 * absdiff <= absprod -- 5 good bits is "close enough" */
456 if (32.0 * absdiff
<= absprod
)
457 return PyInt_FromLong(longprod
);
459 return PyLong_Type
.tp_as_number
->nb_multiply(v
, w
);
463 /* Return type of i_divmod */
465 DIVMOD_OK
, /* Correct result */
466 DIVMOD_OVERFLOW
, /* Overflow, try again using longs */
467 DIVMOD_ERROR
/* Exception raised */
470 static enum divmod_result
471 i_divmod(register long x
, register long y
,
472 long *p_xdivy
, long *p_xmody
)
477 PyErr_SetString(PyExc_ZeroDivisionError
,
478 "integer division or modulo by zero");
481 /* (-sys.maxint-1)/-1 is the only overflow case. */
482 if (y
== -1 && x
< 0 && x
== -x
)
483 return DIVMOD_OVERFLOW
;
485 xmody
= x
- xdivy
* y
;
486 /* If the signs of x and y differ, and the remainder is non-0,
487 * C89 doesn't define whether xdivy is now the floor or the
488 * ceiling of the infinitely precise quotient. We want the floor,
489 * and we have it iff the remainder's sign matches y's.
491 if (xmody
&& ((y
^ xmody
) < 0) /* i.e. and signs differ */) {
494 assert(xmody
&& ((y
^ xmody
) >= 0));
502 int_div(PyIntObject
*x
, PyIntObject
*y
)
506 CONVERT_TO_LONG(x
, xi
);
507 CONVERT_TO_LONG(y
, yi
);
508 switch (i_divmod(xi
, yi
, &d
, &m
)) {
510 return PyInt_FromLong(d
);
511 case DIVMOD_OVERFLOW
:
512 return PyLong_Type
.tp_as_number
->nb_divide((PyObject
*)x
,
520 int_classic_div(PyIntObject
*x
, PyIntObject
*y
)
524 CONVERT_TO_LONG(x
, xi
);
525 CONVERT_TO_LONG(y
, yi
);
526 if (Py_DivisionWarningFlag
&&
527 PyErr_Warn(PyExc_DeprecationWarning
, "classic int division") < 0)
529 switch (i_divmod(xi
, yi
, &d
, &m
)) {
531 return PyInt_FromLong(d
);
532 case DIVMOD_OVERFLOW
:
533 return PyLong_Type
.tp_as_number
->nb_divide((PyObject
*)x
,
541 int_true_divide(PyObject
*v
, PyObject
*w
)
543 /* If they aren't both ints, give someone else a chance. In
544 particular, this lets int/long get handled by longs, which
545 underflows to 0 gracefully if the long is too big to convert
547 if (PyInt_Check(v
) && PyInt_Check(w
))
548 return PyFloat_Type
.tp_as_number
->nb_true_divide(v
, w
);
549 Py_INCREF(Py_NotImplemented
);
550 return Py_NotImplemented
;
554 int_mod(PyIntObject
*x
, PyIntObject
*y
)
558 CONVERT_TO_LONG(x
, xi
);
559 CONVERT_TO_LONG(y
, yi
);
560 switch (i_divmod(xi
, yi
, &d
, &m
)) {
562 return PyInt_FromLong(m
);
563 case DIVMOD_OVERFLOW
:
564 return PyLong_Type
.tp_as_number
->nb_remainder((PyObject
*)x
,
572 int_divmod(PyIntObject
*x
, PyIntObject
*y
)
576 CONVERT_TO_LONG(x
, xi
);
577 CONVERT_TO_LONG(y
, yi
);
578 switch (i_divmod(xi
, yi
, &d
, &m
)) {
580 return Py_BuildValue("(ll)", d
, m
);
581 case DIVMOD_OVERFLOW
:
582 return PyLong_Type
.tp_as_number
->nb_divmod((PyObject
*)x
,
590 int_pow(PyIntObject
*v
, PyIntObject
*w
, PyIntObject
*z
)
592 register long iv
, iw
, iz
=0, ix
, temp
, prev
;
593 CONVERT_TO_LONG(v
, iv
);
594 CONVERT_TO_LONG(w
, iw
);
596 if ((PyObject
*)z
!= Py_None
) {
597 PyErr_SetString(PyExc_TypeError
, "pow() 2nd argument "
598 "cannot be negative when 3rd argument specified");
601 /* Return a float. This works because we know that
602 this calls float_pow() which converts its
603 arguments to double. */
604 return PyFloat_Type
.tp_as_number
->nb_power(
605 (PyObject
*)v
, (PyObject
*)w
, (PyObject
*)z
);
607 if ((PyObject
*)z
!= Py_None
) {
608 CONVERT_TO_LONG(z
, iz
);
610 PyErr_SetString(PyExc_ValueError
,
611 "pow() 3rd argument cannot be 0");
616 * XXX: The original exponentiation code stopped looping
617 * when temp hit zero; this code will continue onwards
618 * unnecessarily, but at least it won't cause any errors.
619 * Hopefully the speed improvement from the fast exponentiation
620 * will compensate for the slight inefficiency.
621 * XXX: Better handling of overflows is desperately needed.
626 prev
= ix
; /* Save value for overflow check */
630 break; /* Avoid ix / 0 */
631 if (ix
/ temp
!= prev
) {
632 return PyLong_Type
.tp_as_number
->nb_power(
638 iw
>>= 1; /* Shift exponent down by 1 bit */
641 temp
*= temp
; /* Square the value of temp */
642 if (prev
!= 0 && temp
/ prev
!= prev
) {
643 return PyLong_Type
.tp_as_number
->nb_power(
644 (PyObject
*)v
, (PyObject
*)w
, (PyObject
*)z
);
647 /* If we did a multiplication, perform a modulo */
654 switch (i_divmod(ix
, iz
, &div
, &mod
)) {
658 case DIVMOD_OVERFLOW
:
659 return PyLong_Type
.tp_as_number
->nb_power(
660 (PyObject
*)v
, (PyObject
*)w
, (PyObject
*)z
);
665 return PyInt_FromLong(ix
);
669 int_neg(PyIntObject
*v
)
674 if (a
< 0 && x
< 0) {
675 PyObject
*o
= PyLong_FromLong(a
);
677 PyObject
*result
= PyNumber_Negative(o
);
683 return PyInt_FromLong(x
);
687 int_pos(PyIntObject
*v
)
689 if (PyInt_CheckExact(v
)) {
691 return (PyObject
*)v
;
694 return PyInt_FromLong(v
->ob_ival
);
698 int_abs(PyIntObject
*v
)
707 int_nonzero(PyIntObject
*v
)
709 return v
->ob_ival
!= 0;
713 int_invert(PyIntObject
*v
)
715 return PyInt_FromLong(~v
->ob_ival
);
719 int_lshift(PyIntObject
*v
, PyIntObject
*w
)
722 PyObject
*vv
, *ww
, *result
;
724 CONVERT_TO_LONG(v
, a
);
725 CONVERT_TO_LONG(w
, b
);
727 PyErr_SetString(PyExc_ValueError
, "negative shift count");
730 if (a
== 0 || b
== 0)
733 vv
= PyLong_FromLong(PyInt_AS_LONG(v
));
736 ww
= PyLong_FromLong(PyInt_AS_LONG(w
));
741 result
= PyNumber_Lshift(vv
, ww
);
747 if (a
!= Py_ARITHMETIC_RIGHT_SHIFT(long, c
, b
)) {
748 vv
= PyLong_FromLong(PyInt_AS_LONG(v
));
751 ww
= PyLong_FromLong(PyInt_AS_LONG(w
));
756 result
= PyNumber_Lshift(vv
, ww
);
761 return PyInt_FromLong(c
);
765 int_rshift(PyIntObject
*v
, PyIntObject
*w
)
768 CONVERT_TO_LONG(v
, a
);
769 CONVERT_TO_LONG(w
, b
);
771 PyErr_SetString(PyExc_ValueError
, "negative shift count");
774 if (a
== 0 || b
== 0)
783 a
= Py_ARITHMETIC_RIGHT_SHIFT(long, a
, b
);
785 return PyInt_FromLong(a
);
789 int_and(PyIntObject
*v
, PyIntObject
*w
)
792 CONVERT_TO_LONG(v
, a
);
793 CONVERT_TO_LONG(w
, b
);
794 return PyInt_FromLong(a
& b
);
798 int_xor(PyIntObject
*v
, PyIntObject
*w
)
801 CONVERT_TO_LONG(v
, a
);
802 CONVERT_TO_LONG(w
, b
);
803 return PyInt_FromLong(a
^ b
);
807 int_or(PyIntObject
*v
, PyIntObject
*w
)
810 CONVERT_TO_LONG(v
, a
);
811 CONVERT_TO_LONG(w
, b
);
812 return PyInt_FromLong(a
| b
);
816 int_coerce(PyObject
**pv
, PyObject
**pw
)
818 if (PyInt_Check(*pw
)) {
823 return 1; /* Can't do it */
827 int_int(PyIntObject
*v
)
829 if (PyInt_CheckExact(v
))
832 v
= (PyIntObject
*)PyInt_FromLong(v
->ob_ival
);
833 return (PyObject
*)v
;
837 int_long(PyIntObject
*v
)
839 return PyLong_FromLong((v
-> ob_ival
));
843 int_float(PyIntObject
*v
)
845 return PyFloat_FromDouble((double)(v
-> ob_ival
));
849 int_oct(PyIntObject
*v
)
852 long x
= v
-> ob_ival
;
854 PyOS_snprintf(buf
, sizeof(buf
), "-0%lo", -x
);
858 PyOS_snprintf(buf
, sizeof(buf
), "0%lo", x
);
859 return PyString_FromString(buf
);
863 int_hex(PyIntObject
*v
)
866 long x
= v
-> ob_ival
;
868 PyOS_snprintf(buf
, sizeof(buf
), "-0x%lx", -x
);
870 PyOS_snprintf(buf
, sizeof(buf
), "0x%lx", x
);
871 return PyString_FromString(buf
);
875 int_subtype_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
);
878 int_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
882 static const char *kwlist
[] = {"x", "base", 0};
884 if (type
!= &PyInt_Type
)
885 return int_subtype_new(type
, args
, kwds
); /* Wimp out */
886 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "|Oi:int", kwlist
,
890 return PyInt_FromLong(0L);
892 return PyNumber_Int(x
);
893 if (PyString_Check(x
))
894 return PyInt_FromString(PyString_AS_STRING(x
), NULL
, base
);
895 #ifdef Py_USING_UNICODE
896 if (PyUnicode_Check(x
))
897 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x
),
898 PyUnicode_GET_SIZE(x
),
901 PyErr_SetString(PyExc_TypeError
,
902 "int() can't convert non-string with explicit base");
906 /* Wimpy, slow approach to tp_new calls for subtypes of int:
907 first create a regular int from whatever arguments we got,
908 then allocate a subtype instance and initialize its ob_ival
909 from the regular int. The regular int is then thrown away.
912 int_subtype_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
917 assert(PyType_IsSubtype(type
, &PyInt_Type
));
918 tmp
= int_new(&PyInt_Type
, args
, kwds
);
921 if (!PyInt_Check(tmp
)) {
922 ival
= PyLong_AsLong(tmp
);
923 if (ival
== -1 && PyErr_Occurred()) {
928 ival
= ((PyIntObject
*)tmp
)->ob_ival
;
931 new = type
->tp_alloc(type
, 0);
936 ((PyIntObject
*)new)->ob_ival
= ival
;
942 int_getnewargs(PyIntObject
*v
)
944 return Py_BuildValue("(l)", v
->ob_ival
);
947 static PyMethodDef int_methods
[] = {
948 {"__getnewargs__", (PyCFunction
)int_getnewargs
, METH_NOARGS
},
949 {NULL
, NULL
} /* sentinel */
952 PyDoc_STRVAR(int_doc
,
953 "int(x[, base]) -> integer\n\
955 Convert a string or number to an integer, if possible. A floating point\n\
956 argument will be truncated towards zero (this does not include a string\n\
957 representation of a floating point number!) When converting a string, use\n\
958 the optional base. It is an error to supply a base when converting a\n\
959 non-string. If the argument is outside the integer range a long object\n\
960 will be returned instead.");
962 static PyNumberMethods int_as_number
= {
963 (binaryfunc
)int_add
, /*nb_add*/
964 (binaryfunc
)int_sub
, /*nb_subtract*/
965 (binaryfunc
)int_mul
, /*nb_multiply*/
966 (binaryfunc
)int_classic_div
, /*nb_divide*/
967 (binaryfunc
)int_mod
, /*nb_remainder*/
968 (binaryfunc
)int_divmod
, /*nb_divmod*/
969 (ternaryfunc
)int_pow
, /*nb_power*/
970 (unaryfunc
)int_neg
, /*nb_negative*/
971 (unaryfunc
)int_pos
, /*nb_positive*/
972 (unaryfunc
)int_abs
, /*nb_absolute*/
973 (inquiry
)int_nonzero
, /*nb_nonzero*/
974 (unaryfunc
)int_invert
, /*nb_invert*/
975 (binaryfunc
)int_lshift
, /*nb_lshift*/
976 (binaryfunc
)int_rshift
, /*nb_rshift*/
977 (binaryfunc
)int_and
, /*nb_and*/
978 (binaryfunc
)int_xor
, /*nb_xor*/
979 (binaryfunc
)int_or
, /*nb_or*/
980 int_coerce
, /*nb_coerce*/
981 (unaryfunc
)int_int
, /*nb_int*/
982 (unaryfunc
)int_long
, /*nb_long*/
983 (unaryfunc
)int_float
, /*nb_float*/
984 (unaryfunc
)int_oct
, /*nb_oct*/
985 (unaryfunc
)int_hex
, /*nb_hex*/
986 0, /*nb_inplace_add*/
987 0, /*nb_inplace_subtract*/
988 0, /*nb_inplace_multiply*/
989 0, /*nb_inplace_divide*/
990 0, /*nb_inplace_remainder*/
991 0, /*nb_inplace_power*/
992 0, /*nb_inplace_lshift*/
993 0, /*nb_inplace_rshift*/
994 0, /*nb_inplace_and*/
995 0, /*nb_inplace_xor*/
997 (binaryfunc
)int_div
, /* nb_floor_divide */
998 int_true_divide
, /* nb_true_divide */
999 0, /* nb_inplace_floor_divide */
1000 0, /* nb_inplace_true_divide */
1003 PyTypeObject PyInt_Type
= {
1004 PyObject_HEAD_INIT(&PyType_Type
)
1007 sizeof(PyIntObject
),
1009 (destructor
)int_dealloc
, /* tp_dealloc */
1010 (printfunc
)int_print
, /* tp_print */
1013 (cmpfunc
)int_compare
, /* tp_compare */
1014 (reprfunc
)int_repr
, /* tp_repr */
1015 &int_as_number
, /* tp_as_number */
1016 0, /* tp_as_sequence */
1017 0, /* tp_as_mapping */
1018 (hashfunc
)int_hash
, /* tp_hash */
1020 (reprfunc
)int_repr
, /* tp_str */
1021 PyObject_GenericGetAttr
, /* tp_getattro */
1022 0, /* tp_setattro */
1023 0, /* tp_as_buffer */
1024 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
|
1025 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1026 int_doc
, /* tp_doc */
1027 0, /* tp_traverse */
1029 0, /* tp_richcompare */
1030 0, /* tp_weaklistoffset */
1032 0, /* tp_iternext */
1033 int_methods
, /* tp_methods */
1038 0, /* tp_descr_get */
1039 0, /* tp_descr_set */
1040 0, /* tp_dictoffset */
1043 int_new
, /* tp_new */
1044 (freefunc
)int_free
, /* tp_free */
1052 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
1053 for (ival
= -NSMALLNEGINTS
; ival
< NSMALLPOSINTS
; ival
++) {
1054 if (!free_list
&& (free_list
= fill_free_list()) == NULL
)
1056 /* PyObject_New is inlined */
1058 free_list
= (PyIntObject
*)v
->ob_type
;
1059 PyObject_INIT(v
, &PyInt_Type
);
1061 small_ints
[ival
+ NSMALLNEGINTS
] = v
;
1071 PyIntBlock
*list
, *next
;
1073 int bc
, bf
; /* block count, number of freed blocks */
1074 int irem
, isum
; /* remaining unfreed ints per block, total */
1076 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
1079 i
= NSMALLNEGINTS
+ NSMALLPOSINTS
;
1092 while (list
!= NULL
) {
1095 for (i
= 0, p
= &list
->objects
[0];
1098 if (PyInt_CheckExact(p
) && p
->ob_refcnt
!= 0)
1103 list
->next
= block_list
;
1105 for (i
= 0, p
= &list
->objects
[0];
1108 if (!PyInt_CheckExact(p
) ||
1109 p
->ob_refcnt
== 0) {
1110 p
->ob_type
= (struct _typeobject
*)
1114 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
1115 else if (-NSMALLNEGINTS
<= p
->ob_ival
&&
1116 p
->ob_ival
< NSMALLPOSINTS
&&
1117 small_ints
[p
->ob_ival
+
1118 NSMALLNEGINTS
] == NULL
) {
1120 small_ints
[p
->ob_ival
+
1133 if (!Py_VerboseFlag
)
1135 fprintf(stderr
, "# cleanup ints");
1137 fprintf(stderr
, "\n");
1141 ": %d unfreed int%s in %d out of %d block%s\n",
1142 isum
, isum
== 1 ? "" : "s",
1143 bc
- bf
, bc
, bc
== 1 ? "" : "s");
1145 if (Py_VerboseFlag
> 1) {
1147 while (list
!= NULL
) {
1148 for (i
= 0, p
= &list
->objects
[0];
1151 if (PyInt_CheckExact(p
) && p
->ob_refcnt
!= 0)
1153 "# <int at %p, refcnt=%d, val=%ld>\n",
1154 p
, p
->ob_refcnt
, p
->ob_ival
);