@@ -688,7 +688,36 @@ def format_and_strip(
688688 if not chunks :
689689 raise ValueError ("No formatting placeholders found" )
690690
691- params = list (reversed (params ))
691+ params = params [: len (chunks ) - 1 ]
692+
693+ if len (params ) < len (chunks ) - 1 :
694+ raise ValueError ("Not enough params." )
695+
696+ concat_chunks = []
697+ iter_chunks = iter (chunks ) # type: Optional[Iterator]
698+ iter_params = iter (params ) # type: Optional[Iterator]
699+
700+ while iter_chunks is not None or iter_params is not None :
701+ if iter_chunks is not None :
702+ try :
703+ concat_chunks .append (next (iter_chunks ))
704+ except StopIteration :
705+ iter_chunks = None
706+
707+ if iter_params is not None :
708+ try :
709+ concat_chunks .append (str (next (iter_params )))
710+ except StopIteration :
711+ iter_params = None
712+
713+ return concat_strings (
714+ concat_chunks , strip_string = strip_string , max_length = max_length
715+ )
716+
717+
718+ def concat_strings (
719+ chunks , strip_string = strip_string , max_length = MAX_FORMAT_PARAM_LENGTH
720+ ):
692721 rv_remarks = [] # type: List[Any]
693722 rv_original_length = 0
694723 rv_length = 0
@@ -700,28 +729,25 @@ def realign_remark(remark):
700729 for i , x in enumerate (remark )
701730 ]
702731
703- for chunk in chunks [: - 1 ] :
704- rv . append (chunk )
705- rv_length += len ( chunk )
706- rv_original_length += len ( chunk )
707- if not params :
708- raise ValueError ( "Not enough params." )
709- param = params . pop ( )
732+ for chunk in chunks :
733+ if isinstance (chunk , AnnotatedValue ):
734+ # Assume it's already stripped!
735+ stripped_chunk = chunk
736+ chunk = chunk . value
737+ else :
738+ stripped_chunk = strip_string ( chunk , max_length = max_length )
710739
711- stripped_param = strip_string (param , max_length = max_length )
712- if isinstance (stripped_param , AnnotatedValue ):
740+ if isinstance (stripped_chunk , AnnotatedValue ):
713741 rv_remarks .extend (
714- realign_remark (remark ) for remark in stripped_param .metadata ["rem" ]
742+ realign_remark (remark ) for remark in stripped_chunk .metadata ["rem" ]
715743 )
716- stripped_param = stripped_param .value
717-
718- rv_original_length += len (param )
719- rv_length += len (stripped_param )
720- rv .append (stripped_param )
744+ stripped_chunk_value = stripped_chunk .value
745+ else :
746+ stripped_chunk_value = stripped_chunk
721747
722- rv . append ( chunks [ - 1 ] )
723- rv_length += len (chunks [ - 1 ])
724- rv_original_length += len ( chunks [ - 1 ])
748+ rv_original_length += len ( chunk )
749+ rv_length += len (stripped_chunk_value ) # type: ignore
750+ rv . append ( stripped_chunk_value ) # type: ignore
725751
726752 rv_joined = u"" .join (rv )
727753 assert len (rv_joined ) == rv_length
0 commit comments