From 68bd800e910da5d9de144446ea809ac1e91b7b91 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Sun, 1 Jun 2025 19:46:07 +0800 Subject: [PATCH 1/5] Change the default *encoding* of `calendar.HTMLCalendar` to UTF-8 --- Doc/library/calendar.rst | 10 +++++++--- Doc/whatsnew/3.15.rst | 7 +++++++ Lib/calendar.py | 14 +++----------- .../2025-06-01-19-45-55.gh-issue-135001.KsBXSl.rst | 2 ++ 4 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-06-01-19-45-55.gh-issue-135001.KsBXSl.rst diff --git a/Doc/library/calendar.rst b/Doc/library/calendar.rst index 39090e36ed9c0d..de863854e987bd 100644 --- a/Doc/library/calendar.rst +++ b/Doc/library/calendar.rst @@ -245,14 +245,16 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is specifies the number of months per row. - .. method:: formatyearpage(theyear, width=3, css='calendar.css', encoding=None) + .. method:: formatyearpage(theyear, width=3, css='calendar.css', encoding='utf-8') Return a year's calendar as a complete HTML page. *width* (defaulting to 3) specifies the number of months per row. *css* is the name for the cascading style sheet to be used. :const:`None` can be passed if no style sheet should be used. *encoding* specifies the encoding to be used for the - output (defaulting to the system default encoding). + output (defaulting to ``utf-8``). + .. versionchanged:: next + The default value of the *encoding* has been changed to utf-8. .. method:: formatmonthname(theyear, themonth, withyear=True) @@ -651,8 +653,10 @@ The following options are accepted: .. option:: --encoding ENCODING, -e ENCODING The encoding to use for output. - :option:`--encoding` is required if :option:`--locale` is set. + Defaults to utf-8. +.. versionchanged:: next + The default value has been changed to utf-8. .. option:: --type {text,html}, -t {text,html} diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index 6d1f653f086a15..e3e532eb8cb4ef 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -138,6 +138,13 @@ Deprecated Removed ======= +calendar +-------- + +* The default *encoding* parameter value in :meth:`calendar.HTMLCalendar.formatyearpage` + is now utf-8. + (Contributed by Jiahao Li in :gh:`135001`.) + ctypes ------ diff --git a/Lib/calendar.py b/Lib/calendar.py index 18f76d52ff8581..6c9809985d7122 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -560,12 +560,10 @@ def formatyear(self, theyear, width=3): a('') return ''.join(v) - def formatyearpage(self, theyear, width=3, css='calendar.css', encoding=None): + def formatyearpage(self, theyear, width=3, css='calendar.css', encoding='utf-8'): """ Return a formatted year as a complete HTML page. """ - if encoding is None: - encoding = sys.getdefaultencoding() v = [] a = v.append a('\n' % encoding) @@ -845,8 +843,8 @@ def main(args=None): ) parser.add_argument( "-e", "--encoding", - default=None, - help="encoding to use for output" + default="utf-8", + help="encoding to use for output (default utf-8)" ) parser.add_argument( "-t", "--type", @@ -872,10 +870,6 @@ def main(args=None): options = parser.parse_args(args) - if options.locale and not options.encoding: - parser.error("if --locale is specified --encoding is required") - sys.exit(1) - locale = options.locale, options.encoding today = datetime.date.today() @@ -889,8 +883,6 @@ def main(args=None): cal = HTMLCalendar() cal.setfirstweekday(options.first_weekday) encoding = options.encoding - if encoding is None: - encoding = sys.getdefaultencoding() optdict = dict(encoding=encoding, css=options.css) write = sys.stdout.buffer.write if options.year is None: diff --git a/Misc/NEWS.d/next/Library/2025-06-01-19-45-55.gh-issue-135001.KsBXSl.rst b/Misc/NEWS.d/next/Library/2025-06-01-19-45-55.gh-issue-135001.KsBXSl.rst new file mode 100644 index 00000000000000..e573349edfde5f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-06-01-19-45-55.gh-issue-135001.KsBXSl.rst @@ -0,0 +1,2 @@ +The default *encoding* parameter value in +:meth:`calendar.HTMLCalendar.formatyearpage` is now utf-8. From a232d70183f1f0c500b5cfc15704f379138b721f Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Fri, 6 Jun 2025 09:21:09 +0800 Subject: [PATCH 2/5] fix --- Doc/library/calendar.rst | 12 +++--------- Lib/calendar.py | 12 ++++++++++-- .../2025-06-01-19-45-55.gh-issue-135001.KsBXSl.rst | 2 -- .../2025-06-06-09-20-46.gh-issue-135001.zotIVz.rst | 3 +++ 4 files changed, 16 insertions(+), 13 deletions(-) delete mode 100644 Misc/NEWS.d/next/Library/2025-06-01-19-45-55.gh-issue-135001.KsBXSl.rst create mode 100644 Misc/NEWS.d/next/Library/2025-06-06-09-20-46.gh-issue-135001.zotIVz.rst diff --git a/Doc/library/calendar.rst b/Doc/library/calendar.rst index de863854e987bd..2fa80e93acc425 100644 --- a/Doc/library/calendar.rst +++ b/Doc/library/calendar.rst @@ -245,16 +245,13 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is specifies the number of months per row. - .. method:: formatyearpage(theyear, width=3, css='calendar.css', encoding='utf-8') + .. method:: formatyearpage(theyear, width=3, css='calendar.css', encoding=None) Return a year's calendar as a complete HTML page. *width* (defaulting to 3) specifies the number of months per row. *css* is the name for the cascading style sheet to be used. :const:`None` can be passed if no style sheet should be used. *encoding* specifies the encoding to be used for the - output (defaulting to ``utf-8``). - - .. versionchanged:: next - The default value of the *encoding* has been changed to utf-8. + output (defaulting to ``'utf-8'``). .. method:: formatmonthname(theyear, themonth, withyear=True) @@ -653,10 +650,7 @@ The following options are accepted: .. option:: --encoding ENCODING, -e ENCODING The encoding to use for output. - Defaults to utf-8. - -.. versionchanged:: next - The default value has been changed to utf-8. + :option:`--encoding` is required if :option:`--locale` is set. .. option:: --type {text,html}, -t {text,html} diff --git a/Lib/calendar.py b/Lib/calendar.py index 6c9809985d7122..3be1b50500eb07 100644 --- a/Lib/calendar.py +++ b/Lib/calendar.py @@ -560,10 +560,12 @@ def formatyear(self, theyear, width=3): a('') return ''.join(v) - def formatyearpage(self, theyear, width=3, css='calendar.css', encoding='utf-8'): + def formatyearpage(self, theyear, width=3, css='calendar.css', encoding=None): """ Return a formatted year as a complete HTML page. """ + if encoding is None: + encoding = 'utf-8' v = [] a = v.append a('\n' % encoding) @@ -843,7 +845,7 @@ def main(args=None): ) parser.add_argument( "-e", "--encoding", - default="utf-8", + default=None, help="encoding to use for output (default utf-8)" ) parser.add_argument( @@ -870,6 +872,10 @@ def main(args=None): options = parser.parse_args(args) + if options.locale and not options.encoding: + parser.error("if --locale is specified --encoding is required") + sys.exit(1) + locale = options.locale, options.encoding today = datetime.date.today() @@ -883,6 +889,8 @@ def main(args=None): cal = HTMLCalendar() cal.setfirstweekday(options.first_weekday) encoding = options.encoding + if encoding is None: + encoding = 'utf-8' optdict = dict(encoding=encoding, css=options.css) write = sys.stdout.buffer.write if options.year is None: diff --git a/Misc/NEWS.d/next/Library/2025-06-01-19-45-55.gh-issue-135001.KsBXSl.rst b/Misc/NEWS.d/next/Library/2025-06-01-19-45-55.gh-issue-135001.KsBXSl.rst deleted file mode 100644 index e573349edfde5f..00000000000000 --- a/Misc/NEWS.d/next/Library/2025-06-01-19-45-55.gh-issue-135001.KsBXSl.rst +++ /dev/null @@ -1,2 +0,0 @@ -The default *encoding* parameter value in -:meth:`calendar.HTMLCalendar.formatyearpage` is now utf-8. diff --git a/Misc/NEWS.d/next/Library/2025-06-06-09-20-46.gh-issue-135001.zotIVz.rst b/Misc/NEWS.d/next/Library/2025-06-06-09-20-46.gh-issue-135001.zotIVz.rst new file mode 100644 index 00000000000000..655db9a5c2b5b3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-06-06-09-20-46.gh-issue-135001.zotIVz.rst @@ -0,0 +1,3 @@ +Replace *encoding* parameter value in +:meth:`calendar.HTMLCalendar.formatyearpage` from +:func:`sys.getdefaultencoding()` to ``utf-8``. From a0465f39418017dbb8eadaa0e0b41763acd5660f Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Fri, 6 Jun 2025 09:31:07 +0800 Subject: [PATCH 3/5] fix --- Doc/library/calendar.rst | 2 ++ Doc/whatsnew/3.15.rst | 7 ------- Lib/test/test_calendar.py | 2 +- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/Doc/library/calendar.rst b/Doc/library/calendar.rst index 2fa80e93acc425..b292d828841f2f 100644 --- a/Doc/library/calendar.rst +++ b/Doc/library/calendar.rst @@ -253,6 +253,7 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is sheet should be used. *encoding* specifies the encoding to be used for the output (defaulting to ``'utf-8'``). + .. method:: formatmonthname(theyear, themonth, withyear=True) Return a month name as an HTML table row. If *withyear* is true the year @@ -652,6 +653,7 @@ The following options are accepted: The encoding to use for output. :option:`--encoding` is required if :option:`--locale` is set. + .. option:: --type {text,html}, -t {text,html} Print the calendar to the terminal as text, diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index e3e532eb8cb4ef..6d1f653f086a15 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -138,13 +138,6 @@ Deprecated Removed ======= -calendar --------- - -* The default *encoding* parameter value in :meth:`calendar.HTMLCalendar.formatyearpage` - is now utf-8. - (Contributed by Jiahao Li in :gh:`135001`.) - ctypes ------ diff --git a/Lib/test/test_calendar.py b/Lib/test/test_calendar.py index 7ade4271b7a156..bc39c86b8cf62d 100644 --- a/Lib/test/test_calendar.py +++ b/Lib/test/test_calendar.py @@ -417,7 +417,7 @@ def test_output_htmlcalendar_encoding_utf8(self): self.check_htmlcalendar_encoding('utf-8', 'utf-8') def test_output_htmlcalendar_encoding_default(self): - self.check_htmlcalendar_encoding(None, sys.getdefaultencoding()) + self.check_htmlcalendar_encoding(None, 'utf-8') def test_yeardatescalendar(self): def shrink(cal): From f5da0b981cde2c5656a799bc58d31dad09f23580 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Fri, 6 Jun 2025 09:38:10 +0800 Subject: [PATCH 4/5] lint --- .../next/Library/2025-06-06-09-20-46.gh-issue-135001.zotIVz.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-06-06-09-20-46.gh-issue-135001.zotIVz.rst b/Misc/NEWS.d/next/Library/2025-06-06-09-20-46.gh-issue-135001.zotIVz.rst index 655db9a5c2b5b3..f79b31b3532659 100644 --- a/Misc/NEWS.d/next/Library/2025-06-06-09-20-46.gh-issue-135001.zotIVz.rst +++ b/Misc/NEWS.d/next/Library/2025-06-06-09-20-46.gh-issue-135001.zotIVz.rst @@ -1,3 +1,3 @@ Replace *encoding* parameter value in :meth:`calendar.HTMLCalendar.formatyearpage` from -:func:`sys.getdefaultencoding()` to ``utf-8``. +:func:`sys.getdefaultencoding` to ``utf-8``. From bf55ca4f2adbedcf2b684c3660c2beb98f690206 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Fri, 6 Jun 2025 16:07:33 +0800 Subject: [PATCH 5/5] Delete Misc/NEWS.d/next/Library/2025-06-06-09-20-46.gh-issue-135001.zotIVz.rst --- .../Library/2025-06-06-09-20-46.gh-issue-135001.zotIVz.rst | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 Misc/NEWS.d/next/Library/2025-06-06-09-20-46.gh-issue-135001.zotIVz.rst diff --git a/Misc/NEWS.d/next/Library/2025-06-06-09-20-46.gh-issue-135001.zotIVz.rst b/Misc/NEWS.d/next/Library/2025-06-06-09-20-46.gh-issue-135001.zotIVz.rst deleted file mode 100644 index f79b31b3532659..00000000000000 --- a/Misc/NEWS.d/next/Library/2025-06-06-09-20-46.gh-issue-135001.zotIVz.rst +++ /dev/null @@ -1,3 +0,0 @@ -Replace *encoding* parameter value in -:meth:`calendar.HTMLCalendar.formatyearpage` from -:func:`sys.getdefaultencoding` to ``utf-8``.