Mercurial > cpython-lru_cache-weakref
annotate Python/mystrtoul.c @ 39496:352ffe77b71d legacy-trunk
Patch #1535500: fix segfault in BZ2File.writelines and make sure it
raises the correct exceptions.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Mon, 14 Aug 2006 21:42:55 +0000 |
| parents | 40b446ed3f12 |
| children | 6ba402b60041 |
| rev | line source |
|---|---|
| 1449 | 1 |
| 9656 | 2 #include "Python.h" |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
3 |
|
8634
1171381fa01c
Sigh. The hack to enable special treatment for errno on SGI machines
Guido van Rossum <guido@python.org>
parents:
7979
diff
changeset
|
4 #if defined(__sgi) && defined(WITH_THREAD) && !defined(_SGI_MP_SOURCE) |
|
1171381fa01c
Sigh. The hack to enable special treatment for errno on SGI machines
Guido van Rossum <guido@python.org>
parents:
7979
diff
changeset
|
5 #define _SGI_MP_SOURCE |
|
1171381fa01c
Sigh. The hack to enable special treatment for errno on SGI machines
Guido van Rossum <guido@python.org>
parents:
7979
diff
changeset
|
6 #endif |
|
1171381fa01c
Sigh. The hack to enable special treatment for errno on SGI machines
Guido van Rossum <guido@python.org>
parents:
7979
diff
changeset
|
7 |
| 2310 | 8 /* Convert a possibly signed character to a nonnegative int */ |
| 9 /* XXX This assumes characters are 8 bits wide */ | |
| 10 #ifdef __CHAR_UNSIGNED__ | |
| 11 #define Py_CHARMASK(c) (c) | |
| 12 #else | |
| 13 #define Py_CHARMASK(c) ((c) & 0xff) | |
| 14 #endif | |
| 15 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
16 /* strtol and strtoul, renamed to avoid conflicts */ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
17 |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
18 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
19 #include <ctype.h> |
|
38716
42808526511c
Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
38301
diff
changeset
|
20 #ifdef HAVE_ERRNO_H |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
21 #include <errno.h> |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
22 #endif |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
23 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
24 /* Static overflow check values for bases 2 through 36. |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
25 * smallmax[base] is the largest unsigned long i such that |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
26 * i * base doesn't overflow unsigned long. |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
27 */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
28 static unsigned long smallmax[] = { |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
29 0, /* bases 0 and 1 are invalid */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
30 0, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
31 ULONG_MAX / 2, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
32 ULONG_MAX / 3, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
33 ULONG_MAX / 4, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
34 ULONG_MAX / 5, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
35 ULONG_MAX / 6, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
36 ULONG_MAX / 7, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
37 ULONG_MAX / 8, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
38 ULONG_MAX / 9, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
39 ULONG_MAX / 10, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
40 ULONG_MAX / 11, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
41 ULONG_MAX / 12, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
42 ULONG_MAX / 13, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
43 ULONG_MAX / 14, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
44 ULONG_MAX / 15, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
45 ULONG_MAX / 16, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
46 ULONG_MAX / 17, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
47 ULONG_MAX / 18, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
48 ULONG_MAX / 19, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
49 ULONG_MAX / 20, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
50 ULONG_MAX / 21, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
51 ULONG_MAX / 22, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
52 ULONG_MAX / 23, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
53 ULONG_MAX / 24, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
54 ULONG_MAX / 25, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
55 ULONG_MAX / 26, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
56 ULONG_MAX / 27, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
57 ULONG_MAX / 28, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
58 ULONG_MAX / 29, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
59 ULONG_MAX / 30, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
60 ULONG_MAX / 31, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
61 ULONG_MAX / 32, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
62 ULONG_MAX / 33, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
63 ULONG_MAX / 34, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
64 ULONG_MAX / 35, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
65 ULONG_MAX / 36, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
66 }; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
67 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
68 /* maximum digits that can't ever overflow for bases 2 through 36, |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
69 * calculated by [int(math.floor(math.log(2**32, i))) for i in range(2, 37)]. |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
70 * Note that this is pessimistic if sizeof(long) > 4. |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
71 */ |
|
39039
95f9e0184555
On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents:
38716
diff
changeset
|
72 #if SIZEOF_LONG == 4 |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
73 static int digitlimit[] = { |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
74 0, 0, 32, 20, 16, 13, 12, 11, 10, 10, /* 0 - 9 */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
75 9, 9, 8, 8, 8, 8, 8, 7, 7, 7, /* 10 - 19 */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
76 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, /* 20 - 29 */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
77 6, 6, 6, 6, 6, 6, 6}; /* 30 - 36 */ |
|
39039
95f9e0184555
On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents:
38716
diff
changeset
|
78 #elif SIZEOF_LONG == 8 |
|
95f9e0184555
On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents:
38716
diff
changeset
|
79 /* [int(math.floor(math.log(2**64, i))) for i in range(2, 37)] */ |
|
95f9e0184555
On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents:
38716
diff
changeset
|
80 static int digitlimit[] = { |
|
95f9e0184555
On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents:
38716
diff
changeset
|
81 0, 0, 64, 40, 32, 27, 24, 22, 21, 20, /* 0 - 9 */ |
|
95f9e0184555
On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents:
38716
diff
changeset
|
82 19, 18, 17, 17, 16, 16, 16, 15, 15, 15, /* 10 - 19 */ |
|
95f9e0184555
On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents:
38716
diff
changeset
|
83 14, 14, 14, 14, 13, 13, 13, 13, 13, 13, /* 20 - 29 */ |
|
95f9e0184555
On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents:
38716
diff
changeset
|
84 13, 12, 12, 12, 12, 12, 12}; /* 30 - 36 */ |
|
95f9e0184555
On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents:
38716
diff
changeset
|
85 #else |
|
95f9e0184555
On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents:
38716
diff
changeset
|
86 #error "Need table for SIZEOF_LONG" |
|
95f9e0184555
On 64 bit systems, int literals that use less than 64 bits are now ints
Neal Norwitz <nnorwitz@gmail.com>
parents:
38716
diff
changeset
|
87 #endif |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
88 |
| 1449 | 89 /* |
| 90 ** strtoul | |
| 91 ** This is a general purpose routine for converting | |
| 92 ** an ascii string to an integer in an arbitrary base. | |
| 93 ** Leading white space is ignored. If 'base' is zero | |
| 94 ** it looks for a leading 0, 0x or 0X to tell which | |
| 95 ** base. If these are absent it defaults to 10. | |
| 96 ** Base must be 0 or between 2 and 36 (inclusive). | |
| 97 ** If 'ptr' is non-NULL it will contain a pointer to | |
| 98 ** the end of the scan. | |
| 99 ** Errors due to bad pointers will probably result in | |
| 100 ** exceptions - we don't check for them. | |
| 101 */ | |
| 102 unsigned long | |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
103 PyOS_strtoul(register char *str, char **ptr, int base) |
| 1449 | 104 { |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
105 register unsigned long result = 0; /* return value of the function */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
106 register int c; /* current input character */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
107 register int ovlimit; /* required digits to overflow */ |
| 1449 | 108 |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
109 /* skip leading white space */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
110 while (*str && isspace(Py_CHARMASK(*str))) |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
111 ++str; |
| 1449 | 112 |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
113 /* check for leading 0 or 0x for auto-base or base 16 */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
114 switch (base) { |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
115 case 0: /* look for leading 0, 0x or 0X */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
116 if (*str == '0') { |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
117 ++str; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
118 if (*str == 'x' || *str == 'X') { |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
119 ++str; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
120 base = 16; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
121 } |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
122 else |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
123 base = 8; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
124 } |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
125 else |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
126 base = 10; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
127 break; |
| 1449 | 128 |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
129 case 16: /* skip leading 0x or 0X */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
130 if (*str == '0') { |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
131 ++str; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
132 if (*str == 'x' || *str == 'X') |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
133 ++str; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
134 } |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
135 break; |
| 1449 | 136 } |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
137 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
138 /* catch silly bases */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
139 if (base < 2 || base > 36) { |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
140 if (ptr) |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
141 *ptr = str; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
142 return 0; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
143 } |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
144 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
145 /* skip leading zeroes */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
146 while (*str == '0') |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
147 ++str; |
| 1449 | 148 |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
149 /* base is guaranteed to be in [2, 36] at this point */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
150 ovlimit = digitlimit[base]; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
151 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
152 /* do the conversion until non-digit character encountered */ |
|
38301
dccbcbbd6720
A new table to help string->integer conversion was added yesterday to
Tim Peters <tim.peters@gmail.com>
parents:
38258
diff
changeset
|
153 while ((c = _PyLong_DigitValue[Py_CHARMASK(*str)]) < base) { |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
154 if (ovlimit > 0) /* no overflow check required */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
155 result = result * base + c; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
156 else { /* requires overflow check */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
157 register unsigned long temp_result; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
158 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
159 if (ovlimit < 0) /* guaranteed overflow */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
160 goto overflowed; |
| 1449 | 161 |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
162 /* there could be an overflow */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
163 /* check overflow just from shifting */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
164 if (result > smallmax[base]) |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
165 goto overflowed; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
166 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
167 result *= base; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
168 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
169 /* check overflow from the digit's value */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
170 temp_result = result + c; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
171 if (temp_result < result) |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
172 goto overflowed; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
173 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
174 result = temp_result; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
175 } |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
176 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
177 ++str; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
178 --ovlimit; |
| 1449 | 179 } |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
180 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
181 /* set pointer to point to the last character scanned */ |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
182 if (ptr) |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
183 *ptr = str; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
184 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
185 return result; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
186 |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
187 overflowed: |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
188 if (ptr) { |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
189 /* spool through remaining digit characters */ |
|
38301
dccbcbbd6720
A new table to help string->integer conversion was added yesterday to
Tim Peters <tim.peters@gmail.com>
parents:
38258
diff
changeset
|
190 while (_PyLong_DigitValue[Py_CHARMASK(*str)] < base) |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
191 ++str; |
|
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
192 *ptr = str; |
|
6849
51f8d4da81e4
For base 10, cast unsigned long to long before testing overflow.
Guido van Rossum <guido@python.org>
parents:
5440
diff
changeset
|
193 } |
| 1449 | 194 errno = ERANGE; |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
195 return (unsigned long)-1; |
| 1449 | 196 } |
| 197 | |
|
39200
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
198 /* Checking for overflow in PyOS_strtol is a PITA since C doesn't define |
|
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
199 * anything about what happens when a signed integer operation overflows, |
|
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
200 * and some compilers think they're doing you a favor by being "clever" |
|
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
201 * then. Python assumes a 2's-complement representation, so that the bit |
|
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
202 * pattern for the largest postive signed long is LONG_MAX, and for |
|
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
203 * the smallest negative signed long is LONG_MAX + 1. |
|
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
204 */ |
|
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
205 |
| 1449 | 206 long |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
207 PyOS_strtol(char *str, char **ptr, int base) |
| 1449 | 208 { |
| 209 long result; | |
|
39200
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
210 unsigned long uresult; |
| 1449 | 211 char sign; |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
212 |
| 2310 | 213 while (*str && isspace(Py_CHARMASK(*str))) |
| 1449 | 214 str++; |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
215 |
| 1449 | 216 sign = *str; |
| 217 if (sign == '+' || sign == '-') | |
| 218 str++; | |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
219 |
|
39200
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
220 uresult = PyOS_strtoul(str, ptr, base); |
|
38258
9fdd434216ef
Bug #1334662 / patch #1335972: int(string, base) wrong answers.
Tim Peters <tim.peters@gmail.com>
parents:
30968
diff
changeset
|
221 |
|
39200
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
222 if (uresult <= (unsigned long)LONG_MAX) { |
|
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
223 result = (long)uresult; |
|
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
224 if (sign == '-') |
|
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
225 result = -result; |
|
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
226 } |
|
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
227 else if (sign == '-' && uresult == (unsigned long)LONG_MAX + 1) { |
|
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
228 assert(LONG_MIN == -LONG_MAX-1); |
|
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
229 result = LONG_MIN; |
|
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
230 } |
|
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
231 else { |
| 1449 | 232 errno = ERANGE; |
|
39200
40b446ed3f12
Bug #1521947: possible bug in mystrtol.c with recent gcc.
Tim Peters <tim.peters@gmail.com>
parents:
39039
diff
changeset
|
233 result = LONG_MAX; |
| 1449 | 234 } |
| 235 return result; | |
| 236 } |
