From 967952dd408cfcc8acc25b2ab15d007fe64bf85f Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Wed, 11 Mar 2020 05:47:21 +0900 Subject: [PATCH] Fix str.splitlines() for \r --- Lib/test/string_tests.py | 2 -- vm/src/obj/objstr.rs | 8 ++++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index 4af8a70ee..f1bfdf0c4 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -950,8 +950,6 @@ class BaseTest: self.checkequal('Getint', "getInt", 'title') self.checkraises(TypeError, 'hello', 'title', 42) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_splitlines(self): self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines') self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines') diff --git a/vm/src/obj/objstr.rs b/vm/src/obj/objstr.rs index e3b186cf4..d1885bb36 100644 --- a/vm/src/obj/objstr.rs +++ b/vm/src/obj/objstr.rs @@ -797,11 +797,15 @@ impl PyString { let keepends = args.keepends.unwrap_or(false); let mut elements = vec![]; let mut curr = "".to_owned(); - for ch in self.value.chars() { - if ch == '\n' { + let mut chars = self.value.chars().peekable(); + while let Some(ch) = chars.next() { + if ch == '\n' || ch == '\r' { if keepends { curr.push(ch); } + if ch == '\r' && chars.peek() == Some(&'\n') { + continue; + } elements.push(vm.ctx.new_str(curr.clone())); curr.clear(); } else {