@@ -1858,8 +1858,30 @@ def _complete_statement(self, line: str) -> Statement:
1858
1858
pipe runs out. We can't refactor it because we need to retain
1859
1859
backwards compatibility with the standard library version of cmd.
1860
1860
"""
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
1863
1885
if not self .quit_on_sigint :
1864
1886
try :
1865
1887
newline = self .pseudo_raw_input (self .continuation_prompt )
@@ -1885,7 +1907,6 @@ def _complete_statement(self, line: str) -> Statement:
1885
1907
newline = '\n '
1886
1908
self .poutput (newline )
1887
1909
line = '{}\n {}' .format (statement .raw , newline )
1888
- statement = self .statement_parser .parse (line )
1889
1910
1890
1911
if not statement .command :
1891
1912
raise EmptyStatement ()
0 commit comments