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

Commit586b24d

Browse files
authored
bpo-46841: Fix error message hacks inGET_AWAITABLE (GH-31664)
1 parent03c2a36 commit586b24d

File tree

8 files changed

+37
-32
lines changed

8 files changed

+37
-32
lines changed

‎Doc/library/dis.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,15 +475,24 @@ the original TOS1.
475475

476476
**Coroutine opcodes**
477477

478-
..opcode::GET_AWAITABLE
478+
..opcode::GET_AWAITABLE (where)
479479

480480
Implements ``TOS = get_awaitable(TOS)``, where ``get_awaitable(o)``
481481
returns ``o`` if ``o`` is a coroutine object or a generator object with
482482
the CO_ITERABLE_COROUTINE flag, or resolves
483483
``o.__await__``.
484484

485+
If the ``where`` operand is nonzero, it indicates where the instruction
486+
occurs:
487+
488+
* ``1`` After a call to ``__aenter__``
489+
* ``2`` After a call to ``__aexit__``
490+
485491
..versionadded::3.5
486492

493+
..versionchanged::3.11
494+
Previously, this instruction did not have an oparg.
495+
487496

488497
..opcode::GET_AITER
489498

‎Include/opcode.h

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎Lib/importlib/_bootstrap_external.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ def _write_atomic(path, data, mode=0o666):
392392
# Python 3.11a5 3483 (Use inline caching for COMPARE_OP and BINARY_SUBSCR)
393393
# Python 3.11a5 3484 (Use inline caching for LOAD_ATTR, LOAD_METHOD, and
394394
# STORE_ATTR)
395+
# Python 3.11a5 3485 (Add an oparg to GET_AWAITABLE)
395396

396397
# Python 3.12 will start with magic number 3500
397398

@@ -406,7 +407,7 @@ def _write_atomic(path, data, mode=0o666):
406407
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
407408
# in PC/launcher.c must also be updated.
408409

409-
MAGIC_NUMBER= (3484).to_bytes(2,'little')+b'\r\n'
410+
MAGIC_NUMBER= (3485).to_bytes(2,'little')+b'\r\n'
410411
_RAW_MAGIC_NUMBER=int.from_bytes(MAGIC_NUMBER,'little')# For import.c
411412

412413
_PYCACHE='__pycache__'

‎Lib/opcode.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ def jabs_op(name, op, entries=0):
9292
def_op('PRINT_EXPR',70)
9393
def_op('LOAD_BUILD_CLASS',71)
9494

95-
def_op('GET_AWAITABLE',73)
9695
def_op('LOAD_ASSERTION_ERROR',74)
9796
def_op('RETURN_GENERATOR',75)
9897

@@ -153,7 +152,7 @@ def jabs_op(name, op, entries=0):
153152
jabs_op('POP_JUMP_IF_NOT_NONE',128)
154153
jabs_op('POP_JUMP_IF_NONE',129)
155154
def_op('RAISE_VARARGS',130)# Number of raise arguments (1, 2, or 3)
156-
155+
def_op('GET_AWAITABLE',131)
157156
def_op('MAKE_FUNCTION',132)# Flags
158157
def_op('BUILD_SLICE',133)# Number of items
159158
jabs_op('JUMP_NO_INTERRUPT',134)# Target byte offset from beginning of code
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Use an oparg to simplify the construction of helpful error messages in
2+
:opcode:`GET_AWAITABLE`.

‎Python/ceval.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg
9595
staticintcheck_except_type_valid(PyThreadState*tstate,PyObject*right);
9696
staticintcheck_except_star_type_valid(PyThreadState*tstate,PyObject*right);
9797
staticvoidformat_kwargs_error(PyThreadState*,PyObject*func,PyObject*kwargs);
98-
staticvoidformat_awaitable_error(PyThreadState*,PyTypeObject*,int,int);
98+
staticvoidformat_awaitable_error(PyThreadState*,PyTypeObject*,int);
9999
staticintget_exception_handler(PyCodeObject*,int,int*,int*,int*);
100100
static_PyInterpreterFrame*
101101
_PyEvalFramePushAndInit(PyThreadState*tstate,PyFunctionObject*func,
@@ -2505,13 +2505,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
25052505
PyObject*iter=_PyCoro_GetAwaitableIter(iterable);
25062506

25072507
if (iter==NULL) {
2508-
intopcode_at_minus_4=0;
2509-
if ((next_instr-first_instr)>4) {
2510-
opcode_at_minus_4=_Py_OPCODE(next_instr[-4]);
2511-
}
2512-
format_awaitable_error(tstate,Py_TYPE(iterable),
2513-
opcode_at_minus_4,
2514-
_Py_OPCODE(next_instr[-2]));
2508+
format_awaitable_error(tstate,Py_TYPE(iterable),oparg);
25152509
}
25162510

25172511
Py_DECREF(iterable);
@@ -7638,16 +7632,16 @@ format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
76387632
}
76397633

76407634
staticvoid
7641-
format_awaitable_error(PyThreadState*tstate,PyTypeObject*type,intprevprevprevopcode,intprevopcode)
7635+
format_awaitable_error(PyThreadState*tstate,PyTypeObject*type,intoparg)
76427636
{
76437637
if (type->tp_as_async==NULL||type->tp_as_async->am_await==NULL) {
7644-
if (prevopcode==BEFORE_ASYNC_WITH) {
7638+
if (oparg==1) {
76457639
_PyErr_Format(tstate,PyExc_TypeError,
76467640
"'async with' received an object from __aenter__ "
76477641
"that does not implement __await__: %.100s",
76487642
type->tp_name);
76497643
}
7650-
elseif (prevopcode==WITH_EXCEPT_START|| (prevopcode==CALL&&prevprevprevopcode==LOAD_CONST)) {
7644+
elseif (oparg==2) {
76517645
_PyErr_Format(tstate,PyExc_TypeError,
76527646
"'async with' received an object from __aexit__ "
76537647
"that does not implement __await__: %.100s",

‎Python/compile.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,7 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
19781978
return0;
19791979
}
19801980
if (info->fb_type==ASYNC_WITH) {
1981-
ADDOP(c,GET_AWAITABLE);
1981+
ADDOP_I(c,GET_AWAITABLE,2);
19821982
ADDOP_LOAD_CONST(c,Py_None);
19831983
ADD_YIELD_FROM(c,1);
19841984
}
@@ -5353,7 +5353,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
53535353
ADDOP_I(c,CALL,0);
53545354

53555355
if (is_async_generator&&type!=COMP_GENEXP) {
5356-
ADDOP(c,GET_AWAITABLE);
5356+
ADDOP_I(c,GET_AWAITABLE,0);
53575357
ADDOP_LOAD_CONST(c,Py_None);
53585358
ADD_YIELD_FROM(c,1);
53595359
}
@@ -5485,7 +5485,7 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
54855485
VISIT(c,expr,item->context_expr);
54865486

54875487
ADDOP(c,BEFORE_ASYNC_WITH);
5488-
ADDOP(c,GET_AWAITABLE);
5488+
ADDOP_I(c,GET_AWAITABLE,1);
54895489
ADDOP_LOAD_CONST(c,Py_None);
54905490
ADD_YIELD_FROM(c,1);
54915491

@@ -5522,7 +5522,7 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
55225522
SET_LOC(c,s);
55235523
if(!compiler_call_exit_with_nones(c))
55245524
return0;
5525-
ADDOP(c,GET_AWAITABLE);
5525+
ADDOP_I(c,GET_AWAITABLE,2);
55265526
ADDOP_LOAD_CONST(c,Py_None);
55275527
ADD_YIELD_FROM(c,1);
55285528

@@ -5536,7 +5536,7 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
55365536
ADDOP_JUMP(c,SETUP_CLEANUP,cleanup);
55375537
ADDOP(c,PUSH_EXC_INFO);
55385538
ADDOP(c,WITH_EXCEPT_START);
5539-
ADDOP(c,GET_AWAITABLE);
5539+
ADDOP_I(c,GET_AWAITABLE,2);
55405540
ADDOP_LOAD_CONST(c,Py_None);
55415541
ADD_YIELD_FROM(c,1);
55425542
compiler_with_except_finish(c,cleanup);
@@ -5710,7 +5710,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
57105710
}
57115711

57125712
VISIT(c,expr,e->v.Await.value);
5713-
ADDOP(c,GET_AWAITABLE);
5713+
ADDOP_I(c,GET_AWAITABLE,0);
57145714
ADDOP_LOAD_CONST(c,Py_None);
57155715
ADD_YIELD_FROM(c,1);
57165716
break;

‎Python/opcode_targets.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp