Re: [Python-Dev] [Python-checkins] bpo-34239: Convert test_bz2 to use tempfile (#8485)

2018年7月28日 15:57:29 -0700

On Thu, Jul 26, 2018 at 2:05 PM, Tim Golden <[email protected]> wrote:
> https://github.com/python/cpython/commit/6a62e1d365934de82ff7c634981b3fbf218b4d5f
> commit: 6a62e1d365934de82ff7c634981b3fbf218b4d5f
> branch: master
> author: Tim Golden <[email protected]>
> committer: GitHub <[email protected]>
> date: 2018年07月26日T22:05:00+01:00
> summary:
>
> bpo-34239: Convert test_bz2 to use tempfile (#8485)
>
> * bpo-34239: Convert test_bz2 to use tempfile
>
> test_bz2 currently uses the test.support.TESTFN functionality which creates a 
> temporary file local to the test directory named around the pid.
>
> This can give rise to race conditions where tests are competing with each 
> other to delete and recreate the file.
Per the other thread--
https://mail.python.org/pipermail/python-dev/2018-July/154762.html
this seems like a wrong statement of the problem as tests are properly
cleaning up after themselves. The leading hypothesis is that unrelated
Windows processes are delaying the deletion (e.g. virus scanners).
--Chris
>
> This change converts the tests to use tempfile.mkstemp which gives a 
> different file every time from the system's temp area
>
> files:
> M Lib/test/test_bz2.py
>
> diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py
> index 003497f28b16..e62729a5a2f8 100644
> --- a/Lib/test/test_bz2.py
> +++ b/Lib/test/test_bz2.py
> @@ -6,6 +6,7 @@
> import os
> import pickle
> import glob
> +import tempfile
> import pathlib
> import random
> import shutil
> @@ -76,11 +77,14 @@ class BaseTest(unittest.TestCase):
> BIG_DATA = bz2.compress(BIG_TEXT, compresslevel=1)
>
> def setUp(self):
> - self.filename = support.TESTFN
> + fd, self.filename = tempfile.mkstemp()
> + os.close(fd)
>
> def tearDown(self):
> - if os.path.isfile(self.filename):
> + try:
> os.unlink(self.filename)
> + except FileNotFoundError:
> + pass
>
>
> class BZ2FileTest(BaseTest):
>
> _______________________________________________
> Python-checkins mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/python-checkins
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to