@@ -1858,8 +1858,30 @@ def _complete_statement(self, line: str) -> Statement:
18581858 pipe runs out. We can't refactor it because we need to retain
18591859 backwards compatibility with the standard library version of cmd.
18601860 """
1861- statement = self .statement_parser .parse (self .preparse (line ))
1862- while statement .multiline_command and not statement .terminator :
1861+ # preparse() is deprecated, use self.register_postparsing_hook() instead
1862+ line = self .preparse (line )
1863+
1864+ while True :
1865+ try :
1866+ statement = self .statement_parser .parse (line )
1867+ if statement .multiline_command and statement .terminator :
1868+ # we have a completed multiline command, we are done
1869+ break
1870+ if not statement .multiline_command :
1871+ # it's not a multiline command, but we parsed it ok
1872+ # so we are done
1873+ break
1874+ except ValueError :
1875+ # we have unclosed quotation marks, lets parse only the command
1876+ # and see if it's a multiline
1877+ statement = self .statement_parser .parse_command_only (line )
1878+ if not statement .multiline_command :
1879+ # not a multiline command, so raise the exception
1880+ raise
1881+
1882+ # if we get here we must have:
1883+ # - a multiline command with no terminator
1884+ # - a multiline command with unclosed quotation marks
18631885 if not self .quit_on_sigint :
18641886 try :
18651887 newline = self .pseudo_raw_input (self .continuation_prompt )
@@ -1885,7 +1907,6 @@ def _complete_statement(self, line: str) -> Statement:
18851907 newline = '\n '
18861908 self .poutput (newline )
18871909 line = '{}\n {}' .format (statement .raw , newline )
1888- statement = self .statement_parser .parse (line )
18891910
18901911 if not statement .command :
18911912 raise EmptyStatement ()
0 commit comments