summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-04-24 20:28:24 +0300
committerSerhiy Storchaka <storchaka@gmail.com>2018-04-24 20:29:32 +0300
commit32bc1a74a52c0ac81fd65d3f08a4dd94489bf32e (patch)
tree170414eba64ad855a3e2075781cfbcdf365f4cff
parent729945a655b2c351ad4c91293a494c066f3bb152 (diff)
downloadsimplejson-tempfile-py25.tar.gz
Fix tests in Python 2.5.tempfile-py25
NamedTemporaryFile supports the "delete" argument only since 2.6.
-rw-r--r--simplejson/tests/test_tool.py27
1 files changed, 18 insertions, 9 deletions
diff --git a/simplejson/tests/test_tool.py b/simplejson/tests/test_tool.py
index ebb295b..914bff8 100644
--- a/simplejson/tests/test_tool.py
+++ b/simplejson/tests/test_tool.py
@@ -21,6 +21,15 @@ except ImportError:
"".encode(),
stderr).strip()
+def open_temp_file():
+ if sys.version_info >= (2, 6):
+ file = tempfile.NamedTemporaryFile(delete=False)
+ filename = file.name
+ else:
+ fd, filename = tempfile.mkstemp()
+ file = os.fdopen(fd, 'w+b')
+ return file, filename
+
class TestTool(unittest.TestCase):
data = """
@@ -71,35 +80,35 @@ class TestTool(unittest.TestCase):
self.expect.splitlines())
def test_infile_stdout(self):
- infile = tempfile.NamedTemporaryFile(delete=False)
+ infile, infile_name = open_temp_file()
try:
infile.write(self.data.encode())
infile.close()
self.assertEqual(
- self.runTool(args=[infile.name]),
+ self.runTool(args=[infile_name]),
self.expect.splitlines())
finally:
- os.unlink(infile.name)
+ os.unlink(infile_name)
def test_infile_outfile(self):
- infile = tempfile.NamedTemporaryFile(delete=False)
+ infile, infile_name = open_temp_file()
try:
infile.write(self.data.encode())
infile.close()
# outfile will get overwritten by tool, so the delete
# may not work on some platforms. Do it manually.
- outfile = tempfile.NamedTemporaryFile(delete=False)
+ outfile, outfile_name = open_temp_file()
try:
outfile.close()
self.assertEqual(
- self.runTool(args=[infile.name, outfile.name]),
+ self.runTool(args=[infile_name, outfile_name]),
[])
- with open(outfile.name, 'rb') as f:
+ with open(outfile_name, 'rb') as f:
self.assertEqual(
f.read().decode('utf8').splitlines(),
self.expect.splitlines()
)
finally:
- os.unlink(outfile.name)
+ os.unlink(outfile_name)
finally:
- os.unlink(infile.name)
+ os.unlink(infile_name)