Repository navigation
Open
Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = None
created_at = <Date 2014-08-20.10:01:51.226>
labels = ['type-bug', 'docs']
title = 'str.splitlines splitting on non-\\r\\n characters'
updated_at = <Date 2018-10-07.20:48:13.606>
user = 'https://bugs.python.org/scharron'bugs.python.org fields:
activity = <Date 2018-10-07.20:48:13.606>
actor = 'nascheme'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2014-08-20.10:01:51.226>
creator = 'scharron'
dependencies = []
files = ['43071', '43072', '43075']
hgrepos = []
issue_num = 22232
keywords = ['patch']
message_count = 34.0
messages = ['225561', '225564', '225705', '225709', '225723', '225736', '225747', '225748', '225755', '225758', '225766', '225767', '225769', '225873', '225874', '230138', '230143', '230144', '230152', '246547', '246570', '266781', '266782', '266785', '266789', '266800', '327105', '327106', '327113', '327137', '327141', '327162', '327269', '327308']
nosy_count = 16.0
nosy_names = ['lemburg', 'barry', 'nascheme', 'terry.reedy', 'gregory.p.smith', 'vstinner', 'jwilk', 'ezio.melotti', 'steven.daprano', 'r.david.murray', 'docs@python', 'python-dev', 'martin.panter', 'serhiy.storchaka', 'scharron', 'Alexander Schrijver']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue22232'
versions = ['Python 2.7', 'Python 3.4', 'Python 3.5']
Activity
scharron commentedon Aug 20, 2014
According to the documentation, str.splitlines uses the universal newlines to split lines.
The documentation says it's all about \r, \n, and \r\n (https://docs.python.org/3.5/glossary.html#term-universal-newlines)
However, it's also splitting on other characters. Reading the code, it seems the list of characters is from Objects/unicodeobject.c , in _PyUnicode_Init, the linebreak array.
When testing any of these characters, it splits the string.
Other libraries are using str.splitlines assuming it only breaks on these \r and \n characters. This is the case of email.feedparser for instance, used by http.client to parse headers. These HTTP headers should be separated by CLRF as specified by http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.
Either the documentation should state that splitlines splits on other characters or it should stick to the documentation and split only on \r and \n characters.
If it splits on other characters, the list could be improved, as the unicode reference lists the mandatory characters for line breaking : http://www.unicode.org/reports/tr14/tr14-32.html#BK
scharron commentedon Aug 20, 2014
For an example of a serious bug caused by this, see http://bugs.python.org/issue22233
terryjreedy commentedon Aug 22, 2014
[-]str.splitlines splitting on none-\r\n characters[/-][+]str.splitlines splitting on non-\r\n characters[/+]scharron commentedon Aug 22, 2014
It's also at line bpo-14941 for unicode strings if I understand correctly
With 3.4.0:
>>> "a\x85b\x1ec".splitlines() ['a', 'b', 'c']bitdancer commentedon Aug 23, 2014
See bpo-7643 for some technical background. There are some other interesting issues to read if you seach the tracker for 'splitlines unicode', one of which is an open doc issue. Clearly the docs about this are inadequate.
Basically, though, I think you are correct. email should not be using splitlines(). It was more or less correct when email was splitting binary data, but even then it wasn't exactly correct per the letter of RFC.
Unfortunately not using splitlines has some performance implications...but then again we haven't done any sort of performance improvement pass on the new email code, so it may well be marginal in the overall scheme of things.
scharron commentedon Aug 23, 2014
This is a known issue, and will be resolved by improving documentation, I'm closing this bug
Thanks !
bitdancer commentedon Aug 23, 2014
OK, we'll use bpo-22232 to resolve the issue of email using splitlines.
serhiy-storchaka commentedon Aug 23, 2014
May be add a parameter to str.splitlines() which will switch behavior to split on '\n', '\r' and '\r\n' only?
terryjreedy commentedon Aug 23, 2014
Unless there is already another issue for improving the doc, this should at least be left open as a doc issue.
But I had the same thought as Serhiy, that we should at least optionally make the current doc correct. Two possibilities:
Changing both code and doc, at least in 3.5, says that both are wrong. If we agree on this, there is still the awkward issue of what to do for 3.4. Just change the doc? Then email must do something different in 3.4 to work around the code behavior. I think this may warrant a pydev discussion.
Another issue is whether latin-1 linebreaks are privileged. Why not implement the full unicode linebreak algorithm.
An additional complication is that in 2.x, .splitlines acts as advertised.
>>> 'a\x0ab\x0bc\x0cd\x0dda\x0d\x0a1c\x1c1d\x1d1e\x1e85\x85end'.splitlines() ['a', 'b\x0bc\x0cd', 'da', '1c\x1c1d\x1d1e\x1e85\x85end']20 remaining items
AlexanderSchrijver commentedon May 31, 2016
Oops, wrong diff. Sorry, this is the correct one for 2.7.
vadmium commentedon May 31, 2016
For Python 3, the bytes.splitlines() and bytearray.splitlines() documentation has been moved to a separate section out (bpo-21777). I don’t think it is good to add much detail of bytes.splitlines() in the str.splitlines() documentation.
For Python 2, perhaps see Matthew’s patches for 2.7 in bpo-12855. IMO we could reopen that bug if that helps, because only the Python 3 branches were comitted.
AlexanderSchrijver commentedon Jun 1, 2016
I appeared to have missed the reference to that issue when I read this issue the first time. Re-opening that issue makes sense to me.
nascheme commentedon Oct 5, 2018
If we introduce a keyword parameter, I think the default of str.splitlines() should be changed to match bytes.splitlines (and match Python 2 str.splitlines()). I.e. split on \r and \n by default. I looked through the stdline and I can't find any calls that should actually by splitting on the extra characters. I will check it again though.
Does anyone have an example of where the current behaviour is actually wanted?
serhiy-storchaka commentedon Oct 5, 2018
If change the default behavior we need to wait several releases after adding this option. Users should be able to pick the current behavior explicitly.
Currently the workaround is using regular expressions.
For s.splitlines(keepends=False):
For s.splitlines(keepends=True):
malemburg commentedon Oct 5, 2018
I am -1 on changing the default behavior. The Unicode standard defines what a linebreak code point is (all code points with character properties Zl or bidirectional property B) and we adhere to that. This may confuse parsers coming from the ASCII world, but that's really a problem with those parsers assuming that .splitlines() only splits on ASCII line breaks, i.e. they are not written in a Unicode compatible way.
As mentioned in https://bugs.python.org/issue18291 we could add a parameter to .splitlines(), but this would render the method not much faster than re.split().
Using re.split() is not a work-around in his case, it's an explicit form of defining the character you want to split lines on, if the standards defining your file format as only accepting ASCII line break characters.
Since there are many such file formats, perhaps adding a parameter asciionly=True/False would make sense. .splitlines() could then be made to only split on ASCII linebreak characters. This new parameter would then have to default to False to maintain compatibility with Unicode and all previous releases.
nascheme commentedon Oct 5, 2018
I've created a topic on this inside the "Ideas" area of discuss.python.org. Sorry if that wasn't appropriate, not sure if I should have keep the discussion here.
Inada Naoki suggests creating a new method str.iterlines{[keepends]). Given that people are -1 on changing str.splitlines, I think that's a good solution. A new method is better yet if it would only split on '\n', that way fp.read().iterlines() matches fp.readlines(). It is what people seem to expect and is the most handy behaviour. So, str and bytes would both get the new method and they would both split on only '\n'.
If we do that, I think nearly every use of splitlines() should get changed to iterlines().
malemburg commentedon Oct 5, 2018
Why not simply add a new parameter, to make people who want
ASCII linebreaks continue to use .splitlines() ?
It think it would be less than ideal to have one method break on
all Unicode line breaks and another only on ASCII ones.
nascheme commentedon Oct 5, 2018
That could work but I think in nearly every case you don't want to use splitlines() without supplying the parameter. So, it seems like a bit of trap for new users. Worse, because in Python 2, str.splitlines() does what they want, they will do the simple thing which is likely wrong.
If we do stick with just splitlines(), perhaps it should get a 'newline' parameter that mostly matches io.open (i.e. it controls universal newline behavior). So if you don't want to change behavior, str.splitlines(newline=None) would split as it currently does. To make it split like io files do, you would have to do newline='\n'.
To me, it seems attractive that:
fp.readlines() == fp.read().iterlines()
You suggestion would make it something like:
fp.readlines() == fp.read().splitlines(newline='\n')
I guess I could live with that but it seems unnecessarily ugly and verbose for what is the most common usage.
stevendaprano commentedon Oct 7, 2018
I don't like the idea of adding a second bool parameter to splitlines. Guido has a rough rule of thumb (which I agree with) of "no constant bool parameters". If people will typically call a function with some sort of "mode" parameter using a hard-coded bool, then we should usually prefer to split the two modes into distinct functions.
As an example, we have statistics.stdev and pstdev rather than stdev(data, population=False).
Obviously this is a guideline, not a hard rule, and there are exceptions. Such as str.splitlines :-)
In any case, I suggest a separate string method. Even though the name is slightly inaccurate, I suggest "ascii_splitlines" which I think is accurate enough to capture the spirit of what we intend (split on *only* \n \r and \r\n) and we can leave the details in the docs.
nascheme commentedon Oct 7, 2018
I too would prefer a new method name rather than overloading splitlines() with more keyword args (passed as hardcoded constants, usually). Again, I think we want:
list(open(..).read().<splitmethod>()) == list(open(..))
readlines() returns a list but I think this method should return an iterator (seems more Python 3 like to me, call list if you want a list). In that case, iterlines() seems like the right name to me. I think it should take a 'newline' keyword that behaves the same as the open() version of the keyword.
LoicGrobol commentedon Apr 9, 2025
@malemburg I realise this is extreme nitpicking, but
I couldn't find a trace of bidirectional B characters being non-tailorable line breaks (classes BK, CR, LF, NL), neither in the line breaking not in the bidirectional algorithm specification. What I think the latter means is that B characters actually only “terminate paragraphs” in the sense of “terminating the effect of directional formatting characters”. Now it is true that
\N{PARAGRAPH SEPARATOR}is both B and BK, but does it means that all B characters should be line breaks? In particular what about\N{FILE SEPARATOR},\N{GROUP SEPARATOR}and\N{RECORD SEPARATOR}, who are all caught bystr.splitlines, but have the Combining Mark property (see e.g. the list of line breaks), which should in fact forbid a line break?malemburg commentedon Apr 10, 2025
At the time this code was written (in 2000), Unicode 3.0 was the current version and in those days, bidirectional B code points were listed as "Paragraph Separator", in particular the important ones CR, LF. See https://www.unicode.org/Public/3.0-Update/UnicodeData-3.0.0.html and https://www.unicode.org/Public/3.0-Update/UnicodeData-3.0.0.txt for details.
I don't remember, but it's well possible that I wasn't aware of the CM LineBreak property at the time.
I guess the
_PyUnicode_IsLinebreak()function should really use the LineBreak.txt file as basis and only include code points which are listed as "Non-tailorable" in that file: https://www.unicode.org/Public/UCD/latest/ucd/LineBreak.txtPerhaps it's time to fix this after 25 years 😄
Patches are welcome !
LoicGrobol commentedon Apr 10, 2025
@malemburg alright, I'll start working on a patch then!