2 /* Integer object implementation */
10 return LONG_MAX
; /* To initialize sys.maxint */
13 /* Standard Booleans */
15 PyIntObject _Py_ZeroStruct
= {
16 PyObject_HEAD_INIT(&PyInt_Type
)
20 PyIntObject _Py_TrueStruct
= {
21 PyObject_HEAD_INIT(&PyInt_Type
)
25 /* Return 1 if exception raised, 0 if caller should retry using longs */
29 if (PyErr_Warn(PyExc_OverflowWarning
, msg
) < 0) {
30 if (PyErr_ExceptionMatches(PyExc_OverflowWarning
))
31 PyErr_SetString(PyExc_OverflowError
, msg
);
38 /* Integers are quite normal objects, to make object handling uniform.
39 (Using odd pointers to represent integers would save much space
40 but require extra checks for this special case throughout the code.)
41 Since, a typical Python program spends much of its time allocating
42 and deallocating integers, these operations should be very fast.
43 Therefore we use a dedicated allocation scheme with a much lower
44 overhead (in space and time) than straight malloc(): a simple
45 dedicated free list, filled when necessary with memory from malloc().
48 #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
49 #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
50 #define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
53 struct _intblock
*next
;
54 PyIntObject objects
[N_INTOBJECTS
];
57 typedef struct _intblock PyIntBlock
;
59 static PyIntBlock
*block_list
= NULL
;
60 static PyIntObject
*free_list
= NULL
;
66 /* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */
67 p
= (PyIntObject
*) PyMem_MALLOC(sizeof(PyIntBlock
));
69 return (PyIntObject
*) PyErr_NoMemory();
70 ((PyIntBlock
*)p
)->next
= block_list
;
71 block_list
= (PyIntBlock
*)p
;
72 p
= &((PyIntBlock
*)p
)->objects
[0];
75 q
->ob_type
= (struct _typeobject
*)(q
-1);
77 return p
+ N_INTOBJECTS
- 1;
81 #define NSMALLPOSINTS 100
84 #define NSMALLNEGINTS 1
86 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
87 /* References to small integers are saved in this array so that they
89 The integers that are saved are those in the range
90 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
92 static PyIntObject
*small_ints
[NSMALLNEGINTS
+ NSMALLPOSINTS
];
95 int quick_int_allocs
, quick_neg_int_allocs
;
99 PyInt_FromLong(long ival
)
101 register PyIntObject
*v
;
102 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
103 if (-NSMALLNEGINTS
<= ival
&& ival
< NSMALLPOSINTS
&&
104 (v
= small_ints
[ival
+ NSMALLNEGINTS
]) != NULL
) {
110 quick_neg_int_allocs
++;
112 return (PyObject
*) v
;
115 if (free_list
== NULL
) {
116 if ((free_list
= fill_free_list()) == NULL
)
119 /* PyObject_New is inlined */
121 free_list
= (PyIntObject
*)v
->ob_type
;
122 PyObject_INIT(v
, &PyInt_Type
);
124 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
125 if (-NSMALLNEGINTS
<= ival
&& ival
< NSMALLPOSINTS
) {
126 /* save this one for a following allocation */
128 small_ints
[ival
+ NSMALLNEGINTS
] = v
;
131 return (PyObject
*) v
;
135 int_dealloc(PyIntObject
*v
)
137 if (PyInt_CheckExact(v
)) {
138 v
->ob_type
= (struct _typeobject
*)free_list
;
142 v
->ob_type
->tp_free((PyObject
*)v
);
146 PyInt_AsLong(register PyObject
*op
)
152 if (op
&& PyInt_Check(op
))
153 return PyInt_AS_LONG((PyIntObject
*) op
);
155 if (op
== NULL
|| (nb
= op
->ob_type
->tp_as_number
) == NULL
||
156 nb
->nb_int
== NULL
) {
157 PyErr_SetString(PyExc_TypeError
, "an integer is required");
161 io
= (PyIntObject
*) (*nb
->nb_int
) (op
);
164 if (!PyInt_Check(io
)) {
165 PyErr_SetString(PyExc_TypeError
,
166 "nb_int should return int object");
170 val
= PyInt_AS_LONG(io
);
177 PyInt_FromString(char *s
, char **pend
, int base
)
181 char buffer
[256]; /* For errors */
183 if ((base
!= 0 && base
< 2) || base
> 36) {
184 PyErr_SetString(PyExc_ValueError
, "int() base must be >= 2 and <= 36");
188 while (*s
&& isspace(Py_CHARMASK(*s
)))
191 if (base
== 0 && s
[0] == '0')
192 x
= (long) PyOS_strtoul(s
, &end
, base
);
194 x
= PyOS_strtol(s
, &end
, base
);
195 if (end
== s
|| !isalnum(Py_CHARMASK(end
[-1])))
197 while (*end
&& isspace(Py_CHARMASK(*end
)))
201 sprintf(buffer
, "invalid literal for int(): %.200s", s
);
202 PyErr_SetString(PyExc_ValueError
, buffer
);
205 else if (errno
!= 0) {
206 sprintf(buffer
, "int() literal too large: %.200s", s
);
207 PyErr_SetString(PyExc_ValueError
, buffer
);
212 return PyInt_FromLong(x
);
215 #ifdef Py_USING_UNICODE
217 PyInt_FromUnicode(Py_UNICODE
*s
, int length
, int base
)
221 if (length
>= sizeof(buffer
)) {
222 PyErr_SetString(PyExc_ValueError
,
223 "int() literal too large to convert");
226 if (PyUnicode_EncodeDecimal(s
, length
, buffer
, NULL
))
228 return PyInt_FromString(buffer
, NULL
, base
);
234 /* Integers are seen as the "smallest" of all numeric types and thus
235 don't have any knowledge about conversion of other types to
238 #define CONVERT_TO_LONG(obj, lng) \
239 if (PyInt_Check(obj)) { \
240 lng = PyInt_AS_LONG(obj); \
243 Py_INCREF(Py_NotImplemented); \
244 return Py_NotImplemented; \
249 int_print(PyIntObject
*v
, FILE *fp
, int flags
)
250 /* flags -- not used but required by interface */
252 fprintf(fp
, "%ld", v
->ob_ival
);
257 int_repr(PyIntObject
*v
)
260 sprintf(buf
, "%ld", v
->ob_ival
);
261 return PyString_FromString(buf
);
265 int_compare(PyIntObject
*v
, PyIntObject
*w
)
267 register long i
= v
->ob_ival
;
268 register long j
= w
->ob_ival
;
269 return (i
< j
) ? -1 : (i
> j
) ? 1 : 0;
273 int_hash(PyIntObject
*v
)
275 /* XXX If this is changed, you also need to change the way
276 Python's long, float and complex types are hashed. */
277 long x
= v
-> ob_ival
;
284 int_add(PyIntObject
*v
, PyIntObject
*w
)
286 register long a
, b
, x
;
287 CONVERT_TO_LONG(v
, a
);
288 CONVERT_TO_LONG(w
, b
);
290 if ((x
^a
) >= 0 || (x
^b
) >= 0)
291 return PyInt_FromLong(x
);
292 if (err_ovf("integer addition"))
294 return PyLong_Type
.tp_as_number
->nb_add((PyObject
*)v
, (PyObject
*)w
);
298 int_sub(PyIntObject
*v
, PyIntObject
*w
)
300 register long a
, b
, x
;
301 CONVERT_TO_LONG(v
, a
);
302 CONVERT_TO_LONG(w
, b
);
304 if ((x
^a
) >= 0 || (x
^~b
) >= 0)
305 return PyInt_FromLong(x
);
306 if (err_ovf("integer subtraction"))
308 return PyLong_Type
.tp_as_number
->nb_subtract((PyObject
*)v
,
313 Integer overflow checking used to be done using a double, but on 64
314 bit machines (where both long and double are 64 bit) this fails
315 because the double doesn't have enough precision. John Tromp suggests
316 the following algorithm:
318 Suppose again we normalize a and b to be nonnegative.
319 Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
320 Now we test ah and bh against zero and get essentially 3 possible outcomes.
322 1) both ah and bh > 0 : then report overflow
324 2) both ah and bh = 0 : then compute a*b and report overflow if it comes out
327 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
328 compute al*bl and report overflow if it's negative
329 add (ah*bl)<<32 to al*bl and report overflow if
332 In case of no overflow the result is then negated if necessary.
334 The majority of cases will be 2), in which case this method is the same as
335 what I suggested before. If multiplication is expensive enough, then the
336 other method is faster on case 3), but also more work to program, so I
337 guess the above is the preferred solution.
342 int_mul(PyObject
*v
, PyObject
*w
)
344 long a
, b
, ah
, bh
, x
, y
;
347 if (!PyInt_Check(v
) &&
348 v
->ob_type
->tp_as_sequence
&&
349 v
->ob_type
->tp_as_sequence
->sq_repeat
) {
352 return (*v
->ob_type
->tp_as_sequence
->sq_repeat
)(v
, a
);
354 if (!PyInt_Check(w
) &&
355 w
->ob_type
->tp_as_sequence
&&
356 w
->ob_type
->tp_as_sequence
->sq_repeat
) {
359 return (*w
->ob_type
->tp_as_sequence
->sq_repeat
)(w
, a
);
362 CONVERT_TO_LONG(v
, a
);
363 CONVERT_TO_LONG(w
, b
);
364 ah
= a
>> (LONG_BIT
/2);
365 bh
= b
>> (LONG_BIT
/2);
367 /* Quick test for common case: two small positive ints */
369 if (ah
== 0 && bh
== 0) {
373 return PyInt_FromLong(x
);
376 /* Arrange that a >= b >= 0 */
381 /* Largest negative */
382 if (b
== 0 || b
== 1) {
390 ah
= a
>> (LONG_BIT
/2);
395 /* Largest negative */
396 if (a
== 0 || (a
== 1 && s
== 1)) {
404 bh
= b
>> (LONG_BIT
/2);
407 /* 1) both ah and bh > 0 : then report overflow */
409 if (ah
!= 0 && bh
!= 0)
412 /* 2) both ah and bh = 0 : then compute a*b and report
413 overflow if it comes out negative */
415 if (ah
== 0 && bh
== 0) {
419 return PyInt_FromLong(x
*s
);
428 /* bh not used beyond this point */
431 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
433 compute al*bl and report overflow if it's negative
434 add (ah*bl)<<32 to al*bl and report overflow if
436 (NB b == bl in this case, and we make a = al) */
439 if (y
>= (1L << (LONG_BIT
/2 - 1)))
441 a
&= (1L << (LONG_BIT
/2)) - 1;
445 x
+= y
<< (LONG_BIT
/2);
449 return PyInt_FromLong(x
* s
);
452 if (err_ovf("integer multiplication"))
454 return PyLong_Type
.tp_as_number
->nb_multiply(v
, w
);
457 /* Return type of i_divmod */
459 DIVMOD_OK
, /* Correct result */
460 DIVMOD_OVERFLOW
, /* Overflow, try again using longs */
461 DIVMOD_ERROR
/* Exception raised */
464 static enum divmod_result
465 i_divmod(register long x
, register long y
,
466 long *p_xdivy
, long *p_xmody
)
471 PyErr_SetString(PyExc_ZeroDivisionError
,
472 "integer division or modulo by zero");
475 /* (-sys.maxint-1)/-1 is the only overflow case. */
476 if (y
== -1 && x
< 0 && x
== -x
) {
477 if (err_ovf("integer division"))
479 return DIVMOD_OVERFLOW
;
482 xmody
= x
- xdivy
* y
;
483 /* If the signs of x and y differ, and the remainder is non-0,
484 * C89 doesn't define whether xdivy is now the floor or the
485 * ceiling of the infinitely precise quotient. We want the floor,
486 * and we have it iff the remainder's sign matches y's.
488 if (xmody
&& ((y
^ xmody
) < 0) /* i.e. and signs differ */) {
491 assert(xmody
&& ((y
^ xmody
) >= 0));
499 int_div(PyIntObject
*x
, PyIntObject
*y
)
503 CONVERT_TO_LONG(x
, xi
);
504 CONVERT_TO_LONG(y
, yi
);
505 switch (i_divmod(xi
, yi
, &d
, &m
)) {
507 return PyInt_FromLong(d
);
508 case DIVMOD_OVERFLOW
:
509 return PyLong_Type
.tp_as_number
->nb_divide((PyObject
*)x
,
517 int_classic_div(PyIntObject
*x
, PyIntObject
*y
)
521 CONVERT_TO_LONG(x
, xi
);
522 CONVERT_TO_LONG(y
, yi
);
523 if (Py_DivisionWarningFlag
&&
524 PyErr_Warn(PyExc_DeprecationWarning
, "classic int division") < 0)
526 switch (i_divmod(xi
, yi
, &d
, &m
)) {
528 return PyInt_FromLong(d
);
529 case DIVMOD_OVERFLOW
:
530 return PyLong_Type
.tp_as_number
->nb_divide((PyObject
*)x
,
538 int_true_divide(PyObject
*v
, PyObject
*w
)
540 /* If they aren't both ints, give someone else a chance. In
541 particular, this lets int/long get handled by longs, which
542 underflows to 0 gracefully if the long is too big to convert
544 if (PyInt_Check(v
) && PyInt_Check(w
))
545 return PyFloat_Type
.tp_as_number
->nb_true_divide(v
, w
);
546 Py_INCREF(Py_NotImplemented
);
547 return Py_NotImplemented
;
551 int_mod(PyIntObject
*x
, PyIntObject
*y
)
555 CONVERT_TO_LONG(x
, xi
);
556 CONVERT_TO_LONG(y
, yi
);
557 switch (i_divmod(xi
, yi
, &d
, &m
)) {
559 return PyInt_FromLong(m
);
560 case DIVMOD_OVERFLOW
:
561 return PyLong_Type
.tp_as_number
->nb_remainder((PyObject
*)x
,
569 int_divmod(PyIntObject
*x
, PyIntObject
*y
)
573 CONVERT_TO_LONG(x
, xi
);
574 CONVERT_TO_LONG(y
, yi
);
575 switch (i_divmod(xi
, yi
, &d
, &m
)) {
577 return Py_BuildValue("(ll)", d
, m
);
578 case DIVMOD_OVERFLOW
:
579 return PyLong_Type
.tp_as_number
->nb_divmod((PyObject
*)x
,
587 int_pow(PyIntObject
*v
, PyIntObject
*w
, PyIntObject
*z
)
589 register long iv
, iw
, iz
=0, ix
, temp
, prev
;
590 CONVERT_TO_LONG(v
, iv
);
591 CONVERT_TO_LONG(w
, iw
);
593 if ((PyObject
*)z
!= Py_None
) {
594 PyErr_SetString(PyExc_TypeError
, "pow() 2nd argument "
595 "cannot be negative when 3rd argument specified");
598 /* Return a float. This works because we know that
599 this calls float_pow() which converts its
600 arguments to double. */
601 return PyFloat_Type
.tp_as_number
->nb_power(
602 (PyObject
*)v
, (PyObject
*)w
, (PyObject
*)z
);
604 if ((PyObject
*)z
!= Py_None
) {
605 CONVERT_TO_LONG(z
, iz
);
607 PyErr_SetString(PyExc_ValueError
,
608 "pow() 3rd argument cannot be 0");
613 * XXX: The original exponentiation code stopped looping
614 * when temp hit zero; this code will continue onwards
615 * unnecessarily, but at least it won't cause any errors.
616 * Hopefully the speed improvement from the fast exponentiation
617 * will compensate for the slight inefficiency.
618 * XXX: Better handling of overflows is desperately needed.
623 prev
= ix
; /* Save value for overflow check */
627 break; /* Avoid ix / 0 */
628 if (ix
/ temp
!= prev
) {
629 if (err_ovf("integer exponentiation"))
631 return PyLong_Type
.tp_as_number
->nb_power(
637 iw
>>= 1; /* Shift exponent down by 1 bit */
640 temp
*= temp
; /* Square the value of temp */
641 if (prev
!=0 && temp
/prev
!=prev
) {
642 if (err_ovf("integer exponentiation"))
644 return PyLong_Type
.tp_as_number
->nb_power(
645 (PyObject
*)v
, (PyObject
*)w
, (PyObject
*)z
);
648 /* If we did a multiplication, perform a modulo */
655 switch (i_divmod(ix
, iz
, &div
, &mod
)) {
659 case DIVMOD_OVERFLOW
:
660 return PyLong_Type
.tp_as_number
->nb_power(
661 (PyObject
*)v
, (PyObject
*)w
, (PyObject
*)z
);
666 return PyInt_FromLong(ix
);
670 int_neg(PyIntObject
*v
)
675 if (a
< 0 && x
< 0) {
676 if (err_ovf("integer negation"))
678 return PyNumber_Negative(PyLong_FromLong(a
));
680 return PyInt_FromLong(x
);
684 int_pos(PyIntObject
*v
)
686 if (PyInt_CheckExact(v
)) {
688 return (PyObject
*)v
;
691 return PyInt_FromLong(v
->ob_ival
);
695 int_abs(PyIntObject
*v
)
704 int_nonzero(PyIntObject
*v
)
706 return v
->ob_ival
!= 0;
710 int_invert(PyIntObject
*v
)
712 return PyInt_FromLong(~v
->ob_ival
);
716 int_lshift(PyIntObject
*v
, PyIntObject
*w
)
719 CONVERT_TO_LONG(v
, a
);
720 CONVERT_TO_LONG(w
, b
);
722 PyErr_SetString(PyExc_ValueError
, "negative shift count");
725 if (a
== 0 || b
== 0)
728 return PyInt_FromLong(0L);
730 a
= (long)((unsigned long)a
<< b
);
731 return PyInt_FromLong(a
);
735 int_rshift(PyIntObject
*v
, PyIntObject
*w
)
738 CONVERT_TO_LONG(v
, a
);
739 CONVERT_TO_LONG(w
, b
);
741 PyErr_SetString(PyExc_ValueError
, "negative shift count");
744 if (a
== 0 || b
== 0)
753 a
= Py_ARITHMETIC_RIGHT_SHIFT(long, a
, b
);
755 return PyInt_FromLong(a
);
759 int_and(PyIntObject
*v
, PyIntObject
*w
)
762 CONVERT_TO_LONG(v
, a
);
763 CONVERT_TO_LONG(w
, b
);
764 return PyInt_FromLong(a
& b
);
768 int_xor(PyIntObject
*v
, PyIntObject
*w
)
771 CONVERT_TO_LONG(v
, a
);
772 CONVERT_TO_LONG(w
, b
);
773 return PyInt_FromLong(a
^ b
);
777 int_or(PyIntObject
*v
, PyIntObject
*w
)
780 CONVERT_TO_LONG(v
, a
);
781 CONVERT_TO_LONG(w
, b
);
782 return PyInt_FromLong(a
| b
);
786 int_coerce(PyObject
**pv
, PyObject
**pw
)
788 if (PyInt_Check(*pw
)) {
793 return 1; /* Can't do it */
797 int_int(PyIntObject
*v
)
800 return (PyObject
*)v
;
804 int_long(PyIntObject
*v
)
806 return PyLong_FromLong((v
-> ob_ival
));
810 int_float(PyIntObject
*v
)
812 return PyFloat_FromDouble((double)(v
-> ob_ival
));
816 int_oct(PyIntObject
*v
)
819 long x
= v
-> ob_ival
;
823 sprintf(buf
, "0%lo", x
);
824 return PyString_FromString(buf
);
828 int_hex(PyIntObject
*v
)
831 long x
= v
-> ob_ival
;
832 sprintf(buf
, "0x%lx", x
);
833 return PyString_FromString(buf
);
836 staticforward PyObject
*
837 int_subtype_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
);
840 int_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
844 static char *kwlist
[] = {"x", "base", 0};
846 if (type
!= &PyInt_Type
)
847 return int_subtype_new(type
, args
, kwds
); /* Wimp out */
848 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "|Oi:int", kwlist
,
852 return PyInt_FromLong(0L);
854 return PyNumber_Int(x
);
855 if (PyString_Check(x
))
856 return PyInt_FromString(PyString_AS_STRING(x
), NULL
, base
);
857 #ifdef Py_USING_UNICODE
858 if (PyUnicode_Check(x
))
859 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x
),
860 PyUnicode_GET_SIZE(x
),
863 PyErr_SetString(PyExc_TypeError
,
864 "int() can't convert non-string with explicit base");
868 /* Wimpy, slow approach to tp_new calls for subtypes of int:
869 first create a regular int from whatever arguments we got,
870 then allocate a subtype instance and initialize its ob_ival
871 from the regular int. The regular int is then thrown away.
874 int_subtype_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
878 assert(PyType_IsSubtype(type
, &PyInt_Type
));
879 tmp
= int_new(&PyInt_Type
, args
, kwds
);
882 assert(PyInt_Check(tmp
));
883 new = type
->tp_alloc(type
, 0);
886 ((PyIntObject
*)new)->ob_ival
= ((PyIntObject
*)tmp
)->ob_ival
;
891 static char int_doc
[] =
892 "int(x[, base]) -> integer\n\
894 Convert a string or number to an integer, if possible. A floating point\n\
895 argument will be truncated towards zero (this does not include a string\n\
896 representation of a floating point number!) When converting a string, use\n\
897 the optional base. It is an error to supply a base when converting a\n\
900 static PyNumberMethods int_as_number
= {
901 (binaryfunc
)int_add
, /*nb_add*/
902 (binaryfunc
)int_sub
, /*nb_subtract*/
903 (binaryfunc
)int_mul
, /*nb_multiply*/
904 (binaryfunc
)int_classic_div
, /*nb_divide*/
905 (binaryfunc
)int_mod
, /*nb_remainder*/
906 (binaryfunc
)int_divmod
, /*nb_divmod*/
907 (ternaryfunc
)int_pow
, /*nb_power*/
908 (unaryfunc
)int_neg
, /*nb_negative*/
909 (unaryfunc
)int_pos
, /*nb_positive*/
910 (unaryfunc
)int_abs
, /*nb_absolute*/
911 (inquiry
)int_nonzero
, /*nb_nonzero*/
912 (unaryfunc
)int_invert
, /*nb_invert*/
913 (binaryfunc
)int_lshift
, /*nb_lshift*/
914 (binaryfunc
)int_rshift
, /*nb_rshift*/
915 (binaryfunc
)int_and
, /*nb_and*/
916 (binaryfunc
)int_xor
, /*nb_xor*/
917 (binaryfunc
)int_or
, /*nb_or*/
918 int_coerce
, /*nb_coerce*/
919 (unaryfunc
)int_int
, /*nb_int*/
920 (unaryfunc
)int_long
, /*nb_long*/
921 (unaryfunc
)int_float
, /*nb_float*/
922 (unaryfunc
)int_oct
, /*nb_oct*/
923 (unaryfunc
)int_hex
, /*nb_hex*/
924 0, /*nb_inplace_add*/
925 0, /*nb_inplace_subtract*/
926 0, /*nb_inplace_multiply*/
927 0, /*nb_inplace_divide*/
928 0, /*nb_inplace_remainder*/
929 0, /*nb_inplace_power*/
930 0, /*nb_inplace_lshift*/
931 0, /*nb_inplace_rshift*/
932 0, /*nb_inplace_and*/
933 0, /*nb_inplace_xor*/
935 (binaryfunc
)int_div
, /* nb_floor_divide */
936 int_true_divide
, /* nb_true_divide */
937 0, /* nb_inplace_floor_divide */
938 0, /* nb_inplace_true_divide */
941 PyTypeObject PyInt_Type
= {
942 PyObject_HEAD_INIT(&PyType_Type
)
947 (destructor
)int_dealloc
, /* tp_dealloc */
948 (printfunc
)int_print
, /* tp_print */
951 (cmpfunc
)int_compare
, /* tp_compare */
952 (reprfunc
)int_repr
, /* tp_repr */
953 &int_as_number
, /* tp_as_number */
954 0, /* tp_as_sequence */
955 0, /* tp_as_mapping */
956 (hashfunc
)int_hash
, /* tp_hash */
959 PyObject_GenericGetAttr
, /* tp_getattro */
961 0, /* tp_as_buffer */
962 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
|
963 Py_TPFLAGS_BASETYPE
, /* tp_flags */
964 int_doc
, /* tp_doc */
967 0, /* tp_richcompare */
968 0, /* tp_weaklistoffset */
976 0, /* tp_descr_get */
977 0, /* tp_descr_set */
978 0, /* tp_dictoffset */
981 int_new
, /* tp_new */
988 PyIntBlock
*list
, *next
;
990 int bc
, bf
; /* block count, number of freed blocks */
991 int irem
, isum
; /* remaining unfreed ints per block, total */
993 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
996 i
= NSMALLNEGINTS
+ NSMALLPOSINTS
;
1009 while (list
!= NULL
) {
1012 for (i
= 0, p
= &list
->objects
[0];
1015 if (PyInt_CheckExact(p
) && p
->ob_refcnt
!= 0)
1020 list
->next
= block_list
;
1022 for (i
= 0, p
= &list
->objects
[0];
1025 if (!PyInt_CheckExact(p
) ||
1026 p
->ob_refcnt
== 0) {
1027 p
->ob_type
= (struct _typeobject
*)
1031 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
1032 else if (-NSMALLNEGINTS
<= p
->ob_ival
&&
1033 p
->ob_ival
< NSMALLPOSINTS
&&
1034 small_ints
[p
->ob_ival
+
1035 NSMALLNEGINTS
] == NULL
) {
1037 small_ints
[p
->ob_ival
+
1044 PyMem_FREE(list
); /* XXX PyObject_FREE ??? */
1050 if (!Py_VerboseFlag
)
1052 fprintf(stderr
, "# cleanup ints");
1054 fprintf(stderr
, "\n");
1058 ": %d unfreed int%s in %d out of %d block%s\n",
1059 isum
, isum
== 1 ? "" : "s",
1060 bc
- bf
, bc
, bc
== 1 ? "" : "s");
1062 if (Py_VerboseFlag
> 1) {
1064 while (list
!= NULL
) {
1065 for (i
= 0, p
= &list
->objects
[0];
1068 if (PyInt_CheckExact(p
) && p
->ob_refcnt
!= 0)
1070 "# <int at %p, refcnt=%d, val=%ld>\n",
1071 p
, p
->ob_refcnt
, p
->ob_ival
);