annotate Objects/intobject.c @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents c4e60988834d
children a184227cfb94
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>
53289
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
6 #include <float.h>
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
8 static PyObject *int_int(PyIntObject *v);
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
9
1448
328f446d2016 Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
10 long
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
11 PyInt_GetMax(void)
1448
328f446d2016 Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
12 {
328f446d2016 Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
13 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
14 }
328f446d2016 Added getmaxint() so sys can initialize sys.maxint.
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
15
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
16 /* Integers are quite normal objects, to make object handling uniform.
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
17 (Using odd pointers to represent integers would save much space
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
18 but require extra checks for this special case throughout the code.)
23299
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
19 Since a typical Python program spends much of its time allocating
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
20 and deallocating integers, these operations should be very fast.
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
21 Therefore we use a dedicated allocation scheme with a much lower
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
22 overhead (in space and time) than straight malloc(): a simple
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
23 dedicated free list, filled when necessary with memory from malloc().
23299
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
24
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
25 block_list is a singly-linked list of all PyIntBlocks ever allocated,
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
26 linked via their next members. PyIntBlocks are never returned to the
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
27 system before shutdown (PyInt_Fini).
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
28
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
29 free_list is a singly-linked list of available PyIntObjects, linked
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
30 via abuse of their ob_type members.
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
31 */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
32
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
33 #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
34 #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
35 #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
36
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
37 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
38 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
39 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
40 };
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
41
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
42 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
43
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
44 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
45 static PyIntObject *free_list = NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
46
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
47 static PyIntObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
48 fill_free_list(void)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
49 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
50 PyIntObject *p, *q;
23299
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
51 /* Python's object allocator isn't appropriate for large blocks. */
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
52 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
53 if (p == NULL)
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
54 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
55 ((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
56 block_list = (PyIntBlock *)p;
23299
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
57 /* Link the int objects together, from rear to front, then return
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
58 the address of the last int object in the block. */
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
59 p = &((PyIntBlock *)p)->objects[0];
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
60 q = p + N_INTOBJECTS;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
61 while (--q > p)
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
62 Py_TYPE(q) = (struct _typeobject *)(q-1);
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
63 Py_TYPE(q) = NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
64 return p + N_INTOBJECTS - 1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
65 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
66
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
67 #ifndef NSMALLPOSINTS
36645
5a836986206d RFE #1436243: make integers in [0..256] preallocated.
Georg Brandl <georg@python.org>
parents: 36505
diff changeset
68 #define NSMALLPOSINTS 257
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
69 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
70 #ifndef NSMALLNEGINTS
26889
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
71 #define NSMALLNEGINTS 5
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
72 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
73 #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
74 /* 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
75 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
76 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
77 -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
78 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
79 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
80 #endif
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
81 #ifdef COUNT_ALLOCS
50627
f0e0c3536e22 Issue #4850: Change COUNT_ALLOCS variables to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 50205
diff changeset
82 Py_ssize_t quick_int_allocs;
f0e0c3536e22 Issue #4850: Change COUNT_ALLOCS variables to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 50205
diff changeset
83 Py_ssize_t quick_neg_int_allocs;
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
84 #endif
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
85
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
86 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
87 PyInt_FromLong(long ival)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
89 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
90 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
26889
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
91 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
92 v = small_ints[ival + NSMALLNEGINTS];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
93 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
94 #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
95 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
96 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
97 else
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
98 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
99 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
100 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
101 }
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
102 #endif
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
103 if (free_list == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
104 if ((free_list = fill_free_list()) == NULL)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
105 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 }
24898
a869d2de536f Call me anal, but there was a particular phrase that was speading to
Guido van Rossum <guido@python.org>
parents: 24831
diff changeset
107 /* Inline PyObject_New */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
108 v = free_list;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
109 free_list = (PyIntObject *)Py_TYPE(v);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
110 PyObject_INIT(v, &PyInt_Type);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
111 v->ob_ival = ival;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
112 return (PyObject *) v;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
113 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
114
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
115 PyObject *
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
116 PyInt_FromSize_t(size_t ival)
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
117 {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
118 if (ival <= LONG_MAX)
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
119 return PyInt_FromLong((long)ival);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
120 return _PyLong_FromSize_t(ival);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
121 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
122
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
123 PyObject *
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
124 PyInt_FromSsize_t(Py_ssize_t ival)
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
125 {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
126 if (ival >= LONG_MIN && ival <= LONG_MAX)
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
127 return PyInt_FromLong((long)ival);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
128 return _PyLong_FromSsize_t(ival);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
129 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
130
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
131 static void
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
132 int_dealloc(PyIntObject *v)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 39
diff changeset
133 {
19865
f2a3009b528e Replace a few places where X->ob_type was compared to &PyXXX_Type with
Guido van Rossum <guido@python.org>
parents: 19719
diff changeset
134 if (PyInt_CheckExact(v)) {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
135 Py_TYPE(v) = (struct _typeobject *)free_list;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
136 free_list = v;
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
137 }
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
138 else
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
139 Py_TYPE(v)->tp_free((PyObject *)v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
23279
200559fcc664 Make sure that tp_free frees the int the same way as tp_dealloc would.
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
142 static void
200559fcc664 Make sure that tp_free frees the int the same way as tp_dealloc would.
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
143 int_free(PyIntObject *v)
200559fcc664 Make sure that tp_free frees the int the same way as tp_dealloc would.
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
144 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
145 Py_TYPE(v) = (struct _typeobject *)free_list;
23279
200559fcc664 Make sure that tp_free frees the int the same way as tp_dealloc would.
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
146 free_list = v;
200559fcc664 Make sure that tp_free frees the int the same way as tp_dealloc would.
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
147 }
200559fcc664 Make sure that tp_free frees the int the same way as tp_dealloc would.
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
148
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 long
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
150 PyInt_AsLong(register PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
152 PyNumberMethods *nb;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
153 PyIntObject *io;
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
154 long val;
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
155
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
156 if (op && PyInt_Check(op))
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
157 return PyInt_AS_LONG((PyIntObject*) op);
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
158
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
159 if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
160 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
161 PyErr_SetString(PyExc_TypeError, "an integer is required");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 }
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
164
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
165 io = (PyIntObject*) (*nb->nb_int) (op);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
166 if (io == NULL)
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
167 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
168 if (!PyInt_Check(io)) {
26291
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
169 if (PyLong_Check(io)) {
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
170 /* got a long? => retry int conversion */
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
171 val = PyLong_AsLong((PyObject *)io);
27993
beea4f21c9f0 Strange control flow in PyInt_AsLong. When nb_int is called inside
Thomas Heller <theller@ctypes.org>
parents: 27851
diff changeset
172 Py_DECREF(io);
beea4f21c9f0 Strange control flow in PyInt_AsLong. When nb_int is called inside
Thomas Heller <theller@ctypes.org>
parents: 27851
diff changeset
173 if ((val == -1) && PyErr_Occurred())
26291
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
174 return -1;
27993
beea4f21c9f0 Strange control flow in PyInt_AsLong. When nb_int is called inside
Thomas Heller <theller@ctypes.org>
parents: 27851
diff changeset
175 return val;
26291
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
176 }
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
177 else
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
178 {
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
179 Py_DECREF(io);
26291
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
180 PyErr_SetString(PyExc_TypeError,
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
181 "nb_int should return int object");
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
182 return -1;
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
183 }
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
184 }
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
185
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
186 val = PyInt_AS_LONG(io);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
187 Py_DECREF(io);
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
188
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
189 return val;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
192 Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
193 PyInt_AsSsize_t(register PyObject *op)
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
194 {
36505
5825c6ace28b Avoid unused variables when SIZEOF_SIZE_T == SIZEOF_LONG. Also normalize
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
195 #if SIZEOF_SIZE_T != SIZEOF_LONG
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
196 PyNumberMethods *nb;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
197 PyIntObject *io;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
198 Py_ssize_t val;
36505
5825c6ace28b Avoid unused variables when SIZEOF_SIZE_T == SIZEOF_LONG. Also normalize
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
199 #endif
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37815
diff changeset
200
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37815
diff changeset
201 if (op == NULL) {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37815
diff changeset
202 PyErr_SetString(PyExc_TypeError, "an integer is required");
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37815
diff changeset
203 return -1;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37815
diff changeset
204 }
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37815
diff changeset
205
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37815
diff changeset
206 if (PyInt_Check(op))
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37815
diff changeset
207 return PyInt_AS_LONG((PyIntObject*) op);
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37815
diff changeset
208 if (PyLong_Check(op))
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
209 return _PyLong_AsSsize_t(op);
36505
5825c6ace28b Avoid unused variables when SIZEOF_SIZE_T == SIZEOF_LONG. Also normalize
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
210 #if SIZEOF_SIZE_T == SIZEOF_LONG
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
211 return PyInt_AsLong(op);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
212 #else
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
213
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
214 if ((nb = Py_TYPE(op)->tp_as_number) == NULL ||
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
215 (nb->nb_int == NULL && nb->nb_long == 0)) {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
216 PyErr_SetString(PyExc_TypeError, "an integer is required");
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
217 return -1;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
218 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
219
41634
54179538a387 As per Armin Rigo's suggestion, remove special handing from intobject.c to deal with the peculiarities of classobject's implementation of the number protocol. The nb_long method of classobject now falls back to nb_int if there is no __long__ attribute present.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
220 if (nb->nb_long != 0)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
221 io = (PyIntObject*) (*nb->nb_long) (op);
41634
54179538a387 As per Armin Rigo's suggestion, remove special handing from intobject.c to deal with the peculiarities of classobject's implementation of the number protocol. The nb_long method of classobject now falls back to nb_int if there is no __long__ attribute present.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41590
diff changeset
222 else
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
223 io = (PyIntObject*) (*nb->nb_int) (op);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
224 if (io == NULL)
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
225 return -1;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
226 if (!PyInt_Check(io)) {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
227 if (PyLong_Check(io)) {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
228 /* got a long? => retry int conversion */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
229 val = _PyLong_AsSsize_t((PyObject *)io);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
230 Py_DECREF(io);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
231 if ((val == -1) && PyErr_Occurred())
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
232 return -1;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
233 return val;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
234 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
235 else
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
236 {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
237 Py_DECREF(io);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
238 PyErr_SetString(PyExc_TypeError,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
239 "nb_int should return int object");
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
240 return -1;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
241 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
242 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
243
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
244 val = PyInt_AS_LONG(io);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
245 Py_DECREF(io);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
246
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
247 return val;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
248 #endif
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
249 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
250
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
251 unsigned long
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
252 PyInt_AsUnsignedLongMask(register PyObject *op)
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
253 {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
254 PyNumberMethods *nb;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
255 PyIntObject *io;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
256 unsigned long val;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
257
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
258 if (op && PyInt_Check(op))
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
259 return PyInt_AS_LONG((PyIntObject*) op);
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
260 if (op && PyLong_Check(op))
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
261 return PyLong_AsUnsignedLongMask(op);
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
262
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
263 if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
264 nb->nb_int == NULL) {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
265 PyErr_SetString(PyExc_TypeError, "an integer is required");
37815
20cb13bdf1f0 C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents: 37611
diff changeset
266 return (unsigned long)-1;
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
267 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
268
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
269 io = (PyIntObject*) (*nb->nb_int) (op);
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
270 if (io == NULL)
37815
20cb13bdf1f0 C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents: 37611
diff changeset
271 return (unsigned long)-1;
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
272 if (!PyInt_Check(io)) {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
273 if (PyLong_Check(io)) {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
274 val = PyLong_AsUnsignedLongMask((PyObject *)io);
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
275 Py_DECREF(io);
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
276 if (PyErr_Occurred())
37815
20cb13bdf1f0 C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents: 37611
diff changeset
277 return (unsigned long)-1;
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
278 return val;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
279 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
280 else
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
281 {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
282 Py_DECREF(io);
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
283 PyErr_SetString(PyExc_TypeError,
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
284 "nb_int should return int object");
37815
20cb13bdf1f0 C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents: 37611
diff changeset
285 return (unsigned long)-1;
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
286 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
287 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
288
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
289 val = PyInt_AS_LONG(io);
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
290 Py_DECREF(io);
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
291
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
292 return val;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
293 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
294
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
295 #ifdef HAVE_LONG_LONG
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
296 unsigned PY_LONG_LONG
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
297 PyInt_AsUnsignedLongLongMask(register PyObject *op)
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
298 {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
299 PyNumberMethods *nb;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
300 PyIntObject *io;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
301 unsigned PY_LONG_LONG val;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
302
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
303 if (op && PyInt_Check(op))
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
304 return PyInt_AS_LONG((PyIntObject*) op);
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
305 if (op && PyLong_Check(op))
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
306 return PyLong_AsUnsignedLongLongMask(op);
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
307
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
308 if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
309 nb->nb_int == NULL) {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
310 PyErr_SetString(PyExc_TypeError, "an integer is required");
37815
20cb13bdf1f0 C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents: 37611
diff changeset
311 return (unsigned PY_LONG_LONG)-1;
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
312 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
313
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
314 io = (PyIntObject*) (*nb->nb_int) (op);
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
315 if (io == NULL)
37815
20cb13bdf1f0 C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents: 37611
diff changeset
316 return (unsigned PY_LONG_LONG)-1;
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
317 if (!PyInt_Check(io)) {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
318 if (PyLong_Check(io)) {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
319 val = PyLong_AsUnsignedLongLongMask((PyObject *)io);
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
320 Py_DECREF(io);
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
321 if (PyErr_Occurred())
37815
20cb13bdf1f0 C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents: 37611
diff changeset
322 return (unsigned PY_LONG_LONG)-1;
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
323 return val;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
324 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
325 else
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
326 {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
327 Py_DECREF(io);
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
328 PyErr_SetString(PyExc_TypeError,
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
329 "nb_int should return int object");
37815
20cb13bdf1f0 C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents: 37611
diff changeset
330 return (unsigned PY_LONG_LONG)-1;
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
331 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
332 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
333
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
334 val = PyInt_AS_LONG(io);
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
335 Py_DECREF(io);
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
336
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
337 return val;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
338 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
339 #endif
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 27993
diff changeset
340
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
341 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
342 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
343 {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
344 char *end;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
345 long x;
37611
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
346 Py_ssize_t slen;
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
347 PyObject *sobj, *srepr;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
348
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
349 if ((base != 0 && base < 2) || base > 36) {
27851
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
350 PyErr_SetString(PyExc_ValueError,
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
351 "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
352 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
353 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
354
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
355 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
356 s++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
357 errno = 0;
27851
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
358 if (base == 0 && s[0] == '0') {
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
359 x = (long) PyOS_strtoul(s, &end, base);
27851
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
360 if (x < 0)
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 30131
diff changeset
361 return PyLong_FromString(s, pend, base);
27851
7a20bbdbff36 Issue a warning when int('0...', 0) returns an int with the sign
Guido van Rossum <guido@python.org>
parents: 27779
diff changeset
362 }
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
363 else
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
364 x = PyOS_strtol(s, &end, base);
16997
86fd723f2928 Use Py_CHARMASK for ctype macros. Fixes bug #232787.
Martin v. Löwis <martin@v.loewis.de>
parents: 15945
diff changeset
365 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
366 goto bad;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
367 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
368 end++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
369 if (*end != '\0') {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
370 bad:
37611
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
371 slen = strlen(s) < 200 ? strlen(s) : 200;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
372 sobj = PyString_FromStringAndSize(s, slen);
37611
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
373 if (sobj == NULL)
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
374 return NULL;
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
375 srepr = PyObject_Repr(sobj);
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
376 Py_DECREF(sobj);
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
377 if (srepr == NULL)
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
378 return NULL;
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
379 PyErr_Format(PyExc_ValueError,
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
380 "invalid literal for int() with base %d: %s",
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
381 base, PyString_AS_STRING(srepr));
37611
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
382 Py_DECREF(srepr);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
383 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
384 }
33256
6641d0542592 Stop producing or using OverflowWarning. PEP 237 thought this would
Tim Peters <tim.peters@gmail.com>
parents: 32759
diff changeset
385 else if (errno != 0)
26081
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
386 return PyLong_FromString(s, pend, base);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
387 if (pend)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
388 *pend = end;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
389 return PyInt_FromLong(x);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
390 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11332
diff changeset
391
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
392 #ifdef Py_USING_UNICODE
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
393 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
394 PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t 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
395 {
26081
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
396 PyObject *result;
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37304
diff changeset
397 char *buffer = (char *)PyMem_MALLOC(length+1);
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
398
26081
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
399 if (buffer == NULL)
41733
0ad6951c18f7 Port rev 55353 from Guido:
Neal Norwitz <nnorwitz@gmail.com>
parents: 41634
diff changeset
400 return PyErr_NoMemory();
26081
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
401
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
402 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
403 PyMem_FREE(buffer);
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
404 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
405 }
26081
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
406 result = PyInt_FromString(buffer, NULL, base);
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
407 PyMem_FREE(buffer);
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25217
diff changeset
408 return result;
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
409 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
410 #endif
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
411
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
413
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
414 /* 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
415 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
416 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
417
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
418 #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
419 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
420 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
421 } \
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
422 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
423 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
424 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
425 }
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
426
750
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 610
diff changeset
427 /* ARGSUSED */
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 240
diff changeset
428 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
429 int_print(PyIntObject *v, FILE *fp, int flags)
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
430 /* flags -- not used but required by interface */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 {
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
432 long int_val = v->ob_ival;
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
433 Py_BEGIN_ALLOW_THREADS
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
434 fprintf(fp, "%ld", int_val);
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
435 Py_END_ALLOW_THREADS
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 240
diff changeset
436 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
438
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
440 int_compare(PyIntObject *v, PyIntObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 register long i = v->ob_ival;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 register long j = w->ob_ival;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 return (i < j) ? -1 : (i > j) ? 1 : 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
446
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
447 static long
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
448 int_hash(PyIntObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
449 {
4903
6a629d278a18 Fix overflow test for multiply to catch some cases it missed.
Guido van Rossum <guido@python.org>
parents: 4606
diff changeset
450 /* 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
451 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
452 long x = v -> ob_ival;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
453 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
454 x = -2;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
455 return x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
456 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
457
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
458 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
459 int_add(PyIntObject *v, PyIntObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 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
462 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
463 CONVERT_TO_LONG(w, b);
57533
8f6eee42f810 Issue #7406: Fix some occurrences of potential signed overflow in int
Mark Dickinson <dickinsm@gmail.com>
parents: 57300
diff changeset
464 /* casts in the line below avoid undefined behaviour on overflow */
8f6eee42f810 Issue #7406: Fix some occurrences of potential signed overflow in int
Mark Dickinson <dickinsm@gmail.com>
parents: 57300
diff changeset
465 x = (long)((unsigned long)a + b);
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
466 if ((x^a) >= 0 || (x^b) >= 0)
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
467 return PyInt_FromLong(x);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
468 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
470
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
471 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
472 int_sub(PyIntObject *v, PyIntObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 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
475 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
476 CONVERT_TO_LONG(w, b);
57533
8f6eee42f810 Issue #7406: Fix some occurrences of potential signed overflow in int
Mark Dickinson <dickinsm@gmail.com>
parents: 57300
diff changeset
477 /* casts in the line below avoid undefined behaviour on overflow */
8f6eee42f810 Issue #7406: Fix some occurrences of potential signed overflow in int
Mark Dickinson <dickinsm@gmail.com>
parents: 57300
diff changeset
478 x = (long)((unsigned long)a - b);
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
479 if ((x^a) >= 0 || (x^~b) >= 0)
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
480 return PyInt_FromLong(x);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
481 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
482 (PyObject *)w);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
484
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
485 /*
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
486 Integer overflow checking for * is painful: Python tried a couple ways, but
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
487 they didn't work on all platforms, or failed in endcases (a product of
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
488 -sys.maxint-1 has been a particular pain).
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
489
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
490 Here's another way:
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
491
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
492 The native long product x*y is either exactly right or *way* off, being
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
493 just the last n bits of the true product, where n is the number of bits
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
494 in a long (the delivered product is the true product plus i*2**n for
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
495 some integer i).
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
496
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
497 The native double product (double)x * (double)y is subject to three
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
498 rounding errors: on a sizeof(long)==8 box, each cast to double can lose
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
499 info, and even on a sizeof(long)==4 box, the multiplication can lose info.
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
500 But, unlike the native long product, it's not in *range* trouble: even
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
501 if sizeof(long)==32 (256-bit longs), the product easily fits in the
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
502 dynamic range of a double. So the leading 50 (or so) bits of the double
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
503 product are correct.
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
504
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
505 We check these two ways against each other, and declare victory if they're
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
506 approximately the same. Else, because the native long product is the only
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
507 one that can lose catastrophic amounts of information, it's the native long
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
508 product that must have overflowed.
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
509 */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
510
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
511 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
512 int_mul(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 {
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
514 long a, b;
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
515 long longprod; /* a*b in native long arithmetic */
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
516 double doubled_longprod; /* (double)longprod */
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
517 double doubleprod; /* (double)a * (double)b */
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
518
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
519 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
520 CONVERT_TO_LONG(w, b);
57533
8f6eee42f810 Issue #7406: Fix some occurrences of potential signed overflow in int
Mark Dickinson <dickinsm@gmail.com>
parents: 57300
diff changeset
521 /* casts in the next line avoid undefined behaviour on overflow */
8f6eee42f810 Issue #7406: Fix some occurrences of potential signed overflow in int
Mark Dickinson <dickinsm@gmail.com>
parents: 57300
diff changeset
522 longprod = (long)((unsigned long)a * b);
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
523 doubleprod = (double)a * (double)b;
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
524 doubled_longprod = (double)longprod;
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
525
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
526 /* Fast path for normal case: small multiplicands, and no info
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
527 is lost in either method. */
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
528 if (doubled_longprod == doubleprod)
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
529 return PyInt_FromLong(longprod);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
530
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
531 /* Somebody somewhere lost info. Close enough, or way off? Note
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
532 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
533 The difference either is or isn't significant compared to the
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
534 true value (of which doubleprod is a good approximation).
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
535 */
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
536 {
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
537 const double diff = doubled_longprod - doubleprod;
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
538 const double absdiff = diff >= 0.0 ? diff : -diff;
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
539 const double absprod = doubleprod >= 0.0 ? doubleprod :
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
540 -doubleprod;
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
541 /* absdiff/absprod <= 1/32 iff
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
542 32 * absdiff <= absprod -- 5 good bits is "close enough" */
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
543 if (32.0 * absdiff <= absprod)
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
544 return PyInt_FromLong(longprod);
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
545 else
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
546 return PyLong_Type.tp_as_number->nb_multiply(v, w);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
547 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
549
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
550 /* Integer overflow checking for unary negation: on a 2's-complement
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
551 * box, -x overflows iff x is the most negative long. In this case we
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
552 * get -x == x. However, -x is undefined (by C) if x /is/ the most
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
553 * negative long (it's a signed overflow case), and some compilers care.
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
554 * So we cast x to unsigned long first. However, then other compilers
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
555 * warn about applying unary minus to an unsigned operand. Hence the
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
556 * weird "0-".
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
557 */
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
558 #define UNARY_NEG_WOULD_OVERFLOW(x) \
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
559 ((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x))
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
560
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
561 /* Return type of i_divmod */
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
562 enum divmod_result {
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
563 DIVMOD_OK, /* Correct result */
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
564 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
565 DIVMOD_ERROR /* Exception raised */
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
566 };
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
567
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
568 static enum divmod_result
18146
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
569 i_divmod(register long x, register long y,
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
570 long *p_xdivy, long *p_xmody)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 {
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
572 long xdivy, xmody;
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
573
18146
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
574 if (y == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
575 PyErr_SetString(PyExc_ZeroDivisionError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
576 "integer division or modulo by zero");
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
577 return DIVMOD_ERROR;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 }
18146
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
579 /* (-sys.maxint-1)/-1 is the only overflow case. */
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
580 if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x))
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
581 return DIVMOD_OVERFLOW;
18146
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
582 xdivy = x / y;
57567
040c38ed2251 Avoid undefined behaviour due to overflow in i_divmod (Objects/intobject.c).
Mark Dickinson <dickinsm@gmail.com>
parents: 57533
diff changeset
583 /* xdiv*y can overflow on platforms where x/y gives floor(x/y)
040c38ed2251 Avoid undefined behaviour due to overflow in i_divmod (Objects/intobject.c).
Mark Dickinson <dickinsm@gmail.com>
parents: 57533
diff changeset
584 * for x and y with differing signs. (This is unusual
040c38ed2251 Avoid undefined behaviour due to overflow in i_divmod (Objects/intobject.c).
Mark Dickinson <dickinsm@gmail.com>
parents: 57533
diff changeset
585 * behaviour, and C99 prohibits it, but it's allowed by C89;
040c38ed2251 Avoid undefined behaviour due to overflow in i_divmod (Objects/intobject.c).
Mark Dickinson <dickinsm@gmail.com>
parents: 57533
diff changeset
586 * for an example of overflow, take x = LONG_MIN, y = 5 or x =
040c38ed2251 Avoid undefined behaviour due to overflow in i_divmod (Objects/intobject.c).
Mark Dickinson <dickinsm@gmail.com>
parents: 57533
diff changeset
587 * LONG_MAX, y = -5.) However, x - xdivy*y is always
040c38ed2251 Avoid undefined behaviour due to overflow in i_divmod (Objects/intobject.c).
Mark Dickinson <dickinsm@gmail.com>
parents: 57533
diff changeset
588 * representable as a long, since it lies strictly between
040c38ed2251 Avoid undefined behaviour due to overflow in i_divmod (Objects/intobject.c).
Mark Dickinson <dickinsm@gmail.com>
parents: 57533
diff changeset
589 * -abs(y) and abs(y). We add casts to avoid intermediate
040c38ed2251 Avoid undefined behaviour due to overflow in i_divmod (Objects/intobject.c).
Mark Dickinson <dickinsm@gmail.com>
parents: 57533
diff changeset
590 * overflow.
040c38ed2251 Avoid undefined behaviour due to overflow in i_divmod (Objects/intobject.c).
Mark Dickinson <dickinsm@gmail.com>
parents: 57533
diff changeset
591 */
040c38ed2251 Avoid undefined behaviour due to overflow in i_divmod (Objects/intobject.c).
Mark Dickinson <dickinsm@gmail.com>
parents: 57533
diff changeset
592 xmody = (long)(x - (unsigned long)xdivy * y);
18146
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
593 /* If the signs of x and y differ, and the remainder is non-0,
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
594 * C89 doesn't define whether xdivy is now the floor or the
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
595 * ceiling of the infinitely precise quotient. We want the floor,
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
596 * and we have it iff the remainder's sign matches y's.
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
597 */
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
598 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
599 xmody += y;
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
600 --xdivy;
b51b4c2bf5f9 SF bug 434186: 0x80000000/2 != 0x80000000>>1
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
601 assert(xmody && ((y ^ xmody) >= 0));
229
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
602 }
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
603 *p_xdivy = xdivy;
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
604 *p_xmody = xmody;
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
605 return DIVMOD_OK;
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
606 }
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
607
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
608 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
609 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
610 {
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
611 long xi, yi;
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
612 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
613 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
614 CONVERT_TO_LONG(y, yi);
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
615 switch (i_divmod(xi, yi, &d, &m)) {
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
616 case DIVMOD_OK:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
617 return PyInt_FromLong(d);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
618 case DIVMOD_OVERFLOW:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
619 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
620 (PyObject *)y);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
621 default:
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
622 return NULL;
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
623 }
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
624 }
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
625
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
626 static PyObject *
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
627 int_classic_div(PyIntObject *x, PyIntObject *y)
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
628 {
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
629 long xi, yi;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
630 long d, m;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
631 CONVERT_TO_LONG(x, xi);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
632 CONVERT_TO_LONG(y, yi);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
633 if (Py_DivisionWarningFlag &&
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
634 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
635 return NULL;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
636 switch (i_divmod(xi, yi, &d, &m)) {
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
637 case DIVMOD_OK:
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
638 return PyInt_FromLong(d);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
639 case DIVMOD_OVERFLOW:
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
640 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
641 (PyObject *)y);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
642 default:
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
643 return NULL;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
644 }
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
645 }
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
646
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
647 static PyObject *
57941
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
648 int_true_divide(PyIntObject *x, PyIntObject *y)
19676
c768d3191df1 Move int_true_divide next to the other division routines.
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
649 {
57941
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
650 long xi, yi;
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19676
diff changeset
651 /* If they aren't both ints, give someone else a chance. In
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19676
diff changeset
652 particular, this lets int/long get handled by longs, which
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19676
diff changeset
653 underflows to 0 gracefully if the long is too big to convert
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19676
diff changeset
654 to float. */
57941
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
655 CONVERT_TO_LONG(x, xi);
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
656 CONVERT_TO_LONG(y, yi);
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
657 if (yi == 0) {
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
658 PyErr_SetString(PyExc_ZeroDivisionError,
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
659 "division by zero");
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
660 return NULL;
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
661 }
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
662 if (xi == 0)
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
663 return PyFloat_FromDouble(yi < 0 ? -0.0 : 0.0);
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
664
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
665 #define WIDTH_OF_ULONG (CHAR_BIT*SIZEOF_LONG)
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
666 #if DBL_MANT_DIG < WIDTH_OF_ULONG
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
667 if ((xi >= 0 ? 0UL + xi : 0UL - xi) >> DBL_MANT_DIG ||
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
668 (yi >= 0 ? 0UL + yi : 0UL - yi) >> DBL_MANT_DIG)
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
669 /* Large x or y. Use long integer arithmetic. */
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
670 return PyLong_Type.tp_as_number->nb_true_divide(
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
671 (PyObject *)x, (PyObject *)y);
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
672 else
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
673 #endif
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
674 /* Both ints can be exactly represented as doubles. Do a
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
675 floating-point division. */
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
676 return PyFloat_FromDouble((double)xi / (double)yi);
19676
c768d3191df1 Move int_true_divide next to the other division routines.
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
677 }
c768d3191df1 Move int_true_divide next to the other division routines.
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
678
c768d3191df1 Move int_true_divide next to the other division routines.
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
679 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
680 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
681 {
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
682 long xi, yi;
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
683 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
684 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
685 CONVERT_TO_LONG(y, yi);
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
686 switch (i_divmod(xi, yi, &d, &m)) {
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
687 case DIVMOD_OK:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
688 return PyInt_FromLong(m);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
689 case DIVMOD_OVERFLOW:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
690 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
691 (PyObject *)y);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
692 default:
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
693 return NULL;
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
694 }
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
695 }
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
696
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
697 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
698 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
699 {
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 long xi, yi;
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
701 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
702 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
703 CONVERT_TO_LONG(y, yi);
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
704 switch (i_divmod(xi, yi, &d, &m)) {
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
705 case DIVMOD_OK:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
706 return Py_BuildValue("(ll)", d, m);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
707 case DIVMOD_OVERFLOW:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
708 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
709 (PyObject *)y);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
710 default:
610
dcc6de1c37a4 Make / and % do the same as divmod.
Guido van Rossum <guido@python.org>
parents: 589
diff changeset
711 return NULL;
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
712 }
229
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
713 }
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
714
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
715 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
716 int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 {
4606
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
718 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
719 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
720 CONVERT_TO_LONG(w, iw);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
721 if (iw < 0) {
19666
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
722 if ((PyObject *)z != Py_None) {
19719
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19677
diff changeset
723 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19677
diff changeset
724 "cannot be negative when 3rd argument specified");
19666
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
725 return NULL;
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
726 }
18499
2bb75a1f3184 On int to the negative integral power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents: 18146
diff changeset
727 /* Return a float. This works because we know that
2bb75a1f3184 On int to the negative integral power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents: 18146
diff changeset
728 this calls float_pow() which converts its
2bb75a1f3184 On int to the negative integral power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents: 18146
diff changeset
729 arguments to double. */
2bb75a1f3184 On int to the negative integral power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents: 18146
diff changeset
730 return PyFloat_Type.tp_as_number->nb_power(
2bb75a1f3184 On int to the negative integral power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents: 18146
diff changeset
731 (PyObject *)v, (PyObject *)w, (PyObject *)z);
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
732 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
733 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
734 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
735 if (iz == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
736 PyErr_SetString(PyExc_ValueError,
19719
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19677
diff changeset
737 "pow() 3rd argument cannot be 0");
4606
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
738 return NULL;
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
739 }
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
740 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
741 /*
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
742 * XXX: The original exponentiation code stopped looping
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
743 * 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
744 * 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
745 * 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
746 * will compensate for the slight inefficiency.
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
747 * 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
748 */
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
749 temp = iv;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
750 ix = 1;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
751 while (iw > 0) {
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
752 prev = ix; /* Save value for overflow check */
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
753 if (iw & 1) {
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
754 ix = ix*temp;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
755 if (temp == 0)
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
756 break; /* Avoid ix / 0 */
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
757 if (ix / temp != prev) {
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
758 return PyLong_Type.tp_as_number->nb_power(
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
759 (PyObject *)v,
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
760 (PyObject *)w,
19454
9afd9d5fb97a int_pow(): Repair typo when passing on to float pow (the 2nd argument was
Tim Peters <tim.peters@gmail.com>
parents: 19435
diff changeset
761 (PyObject *)z);
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
762 }
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
763 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
764 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
765 if (iw==0) break;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
766 prev = temp;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
767 temp *= temp; /* Square the value of temp */
33256
6641d0542592 Stop producing or using OverflowWarning. PEP 237 thought this would
Tim Peters <tim.peters@gmail.com>
parents: 32759
diff changeset
768 if (prev != 0 && temp / prev != prev) {
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
769 return PyLong_Type.tp_as_number->nb_power(
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
770 (PyObject *)v, (PyObject *)w, (PyObject *)z);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
771 }
4606
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
772 if (iz) {
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
773 /* 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
774 ix = ix % iz;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
775 temp = temp % iz;
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
776 }
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
777 }
4606
739b8480f1ac Fix core dump from pow(x,y,0).
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
778 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
779 long div, mod;
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
780 switch (i_divmod(ix, iz, &div, &mod)) {
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
781 case DIVMOD_OK:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
782 ix = mod;
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
783 break;
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
784 case DIVMOD_OVERFLOW:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
785 return PyLong_Type.tp_as_number->nb_power(
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
786 (PyObject *)v, (PyObject *)w, (PyObject *)z);
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
787 default:
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
788 return NULL;
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
789 }
1838
5f6acd1ab5c5 mods by Andrew Kuchling to implement
Guido van Rossum <guido@python.org>
parents: 1448
diff changeset
790 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
791 return PyInt_FromLong(ix);
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 21124
diff changeset
792 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
793
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
794 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
795 int_neg(PyIntObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
796 {
39984
ca887d55c592 Fix integer negation and absolute value to not rely
Martin v. Löwis <martin@v.loewis.de>
parents: 39741
diff changeset
797 register long a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
798 a = v->ob_ival;
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
799 /* check for overflow */
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39984
diff changeset
800 if (UNARY_NEG_WOULD_OVERFLOW(a)) {
33256
6641d0542592 Stop producing or using OverflowWarning. PEP 237 thought this would
Tim Peters <tim.peters@gmail.com>
parents: 32759
diff changeset
801 PyObject *o = PyLong_FromLong(a);
27301
5a975e82d70f SF # 669553, fix memory (ref) leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 26923
diff changeset
802 if (o != NULL) {
5a975e82d70f SF # 669553, fix memory (ref) leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 26923
diff changeset
803 PyObject *result = PyNumber_Negative(o);
5a975e82d70f SF # 669553, fix memory (ref) leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 26923
diff changeset
804 Py_DECREF(o);
5a975e82d70f SF # 669553, fix memory (ref) leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 26923
diff changeset
805 return result;
5a975e82d70f SF # 669553, fix memory (ref) leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 26923
diff changeset
806 }
5a975e82d70f SF # 669553, fix memory (ref) leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 26923
diff changeset
807 return NULL;
19435
dad3209ce973 Change all case where we used to raise OverflowError to issue a
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
808 }
39984
ca887d55c592 Fix integer negation and absolute value to not rely
Martin v. Löwis <martin@v.loewis.de>
parents: 39741
diff changeset
809 return PyInt_FromLong(-a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
810 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
811
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
812 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
813 int_abs(PyIntObject *v)
229
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
814 {
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
815 if (v->ob_ival >= 0)
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
816 return int_int(v);
229
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
817 else
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
818 return int_neg(v);
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
819 }
08597e2b63d3 Added divmod and abs, fixed negative powers
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
820
240
0aad3a7b97ef Added nonzero test
Guido van Rossum <guido@python.org>
parents: 229
diff changeset
821 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
822 int_nonzero(PyIntObject *v)
240
0aad3a7b97ef Added nonzero test
Guido van Rossum <guido@python.org>
parents: 229
diff changeset
823 {
0aad3a7b97ef Added nonzero test
Guido van Rossum <guido@python.org>
parents: 229
diff changeset
824 return v->ob_ival != 0;
0aad3a7b97ef Added nonzero test
Guido van Rossum <guido@python.org>
parents: 229
diff changeset
825 }
0aad3a7b97ef Added nonzero test
Guido van Rossum <guido@python.org>
parents: 229
diff changeset
826
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
827 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
828 int_invert(PyIntObject *v)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
829 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
830 return PyInt_FromLong(~v->ob_ival);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
831 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
832
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
833 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
834 int_lshift(PyIntObject *v, PyIntObject *w)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
835 {
24736
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
836 long a, b, c;
32418
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
837 PyObject *vv, *ww, *result;
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
838
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
839 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
840 CONVERT_TO_LONG(w, b);
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
841 if (b < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
842 PyErr_SetString(PyExc_ValueError, "negative shift count");
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
843 return NULL;
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
844 }
19878
cc146ee52897 More bug 460020. When I is a subclass of int, disable the +I(whatever),
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
845 if (a == 0 || b == 0)
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
846 return int_int(v);
1365
a5fc104849aa Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
847 if (b >= LONG_BIT) {
32418
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
848 vv = PyLong_FromLong(PyInt_AS_LONG(v));
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
849 if (vv == NULL)
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
850 return NULL;
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
851 ww = PyLong_FromLong(PyInt_AS_LONG(w));
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
852 if (ww == NULL) {
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
853 Py_DECREF(vv);
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
854 return NULL;
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
855 }
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
856 result = PyNumber_Lshift(vv, ww);
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
857 Py_DECREF(vv);
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
858 Py_DECREF(ww);
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
859 return result;
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
860 }
24742
c659f04ee499 int_lshift(): Simplified/sped overflow-checking.
Tim Peters <tim.peters@gmail.com>
parents: 24738
diff changeset
861 c = a << b;
c659f04ee499 int_lshift(): Simplified/sped overflow-checking.
Tim Peters <tim.peters@gmail.com>
parents: 24738
diff changeset
862 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
32418
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
863 vv = PyLong_FromLong(PyInt_AS_LONG(v));
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
864 if (vv == NULL)
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
865 return NULL;
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
866 ww = PyLong_FromLong(PyInt_AS_LONG(w));
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
867 if (ww == NULL) {
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
868 Py_DECREF(vv);
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
869 return NULL;
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
870 }
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
871 result = PyNumber_Lshift(vv, ww);
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
872 Py_DECREF(vv);
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
873 Py_DECREF(ww);
255a71f2d215 SF bug #980419: int left-shift causes memory leak
Raymond Hettinger <python@rcn.com>
parents: 32269
diff changeset
874 return result;
24736
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
875 }
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24702
diff changeset
876 return PyInt_FromLong(c);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
877 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
878
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
879 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
880 int_rshift(PyIntObject *v, PyIntObject *w)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
881 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
882 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
883 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
884 CONVERT_TO_LONG(w, b);
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
885 if (b < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
886 PyErr_SetString(PyExc_ValueError, "negative shift count");
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
887 return NULL;
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
888 }
19878
cc146ee52897 More bug 460020. When I is a subclass of int, disable the +I(whatever),
Tim Peters <tim.peters@gmail.com>
parents: 19865
diff changeset
889 if (a == 0 || b == 0)
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
890 return int_int(v);
1365
a5fc104849aa Changes to make range checks portable to 64-bit machines.
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
891 if (b >= LONG_BIT) {
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
892 if (a < 0)
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
893 a = -1;
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
894 else
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
895 a = 0;
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
896 }
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
897 else {
13423
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
898 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
589
6077704dab7a Different shift implementation.
Guido van Rossum <guido@python.org>
parents: 471
diff changeset
899 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
900 return PyInt_FromLong(a);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
901 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
902
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
903 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
904 int_and(PyIntObject *v, PyIntObject *w)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
905 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
906 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
907 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
908 CONVERT_TO_LONG(w, b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
909 return PyInt_FromLong(a & b);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
910 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
911
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
912 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
913 int_xor(PyIntObject *v, PyIntObject *w)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
914 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
915 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
916 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
917 CONVERT_TO_LONG(w, b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
918 return PyInt_FromLong(a ^ b);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
919 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
920
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
921 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
922 int_or(PyIntObject *v, PyIntObject *w)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
923 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
924 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
925 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
926 CONVERT_TO_LONG(w, b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
927 return PyInt_FromLong(a | b);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
928 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
929
19976
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
930 static int
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
931 int_coerce(PyObject **pv, PyObject **pw)
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
932 {
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
933 if (PyInt_Check(*pw)) {
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
934 Py_INCREF(*pv);
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
935 Py_INCREF(*pw);
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
936 return 0;
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
937 }
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
938 return 1; /* Can't do it */
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
939 }
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
940
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
941 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
942 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
943 {
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33256
diff changeset
944 if (PyInt_CheckExact(v))
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33256
diff changeset
945 Py_INCREF(v);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33256
diff changeset
946 else
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33256
diff changeset
947 v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
948 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
949 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
950
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
951 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
952 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
953 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
954 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
955 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
956
53289
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
957 static const unsigned char BitLengthTable[32] = {
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
958 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
959 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
960 };
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
961
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
962 static int
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
963 bits_in_ulong(unsigned long d)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
964 {
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
965 int d_bits = 0;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
966 while (d >= 32) {
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
967 d_bits += 6;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
968 d >>= 6;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
969 }
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
970 d_bits += (int)BitLengthTable[d];
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
971 return d_bits;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
972 }
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
973
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
974 #if 8*SIZEOF_LONG-1 <= DBL_MANT_DIG
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
975 /* Every Python int can be exactly represented as a float. */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
976
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
977 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
978 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
979 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
980 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
981 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
982
53289
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
983 #else
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
984 /* Here not all Python ints are exactly representable as floats, so we may
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
985 have to round. We do this manually, since the C standards don't specify
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
986 whether converting an integer to a float rounds up or down */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
987
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
988 static PyObject *
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
989 int_float(PyIntObject *v)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
990 {
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
991 unsigned long abs_ival, lsb;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
992 int round_up;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
993
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
994 if (v->ob_ival < 0)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
995 abs_ival = 0U-(unsigned long)v->ob_ival;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
996 else
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
997 abs_ival = (unsigned long)v->ob_ival;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
998 if (abs_ival < (1L << DBL_MANT_DIG))
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
999 /* small integer; no need to round */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1000 return PyFloat_FromDouble((double)v->ob_ival);
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1001
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1002 /* Round abs_ival to MANT_DIG significant bits, using the
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1003 round-half-to-even rule. abs_ival & lsb picks out the 'rounding'
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1004 bit: the first bit after the most significant MANT_DIG bits of
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1005 abs_ival. We round up if this bit is set, provided that either:
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1006
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1007 (1) abs_ival isn't exactly halfway between two floats, in which
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1008 case at least one of the bits following the rounding bit must be
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1009 set; i.e., abs_ival & lsb-1 != 0, or:
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1010
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1011 (2) the resulting rounded value has least significant bit 0; or
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1012 in other words the bit above the rounding bit is set (this is the
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1013 'to-even' bit of round-half-to-even); i.e., abs_ival & 2*lsb != 0
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1014
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1015 The condition "(1) or (2)" equates to abs_ival & 3*lsb-1 != 0. */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1016
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1017 lsb = 1L << (bits_in_ulong(abs_ival)-DBL_MANT_DIG-1);
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1018 round_up = (abs_ival & lsb) && (abs_ival & (3*lsb-1));
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1019 abs_ival &= -2*lsb;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1020 if (round_up)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1021 abs_ival += 2*lsb;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1022 return PyFloat_FromDouble(v->ob_ival < 0 ?
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1023 -(double)abs_ival :
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1024 (double)abs_ival);
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1025 }
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1026
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1027 #endif
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1028
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
1029 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
1030 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
1031 {
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1032 return _PyInt_Format(v, 8, 0);
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1033 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1034
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
1035 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13423
diff changeset
1036 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
1037 {
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1038 return _PyInt_Format(v, 16, 0);
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1039 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1040
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23832
diff changeset
1041 static PyObject *
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1042 int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1043
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1044 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1045 int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1046 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1047 PyObject *x = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1048 int base = -909;
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36645
diff changeset
1049 static char *kwlist[] = {"x", "base", 0};
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1050
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1051 if (type != &PyInt_Type)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1052 return int_subtype_new(type, args, kwds); /* Wimp out */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1053 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1054 &x, &base))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1055 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1056 if (x == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1057 return PyInt_FromLong(0L);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1058 if (base == -909)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1059 return PyNumber_Int(x);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
1060 if (PyString_Check(x)) {
40103
4159386f8dbd Bug #1545497: when given an explicit base, int() did ignore NULs
Georg Brandl <georg@python.org>
parents: 39995
diff changeset
1061 /* Since PyInt_FromString doesn't have a length parameter,
4159386f8dbd Bug #1545497: when given an explicit base, int() did ignore NULs
Georg Brandl <georg@python.org>
parents: 39995
diff changeset
1062 * check here for possible NULs in the string. */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
1063 char *string = PyString_AS_STRING(x);
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
1064 if (strlen(string) != PyString_Size(x)) {
40103
4159386f8dbd Bug #1545497: when given an explicit base, int() did ignore NULs
Georg Brandl <georg@python.org>
parents: 39995
diff changeset
1065 /* create a repr() of the input string,
4159386f8dbd Bug #1545497: when given an explicit base, int() did ignore NULs
Georg Brandl <georg@python.org>
parents: 39995
diff changeset
1066 * just like PyInt_FromString does */
4159386f8dbd Bug #1545497: when given an explicit base, int() did ignore NULs
Georg Brandl <georg@python.org>
parents: 39995
diff changeset
1067 PyObject *srepr;
4159386f8dbd Bug #1545497: when given an explicit base, int() did ignore NULs
Georg Brandl <georg@python.org>
parents: 39995
diff changeset
1068 srepr = PyObject_Repr(x);
4159386f8dbd Bug #1545497: when given an explicit base, int() did ignore NULs
Georg Brandl <georg@python.org>
parents: 39995
diff changeset
1069 if (srepr == NULL)
4159386f8dbd Bug #1545497: when given an explicit base, int() did ignore NULs
Georg Brandl <georg@python.org>
parents: 39995
diff changeset
1070 return NULL;
4159386f8dbd Bug #1545497: when given an explicit base, int() did ignore NULs
Georg Brandl <georg@python.org>
parents: 39995
diff changeset
1071 PyErr_Format(PyExc_ValueError,
4159386f8dbd Bug #1545497: when given an explicit base, int() did ignore NULs
Georg Brandl <georg@python.org>
parents: 39995
diff changeset
1072 "invalid literal for int() with base %d: %s",
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
1073 base, PyString_AS_STRING(srepr));
40103
4159386f8dbd Bug #1545497: when given an explicit base, int() did ignore NULs
Georg Brandl <georg@python.org>
parents: 39995
diff changeset
1074 Py_DECREF(srepr);
4159386f8dbd Bug #1545497: when given an explicit base, int() did ignore NULs
Georg Brandl <georg@python.org>
parents: 39995
diff changeset
1075 return NULL;
4159386f8dbd Bug #1545497: when given an explicit base, int() did ignore NULs
Georg Brandl <georg@python.org>
parents: 39995
diff changeset
1076 }
4159386f8dbd Bug #1545497: when given an explicit base, int() did ignore NULs
Georg Brandl <georg@python.org>
parents: 39995
diff changeset
1077 return PyInt_FromString(string, NULL, base);
4159386f8dbd Bug #1545497: when given an explicit base, int() did ignore NULs
Georg Brandl <georg@python.org>
parents: 39995
diff changeset
1078 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
1079 #ifdef Py_USING_UNICODE
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1080 if (PyUnicode_Check(x))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1081 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1082 PyUnicode_GET_SIZE(x),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1083 base);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
1084 #endif
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1085 PyErr_SetString(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1086 "int() can't convert non-string with explicit base");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1087 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1088 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1089
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1090 /* Wimpy, slow approach to tp_new calls for subtypes of int:
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1091 first create a regular int from whatever arguments we got,
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1092 then allocate a subtype instance and initialize its ob_ival
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1093 from the regular int. The regular int is then thrown away.
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1094 */
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1095 static PyObject *
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1096 int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1097 {
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37304
diff changeset
1098 PyObject *tmp, *newobj;
27779
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
1099 long ival;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1100
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1101 assert(PyType_IsSubtype(type, &PyInt_Type));
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1102 tmp = int_new(&PyInt_Type, args, kwds);
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1103 if (tmp == NULL)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1104 return NULL;
27779
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
1105 if (!PyInt_Check(tmp)) {
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
1106 ival = PyLong_AsLong(tmp);
30131
c33d3689ed5b Add a couple of decrefs to error paths.
Michael W. Hudson <mwh@python.net>
parents: 29641
diff changeset
1107 if (ival == -1 && PyErr_Occurred()) {
c33d3689ed5b Add a couple of decrefs to error paths.
Michael W. Hudson <mwh@python.net>
parents: 29641
diff changeset
1108 Py_DECREF(tmp);
27779
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
1109 return NULL;
30131
c33d3689ed5b Add a couple of decrefs to error paths.
Michael W. Hudson <mwh@python.net>
parents: 29641
diff changeset
1110 }
27779
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
1111 } else {
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
1112 ival = ((PyIntObject *)tmp)->ob_ival;
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
1113 }
fc5a60a61e32 Fix SF bug #683467, 'int' ability to generate longs not inherited
Neal Norwitz <nnorwitz@gmail.com>
parents: 27521
diff changeset
1114
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37304
diff changeset
1115 newobj = type->tp_alloc(type, 0);
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37304
diff changeset
1116 if (newobj == NULL) {
29641
4909aa64405f SF patch 703666: Several objects don't decref tmp on failure in subtype_new
Raymond Hettinger <python@rcn.com>
parents: 28652
diff changeset
1117 Py_DECREF(tmp);
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1118 return NULL;
29641
4909aa64405f SF patch 703666: Several objects don't decref tmp on failure in subtype_new
Raymond Hettinger <python@rcn.com>
parents: 28652
diff changeset
1119 }
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37304
diff changeset
1120 ((PyIntObject *)newobj)->ob_ival = ival;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1121 Py_DECREF(tmp);
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37304
diff changeset
1122 return newobj;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1123 }
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1124
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
1125 static PyObject *
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
1126 int_getnewargs(PyIntObject *v)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
1127 {
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
1128 return Py_BuildValue("(l)", v->ob_ival);
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
1129 }
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
1130
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1131 static PyObject *
53709
63ddce9faf4a Remove unnecessary uses of context in PyGetSetDef. See issue #5880.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
1132 int_get0(PyIntObject *v, void *context) {
63ddce9faf4a Remove unnecessary uses of context in PyGetSetDef. See issue #5880.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
1133 return PyInt_FromLong(0L);
63ddce9faf4a Remove unnecessary uses of context in PyGetSetDef. See issue #5880.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
1134 }
63ddce9faf4a Remove unnecessary uses of context in PyGetSetDef. See issue #5880.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
1135
63ddce9faf4a Remove unnecessary uses of context in PyGetSetDef. See issue #5880.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
1136 static PyObject *
63ddce9faf4a Remove unnecessary uses of context in PyGetSetDef. See issue #5880.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
1137 int_get1(PyIntObject *v, void *context) {
63ddce9faf4a Remove unnecessary uses of context in PyGetSetDef. See issue #5880.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
1138 return PyInt_FromLong(1L);
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1139 }
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1140
56240
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1141 /* Convert an integer to a decimal string. On many platforms, this
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1142 will be significantly faster than the general arbitrary-base
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1143 conversion machinery in _PyInt_Format, thanks to optimization
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1144 opportunities offered by division by a compile-time constant. */
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1145 static PyObject *
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1146 int_to_decimal_string(PyIntObject *v) {
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1147 char buf[sizeof(long)*CHAR_BIT/3+6], *p, *bufend;
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1148 long n = v->ob_ival;
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1149 unsigned long absn;
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1150 p = bufend = buf + sizeof(buf);
57300
ceb3b36dd681 Silence another MSVC warning about unary minus.
Mark Dickinson <dickinsm@gmail.com>
parents: 56240
diff changeset
1151 absn = n < 0 ? 0UL - n : n;
56240
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1152 do {
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1153 *--p = '0' + absn % 10;
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1154 absn /= 10;
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1155 } while (absn);
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1156 if (n < 0)
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1157 *--p = '-';
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1158 return PyString_FromStringAndSize(p, bufend - p);
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1159 }
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1160
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1161 /* Convert an integer to the given base. Returns a string.
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1162 If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'.
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1163 If newstyle is zero, then use the pre-2.6 behavior of octal having
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1164 a leading "0" */
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1165 PyAPI_FUNC(PyObject*)
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1166 _PyInt_Format(PyIntObject *v, int base, int newstyle)
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1167 {
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1168 /* There are no doubt many, many ways to optimize this, using code
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1169 similar to _PyLong_Format */
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1170 long n = v->ob_ival;
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1171 int negative = n < 0;
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1172 int is_zero = n == 0;
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1173
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1174 /* For the reasoning behind this size, see
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1175 http://c-faq.com/misc/hexio.html. Then, add a few bytes for
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1176 the possible sign and prefix "0[box]" */
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1177 char buf[sizeof(n)*CHAR_BIT+6];
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1178
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1179 /* Start by pointing to the end of the buffer. We fill in from
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1180 the back forward. */
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1181 char* p = &buf[sizeof(buf)];
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1182
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1183 assert(base >= 2 && base <= 36);
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1184
56240
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1185 /* Special case base 10, for speed */
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1186 if (base == 10)
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1187 return int_to_decimal_string(v);
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1188
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1189 do {
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1190 /* I'd use i_divmod, except it doesn't produce the results
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1191 I want when n is negative. So just duplicate the salient
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1192 part here. */
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1193 long div = n / base;
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1194 long mod = n - div * base;
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1195
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1196 /* convert abs(mod) to the right character in [0-9, a-z] */
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1197 char cdigit = (char)(mod < 0 ? -mod : mod);
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1198 cdigit += (cdigit < 10) ? '0' : 'a'-10;
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1199 *--p = cdigit;
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1200
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1201 n = div;
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1202 } while(n);
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1203
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1204 if (base == 2) {
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1205 *--p = 'b';
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1206 *--p = '0';
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1207 }
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1208 else if (base == 8) {
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1209 if (newstyle) {
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1210 *--p = 'o';
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1211 *--p = '0';
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1212 }
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1213 else
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1214 if (!is_zero)
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1215 *--p = '0';
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1216 }
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1217 else if (base == 16) {
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1218 *--p = 'x';
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1219 *--p = '0';
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1220 }
56240
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1221 else {
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1222 *--p = '#';
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1223 *--p = '0' + base%10;
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1224 if (base > 10)
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1225 *--p = '0' + base/10;
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1226 }
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1227 if (negative)
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1228 *--p = '-';
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1229
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47392
diff changeset
1230 return PyString_FromStringAndSize(p, &buf[sizeof(buf)] - p);
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1231 }
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 45143
diff changeset
1232
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1233 static PyObject *
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1234 int__format__(PyObject *self, PyObject *args)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1235 {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1236 PyObject *format_spec;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1237
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1238 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1239 return NULL;
47294
4fe192d48960 Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46473
diff changeset
1240 if (PyBytes_Check(format_spec))
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
1241 return _PyInt_FormatAdvanced(self,
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
1242 PyBytes_AS_STRING(format_spec),
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
1243 PyBytes_GET_SIZE(format_spec));
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1244 if (PyUnicode_Check(format_spec)) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1245 /* Convert format_spec to a str */
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
1246 PyObject *result;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
1247 PyObject *str_spec = PyObject_Str(format_spec);
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1248
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
1249 if (str_spec == NULL)
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
1250 return NULL;
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1251
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
1252 result = _PyInt_FormatAdvanced(self,
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
1253 PyBytes_AS_STRING(str_spec),
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
1254 PyBytes_GET_SIZE(str_spec));
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1255
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
1256 Py_DECREF(str_spec);
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1257 return result;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1258 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1259 PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode");
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1260 return NULL;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1261 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1262
50205
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1263 static PyObject *
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1264 int_bit_length(PyIntObject *v)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1265 {
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1266 unsigned long n;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1267
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1268 if (v->ob_ival < 0)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1269 /* avoid undefined behaviour when v->ob_ival == -LONG_MAX-1 */
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1270 n = 0U-(unsigned long)v->ob_ival;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1271 else
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1272 n = (unsigned long)v->ob_ival;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1273
53289
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 50627
diff changeset
1274 return PyInt_FromLong(bits_in_ulong(n));
50205
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1275 }
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1276
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1277 PyDoc_STRVAR(int_bit_length_doc,
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1278 "int.bit_length() -> int\n\
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1279 \n\
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1280 Number of bits necessary to represent self in binary.\n\
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1281 >>> bin(37)\n\
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1282 '0b100101'\n\
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1283 >>> (37).bit_length()\n\
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1284 6");
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1285
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46135
diff changeset
1286 #if 0
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46135
diff changeset
1287 static PyObject *
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46135
diff changeset
1288 int_is_finite(PyObject *v)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46135
diff changeset
1289 {
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46135
diff changeset
1290 Py_RETURN_TRUE;
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46135
diff changeset
1291 }
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46135
diff changeset
1292 #endif
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46135
diff changeset
1293
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
1294 static PyMethodDef int_methods[] = {
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1295 {"conjugate", (PyCFunction)int_int, METH_NOARGS,
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1296 "Returns self, the complex conjugate of any int."},
50205
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1297 {"bit_length", (PyCFunction)int_bit_length, METH_NOARGS,
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 47897
diff changeset
1298 int_bit_length_doc},
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46135
diff changeset
1299 #if 0
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46135
diff changeset
1300 {"is_finite", (PyCFunction)int_is_finite, METH_NOARGS,
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46135
diff changeset
1301 "Returns always True."},
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46135
diff changeset
1302 #endif
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1303 {"__trunc__", (PyCFunction)int_int, METH_NOARGS,
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1304 "Truncating an Integral returns itself."},
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
1305 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
1306 {"__format__", (PyCFunction)int__format__, METH_VARARGS},
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
1307 {NULL, NULL} /* sentinel */
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
1308 };
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
1309
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1310 static PyGetSetDef int_getset[] = {
53709
63ddce9faf4a Remove unnecessary uses of context in PyGetSetDef. See issue #5880.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
1311 {"real",
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1312 (getter)int_int, (setter)NULL,
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1313 "the real part of a complex number",
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1314 NULL},
53709
63ddce9faf4a Remove unnecessary uses of context in PyGetSetDef. See issue #5880.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
1315 {"imag",
63ddce9faf4a Remove unnecessary uses of context in PyGetSetDef. See issue #5880.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
1316 (getter)int_get0, (setter)NULL,
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1317 "the imaginary part of a complex number",
53709
63ddce9faf4a Remove unnecessary uses of context in PyGetSetDef. See issue #5880.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
1318 NULL},
63ddce9faf4a Remove unnecessary uses of context in PyGetSetDef. See issue #5880.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
1319 {"numerator",
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1320 (getter)int_int, (setter)NULL,
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1321 "the numerator of a rational number in lowest terms",
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1322 NULL},
53709
63ddce9faf4a Remove unnecessary uses of context in PyGetSetDef. See issue #5880.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
1323 {"denominator",
63ddce9faf4a Remove unnecessary uses of context in PyGetSetDef. See issue #5880.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
1324 (getter)int_get1, (setter)NULL,
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1325 "the denominator of a rational number in lowest terms",
53709
63ddce9faf4a Remove unnecessary uses of context in PyGetSetDef. See issue #5880.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
1326 NULL},
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1327 {NULL} /* Sentinel */
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1328 };
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1329
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23299
diff changeset
1330 PyDoc_STRVAR(int_doc,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1331 "int(x[, base]) -> integer\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1332 \n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1333 Convert a string or number to an integer, if possible. A floating point\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1334 argument will be truncated towards zero (this does not include a string\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1335 representation of a floating point number!) When converting a string, use\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1336 the optional base. It is an error to supply a base when converting a\n\
40586
b4ba7249aba5 Minor change in int() docstring for proper spacing.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 40585
diff changeset
1337 non-string. If base is zero, the proper base is guessed based on the\n\
40585
667bd027d800 Mention in the int() docstring that a base zero has meaning, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 40103
diff changeset
1338 string content. If the argument is outside the integer range a\n\
667bd027d800 Mention in the int() docstring that a base zero has meaning, as
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 40103
diff changeset
1339 long object will be returned instead.");
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1340
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
1341 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
1342 (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
1343 (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
1344 (binaryfunc)int_mul, /*nb_multiply*/
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19590
diff changeset
1345 (binaryfunc)int_classic_div, /*nb_divide*/
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1346 (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
1347 (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
1348 (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
1349 (unaryfunc)int_neg, /*nb_negative*/
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1350 (unaryfunc)int_int, /*nb_positive*/
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
1351 (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
1352 (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
1353 (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
1354 (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
1355 (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
1356 (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
1357 (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
1358 (binaryfunc)int_or, /*nb_or*/
19976
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
1359 int_coerce, /*nb_coerce*/
15724
2dcc4a86a876 Make int a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1360 (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
1361 (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
1362 (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
1363 (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
1364 (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
1365 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
1366 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
1367 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
1368 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
1369 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
1370 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
1371 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
1372 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
1373 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
1374 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
1375 0, /*nb_inplace_or*/
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1376 (binaryfunc)int_div, /* nb_floor_divide */
57941
c4e60988834d Issue #1811: Improve accuracy and consistency of true division for integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 57567
diff changeset
1377 (binaryfunc)int_true_divide, /* nb_true_divide */
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1378 0, /* nb_inplace_floor_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1379 0, /* nb_inplace_true_divide */
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37815
diff changeset
1380 (unaryfunc)int_int, /* nb_index */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1381 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1382
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
1383 PyTypeObject PyInt_Type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41733
diff changeset
1384 PyVarObject_HEAD_INIT(&PyType_Type, 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1385 "int",
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4952
diff changeset
1386 sizeof(PyIntObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1387 0,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1388 (destructor)int_dealloc, /* tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1389 (printfunc)int_print, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1390 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1391 0, /* tp_setattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1392 (cmpfunc)int_compare, /* tp_compare */
56240
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1393 (reprfunc)int_to_decimal_string, /* tp_repr */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1394 &int_as_number, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1395 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1396 0, /* tp_as_mapping */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1397 (hashfunc)int_hash, /* tp_hash */
56240
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1398 0, /* tp_call */
546101a6677c Issue #6713: Improve decimal int -> string conversions. Thanks Gawain
Mark Dickinson <dickinsm@gmail.com>
parents: 53709
diff changeset
1399 (reprfunc)int_to_decimal_string, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1400 PyObject_GenericGetAttr, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1401 0, /* tp_setattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1402 0, /* tp_as_buffer */
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1403 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
40908
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40586
diff changeset
1404 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_INT_SUBCLASS, /* tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1405 int_doc, /* tp_doc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1406 0, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1407 0, /* tp_clear */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1408 0, /* tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1409 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1410 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1411 0, /* tp_iternext */
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27301
diff changeset
1412 int_methods, /* tp_methods */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1413 0, /* tp_members */
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1414 int_getset, /* tp_getset */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1415 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1416 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1417 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1418 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1419 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1420 0, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1421 0, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18755
diff changeset
1422 int_new, /* tp_new */
23279
200559fcc664 Make sure that tp_free frees the int the same way as tp_dealloc would.
Guido van Rossum <guido@python.org>
parents: 22785
diff changeset
1423 (freefunc)int_free, /* tp_free */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1424 };
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1425
26889
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1426 int
26923
dbfc3ef7d003 Since the *_Init() are private, prefix with _, suggested by Skip
Neal Norwitz <nnorwitz@gmail.com>
parents: 26889
diff changeset
1427 _PyInt_Init(void)
26889
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1428 {
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1429 PyIntObject *v;
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1430 int ival;
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1431 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1432 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
31483
65d28fff1172 SF patch #875689: >100k alloc wasted on startup
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
1433 if (!free_list && (free_list = fill_free_list()) == NULL)
26889
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1434 return 0;
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1435 /* PyObject_New is inlined */
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1436 v = free_list;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
1437 free_list = (PyIntObject *)Py_TYPE(v);
26889
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1438 PyObject_INIT(v, &PyInt_Type);
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1439 v->ob_ival = ival;
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1440 small_ints[ival + NSMALLNEGINTS] = v;
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1441 }
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1442 #endif
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1443 return 1;
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1444 }
12753e7b15f7 SF #561244, Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 26867
diff changeset
1445
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1446 int
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1447 PyInt_ClearFreeList(void)
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1448 {
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1449 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
1450 PyIntBlock *list, *next;
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1451 int i;
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1452 int u; /* remaining unfreed ints per block */
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1453 int freelist_size = 0;
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1454
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1455 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
1456 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
1457 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
1458 while (list != NULL) {
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1459 u = 0;
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1460 for (i = 0, p = &list->objects[0];
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1461 i < N_INTOBJECTS;
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1462 i++, p++) {
19865
f2a3009b528e Replace a few places where X->ob_type was compared to &PyXXX_Type with
Guido van Rossum <guido@python.org>
parents: 19719
diff changeset
1463 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1464 u++;
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1465 }
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1466 next = list->next;
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1467 if (u) {
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1468 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
1469 block_list = list;
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1470 for (i = 0, p = &list->objects[0];
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1471 i < N_INTOBJECTS;
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1472 i++, p++) {
19865
f2a3009b528e Replace a few places where X->ob_type was compared to &PyXXX_Type with
Guido van Rossum <guido@python.org>
parents: 19719
diff changeset
1473 if (!PyInt_CheckExact(p) ||
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19455
diff changeset
1474 p->ob_refcnt == 0) {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43378
diff changeset
1475 Py_TYPE(p) = (struct _typeobject *)
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
1476 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
1477 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
1478 }
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
1479 #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
1480 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
1481 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
1482 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
1483 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
1484 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
1485 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
1486 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
1487 }
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
1488 #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
1489 }
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1490 }
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1491 else {
23299
01478a132908 Just added comments, and cleared some XXX questions, related to int
Tim Peters <tim.peters@gmail.com>
parents: 23279
diff changeset
1492 PyMem_FREE(list);
10373
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1493 }
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1494 freelist_size += u;
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1495 list = next;
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1496 }
45143
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1497
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1498 return freelist_size;
45143
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1499 }
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1500
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1501 void
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1502 PyInt_Fini(void)
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1503 {
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1504 PyIntObject *p;
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1505 PyIntBlock *list;
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1506 int i;
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1507 int u; /* total unfreed ints per block */
45143
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1508
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1509 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1510 PyIntObject **q;
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1511
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1512 i = NSMALLNEGINTS + NSMALLPOSINTS;
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1513 q = small_ints;
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1514 while (--i >= 0) {
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1515 Py_XDECREF(*q);
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1516 *q++ = NULL;
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1517 }
8fdd6d62ea55 Patch #1953
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
1518 #endif
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1519 u = PyInt_ClearFreeList();
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1520 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
1521 return;
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1522 fprintf(stderr, "# cleanup ints");
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1523 if (!u) {
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1524 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
1525 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1526 else {
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1527 fprintf(stderr,
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1528 ": %d unfreed int%s\n",
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1529 u, u == 1 ? "" : "s");
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1530 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1531 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
1532 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
1533 while (list != NULL) {
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1534 for (i = 0, p = &list->objects[0];
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1535 i < N_INTOBJECTS;
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47822
diff changeset
1536 i++, p++) {
19865
f2a3009b528e Replace a few places where X->ob_type was compared to &PyXXX_Type with
Guido van Rossum <guido@python.org>
parents: 19719
diff changeset
1537 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
36759
83b860acfe95 Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
1538 /* XXX(twouters) cast refcount to
83b860acfe95 Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
1539 long until %zd is universally
83b860acfe95 Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
1540 available
83b860acfe95 Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
1541 */
10391
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1542 fprintf(stderr,
36759
83b860acfe95 Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
1543 "# <int at %p, refcnt=%ld, val=%ld>\n",
83b860acfe95 Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
1544 p, (long)p->ob_refcnt,
83b860acfe95 Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
1545 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
1546 }
7b810aa35b5c Vladimir has restructured his code somewhat so that the blocks are now
Guido van Rossum <guido@python.org>
parents: 10373
diff changeset
1547 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
1548 }
07ee795774d8 Patch by Vladimir Marangoz to allow freeing of the allocated blocks of
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
1549 }
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1550 }