Skip to content

ZipFile.write fails with bad modification time #45243

Open

Description

@gradha
mannequin
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']

    Activity

    1. gradha commented on Jul 25, 2007

      gradhamannequin
      MannequinAuthor

      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.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Jul 25, 2007
    3. gradha commented on Jul 25, 2007

      gradhamannequin
      MannequinAuthor

      File Added: ignore_negative_modification_time.txt

    4. gradha commented on Aug 26, 2007

      gradhamannequin
      MannequinAuthor

      Any progress report on this issue, please?

    5. alanmcintyre commented on Dec 23, 2007

      alanmcintyremannequin
      Mannequin

      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.

    6. serhiy-storchaka commented on Apr 28, 2012

      @serhiy-storchaka
      Member

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

    7. changed the title [-]ZipFile.write fails with bad modification time[/-] [+]Loveubylulu@gmail.com[/+] on Jul 28, 2019
    8. changed the title [-]ZipFile.write fails with bad modification time[/-] [+]Loveubylulu@gmail.com[/+] on Jul 28, 2019
    9. tirkarthi commented on Jul 28, 2019

      @tirkarthi
      Member

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

    10. 1 remaining item

    11. changed the title [-]Loveubylulu@gmail.com[/-] [+]ZipFile.write fails with bad modification time[/+] on Jul 28, 2019
    12. transferred this issue fromon Apr 10, 2022
    13. Akasurde commented on Jul 3, 2024

      @Akasurde
      Contributor

      @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
    14. msullivan commented on Oct 13, 2026

      @msullivan
      Contributor

      I checked on windows, and on 3.14 and earlier, I got OSError, but on 3.15 I get "ZIP does not support timestamps before 1980" and passing strict_timestamps=False will fix that.

      Fix was presumably #143463, which fixes negative timestamps on windows.

      I think this can be closed.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.10 (EOL)end of life3.11only security fixes3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions