Skip to content

Use the 'with' statement in conjunction with 'open' throughout test modules #52192

Closed

Description

@midnightdf
BPO 7944
Nosy @rhettinger, @amauryfa, @giampaolo, @ezio-melotti, @merwok, @voidspace
Dependencies
  • bpo-7232: Support of 'with' statement fo TarFile class
  • Files
  • test_old_mailbox.py: Modified version of test_old_mailbox.py
  • test_old_mailbox.patch
  • issue7944_gzip.diff
  • issue7944_marshal.diff
  • issue7944_zipfile64.diff
  • issue7944_tarfile.diff: This depends on Support of 'with' statement fo TarFile class #51481
  • issue7944_tempfile.diff: Updated with review comments from RDM
  • test_os.patch
  • issue7944_tarfile_2_7.diff
  • 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 = 'https://github.com/ezio-melotti'
    closed_at = <Date 2016-01-13.20:23:00.745>
    created_at = <Date 2010-02-16.17:43:50.012>
    labels = ['easy', 'type-feature', 'tests']
    title = "Use the 'with' statement in conjunction with 'open' throughout test modules"
    updated_at = <Date 2016-01-13.20:23:00.744>
    user = 'https://bugs.python.org/midnightdf'

    bugs.python.org fields:

    activity = <Date 2016-01-13.20:23:00.744>
    actor = 'ezio.melotti'
    assignee = 'ezio.melotti'
    closed = True
    closed_date = <Date 2016-01-13.20:23:00.745>
    closer = 'ezio.melotti'
    components = ['Tests']
    creation = <Date 2010-02-16.17:43:50.012>
    creator = 'midnightdf'
    dependencies = ['7232']
    files = ['16235', '16237', '16286', '16287', '16290', '16311', '16312', '19218', '36212']
    hgrepos = []
    issue_num = 7944
    keywords = ['patch', 'easy']
    message_count = 22.0
    messages = ['99425', '99434', '99617', '99618', '99619', '99699', '99700', '99703', '99780', '118498', '118500', '118553', '118554', '118556', '118557', '118610', '185771', '185817', '224458', '224564', '258166', '258167']
    nosy_count = 10.0
    nosy_names = ['rhettinger', 'amaury.forgeotdarc', 'giampaolo.rodola', 'ezio.melotti', 'eric.araujo', 'michael.foord', 'midnightdf', 'BreamoreBoy', 'python-dev', 'evans.mungai']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue7944'
    versions = ['Python 2.7']

    Activity

    1. midnightdf commented on Feb 16, 2010

      midnightdfmannequin
      MannequinAuthor

      Sprinkled throughout CPython's test modules are snippets of code such as the following taken from 2.7A3's test_old_mailbox.py (line 141):
      box = mailbox.UnixMailbox(open(self._path, 'r'))

      The key thing to observe here is the file being opened yet never has it's 'close' method explicitly called. While this is fine for CPython's rather predictable garbage collections, it's quite possible that in alternate implementations of Python this file object won't be destroyed until well after line 141. This can result in seemingly random failures of CPython's tests under alternate implementations of Python.

      The solution to this problem would be to consistently use the 'with' statement (or alternatively close all open file objects) throughout all CPython test modules.

      I've gone ahead and submitted an updated (2.7A3) version of test_old_mailbox.py which addresses this. We can find other places where this is going on as well, but for the most part the tests already seem to be doing the right thing.

    2. amauryfa commented on Feb 16, 2010

      @amauryfa
      Contributor

      Please use patches to show where the code changed.
      (I generated one; it looks good to me)

      I would like this kind of patches applied to CPython.
      Looking at the Pypy test suite, the following test files seem easy to update:
      test_bz2
      test_dumbdbm
      test_mailbox
      test_marshal
      test_mmap
      test_pkgimport
      test_tarfile
      test_tempfile
      test_traceback

    3. briancurtin commented on Feb 20, 2010

      @briancurtin
      Member

      Does anyone prefer if this should be done on a patch-per-module basis, or just throw together one huge change?

      Also, I've found a few places where context managers aren't supported where they probably should be, so I'll spin off separate issues for those.

    4. voidspace commented on Feb 20, 2010

      @voidspace
      Contributor

      There are several standard library modules that deliberately maintain compatibility with earlier versions of Python. The 'with' statement shouldn't be used for tests of any modules that still need compatibility with Python 2.4.

    5. ezio-melotti commented on Feb 20, 2010

      @ezio-melotti
      Member

      Some are listed in the PEP-291 (http://www.python.org/dev/peps/pep-0291/) but I don't know if that list is really updated.

    6. briancurtin commented on Feb 22, 2010

      @briancurtin
      Member

      I'm going to go ahead with the patch-per-module approach, but anyone feel free to stop me from doing that.

      Here's a patch for test_gzip on trunk.

    7. briancurtin commented on Feb 22, 2010

      @briancurtin
      Member

      Here's a patch for test_marshal on trunk.

    8. briancurtin commented on Feb 22, 2010

      @briancurtin
      Member

      Here's a patch for test_tempfile on trunk. The rest will just be silently added since this is already annoying.

    9. briancurtin commented on Feb 22, 2010

      @briancurtin
      Member

      Patch for test_tarfile. However, bpo-7232 needs to go in first.

    10. briancurtin commented on Oct 13, 2010

      @briancurtin
      Member

      Fixed test_gzip in r85400.

    11. briancurtin commented on Oct 13, 2010

      @briancurtin
      Member

      Fixed test_mailbox in r85401.
      Fixed test_marshal in r85402.
      Fixed test_bz2 in r85403.

    12. giampaolo commented on Oct 13, 2010

      @giampaolo
      Contributor

      Here is a patch for test_os.py.

    13. rhettinger commented on Oct 13, 2010

      @rhettinger
      Contributor

      I'm not sure that this is a worthwhile exercise unless it can be shown for certain that alternate implementations need this. Otherwise, it just changes tests in a way that isn't backwards compatible, makes it more difficult to apply patches cleanly, and risks damaging an otherwise good test.

    14. midnightdf commented on Oct 13, 2010

      midnightdfmannequin
      MannequinAuthor

      I guarantee you that IronPython needs this. I was a tester on IronPython for four years, and saw hundreds of random failures due to this specific issue.

      My best,

      Dave

    15. rhettinger commented on Oct 13, 2010

      @rhettinger
      Contributor

      If you're sure that is still the case, this can proceed.

    16. briancurtin commented on Oct 14, 2010

      @briancurtin
      Member

      Backported test_gzip to to release27-maint in r85446.
      Backported test_mailbox to to release27-maint in r85447.
      Backported test_bz2 to to release27-maint in r85448.

      Fixed test_old_mailbox, the original problem file, in release27-maint in r85449.

    17. BreamoreBoy commented on Apr 1, 2013

      BreamoreBoymannequin
      Mannequin

      bpo-7232 has been closed so test_tarfile can presumably now be committed, also test_os.

    18. ezio-melotti commented on Apr 2, 2013

      @ezio-melotti
      Member

      FWIW test_tarfile seems to use the "with" statement on 3.3+ already.
      On 2.7 is not always used, but I'm not sure it's worth backporting/fixing the remaining occurrences there.
      Most of the fixes in the test_os patch have already being applied too.
      Either someone updates the current patch and/or proposes new ones, or this can probably be closed.

    19. BreamoreBoy commented on Jul 31, 2014

      BreamoreBoymannequin
      Mannequin

      Following on from msg185817 I'd suggest this is closed.

    20. evansmungai commented on Aug 2, 2014

      evansmungaimannequin
      Mannequin

      Backport for test_tarfile.py

    21. python-dev commented on Jan 13, 2016

      python-devmannequin
      Mannequin

      New changeset e3763d98c46e by Ezio Melotti in branch '2.7':
      bpo-7944: close files explicitly in test_tarfile (backport d560eece0857).
      https://hg.python.org/cpython/rev/e3763d98c46e

    22. ezio-melotti commented on Jan 13, 2016

      @ezio-melotti
      Member

      Thanks for the patch, however I got several errors while trying to apply it, so I ended up backporting d560eece0857 instead.
      This can be closed now.

    23. self-assigned this
      on Jan 13, 2016
    24. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    easytestsTests in the Lib/test dirtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions