summaryrefslogtreecommitdiff
path: root/tests/functional/c/consider/consider_using_with.py
diff options
context:
space:
mode:
authorDudeNr33 <3929834+DudeNr33@users.noreply.github.com>2021-04-23 20:31:21 +0200
committerGitHub <noreply@github.com>2021-04-23 20:31:21 +0200
commit922f38969c326826344e2d25af499cf8c5f80d8c (patch)
treedddd7f1ac39b138cd64c9538783915c6d3e83f11 /tests/functional/c/consider/consider_using_with.py
parent81c7c50ad0273e2027f636edbddba9d410c8319a (diff)
downloadpylint-git-922f38969c326826344e2d25af499cf8c5f80d8c.tar.gz
Enhancement #3413 ``consider-using-with`` (#4372)
* Implement consider-using-with check * Fix or disable consider-using-with in codebase * Fix ticket number in ChangeLog * Move functional test for ``open()`` into separate testfile and exclude this test from running with PyPy Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Diffstat (limited to 'tests/functional/c/consider/consider_using_with.py')
-rw-r--r--tests/functional/c/consider/consider_using_with.py138
1 files changed, 138 insertions, 0 deletions
diff --git a/tests/functional/c/consider/consider_using_with.py b/tests/functional/c/consider/consider_using_with.py
new file mode 100644
index 000000000..234c6093d
--- /dev/null
+++ b/tests/functional/c/consider/consider_using_with.py
@@ -0,0 +1,138 @@
+# pylint: disable=missing-function-docstring, missing-module-docstring, invalid-name, import-outside-toplevel
+import codecs
+import multiprocessing
+import subprocess
+import tarfile
+import tempfile
+import threading
+import urllib
+import zipfile
+from concurrent import futures
+
+
+def test_codecs_open():
+ fh = codecs.open("test.txt", "utf8") # [consider-using-with]
+ fh.close()
+
+
+def test_urlopen():
+ _ = urllib.request.urlopen("http://www.python.org") # [consider-using-with]
+
+
+def test_temporary_file():
+ _ = tempfile.TemporaryFile("r") # [consider-using-with]
+
+
+def test_named_temporary_file():
+ _ = tempfile.NamedTemporaryFile("r") # [consider-using-with]
+
+
+def test_spooled_temporary_file():
+ _ = tempfile.SpooledTemporaryFile("r") # [consider-using-with]
+
+
+def test_temporary_directory():
+ _ = tempfile.TemporaryDirectory() # [consider-using-with]
+
+
+def test_zipfile():
+ myzip = zipfile.ZipFile("spam.zip", "w") # [consider-using-with]
+ _ = myzip.open("eggs.txt") # [consider-using-with]
+
+
+def test_pyzipfile():
+ myzip = zipfile.PyZipFile("spam.zip", "w") # [consider-using-with]
+
+ with zipfile.PyZipFile("spam.zip", "w"): # must not trigger
+ pass
+
+ _ = myzip.open("eggs.txt") # [consider-using-with]
+
+ with myzip.open("eggs.txt"): # must not trigger
+ pass
+
+
+def test_tarfile():
+ tf = tarfile.open("/tmp/test.tar", "w") # [consider-using-with]
+ tf.close()
+
+ with tarfile.open("/tmp/test.tar", "w"): # must not trigger
+ pass
+
+ tf = tarfile.TarFile("/tmp/test2.tar", "w") # [consider-using-with]
+ tf.close()
+
+ with tarfile.TarFile("/tmp/test2.tar", "w"): # must not trigger
+ pass
+
+
+def test_lock_acquisition():
+ lock = threading.Lock()
+ lock.acquire() # [consider-using-with]
+ lock.release()
+
+ with lock: # must not trigger
+ pass
+
+ rlock = threading.RLock()
+ rlock.acquire() # [consider-using-with]
+ rlock.release()
+
+ with rlock: # must not trigger
+ pass
+
+ # Not working currently
+ # condition = threading.Condition()
+ # condition.acquire()
+ # condition.release()
+
+ # with condition: # must not trigger
+ # pass
+
+ sema = threading.Semaphore()
+ sema.acquire() # [consider-using-with]
+ sema.release()
+
+ with sema: # must not trigger
+ pass
+
+ bounded_sema = threading.BoundedSemaphore()
+ bounded_sema.acquire() # [consider-using-with]
+ bounded_sema.release()
+
+ with bounded_sema: # must not trigger
+ pass
+
+
+def test_multiprocessing():
+ # the different Locks provided by multiprocessing would be candidates
+ # for consider-using-with as well, but they lead to InferenceErrors.
+ _ = multiprocessing.Pool() # [consider-using-with]
+ with multiprocessing.Pool():
+ pass
+
+ manager = multiprocessing.managers.BaseManager()
+ manager.start() # [consider-using-with]
+ with multiprocessing.managers.BaseManager():
+ pass
+
+ manager = multiprocessing.managers.SyncManager()
+ manager.start() # [consider-using-with]
+ with multiprocessing.managers.SyncManager():
+ pass
+
+
+def test_futures():
+ _ = futures.ThreadPoolExecutor() # [consider-using-with]
+ with futures.ThreadPoolExecutor():
+ pass
+
+ _ = futures.ProcessPoolExecutor() # [consider-using-with]
+ with futures.ProcessPoolExecutor():
+ pass
+
+
+def test_popen():
+ _ = subprocess.Popen("sh") # [consider-using-with]
+ with subprocess.Popen("sh"):
+ pass