Replace uses of tempfile.mktemp with tempfile.NamedTemporaryFile

This issue was flagged as a security issue in the static code analyzer
This commit is contained in:
Ogi Moore 2021-05-28 21:15:11 -07:00
parent ceeacf4e5f
commit d859cec95a
3 changed files with 15 additions and 28 deletions

View File

@ -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)

View File

@ -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

View File

@ -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)