Created on 2016-09-23 08:51 by xiang.zhang, last changed 2022-04-11 14:58 by admin. This issue is now closed.
Currently TextCalendar.prweek/month/year outputs an extra whitespace which makes the output weird:
>>> calendar.TextCalendar().prweek([(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7)], 2)
1 2 3 4 5 6 7 >>> calendar.TextCalendar().prmonth(2016,9)
September 2016
Mo Tu We Th Fr Sa Su
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
>>>
The patch LGTM. Could you please add tests? I suppose this is a consequence of porting from Python 2 to Python 3 (maybe with 2to3). Usually "print x," in Python 2 corresponds to "print(x, end=' ')" in Python 3. But if x ends with \n, print with a comma in Python 2 don't add a space, this corresponds to "print(x, end='')". Similar issue was fixed not long ago. Maybe there are other similar issues in the stdlib or scripts.
prweek() in Python 2 adds a final space. Python 3 version looks correct.
A single `print x,` seems to me won't add the trailing space. And I don't understand why in py2 repl even with comma there is still a newline:
>>> print 'a',
a
>>>
>>> print('a', end='')
a>>>
Actually the behavior of the print statement in Python 2 is more complex. "print x," doesn't output a space after x, but sets the softspace attribute of the file to true (unless x is a string ending with non-space whitespace character like \n or \t). print tests this flag before outputting a value and outputs a space if it is true. Any output to a file resets this flag. This behavior can't be exactly reproduced in Python 3.
It looks to me that only prmonth() should be changed. prweek() and pryear() either match the behavior of PYthon 2 or are the good approximation of it.
New changeset 8d54c2a1c796 by Serhiy Storchaka in branch '3.5': Issue #28255: calendar.TextCalendar().prmonth() no longer prints a space https://hg.python.org/cpython/rev/8d54c2a1c796 New changeset ebe5bd33f86f by Serhiy Storchaka in branch '3.6': Issue #28255: calendar.TextCalendar().prmonth() no longer prints a space https://hg.python.org/cpython/rev/ebe5bd33f86f New changeset 049e58f74272 by Serhiy Storchaka in branch 'default': Issue #28255: calendar.TextCalendar().prmonth() no longer prints a space https://hg.python.org/cpython/rev/049e58f74272
New changeset 4a3892f49e1a by Serhiy Storchaka in branch 'default': Issue #28255: calendar.TextCalendar.prweek() no longer prints a space after https://hg.python.org/cpython/rev/4a3892f49e1a
prweek() and pryear() are changed only in 3.7. If this breaks somebody's code, there is enough time to update this code or rollback changes.
assignee: serhiy.storchaka
resolution: fixed
stage: patch review -> resolved
messages: + msg277263