Skip to content

Commit ac056fe

Browse files
committed
Escape slashes
1 parent bdb7b7b commit ac056fe

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/Graphics/Matplotlib/Internal.hs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,18 @@ pyIncludes backend = ["import matplotlib"
332332
,"axes = [plot.gca()]"
333333
,"ax = axes[0]"]
334334

335+
-- | These will be Python strings and slashes would cause unwanted control characters.
336+
escapeSlashes ('\\':cs) = '\\':'\\':escapeSlashes cs
337+
escapeSlashes (c:cs) = c:escapeSlashes cs
338+
escapeSlashes [] = []
339+
335340
-- | The python command that reads external data into the python data array
336341
pyReadData :: [Char] -> [[Char]]
337-
pyReadData filename = ["data = json.loads(open('" ++ filename ++ "').read())"]
342+
pyReadData filename = ["data = json.loads(open('" ++ escapeSlashes filename ++ "').read())"]
338343

339344
-- | The python command that reads an image into the img variable
340345
pyReadImage :: [Char] -> [[Char]]
341-
pyReadImage filename = ["img = mpimg.imread('" ++ filename ++ "')"]
346+
pyReadImage filename = ["img = mpimg.imread('" ++ escapeSlashes filename ++ "')"]
342347

343348
-- | Detach python so we don't block (TODO This isn't working reliably)
344349
pyDetach :: [[Char]]
@@ -353,7 +358,7 @@ pyOnscreen = ["plot.draw()"
353358

354359
-- | Python code that saves a figure
355360
pyFigure :: [Char] -> [[Char]]
356-
pyFigure output = ["plot.savefig('" ++ output ++ "')"]
361+
pyFigure output = ["plot.savefig('" ++ escapeSlashes output ++ "')"]
357362

358363
-- | Create a positional option
359364
o1 x = P $ toPythonOpt x

0 commit comments

Comments
 (0)