diff --git a/pyqtgraph/configfile.py b/pyqtgraph/configfile.py index a4ad9191..674c1620 100644 --- a/pyqtgraph/configfile.py +++ b/pyqtgraph/configfile.py @@ -12,6 +12,7 @@ as it can be converted to/from a string using repr and eval. import re, os, sys, datetime import numpy from collections import OrderedDict +import tempfile from . import units from .python2_3 import asUnicode, basestring from .Qt import QtCore @@ -187,11 +188,8 @@ def measureIndent(s): while n < len(s) and s[n] == ' ': n += 1 return n - - - + if __name__ == '__main__': - import tempfile cf = """ key: 'value' key2: ##comment @@ -201,16 +199,13 @@ key2: ##comment key22: [1,2,3] key23: 234 #comment """ - fn = tempfile.mktemp() - with open(fn, 'w') as tf: - tf.write(cf) - print("=== Test:===") - num = 1 - for line in cf.split('\n'): - print("%02d %s" % (num, line)) - num += 1 - print(cf) - print("============") - data = readConfigFile(fn) + with tempfile.NamedTemporaryFile(encoding="utf-8") as tf: + tf.write(cf.encode("utf-8")) + print("=== Test:===") + for num, line in enumerate(cf.split('\n'), start=1): + print("%02d %s" % (num, line)) + print(cf) + print("============") + data = readConfigFile(tf.name) print(data) - os.remove(fn) + diff --git a/pyqtgraph/metaarray/MetaArray.py b/pyqtgraph/metaarray/MetaArray.py index b07f66ab..92930cae 100644 --- a/pyqtgraph/metaarray/MetaArray.py +++ b/pyqtgraph/metaarray/MetaArray.py @@ -1330,8 +1330,6 @@ if __name__ == '__main__': #### File I/O tests print("\n================ File I/O Tests ===================\n") - import tempfile - tf = tempfile.mktemp() tf = 'test.ma' # write whole array diff --git a/tests/exporters/test_csv.py b/tests/exporters/test_csv.py index 0772d34e..bcb7fcaf 100644 --- a/tests/exporters/test_csv.py +++ b/tests/exporters/test_csv.py @@ -4,7 +4,6 @@ CSV export test from __future__ import division, print_function, absolute_import import pyqtgraph as pg import csv -import os import tempfile app = pg.mkQApp() @@ -15,9 +14,6 @@ def approxeq(a, b): def test_CSVExporter(): - tempfilename = tempfile.NamedTemporaryFile(suffix='.csv').name - print("using %s as a temporary file" % tempfilename) - plt = pg.plot() y1 = [1,3,2,3,1,6,9,8,4,2] plt.plot(y=y1, name='myPlot') @@ -31,11 +27,10 @@ def test_CSVExporter(): plt.plot(x=x3, y=y3, stepMode="center") ex = pg.exporters.CSVExporter(plt.plotItem) - ex.export(fileName=tempfilename) - - with open(tempfilename, 'r') as csv_file: - r = csv.reader(csv_file) - lines = [line for line in r] + with tempfile.NamedTemporaryFile(mode="w+t", suffix='.csv', encoding="utf-8", delete=False) as tf: + print("using %s as a temporary file" % tf.name) + ex.export(fileName=tf.name) + lines = [line for line in csv.reader(tf)] header = lines.pop(0) assert header == ['myPlot_x', 'myPlot_y', 'x0001', 'y0001', 'x0002', 'y0002'] @@ -49,4 +44,3 @@ def test_CSVExporter(): assert (i >= len(x3) and vals[4] == '') or approxeq(float(vals[4]), x3[i]) assert (i >= len(y3) and vals[5] == '') or approxeq(float(vals[5]), y3[i]) - os.unlink(tempfilename)