[Bug]: Exceptions raised when `PyErr_SetHandledException` is set don't set the context correctly
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 48/100
Línea de trabajo
Empieza ejecutando el ejemplo de Cython proporcionado en GraalPy 25.0.2 o 25.0.3 y compara los valores de context para las cinco funciones. Sigue las rutas de la C API que implican PyErr_SetHandledException, PyErr_GetRaisedException y PyErr_SetString. Se considera terminado cuando las excepciones generadas por C reciben el context esperado en run_both_compiled y raise_then_call_python sin hacer que regresen los problemas en los tres casos que funcionan.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Describe the bug
In Python, raising an exception in an except block leads to the __context__ attribute on the new exception being set. This works correctly in GraalPython.
However the C API equivalent doesn't end up setting __context__. C API raised exceptions appear to have __context__ set correctly when raised from a Python catch block. However, neither Python nor C API execptions have __context__ set correctly when the handled exception is set with PyErr_SetHandledException.
Below is an example. I've used Cython for a quick way of wrapping it, but I think all the interesting behaviour is in the manual C API code.
// nocontexth.h
int raise_value_error(void) {
PyErr_SetString(PyExc_ValueError, "I'm a value error");
return -1;
}
int raise_type_error(void) {
PyErr_SetString(PyExc_TypeError, "I'm a type error");
return -1;
}
int raise_and_handle_exceptions(void) {
PyErr_SetString(PyExc_ValueError, "I'm a value error");
PyObject *exc = PyErr_GetRaisedException();
PyErr_SetHandledException(exc);
PyErr_SetString(PyExc_TypeError, "I'm a type error");
PyErr_SetHandledException(NULL);
return -1;
}
int raise_then_call_python(PyObject *callable) {
PyErr_SetString(PyExc_ValueError, "I'm a value error");
PyObject *exc = PyErr_GetRaisedException();
PyErr_SetHandledException(exc);
PyObject_CallNoArgs(callable);
PyErr_SetHandledException(NULL);
return PyErr_Occurred() ? -1 : 0;
}
# nocontext.pyx
cdef extern from "nocontexth.h":
cpdef int raise_value_error() except -1
cpdef int raise_type_error() except -1
cpdef int raise_and_handle_exceptions() except -1
cpdef int raise_then_call_python(object) except -1
Compile this with cythonize -if nocontext.pyx.
# testnocontext.py
import nocontext
import traceback
def run_python():
try:
raise ValueError("I'm a value error")
except Exception:
raise TypeError("I'm a type error")
def run_both_compiled():
nocontext.raise_and_handle_exceptions()
def raise_first_compiled():
try:
nocontext.raise_value_error()
except Exception:
raise TypeError("I'm a type error")
def raise_second_compiled():
try:
raise ValueError("I'm a value error")
except Exception:
nocontext.raise_type_error()
def raise_then_call_python():
def f():
raise TypeError("I'm a type error")
nocontext.raise_then_call_python(f)
for name in ["run_python", "run_both_compiled", "raise_first_compiled", "raise_second_compiled", "raise_then_call_python"]:
print(name)
print("-"*len(name))
try:
globals()[name]()
except:
traceback.print_exc()
print("\n\n")
Run this with python testnocontext.py.
Three of the tests work correctly. The two that don't are run_both_compiled and raise_then_call_python which suggests to me that the problem is with setting the handled exception from C.
Operating system
Linux
CPU architecture
x86_64
GraalPy version
25.0.2 and 25.0.3
JDK version
No response
Context configuration
No response
Steps to reproduce
Full example is in the "describe the bug" section
Expected behavior
__context__ set correctly
Stack trace
Additional context
This isn't causing me a real issue - it's just something I spotted from when adding some Cython tests. I've fixed it by disabling the tests in GraalPython. The effect of not fixing it is slightly worse exception messages but nothing more major.
- Lenguaje dominante
- Python
- Estrellas
- 1.6k
- Forks
- 155
- Merge medio
- 7 h 35 min
- PR fusionados (30 d)
- 48
Preparar el entorno
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de oracle/graalpython
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
oracle/graalpython#1105 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
oracle/graalpython#1102 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 5/5 Más de una semana Aptitud para principiantes 35/100
oracle/graalpython#1169 · 1 comentario · 1 asignado ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 3/5 1-2 días Aptitud para principiantes 74/100
oracle/graalpython#1133 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 3/5 1-2 días Aptitud para principiantes 68/100
oracle/graalpython#1112 ·
Los mantenedores suelen responder en 1 día
Todos los issues de oracle/graalpython
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
gradio-app/gradio#13895 ·
Los mantenedores suelen responder en 1 día
-
build-error
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
spack/spack-packages#6713 ·
Los mantenedores suelen responder en 1 día
-
Use issue templatesAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
ActivityWatch/activitywatch#1464 · 1 reacción ·
Los mantenedores suelen responder en 1 día
-
[Bug]: The ckg tool drops the return type of every decorated Python method in class search resultsAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
bytedance/trae-agent#483 ·
Los mantenedores suelen responder en 1 día