Skip to content

Commit 766ae97

Browse files
committed
Fix some errors when using python3 tempfile
1 parent eba6101 commit 766ae97

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

tests/src/python/qgis_local_server.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
from utilities import (
2727
unitTestDataPath,
2828
getExecutablePath,
29-
openInBrowserTab
29+
openInBrowserTab,
30+
getTempfilePath
3031
)
3132

3233
# allow import error to be raised if qgis is not on sys.path
@@ -422,10 +423,10 @@ def get_capabilities(self, params, browser=False):
422423
res = urllib.urlopen(url)
423424
xml = res.read()
424425
if browser:
425-
tmp = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
426-
tmp.write(xml)
427-
url = tmp.name
428-
tmp.close()
426+
tmp_name = getTempfilePath('html')
427+
with open(tmp_name, 'wt') as temp_html:
428+
temp_html.write(xml)
429+
url = tmp_name
429430
openInBrowserTab(url)
430431
return False, ''
431432

@@ -513,10 +514,9 @@ def get_map(self, params, browser=False):
513514
and tmp_png.info().getmaintype() == 'image'
514515
and tmp_png.info().getheader('Content-Type') == 'image/png'):
515516

516-
tmp = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
517-
filepath = tmp.name
518-
tmp.write(tmp_png.read())
519-
tmp.close()
517+
filepath = getTempfilePath('png')
518+
with open(filepath, 'wb') as temp_image:
519+
temp_image.write(tmp_png.read())
520520
success = True
521521
else:
522522
raise ServerProcessError(

tests/src/python/test_qgis_local_server.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
unittest
4444
)
4545

46-
from utilities import openInBrowserTab
46+
from utilities import openInBrowserTab, getTempfilePath
4747

4848
start_app()
4949
MAPSERV = getLocalServer()
@@ -171,10 +171,10 @@ def run_suite(module, tests):
171171
report += '\n<h3>{0}</h3>\n{1}'.format(k, v)
172172
report += '</body></html>'
173173

174-
tmp = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
175-
tmp.write(report)
176-
tmp.close()
177-
openInBrowserTab('file://' + tmp.name)
174+
tmp_name = getTempfilePath("html")
175+
with open(tmp_name, 'wb') as temp_file:
176+
temp_file.write(report)
177+
openInBrowserTab('file://' + tmp_name)
178178

179179
return res
180180

tests/src/python/test_qgspallabeling_base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,10 @@ def runSuite(module, tests):
469469
report += '\n<h3>{0}</h3>\n{1}'.format(k, v)
470470
report += '</body></html>'
471471

472-
tmp = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
473-
tmp.write(report)
474-
tmp.close()
475-
openInBrowserTab('file://' + tmp.name)
472+
tmp_name = getTempfilePath('html')
473+
with open(tmp_name, 'wt') as report_file:
474+
report_file.write(report)
475+
openInBrowserTab('file://' + tmp_name)
476476

477477
return res
478478

0 commit comments

Comments
 (0)