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

Commit14d0126

Browse files
authored
gh-105481: remove HAS_ARG, HAS_CONST, IS_JUMP_OPCODE, IS_PSEUDO_OPCODE and replace by their new versions (#105865)
1 parent34e93d3 commit14d0126

File tree

12 files changed

+38
-138
lines changed

12 files changed

+38
-138
lines changed

‎Doc/library/dis.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@ operation is being performed, so the intermediate analysis object isn't useful:
318318
..versionchanged::3.8
319319
Added *jump* parameter.
320320

321+
..versionchanged::3.13
322+
If ``oparg`` is omitted (or ``None``), the stack effect is now returned
323+
for ``oparg=0``. Previously this was an error for opcodes that use their
324+
arg. It is also no longer an error to pass an integer ``oparg`` when
325+
the ``opcode`` does not use it; the ``oparg`` in this case is ignored.
326+
321327

322328
.. _bytecodes:
323329

‎Include/internal/pycore_opcode.h

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

‎Include/internal/pycore_opcode_utils.h

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ extern "C" {
1515

1616
#defineIS_WITHIN_OPCODE_RANGE(opcode) \
1717
(((opcode) >= 0 && (opcode) <= MAX_REAL_OPCODE) || \
18-
IS_PSEUDO_OPCODE(opcode))
19-
20-
#defineIS_JUMP_OPCODE(opcode) \
21-
is_bit_set_in_table(_PyOpcode_Jump, opcode)
18+
IS_PSEUDO_INSTR(opcode))
2219

2320
#defineIS_BLOCK_PUSH_OPCODE(opcode) \
2421
((opcode) == SETUP_FINALLY || \
@@ -55,27 +52,6 @@ extern "C" {
5552
(opcode) == RAISE_VARARGS || \
5653
(opcode) == RERAISE)
5754

58-
#defineLOG_BITS_PER_INT 5
59-
#defineMASK_LOW_LOG_BITS 31
60-
61-
staticinlineint
62-
is_bit_set_in_table(constuint32_t*table,intbitindex) {
63-
/* Is the relevant bit set in the relevant word? */
64-
/* 512 bits fit into 9 32-bits words.
65-
* Word is indexed by (bitindex>>ln(size of int in bits)).
66-
* Bit within word is the low bits of bitindex.
67-
*/
68-
if (bitindex >=0&&bitindex<512) {
69-
uint32_tword=table[bitindex >>LOG_BITS_PER_INT];
70-
return (word >> (bitindex&MASK_LOW_LOG_BITS))&1;
71-
}
72-
else {
73-
return0;
74-
}
75-
}
76-
77-
#undef LOG_BITS_PER_INT
78-
#undef MASK_LOW_LOG_BITS
7955

8056
/* Flags used in the oparg for MAKE_FUNCTION */
8157
#defineMAKE_FUNCTION_DEFAULTS 0x01

‎Include/opcode.h

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

‎Lib/test/test__opcode.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,14 @@ def test_stack_effect(self):
1515
self.assertEqual(stack_effect(dis.opmap['BUILD_SLICE'],1),-1)
1616
self.assertEqual(stack_effect(dis.opmap['BUILD_SLICE'],3),-2)
1717
self.assertRaises(ValueError,stack_effect,30000)
18-
self.assertRaises(ValueError,stack_effect,dis.opmap['BUILD_SLICE'])
19-
self.assertRaises(ValueError,stack_effect,dis.opmap['POP_TOP'],0)
2018
# All defined opcodes
2119
has_arg=dis.hasarg
2220
forname,codeinfilter(lambdaitem:item[0]notindis.deoptmap,dis.opmap.items()):
2321
ifcode>=opcode.MIN_INSTRUMENTED_OPCODE:
2422
continue
2523
withself.subTest(opname=name):
26-
ifcodenotinhas_arg:
27-
stack_effect(code)
28-
self.assertRaises(ValueError,stack_effect,code,0)
29-
else:
30-
stack_effect(code,0)
31-
self.assertRaises(ValueError,stack_effect,code)
24+
stack_effect(code)
25+
stack_effect(code,0)
3226
# All not defined opcodes
3327
forcodeinset(range(256))-set(dis.opmap.values()):
3428
withself.subTest(opcode=code):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:func:`~dis.stack_effect` no longer raises an exception if an ``oparg`` is
2+
provided for an ``opcode`` that doesn't use its arg, or when it is not
3+
provided for an ``opcode`` that does use it. In the latter case, the stack
4+
effect is returned for ``oparg=0``.

‎Modules/_opcode.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,16 @@ _opcode_stack_effect_impl(PyObject *module, int opcode, PyObject *oparg,
2727
PyObject*jump)
2828
/*[clinic end generated code: output=64a18f2ead954dbb input=461c9d4a44851898]*/
2929
{
30-
inteffect;
3130
intoparg_int=0;
3231
intjump_int;
33-
if (HAS_ARG(opcode)) {
34-
if (oparg==Py_None) {
35-
PyErr_SetString(PyExc_ValueError,
36-
"stack_effect: opcode requires oparg but oparg was not specified");
37-
return-1;
38-
}
32+
33+
if (oparg!=Py_None) {
3934
oparg_int= (int)PyLong_AsLong(oparg);
4035
if ((oparg_int==-1)&&PyErr_Occurred()) {
4136
return-1;
4237
}
4338
}
44-
elseif (oparg!=Py_None) {
45-
PyErr_SetString(PyExc_ValueError,
46-
"stack_effect: opcode does not permit oparg but oparg was specified");
47-
return-1;
48-
}
39+
4940
if (jump==Py_None) {
5041
jump_int=-1;
5142
}
@@ -60,11 +51,10 @@ _opcode_stack_effect_impl(PyObject *module, int opcode, PyObject *oparg,
6051
"stack_effect: jump must be False, True or None");
6152
return-1;
6253
}
63-
effect=PyCompile_OpcodeStackEffectWithJump(opcode,oparg_int,jump_int);
54+
inteffect=PyCompile_OpcodeStackEffectWithJump(opcode,oparg_int,jump_int);
6455
if (effect==PY_INVALID_STACK_EFFECT) {
65-
PyErr_SetString(PyExc_ValueError,
66-
"invalid opcode or oparg");
67-
return-1;
56+
PyErr_SetString(PyExc_ValueError,"invalid opcode or oparg");
57+
return-1;
6858
}
6959
returneffect;
7060
}

‎Python/assemble.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,9 @@ static void
339339
write_instr(_Py_CODEUNIT*codestr,instruction*instr,intilen)
340340
{
341341
intopcode=instr->i_opcode;
342-
assert(IS_PSEUDO_OPCODE(opcode)==IS_PSEUDO_INSTR(opcode));
343342
assert(!IS_PSEUDO_INSTR(opcode));
344343
intoparg=instr->i_oparg;
345-
assert(HAS_ARG(opcode)||oparg==0);
344+
assert(OPCODE_HAS_ARG(opcode)||oparg==0);
346345
intcaches=_PyOpcode_Caches[opcode];
347346
switch (ilen-caches) {
348347
case4:

‎Python/ceval.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include"pycore_frame.h"
3030
#include"frameobject.h"// _PyInterpreterFrame_GetLine
3131
#include"opcode.h"
32+
#include"opcode_metadata.h"
3233
#include"pydtrace.h"
3334
#include"setobject.h"
3435
#include"structmember.h"// struct PyMemberDef, T_OFFSET_EX
@@ -141,7 +142,7 @@ lltrace_instruction(_PyInterpreterFrame *frame,
141142
constchar*opname=_PyOpcode_OpName[opcode];
142143
assert(opname!=NULL);
143144
intoffset= (int)(next_instr-_PyCode_CODE(_PyFrame_GetCode(frame)));
144-
if (HAS_ARG((int)_PyOpcode_Deopt[opcode])) {
145+
if (OPCODE_HAS_ARG((int)_PyOpcode_Deopt[opcode])) {
145146
printf("%d: %s %d\n",offset*2,opname,oparg);
146147
}
147148
else {
@@ -882,7 +883,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
882883
#ifUSE_COMPUTED_GOTOS
883884
_unknown_opcode:
884885
#else
885-
EXTRA_CASES// Fromopcode.h, a 'case' for each unused opcode
886+
EXTRA_CASES// Frompycore_opcode.h, a 'case' for each unused opcode
886887
#endif
887888
/* Tell C compilers not to hold the opcode variable in the loop.
888889
next_instr points the current instruction without TARGET(). */

‎Python/compile.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,8 @@ enum {
134134
int
135135
_PyCompile_InstrSize(intopcode,intoparg)
136136
{
137-
assert(IS_PSEUDO_OPCODE(opcode)==IS_PSEUDO_INSTR(opcode));
138137
assert(!IS_PSEUDO_INSTR(opcode));
139-
assert(HAS_ARG(opcode)||oparg==0);
138+
assert(OPCODE_HAS_ARG(opcode)||oparg==0);
140139
intextended_args= (0xFFFFFF<oparg)+ (0xFFFF<oparg)+ (0xFF<oparg);
141140
intcaches=_PyOpcode_Caches[opcode];
142141
returnextended_args+1+caches;
@@ -248,15 +247,9 @@ instr_sequence_use_label(instr_sequence *seq, int lbl) {
248247
staticint
249248
instr_sequence_addop(instr_sequence*seq,intopcode,intoparg,locationloc)
250249
{
251-
/* compare old and new opcode macros - use ! to compare as bools. */
252-
assert(!HAS_ARG(opcode)== !OPCODE_HAS_ARG(opcode));
253-
assert(!HAS_CONST(opcode)== !OPCODE_HAS_CONST(opcode));
254-
assert(!OPCODE_HAS_JUMP(opcode)== !OPCODE_HAS_JUMP(opcode));
255-
256250
assert(0 <=opcode&&opcode <=MAX_OPCODE);
257-
assert(IS_PSEUDO_OPCODE(opcode)==IS_PSEUDO_INSTR(opcode));
258251
assert(IS_WITHIN_OPCODE_RANGE(opcode));
259-
assert(HAS_ARG(opcode)||HAS_TARGET(opcode)||oparg==0);
252+
assert(OPCODE_HAS_ARG(opcode)||HAS_TARGET(opcode)||oparg==0);
260253
assert(0 <=oparg&&oparg< (1 <<30));
261254

262255
intidx=instr_sequence_next_inst(seq);
@@ -874,7 +867,7 @@ PyCompile_OpcodeStackEffect(int opcode, int oparg)
874867
staticint
875868
codegen_addop_noarg(instr_sequence*seq,intopcode,locationloc)
876869
{
877-
assert(!HAS_ARG(opcode));
870+
assert(!OPCODE_HAS_ARG(opcode));
878871
assert(!IS_ASSEMBLER_OPCODE(opcode));
879872
returninstr_sequence_addop(seq,opcode,0,loc);
880873
}
@@ -1151,7 +1144,7 @@ codegen_addop_j(instr_sequence *seq, location loc,
11511144
}
11521145

11531146
#defineADDOP_N(C,LOC,OP,O,TYPE) { \
1154-
assert(!HAS_CONST(OP));/* use ADDOP_LOAD_CONST_NEW */ \
1147+
assert(!OPCODE_HAS_CONST(OP));/* use ADDOP_LOAD_CONST_NEW */ \
11551148
if (compiler_addop_o((C)->u, (LOC), (OP), (C)->u->u_metadata.u_ ## TYPE, (O)) < 0) { \
11561149
Py_DECREF((O)); \
11571150
return ERROR; \
@@ -7798,7 +7791,7 @@ instructions_to_instr_sequence(PyObject *instructions, instr_sequence *seq)
77987791
gotoerror;
77997792
}
78007793
intoparg;
7801-
if (HAS_ARG(opcode)) {
7794+
if (OPCODE_HAS_ARG(opcode)) {
78027795
oparg=PyLong_AsLong(PyTuple_GET_ITEM(item,1));
78037796
if (PyErr_Occurred()) {
78047797
gotoerror;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp