@@ -2690,6 +2690,25 @@ to_bool_fail_kind(PyObject *value)
26902690 return SPEC_FAIL_OTHER ;
26912691}
26922692
2693+ static int
2694+ check_type_always_true (PyTypeObject * ty )
2695+ {
2696+ PyNumberMethods * nb = ty -> tp_as_number ;
2697+ if (nb && nb -> nb_bool ) {
2698+ return SPEC_FAIL_TO_BOOL_NUMBER ;
2699+ }
2700+ PyMappingMethods * mp = ty -> tp_as_mapping ;
2701+ if (mp && mp -> mp_length ) {
2702+ return SPEC_FAIL_TO_BOOL_MAPPING ;
2703+ }
2704+ PySequenceMethods * sq = ty -> tp_as_sequence ;
2705+ if (sq && sq -> sq_length ) {
2706+ return SPEC_FAIL_TO_BOOL_SEQUENCE ;
2707+ }
2708+ return 0 ;
2709+ }
2710+
2711+
26932712void
26942713_Py_Specialize_ToBool (_PyStackRef value_o , _Py_CODEUNIT * instr )
26952714{
@@ -2718,33 +2737,25 @@ _Py_Specialize_ToBool(_PyStackRef value_o, _Py_CODEUNIT *instr)
27182737 return ;
27192738 }
27202739 if (PyType_HasFeature (Py_TYPE (value ), Py_TPFLAGS_HEAPTYPE )) {
2721- PyNumberMethods * nb = Py_TYPE (value )-> tp_as_number ;
2722- if (nb && nb -> nb_bool ) {
2723- unspecialize (instr , SPEC_FAIL_TO_BOOL_NUMBER );
2724- return ;
2725- }
2726- PyMappingMethods * mp = Py_TYPE (value )-> tp_as_mapping ;
2727- if (mp && mp -> mp_length ) {
2728- unspecialize (instr , SPEC_FAIL_TO_BOOL_MAPPING );
2729- return ;
2730- }
2731- PySequenceMethods * sq = Py_TYPE (value )-> tp_as_sequence ;
2732- if (sq && sq -> sq_length ) {
2733- unspecialize (instr , SPEC_FAIL_TO_BOOL_SEQUENCE );
2740+ unsigned int version = 0 ;
2741+ int err = _PyType_Validate (Py_TYPE (value ), check_type_always_true , & version );
2742+ if (err < 0 ) {
2743+ unspecialize (instr , SPEC_FAIL_OUT_OF_VERSIONS );
27342744 return ;
27352745 }
2736- if (! PyUnstable_Type_AssignVersionTag ( Py_TYPE ( value )) ) {
2737- unspecialize (instr , SPEC_FAIL_OUT_OF_VERSIONS );
2746+ else if (err > 0 ) {
2747+ unspecialize (instr , err );
27382748 return ;
27392749 }
2740- uint32_t version = Py_TYPE (value )-> tp_version_tag ;
2750+
2751+ assert (err == 0 );
27412752 if (version == 0 ) {
27422753 unspecialize (instr , SPEC_FAIL_OUT_OF_VERSIONS );
27432754 return ;
27442755 }
2745- specialize (instr , TO_BOOL_ALWAYS_TRUE );
2746- write_u32 (cache -> version , version );
27472756 assert (version );
2757+ write_u32 (cache -> version , version );
2758+ specialize (instr , TO_BOOL_ALWAYS_TRUE );
27482759 return ;
27492760 }
27502761 unspecialize (instr , to_bool_fail_kind (value ));
0 commit comments