39 lines
1.6 KiB
Python
39 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Check only structseq_repr's name_utf8 declaration and evaluation order."""
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
source = Path(sys.argv[1] if len(sys.argv) > 1 else "cpython/Objects/structseq.c")
|
|
match = re.search(
|
|
r"static PyObject \*\nstructseq_repr\(PyStructSequence \*obj\)\n\{(.*?)\n\}",
|
|
source.read_text(),
|
|
re.S,
|
|
)
|
|
assert match, "structseq_repr definition not found"
|
|
body = match.group(1)
|
|
assert re.search(
|
|
r"for \(i=0; i < VISIBLE_SIZE\(obj\); i\+\+\) \{"
|
|
r"\s*const char \*name_utf8;\s*PyObject \*name;\s*if \(i > 0\)",
|
|
body,
|
|
), "structseq_repr: VC6 requires name_utf8 declared before loop-body statements"
|
|
assert len(re.findall(r"const char \*name_utf8\b", body)) == 1, (
|
|
"declare name_utf8 exactly once"
|
|
)
|
|
assert re.search(
|
|
r'if \(i > 0\) \{\s*/\* Write ", " \*/\s*'
|
|
r'if \(_PyUnicodeWriter_WriteASCIIString\(&writer, ", ", 2\) < 0\) '
|
|
r'\{\s*goto error;\s*\}\s*\}\s*'
|
|
r'/\* Write "name=repr" \*/\s*'
|
|
r'name_utf8 = typ->tp_members\[i\].name;\s*'
|
|
r'if \(name_utf8 == NULL\) \{\s*'
|
|
r'PyErr_Format\(PyExc_SystemError, "In structseq_repr\(\), member %zd name is NULL"'
|
|
r'\s*" for type %.500s", i, typ->tp_name\);\s*goto error;\s*\}\s*'
|
|
r'name = PyUnicode_DecodeUTF8\(name_utf8, strlen\(name_utf8\), NULL\);',
|
|
body,
|
|
), "preserve separator/error handling before member lookup, NULL check, and decoding"
|
|
assert len(re.findall(r"\bname_utf8\s*=", body)) == 2, (
|
|
"preserve the single assignment and NULL comparison"
|
|
)
|
|
print("structseq_repr name_utf8 C89 declaration and evaluation order: PASS")
|