From 83c8c3b0e8c699cd52e7d539c49dabbbf7bf384c Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Wed, 12 Jan 2022 20:59:03 -0500 Subject: [PATCH] Manually handle prefix -F in psconvert So that fig.savefig won't insert `\040` characters when saving filenames with spaces. Resolves problem mentioned in https://github.com/GenericMappingTools/pygmt/pull/1487/files#r703116544 --- pygmt/figure.py | 7 ++++++- pygmt/tests/test_figure.py | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index 621f747127d..5b7a2a3cf3b 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -192,8 +192,13 @@ def psconvert(self, **kwargs): # Default cropping the figure to True if "A" not in kwargs: kwargs["A"] = "" + # Manually handle prefix -F argument so spaces aren't converted to \040 + # by build_arg_string function. For more information, see + # https://github.com/GenericMappingTools/pygmt/pull/1487 + prefix = kwargs.pop("F") + with Session() as lib: - lib.call_module("psconvert", build_arg_string(kwargs)) + lib.call_module("psconvert", f'-F"{prefix}" {build_arg_string(kwargs)}') def savefig( self, fname, transparent=False, crop=True, anti_alias=True, show=False, **kwargs diff --git a/pygmt/tests/test_figure.py b/pygmt/tests/test_figure.py index 3c77b146faa..73b907dfee5 100644 --- a/pygmt/tests/test_figure.py +++ b/pygmt/tests/test_figure.py @@ -138,7 +138,7 @@ def test_figure_savefig_filename_with_spaces(): fig = Figure() fig.basemap(region=[0, 1, 0, 1], projection="X1c/1c", frame=True) with GMTTempFile(prefix="pygmt-filename with spaces", suffix=".png") as imgfile: - fig.savefig(imgfile.name) + fig.savefig(fname=imgfile.name) assert r"\040" not in os.path.abspath(imgfile.name) assert os.path.exists(imgfile.name)