Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
Bug report
If a bound method is used as the condition for BaseExceptionGroup.split the following exception is raised:
TypeError: expected a function, exception type or tuple of exception types
Consider the following example:
classHandleError:def__enter__(self):passdef__exit__(self,exc_type,exc_inst,exc_tb):ifexc_typeisNone:returnFalseifisinstance(exc_inst,ExceptionGroup):match,rest=exc_inst.split(self._log_and_ignore_error)ifmatchisNone:returnFalseelifrestisNone:returnTrueraiserestelse:returnself._log_and_ignore_error(exc_inst)def_log_and_ignore_error(self,e:BaseException)->bool: ...returnTruewithHandleError():raiseExceptionGroup('foo', [ValueError('bar')])
If we replace the bound method with a lambda (lambda e: self._log_and_ignore_error(e)) then no exception is raised.
I think it would be useful to accept any callable here. I guess the code update would be simple too, just replacePyFunction_Check withPyCallable_Check in
if (PyFunction_Check(value)) {*type=EXCEPTION_GROUP_MATCH_BY_PREDICATE;return0; }
Update the exception message to allow callables, and change the assert in
caseEXCEPTION_GROUP_MATCH_BY_PREDICATE: {assert(PyFunction_Check(matcher_value));PyObject*exc_matches=PyObject_CallOneArg(matcher_value,exc);
to callPyCallable_Check again.
Your environment
- CPython versions tested on: 3.11.2
The c code snippets are from origin/main.