Skip to content

Commit

Permalink
gh-69714: Make calendar module fully tested (#93655)
Browse files Browse the repository at this point in the history
There are 3 paths to use `locale` argument in
`calendar.Locale{Text|HTML}Calendar.__init__(..., locale=None)`:
(1) `locale=None` -- denotes the "default locale"[1]
(2) `locale=""` -- denotes the native environment
(3) `locale=other_valid_locale` -- denotes a custom locale

So far case (2) is covered and case (1) is in 78935da (same branch).
This commit adds a remaining case (3).

[1] In the current implementation, this translates into the following
approach:

GET current locale
IF current locale == "C" THEN
  SET current locale TO ""
  GET current locale
ENDIF

* Remove unreachable code (and increase test coverage)

This condition cannot be true. `_locale.setlocale()` from the C module
raises `locale.Error` instead of returning `None` for
`different_locale.__enter__` (where `self.oldlocale` is set).

* Expand the try clause to calls to `LocaleTextCalendar.formatmonthname()`.

This method temporarily changes the current locale to the given locale,
so `_locale.setlocale()` may raise `local.Error`.


Co-authored-by: Rohit Mediratta <[email protected]>
Co-authored-by: Jessica McKellar <[email protected]>
Co-authored-by: Adam Turner <[email protected]>
Co-authored-by: Irit Katriel <[email protected]>
  • Loading branch information
5 people authored Jul 22, 2023
1 parent 463b56d commit 2aba047
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 54 deletions.
16 changes: 7 additions & 9 deletions Lib/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,6 @@ def __enter__(self):
_locale.setlocale(_locale.LC_TIME, self.locale)

def __exit__(self, *args):
if self.oldlocale is None:
return
_locale.setlocale(_locale.LC_TIME, self.oldlocale)


Expand Down Expand Up @@ -690,7 +688,7 @@ def timegm(tuple):
return seconds


def main(args):
def main(args=None):
import argparse
parser = argparse.ArgumentParser()
textgroup = parser.add_argument_group('text only arguments')
Expand Down Expand Up @@ -747,7 +745,7 @@ def main(args):
help="month number (1-12, text only)"
)

options = parser.parse_args(args[1:])
options = parser.parse_args(args)

if options.locale and not options.encoding:
parser.error("if --locale is specified --encoding is required")
Expand All @@ -756,6 +754,9 @@ def main(args):
locale = options.locale, options.encoding

if options.type == "html":
if options.month:
parser.error("incorrect number of arguments")
sys.exit(1)
if options.locale:
cal = LocaleHTMLCalendar(locale=locale)
else:
Expand All @@ -767,11 +768,8 @@ def main(args):
write = sys.stdout.buffer.write
if options.year is None:
write(cal.formatyearpage(datetime.date.today().year, **optdict))
elif options.month is None:
write(cal.formatyearpage(options.year, **optdict))
else:
parser.error("incorrect number of arguments")
sys.exit(1)
write(cal.formatyearpage(options.year, **optdict))
else:
if options.locale:
cal = LocaleTextCalendar(locale=locale)
Expand All @@ -795,4 +793,4 @@ def main(args):


if __name__ == "__main__":
main(sys.argv)
main()
Loading

0 comments on commit 2aba047

Please sign in to comment.