@@ -1140,6 +1140,7 @@ private HashingCollectionNodes.SetItemNode getSetItemNode() {
1140
1140
// int(x, base=10)
1141
1141
@ Builtin (name = INT , minNumOfPositionalArgs = 1 , parameterNames = {"cls" , "x" , "base" }, numOfPositionalOnlyArgs = 2 , constructsClass = PythonBuiltinClassType .PInt )
1142
1142
@ GenerateNodeFactory
1143
+ @ ReportPolymorphism
1143
1144
public abstract static class IntNode extends PythonTernaryBuiltinNode {
1144
1145
1145
1146
private final ConditionProfile invalidBase = ConditionProfile .createBinaryProfile ();
@@ -1528,8 +1529,9 @@ Object fail(Object cls, Object arg, Object base) {
1528
1529
throw raise (TypeError , ErrorMessages .INT_CANT_CONVERT_STRING_WITH_EXPL_BASE );
1529
1530
}
1530
1531
1531
- @ Specialization (guards = {"isNoValue(base)" , "!isNoValue(obj)" , "!isHandledType(obj)" })
1532
- Object createIntGeneric (VirtualFrame frame , Object cls , Object obj , @ SuppressWarnings ("unused" ) PNone base ) {
1532
+ @ Specialization (guards = {"isNoValue(base)" , "!isNoValue(obj)" , "!isHandledType(obj)" }, limit = "2" )
1533
+ Object createIntGeneric (VirtualFrame frame , Object cls , Object obj , @ SuppressWarnings ("unused" ) PNone base ,
1534
+ @ CachedLibrary ("obj" ) PythonObjectLibrary lib ) {
1533
1535
// This method (together with callInt and callIndex) reflects the logic of PyNumber_Long
1534
1536
// in CPython. We don't use PythonObjectLibrary here since the original CPython function
1535
1537
// does not use any of the conversion functions (such as _PyLong_AsInt or
@@ -1545,7 +1547,17 @@ Object createIntGeneric(VirtualFrame frame, Object cls, Object obj, @SuppressWar
1545
1547
if (result == PNone .NO_VALUE ) {
1546
1548
Object truncResult = callTrunc (frame , obj );
1547
1549
if (truncResult == PNone .NO_VALUE ) {
1548
- throw raise (TypeError , ErrorMessages .ARG_MUST_BE_STRING_OR_BYTELIKE_OR_NUMBER , "int()" , obj );
1550
+ if (lib .isBuffer (obj )) {
1551
+ try {
1552
+ byte [] bytes = lib .getBufferBytes (obj );
1553
+ return stringToInt (frame , cls , toString (bytes ), 10 , obj );
1554
+ } catch (UnsupportedMessageException e ) {
1555
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
1556
+ throw new IllegalStateException ("Object claims to be a buffer but does not support getBufferBytes()" );
1557
+ }
1558
+ } else {
1559
+ throw raise (TypeError , ErrorMessages .ARG_MUST_BE_STRING_OR_BYTELIKE_OR_NUMBER , "int()" , obj );
1560
+ }
1549
1561
}
1550
1562
if (isIntegerType (truncResult )) {
1551
1563
result = truncResult ;
0 commit comments