diff --git a/demo_code.py b/demo_code.py index da4cfad77..ba249b141 100644 --- a/demo_code.py +++ b/demo_code.py @@ -36,10 +36,14 @@ def limits(self): def get_number(self, min_max=[1, 10]): """Get a random number between min and max.""" - assert all([isinstance(i, int) for i in min_max]) + if not all([isinstance(i, int) for i in min_max]): + raise AssertionError return random.randint(*min_max) +import pdb +import tempfile + def main(options: dict = {}) -> str: pdb.set_trace() if "run" in options: @@ -54,9 +58,9 @@ def main(options: dict = {}) -> str: sorted(value, key=lambda k: len(k)) - f = open("/tmp/.deepsource.toml", "r") - f.write("config file.") - f.close() + with tempfile.TemporaryFile(mode="w+t") as tmp: + tmp.write("config file.") + tmp.seek(0) def moon_chooser(moon, moons=["europa", "callisto", "phobos"]): @@ -72,9 +76,9 @@ def get_users(): def tar_something(): - os.tempnam("dir1") - subprocess.Popen("/bin/chown *", shell=True) - o.system("/bin/tar xvzf *") + with os.tmpfile() as tmpf: + subprocess.Popen("/bin/chown /full/path/to/target", shell=True) + o.system("/bin/tar xvzf /full/path/to/archive.tar.gz") def bad_isinstance(initial_condition, object, other_obj, foo, bar, baz): diff --git a/hello.py b/hello.py index dc8227b07..7ab43714e 100644 --- a/hello.py +++ b/hello.py @@ -38,10 +38,14 @@ def limits(self): def get_number(self, min_max=[1, 10]): """Get a random number between min and max.""" - assert all([isinstance(i, int) for i in min_max]) + if not all([isinstance(i, int) for i in min_max]): + raise AssertionError return random.randint(*min_max) +import pdb +import tempfile + def main(options: dict = {}) -> str: pdb.set_trace() if "run" in options: @@ -56,9 +60,10 @@ def main(options: dict = {}) -> str: sorted(value, key=lambda k: len(k)) - f = open("/tmp/.deepsource.toml", "r") - f.write("config file.") - f.close() + with tempfile.TemporaryFile("w+") as tmp: + tmp.write("config file.") + tmp.seek(0) + tmp.read() def moon_chooser(moon, moons=["europa", "callisto", "phobos"]): @@ -68,15 +73,16 @@ def moon_chooser(moon, moons=["europa", "callisto", "phobos"]): return random.choice(moons) +import tempfile def get_users(): raw = '"username") AS "val" FROM "auth_user" WHERE "username"="admin" --' return User.objects.annotate(val=RawSQL(raw, [])) def tar_something(): - os.tempnam("dir1") - subprocess.Popen("/bin/chown *", shell=True) - o.system("/bin/tar xvzf *") + with tempfile.TemporaryFile() as tmp: + subprocess.Popen("/bin/chown /full/path/to/target", shell=True) + o.system("/bin/tar xvzf *") def bad_isinstance(initial_condition, object, other_obj, foo, bar, baz): @@ -118,9 +124,8 @@ def chained_comparison(): if __name__ == "__main__": args = ["--disable", "all"] - f = open("/tmp/.deepsource.toml", "r") - f.write("config file.") - f.close() + with tempfile.TemporaryFile("w+t") as tmp: + tmp.write("config file.") assert args is not None for i in range(len(args)): has_truthy = True if args[i] else False