cpython icon indicating copy to clipboard operation
cpython copied to clipboard

ZipFile.write fails with bad modification time

Open f3de81e4-f48e-4151-85fb-ec788fcb77c4 opened this issue 18 years ago • 7 comments

BPO 1760357
Nosy @warsaw, @bitdancer, @briancurtin, @serhiy-storchaka, @tirkarthi
Files
  • debug_session.txt: Debug session showing negative mtime
  • ignore_negative_modification_time.txt: Most likely incorrect fix to avoid this problem
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2007-07-25.14:54:52.000>
    labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
    title = 'ZipFile.write fails with bad modification time'
    updated_at = <Date 2021-10-20.12:53:55.412>
    user = 'https://bugs.python.org/gradha'
    

    bugs.python.org fields:

    activity = <Date 2021-10-20.12:53:55.412>
    actor = 'christian.heimes'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2007-07-25.14:54:52.000>
    creator = 'gradha'
    dependencies = []
    files = ['8133', '8134']
    hgrepos = []
    issue_num = 1760357
    keywords = ['patch']
    message_count = 6.0
    messages = ['52944', '52945', '55307', '58978', '159533', '348574']
    nosy_count = 8.0
    nosy_names = ['barry', 'gradha', 'alanmcintyre', 'r.david.murray', 'brian.curtin', 'serhiy.storchaka', 'xtreak', 'Linsey Alvarez']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'test needed'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue1760357'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']
    

    Running on Windows XP against a networked file server with Windows 2003 I have a file which due to some obscure reason it has a negative modification time as returned by Python os functions. When I look at the file in the explorer the modification time column is blank, and when I view the properties of the file there the modification time is set to the 25th of April of 1906 at 2:03:44.

    If I try to zip this file the zip creation fails with a Value error when the file's st_mtime value is passed to time.localtime().

    I'm attaching a debug session log along with a possible patch to avoid such problems, though maybe the function that returns st_mtime should never return negative values because other library functions expect zero or positive values too.

    In the log you will see that it is a custom rastertech\zipfile.py file that is failing. This is just the SVN development version of zipfile along with some modifications since changes like 1739648 seem to be very low priority. The patch is generated against a clean SVN version.

    File Added: ignore_negative_modification_time.txt

    Any progress report on this issue, please?

    At the moment I don't have a Windows machine available, but on my Mac, time.localtime doesn't seem to mind interpreting negative input values. So I doubt that forcing timestamps to be non-negative is the way to fix this.

    I poked around a bit in the current 2.6 source, and it seems to me that in this case the Windows XP localtime function doesn't like a timestamp value that was generated by the same platform's stat function. It seems like raising a ValueError is the correct thing for the time module to do, since it relies on the platform to do the conversions; what should zipfile.writestr do? I don't like the idea of silently using some dummy timestamp if the conversion of os.stat results fails, and I can't think of an alternative at the moment.

    An alternative is to use the current time, as for stdin.

    serhiy-storchaka avatar Apr 28 '12 19:04 serhiy-storchaka

    Linsey Alvarez, I am not sure how it's related to email. So I am removing the email tag and changing the subject back.

    tirkarthi avatar Jul 28 '19 06:07 tirkarthi

    @serhiy-storchaka I think this is already addressed in the current versions of Python.

    >>> os.stat("hello.txt")
    os.stat_result(st_mode=33188, st_ino=31001686, st_dev=16777233, st_nlink=1, st_uid=501, st_gid=0, st_size=0, st_atime=-2019621600, st_mtime=-2019621600, st_ctime=1720029468)
    >>> os.stat("hello.txt").st_mtime
    -2019621600.0
    >>> zi.from_file("hello.txt")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'ZipFile' object has no attribute 'from_file'
    >>> info = zipfile.ZipInfo.from_file("hello.txt")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/zipfile.py", line 517, in from_file
        zinfo = cls(arcname, date_time)
      File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/zipfile.py", line 361, in __init__
        raise ValueError('ZIP does not support timestamps before 1980')
    ValueError: ZIP does not support timestamps before 1980
    >>> zi.write("hello.txt")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/zipfile.py", line 1727, in write
        zinfo = ZipInfo.from_file(filename, arcname,
      File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/zipfile.py", line 517, in from_file
        zinfo = cls(arcname, date_time)
      File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/zipfile.py", line 361, in __init__
        raise ValueError('ZIP does not support timestamps before 1980')
    ValueError: ZIP does not support timestamps before 1980
    

    Akasurde avatar Jul 03 '24 18:07 Akasurde