Mercurial > cpython-lru_cache-weakref
comparison Modules/python.c @ 61467:f197407bdba2 3.1
Recorded merge of revisions 81032 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
................
r81032 | antoine.pitrou | 2010-05-09 17:52:27 +0200 (dim., 09 mai 2010) | 9 lines
Recorded merge of revisions 81029 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r81029 | antoine.pitrou | 2010-05-09 16:46:46 +0200 (dim., 09 mai 2010) | 3 lines
Untabify C files. Will watch buildbots.
........
................
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Sun, 09 May 2010 16:14:21 +0000 |
| parents | ec5d228a83ef |
| children | 3c9232a684ae |
comparison
equal
deleted
inserted
replaced
| 61459:f8c19c5f5dd2 | 61467:f197407bdba2 |
|---|---|
| 9 | 9 |
| 10 #ifdef MS_WINDOWS | 10 #ifdef MS_WINDOWS |
| 11 int | 11 int |
| 12 wmain(int argc, wchar_t **argv) | 12 wmain(int argc, wchar_t **argv) |
| 13 { | 13 { |
| 14 return Py_Main(argc, argv); | 14 return Py_Main(argc, argv); |
| 15 } | 15 } |
| 16 #else | 16 #else |
| 17 static wchar_t* | 17 static wchar_t* |
| 18 char2wchar(char* arg) | 18 char2wchar(char* arg) |
| 19 { | 19 { |
| 20 wchar_t *res; | 20 wchar_t *res; |
| 21 #ifdef HAVE_BROKEN_MBSTOWCS | 21 #ifdef HAVE_BROKEN_MBSTOWCS |
| 22 /* Some platforms have a broken implementation of | 22 /* Some platforms have a broken implementation of |
| 23 * mbstowcs which does not count the characters that | 23 * mbstowcs which does not count the characters that |
| 24 * would result from conversion. Use an upper bound. | 24 * would result from conversion. Use an upper bound. |
| 25 */ | 25 */ |
| 26 size_t argsize = strlen(arg); | 26 size_t argsize = strlen(arg); |
| 27 #else | 27 #else |
| 28 size_t argsize = mbstowcs(NULL, arg, 0); | 28 size_t argsize = mbstowcs(NULL, arg, 0); |
| 29 #endif | 29 #endif |
| 30 size_t count; | 30 size_t count; |
| 31 unsigned char *in; | 31 unsigned char *in; |
| 32 wchar_t *out; | 32 wchar_t *out; |
| 33 #ifdef HAVE_MBRTOWC | 33 #ifdef HAVE_MBRTOWC |
| 34 mbstate_t mbs; | 34 mbstate_t mbs; |
| 35 #endif | 35 #endif |
| 36 if (argsize != (size_t)-1) { | 36 if (argsize != (size_t)-1) { |
| 37 res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t)); | 37 res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t)); |
| 38 if (!res) | 38 if (!res) |
| 39 goto oom; | 39 goto oom; |
| 40 count = mbstowcs(res, arg, argsize+1); | 40 count = mbstowcs(res, arg, argsize+1); |
| 41 if (count != (size_t)-1) { | 41 if (count != (size_t)-1) { |
| 42 wchar_t *tmp; | 42 wchar_t *tmp; |
| 43 /* Only use the result if it contains no | 43 /* Only use the result if it contains no |
| 44 surrogate characters. */ | 44 surrogate characters. */ |
| 45 for (tmp = res; *tmp != 0 && | 45 for (tmp = res; *tmp != 0 && |
| 46 (*tmp < 0xd800 || *tmp > 0xdfff); tmp++) | 46 (*tmp < 0xd800 || *tmp > 0xdfff); tmp++) |
| 47 ; | 47 ; |
| 48 if (*tmp == 0) | 48 if (*tmp == 0) |
| 49 return res; | 49 return res; |
| 50 } | 50 } |
| 51 PyMem_Free(res); | 51 PyMem_Free(res); |
| 52 } | 52 } |
| 53 /* Conversion failed. Fall back to escaping with surrogateescape. */ | 53 /* Conversion failed. Fall back to escaping with surrogateescape. */ |
| 54 #ifdef HAVE_MBRTOWC | 54 #ifdef HAVE_MBRTOWC |
| 55 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */ | 55 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */ |
| 56 | 56 |
| 57 /* Overallocate; as multi-byte characters are in the argument, the | 57 /* Overallocate; as multi-byte characters are in the argument, the |
| 58 actual output could use less memory. */ | 58 actual output could use less memory. */ |
| 59 argsize = strlen(arg) + 1; | 59 argsize = strlen(arg) + 1; |
| 60 res = PyMem_Malloc(argsize*sizeof(wchar_t)); | 60 res = PyMem_Malloc(argsize*sizeof(wchar_t)); |
| 61 if (!res) goto oom; | 61 if (!res) goto oom; |
| 62 in = (unsigned char*)arg; | 62 in = (unsigned char*)arg; |
| 63 out = res; | 63 out = res; |
| 64 memset(&mbs, 0, sizeof mbs); | 64 memset(&mbs, 0, sizeof mbs); |
| 65 while (argsize) { | 65 while (argsize) { |
| 66 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs); | 66 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs); |
| 67 if (converted == 0) | 67 if (converted == 0) |
| 68 /* Reached end of string; null char stored. */ | 68 /* Reached end of string; null char stored. */ |
| 69 break; | 69 break; |
| 70 if (converted == (size_t)-2) { | 70 if (converted == (size_t)-2) { |
| 71 /* Incomplete character. This should never happen, | 71 /* Incomplete character. This should never happen, |
| 72 since we provide everything that we have - | 72 since we provide everything that we have - |
| 73 unless there is a bug in the C library, or I | 73 unless there is a bug in the C library, or I |
| 74 misunderstood how mbrtowc works. */ | 74 misunderstood how mbrtowc works. */ |
| 75 fprintf(stderr, "unexpected mbrtowc result -2\n"); | 75 fprintf(stderr, "unexpected mbrtowc result -2\n"); |
| 76 return NULL; | 76 return NULL; |
| 77 } | 77 } |
| 78 if (converted == (size_t)-1) { | 78 if (converted == (size_t)-1) { |
| 79 /* Conversion error. Escape as UTF-8b, and start over | 79 /* Conversion error. Escape as UTF-8b, and start over |
| 80 in the initial shift state. */ | 80 in the initial shift state. */ |
| 81 *out++ = 0xdc00 + *in++; | 81 *out++ = 0xdc00 + *in++; |
| 82 argsize--; | 82 argsize--; |
| 83 memset(&mbs, 0, sizeof mbs); | 83 memset(&mbs, 0, sizeof mbs); |
| 84 continue; | 84 continue; |
| 85 } | 85 } |
| 86 if (*out >= 0xd800 && *out <= 0xdfff) { | 86 if (*out >= 0xd800 && *out <= 0xdfff) { |
| 87 /* Surrogate character. Escape the original | 87 /* Surrogate character. Escape the original |
| 88 byte sequence with surrogateescape. */ | 88 byte sequence with surrogateescape. */ |
| 89 argsize -= converted; | 89 argsize -= converted; |
| 90 while (converted--) | 90 while (converted--) |
| 91 *out++ = 0xdc00 + *in++; | 91 *out++ = 0xdc00 + *in++; |
| 92 continue; | 92 continue; |
| 93 } | 93 } |
| 94 /* successfully converted some bytes */ | 94 /* successfully converted some bytes */ |
| 95 in += converted; | 95 in += converted; |
| 96 argsize -= converted; | 96 argsize -= converted; |
| 97 out++; | 97 out++; |
| 98 } | 98 } |
| 99 #else | 99 #else |
| 100 /* Cannot use C locale for escaping; manually escape as if charset | 100 /* Cannot use C locale for escaping; manually escape as if charset |
| 101 is ASCII (i.e. escape all bytes > 128. This will still roundtrip | 101 is ASCII (i.e. escape all bytes > 128. This will still roundtrip |
| 102 correctly in the locale's charset, which must be an ASCII superset. */ | 102 correctly in the locale's charset, which must be an ASCII superset. */ |
| 103 res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t)); | 103 res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t)); |
| 104 if (!res) goto oom; | 104 if (!res) goto oom; |
| 105 in = (unsigned char*)arg; | 105 in = (unsigned char*)arg; |
| 106 out = res; | 106 out = res; |
| 107 while(*in) | 107 while(*in) |
| 108 if(*in < 128) | 108 if(*in < 128) |
| 109 *out++ = *in++; | 109 *out++ = *in++; |
| 110 else | 110 else |
| 111 *out++ = 0xdc00 + *in++; | 111 *out++ = 0xdc00 + *in++; |
| 112 *out = 0; | 112 *out = 0; |
| 113 #endif | 113 #endif |
| 114 return res; | 114 return res; |
| 115 oom: | 115 oom: |
| 116 fprintf(stderr, "out of memory\n"); | 116 fprintf(stderr, "out of memory\n"); |
| 117 return NULL; | 117 return NULL; |
| 118 } | 118 } |
| 119 | 119 |
| 120 int | 120 int |
| 121 main(int argc, char **argv) | 121 main(int argc, char **argv) |
| 122 { | 122 { |
| 123 wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc); | 123 wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc); |
| 124 /* We need a second copies, as Python might modify the first one. */ | 124 /* We need a second copies, as Python might modify the first one. */ |
| 125 wchar_t **argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc); | 125 wchar_t **argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc); |
| 126 int i, res; | 126 int i, res; |
| 127 char *oldloc; | 127 char *oldloc; |
| 128 /* 754 requires that FP exceptions run in "no stop" mode by default, | 128 /* 754 requires that FP exceptions run in "no stop" mode by default, |
| 129 * and until C vendors implement C99's ways to control FP exceptions, | 129 * and until C vendors implement C99's ways to control FP exceptions, |
| 130 * Python requires non-stop mode. Alas, some platforms enable FP | 130 * Python requires non-stop mode. Alas, some platforms enable FP |
| 131 * exceptions by default. Here we disable them. | 131 * exceptions by default. Here we disable them. |
| 132 */ | 132 */ |
| 133 #ifdef __FreeBSD__ | 133 #ifdef __FreeBSD__ |
| 134 fp_except_t m; | 134 fp_except_t m; |
| 135 | 135 |
| 136 m = fpgetmask(); | 136 m = fpgetmask(); |
| 137 fpsetmask(m & ~FP_X_OFL); | 137 fpsetmask(m & ~FP_X_OFL); |
| 138 #endif | 138 #endif |
| 139 if (!argv_copy || !argv_copy2) { | 139 if (!argv_copy || !argv_copy2) { |
| 140 fprintf(stderr, "out of memory\n"); | 140 fprintf(stderr, "out of memory\n"); |
| 141 return 1; | 141 return 1; |
| 142 } | 142 } |
| 143 oldloc = strdup(setlocale(LC_ALL, NULL)); | 143 oldloc = strdup(setlocale(LC_ALL, NULL)); |
| 144 setlocale(LC_ALL, ""); | 144 setlocale(LC_ALL, ""); |
| 145 for (i = 0; i < argc; i++) { | 145 for (i = 0; i < argc; i++) { |
| 146 argv_copy2[i] = argv_copy[i] = char2wchar(argv[i]); | 146 argv_copy2[i] = argv_copy[i] = char2wchar(argv[i]); |
| 147 if (!argv_copy[i]) | 147 if (!argv_copy[i]) |
| 148 return 1; | 148 return 1; |
| 149 } | 149 } |
| 150 setlocale(LC_ALL, oldloc); | 150 setlocale(LC_ALL, oldloc); |
| 151 free(oldloc); | 151 free(oldloc); |
| 152 res = Py_Main(argc, argv_copy); | 152 res = Py_Main(argc, argv_copy); |
| 153 for (i = 0; i < argc; i++) { | 153 for (i = 0; i < argc; i++) { |
| 154 PyMem_Free(argv_copy2[i]); | 154 PyMem_Free(argv_copy2[i]); |
| 155 } | 155 } |
| 156 PyMem_Free(argv_copy); | 156 PyMem_Free(argv_copy); |
| 157 PyMem_Free(argv_copy2); | 157 PyMem_Free(argv_copy2); |
| 158 return res; | 158 return res; |
| 159 } | 159 } |
| 160 #endif | 160 #endif |
