-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-124529: Fix _strptime to make %c/%x accept a year with fewer digits
#124778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zuo
wants to merge
25
commits into
python:main
Choose a base branch
from
zuo:strptime-c-and-y-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
827db45
gh-124529: Fix _strptime to make %c/%x accept year with fewer digits
zuo 1d916a0
Add the blurb
zuo a4ba3c9
Fix/improve (and shorten) the blurb
zuo cef0722
Fix/improve/refactor tests and restrict their scope
zuo 6556bdf
Test fail message fixes
zuo 56c84dc
Continue enhancing/improving tests
zuo b42783c
Improve docstring
zuo 97b523e
Add a suitable mention in the docs + improve blurb
zuo 6c2addf
Docstring edit
zuo fb32c03
Edit the mention in the docs
zuo 722efe4
Minor docs edit
zuo 7b46a48
Fix/improve tests
zuo 3d4116a
Shorten blurb again
zuo 8eac106
Minor docs edit
zuo 1956662
Docstring edit
zuo a77c3da
Docs edit
zuo b3b161f
Docstring edit
zuo 949e66a
Minor test improvements/refactoring
zuo 059d4c5
Add `time.strptime()`-dedicated test
zuo 95753ac
Minor test improvements/refactoring
zuo ed4582b
Merge branch 'main' into strptime-c-and-y-fix
zuo 990b075
Minor test improvement
zuo ecc359d
Test improvements/refactoring
zuo 6d690ca
Minor refactoring
zuo 4ad1062
Merge branch 'main' into strptime-c-and-y-fix
zuo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,11 +161,42 @@ def test_compile(self): | |
| for directive in ('a','A','b','B','c','d','G','H','I','j','m','M','p', | ||
| 'S','u','U','V','w','W','x','X','y','Y','Z','%'): | ||
| fmt = "%d %Y" if directive == 'd' else "%" + directive | ||
| input_string = time.strftime(fmt) | ||
| compiled = self.time_re.compile(fmt) | ||
| found = compiled.match(time.strftime(fmt)) | ||
| self.assertTrue(found, "Matching failed on '%s' using '%s' regex" % | ||
| (time.strftime(fmt), | ||
| compiled.pattern)) | ||
| found = compiled.match(input_string) | ||
| self.assertTrue(found, | ||
| (f"Matching failed on '{input_string}' " | ||
| f"using '{compiled.pattern}' regex")) | ||
| for directive in ('c', 'x'): | ||
| fmt = "%" + directive | ||
| with self.subTest(f"{fmt!r} should match input containing " | ||
| f"year with fewer digits than usual"): | ||
| # gh-124529 | ||
| params = _input_str_and_expected_year_for_few_digits_year(fmt) | ||
| if params is None: | ||
| self.skipTest(f"this subtest needs locale for which " | ||
| f"{fmt!r} includes year in some variant") | ||
| input_string, _ = params | ||
| compiled = self.time_re.compile(fmt) | ||
| found = compiled.match(input_string) | ||
| self.assertTrue(found, | ||
| (f"Matching failed on '{input_string}' " | ||
| f"using '{compiled.pattern}' regex")) | ||
| for directive in ('y', 'Y'): | ||
| fmt = "%" + directive | ||
| with self.subTest(f"{fmt!r} should not match input containing " | ||
| f"year with fewer digits than usual"): | ||
| params = _input_str_and_expected_year_for_few_digits_year(fmt) | ||
| if params is None: | ||
| self.skipTest(f"this subtest needs locale for which " | ||
| f"{fmt!r} includes year in some variant") | ||
| input_string, _ = params | ||
| compiled = self.time_re.compile(fmt) | ||
| found = compiled.match(input_string) | ||
| self.assertFalse(found, | ||
| (f"Matching unexpectedly succeeded " | ||
| f"on '{input_string}' using " | ||
| f"'{compiled.pattern}' regex")) | ||
|
|
||
| def test_blankpattern(self): | ||
| # Make sure when tuple or something has no values no regex is generated. | ||
|
|
@@ -299,6 +330,25 @@ def helper(self, directive, position): | |
| (directive, strf_output, strp_output[position], | ||
| self.time_tuple[position])) | ||
|
|
||
| def helper_for_directives_accepting_few_digits_year(self, directive): | ||
| fmt = "%" + directive | ||
| params = _input_str_and_expected_year_for_few_digits_year(fmt) | ||
| if params is None: | ||
| self.skipTest(f"test needs locale for which {fmt!r} " | ||
| f"includes year in some variant") | ||
| input_string, expected_year = params | ||
| try: | ||
| output_year = _strptime._strptime(input_string, fmt)[0][0] | ||
| except ValueError as exc: | ||
| # See: gh-124529 | ||
| self.fail(f"testing of {directive!r} directive failed; " | ||
| f"{input_string!r} -> exception: {exc!r}") | ||
| else: | ||
| self.assertEqual(output_year, expected_year, | ||
| (f"testing of {directive!r} directive failed; " | ||
| f"{input_string!r} -> output including year " | ||
| f"{output_year!r} != {expected_year!r}")) | ||
|
|
||
| def test_year(self): | ||
| # Test that the year is handled properly | ||
| for directive in ('y', 'Y'): | ||
|
|
@@ -312,6 +362,17 @@ def test_year(self): | |
| "'y' test failed; passed in '%s' " | ||
| "and returned '%s'" % (bound, strp_output[0])) | ||
|
|
||
| def test_bad_year(self): | ||
| for directive, bad_inputs in ( | ||
| ('y', ('9', '100', 'ni')), | ||
| ('Y', ('7', '42', '999', '10000', 'SPAM')), | ||
| ): | ||
| fmt = "%" + directive | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you can use f-string. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both ways are valid; the one I chose is more consistent with the existing code in this module. |
||
| for input_val in bad_inputs: | ||
| with self.subTest(directive=directive, input_val=input_val): | ||
| with self.assertRaises(ValueError): | ||
| _strptime._strptime_time(input_val, fmt) | ||
|
|
||
| def test_month(self): | ||
| # Test for month directives | ||
| for directive in ('B', 'b', 'm'): | ||
|
|
@@ -454,11 +515,21 @@ def test_date_time(self): | |
| for position in range(6): | ||
| self.helper('c', position) | ||
|
|
||
| def test_date_time_accepting_few_digits_year(self): # gh-124529 | ||
| # Test %c directive with input containing year | ||
| # number consisting of fewer digits than usual | ||
| self.helper_for_directives_accepting_few_digits_year('c') | ||
|
|
||
| def test_date(self): | ||
| # Test %x directive | ||
| for position in range(0,3): | ||
| self.helper('x', position) | ||
|
|
||
| def test_date_accepting_few_digits_year(self): # gh-124529 | ||
| # Test %x directive with input containing year | ||
| # number consisting of fewer digits than usual | ||
| self.helper_for_directives_accepting_few_digits_year('x') | ||
|
|
||
| def test_time(self): | ||
| # Test %X directive | ||
| for position in range(3,6): | ||
|
|
@@ -769,5 +840,55 @@ def test_TimeRE_recreation_timezone(self): | |
| _strptime._strptime_time(oldtzname[1], '%Z') | ||
|
|
||
|
|
||
| def _input_str_and_expected_year_for_few_digits_year(fmt): | ||
| # This helper, for the given format string (fmt), returns a 2-tuple: | ||
| # (<strptime input string>, <expected year>) | ||
| # where: | ||
| # * <strptime input string> -- is a `strftime(fmt)`-result-like str | ||
| # containing a year number which is *shorter* than the usual four | ||
| # or two digits (namely: the contained year number consist of just | ||
| # one digit: 7; the choice of this particular digit is arbitrary); | ||
| # * <expected year> -- is an int representing the year number that | ||
| # is expected to be part of the result of a `strptime(<strptime | ||
| # input string>, fmt)` call (namely: either 7 or 2007, depending | ||
| # on the given format string and current locale...); however, it | ||
| # is None if <strptime input string> does *not* contain the year | ||
| # part (for the given format string and current locale). | ||
|
|
||
| # 1. Prepare auxiliary *magic* time data (note that the magic values | ||
| # we use here are guaranteed to be compatible with `time.strftime()` | ||
| # and also well distinguishable within a formatted string, thanks to | ||
| # the fact that the amount of overloaded numbers is minimized, as in | ||
| # `_strptime.LocaleTime.__calc_date_time()`...): | ||
| magic_year = 1999 | ||
| magic_tt = (magic_year, 3, 17, 22, 44, 55, 2, 76, 0) | ||
| magic_4digits = str(magic_year) | ||
| magic_2digits = magic_4digits[-2:] | ||
|
|
||
| # 2. Pick our example year whose representation | ||
| # is shorter than the usual four or two digits: | ||
| input_year_str = '7' | ||
|
|
||
| # 3. Determine the <strptime input string> part of the return value: | ||
| input_string = time.strftime(fmt, magic_tt) | ||
| if (index_4digits := input_string.find(magic_4digits)) != -1: | ||
| # `input_string` contains up-to-4-digit year representation | ||
| input_string = input_string.replace(magic_4digits, input_year_str) | ||
| if (index_2digits := input_string.find(magic_2digits)) != -1: | ||
| # `input_string` contains up-to-2-digit year representation | ||
| input_string = input_string.replace(magic_2digits, input_year_str) | ||
|
|
||
| # 4. Determine the <expected year> part of the return value: | ||
| if index_4digits > index_2digits: | ||
| expected_year = int(input_year_str) | ||
| elif index_4digits < index_2digits: | ||
| expected_year = 2000 + int(input_year_str) | ||
| else: | ||
| assert index_4digits == index_2digits == -1 | ||
| expected_year = None | ||
|
|
||
| return input_string, expected_year | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is't possible for
_strptime._strptimeto return a one-dim list?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It always returns a 3-
tuple, the 0th item of which is a 9-tuple, the 0th item of which is aintrepresenting the year number.