8

I'm interested to escape a string in Python3.x, such as:

SOME_MACRO(a, b)

into...

SOME_MACRO\(a,\ b\)

... so that it can be passed to a program (not gcc in this case) as a define,

eg,

some_program -DSOME_MACRO\(a,\ b\)="some expression"

I would expect shlex would have this functionality but I didn't find how to do this and checked many similar questions already.

I don't mind to write some simple function to do this, but this seems the kind of thing Python would include.

Note: the program I'm passing the argument to wont accept:

-D"SOME_MACRO(a, b)"="some expression"

... it expects the first character to be an identifier.

ideasman42
  • 42,413
  • 44
  • 197
  • 320

2 Answers2

13

In Python 3.3 you can use shlex.quote to return a shell-escaped version of a string. It is the successor of pipes.quote, which was deprecated since Python 1.6. Note that the documentation recommends this for cases where you cannot use a list, as suggested in another answer. Also according to the documentation, the quoting is compatible with UNIX shells. I can't guarantee that it will work for your case, but a quick test with rm, using pipes because I don't have Python 3.3:

$ touch \(a\ b\)
$ ls
(a b)

>>> import subprocess, pipes
>>> filename = pipes.quote("(a b)")
>>> command = 'rm {}'.format(filename)
>>> subprocess.Popen(command, shell=True)

$ ls
$
Paulo Almeida
  • 7,803
  • 28
  • 36
2

Doing it correctly means not having to worry about this. The shell has to worry about spaces and quotes and parens; Python does not.

proc = subprocess.Popen([..., "-DSOME_MACRO(a, b)=some expression", ...], ...)
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358