Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba81186

Browse files
Add tests after analyze_descriptor, also wrap in easy-to-use macro
1 parent 307d6c6 commit ba81186

1 file changed

Lines changed: 30 additions & 17 deletions

File tree

Python/specialize.c

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,21 @@ initial_counter_value(void) {
499499
#define SPEC_FAIL_NOT_FOLLOWED_BY_COND_JUMP 14
500500
#define SPEC_FAIL_BIG_INT 15
501501

502+
static inline int
503+
invalid_tp_version(int opcode, PyTypeObject *type)
504+
{
505+
if (!_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
506+
SPECIALIZATION_FAIL(opcode, SPEC_FAIL_OUT_OF_VERSIONS);
507+
return 1;
508+
}
509+
return 0;
510+
}
511+
512+
#ifndef INVALID_TP_VERSION
513+
#define INVALID_TP_VERSION(opcode, type) \
514+
invalid_tp_version(opcode, ((PyTypeObject *)(type)))
515+
#endif
516+
502517
static int
503518
specialize_module_load_attr(
504519
PyObject *owner, _Py_CODEUNIT *instr, PyObject *name,
@@ -696,12 +711,11 @@ _Py_Specialize_LoadAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, Sp
696711
return -1;
697712
}
698713
}
699-
if (!_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
700-
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OUT_OF_VERSIONS);
701-
goto fail;
702-
}
703714
PyObject *descr;
704715
DesciptorClassification kind = analyze_descriptor(type, name, &descr, 0);
716+
if (INVALID_TP_VERSION(LOAD_ATTR, type)) {
717+
goto fail;
718+
}
705719
switch(kind) {
706720
case OVERRIDING:
707721
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OVERRIDING_DESCRIPTOR);
@@ -789,12 +803,11 @@ _Py_Specialize_StoreAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, S
789803
SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_OVERRIDDEN);
790804
goto fail;
791805
}
792-
if (!_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
793-
SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_OUT_OF_VERSIONS);
794-
goto fail;
795-
}
796806
PyObject *descr;
797807
DesciptorClassification kind = analyze_descriptor(type, name, &descr, 1);
808+
if (INVALID_TP_VERSION(STORE_ATTR, type)) {
809+
goto fail;
810+
}
798811
switch(kind) {
799812
case OVERRIDING:
800813
SPECIALIZATION_FAIL(STORE_ATTR, SPEC_FAIL_OVERRIDING_DESCRIPTOR);
@@ -906,14 +919,12 @@ static int
906919
specialize_class_load_method(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name,
907920
_PyAttrCache *cache1, _PyObjectCache *cache2)
908921
{
909-
if (!_PyType_HasFeature((PyTypeObject *)owner, Py_TPFLAGS_VALID_VERSION_TAG)) {
910-
SPECIALIZATION_FAIL(LOAD_METHOD, SPEC_FAIL_OUT_OF_VERSIONS);
911-
return -1;
912-
}
913-
914922
PyObject *descr = NULL;
915923
DesciptorClassification kind = 0;
916924
kind = analyze_descriptor((PyTypeObject *)owner, name, &descr, 0);
925+
if (INVALID_TP_VERSION(LOAD_METHOD, owner)) {
926+
return -1;
927+
}
917928
switch (kind) {
918929
case METHOD:
919930
case NON_DESCRIPTOR:
@@ -959,13 +970,12 @@ _Py_Specialize_LoadMethod(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name,
959970
goto success;
960971
}
961972

962-
if (!_PyType_HasFeature(owner_cls, Py_TPFLAGS_VALID_VERSION_TAG)) {
963-
SPECIALIZATION_FAIL(LOAD_METHOD, SPEC_FAIL_OUT_OF_VERSIONS);
964-
goto fail;
965-
}
966973
PyObject *descr = NULL;
967974
DesciptorClassification kind = 0;
968975
kind = analyze_descriptor(owner_cls, name, &descr, 0);
976+
if (INVALID_TP_VERSION(LOAD_METHOD, owner_cls)) {
977+
goto fail;
978+
}
969979
assert(descr != NULL || kind == ABSENT || kind == GETSET_OVERRIDDEN);
970980
if (kind != METHOD) {
971981
SPECIALIZATION_FAIL(LOAD_METHOD, load_method_fail_kind(kind));
@@ -1189,6 +1199,9 @@ _Py_Specialize_BinarySubscr(
11891199
}
11901200
PyTypeObject *cls = Py_TYPE(container);
11911201
PyObject *descriptor = _PyType_LookupId(cls, &PyId___getitem__);
1202+
if (INVALID_TP_VERSION(BINARY_SUBSCR, cls)) {
1203+
goto fail;
1204+
}
11921205
if (descriptor && Py_TYPE(descriptor) == &PyFunction_Type) {
11931206
PyFunctionObject *func = (PyFunctionObject *)descriptor;
11941207
PyCodeObject *code = (PyCodeObject *)func->func_code;

0 commit comments

Comments
 (0)