Mercurial > cpython-lru_cache-weakref
annotate Python/mystrtoul.c @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 65d2d8e67da3 |
| children | 4fa19659591e |
| rev | line source |
|---|---|
| 1449 | 1 /*********************************************************** |
|
2062
d0d0a69b5ca3
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
d0d0a69b5ca3
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
3 The Netherlands. |
| 1449 | 4 |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 1449 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 1449 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
15 permission. |
| 1449 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4043
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 1449 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
32 #include "config.h" |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
33 |
| 2310 | 34 /* Convert a possibly signed character to a nonnegative int */ |
| 35 /* XXX This assumes characters are 8 bits wide */ | |
| 36 #ifdef __CHAR_UNSIGNED__ | |
| 37 #define Py_CHARMASK(c) (c) | |
| 38 #else | |
| 39 #define Py_CHARMASK(c) ((c) & 0xff) | |
| 40 #endif | |
| 41 | |
| 2155 | 42 #include "rename2.h" |
| 43 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
44 /* 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
|
45 |
| 1449 | 46 /* |
| 47 ** strtoul | |
| 48 ** This is a general purpose routine for converting | |
| 49 ** an ascii string to an integer in an arbitrary base. | |
| 50 ** Leading white space is ignored. If 'base' is zero | |
| 51 ** it looks for a leading 0, 0x or 0X to tell which | |
| 52 ** base. If these are absent it defaults to 10. | |
| 53 ** Base must be 0 or between 2 and 36 (inclusive). | |
| 54 ** If 'ptr' is non-NULL it will contain a pointer to | |
| 55 ** the end of the scan. | |
| 56 ** Errors due to bad pointers will probably result in | |
| 57 ** exceptions - we don't check for them. | |
| 58 */ | |
| 59 | |
| 60 #include <ctype.h> | |
| 61 #include <errno.h> | |
| 62 | |
| 63 unsigned long | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
64 mystrtoul(str, ptr, base) |
| 1449 | 65 register char * str; |
| 66 char ** ptr; | |
| 67 int base; | |
| 68 { | |
| 69 register unsigned long result; /* return value of the function */ | |
| 70 register int c; /* current input character */ | |
| 71 register unsigned long temp; /* used in overflow testing */ | |
| 72 int ovf; /* true if overflow occurred */ | |
| 73 | |
| 74 result = 0; | |
| 75 ovf = 0; | |
| 76 | |
| 77 /* catch silly bases */ | |
| 78 if (base != 0 && (base < 2 || base > 36)) | |
| 79 { | |
| 80 if (ptr) | |
| 81 *ptr = str; | |
| 82 return 0; | |
| 83 } | |
| 84 | |
| 85 /* skip leading white space */ | |
| 2310 | 86 while (*str && isspace(Py_CHARMASK(*str))) |
| 1449 | 87 str++; |
| 88 | |
| 89 /* check for leading 0 or 0x for auto-base or base 16 */ | |
| 90 switch (base) | |
| 91 { | |
| 92 case 0: /* look for leading 0, 0x or 0X */ | |
| 93 if (*str == '0') | |
| 94 { | |
| 95 str++; | |
| 96 if (*str == 'x' || *str == 'X') | |
| 97 { | |
| 98 str++; | |
| 99 base = 16; | |
| 100 } | |
| 101 else | |
| 102 base = 8; | |
| 103 } | |
| 104 else | |
| 105 base = 10; | |
| 106 break; | |
| 107 | |
| 108 case 16: /* skip leading 0x or 0X */ | |
| 109 if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X')) | |
| 110 str += 2; | |
| 111 break; | |
| 112 } | |
| 113 | |
| 114 /* do the conversion */ | |
| 2310 | 115 while (c = Py_CHARMASK(*str)) |
| 1449 | 116 { |
| 117 if (isdigit(c) && c - '0' < base) | |
| 118 c -= '0'; | |
| 119 else | |
| 120 { | |
| 121 if (isupper(c)) | |
| 122 c = tolower(c); | |
| 123 if (c >= 'a' && c <= 'z') | |
| 124 c -= 'a' - 10; | |
| 125 else /* non-"digit" character */ | |
| 126 break; | |
| 127 if (c >= base) /* non-"digit" character */ | |
| 128 break; | |
| 129 } | |
| 130 temp = result; | |
| 131 result = result * base + c; | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
132 #ifndef MPW |
| 1449 | 133 if ((result - c) / base != temp) /* overflow */ |
| 134 ovf = 1; | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
135 #endif |
| 1449 | 136 str++; |
| 137 } | |
| 138 | |
| 139 /* set pointer to point to the last character scanned */ | |
| 140 if (ptr) | |
| 141 *ptr = str; | |
| 142 if (ovf) | |
| 143 { | |
| 144 result = ~0; | |
| 145 errno = ERANGE; | |
| 146 } | |
| 147 return result; | |
| 148 } | |
| 149 | |
| 150 long | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
151 mystrtol(str, ptr, base) |
| 1449 | 152 char * str; |
| 153 char ** ptr; | |
| 154 int base; | |
| 155 { | |
| 156 long result; | |
| 157 char sign; | |
| 158 | |
| 2310 | 159 while (*str && isspace(Py_CHARMASK(*str))) |
| 1449 | 160 str++; |
| 161 | |
| 162 sign = *str; | |
| 163 if (sign == '+' || sign == '-') | |
| 164 str++; | |
| 165 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1449
diff
changeset
|
166 result = (long) mystrtoul(str, ptr, base); |
| 1449 | 167 |
| 168 /* Signal overflow if the result appears negative, | |
| 169 except for the largest negative integer */ | |
| 170 if (result < 0 && !(sign == '-' && result == -result)) { | |
| 171 errno = ERANGE; | |
| 172 result = 0x7fffffff; | |
| 173 } | |
| 174 | |
| 175 if (sign == '-') | |
| 176 result = -result; | |
| 177 | |
| 178 return result; | |
| 179 } |
