70
70
import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
71
71
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
72
72
import com .oracle .graal .python .nodes .truffle .PythonArithmeticTypes ;
73
- import com .oracle .graal .python .nodes .util .CoerceToIntegerNode ;
74
73
import com .oracle .graal .python .runtime .PythonCore ;
75
74
import com .oracle .graal .python .runtime .exception .PException ;
76
75
import com .oracle .truffle .api .CompilerDirectives ;
@@ -226,7 +225,6 @@ private ReadAttributeFromObjectNode getAttrNode() {
226
225
abstract static class B2aBase64Node extends PythonBinaryBuiltinNode {
227
226
228
227
@ Child private SequenceStorageNodes .ToByteArrayNode toByteArray ;
229
- @ Child private CoerceToIntegerNode castToIntNode ;
230
228
@ Child private B2aBase64Node recursiveNode ;
231
229
232
230
private SequenceStorageNodes .ToByteArrayNode getToByteArrayNode () {
@@ -237,16 +235,6 @@ private SequenceStorageNodes.ToByteArrayNode getToByteArrayNode() {
237
235
return toByteArray ;
238
236
}
239
237
240
- private CoerceToIntegerNode getCastToIntNode () {
241
- if (castToIntNode == null ) {
242
- CompilerDirectives .transferToInterpreterAndInvalidate ();
243
- castToIntNode = insert (CoerceToIntegerNode .create (val -> {
244
- throw raise (PythonBuiltinClassType .TypeError , "an integer is required (got type %p)" , val );
245
- }));
246
- }
247
- return castToIntNode ;
248
- }
249
-
250
238
private B2aBase64Node getRecursiveNode () {
251
239
if (recursiveNode == null ) {
252
240
CompilerDirectives .transferToInterpreterAndInvalidate ();
@@ -279,14 +267,16 @@ PBytes b2aBytesLike(PIBytesLike data, PInt newline) {
279
267
return b2a (getToByteArrayNode ().execute (data .getSequenceStorage ()), !newline .isZero ());
280
268
}
281
269
282
- @ Specialization
283
- PBytes b2aBytesLike (VirtualFrame frame , PIBytesLike data , Object newline ) {
284
- return (PBytes ) getRecursiveNode ().execute (frame , data , getCastToIntNode ().execute (newline ));
270
+ @ Specialization (limit = "1" )
271
+ PBytes b2aBytesLike (VirtualFrame frame , PIBytesLike data , Object newline ,
272
+ @ CachedLibrary ("newline" ) PythonObjectLibrary lib ) {
273
+ return (PBytes ) getRecursiveNode ().execute (frame , data , asPInt (newline , lib ));
285
274
}
286
275
287
276
@ Specialization (guards = "isNoValue(newline)" )
288
- PBytes b2aArray (VirtualFrame frame , PArray data , @ SuppressWarnings ("unused" ) PNone newline ) {
289
- return b2aArray (frame , data , 1 );
277
+ PBytes b2aArray (VirtualFrame frame , PArray data , @ SuppressWarnings ("unused" ) PNone newline ,
278
+ @ CachedLibrary (limit = "1" ) PythonObjectLibrary lib ) {
279
+ return b2aArray (frame , data , 1 , lib );
290
280
}
291
281
292
282
@ Specialization
@@ -299,9 +289,10 @@ PBytes b2aArray(PArray data, PInt newline) {
299
289
return b2a (getToByteArrayNode ().execute (data .getSequenceStorage ()), !newline .isZero ());
300
290
}
301
291
302
- @ Specialization
303
- PBytes b2aArray (VirtualFrame frame , PArray data , Object newline ) {
304
- return (PBytes ) getRecursiveNode ().execute (frame , data , getCastToIntNode ().execute (newline ));
292
+ @ Specialization (limit = "1" )
293
+ PBytes b2aArray (VirtualFrame frame , PArray data , Object newline ,
294
+ @ CachedLibrary ("newline" ) PythonObjectLibrary lib ) {
295
+ return (PBytes ) getRecursiveNode ().execute (frame , data , asPInt (newline , lib ));
305
296
}
306
297
307
298
@ Specialization (guards = "isNoValue(newline)" )
@@ -329,15 +320,23 @@ PBytes b2aMmeory(VirtualFrame frame, PMemoryView data, PInt newline,
329
320
return b2aMemory (frame , data , newline .isZero () ? 0 : 1 , toBytesNode , isBytesProfile );
330
321
}
331
322
332
- @ Specialization
333
- PBytes b2aMmeory (VirtualFrame frame , PMemoryView data , Object newline ) {
334
- return (PBytes ) getRecursiveNode ().execute (frame , data , getCastToIntNode ().execute (newline ));
323
+ @ Specialization (limit = "1" )
324
+ PBytes b2aMmeory (VirtualFrame frame , PMemoryView data , Object newline ,
325
+ @ CachedLibrary ("newline" ) PythonObjectLibrary lib ) {
326
+ return (PBytes ) getRecursiveNode ().execute (frame , data , asPInt (newline , lib ));
335
327
}
336
328
337
329
@ Fallback
338
330
PBytes b2sGeneral (Object data , @ SuppressWarnings ("unused" ) Object newline ) {
339
331
throw raise (PythonBuiltinClassType .TypeError , "a bytes-like object is required, not '%p'" , data );
340
332
}
333
+
334
+ private Object asPInt (Object obj , PythonObjectLibrary lib ) {
335
+ if (lib .canBePInt (obj )) {
336
+ return lib .asPInt (obj );
337
+ }
338
+ throw raise (PythonBuiltinClassType .TypeError , "an integer is required (got type %p)" , obj );
339
+ }
341
340
}
342
341
343
342
@ Builtin (name = "b2a_hex" , minNumOfPositionalArgs = 1 )
0 commit comments