Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

More callables should be usable as Exception Group predicates #105730

Closed
Labels
3.13bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement
@SlaushVunter

Description

@SlaushVunter

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.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp