Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

[Bug]: Exceptions raised when `PyErr_SetHandledException` is set don't set the context correctly

オープン
#980 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
静か
技術スタック
c, python
領域
api, backend

調査の方向性

まず、提供されたCythonの例をGraalPy 25.0.2または25.0.3で実行し、5つの関数の__context__の値を比較します。PyErr_SetHandledException、PyErr_GetRaisedException、PyErr_SetStringが関与するC APIの経路を追跡します。run_both_compiledとraise_then_call_pythonでCから送出された例外が期待される__context__を受け取り、動作している3つのケースにリグレッションが発生しなければ完了です。

索引モデルが issue の本文から書いたものです。

説明

bug
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.

主要言語
Python
スター
1.6k
フォーク
155
平均マージ
7時間 45分
マージ済み PR(30日)
47

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

oracle/graalpython のほかの issue

oracle/graalpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。