Skip to content

Commit 962fed9

Browse files
committed
Fix closes Issue10087 - fixing the output of calendar display in the html format. Patch by Chris Lambacher. Test Contributed by catherine.
1 parent 77c4fd0 commit 962fed9

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

Lib/calendar.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ def main(args):
636636
parser.add_option(
637637
"-e", "--encoding",
638638
dest="encoding", default=None,
639-
help="Encoding to use for output"
639+
help="Encoding to use for output."
640640
)
641641
parser.add_option(
642642
"-t", "--type",
@@ -662,10 +662,11 @@ def main(args):
662662
if encoding is None:
663663
encoding = sys.getdefaultencoding()
664664
optdict = dict(encoding=encoding, css=options.css)
665+
write = sys.stdout.buffer.write
665666
if len(args) == 1:
666-
print(cal.formatyearpage(datetime.date.today().year, **optdict))
667+
write(cal.formatyearpage(datetime.date.today().year, **optdict))
667668
elif len(args) == 2:
668-
print(cal.formatyearpage(int(args[1]), **optdict))
669+
write(cal.formatyearpage(int(args[1]), **optdict))
669670
else:
670671
parser.error("incorrect number of arguments")
671672
sys.exit(1)
@@ -687,9 +688,11 @@ def main(args):
687688
else:
688689
parser.error("incorrect number of arguments")
689690
sys.exit(1)
691+
write = sys.stdout.write
690692
if options.encoding:
691693
result = result.encode(options.encoding)
692-
print(result)
694+
write = sys.stdout.buffer.write
695+
write(result)
693696

694697

695698
if __name__ == "__main__":

Lib/test/test_calendar.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33

44
from test import support
5+
from test.script_helper import assert_python_ok
56
import time
67
import locale
78

@@ -451,6 +452,11 @@ def test_several_leapyears_in_range(self):
451452
self.assertEqual(calendar.leapdays(1997,2020), 5)
452453

453454

455+
class ConsoleOutputTestCase(unittest.TestCase):
456+
def test_outputs_bytes(self):
457+
(return_code, stdout, stderr) = assert_python_ok('-m', 'calendar', '--type=html', '2010')
458+
self.assertEqual(stdout[:6], b'<?xml ')
459+
454460
def test_main():
455461
support.run_unittest(
456462
OutputTestCase,
@@ -460,6 +466,7 @@ def test_main():
460466
TimegmTestCase,
461467
MonthRangeTestCase,
462468
LeapdaysTestCase,
469+
ConsoleOutputTestCase
463470
)
464471

465472

0 commit comments

Comments
 (0)