Skip to content

Commit 991989e

Browse files
committed
[GR-52612] check __new__ against requested name of builtin annotations
PullRequest: graalpython/3239
2 parents c65159a + 5e66da9 commit 991989e

File tree

12 files changed

+240
-16
lines changed

12 files changed

+240
-16
lines changed

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,11 +984,12 @@ static void print_c_stacktrace() {
984984
#endif
985985

986986
static void unimplemented(const char* name) {
987+
printf("Function not implemented in GraalPy: %s\n", name);
987988
printf("Native stacktrace:\n");
988989
print_c_stacktrace();
989990
}
990991

991-
#define FUNC_NOT_IMPLEMENTED unimplemented(__func__); GraalPyTrufflePrintStacktrace(__func__, "Function not implemented in GraalPy: "); exit(-1);
992+
#define FUNC_NOT_IMPLEMENTED unimplemented(__func__); GraalPyTrufflePrintStacktrace(); exit(-1);
992993

993994
// {{start CAPI_BUILTINS}}
994995
#include "capi.gen.c.h"

graalpython/com.oracle.graal.python.cext/src/capi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ EXCEPTION(ArithmeticError) \
408408
EXCEPTION(AssertionError) \
409409
EXCEPTION(AttributeError) \
410410
EXCEPTION(BaseException) \
411+
EXCEPTION(BaseExceptionGroup) \
411412
EXCEPTION(BlockingIOError) \
412413
EXCEPTION(BrokenPipeError) \
413414
EXCEPTION(BufferError) \

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_object.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,19 @@ def test_new(self):
541541
tester = TestNew()
542542
assert tester.get_none() is None
543543

544+
def test_object_new_m_ml(self):
545+
ObjectNew = CPyExtType("ObjectNew_",
546+
'''
547+
static PyObject* get_flags(PyObject* cls, PyObject* func) {
548+
return PyLong_FromLong(PyCFunction_GetFlags(func));
549+
}
550+
551+
''',
552+
tp_methods='''{"get_flags", (PyCFunction)get_flags, METH_O | METH_CLASS, ""}''',
553+
)
554+
f = ObjectNew.get_flags(object.__new__)
555+
assert f == 3, "PyCFunction_GetFlags(object.__new__) 3 != %d" % f
556+
544557
def test_init(self):
545558
TestInit = CPyExtType("TestInit",
546559
'''static PyObject* testnew_new(PyTypeObject* cls, PyObject* a, PyObject* b) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextContextBuiltins.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5454
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiBinaryBuiltinNode;
5555
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiBuiltin;
56+
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiNullaryBuiltinNode;
5657
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiTernaryBuiltinNode;
5758
import com.oracle.graal.python.builtins.objects.PNone;
5859
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.PRaiseNativeNode;
@@ -71,13 +72,12 @@
7172

7273
public final class PythonCextContextBuiltins {
7374

74-
@CApiBuiltin(ret = VoidNoReturn, args = {ConstCharPtrAsTruffleString, ConstCharPtrAsTruffleString}, call = Ignored)
75-
abstract static class PyTrufflePrintStacktrace extends CApiBinaryBuiltinNode {
75+
@CApiBuiltin(ret = VoidNoReturn, args = {}, call = Ignored)
76+
abstract static class PyTrufflePrintStacktrace extends CApiNullaryBuiltinNode {
7677

7778
@Specialization
7879
@TruffleBoundary
79-
static Object stacktrace(TruffleString where, TruffleString msg) {
80-
System.err.println(msg.toJavaStringUncached() + where.toJavaStringUncached());
80+
static Object stacktrace() {
8181
printPythonLikeStackTrace();
8282
return 0;
8383
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/hashlib/HashlibModuleBuiltins.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ static Object hmacDigest(VirtualFrame frame, PythonModule self, Object key, Obje
256256
@Cached PRaiseNode.Lazy raiseNode) {
257257
if (msg instanceof PNone) {
258258
// hmac_digest is a bit more strict
259-
throw raiseNode.get(inliningTarget).raise(PythonBuiltinClassType.TypeError, ErrorMessages.A_BYTES_LIKE_OBJECT_IS_REQUIRED_NOT_P, msg);
259+
throw raiseNode.get(inliningTarget).raise(PythonBuiltinClassType.TypeError, ErrorMessages.BYTESLIKE_OBJ_REQUIRED, msg);
260260
}
261261
Object hmacObject = newNode.execute(frame, self, key, msg, digest);
262262
return digestNode.execute(frame, hmacObject);
@@ -311,7 +311,7 @@ static Object hmacNew(@SuppressWarnings("unused") PythonModule self, Object keyO
311311
TruffleString digestmod = castStr.execute(inliningTarget, digestmodObj);
312312
Object key;
313313
if (!acquireLib.hasBuffer(keyObj)) {
314-
throw raiseNode.get(inliningTarget).raise(PythonBuiltinClassType.TypeError, ErrorMessages.A_BYTES_LIKE_OBJECT_IS_REQUIRED_NOT_P, keyObj);
314+
throw raiseNode.get(inliningTarget).raise(PythonBuiltinClassType.TypeError, ErrorMessages.BYTESLIKE_OBJ_REQUIRED, keyObj);
315315
} else {
316316
key = acquireLib.acquireReadonly(keyObj);
317317
}
@@ -322,7 +322,7 @@ static Object hmacNew(@SuppressWarnings("unused") PythonModule self, Object keyO
322322
} else if (acquireLib.hasBuffer(msgObj)) {
323323
msg = acquireLib.acquireReadonly(msgObj);
324324
} else {
325-
throw raiseNode.get(inliningTarget).raise(PythonBuiltinClassType.TypeError, ErrorMessages.A_BYTES_LIKE_OBJECT_IS_REQUIRED_NOT_P, msgObj);
325+
throw raiseNode.get(inliningTarget).raise(PythonBuiltinClassType.TypeError, ErrorMessages.BYTESLIKE_OBJ_REQUIRED, msgObj);
326326
}
327327
try {
328328
byte[] msgBytes = msg == null ? null : bufferLib.getInternalOrCopiedByteArray(msg);
@@ -374,7 +374,7 @@ static Object doIt(VirtualFrame frame, Node inliningTarget, PythonBuiltinClassTy
374374
} else if (acquireLib.hasBuffer(value)) {
375375
buffer = acquireLib.acquireReadonly(value, frame, indirectCallData);
376376
} else {
377-
throw raise.get(inliningTarget).raise(PythonBuiltinClassType.TypeError, ErrorMessages.A_BYTES_LIKE_OBJECT_IS_REQUIRED_NOT_P, value);
377+
throw raise.get(inliningTarget).raise(PythonBuiltinClassType.TypeError, ErrorMessages.BYTESLIKE_OBJ_REQUIRED, value);
378378
}
379379
try {
380380
byte[] bytes = buffer == null ? null : bufferLib.getInternalOrCopiedByteArray(buffer);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesCommonBuiltins.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import static com.oracle.graal.python.nodes.BuiltinNames.J_REMOVEPREFIX;
3737
import static com.oracle.graal.python.nodes.BuiltinNames.J_REMOVESUFFIX;
3838
import static com.oracle.graal.python.nodes.BuiltinNames.J_STARTSWITH;
39-
import static com.oracle.graal.python.nodes.ErrorMessages.A_BYTES_LIKE_OBJECT_IS_REQUIRED_NOT_P;
39+
import static com.oracle.graal.python.nodes.ErrorMessages.BYTESLIKE_OBJ_REQUIRED;
4040
import static com.oracle.graal.python.nodes.ErrorMessages.DECODER_RETURNED_P_INSTEAD_OF_BYTES;
4141
import static com.oracle.graal.python.nodes.ErrorMessages.DESCRIPTOR_NEED_OBJ;
4242
import static com.oracle.graal.python.nodes.ErrorMessages.FIRST_ARG_MUST_BE_BYTES_OR_A_TUPLE_OF_BYTES_NOT_P;
@@ -500,7 +500,7 @@ static BytesNodes.ToBytesNode createToBytes() {
500500

501501
@NeverDefault
502502
static BytesNodes.ToBytesNode createToBytesFromTuple() {
503-
return BytesNodes.ToBytesNode.create(PythonBuiltinClassType.TypeError, A_BYTES_LIKE_OBJECT_IS_REQUIRED_NOT_P);
503+
return BytesNodes.ToBytesNode.create(PythonBuiltinClassType.TypeError, BYTESLIKE_OBJ_REQUIRED);
504504
}
505505
}
506506

@@ -1368,7 +1368,7 @@ static PBytesLike replace(Object self, Object substrBuffer, Object replacementBu
13681368
@Fallback
13691369
static boolean error(@SuppressWarnings("unused") Object self, Object substr, @SuppressWarnings("unused") Object replacement, @SuppressWarnings("unused") Object count,
13701370
@Cached PRaiseNode raiseNode) {
1371-
throw raiseNode.raise(TypeError, ErrorMessages.BYTESLIKE_OBJ_REQUIRED, substr);
1371+
throw raiseNode.raise(TypeError, BYTESLIKE_OBJ_REQUIRED, substr);
13721372
}
13731373

13741374
@TruffleBoundary(allowInlining = true)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/BuiltinMethodDescriptor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.function;
4242

43+
import static com.oracle.graal.python.nodes.SpecialMethodNames.J___NEW__;
44+
4345
import java.util.Objects;
4446
import java.util.concurrent.ConcurrentHashMap;
4547

@@ -137,6 +139,9 @@ private static Builtin findBuiltinAnnotation(String name, NodeFactory<? extends
137139
if (builtin.name().equals(name)) {
138140
return builtin;
139141
}
142+
if (builtin.constructsClass() != PythonBuiltinClassType.nil && J___NEW__.equals(name)) {
143+
return builtin;
144+
}
140145
}
141146
throw new IllegalStateException(String.format(
142147
"Cannot find corresponding builtin annotation on class %s for builtin '%s'",

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,6 @@ public abstract class ErrorMessages {
809809
public static final TruffleString CANNOT_INTERN_P = tsLiteral("can't intern %p");
810810
public static final TruffleString METHOD_REQUIRES_A_BYTES_OBJECT_GOT_P = tsLiteral("Method requires a 'bytes' object, got '%p'");
811811
public static final TruffleString FIRST_ARG_MUST_BE_BYTES_OR_A_TUPLE_OF_BYTES_NOT_P = tsLiteral("first arg must be bytes or a tuple of bytes, not %p");
812-
public static final TruffleString A_BYTES_LIKE_OBJECT_IS_REQUIRED_NOT_P = tsLiteral("a bytes-like object is required, not '%p'");
813812
public static final TruffleString TYPE_S_TAKES_AT_LEAST_ONE_ARGUMENT = tsLiteral("type '%s' takes at least one argument");
814813
public static final TruffleString S_TAKES_AT_LEAST_D_ARGUMENTS_D_GIVEN = tsLiteral("%s() takes at least %d arguments (%d given)");
815814
public static final TruffleString S_TAKES_AT_MOST_D_ARGUMENTS_D_GIVEN = tsLiteral("%s() takes at most %d arguments (%d given)");

graalpython/lib-graalpython/patches/Cython/Cython-0.29.37.patch

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,33 @@ index aaa8a8e..152e9dc 100644
177177
Py_DECREF(ev);
178178
#else
179179
{
180+
@@ -715,7 +713,7 @@ PyObject *__Pyx_Coroutine_SendEx(__pyx_CoroutineObject *self, PyObject *value, i
181+
182+
exc_state = &self->gi_exc_state;
183+
if (exc_state->exc_type) {
184+
- #if CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_PYSTON
185+
+ #if CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_PYSTON || GRAALVM_PYTHON
186+
// FIXME: what to do in PyPy?
187+
#else
188+
// Generators always return to their most recent caller, not
189+
@@ -777,7 +775,7 @@ static CYTHON_INLINE void __Pyx_Coroutine_ResetFrameBackpointer(__Pyx_ExcInfoStr
190+
PyObject *exc_tb = exc_state->exc_traceback;
191+
192+
if (likely(exc_tb)) {
193+
-#if CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_PYSTON
194+
+#if CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_PYSTON || GRAALVM_PYTHON
195+
// FIXME: what to do in PyPy?
196+
#else
197+
PyTracebackObject *tb = (PyTracebackObject *) exc_tb;
198+
@@ -1250,7 +1248,7 @@ static void __Pyx_Coroutine_del(PyObject *self) {
199+
#else
200+
{PyObject *msg;
201+
char *cmsg;
202+
- #if CYTHON_COMPILING_IN_PYPY
203+
+ #if CYTHON_COMPILING_IN_PYPY || GRAALVM_PYTHON
204+
msg = NULL;
205+
cmsg = (char*) "coroutine was never awaited";
206+
#else
180207
diff --git a/Cython/Utility/CythonFunction.c b/Cython/Utility/CythonFunction.c
181208
index 93f577f..5dac1d7 100644
182209
--- a/Cython/Utility/CythonFunction.c
@@ -212,7 +239,7 @@ index cfff606..4a6a6ea 100644
212239
+ #undef CYTHON_USE_PYTYPE_LOOKUP
213240
+ #define CYTHON_USE_PYTYPE_LOOKUP 0
214241
+ #undef CYTHON_USE_ASYNC_SLOTS
215-
+ #define CYTHON_USE_ASYNC_SLOTS 0
242+
+ #define CYTHON_USE_ASYNC_SLOTS 1
216243
+ #undef CYTHON_USE_PYLIST_INTERNALS
217244
+ #define CYTHON_USE_PYLIST_INTERNALS 0
218245
+ #undef CYTHON_USE_UNICODE_INTERNALS
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
diff --git a/src/greenlet/TThreadStateDestroy.cpp b/src/greenlet/TThreadStateDestroy.cpp
2+
index 645bf52..80e8864 100644
3+
--- a/src/greenlet/TThreadStateDestroy.cpp
4+
+++ b/src/greenlet/TThreadStateDestroy.cpp
5+
@@ -125,6 +125,9 @@ struct ThreadState_DestroyNoGIL
6+
// drop this lock.
7+
LockGuard cleanup_lock(*mod_globs->thread_states_to_destroy_lock);
8+
9+
+#if GRAALVM_PYTHON
10+
+ return; // 'PyInterpreterState_Head()' is not supported
11+
+#else
12+
if (state && state->has_main_greenlet()) {
13+
// Because we don't have the GIL, this is a race condition.
14+
if (!PyInterpreterState_Head()) {
15+
@@ -150,6 +153,7 @@ struct ThreadState_DestroyNoGIL
16+
}
17+
}
18+
}
19+
+#endif
20+
}
21+
22+
static int

0 commit comments

Comments
 (0)