Skip to content

Commit 9fc208a

Browse files
author
Eddy Xu
committed
still fix cevel issues.
1 parent 18eb66d commit 9fc208a

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Python/ceval.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,53 @@
1919

2020
#include <ctype.h>
2121

22+
int
23+
_Py3kWarn_NextOpcode(void)
24+
{
25+
PyFrameObject *frame;
26+
char *code;
27+
Py_ssize_t n;
28+
int offset;
29+
int op;
30+
int steps;
31+
32+
frame = PyEval_GetFrame();
33+
if (frame == NULL || frame->f_code == NULL)
34+
return -1;
35+
if (PyString_AsStringAndSize(frame->f_code->co_code, &code, &n) < 0) {
36+
PyErr_Clear();
37+
return -1;
38+
}
39+
40+
offset = frame->f_lasti;
41+
if (offset < 0 || offset >= n)
42+
return -1;
43+
44+
op = (unsigned char)code[offset];
45+
offset += 1;
46+
if (HAS_ARG(op))
47+
offset += 2;
48+
49+
/* These warnings only care about the immediate consumer of the
50+
just-evaluated call result. Looking ahead a handful of opcodes is
51+
enough to skip trivial stack setup before we either find a relevant
52+
consumer or hit a stop opcode showing the value has already been
53+
stored, returned, or discarded. */
54+
for (steps = 0; steps < 8 && offset >= 0 && offset < n; steps++) {
55+
op = (unsigned char)code[offset];
56+
if (op == BINARY_ADD || op == INPLACE_ADD || op == GET_ITER ||
57+
op == BINARY_SUBSCR || op == STORE_SUBSCR)
58+
return op;
59+
if (op == RETURN_VALUE || op == STORE_NAME || op == STORE_FAST ||
60+
op == STORE_GLOBAL || op == STORE_ATTR || op == POP_TOP)
61+
return -1;
62+
offset += 1;
63+
if (HAS_ARG(op))
64+
offset += 2;
65+
}
66+
return -1;
67+
}
68+
2269
#ifndef WITH_TSC
2370

2471
#define READ_TIMESTAMP(var)

0 commit comments

Comments
 (0)