annotate Python/getopt.c @ 69888:8752a81ada2d

faulthandler: dump all threads by default * Set the default value of all_threads arguments to True * Py_FatalError() dumps all threads, instead of only the current thread Dump only the current thread is not reliable. In some cases, Python is unable to retrieve the state of the current thread and so is unable to dump the traceback. faulthandler keeps a reference to the interpreter and so is always able to dump the traceback of all threads.
author Victor Stinner <victor.stinner@haypocalc.com>
date Sat, 07 May 2011 12:43:00 +0200
parents 0b5bfba7a907
children 0e8bd4785ea0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1598
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
1 /*---------------------------------------------------------------------------*
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
2 * <RCS keywords>
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
3 *
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
4 * C++ Library
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
5 *
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
6 * Copyright 1992-1994, David Gottner
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
7 *
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
8 * All Rights Reserved
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
9 *
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
10 * Permission to use, copy, modify, and distribute this software and its
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
11 * documentation for any purpose and without fee is hereby granted,
1598
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
12 * provided that the above copyright notice, this permission notice and
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
13 * the following disclaimer notice appear unmodified in all copies.
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
14 *
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
15 * I DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL I
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
17 * BE LIABLE FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
18 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
19 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
20 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
21 *
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
22 * Nevertheless, I would like to know about bugs in this library or
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
23 * suggestions for improvment. Send bug reports and feedback to
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
24 * davegottner@delphi.com.
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
25 *---------------------------------------------------------------------------*/
1585
7c75f60468f8 A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
27 /* Modified to support --help and --version, as well as /? on Windows
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
28 * by Georg Brandl. */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
29
46318
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 39442
diff changeset
30 #include <Python.h>
1585
7c75f60468f8 A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 #include <stdio.h>
7c75f60468f8 A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 #include <string.h>
46318
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 39442
diff changeset
33 #include <wchar.h>
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 39442
diff changeset
34 #include <pygetopt.h>
1585
7c75f60468f8 A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 15374
diff changeset
36 #ifdef __cplusplus
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 15374
diff changeset
37 extern "C" {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 15374
diff changeset
38 #endif
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 15374
diff changeset
39
15374
a826a46bae54 Move our own getopt() implementation to _PyOS_GetOpt(), and use it
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
40 int _PyOS_opterr = 1; /* generate error messages */
a826a46bae54 Move our own getopt() implementation to _PyOS_GetOpt(), and use it
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
41 int _PyOS_optind = 1; /* index into argv array */
46318
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 39442
diff changeset
42 wchar_t *_PyOS_optarg = NULL; /* optional argument */
1585
7c75f60468f8 A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
46318
22a74eaf6b22 Change command line processing API to use wchar_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 39442
diff changeset
44 int _PyOS_GetOpt(int argc, wchar_t **argv, wchar_t *optstring)
1585
7c75f60468f8 A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
46 static wchar_t *opt_ptr = L"";
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
47 wchar_t *ptr;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
48 wchar_t option;
1585
7c75f60468f8 A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
50 if (*opt_ptr == '\0') {
1598
ecbadb818847 New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents: 1585
diff changeset
51
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
52 if (_PyOS_optind >= argc)
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
53 return -1;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
54 #ifdef MS_WINDOWS
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
55 else if (wcscmp(argv[_PyOS_optind], L"/?") == 0) {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
56 ++_PyOS_optind;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
57 return 'h';
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
58 }
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
59 #endif
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
60
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
61 else if (argv[_PyOS_optind][0] != L'-' ||
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
62 argv[_PyOS_optind][1] == L'\0' /* lone dash */ )
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
63 return -1;
1585
7c75f60468f8 A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
65 else if (wcscmp(argv[_PyOS_optind], L"--") == 0) {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
66 ++_PyOS_optind;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
67 return -1;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
68 }
1585
7c75f60468f8 A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
70 else if (wcscmp(argv[_PyOS_optind], L"--help") == 0) {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
71 ++_PyOS_optind;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
72 return 'h';
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
73 }
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
74
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
75 else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
76 ++_PyOS_optind;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
77 return 'V';
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
78 }
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
79
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
80
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
81 opt_ptr = &argv[_PyOS_optind++][1];
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
82 }
1585
7c75f60468f8 A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
84 if ( (option = *opt_ptr++) == L'\0')
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
85 return -1;
46430
47d9bbe10897 Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46318
diff changeset
86
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
87 if (option == 'J') {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
88 fprintf(stderr, "-J is reserved for Jython\n");
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
89 return '_';
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
90 }
46430
47d9bbe10897 Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46318
diff changeset
91
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
92 if ((ptr = wcschr(optstring, option)) == NULL) {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
93 if (_PyOS_opterr)
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
94 fprintf(stderr, "Unknown option: -%c\n", (char)option);
1585
7c75f60468f8 A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
96 return '_';
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
97 }
1585
7c75f60468f8 A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
99 if (*(ptr + 1) == L':') {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
100 if (*opt_ptr != L'\0') {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
101 _PyOS_optarg = opt_ptr;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
102 opt_ptr = L"";
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
103 }
1585
7c75f60468f8 A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
105 else {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
106 if (_PyOS_optind >= argc) {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
107 if (_PyOS_opterr)
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
108 fprintf(stderr,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
109 "Argument expected for the -%c option\n", (char)option);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
110 return '_';
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
111 }
1585
7c75f60468f8 A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
113 _PyOS_optarg = argv[_PyOS_optind++];
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
114 }
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
115 }
1585
7c75f60468f8 A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 46453
diff changeset
117 return option;
1585
7c75f60468f8 A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 }
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 15374
diff changeset
119
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 15374
diff changeset
120 #ifdef __cplusplus
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 15374
diff changeset
121 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 15374
diff changeset
122 #endif
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 15374
diff changeset
123