annotate Objects/intobject.c @ 28646:193b8aec00ce legacy-trunk

Changes from Jonathan Riehl to allow his pgen extension (PEP 269) to work. This includes some more code that used to be part of pgen in the main parser; I'm okay with that. I'll see if the Windows build needs work next.
author Guido van Rossum <guido@python.org>
date Thu, 17 Apr 2003 14:55:42 +0000
parents beea4f21c9f0
children 28a2975514cd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Integer object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
4 #include "Python.h"
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
5 #include <ctype.h>
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
1448
328f446d2016 Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
7 long
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
8 PyInt_GetMax(void)
1448
328f446d2016 Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
9 {
328f446d2016 Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
10 return LONG_MAX; /* To initialize sys.maxint */
328f446d2016 Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
11 }
328f446d2016 Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
12
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
13 /* Return 1 if exception raised, 0 if caller should retry using longs */
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
14 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
15 err_ovf(char *msg)
11
bcea6c822906 New error handling.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
16 {
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
17 if (PyErr_Warn(PyExc_OverflowWarning, msg) < 0) {
19455
25b9b634d9e3 err_ovf(): only raise OverflowError when OverflowWarning was raised.
Guido van Rossum <guido@python.org>
parents: 19454
diff changeset
18 if (PyErr_ExceptionMatches(PyExc_OverflowWarning))
25b9b634d9e3 err_ovf(): only raise OverflowError when OverflowWarning was raised.
Guido van Rossum <guido@python.org>
parents: 19454
diff changeset
19 PyErr_SetString(PyExc_OverflowError, msg);
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
20 return 1;
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
21 }
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
22 else
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
23 return 0;
11
bcea6c822906 New error handling.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
24 }
bcea6c822906 New error handling.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
25
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
26 /* Integers are quite normal objects, to make object handling uniform.
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
27 (Using odd pointers to represent integers would save much space
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
28 but require extra checks for this special case throughout the code.)
23299
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
29 Since a typical Python program spends much of its time allocating
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
30 and deallocating integers, these operations should be very fast.
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
31 Therefore we use a dedicated allocation scheme with a much lower
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
32 overhead (in space and time) than straight malloc(): a simple
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
33 dedicated free list, filled when necessary with memory from malloc().
23299
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
34
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
35 block_list is a singly-linked list of all PyIntBlocks ever allocated,
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
36 linked via their next members. PyIntBlocks are never returned to the
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
37 system before shutdown (PyInt_Fini).
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
38
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
39 free_list is a singly-linked list of available PyIntObjects, linked
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
40 via abuse of their ob_type members.
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
41 */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
42
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
43 #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
44 #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
45 #define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
46
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
47 struct _intblock {
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
48 struct _intblock *next;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
49 PyIntObject objects[N_INTOBJECTS];
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
50 };
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
51
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
52 typedef struct _intblock PyIntBlock;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
53
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
54 static PyIntBlock *block_list = NULL;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
55 static PyIntObject *free_list = NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
56
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
57 static PyIntObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
58 fill_free_list(void)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
59 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
60 PyIntObject *p, *q;
23299
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
61 /* Python's object allocator isn't appropriate for large blocks. */
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
62 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
63 if (p == NULL)
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
64 return (PyIntObject *) PyErr_NoMemory();
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
65 ((PyIntBlock *)p)->next = block_list;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
66 block_list = (PyIntBlock *)p;
23299
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
67 /* Link the int objects together, from rear to front, then return
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
68 the address of the last int object in the block. */
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
69 p = &((PyIntBlock *)p)->objects[0];
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
70 q = p + N_INTOBJECTS;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
71 while (--q > p)
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
72 q->ob_type = (struct _typeobject *)(q-1);
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
73 q->ob_type = NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
74 return p + N_INTOBJECTS - 1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
75 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
76
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
77 #ifndef NSMALLPOSINTS
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
78 #define NSMALLPOSINTS 100
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
79 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
80 #ifndef NSMALLNEGINTS
26889
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
81 #define NSMALLNEGINTS 5
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
82 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
83 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
84 /* References to small integers are saved in this array so that they
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
85 can be shared.
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
86 The integers that are saved are those in the range
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
87 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
88 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
89 static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
90 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
91 #ifdef COUNT_ALLOCS
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
92 int quick_int_allocs, quick_neg_int_allocs;
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
93 #endif
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
94
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
95 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
96 PyInt_FromLong(long ival)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
98 register PyIntObject *v;
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
99 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
26889
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
100 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
101 v = small_ints[ival + NSMALLNEGINTS];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
102 Py_INCREF(v);
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
103 #ifdef COUNT_ALLOCS
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
104 if (ival >= 0)
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
105 quick_int_allocs++;
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
106 else
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
107 quick_neg_int_allocs++;
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
108 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
109 return (PyObject *) v;
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
110 }
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
111 #endif
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
112 if (free_list == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
113 if ((free_list = fill_free_list()) == NULL)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
114 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 }
24898
a869d2de536f Call me anal, but there was a particular phrase that was speading to
Guido van Rossum <guido@python.org>
parents: 24831
diff changeset
116 /* Inline PyObject_New */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
117 v = free_list;
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
118 free_list = (PyIntObject *)v->ob_type;
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
119 PyObject_INIT(v, &PyInt_Type);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
120 v->ob_ival = ival;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
121 return (PyObject *) v;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
122 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
123
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
124 static void
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
125 int_dealloc(PyIntObject *v)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
126 {
19865
f2a3009b528e Replace a few places where X->ob_type was compared to &PyXXX_Type with
Guido van Rossum <guido@python.org>
parents: 19719
diff changeset
127 if (PyInt_CheckExact(v)) {
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
128 v->ob_type = (struct _typeobject *)free_list;
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
129 free_list = v;
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
130 }
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
131 else
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
132 v->ob_type->tp_free((PyObject *)v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
23279
200559fcc664 Make sure that tp_free frees the int the same way as tp_dealloc would.
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
135 static void
200559fcc664 Make sure that tp_free frees the int the same way as tp_dealloc would.
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
136 int_free(PyIntObject *v)
200559fcc664 Make sure that tp_free frees the int the same way as tp_dealloc would.
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
137 {
200559fcc664 Make sure that tp_free frees the int the same way as tp_dealloc would.
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
138 v->ob_type = (struct _typeobject *)free_list;
200559fcc664 Make sure that tp_free frees the int the same way as tp_dealloc would.
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
139 free_list = v;
200559fcc664 Make sure that tp_free frees the int the same way as tp_dealloc would.
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
140 }
200559fcc664 Make sure that tp_free frees the int the same way as tp_dealloc would.
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
141
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 long
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
143 PyInt_AsLong(register PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
145 PyNumberMethods *nb;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
146 PyIntObject *io;
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
147 long val;
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
148
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
149 if (op && PyInt_Check(op))
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
150 return PyInt_AS_LONG((PyIntObject*) op);
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
151
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
152 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
153 nb->nb_int == NULL) {
12630
57d4fc8308e3 Replace PyErr_BadArgument() error in PyInt_AsLong() with "an integer
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
154 PyErr_SetString(PyExc_TypeError, "an integer is required");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 }
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
157
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
158 io = (PyIntObject*) (*nb->nb_int) (op);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
159 if (io == NULL)
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
160 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
161 if (!PyInt_Check(io)) {
26291
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
162 if (PyLong_Check(io)) {
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
163 /* got a long? => retry int conversion */
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
164 val = PyLong_AsLong((PyObject *)io);
27993
beea4f21c9f0 Strange control flow in PyInt_AsLong. When nb_int is called inside
Thomas Heller <theller@ctypes.org>
parents: 27851
diff changeset
165 Py_DECREF(io);
beea4f21c9f0 Strange control flow in PyInt_AsLong. When nb_int is called inside
Thomas Heller <theller@ctypes.org>
parents: 27851
diff changeset
166 if ((val == -1) && PyErr_Occurred())
26291
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
167 return -1;
27993
beea4f21c9f0 Strange control flow in PyInt_AsLong. When nb_int is called inside
Thomas Heller <theller@ctypes.org>
parents: 27851
diff changeset
168 return val;
26291
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
169 }
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
170 else
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
171 {
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
172 PyErr_SetString(PyExc_TypeError,
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
173 "nb_int should return int object");
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
174 return -1;
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
175 }
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
176 }
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
177
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
178 val = PyInt_AS_LONG(io);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
179 Py_DECREF(io);
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
180
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
181 return val;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
184 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
185 PyInt_FromString(char *s, char **pend, int base)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
186 {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
187 char *end;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
188 long x;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
189 char buffer[256]; /* For errors */
27851
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
190 int warn = 0;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
191
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
192 if ((base != 0 && base < 2) || base > 36) {
27851
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
193 PyErr_SetString(PyExc_ValueError,
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
194 "int() base must be >= 2 and <= 36");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
195 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
196 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
197
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
198 while (*s && isspace(Py_CHARMASK(*s)))
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
199 s++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
200 errno = 0;
27851
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
201 if (base == 0 && s[0] == '0') {
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
202 x = (long) PyOS_strtoul(s, &end, base);
27851
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
203 if (x < 0)
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
204 warn = 1;
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
205 }
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
206 else
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
207 x = PyOS_strtol(s, &end, base);
16997
86fd723f2928 Use Py_CHARMASK for ctype macros. Fixes bug #232787.
Martin v. Löwis <martin@v.loewis.de>
parents: 15945
diff changeset
208 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
209 goto bad;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
210 while (*end && isspace(Py_CHARMASK(*end)))
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
211 end++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
212 if (*end != '\0') {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
213 bad:
21080
5b459677fc44 PyInt_FromString(), int_repr(), int_oct(), int_hex(): Conversion of
Barry Warsaw <barry@python.org>
parents: 19976
diff changeset
214 PyOS_snprintf(buffer, sizeof(buffer),
5b459677fc44 PyInt_FromString(), int_repr(), int_oct(), int_hex(): Conversion of
Barry Warsaw <barry@python.org>
parents: 19976
diff changeset
215 "invalid literal for int(): %.200s", s);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
216 PyErr_SetString(PyExc_ValueError, buffer);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
217 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
218 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
219 else if (errno != 0) {
26081
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
220 if (err_ovf("string/unicode conversion"))
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
221 return NULL;
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
222 return PyLong_FromString(s, pend, base);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
223 }
27851
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
224 if (warn) {
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
225 if (PyErr_Warn(PyExc_FutureWarning,
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
226 "int('0...', 0): sign will change in Python 2.4") < 0)
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
227 return NULL;
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
228 }
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
229 if (pend)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
230 *pend = end;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
231 return PyInt_FromLong(x);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
232 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
233
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
234 #ifdef Py_USING_UNICODE
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
235 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
236 PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
237 {
26081
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
238 PyObject *result;
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
239 char *buffer = PyMem_MALLOC(length+1);
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
240
26081
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
241 if (buffer == NULL)
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
242 return NULL;
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
243
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
244 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
245 PyMem_FREE(buffer);
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
246 return NULL;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
247 }
26081
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
248 result = PyInt_FromString(buffer, NULL, base);
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
249 PyMem_FREE(buffer);
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
250 return result;
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
251 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
252 #endif
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
253
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
256 /* Integers are seen as the "smallest" of all numeric types and thus
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
257 don't have any knowledge about conversion of other types to
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
258 integers. */
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
259
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
260 #define CONVERT_TO_LONG(obj, lng) \
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
261 if (PyInt_Check(obj)) { \
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
262 lng = PyInt_AS_LONG(obj); \
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
263 } \
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
264 else { \
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
265 Py_INCREF(Py_NotImplemented); \
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
266 return Py_NotImplemented; \
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
267 }
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
268
750
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 610
diff changeset
269 /* ARGSUSED */
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 240
diff changeset
270 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
271 int_print(PyIntObject *v, FILE *fp, int flags)
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
272 /* flags -- not used but required by interface */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 fprintf(fp, "%ld", v->ob_ival);
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 240
diff changeset
275 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
277
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
278 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
279 int_repr(PyIntObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 {
21124
21bb4c4b7e26 SF bug #487743: test_builtin fails on 64 bit platform.
Tim Peters <tim.peters@gmail.com>
parents: 21080
diff changeset
281 char buf[64];
21080
5b459677fc44 PyInt_FromString(), int_repr(), int_oct(), int_hex(): Conversion of
Barry Warsaw <barry@python.org>
parents: 19976
diff changeset
282 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
283 return PyString_FromString(buf);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
285
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
287 int_compare(PyIntObject *v, PyIntObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 register long i = v->ob_ival;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 register long j = w->ob_ival;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 return (i < j) ? -1 : (i > j) ? 1 : 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
293
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
294 static long
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
295 int_hash(PyIntObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
296 {
4903
6a629d278a18 Fix overflow test for multiply to catch some cases it missed.
Guido van Rossum <guido@python.org>
parents: 4606
diff changeset
297 /* XXX If this is changed, you also need to change the way
6a629d278a18 Fix overflow test for multiply to catch some cases it missed.
Guido van Rossum <guido@python.org>
parents: 4606
diff changeset
298 Python's long, float and complex types are hashed. */
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
299 long x = v -> ob_ival;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
300 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
301 x = -2;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
302 return x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
303 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
304
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
305 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
306 int_add(PyIntObject *v, PyIntObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 register long a, b, x;
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
309 CONVERT_TO_LONG(v, a);
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
310 CONVERT_TO_LONG(w, b);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 x = a + b;
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
312 if ((x^a) >= 0 || (x^b) >= 0)
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
313 return PyInt_FromLong(x);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
314 if (err_ovf("integer addition"))
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
315 return NULL;
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
316 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
318
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
319 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
320 int_sub(PyIntObject *v, PyIntObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 register long a, b, x;
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
323 CONVERT_TO_LONG(v, a);
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
324 CONVERT_TO_LONG(w, b);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 x = a - b;
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
326 if ((x^a) >= 0 || (x^~b) >= 0)
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
327 return PyInt_FromLong(x);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
328 if (err_ovf("integer subtraction"))
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
329 return NULL;
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
330 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
331 (PyObject *)w);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
333
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
334 /*
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
335 Integer overflow checking for * is painful: Python tried a couple ways, but
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
336 they didn't work on all platforms, or failed in endcases (a product of
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
337 -sys.maxint-1 has been a particular pain).
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
338
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
339 Here's another way:
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
340
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
341 The native long product x*y is either exactly right or *way* off, being
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
342 just the last n bits of the true product, where n is the number of bits
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
343 in a long (the delivered product is the true product plus i*2**n for
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
344 some integer i).
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
345
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
346 The native double product (double)x * (double)y is subject to three
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
347 rounding errors: on a sizeof(long)==8 box, each cast to double can lose
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
348 info, and even on a sizeof(long)==4 box, the multiplication can lose info.
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
349 But, unlike the native long product, it's not in *range* trouble: even
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
350 if sizeof(long)==32 (256-bit longs), the product easily fits in the
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
351 dynamic range of a double. So the leading 50 (or so) bits of the double
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
352 product are correct.
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
353
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
354 We check these two ways against each other, and declare victory if they're
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
355 approximately the same. Else, because the native long product is the only
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
356 one that can lose catastrophic amounts of information, it's the native long
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
357 product that must have overflowed.
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
358 */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
359
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
360 static PyObject *
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
361 int_mul(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 {
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
363 long a, b;
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
364 long longprod; /* a*b in native long arithmetic */
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
365 double doubled_longprod; /* (double)longprod */
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
366 double doubleprod; /* (double)a * (double)b */
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
367
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
368 CONVERT_TO_LONG(v, a);
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
369 CONVERT_TO_LONG(w, b);
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
370 longprod = a * b;
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
371 doubleprod = (double)a * (double)b;
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
372 doubled_longprod = (double)longprod;
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
373
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
374 /* Fast path for normal case: small multiplicands, and no info
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
375 is lost in either method. */
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
376 if (doubled_longprod == doubleprod)
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
377 return PyInt_FromLong(longprod);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
378
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
379 /* Somebody somewhere lost info. Close enough, or way off? Note
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
380 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
381 The difference either is or isn't significant compared to the
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
382 true value (of which doubleprod is a good approximation).
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
383 */
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
384 {
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
385 const double diff = doubled_longprod - doubleprod;
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
386 const double absdiff = diff >= 0.0 ? diff : -diff;
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
387 const double absprod = doubleprod >= 0.0 ? doubleprod :
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
388 -doubleprod;
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
389 /* absdiff/absprod <= 1/32 iff
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
390 32 * absdiff <= absprod -- 5 good bits is "close enough" */
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
391 if (32.0 * absdiff <= absprod)
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
392 return PyInt_FromLong(longprod);
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
393 else if (err_ovf("integer multiplication"))
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
394 return NULL;
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
395 else
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
396 return PyLong_Type.tp_as_number->nb_multiply(v, w);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
397 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
399
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
400 /* Return type of i_divmod */
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
401 enum divmod_result {
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
402 DIVMOD_OK, /* Correct result */
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
403 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
404 DIVMOD_ERROR /* Exception raised */
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
405 };
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
406
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
407 static enum divmod_result
18146
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
408 i_divmod(register long x, register long y,
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
409 long *p_xdivy, long *p_xmody)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 {
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
411 long xdivy, xmody;
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
412
18146
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
413 if (y == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
414 PyErr_SetString(PyExc_ZeroDivisionError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
415 "integer division or modulo by zero");
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
416 return DIVMOD_ERROR;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 }
18146
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
418 /* (-sys.maxint-1)/-1 is the only overflow case. */
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
419 if (y == -1 && x < 0 && x == -x) {
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
420 if (err_ovf("integer division"))
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
421 return DIVMOD_ERROR;
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
422 return DIVMOD_OVERFLOW;
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
423 }
18146
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
424 xdivy = x / y;
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
425 xmody = x - xdivy * y;
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
426 /* If the signs of x and y differ, and the remainder is non-0,
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
427 * C89 doesn't define whether xdivy is now the floor or the
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
428 * ceiling of the infinitely precise quotient. We want the floor,
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
429 * and we have it iff the remainder's sign matches y's.
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
430 */
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
431 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
432 xmody += y;
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
433 --xdivy;
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
434 assert(xmody && ((y ^ xmody) >= 0));
229
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
435 }
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
436 *p_xdivy = xdivy;
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
437 *p_xmody = xmody;
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
438 return DIVMOD_OK;
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
439 }
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
440
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
441 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
442 int_div(PyIntObject *x, PyIntObject *y)
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
443 {
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
444 long xi, yi;
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
445 long d, m;
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
446 CONVERT_TO_LONG(x, xi);
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
447 CONVERT_TO_LONG(y, yi);
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
448 switch (i_divmod(xi, yi, &d, &m)) {
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
449 case DIVMOD_OK:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
450 return PyInt_FromLong(d);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
451 case DIVMOD_OVERFLOW:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
452 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
453 (PyObject *)y);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
454 default:
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
455 return NULL;
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
456 }
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
457 }
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
458
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
459 static PyObject *
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
460 int_classic_div(PyIntObject *x, PyIntObject *y)
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
461 {
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
462 long xi, yi;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
463 long d, m;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
464 CONVERT_TO_LONG(x, xi);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
465 CONVERT_TO_LONG(y, yi);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
466 if (Py_DivisionWarningFlag &&
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
467 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
468 return NULL;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
469 switch (i_divmod(xi, yi, &d, &m)) {
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
470 case DIVMOD_OK:
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
471 return PyInt_FromLong(d);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
472 case DIVMOD_OVERFLOW:
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
473 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
474 (PyObject *)y);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
475 default:
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
476 return NULL;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
477 }
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
478 }
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
479
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
480 static PyObject *
19676
c768d3191df1 Move int_true_divide next to the other division routines.
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
481 int_true_divide(PyObject *v, PyObject *w)
c768d3191df1 Move int_true_divide next to the other division routines.
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
482 {
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19676
diff changeset
483 /* If they aren't both ints, give someone else a chance. In
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19676
diff changeset
484 particular, this lets int/long get handled by longs, which
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19676
diff changeset
485 underflows to 0 gracefully if the long is too big to convert
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19676
diff changeset
486 to float. */
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19676
diff changeset
487 if (PyInt_Check(v) && PyInt_Check(w))
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19676
diff changeset
488 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19676
diff changeset
489 Py_INCREF(Py_NotImplemented);
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19676
diff changeset
490 return Py_NotImplemented;
19676
c768d3191df1 Move int_true_divide next to the other division routines.
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
491 }
c768d3191df1 Move int_true_divide next to the other division routines.
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
492
c768d3191df1 Move int_true_divide next to the other division routines.
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
493 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
494 int_mod(PyIntObject *x, PyIntObject *y)
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
495 {
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
496 long xi, yi;
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
497 long d, m;
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
498 CONVERT_TO_LONG(x, xi);
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
499 CONVERT_TO_LONG(y, yi);
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
500 switch (i_divmod(xi, yi, &d, &m)) {
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
501 case DIVMOD_OK:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
502 return PyInt_FromLong(m);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
503 case DIVMOD_OVERFLOW:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
504 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
505 (PyObject *)y);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
506 default:
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
507 return NULL;
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
508 }
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
509 }
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
510
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
511 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
512 int_divmod(PyIntObject *x, PyIntObject *y)
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
513 {
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
514 long xi, yi;
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
515 long d, m;
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
516 CONVERT_TO_LONG(x, xi);
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
517 CONVERT_TO_LONG(y, yi);
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
518 switch (i_divmod(xi, yi, &d, &m)) {
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
519 case DIVMOD_OK:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
520 return Py_BuildValue("(ll)", d, m);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
521 case DIVMOD_OVERFLOW:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
522 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
523 (PyObject *)y);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
524 default:
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
525 return NULL;
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
526 }
229
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
527 }
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
528
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
529 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
530 int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 {
4606
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
532 register long iv, iw, iz=0, ix, temp, prev;
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
533 CONVERT_TO_LONG(v, iv);
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
534 CONVERT_TO_LONG(w, iw);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
535 if (iw < 0) {
19666
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
536 if ((PyObject *)z != Py_None) {
19719
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19677
diff changeset
537 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19677
diff changeset
538 "cannot be negative when 3rd argument specified");
19666
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
539 return NULL;
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
540 }
18499
2bb75a1f3184 On int to the negative integral power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents: 18146
diff changeset
541 /* Return a float. This works because we know that
2bb75a1f3184 On int to the negative integral power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents: 18146
diff changeset
542 this calls float_pow() which converts its
2bb75a1f3184 On int to the negative integral power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents: 18146
diff changeset
543 arguments to double. */
2bb75a1f3184 On int to the negative integral power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents: 18146
diff changeset
544 return PyFloat_Type.tp_as_number->nb_power(
2bb75a1f3184 On int to the negative integral power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents: 18146
diff changeset
545 (PyObject *)v, (PyObject *)w, (PyObject *)z);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
546 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
547 if ((PyObject *)z != Py_None) {
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
548 CONVERT_TO_LONG(z, iz);
4606
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
549 if (iz == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
550 PyErr_SetString(PyExc_ValueError,
19719
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19677
diff changeset
551 "pow() 3rd argument cannot be 0");
4606
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
552 return NULL;
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
553 }
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
554 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
555 /*
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
556 * XXX: The original exponentiation code stopped looping
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
557 * when temp hit zero; this code will continue onwards
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
558 * unnecessarily, but at least it won't cause any errors.
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
559 * Hopefully the speed improvement from the fast exponentiation
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
560 * will compensate for the slight inefficiency.
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
561 * XXX: Better handling of overflows is desperately needed.
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
562 */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
563 temp = iv;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
564 ix = 1;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
565 while (iw > 0) {
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
566 prev = ix; /* Save value for overflow check */
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
567 if (iw & 1) {
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
568 ix = ix*temp;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
569 if (temp == 0)
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
570 break; /* Avoid ix / 0 */
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
571 if (ix / temp != prev) {
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
572 if (err_ovf("integer exponentiation"))
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
573 return NULL;
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
574 return PyLong_Type.tp_as_number->nb_power(
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
575 (PyObject *)v,
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
576 (PyObject *)w,
19454
9afd9d5fb97a int_pow(): Repair typo when passing on to float pow (the 2nd argument was
Tim Peters <tim.peters@gmail.com>
parents: 19435
diff changeset
577 (PyObject *)z);
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
578 }
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
579 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
580 iw >>= 1; /* Shift exponent down by 1 bit */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
581 if (iw==0) break;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
582 prev = temp;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
583 temp *= temp; /* Square the value of temp */
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
584 if (prev!=0 && temp/prev!=prev) {
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
585 if (err_ovf("integer exponentiation"))
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
586 return NULL;
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
587 return PyLong_Type.tp_as_number->nb_power(
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
588 (PyObject *)v, (PyObject *)w, (PyObject *)z);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
589 }
4606
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
590 if (iz) {
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
591 /* If we did a multiplication, perform a modulo */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
592 ix = ix % iz;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
593 temp = temp % iz;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
594 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
595 }
4606
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
596 if (iz) {
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
597 long div, mod;
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
598 switch (i_divmod(ix, iz, &div, &mod)) {
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
599 case DIVMOD_OK:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
600 ix = mod;
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
601 break;
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
602 case DIVMOD_OVERFLOW:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
603 return PyLong_Type.tp_as_number->nb_power(
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
604 (PyObject *)v, (PyObject *)w, (PyObject *)z);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
605 default:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
606 return NULL;
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
607 }
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
608 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
609 return PyInt_FromLong(ix);
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
610 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
611
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
612 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
613 int_neg(PyIntObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 register long a, x;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 a = v->ob_ival;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
617 x = -a;
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
618 if (a < 0 && x < 0) {
27301
5a975e82d70f SF # 669553, fix memory (ref) leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 26923
diff changeset
619 PyObject *o;
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
620 if (err_ovf("integer negation"))
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
621 return NULL;
27301
5a975e82d70f SF # 669553, fix memory (ref) leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 26923
diff changeset
622 o = PyLong_FromLong(a);
5a975e82d70f SF # 669553, fix memory (ref) leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 26923
diff changeset
623 if (o != NULL) {
5a975e82d70f SF # 669553, fix memory (ref) leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 26923
diff changeset
624 PyObject *result = PyNumber_Negative(o);
5a975e82d70f SF # 669553, fix memory (ref) leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 26923
diff changeset
625 Py_DECREF(o);
5a975e82d70f SF # 669553, fix memory (ref) leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 26923
diff changeset
626 return result;
5a975e82d70f SF # 669553, fix memory (ref) leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 26923
diff changeset
627 }
5a975e82d70f SF # 669553, fix memory (ref) leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 26923
diff changeset
628 return NULL;
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
629 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
630 return PyInt_FromLong(x);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
631 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
632
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
633 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
634 int_pos(PyIntObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
635 {
19878
cc146ee52897 More bug 460020. When I is a subclass of int, disable the +I(whatever),
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
636 if (PyInt_CheckExact(v)) {
cc146ee52897 More bug 460020. When I is a subclass of int, disable the +I(whatever),
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
637 Py_INCREF(v);
cc146ee52897 More bug 460020. When I is a subclass of int, disable the +I(whatever),
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
638 return (PyObject *)v;
cc146ee52897 More bug 460020. When I is a subclass of int, disable the +I(whatever),
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
639 }
cc146ee52897 More bug 460020. When I is a subclass of int, disable the +I(whatever),
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
640 else
cc146ee52897 More bug 460020. When I is a subclass of int, disable the +I(whatever),
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
641 return PyInt_FromLong(v->ob_ival);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
642 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
643
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
644 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
645 int_abs(PyIntObject *v)
229
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
646 {
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
647 if (v->ob_ival >= 0)
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
648 return int_pos(v);
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
649 else
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
650 return int_neg(v);
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
651 }
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
652
240
0aad3a7b97ef Added nonzero test
Guido van Rossum <guido@python.org>
parents: 229
diff changeset
653 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
654 int_nonzero(PyIntObject *v)
240
0aad3a7b97ef Added nonzero test
Guido van Rossum <guido@python.org>
parents: 229
diff changeset
655 {
0aad3a7b97ef Added nonzero test
Guido van Rossum <guido@python.org>
parents: 229
diff changeset
656 return v->ob_ival != 0;
0aad3a7b97ef Added nonzero test
Guido van Rossum <guido@python.org>
parents: 229
diff changeset
657 }
0aad3a7b97ef Added nonzero test
Guido van Rossum <guido@python.org>
parents: 229
diff changeset
658
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
659 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
660 int_invert(PyIntObject *v)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
661 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
662 return PyInt_FromLong(~v->ob_ival);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
663 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
664
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
665 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
666 int_lshift(PyIntObject *v, PyIntObject *w)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
667 {
24736
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
668 long a, b, c;
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
669 CONVERT_TO_LONG(v, a);
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
670 CONVERT_TO_LONG(w, b);
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
671 if (b < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
672 PyErr_SetString(PyExc_ValueError, "negative shift count");
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
673 return NULL;
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
674 }
19878
cc146ee52897 More bug 460020. When I is a subclass of int, disable the +I(whatever),
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
675 if (a == 0 || b == 0)
cc146ee52897 More bug 460020. When I is a subclass of int, disable the +I(whatever),
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
676 return int_pos(v);
1365
a5fc104849aa Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
677 if (b >= LONG_BIT) {
24831
a602e4878b4e More changes of DeprecationWarning to FutureWarning.
Guido van Rossum <guido@python.org>
parents: 24780
diff changeset
678 if (PyErr_Warn(PyExc_FutureWarning,
24736
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
679 "x<<y losing bits or changing sign "
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
680 "will return a long in Python 2.4 and up") < 0)
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
681 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
682 return PyInt_FromLong(0L);
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
683 }
24742
c659f04ee499 int_lshift(): Simplified/sped overflow-checking.
Tim Peters <tim.peters@gmail.com>
parents: 24738
diff changeset
684 c = a << b;
c659f04ee499 int_lshift(): Simplified/sped overflow-checking.
Tim Peters <tim.peters@gmail.com>
parents: 24738
diff changeset
685 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
24831
a602e4878b4e More changes of DeprecationWarning to FutureWarning.
Guido van Rossum <guido@python.org>
parents: 24780
diff changeset
686 if (PyErr_Warn(PyExc_FutureWarning,
24736
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
687 "x<<y losing bits or changing sign "
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
688 "will return a long in Python 2.4 and up") < 0)
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
689 return NULL;
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
690 }
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
691 return PyInt_FromLong(c);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
692 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
693
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
694 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
695 int_rshift(PyIntObject *v, PyIntObject *w)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
696 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
697 register long a, b;
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
698 CONVERT_TO_LONG(v, a);
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
699 CONVERT_TO_LONG(w, b);
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
700 if (b < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
701 PyErr_SetString(PyExc_ValueError, "negative shift count");
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
702 return NULL;
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
703 }
19878
cc146ee52897 More bug 460020. When I is a subclass of int, disable the +I(whatever),
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
704 if (a == 0 || b == 0)
cc146ee52897 More bug 460020. When I is a subclass of int, disable the +I(whatever),
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
705 return int_pos(v);
1365
a5fc104849aa Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
706 if (b >= LONG_BIT) {
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
707 if (a < 0)
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
708 a = -1;
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
709 else
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
710 a = 0;
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
711 }
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
712 else {
13423
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
713 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
714 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
715 return PyInt_FromLong(a);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
716 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
717
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
718 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
719 int_and(PyIntObject *v, PyIntObject *w)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
720 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
721 register long a, b;
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
722 CONVERT_TO_LONG(v, a);
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
723 CONVERT_TO_LONG(w, b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
724 return PyInt_FromLong(a & b);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
725 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
726
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
727 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
728 int_xor(PyIntObject *v, PyIntObject *w)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
729 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
730 register long a, b;
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
731 CONVERT_TO_LONG(v, a);
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
732 CONVERT_TO_LONG(w, b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
733 return PyInt_FromLong(a ^ b);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
734 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
735
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
736 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
737 int_or(PyIntObject *v, PyIntObject *w)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
738 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
739 register long a, b;
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
740 CONVERT_TO_LONG(v, a);
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
741 CONVERT_TO_LONG(w, b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
742 return PyInt_FromLong(a | b);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
743 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
744
19976
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
745 static int
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
746 int_coerce(PyObject **pv, PyObject **pw)
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
747 {
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
748 if (PyInt_Check(*pw)) {
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
749 Py_INCREF(*pv);
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
750 Py_INCREF(*pw);
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
751 return 0;
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
752 }
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
753 return 1; /* Can't do it */
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
754 }
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
755
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
756 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
757 int_int(PyIntObject *v)
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
758 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
759 Py_INCREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
760 return (PyObject *)v;
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
761 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
762
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
763 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
764 int_long(PyIntObject *v)
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
765 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
766 return PyLong_FromLong((v -> ob_ival));
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
767 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
768
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
769 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
770 int_float(PyIntObject *v)
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
771 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
772 return PyFloat_FromDouble((double)(v -> ob_ival));
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
773 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
774
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
775 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
776 int_oct(PyIntObject *v)
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
777 {
4952
05fd432d9e2b Increased buffer sizes used by hex() and oct() -- on 64-bit or 128-bit
Guido van Rossum <guido@python.org>
parents: 4937
diff changeset
778 char buf[100];
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
779 long x = v -> ob_ival;
24736
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
780 if (x < 0) {
24831
a602e4878b4e More changes of DeprecationWarning to FutureWarning.
Guido van Rossum <guido@python.org>
parents: 24780
diff changeset
781 if (PyErr_Warn(PyExc_FutureWarning,
24736
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
782 "hex()/oct() of negative int will return "
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
783 "a signed string in Python 2.4 and up") < 0)
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
784 return NULL;
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
785 }
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
786 if (x == 0)
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
787 strcpy(buf, "0");
4937
789de7c66712 Changed hex() and oct() again, to never emit a '-' sign.
Guido van Rossum <guido@python.org>
parents: 4926
diff changeset
788 else
21080
5b459677fc44 PyInt_FromString(), int_repr(), int_oct(), int_hex(): Conversion of
Barry Warsaw <barry@python.org>
parents: 19976
diff changeset
789 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
790 return PyString_FromString(buf);
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
791 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
792
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
793 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
794 int_hex(PyIntObject *v)
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
795 {
4952
05fd432d9e2b Increased buffer sizes used by hex() and oct() -- on 64-bit or 128-bit
Guido van Rossum <guido@python.org>
parents: 4937
diff changeset
796 char buf[100];
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
797 long x = v -> ob_ival;
24736
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
798 if (x < 0) {
24831
a602e4878b4e More changes of DeprecationWarning to FutureWarning.
Guido van Rossum <guido@python.org>
parents: 24780
diff changeset
799 if (PyErr_Warn(PyExc_FutureWarning,
24736
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
800 "hex()/oct() of negative int will return "
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
801 "a signed string in Python 2.4 and up") < 0)
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
802 return NULL;
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
803 }
21080
5b459677fc44 PyInt_FromString(), int_repr(), int_oct(), int_hex(): Conversion of
Barry Warsaw <barry@python.org>
parents: 19976
diff changeset
804 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
805 return PyString_FromString(buf);
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
806 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
807
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
808 static PyObject *
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
809 int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
810
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
811 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
812 int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
813 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
814 PyObject *x = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
815 int base = -909;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
816 static char *kwlist[] = {"x", "base", 0};
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
817
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
818 if (type != &PyInt_Type)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
819 return int_subtype_new(type, args, kwds); /* Wimp out */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
820 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
821 &x, &base))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
822 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
823 if (x == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
824 return PyInt_FromLong(0L);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
825 if (base == -909)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
826 return PyNumber_Int(x);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
827 if (PyString_Check(x))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
828 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
829 #ifdef Py_USING_UNICODE
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
830 if (PyUnicode_Check(x))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
831 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
832 PyUnicode_GET_SIZE(x),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
833 base);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
834 #endif
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
835 PyErr_SetString(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
836 "int() can't convert non-string with explicit base");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
837 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
838 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
839
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
840 /* Wimpy, slow approach to tp_new calls for subtypes of int:
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
841 first create a regular int from whatever arguments we got,
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
842 then allocate a subtype instance and initialize its ob_ival
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
843 from the regular int. The regular int is then thrown away.
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
844 */
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
845 static PyObject *
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
846 int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
847 {
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
848 PyObject *tmp, *new;
27779
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
849 long ival;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
850
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
851 assert(PyType_IsSubtype(type, &PyInt_Type));
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
852 tmp = int_new(&PyInt_Type, args, kwds);
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
853 if (tmp == NULL)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
854 return NULL;
27779
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
855 if (!PyInt_Check(tmp)) {
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
856 if (!PyLong_Check(tmp)) {
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
857 PyErr_SetString(PyExc_ValueError,
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
858 "value must convertable to an int");
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
859 return NULL;
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
860 }
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
861 ival = PyLong_AsLong(tmp);
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
862 if (ival == -1 && PyErr_Occurred())
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
863 return NULL;
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
864
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
865 } else {
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
866 ival = ((PyIntObject *)tmp)->ob_ival;
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
867 }
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
868
19590
bf2c77840f1c Fix typo: double semicolons.
Guido van Rossum <guido@python.org>
parents: 19552
diff changeset
869 new = type->tp_alloc(type, 0);
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
870 if (new == NULL)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
871 return NULL;
27779
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
872 ((PyIntObject *)new)->ob_ival = ival;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
873 Py_DECREF(tmp);
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
874 return new;
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
875 }
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
876
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
877 static PyObject *
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
878 int_getnewargs(PyIntObject *v)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
879 {
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
880 return Py_BuildValue("(l)", v->ob_ival);
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
881 }
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
882
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
883 static PyMethodDef int_methods[] = {
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
884 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
885 {NULL, NULL} /* sentinel */
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
886 };
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
887
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23299
diff changeset
888 PyDoc_STRVAR(int_doc,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
889 "int(x[, base]) -> integer\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
890 \n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
891 Convert a string or number to an integer, if possible. A floating point\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
892 argument will be truncated towards zero (this does not include a string\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
893 representation of a floating point number!) When converting a string, use\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
894 the optional base. It is an error to supply a base when converting a\n\
26291
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
895 non-string. If the argument is outside the integer range a long object\n\
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
896 will be returned instead.");
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
897
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
898 static PyNumberMethods int_as_number = {
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
899 (binaryfunc)int_add, /*nb_add*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
900 (binaryfunc)int_sub, /*nb_subtract*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
901 (binaryfunc)int_mul, /*nb_multiply*/
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
902 (binaryfunc)int_classic_div, /*nb_divide*/
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
903 (binaryfunc)int_mod, /*nb_remainder*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
904 (binaryfunc)int_divmod, /*nb_divmod*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
905 (ternaryfunc)int_pow, /*nb_power*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
906 (unaryfunc)int_neg, /*nb_negative*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
907 (unaryfunc)int_pos, /*nb_positive*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
908 (unaryfunc)int_abs, /*nb_absolute*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
909 (inquiry)int_nonzero, /*nb_nonzero*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
910 (unaryfunc)int_invert, /*nb_invert*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
911 (binaryfunc)int_lshift, /*nb_lshift*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
912 (binaryfunc)int_rshift, /*nb_rshift*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
913 (binaryfunc)int_and, /*nb_and*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
914 (binaryfunc)int_xor, /*nb_xor*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
915 (binaryfunc)int_or, /*nb_or*/
19976
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
916 int_coerce, /*nb_coerce*/
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
917 (unaryfunc)int_int, /*nb_int*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
918 (unaryfunc)int_long, /*nb_long*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
919 (unaryfunc)int_float, /*nb_float*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
920 (unaryfunc)int_oct, /*nb_oct*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
921 (unaryfunc)int_hex, /*nb_hex*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
922 0, /*nb_inplace_add*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
923 0, /*nb_inplace_subtract*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
924 0, /*nb_inplace_multiply*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
925 0, /*nb_inplace_divide*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
926 0, /*nb_inplace_remainder*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
927 0, /*nb_inplace_power*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
928 0, /*nb_inplace_lshift*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
929 0, /*nb_inplace_rshift*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
930 0, /*nb_inplace_and*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
931 0, /*nb_inplace_xor*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
932 0, /*nb_inplace_or*/
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
933 (binaryfunc)int_div, /* nb_floor_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
934 int_true_divide, /* nb_true_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
935 0, /* nb_inplace_floor_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
936 0, /* nb_inplace_true_divide */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
937 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
938
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
939 PyTypeObject PyInt_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
940 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
941 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
942 "int",
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
943 sizeof(PyIntObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
944 0,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
945 (destructor)int_dealloc, /* tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
946 (printfunc)int_print, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
947 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
948 0, /* tp_setattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
949 (cmpfunc)int_compare, /* tp_compare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
950 (reprfunc)int_repr, /* tp_repr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
951 &int_as_number, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
952 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
953 0, /* tp_as_mapping */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
954 (hashfunc)int_hash, /* tp_hash */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
955 0, /* tp_call */
21858
d521cf0899c8 Bugfix candidate.
Guido van Rossum <guido@python.org>
parents: 21206
diff changeset
956 (reprfunc)int_repr, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
957 PyObject_GenericGetAttr, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
958 0, /* tp_setattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
959 0, /* tp_as_buffer */
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
960 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
961 Py_TPFLAGS_BASETYPE, /* tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
962 int_doc, /* tp_doc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
963 0, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
964 0, /* tp_clear */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
965 0, /* tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
966 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
967 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
968 0, /* tp_iternext */
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
969 int_methods, /* tp_methods */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
970 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
971 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
972 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
973 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
974 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
975 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
976 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
977 0, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
978 0, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
979 int_new, /* tp_new */
23279
200559fcc664 Make sure that tp_free frees the int the same way as tp_dealloc would.
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
980 (freefunc)int_free, /* tp_free */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
981 };
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
982
26889
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
983 int
26923
dbfc3ef7d003 Since the *_Init() are private, prefix with _, suggested by Skip
Neal Norwitz <nnorwitz@gmail.com>
parents: 26889
diff changeset
984 _PyInt_Init(void)
26889
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
985 {
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
986 PyIntObject *v;
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
987 int ival;
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
988 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
989 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
990 if ((free_list = fill_free_list()) == NULL)
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
991 return 0;
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
992 /* PyObject_New is inlined */
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
993 v = free_list;
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
994 free_list = (PyIntObject *)v->ob_type;
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
995 PyObject_INIT(v, &PyInt_Type);
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
996 v->ob_ival = ival;
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
997 small_ints[ival + NSMALLNEGINTS] = v;
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
998 }
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
999 #endif
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1000 return 1;
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1001 }
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1002
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1003 void
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
1004 PyInt_Fini(void)
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1005 {
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1006 PyIntObject *p;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1007 PyIntBlock *list, *next;
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1008 int i;
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1009 int bc, bf; /* block count, number of freed blocks */
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1010 int irem, isum; /* remaining unfreed ints per block, total */
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1011
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1012 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1013 PyIntObject **q;
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1014
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1015 i = NSMALLNEGINTS + NSMALLPOSINTS;
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1016 q = small_ints;
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1017 while (--i >= 0) {
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1018 Py_XDECREF(*q);
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1019 *q++ = NULL;
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1020 }
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1021 #endif
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1022 bc = 0;
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1023 bf = 0;
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1024 isum = 0;
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1025 list = block_list;
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1026 block_list = NULL;
10431
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1027 free_list = NULL;
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1028 while (list != NULL) {
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1029 bc++;
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1030 irem = 0;
10431
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1031 for (i = 0, p = &list->objects[0];
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1032 i < N_INTOBJECTS;
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1033 i++, p++) {
19865
f2a3009b528e Replace a few places where X->ob_type was compared to &PyXXX_Type with
Guido van Rossum <guido@python.org>
parents: 19719
diff changeset
1034 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1035 irem++;
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1036 }
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1037 next = list->next;
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1038 if (irem) {
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1039 list->next = block_list;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1040 block_list = list;
10431
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1041 for (i = 0, p = &list->objects[0];
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1042 i < N_INTOBJECTS;
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1043 i++, p++) {
19865
f2a3009b528e Replace a few places where X->ob_type was compared to &PyXXX_Type with
Guido van Rossum <guido@python.org>
parents: 19719
diff changeset
1044 if (!PyInt_CheckExact(p) ||
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1045 p->ob_refcnt == 0) {
10431
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1046 p->ob_type = (struct _typeobject *)
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1047 free_list;
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1048 free_list = p;
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1049 }
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1050 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1051 else if (-NSMALLNEGINTS <= p->ob_ival &&
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1052 p->ob_ival < NSMALLPOSINTS &&
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1053 small_ints[p->ob_ival +
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1054 NSMALLNEGINTS] == NULL) {
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1055 Py_INCREF(p);
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1056 small_ints[p->ob_ival +
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1057 NSMALLNEGINTS] = p;
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1058 }
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1059 #endif
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1060 }
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1061 }
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1062 else {
23299
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
1063 PyMem_FREE(list);
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1064 bf++;
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1065 }
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1066 isum += irem;
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1067 list = next;
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1068 }
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1069 if (!Py_VerboseFlag)
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1070 return;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1071 fprintf(stderr, "# cleanup ints");
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1072 if (!isum) {
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1073 fprintf(stderr, "\n");
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1074 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1075 else {
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1076 fprintf(stderr,
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1077 ": %d unfreed int%s in %d out of %d block%s\n",
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1078 isum, isum == 1 ? "" : "s",
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1079 bc - bf, bc, bc == 1 ? "" : "s");
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1080 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1081 if (Py_VerboseFlag > 1) {
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1082 list = block_list;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1083 while (list != NULL) {
10431
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1084 for (i = 0, p = &list->objects[0];
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1085 i < N_INTOBJECTS;
0789e0b55587 Fix a problem with Vladimir's PyInt_Fini code: clear the free list; if
Guido van Rossum <guido@python.org>
parents: 10391
diff changeset
1086 i++, p++) {
19865
f2a3009b528e Replace a few places where X->ob_type was compared to &PyXXX_Type with
Guido van Rossum <guido@python.org>
parents: 19719
diff changeset
1087 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1088 fprintf(stderr,
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12630
diff changeset
1089 "# <int at %p, refcnt=%d, val=%ld>\n",
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12630
diff changeset
1090 p, p->ob_refcnt, p->ob_ival);
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1091 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1092 list = list->next;
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1093 }
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1094 }
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1095 }