@@ -93,21 +93,31 @@ private String[] extractKeywordNames(PKeyword[] keywords) {
93
93
return extractKeywordNames (keywords .length , keywords );
94
94
}
95
95
96
- @ Specialization (guards = {"cachedLen == keywords.length" , "cachedDeclLen == arity.getKeywordNames().length" }, limit = "getVariableArgumentInlineCacheLimit()" )
96
+ @ Specialization (guards = {
97
+ "cachedLen == keywords.length" ,
98
+ "cachedNumParamIds == arity.getNumParameterIds()" ,
99
+ "cachedDeclLen == arity.getNumKeywordNames()"
100
+ }, limit = "getVariableArgumentInlineCacheLimit()" )
97
101
void arityCheck (Arity arity , Object [] arguments , PKeyword [] keywords ,
98
- @ Cached ("arity.getKeywordNames().length" ) int cachedDeclLen ,
102
+ @ Cached ("arity.getNumParameterIds()" ) int cachedNumParamIds ,
103
+ @ Cached ("arity.getNumKeywordNames()" ) int cachedDeclLen ,
99
104
@ Cached ("keywords.length" ) int cachedLen ) {
100
105
String [] kwNames = extractKeywordNames (cachedLen , keywords );
101
- arityCheck (arity , arguments , PArguments .getNumberOfUserArgs (arguments ), cachedDeclLen , cachedLen , kwNames );
106
+ arityCheck (arity , arguments , cachedNumParamIds , PArguments .getNumberOfUserArgs (arguments ), cachedDeclLen , cachedLen , kwNames );
102
107
}
103
108
104
- @ Specialization (guards = {"cachedLen == keywords.length" , "cachedDeclLen == callee.getArity().getKeywordNames().length" }, limit = "getVariableArgumentInlineCacheLimit()" )
109
+ @ Specialization (guards = {
110
+ "cachedLen == keywords.length" ,
111
+ "cachedNumParamIds == callee.getArity().getNumParameterIds()" ,
112
+ "cachedDeclLen == callee.getArity().getNumKeywordNames()"
113
+ }, limit = "getVariableArgumentInlineCacheLimit()" )
105
114
void arityCheckCallable (PythonCallable callee , Object [] arguments , PKeyword [] keywords ,
106
- @ Cached ("callee.getArity().getKeywordNames().length" ) int cachedDeclLen ,
115
+ @ Cached ("callee.getArity().getNumParameterIds()" ) int cachedNumParamIds ,
116
+ @ Cached ("callee.getArity().getNumKeywordNames()" ) int cachedDeclLen ,
107
117
@ Cached ("keywords.length" ) int cachedLen ) {
108
118
String [] kwNames = extractKeywordNames (cachedLen , keywords );
109
119
Arity arity = callee .getArity ();
110
- arityCheck (arity , arguments , PArguments .getNumberOfUserArgs (arguments ), cachedDeclLen , cachedLen , kwNames );
120
+ arityCheck (arity , arguments , cachedNumParamIds , PArguments .getNumberOfUserArgs (arguments ), cachedDeclLen , cachedLen , kwNames );
111
121
}
112
122
113
123
@ Specialization (replaces = "arityCheck" )
@@ -122,13 +132,23 @@ void uncachedCheckCallable(PythonCallable callee, Object[] arguments, PKeyword[]
122
132
arityCheck (callee .getArity (), arguments , PArguments .getNumberOfUserArgs (arguments ), kwNames );
123
133
}
124
134
135
+ @ TruffleBoundary
125
136
private void arityCheck (Arity arity , Object [] arguments , int numOfArgs , String [] keywords ) {
126
- arityCheck (arity , arguments , numOfArgs , arity .getKeywordNames ().length , keywords .length , keywords );
137
+ arityCheck (arity , arguments , arity .getParameterIds ().length , numOfArgs , arity .getKeywordNames ().length , keywords .length , keywords );
138
+ }
139
+
140
+ private void arityCheck (Arity arity , Object [] arguments , int numParameterIds , int numOfArgs , int numOfKeywordsDeclared , int numOfKeywordsGiven , String [] keywords ) {
141
+ checkPositional (arity , arguments , numParameterIds , numOfArgs , keywords );
142
+ checkKeywords (arity , numOfKeywordsDeclared , numOfKeywordsGiven , keywords );
127
143
}
128
144
129
- private void checkPositional (Arity arity , Object [] arguments , int numOfArgs , String [] keywords ) {
145
+ private void checkPositional (Arity arity , Object [] arguments , int numParameterIds , int numOfArgs , String [] keywords ) {
130
146
// check missing paramIds
131
- checkMissingPositionalParamIds (arity , arguments );
147
+ int cntMissingPositional = countMissingPositionalParamIds (numParameterIds , arguments );
148
+
149
+ if (missingPositionalArgs .profile (cntMissingPositional > 0 )) {
150
+ throw raise (TypeError , getMissingPositionalArgsErrorMessage (arity , arguments , cntMissingPositional ));
151
+ }
132
152
133
153
if (lessPositionalArgs .profile (numOfArgs < arity .getMinNumOfPositionalArgs ())) {
134
154
throw raise (TypeError , "%s() takes %s %d positional argument%s (%d given)" ,
@@ -142,19 +162,38 @@ private void checkPositional(Arity arity, Object[] arguments, int numOfArgs, Str
142
162
}
143
163
}
144
164
165
+ private void checkKeywords (Arity arity , int numOfKeywordsDeclared , int numOfKeywordsGiven , String [] keywords ) {
166
+ int cntGivenRequiredKeywords = countGivenRequiredKeywords (arity , numOfKeywordsDeclared , numOfKeywordsGiven , keywords );
167
+
168
+ if (missingKeywordArgs .profile (arity .takesRequiredKeywordArgs () && cntGivenRequiredKeywords < arity .getNumOfRequiredKeywords ())) {
169
+ throw raise (TypeError , getMissingRequiredKeywordsErrorMessage (arity , cntGivenRequiredKeywords , keywords ));
170
+ } else if (noKeywordArgs .profile (!arity .takesKeywordArgs () && numOfKeywordsGiven > 0 )) {
171
+ throw raise (TypeError , "%s() takes no keyword arguments" ,
172
+ arity .getFunctionName ());
173
+ }
174
+ }
175
+
145
176
@ ExplodeLoop
146
- private void checkMissingPositionalParamIds ( Arity arity , Object [] arguments ) {
177
+ private int countMissingPositionalParamIds ( int numParameterIds , Object [] arguments ) {
147
178
int cntMissingPositional = 0 ;
148
- String [] parameterIds = arity .getParameterIds ();
149
- for (int i = 0 ; i < parameterIds .length ; i ++) {
179
+ for (int i = 0 ; i < numParameterIds ; i ++) {
150
180
if (PArguments .getArgument (arguments , i ) == null ) {
151
181
cntMissingPositional += 1 ;
152
182
}
153
183
}
184
+ return cntMissingPositional ;
185
+ }
154
186
155
- if (missingPositionalArgs .profile (cntMissingPositional > 0 )) {
156
- throw raise (TypeError , getMissingPositionalArgsErrorMessage (arity , arguments , cntMissingPositional ));
187
+ @ ExplodeLoop
188
+ private int countGivenRequiredKeywords (Arity arity , int numOfKeywordsDeclared , int numOfKeywordsGiven , String [] keywords ) {
189
+ int cntGivenRequiredKeywords = 0 ;
190
+
191
+ for (int i = 0 ; i < numOfKeywordsGiven ; i ++) {
192
+ String keyword = keywords [i ];
193
+ cntGivenRequiredKeywords += checkKeyword (arity , keyword , numOfKeywordsDeclared );
157
194
}
195
+
196
+ return cntGivenRequiredKeywords ;
158
197
}
159
198
160
199
@ ExplodeLoop (kind = ExplodeLoop .LoopExplosionKind .FULL_EXPLODE_UNTIL_RETURN )
@@ -176,22 +215,6 @@ private int checkKeyword(Arity arity, String keyword, int length) {
176
215
return 0 ;
177
216
}
178
217
179
- @ ExplodeLoop
180
- private void checkKeywords (Arity arity , int numOfKeywordsDeclared , int numOfKeywordsGiven , String [] keywords ) {
181
- int cntGivenRequiredKeywords = 0 ;
182
-
183
- for (int i = 0 ; i < numOfKeywordsGiven ; i ++) {
184
- String keyword = keywords [i ];
185
- cntGivenRequiredKeywords += checkKeyword (arity , keyword , numOfKeywordsDeclared );
186
- }
187
-
188
- if (missingKeywordArgs .profile (arity .takesRequiredKeywordArgs () && cntGivenRequiredKeywords < arity .getNumOfRequiredKeywords ())) {
189
- throw raise (TypeError , getMissingRequiredKeywordsErrorMessage (arity , cntGivenRequiredKeywords , keywords ));
190
- } else if (noKeywordArgs .profile (!arity .takesKeywordArgs () && numOfKeywordsGiven > 0 )) {
191
- throw raise (TypeError , "%s() takes no keyword arguments" , arity .getFunctionName ());
192
- }
193
- }
194
-
195
218
@ TruffleBoundary
196
219
private String getMissingRequiredKeywordsErrorMessage (Arity arity , int cntGivenRequiredKeywords , String [] givenKeywords ) {
197
220
int missingRequiredKeywords = arity .getNumOfRequiredKeywords () - cntGivenRequiredKeywords ;
@@ -281,9 +304,4 @@ private String getExtraPositionalArgsErrorMessage(Arity arity, int numOfArgs, St
281
304
numOfArgs ,
282
305
givenCountMessage );
283
306
}
284
-
285
- private void arityCheck (Arity arity , Object [] arguments , int numOfArgs , int numOfKeywordsDeclared , int numOfKeywordsGiven , String [] keywords ) {
286
- checkPositional (arity , arguments , numOfArgs , keywords );
287
- checkKeywords (arity , numOfKeywordsDeclared , numOfKeywordsGiven , keywords );
288
- }
289
307
}
0 commit comments