Conversation
There was a problem hiding this comment.
Please also test the resulting timestamp is 1980-01-01 as expected.
There was a problem hiding this comment.
Please describe the argument in the main text, then just use .. versionadded:: 3.8 The *strict_timestamps* keyword argument in the changelog. Same below.
There was a problem hiding this comment.
Please use the international and unambiguous "ISO format" to refer to dates: 1980-01-01.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
I have made the requested changes; please review again |
|
Thanks for making the requested changes! @encukou: please review the changes made to this pull request. |
| given. | ||
| The archive must be open with mode ``'w'``, ``'x'`` or ``'a'``. | ||
| The *strict_timestamps* keyword argument allows to zip files older | ||
| than 1980-01-01 at the cost of setting the timestamp to 1980-01-01. |
There was a problem hiding this comment.
Just from the doc, I don't understand if I have to set the parameter to True or False to allow years older than 1980.
| mtime = time.localtime(st.st_mtime) | ||
| date_time = mtime[0:6] | ||
| if not strict_timestamps and date_time[0] < 1980: | ||
| date_time = (1980, 1, 1, 0, 0, 0) |
There was a problem hiding this comment.
It seems like the largest year is 2107. Years after 2017 should also be clamped to 2017-12-31, no?
|
|
||
| def test_add_file_before_1980_no_strict_timestamps(self): | ||
| # Set atime and mtime to 1970-01-01 | ||
| os.utime(TESTFN, (0, 0)) |
There was a problem hiding this comment.
Instead of using the real filesystem which can cause troubles, maybe you can mock os.stat() intsead of return the date that you want?
It may allow to test date after year 2107.
There was a problem hiding this comment.
The previous test is also using this. It shouldn't be a problem then.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
|
||
| .. method:: ZipFile.write(filename, arcname=None, compress_type=None, \ | ||
| compresslevel=None) | ||
| compresslevel=None, strict_timestamps=True) |
There was a problem hiding this comment.
I would prefer to use a keyword-only parameter. Same remark for other methods and functions.
There was a problem hiding this comment.
Bikeshedding: why not just "strict_timestamp=True" (no S)?
There was a problem hiding this comment.
there are more timestamps in the zip archive. each file has it's own.
| @@ -0,0 +1,2 @@ | |||
| ZipFile can zip files older than 1980-01-01 at the cost of mangling the | |||
There was a problem hiding this comment.
You should mention the new timestamps parameter. And maybe explain "mangling": dates before 1980-01-01 are replaced by 1980-01-01 at 00:00.
|
I have made the requested changes; please review again. |
| The *strict_timestamps* keyword argument, when set to ``False``, allows to | ||
| zip files older than 1980-01-01 at the cost of setting the | ||
| timestamp to 1980-01-01. | ||
| Same behavior occures with files older than 2107-12-31. |
There was a problem hiding this comment.
It is not the same behavior, it is not set to 1980-01-01 in that case, but rather to 2107-12-31.
There was a problem hiding this comment.
Maybe something like: "Similar behavior occurs with files older newer than 2107-12-31, the timestamp is also set to the limit."
There was a problem hiding this comment.
Sounds good.
I have also noticed I have 'older' where 'newer' should be.
| The *strict_timestamps* keyword argument, when set to ``False``, allows to | ||
| zip files older than 1980-01-01 at the cost of setting the | ||
| timestamp to 1980-01-01. | ||
| Same behavior occures with files older than 2107-12-31. |
vstinner
left a comment
There was a problem hiding this comment.
Aha, the new code now looks better! A few more comments.
| a :exc:`RuntimeError` was raised. | ||
|
|
||
| .. versionadded:: 3.8 | ||
| The *strict_timestamps* keyword argument |
There was a problem hiding this comment.
nitpick: you may write "keyword-only", and also add a final dot.
| If *arcname* is not specified, the name will be the same as *filename*, but | ||
| with any drive letter and leading path separators removed. | ||
|
|
||
| The *strict_timestamps* keyword argument, when set to ``False``, allows to |
There was a problem hiding this comment.
I don't think that it's needed to repeat here that it's a keyword argument, just write "argument".
| with zipfile.ZipFile(TESTFN2, "w") as zipfp: | ||
| self.assertRaises(struct.error, zipfp.write, TESTFN) | ||
|
|
||
| def test_add_file_after_2107_no_strict_timestamps(self): |
There was a problem hiding this comment.
IMHO you can move this test inside test_add_file_after_2107(), no need to have two different tests.
| with zipfile.ZipFile(TESTFN2, "w") as zipfp: | ||
| zipfp.write(TESTFN, strict_timestamps=False) | ||
| zinfo = zipfp.getinfo(TESTFN) | ||
| self.assertEqual(zinfo.date_time, (1980, 1, 1, 0, 0, 0)) |
There was a problem hiding this comment.
I would help to also make sure that you can add error when using strict_timestamps=True here.
vstinner
left a comment
There was a problem hiding this comment.
LGTM, but I would prefer to be the tests modified for my proposal.
| mtime = time.localtime(st.st_mtime) | ||
| date_time = mtime[0:6] | ||
| if not strict_timestamps and date_time[0] < 1980: | ||
| date_time = (1980, 1, 1, 0, 0, 0) |
| os.utime(TESTFN, (4386268800, 4386268800)) | ||
| with zipfile.ZipFile(TESTFN2, "w") as zipfp: | ||
| self.assertRaises(struct.error, zipfp.write, TESTFN) | ||
| zipfp.write(TESTFN, strict_timestamps=False) |
There was a problem hiding this comment.
I would prefer to reopen the file for the second test, since it's hard to guess what happens when on error occurs. I prefer to limit side effets of unit tests.
| os.utime(TESTFN, (0, 0)) | ||
| with zipfile.ZipFile(TESTFN2, "w") as zipfp: | ||
| self.assertRaises(ValueError, zipfp.write, TESTFN) | ||
| zipfp.write(TESTFN, strict_timestamps=False) |
There was a problem hiding this comment.
I would prefer to reopen the file for the second test, since it's hard to guess what happens when on error occurs. I prefer to limit side effets of unit tests.
This pull request adds a new option for zipping files older than 1980-01-01.
The user has to explicitly opt in by passing a keyword strict_timestamps=False to the ZipFile.write() method. This option will zip the file at the cost of setting the file timestamp to 1980-01-01 if older than zip limit.
https://bugs.python.org/issue34097