annotate Modules/python.c @ 46318:22a74eaf6b22

Change command line processing API to use wchar_t. Fixes #2128.
author Martin v. Löwis <martin@v.loewis.de>
date Sat, 05 Apr 2008 20:41:37 +0000
parents 8eccfae04bb4
children f83dccf417f0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5853
92bcf4cc9421 This is now the "real" main program -- it calls Py_Main(argc, argv)
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Minimal main program -- everything is loaded from the library */
92bcf4cc9421 This is now the "real" main program -- it calls Py_Main(argc, argv)
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
8894
764de731c806 Now include Python.h
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3 #include "Python.h"
46318
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
4 #include <locale.h>
8894
764de731c806 Now include Python.h
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
5
26815
8eccfae04bb4 Apparently FreeBSD enables some HW floating-point exceptions by default.
Tim Peters <tim.peters@gmail.com>
parents: 24471
diff changeset
6 #ifdef __FreeBSD__
8eccfae04bb4 Apparently FreeBSD enables some HW floating-point exceptions by default.
Tim Peters <tim.peters@gmail.com>
parents: 24471
diff changeset
7 #include <floatingpoint.h>
8eccfae04bb4 Apparently FreeBSD enables some HW floating-point exceptions by default.
Tim Peters <tim.peters@gmail.com>
parents: 24471
diff changeset
8 #endif
8eccfae04bb4 Apparently FreeBSD enables some HW floating-point exceptions by default.
Tim Peters <tim.peters@gmail.com>
parents: 24471
diff changeset
9
46318
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
10 #ifdef MS_WINDOWS
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
11 int
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
12 wmain(int argc, wchar_t **argv)
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
13 {
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
14 return Py_Main(argc, argv);
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
15 }
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
16 #else
6055
72d004971b26 Keep gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 5853
diff changeset
17 int
13481
3abecb34737f - ANSI-fication
Fredrik Lundh <fredrik@pythonware.com>
parents: 9601
diff changeset
18 main(int argc, char **argv)
5853
92bcf4cc9421 This is now the "real" main program -- it calls Py_Main(argc, argv)
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 {
46318
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
20 wchar_t **argv_copy = PyMem_Malloc(sizeof(wchar_t*)*argc);
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
21 /* We need a second copies, as Python might modify the first one. */
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
22 wchar_t **argv_copy2 = PyMem_Malloc(sizeof(wchar_t*)*argc);
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
23 int i, res;
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
24 char *oldloc;
26815
8eccfae04bb4 Apparently FreeBSD enables some HW floating-point exceptions by default.
Tim Peters <tim.peters@gmail.com>
parents: 24471
diff changeset
25 /* 754 requires that FP exceptions run in "no stop" mode by default,
8eccfae04bb4 Apparently FreeBSD enables some HW floating-point exceptions by default.
Tim Peters <tim.peters@gmail.com>
parents: 24471
diff changeset
26 * and until C vendors implement C99's ways to control FP exceptions,
8eccfae04bb4 Apparently FreeBSD enables some HW floating-point exceptions by default.
Tim Peters <tim.peters@gmail.com>
parents: 24471
diff changeset
27 * Python requires non-stop mode. Alas, some platforms enable FP
8eccfae04bb4 Apparently FreeBSD enables some HW floating-point exceptions by default.
Tim Peters <tim.peters@gmail.com>
parents: 24471
diff changeset
28 * exceptions by default. Here we disable them.
8eccfae04bb4 Apparently FreeBSD enables some HW floating-point exceptions by default.
Tim Peters <tim.peters@gmail.com>
parents: 24471
diff changeset
29 */
8eccfae04bb4 Apparently FreeBSD enables some HW floating-point exceptions by default.
Tim Peters <tim.peters@gmail.com>
parents: 24471
diff changeset
30 #ifdef __FreeBSD__
8eccfae04bb4 Apparently FreeBSD enables some HW floating-point exceptions by default.
Tim Peters <tim.peters@gmail.com>
parents: 24471
diff changeset
31 fp_except_t m;
8eccfae04bb4 Apparently FreeBSD enables some HW floating-point exceptions by default.
Tim Peters <tim.peters@gmail.com>
parents: 24471
diff changeset
32
8eccfae04bb4 Apparently FreeBSD enables some HW floating-point exceptions by default.
Tim Peters <tim.peters@gmail.com>
parents: 24471
diff changeset
33 m = fpgetmask();
8eccfae04bb4 Apparently FreeBSD enables some HW floating-point exceptions by default.
Tim Peters <tim.peters@gmail.com>
parents: 24471
diff changeset
34 fpsetmask(m & ~FP_X_OFL);
8eccfae04bb4 Apparently FreeBSD enables some HW floating-point exceptions by default.
Tim Peters <tim.peters@gmail.com>
parents: 24471
diff changeset
35 #endif
46318
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
36 if (!argv_copy || !argv_copy2) {
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
37 fprintf(stderr, "out of memory");
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
38 return 1;
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
39 }
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
40 oldloc = setlocale(LC_ALL, NULL);
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
41 setlocale(LC_ALL, "");
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
42 for (i = 0; i < argc; i++) {
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
43 size_t argsize = mbstowcs(NULL, argv[i], 0);
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
44 if (argsize == (size_t)-1) {
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
45 fprintf(stderr, "Could not convert argument %d to string", i);
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
46 return 1;
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
47 }
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
48 argv_copy[i] = PyMem_Malloc((argsize+1)*sizeof(wchar_t));
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
49 argv_copy2[i] = argv_copy[i];
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
50 if (!argv_copy[i]) {
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
51 fprintf(stderr, "out of memory");
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
52 return 1;
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
53 }
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
54 mbstowcs(argv_copy[i], argv[i], argsize+1);
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
55 }
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
56 setlocale(LC_ALL, oldloc);
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
57 res = Py_Main(argc, argv_copy);
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
58 for (i = 0; i < argc; i++) {
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
59 PyMem_Free(argv_copy2[i]);
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
60 }
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
61 PyMem_Free(argv_copy);
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
62 PyMem_Free(argv_copy2);
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
63 return res;
5853
92bcf4cc9421 This is now the "real" main program -- it calls Py_Main(argc, argv)
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 }
46318
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 26815
diff changeset
65 #endif