@@ -99,6 +99,14 @@ static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwarg
9999static void format_awaitable_error (PyThreadState * , PyTypeObject * , int , int );
100100static int get_exception_handler (PyCodeObject * , int , int * , int * , int * );
101101
102+ typedef PyObject * (* convertion_func_ptr )(PyObject * );
103+
104+ static const convertion_func_ptr CONVERSION_FUNCTIONS [4 ] = {
105+ [FVC_STR ] = PyObject_Str ,
106+ [FVC_REPR ] = PyObject_Repr ,
107+ [FVC_ASCII ] = PyObject_ASCII
108+ };
109+
102110#define NAME_ERROR_MSG \
103111 "name '%.200s' is not defined"
104112#define UNBOUNDLOCAL_ERROR_MSG \
@@ -4346,7 +4354,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
43464354 oparg = cache -> adaptive .original_oparg ;
43474355 STAT_DEC (LOAD_METHOD , unquickened );
43484356 JUMP_TO_INSTRUCTION (LOAD_METHOD );
4349- }
4357+ }
43504358 }
43514359
43524360 TARGET (LOAD_METHOD_CACHED ): {
@@ -4364,7 +4372,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
43644372 assert (cache1 -> tp_version != 0 );
43654373 assert (self_cls -> tp_dictoffset >= 0 );
43664374 assert (Py_TYPE (self_cls )-> tp_dictoffset > 0 );
4367-
4375+
43684376 // inline version of _PyObject_GetDictPtr for offset >= 0
43694377 PyObject * dict = self_cls -> tp_dictoffset != 0 ?
43704378 * (PyObject * * ) ((char * )self + self_cls -> tp_dictoffset ) : NULL ;
@@ -4627,62 +4635,47 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
46274635 DISPATCH ();
46284636 }
46294637
4630- TARGET (FORMAT_VALUE ): {
4631- /* Handles f-string value formatting. */
4638+ TARGET (CONVERT_VALUE ): {
46324639 PyObject * result ;
4633- PyObject * fmt_spec ;
4634- PyObject * value ;
4635- PyObject * (* conv_fn )(PyObject * );
4636- int which_conversion = oparg & FVC_MASK ;
4637- int have_fmt_spec = (oparg & FVS_MASK ) == FVS_HAVE_SPEC ;
4638-
4639- fmt_spec = have_fmt_spec ? POP () : NULL ;
4640- value = POP ();
4641-
4642- /* See if any conversion is specified. */
4643- switch (which_conversion ) {
4644- case FVC_NONE : conv_fn = NULL ; break ;
4645- case FVC_STR : conv_fn = PyObject_Str ; break ;
4646- case FVC_REPR : conv_fn = PyObject_Repr ; break ;
4647- case FVC_ASCII : conv_fn = PyObject_ASCII ; break ;
4648- default :
4649- _PyErr_Format (tstate , PyExc_SystemError ,
4650- "unexpected conversion flag %d" ,
4651- which_conversion );
4640+ PyObject * value = POP ();
4641+ convertion_func_ptr conv_fn ;
4642+ assert (oparg >= FVC_STR && oparg <= FVC_ASCII );
4643+ conv_fn = CONVERSION_FUNCTIONS [oparg ];
4644+ result = conv_fn (value );
4645+ Py_DECREF (value );
4646+ if (result == NULL ) {
46524647 goto error ;
46534648 }
4649+ PUSH (result );
4650+ DISPATCH ();
4651+ }
46544652
4655- /* If there's a conversion function, call it and replace
4656- value with that result. Otherwise, just use value,
4657- without conversion. */
4658- if ( conv_fn != NULL ) {
4659- result = conv_fn ( value );
4660- Py_DECREF (value );
4653+ TARGET ( FORMAT_SIMPLE ): {
4654+ PyObject * value = TOP ();
4655+ /* If value is a unicode object, then we know the result
4656+ * of format(value) is value itself. */
4657+ if (! PyUnicode_CheckExact ( value )) {
4658+ PyObject * result = PyObject_Format (value , NULL );
46614659 if (result == NULL ) {
4662- Py_XDECREF (fmt_spec );
46634660 goto error ;
46644661 }
4665- value = result ;
4666- }
4667-
4668- /* If value is a unicode object, and there's no fmt_spec,
4669- then we know the result of format(value) is value
4670- itself. In that case, skip calling format(). I plan to
4671- move this optimization in to PyObject_Format()
4672- itself. */
4673- if (PyUnicode_CheckExact (value ) && fmt_spec == NULL ) {
4674- /* Do nothing, just transfer ownership to result. */
4675- result = value ;
4676- } else {
4677- /* Actually call format(). */
4678- result = PyObject_Format (value , fmt_spec );
4662+ SET_TOP (result );
46794663 Py_DECREF (value );
4680- Py_XDECREF (fmt_spec );
4681- if (result == NULL ) {
4682- goto error ;
4683- }
46844664 }
4665+ DISPATCH ();
4666+ }
46854667
4668+ TARGET (FORMAT_WITH_SPEC ): {
4669+ PyObject * fmt_spec = POP ();
4670+ PyObject * value = POP ();
4671+ PyObject * result ;
4672+ /* Call format(). */
4673+ result = PyObject_Format (value , fmt_spec );
4674+ Py_DECREF (value );
4675+ Py_DECREF (fmt_spec );
4676+ if (result == NULL ) {
4677+ goto error ;
4678+ }
46864679 PUSH (result );
46874680 DISPATCH ();
46884681 }
0 commit comments