64
64
import static com .oracle .graal .python .runtime .exception .PythonErrorType .SystemError ;
65
65
import static com .oracle .graal .python .util .PythonUtils .TS_ENCODING ;
66
66
import static com .oracle .graal .python .util .PythonUtils .toTruffleStringUncached ;
67
+ import static com .oracle .truffle .api .CompilerDirectives .shouldNotReachHere ;
68
+ import static com .oracle .truffle .api .CompilerDirectives .transferToInterpreter ;
67
69
68
70
import java .util .regex .Matcher ;
69
71
import java .util .regex .Pattern ;
@@ -212,7 +214,7 @@ public abstract static class SubtypeNew extends Node {
212
214
* tget the <code>typename_subtype_new</code> function
213
215
*/
214
216
protected NativeCAPISymbol getFunction () {
215
- throw CompilerDirectives . shouldNotReachHere ();
217
+ throw shouldNotReachHere ();
216
218
}
217
219
218
220
protected abstract Object execute (Object object , Object arg );
@@ -238,7 +240,7 @@ Object callNativeConstructor(Object object, Object arg,
238
240
Object result = interopLibrary .execute (importCAPISymbolNode .execute (inliningTarget , cApiContext , getFunction ()), toSulongNode .execute (object ), arg );
239
241
return toJavaNode .execute (result );
240
242
} catch (UnsupportedMessageException | UnsupportedTypeException | ArityException e ) {
241
- throw CompilerDirectives . shouldNotReachHere ("C subtype_new function failed" , e );
243
+ throw shouldNotReachHere ("C subtype_new function failed" , e );
242
244
}
243
245
}
244
246
}
@@ -839,7 +841,7 @@ static Object doWithoutContext(NativeCAPISymbol name, Object[] args,
839
841
return ensureTruffleStringNode .execute (inliningTarget , interopLibrary .execute (importCExtSymbolNode .execute (inliningTarget , cApiContext , name ), args ));
840
842
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e ) {
841
843
// consider these exceptions to be fatal internal errors
842
- throw CompilerDirectives . shouldNotReachHere (e );
844
+ throw shouldNotReachHere (e );
843
845
}
844
846
}
845
847
@@ -1126,7 +1128,7 @@ static Object doNativeObject(Object object, long value,
1126
1128
long refCount = CApiTransitions .readNativeRefCount (pointer );
1127
1129
CApiTransitions .writeNativeRefCount (pointer , refCount + value );
1128
1130
} catch (UnsupportedMessageException e ) {
1129
- throw CompilerDirectives . shouldNotReachHere (e );
1131
+ throw shouldNotReachHere (e );
1130
1132
}
1131
1133
}
1132
1134
return object ;
@@ -1219,7 +1221,7 @@ static Object resolveGeneric(Node inliningTarget, Object pointerObject,
1219
1221
try {
1220
1222
pointer = lib .asPointer (pointerObject );
1221
1223
} catch (UnsupportedMessageException e ) {
1222
- throw CompilerDirectives . shouldNotReachHere (e );
1224
+ throw shouldNotReachHere (e );
1223
1225
}
1224
1226
lookup = CApiTransitions .lookupNative (pointer );
1225
1227
if (lookup != null ) {
@@ -1427,7 +1429,7 @@ Object doGeneric(TruffleString f, Object vaList) {
1427
1429
// That should really not happen because we created the unicode
1428
1430
// object with FromCharPointerNode which guarantees to return a
1429
1431
// String/PString.
1430
- throw CompilerDirectives . shouldNotReachHere ();
1432
+ throw shouldNotReachHere ();
1431
1433
}
1432
1434
vaArgIdx ++;
1433
1435
valid = true ;
@@ -1516,7 +1518,7 @@ private static int getAndCastToInt(InteropLibrary lib, PRaiseNode raiseNode, Obj
1516
1518
try {
1517
1519
return lib .asInt (value );
1518
1520
} catch (UnsupportedMessageException e ) {
1519
- throw CompilerDirectives . shouldNotReachHere ();
1521
+ throw shouldNotReachHere ();
1520
1522
}
1521
1523
}
1522
1524
if (!lib .isPointer (value )) {
@@ -1526,7 +1528,7 @@ private static int getAndCastToInt(InteropLibrary lib, PRaiseNode raiseNode, Obj
1526
1528
try {
1527
1529
return (int ) lib .asPointer (value );
1528
1530
} catch (UnsupportedMessageException e ) {
1529
- throw CompilerDirectives . shouldNotReachHere ();
1531
+ throw shouldNotReachHere ();
1530
1532
}
1531
1533
}
1532
1534
throw raiseNode .raise (PythonBuiltinClassType .SystemError , ErrorMessages .P_OBJ_CANT_BE_INTEPRETED_AS_INTEGER , value );
@@ -1541,7 +1543,7 @@ private static long castToLong(InteropLibrary lib, PRaiseNode raiseNode, Object
1541
1543
try {
1542
1544
return lib .asLong (value );
1543
1545
} catch (UnsupportedMessageException e ) {
1544
- throw CompilerDirectives . shouldNotReachHere ();
1546
+ throw shouldNotReachHere ();
1545
1547
}
1546
1548
}
1547
1549
if (!lib .isPointer (value )) {
@@ -1551,7 +1553,7 @@ private static long castToLong(InteropLibrary lib, PRaiseNode raiseNode, Object
1551
1553
try {
1552
1554
return lib .asPointer (value );
1553
1555
} catch (UnsupportedMessageException e ) {
1554
- throw CompilerDirectives . shouldNotReachHere ();
1556
+ throw shouldNotReachHere ();
1555
1557
}
1556
1558
}
1557
1559
throw raiseNode .raise (PythonBuiltinClassType .SystemError , ErrorMessages .P_OBJ_CANT_BE_INTEPRETED_AS_INTEGER , value );
@@ -1681,7 +1683,7 @@ static Object doGeneric(CApiContext capiContext, ModuleSpec moduleSpec, Object m
1681
1683
ErrorMessages .CREATION_FAILD_WITHOUT_EXCEPTION , ErrorMessages .CREATION_RAISED_EXCEPTION );
1682
1684
module = toJavaNode .execute (result );
1683
1685
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e ) {
1684
- throw CompilerDirectives . shouldNotReachHere (e );
1686
+ throw shouldNotReachHere (e );
1685
1687
}
1686
1688
1687
1689
/*
@@ -1801,7 +1803,7 @@ static int doGeneric(CApiContext capiContext, PythonModule module, Object module
1801
1803
}
1802
1804
}
1803
1805
} catch (UnsupportedMessageException | UnsupportedTypeException | ArityException e ) {
1804
- throw CompilerDirectives . shouldNotReachHere ();
1806
+ throw shouldNotReachHere ();
1805
1807
}
1806
1808
1807
1809
return 0 ;
@@ -1940,23 +1942,23 @@ public abstract static class CreateFunctionNode extends Node {
1940
1942
1941
1943
private static final TruffleLogger LOGGER = CApiContext .getLogger (CreateFunctionNode .class );
1942
1944
1943
- public static Object executeUncached (TruffleString name , Object callable , int wrapper , Object type , Object flags ) {
1945
+ public static PythonObject executeUncached (TruffleString name , Object callable , int wrapper , Object type , Object flags ) {
1944
1946
return CreateFunctionNodeGen .getUncached ().execute (null , name , callable , wrapper , type , flags );
1945
1947
}
1946
1948
1947
- public abstract Object execute (Node inliningTarget , TruffleString name , Object callable , int wrapper , Object type , Object flags );
1949
+ public abstract PythonObject execute (Node inliningTarget , TruffleString name , Object callable , int wrapper , Object type , Object flags );
1948
1950
1949
1951
@ Specialization (guards = "!isNoValue(type)" )
1950
1952
@ TruffleBoundary
1951
- static Object doPythonCallable (TruffleString name , PythonNativeWrapper callable , int signature , Object type , int flags ) {
1953
+ static PythonObject doPythonCallable (TruffleString name , PythonNativeWrapper callable , int signature , Object type , int flags ) {
1952
1954
// This can happen if a native type inherits slots from a managed type. Therefore,
1953
1955
// something like 'base->tp_new' will be a wrapper of the managed '__new__'. So, in this
1954
1956
// case, we assume that the object is already callable.
1955
1957
Object managedCallable = callable .getDelegate ();
1956
1958
PythonContext context = PythonContext .get (null );
1957
1959
PythonLanguage language = context .getLanguage ();
1958
1960
PBuiltinFunction function = PExternalFunctionWrapper .createWrapperFunction (name , managedCallable , type , flags , signature , language , context .factory (), false );
1959
- return function != null ? function : managedCallable ;
1961
+ return function != null ? function : castToPythonObject ( managedCallable ) ;
1960
1962
}
1961
1963
1962
1964
@ Specialization
@@ -1971,7 +1973,7 @@ static PBuiltinFunction doPyCFunctionWrapper(TruffleString name, PyCFunctionWrap
1971
1973
1972
1974
@ Specialization (guards = {"!isNativeWrapper(callable)" })
1973
1975
@ TruffleBoundary
1974
- static Object doNativeCallableWithWrapper (TruffleString name , Object callable , int signature , Object type , int flags ,
1976
+ static PythonObject doNativeCallableWithWrapper (TruffleString name , Object callable , int signature , Object type , int flags ,
1975
1977
@ CachedLibrary (limit = "3" ) InteropLibrary lib ) {
1976
1978
/*
1977
1979
* This can happen if a native type inherits slots from a managed type. For example, if
@@ -1990,7 +1992,15 @@ static Object doNativeCallableWithWrapper(TruffleString name, Object callable, i
1990
1992
}
1991
1993
PythonLanguage language = context .getLanguage ();
1992
1994
PBuiltinFunction function = PExternalFunctionWrapper .createWrapperFunction (name , resolvedCallable , type , flags , signature , language , context .factory (), doArgAndResultConversion );
1993
- return function != null ? function : resolvedCallable ;
1995
+ return function != null ? function : castToPythonObject (resolvedCallable );
1996
+ }
1997
+
1998
+ private static PythonObject castToPythonObject (Object callable ) {
1999
+ if (callable instanceof PythonObject pythonObject ) {
2000
+ return pythonObject ;
2001
+ }
2002
+ transferToInterpreter ();
2003
+ throw shouldNotReachHere ("Unexpected class of callable: " + callable .getClass ());
1994
2004
}
1995
2005
1996
2006
@ TruffleBoundary
@@ -2000,7 +2010,7 @@ public static Object resolveClosurePointer(PythonContext context, Object callabl
2000
2010
try {
2001
2011
pointer = lib .asPointer (callable );
2002
2012
} catch (UnsupportedMessageException e ) {
2003
- throw CompilerDirectives . shouldNotReachHere (e );
2013
+ throw shouldNotReachHere (e );
2004
2014
}
2005
2015
Object delegate = context .getCApiContext ().getClosureDelegate (pointer );
2006
2016
if (delegate != null ) {
0 commit comments