8000 add PyStackRef_IsFalse/True · python/cpython@2eda2f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2eda2f9

Browse files
add PyStackRef_IsFalse/True
1 parent 66eb526 commit 2eda2f9

File tree

5 files changed

+32
-15
lines changed

5 files changed

+32
-15
lines changed

Include/internal/pycore_stackref.h

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,39 @@ typedef union {
1919

2020
#define Py_TAG_DEFERRED (1)
2121

22-
// To catch where stackrefs leak out to the heap,
23-
// in both current and future code.
24-
#ifdef Py_DEBUG
25-
# define Py_TAG_PTR (3)
26-
# define Py_TAG (3)
27-
#else
28-
# define Py_TAG_PTR (0)
29-
# define Py_TAG (1)
30-
#endif
22+
#define Py_TAG_PTR (0)
23+
#define Py_TAG (1)
3124

3225
#ifdef Py_GIL_DISABLED
3326
static const _PyStackRef Py_STACKREF_NULL = { .bits = 0 | Py_TAG_DEFERRED};
27+
3428
#else
3529
static const _PyStackRef Py_STACKREF_NULL = { .bits = 0 };
3630
#endif
3731

3832
#define PyStackRef_IsNull(stackref) ((stackref).bits == Py_STACKREF_NULL.bits)
3933

34+
static inline int
35+
PyStackRef_IsTrue(_PyStackRef stackref) {
36+
#ifdef Py_GIL_DISABLED
37+
const _PyStackRef STACKREF_TRUE = {.bits = ((uintptr_t)Py_True | Py_TAG_DEFERRED)};
38+
#else
39+
const _PyStackRef STACKREF_TRUE = {.bits = ((uintptr_t)Py_True)};
40+
#endif
41+
return stackref.bits == STACKREF_TRUE.bits;
42+
}
43+
44+
static inline int
45+
PyStackRef_IsFalse(_PyStackRef stackref) {
46+
#ifdef Py_GIL_DISABLED
47+
const _PyStackRef STACKREF_FALSE = {.bits = ((uintptr_t)Py_False | Py_TAG_DEFERRED)};
48+
#else
49+
const _PyStackRef STACKREF_FALSE = {.bits = ((uintptr_t)Py_False)};
50+
#endif
51+
return stackref.bits == STACKREF_FALSE.bits;
52+
}
53+
54+
4055
static inline int
4156
PyStackRef_IsDeferred(_PyStackRef ref)
4257
{

Python/bytecodes.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ dummy_func(
318318

319319
pure inst(UNARY_NOT, (value -- res)) {
320320
assert(PyBool_Check(PyStackRef_AsPyObjectBorrow(value)));
321-
res = PyStackRef_FromPyObjectSteal(Py_IsFalse(PyStackRef_AsPyObjectBorrow(value))
321+
res = PyStackRef_FromPyObjectSteal(PyStackRef_IsFalse(value)
322322
? Py_True : Py_False);
323323
}
324324

@@ -2695,7 +2695,7 @@ dummy_func(
26952695

26962696
replaced op(_POP_JUMP_IF_TRUE, (cond -- )) {
26972697
assert(PyBool_Check(PyStackRef_AsPyObjectBorrow(cond)));
2698-
int flag = Py_IsTrue(PyStackRef_AsPyObjectBorrow(cond));
2698+
int flag = PyStackRef_IsTrue(cond);
26992699
#if ENABLE_SPECIALIZATION
27002700
this_instr[1].cache = (this_instr[1].cache << 1) | flag;
27012701
#endif

Python/executor_cases.c.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

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

Tools/cases_generator/analyzer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ def has_error_without_pop(op: parser.InstDef) -> bool:
422422
"PyFloat_AS_DOUBLE",
423423
"_PyFrame_PushUnchecked",
424424
"Py_FatalError",
425+
"PyStackRef_IsTrue",
426+
"PyStackRef_IsFalse",
425427
)
426428

427429
ESCAPING_FUNCTIONS = (

0 commit comments

Comments
 (0)
0