Skip to content

Commit fbf7d38

Browse files
committed
Add local copy of read-only members
1 parent c6689f6 commit fbf7d38

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

‎Python/getopt.c‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,17 @@ _PyOS_GetOpt_Init(struct _PyOS_GetOpt *getopt,
6565
int
6666
_PyOS_GetOpt(struct _PyOS_GetOpt *getopt)
6767
{
68+
// Local copy of read-only members to omit "getopt->"
69+
const Py_ssize_t argc = getopt->argc;
70+
wchar_t * const *argv = getopt->argv;
71+
const int error = getopt->error;
72+
6873
if (*getopt->ptr == '\0') {
69-
if (getopt->index >= getopt->argc) {
74+
if (getopt->index >= argc) {
7075
return -1;
7176
}
7277

73-
const wchar_t *arg = getopt->argv[getopt->index];
78+
const wchar_t *arg = argv[getopt->index];
7479
if (arg[0] != L'-' || arg[1] == L'\0' /* lone dash */ ) {
7580
return -1;
7681
}
@@ -94,7 +99,7 @@ _PyOS_GetOpt(struct _PyOS_GetOpt *getopt)
9499
return 'V';
95100
}
96101

97-
getopt->ptr = &getopt->argv[getopt->index++][1];
102+
getopt->ptr = &argv[getopt->index++][1];
98103
}
99104

100105
wchar_t option = *getopt->ptr++;
@@ -105,7 +110,7 @@ _PyOS_GetOpt(struct _PyOS_GetOpt *getopt)
105110
if (option == L'-') {
106111
// Parse long option.
107112
if (*getopt->ptr == L'\0') {
108-
if (getopt->error) {
113+
if (error) {
109114
fprintf(stderr, "Expected long option\n");
110115
}
111116
return -1;
@@ -119,8 +124,8 @@ _PyOS_GetOpt(struct _PyOS_GetOpt *getopt)
119124
}
120125

121126
if (!opt->name) {
122-
if (getopt->error) {
123-
fprintf(stderr, "Unknown option: %ls\n", getopt->argv[getopt->index - 1]);
127+
if (error) {
128+
fprintf(stderr, "Unknown option: %ls\n", argv[getopt->index - 1]);
124129
}
125130
return '_';
126131
}
@@ -129,20 +134,20 @@ _PyOS_GetOpt(struct _PyOS_GetOpt *getopt)
129134
if (!opt->has_arg) {
130135
return opt->val;
131136
}
132-
if (getopt->index >= getopt->argc) {
133-
if (getopt->error) {
137+
if (getopt->index >= argc) {
138+
if (error) {
134139
fprintf(stderr, "Argument expected for the %ls options\n",
135-
getopt->argv[getopt->index - 1]);
140+
argv[getopt->index - 1]);
136141
}
137142
return '_';
138143
}
139-
getopt->arg = getopt->argv[getopt->index++];
144+
getopt->arg = argv[getopt->index++];
140145
return opt->val;
141146
}
142147

143148
wchar_t *ptr = wcschr(SHORT_OPTS, option);
144149
if (ptr == NULL) {
145-
if (getopt->error) {
150+
if (error) {
146151
fprintf(stderr, "Unknown option: -%c\n", (char)option);
147152
}
148153
return '_';
@@ -154,15 +159,15 @@ _PyOS_GetOpt(struct _PyOS_GetOpt *getopt)
154159
getopt->ptr = L"";
155160
}
156161
else {
157-
if (getopt->index >= getopt->argc) {
158-
if (getopt->error) {
162+
if (getopt->index >= argc) {
163+
if (error) {
159164
fprintf(stderr,
160165
"Argument expected for the -%c option\n", (char)option);
161166
}
162167
return '_';
163168
}
164169

165-
getopt->arg = getopt->argv[getopt->index++];
170+
getopt->arg = argv[getopt->index++];
166171
}
167172
}
168173

0 commit comments

Comments
 (0)