@@ -702,6 +702,13 @@ append_templatestr(PyUnicodeWriter *writer, expr_ty e)
702702
703703 Py_ssize_t last_idx = 0 ;
704704 Py_ssize_t len = asdl_seq_LEN (e -> v .TemplateStr .values );
705+ if (len == 0 ) {
706+ int result = _write_values_subarray (writer , e -> v .TemplateStr .values ,
707+ 0 , len - 1 , 't' , arena );
708+ _PyArena_Free (arena );
709+ return result ;
710+ }
711+
705712 for (Py_ssize_t i = 0 ; i < len ; i ++ ) {
706713 expr_ty value = asdl_seq_GET (e -> v .TemplateStr .values , i );
707714
@@ -774,32 +781,37 @@ append_joinedstr(PyUnicodeWriter *writer, expr_ty e, bool is_format_spec)
774781}
775782
776783static int
777- append_interpolation_value (PyUnicodeWriter * writer , expr_ty e )
784+ append_interpolation_str (PyUnicodeWriter * writer , PyObject * str )
778785{
779786 const char * outer_brace = "{" ;
780- /* Grammar allows PR_TUPLE, but use >PR_TEST for adding parenthesis
781- around a lambda with ':' */
782- PyObject * temp_fv_str = expr_as_unicode (e , PR_TEST + 1 );
783- if (!temp_fv_str ) {
784- return -1 ;
785- }
786- if (PyUnicode_Find (temp_fv_str , _Py_LATIN1_CHR ('{' ), 0 , 1 , 1 ) == 0 ) {
787+ if (PyUnicode_Find (str , _Py_LATIN1_CHR ('{' ), 0 , 1 , 1 ) == 0 ) {
787788 /* Expression starts with a brace, split it with a space from the outer
788789 one. */
789790 outer_brace = "{ " ;
790791 }
791792 if (-1 == append_charp (writer , outer_brace )) {
792- Py_DECREF (temp_fv_str );
793793 return -1 ;
794794 }
795- if (-1 == PyUnicodeWriter_WriteStr (writer , temp_fv_str )) {
796- Py_DECREF (temp_fv_str );
795+ if (-1 == PyUnicodeWriter_WriteStr (writer , str )) {
797796 return -1 ;
798797 }
799- Py_DECREF (temp_fv_str );
800798 return 0 ;
801799}
802800
801+ static int
802+ append_interpolation_value (PyUnicodeWriter * writer , expr_ty e )
803+ {
804+ /* Grammar allows PR_TUPLE, but use >PR_TEST for adding parenthesis
805+ around a lambda with ':' */
806+ PyObject * temp_fv_str = expr_as_unicode (e , PR_TEST + 1 );
807+ if (!temp_fv_str ) {
808+ return -1 ;
809+ }
810+ int result = append_interpolation_str (writer , temp_fv_str );
811+ Py_DECREF (temp_fv_str );
812+ return result ;
813+ }
814+
803815static int
804816append_interpolation_conversion (PyUnicodeWriter * writer , int conversion )
805817{
@@ -843,7 +855,7 @@ append_interpolation_format_spec(PyUnicodeWriter *writer, expr_ty e)
843855static int
844856append_interpolation (PyUnicodeWriter * writer , expr_ty e )
845857{
846- if (-1 == append_interpolation_value (writer , e -> v .Interpolation .value )) {
858+ if (-1 == append_interpolation_str (writer , e -> v .Interpolation .str )) {
847859 return -1 ;
848860 }
849861
0 commit comments