annotate Objects/intobject.c @ 15842:f6cd36498a50 legacy-trunk

Document extensions to .pth files.
author Martin v. Löwis <martin@v.loewis.de>
date Sat, 13 Jan 2001 09:54:41 +0000
parents 2dcc4a86a876
children 2edf5ed30583
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
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 /* Standard Booleans */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
14
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
15 PyIntObject _Py_ZeroStruct = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
16 PyObject_HEAD_INIT(&PyInt_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 0
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 };
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
19
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
20 PyIntObject _Py_TrueStruct = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
21 PyObject_HEAD_INIT(&PyInt_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 1
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
25 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
26 err_ovf(char *msg)
11
bcea6c822906 New error handling.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
27 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
28 PyErr_SetString(PyExc_OverflowError, msg);
11
bcea6c822906 New error handling.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
29 return NULL;
bcea6c822906 New error handling.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
30 }
bcea6c822906 New error handling.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
31
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
32 /* Integers are quite normal objects, to make object handling uniform.
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
33 (Using odd pointers to represent integers would save much space
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
34 but require extra checks for this special case throughout the code.)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
35 Since, a typical Python program spends much of its time allocating
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
36 and deallocating integers, these operations should be very fast.
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
37 Therefore we use a dedicated allocation scheme with a much lower
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
38 overhead (in space and time) than straight malloc(): a simple
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
39 dedicated free list, filled when necessary with memory from malloc().
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
40 */
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 #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
43 #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
44 #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
45
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
46 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
47 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
48 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
49 };
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 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
52
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
53 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
54 static PyIntObject *free_list = NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
55
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
56 static PyIntObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
57 fill_free_list(void)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
58 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
59 PyIntObject *p, *q;
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
60 /* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
61 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
62 if (p == NULL)
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
63 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
64 ((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
65 block_list = (PyIntBlock *)p;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
66 p = &((PyIntBlock *)p)->objects[0];
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
67 q = p + N_INTOBJECTS;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
68 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
69 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
70 q->ob_type = NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
71 return p + N_INTOBJECTS - 1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
72 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
73
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
74 #ifndef NSMALLPOSINTS
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
75 #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
76 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
77 #ifndef NSMALLNEGINTS
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
78 #define NSMALLNEGINTS 1
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 #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
81 /* 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
82 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
83 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
84 -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
85 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
86 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
87 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
88 #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
89 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
90 #endif
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
91
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
92 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
93 PyInt_FromLong(long ival)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
95 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
96 #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
97 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
98 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
99 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
100 #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
101 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
102 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
103 else
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
104 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
105 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
106 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
107 }
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
108 #endif
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
109 if (free_list == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
110 if ((free_list = fill_free_list()) == NULL)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
111 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 }
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
113 /* PyObject_New is inlined */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
114 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
115 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
116 PyObject_INIT(v, &PyInt_Type);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
117 v->ob_ival = ival;
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
118 #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
119 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
120 /* save this one for a following allocation */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
121 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
122 small_ints[ival + NSMALLNEGINTS] = v;
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
123 }
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
124 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
125 return (PyObject *) v;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
126 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
127
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
128 static void
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
129 int_dealloc(PyIntObject *v)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
130 {
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
131 v->ob_type = (struct _typeobject *)free_list;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
132 free_list = 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
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 long
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
136 PyInt_AsLong(register PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
138 PyNumberMethods *nb;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
139 PyIntObject *io;
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
140 long val;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
141
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
142 if (op && PyInt_Check(op))
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
143 return PyInt_AS_LONG((PyIntObject*) op);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
144
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
145 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
146 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
147 PyErr_SetString(PyExc_TypeError, "an integer is required");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 }
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
150
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
151 io = (PyIntObject*) (*nb->nb_int) (op);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
152 if (io == NULL)
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
153 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
154 if (!PyInt_Check(io)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
155 PyErr_SetString(PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
156 "nb_int should return int object");
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
157 return -1;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
158 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
159
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
160 val = PyInt_AS_LONG(io);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
161 Py_DECREF(io);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
162
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
163 return val;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
166 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
167 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
168 {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
169 char *end;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
170 long x;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
171 char buffer[256]; /* For errors */
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
172
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
173 if ((base != 0 && base < 2) || base > 36) {
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
174 PyErr_SetString(PyExc_ValueError, "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
175 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
176 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
177
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
178 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
179 s++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
180 errno = 0;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
181 if (base == 0 && s[0] == '0')
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
182 x = (long) PyOS_strtoul(s, &end, base);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
183 else
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
184 x = PyOS_strtol(s, &end, base);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
185 if (end == s || !isalnum(end[-1]))
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
186 goto bad;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
187 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
188 end++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
189 if (*end != '\0') {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
190 bad:
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
191 sprintf(buffer, "invalid literal for int(): %.200s", s);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
192 PyErr_SetString(PyExc_ValueError, buffer);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
193 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
194 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
195 else if (errno != 0) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
196 sprintf(buffer, "int() literal too large: %.200s", s);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
197 PyErr_SetString(PyExc_ValueError, buffer);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
198 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
199 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
200 if (pend)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
201 *pend = end;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
202 return PyInt_FromLong(x);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
203 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
204
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
205 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
206 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
207 {
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
208 char buffer[256];
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
209
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
210 if (length >= sizeof(buffer)) {
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
211 PyErr_SetString(PyExc_ValueError,
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
212 "int() literal too large to convert");
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
213 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
214 }
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
215 if (PyUnicode_EncodeDecimal(s, length, buffer, 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
216 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
217 return PyInt_FromString(buffer, NULL, base);
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
218 }
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
219
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
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
222 /* 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
223 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
224 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
225
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
226 #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
227 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
228 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
229 } \
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
230 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
231 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
232 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
233 }
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
234
750
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 610
diff changeset
235 /* ARGSUSED */
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 240
diff changeset
236 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
237 int_print(PyIntObject *v, FILE *fp, int flags)
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
238 /* flags -- not used but required by interface */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 fprintf(fp, "%ld", v->ob_ival);
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 240
diff changeset
241 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
243
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
244 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
245 int_repr(PyIntObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 char buf[20];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 sprintf(buf, "%ld", v->ob_ival);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
249 return PyString_FromString(buf);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
251
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
253 int_compare(PyIntObject *v, PyIntObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 register long i = v->ob_ival;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 register long j = w->ob_ival;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 return (i < j) ? -1 : (i > j) ? 1 : 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259
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
260 /* Needed for the new style number compare slots */
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 static PyObject *
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 int_cmp(PyObject *v, PyObject *w)
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 register long a, b;
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 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
266 CONVERT_TO_LONG(w, b);
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 return PyInt_FromLong((a < b) ? -1 : (a > b) ? 1 : 0);
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 }
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
269
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
270 static long
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
271 int_hash(PyIntObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
272 {
4903
6a629d278a18 Fix overflow test for multiply to catch some cases it missed.
Guido van Rossum <guido@python.org>
parents: 4606
diff changeset
273 /* 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
274 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
275 long x = v -> ob_ival;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
276 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
277 x = -2;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
278 return x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
279 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
280
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
281 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
282 int_add(PyIntObject *v, PyIntObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 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
285 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
286 CONVERT_TO_LONG(w, b);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 x = a + b;
11
bcea6c822906 New error handling.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
288 if ((x^a) < 0 && (x^b) < 0)
471
1be2087692a2 Improved exceptions.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
289 return err_ovf("integer addition");
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
290 return PyInt_FromLong(x);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
292
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
293 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
294 int_sub(PyIntObject *v, PyIntObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 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
297 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
298 CONVERT_TO_LONG(w, b);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 x = a - b;
11
bcea6c822906 New error handling.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
300 if ((x^a) < 0 && (x^~b) < 0)
471
1be2087692a2 Improved exceptions.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
301 return err_ovf("integer subtraction");
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
302 return PyInt_FromLong(x);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
304
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
305 /*
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
306 Integer overflow checking used to be done using a double, but on 64
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
307 bit machines (where both long and double are 64 bit) this fails
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 13472
diff changeset
308 because the double doesn't have enough precision. John Tromp suggests
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
309 the following algorithm:
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
310
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
311 Suppose again we normalize a and b to be nonnegative.
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
312 Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
313 Now we test ah and bh against zero and get essentially 3 possible outcomes.
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
314
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
315 1) both ah and bh > 0 : then report overflow
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
316
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
317 2) both ah and bh = 0 : then compute a*b and report overflow if it comes out
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
318 negative
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
319
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
320 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
321 compute al*bl and report overflow if it's negative
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
322 add (ah*bl)<<32 to al*bl and report overflow if
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
323 it's negative
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
324
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
325 In case of no overflow the result is then negated if necessary.
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
326
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
327 The majority of cases will be 2), in which case this method is the same as
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
328 what I suggested before. If multiplication is expensive enough, then the
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
329 other method is faster on case 3), but also more work to program, so I
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
330 guess the above is the preferred solution.
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
331
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
332 */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
333
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
334 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
335 int_mul(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 {
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
337 long a, b, ah, bh, x, y;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
338 int s = 1;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
339
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
340 if (v->ob_type->tp_as_sequence &&
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
341 v->ob_type->tp_as_sequence->sq_repeat) {
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
342 /* sequence * 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
343 a = PyInt_AsLong(w);
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
344 return (*v->ob_type->tp_as_sequence->sq_repeat)(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
345 }
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
346 else if (w->ob_type->tp_as_sequence &&
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
347 w->ob_type->tp_as_sequence->sq_repeat) {
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
348 /* int * sequence */
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
349 a = PyInt_AsLong(v);
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
350 return (*w->ob_type->tp_as_sequence->sq_repeat)(w, 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
351 }
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
352
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
353 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
354 CONVERT_TO_LONG(w, b);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
355 ah = a >> (LONG_BIT/2);
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
356 bh = b >> (LONG_BIT/2);
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
357
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
358 /* Quick test for common case: two small positive ints */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
359
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
360 if (ah == 0 && bh == 0) {
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
361 x = a*b;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
362 if (x < 0)
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
363 goto bad;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
364 return PyInt_FromLong(x);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
365 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
366
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
367 /* Arrange that a >= b >= 0 */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
368
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
369 if (a < 0) {
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
370 a = -a;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
371 if (a < 0) {
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
372 /* Largest negative */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
373 if (b == 0 || b == 1) {
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
374 x = a*b;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
375 goto ok;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
376 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
377 else
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
378 goto bad;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
379 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
380 s = -s;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
381 ah = a >> (LONG_BIT/2);
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
382 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
383 if (b < 0) {
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
384 b = -b;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
385 if (b < 0) {
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
386 /* Largest negative */
4606
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
387 if (a == 0 || (a == 1 && s == 1)) {
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
388 x = a*b;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
389 goto ok;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
390 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
391 else
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
392 goto bad;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
393 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
394 s = -s;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
395 bh = b >> (LONG_BIT/2);
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
396 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
397
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
398 /* 1) both ah and bh > 0 : then report overflow */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
399
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
400 if (ah != 0 && bh != 0)
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
401 goto bad;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
402
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
403 /* 2) both ah and bh = 0 : then compute a*b and report
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
404 overflow if it comes out negative */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
405
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
406 if (ah == 0 && bh == 0) {
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
407 x = a*b;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
408 if (x < 0)
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
409 goto bad;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
410 return PyInt_FromLong(x*s);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
411 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
412
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
413 if (a < b) {
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
414 /* Swap */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
415 x = a;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
416 a = b;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
417 b = x;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
418 ah = bh;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
419 /* bh not used beyond this point */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
420 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
421
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
422 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
423 it's >= 2^31
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
424 compute al*bl and report overflow if it's negative
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
425 add (ah*bl)<<32 to al*bl and report overflow if
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
426 it's negative
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
427 (NB b == bl in this case, and we make a = al) */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
428
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
429 y = ah*b;
4903
6a629d278a18 Fix overflow test for multiply to catch some cases it missed.
Guido van Rossum <guido@python.org>
parents: 4606
diff changeset
430 if (y >= (1L << (LONG_BIT/2 - 1)))
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
431 goto bad;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
432 a &= (1L << (LONG_BIT/2)) - 1;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
433 x = a*b;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
434 if (x < 0)
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
435 goto bad;
4903
6a629d278a18 Fix overflow test for multiply to catch some cases it missed.
Guido van Rossum <guido@python.org>
parents: 4606
diff changeset
436 x += y << (LONG_BIT/2);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
437 if (x < 0)
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
438 goto bad;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
439 ok:
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
440 return PyInt_FromLong(x * s);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
441
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
442 bad:
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
443 return err_ovf("integer multiplication");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
445
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
446 static int
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
447 i_divmod(register long xi, register long yi,
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
448 long *p_xdivy, long *p_xmody)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 {
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
450 long xdivy, xmody;
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
451
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
452 if (yi == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
453 PyErr_SetString(PyExc_ZeroDivisionError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
454 "integer division or modulo by zero");
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
455 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 }
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
457 if (yi < 0) {
11332
fad55eec5dfc Patch by Tim Peters fixing PR#88:
Guido van Rossum <guido@python.org>
parents: 10431
diff changeset
458 if (xi < 0) {
fad55eec5dfc Patch by Tim Peters fixing PR#88:
Guido van Rossum <guido@python.org>
parents: 10431
diff changeset
459 if (yi == -1 && -xi < 0) {
fad55eec5dfc Patch by Tim Peters fixing PR#88:
Guido van Rossum <guido@python.org>
parents: 10431
diff changeset
460 /* most negative / -1 */
fad55eec5dfc Patch by Tim Peters fixing PR#88:
Guido van Rossum <guido@python.org>
parents: 10431
diff changeset
461 err_ovf("integer division");
fad55eec5dfc Patch by Tim Peters fixing PR#88:
Guido van Rossum <guido@python.org>
parents: 10431
diff changeset
462 return -1;
fad55eec5dfc Patch by Tim Peters fixing PR#88:
Guido van Rossum <guido@python.org>
parents: 10431
diff changeset
463 }
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
464 xdivy = -xi / -yi;
11332
fad55eec5dfc Patch by Tim Peters fixing PR#88:
Guido van Rossum <guido@python.org>
parents: 10431
diff changeset
465 }
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
466 else
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
467 xdivy = - (xi / -yi);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
468 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
469 else {
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
470 if (xi < 0)
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
471 xdivy = - (-xi / yi);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
472 else
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
473 xdivy = xi / yi;
229
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
474 }
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
475 xmody = xi - xdivy*yi;
4606
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
476 if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
229
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
477 xmody += yi;
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
478 xdivy -= 1;
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
479 }
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
480 *p_xdivy = xdivy;
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
481 *p_xmody = xmody;
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
482 return 0;
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
483 }
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
484
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
485 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
486 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
487 {
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
488 long xi, yi;
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
489 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
490 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
491 CONVERT_TO_LONG(y, yi);
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
492 if (i_divmod(xi, yi, &d, &m) < 0)
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
493 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
494 return PyInt_FromLong(d);
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
495 }
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
496
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
497 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
498 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
499 {
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
500 long xi, yi;
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
501 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
502 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
503 CONVERT_TO_LONG(y, yi);
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
504 if (i_divmod(xi, yi, &d, &m) < 0)
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
505 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
506 return PyInt_FromLong(m);
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
507 }
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
508
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
509 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
510 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
511 {
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
512 long xi, yi;
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
513 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
514 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
515 CONVERT_TO_LONG(y, yi);
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 if (i_divmod(xi, yi, &d, &m) < 0)
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
517 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
518 return Py_BuildValue("(ll)", d, m);
229
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
519 }
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
520
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
521 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
522 int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 {
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
524 #if 1
4606
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
525 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
526 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
527 CONVERT_TO_LONG(w, iw);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
528 if (iw < 0) {
15041
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 15016
diff changeset
529 if (iv)
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 15016
diff changeset
530 PyErr_SetString(PyExc_ValueError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
531 "cannot raise integer to a negative power");
15041
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 15016
diff changeset
532 else
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 15016
diff changeset
533 PyErr_SetString(PyExc_ZeroDivisionError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
534 "cannot raise 0 to a negative power");
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
535 return NULL;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
536 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
537 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
538 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
539 if (iz == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
540 PyErr_SetString(PyExc_ValueError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
541 "pow() arg 3 cannot be 0");
4606
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
542 return NULL;
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
543 }
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
544 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
545 /*
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
546 * XXX: The original exponentiation code stopped looping
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
547 * 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
548 * 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
549 * 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
550 * will compensate for the slight inefficiency.
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
551 * 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
552 */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
553 temp = iv;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
554 ix = 1;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
555 while (iw > 0) {
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
556 prev = ix; /* Save value for overflow check */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
557 if (iw & 1) {
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
558 ix = ix*temp;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
559 if (temp == 0)
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
560 break; /* Avoid ix / 0 */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
561 if (ix / temp != prev)
11767
1a25bc2d3272 In response to one particular complaint on edu-sig, change some error
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
562 return err_ovf("integer exponentiation");
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
563 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
564 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
565 if (iw==0) break;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
566 prev = temp;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
567 temp *= temp; /* Square the value of temp */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
568 if (prev!=0 && temp/prev!=prev)
11767
1a25bc2d3272 In response to one particular complaint on edu-sig, change some error
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
569 return err_ovf("integer exponentiation");
4606
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
570 if (iz) {
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
571 /* 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
572 ix = ix % iz;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
573 temp = temp % iz;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
574 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
575 }
4606
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
576 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
577 long div, mod;
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
578 if (i_divmod(ix, iz, &div, &mod) < 0)
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
579 return(NULL);
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
580 ix=mod;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
581 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
582 return PyInt_FromLong(ix);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
583 #else
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 register long iv, iw, ix;
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
585 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
586 CONVERT_TO_LONG(w, iw);
229
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
587 if (iw < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
588 PyErr_SetString(PyExc_ValueError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
589 "integer to the negative power");
229
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
590 return NULL;
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
591 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
592 if ((PyObject *)z != Py_None) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
593 PyErr_SetString(PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
594 "pow(int, int, int) not yet supported");
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
595 return NULL;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
596 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 ix = 1;
229
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
598 while (--iw >= 0) {
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
599 long prev = ix;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 ix = ix * iv;
229
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
601 if (iv == 0)
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
602 break; /* 0 to some power -- avoid ix / 0 */
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
603 if (ix / iv != prev)
11767
1a25bc2d3272 In response to one particular complaint on edu-sig, change some error
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
604 return err_ovf("integer exponentiation");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
606 return PyInt_FromLong(ix);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
607 #endif
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
608 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
609
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
610 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
611 int_neg(PyIntObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 register long a, x;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 a = v->ob_ival;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 x = -a;
11
bcea6c822906 New error handling.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
616 if (a < 0 && x < 0)
471
1be2087692a2 Improved exceptions.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
617 return err_ovf("integer negation");
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
618 return PyInt_FromLong(x);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
620
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
621 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
622 int_pos(PyIntObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
623 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
624 Py_INCREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
625 return (PyObject *)v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
626 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
627
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
628 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
629 int_abs(PyIntObject *v)
229
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
630 {
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
631 if (v->ob_ival >= 0)
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
632 return int_pos(v);
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
633 else
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
634 return int_neg(v);
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
635 }
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
636
240
0aad3a7b97ef Added nonzero test
Guido van Rossum <guido@python.org>
parents: 229
diff changeset
637 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
638 int_nonzero(PyIntObject *v)
240
0aad3a7b97ef Added nonzero test
Guido van Rossum <guido@python.org>
parents: 229
diff changeset
639 {
0aad3a7b97ef Added nonzero test
Guido van Rossum <guido@python.org>
parents: 229
diff changeset
640 return v->ob_ival != 0;
0aad3a7b97ef Added nonzero test
Guido van Rossum <guido@python.org>
parents: 229
diff changeset
641 }
0aad3a7b97ef Added nonzero test
Guido van Rossum <guido@python.org>
parents: 229
diff changeset
642
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
643 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
644 int_invert(PyIntObject *v)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
645 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
646 return PyInt_FromLong(~v->ob_ival);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
647 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
648
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
649 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
650 int_lshift(PyIntObject *v, PyIntObject *w)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
651 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
652 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
653 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
654 CONVERT_TO_LONG(w, b);
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
655 if (b < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
656 PyErr_SetString(PyExc_ValueError, "negative shift count");
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
657 return NULL;
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
658 }
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
659 if (a == 0 || b == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
660 Py_INCREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
661 return (PyObject *) v;
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
662 }
1365
a5fc104849aa Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
663 if (b >= LONG_BIT) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
664 return PyInt_FromLong(0L);
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
665 }
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
666 a = (unsigned long)a << b;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
667 return PyInt_FromLong(a);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
668 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
669
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
670 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
671 int_rshift(PyIntObject *v, PyIntObject *w)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
672 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
673 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
674 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
675 CONVERT_TO_LONG(w, b);
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
676 if (b < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
677 PyErr_SetString(PyExc_ValueError, "negative shift count");
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
678 return NULL;
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
679 }
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
680 if (a == 0 || b == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
681 Py_INCREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
682 return (PyObject *) v;
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
683 }
1365
a5fc104849aa Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
684 if (b >= LONG_BIT) {
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
685 if (a < 0)
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
686 a = -1;
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
687 else
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
688 a = 0;
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
689 }
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
690 else {
13423
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
691 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
692 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
693 return PyInt_FromLong(a);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
694 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
695
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
696 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
697 int_and(PyIntObject *v, PyIntObject *w)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
698 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
699 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
700 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
701 CONVERT_TO_LONG(w, b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
702 return PyInt_FromLong(a & b);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
703 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
704
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
705 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
706 int_xor(PyIntObject *v, PyIntObject *w)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
707 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
708 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
709 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
710 CONVERT_TO_LONG(w, b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
711 return PyInt_FromLong(a ^ b);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
712 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
713
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
714 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
715 int_or(PyIntObject *v, PyIntObject *w)
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 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
718 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
719 CONVERT_TO_LONG(w, b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
720 return PyInt_FromLong(a | b);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
721 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
722
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
723 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
724 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
725 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
726 Py_INCREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
727 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
728 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
729
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
730 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
731 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
732 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
733 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
734 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
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_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
738 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
739 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
740 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
741
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
742 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
743 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
744 {
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
745 char buf[100];
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
746 long x = 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
747 if (x == 0)
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
748 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
749 else
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
750 sprintf(buf, "0%lo", x);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
751 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
752 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
753
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
754 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
755 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
756 {
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
757 char buf[100];
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
758 long x = v -> ob_ival;
4937
789de7c66712 Changed hex() and oct() again, to never emit a '-' sign.
Guido van Rossum <guido@python.org>
parents: 4926
diff changeset
759 sprintf(buf, "0x%lx", x);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
760 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
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 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
764 (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
765 (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
766 (binaryfunc)int_mul, /*nb_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
767 (binaryfunc)int_div, /*nb_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
768 (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
769 (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
770 (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
771 (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
772 (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
773 (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
774 (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
775 (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
776 (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
777 (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
778 (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
779 (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
780 (binaryfunc)int_or, /*nb_or*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
781 0, /*nb_coerce*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
782 (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
783 (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
784 (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
785 (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
786 (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
787 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
788 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
789 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
790 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
791 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
792 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
793 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
794 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
795 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
796 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
797 0, /*nb_inplace_or*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
798
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
799 /* New style slots: */
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
800 (binaryfunc)int_cmp, /*nb_cmp*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
801 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
802
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
803 PyTypeObject PyInt_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
804 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
805 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
806 "int",
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
807 sizeof(PyIntObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
808 0,
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
809 (destructor)int_dealloc, /*tp_dealloc*/
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
810 (printfunc)int_print, /*tp_print*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
811 0, /*tp_getattr*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
812 0, /*tp_setattr*/
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
813 (cmpfunc)int_compare, /*tp_compare*/
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
814 (reprfunc)int_repr, /*tp_repr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
815 &int_as_number, /*tp_as_number*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
816 0, /*tp_as_sequence*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
817 0, /*tp_as_mapping*/
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
818 (hashfunc)int_hash, /*tp_hash*/
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
819 0, /*tp_call*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
820 0, /*tp_str*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
821 0, /*tp_getattro*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
822 0, /*tp_setattro*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
823 0, /*tp_as_buffer*/
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
824 Py_TPFLAGS_NEWSTYLENUMBER /*tp_flags*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
825 };
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
826
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
827 void
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
828 PyInt_Fini(void)
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
829 {
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
830 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
831 PyIntBlock *list, *next;
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
832 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
833 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
834 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
835
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
836 #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
837 PyIntObject **q;
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
838
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
839 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
840 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
841 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
842 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
843 *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
844 }
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
845 #endif
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
846 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
847 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
848 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
849 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
850 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
851 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
852 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
853 bc++;
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
854 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
855 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
856 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
857 i++, 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
858 if (PyInt_Check(p) && p->ob_refcnt != 0)
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
859 irem++;
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
860 }
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
861 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
862 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
863 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
864 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
865 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
866 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
867 i++, 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
868 if (!PyInt_Check(p) || p->ob_refcnt == 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
869 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
870 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
871 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
872 }
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
873 #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
874 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
875 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
876 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
877 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
878 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
879 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
880 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
881 }
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
882 #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
883 }
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
884 }
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
885 else {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
886 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
887 bf++;
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
888 }
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
889 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
890 list = next;
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
891 }
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
892 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
893 return;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
894 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
895 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
896 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
897 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
898 else {
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
899 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
900 ": %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
901 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
902 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
903 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
904 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
905 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
906 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
907 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
908 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
909 i++, p++) {
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
910 if (PyInt_Check(p) && p->ob_refcnt != 0)
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
911 fprintf(stderr,
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12630
diff changeset
912 "# <int at %p, refcnt=%d, val=%ld>\n",
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 12630
diff changeset
913 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
914 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
915 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
916 }
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
917 }
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
918 }