From 814d5b9f9564b3f5a2a5428c54736240fbe5be0e 2011-12-09 18:12:42 From: MinRK Date: 2011-12-09 18:12:42 Subject: [PATCH] [qtconsole] carriage-return action matches CR only, not CRLF CarriageReturn action introduced in #1089 clears a line in the qtconsole, which means that CRLF line endings would replace whole lines with '\n'. This changes the regex to only match `\r` not followed by `\n` preventing the CR action from firing on CRLF. Test included closes #1111 --- diff --git a/IPython/frontend/qt/console/ansi_code_processor.py b/IPython/frontend/qt/console/ansi_code_processor.py index f027ccf..76019b3 100644 --- a/IPython/frontend/qt/console/ansi_code_processor.py +++ b/IPython/frontend/qt/console/ansi_code_processor.py @@ -38,7 +38,7 @@ CSI_SUBPATTERN = '\[(.*?)([%s])' % CSI_COMMANDS OSC_SUBPATTERN = '\](.*?)[\x07\x1b]' ANSI_PATTERN = ('\x01?\x1b(%s|%s)\x02?' % \ (CSI_SUBPATTERN, OSC_SUBPATTERN)) -ANSI_OR_SPECIAL_PATTERN = re.compile('(\b|\r)|(?:%s)' % ANSI_PATTERN) +ANSI_OR_SPECIAL_PATTERN = re.compile('(\b|\r(?!\n))|(?:%s)' % ANSI_PATTERN) SPECIAL_PATTERN = re.compile('([\f])') #----------------------------------------------------------------------------- diff --git a/IPython/frontend/qt/console/tests/test_ansi_code_processor.py b/IPython/frontend/qt/console/tests/test_ansi_code_processor.py index bbffd85..30698b1 100644 --- a/IPython/frontend/qt/console/tests/test_ansi_code_processor.py +++ b/IPython/frontend/qt/console/tests/test_ansi_code_processor.py @@ -105,12 +105,21 @@ class TestAnsiCodeProcessor(unittest.TestCase): def test_carriage_return(self): """ Are carriage return characters processed correctly? """ - string = 'foo\rbar' # form feed + string = 'foo\rbar' # carriage return self.assertEquals(list(self.processor.split_string(string)), ['foo', '', 'bar']) self.assertEquals(len(self.processor.actions), 1) action = self.processor.actions[0] self.assertEquals(action.action, 'carriage-return') + def test_carriage_return_newline(self): + """transform CRLF to LF""" + string = 'foo\rbar\r\ncat\r\n' # carriage return and newline + # only one CR action should occur, and '\r\n' should transform to '\n' + self.assertEquals(list(self.processor.split_string(string)), ['foo', '', 'bar\r\ncat\r\n']) + self.assertEquals(len(self.processor.actions), 1) + action = self.processor.actions[0] + self.assertEquals(action.action, 'carriage-return') + def test_beep(self): """ Are beep characters processed correctly? """