Skip to content

Commit 2de6ece

Browse files
amandaonealdwalton76
authored andcommitted
ev3dev#1026: Sound.speak does not work with double quotes (#443)
* ev3dev#1026: Sound.speak does not work with double quotes * Update to use shlex * Remove shell=true
1 parent 8e1bd88 commit 2de6ece

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

ev3dev2/sound.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import os
3232
import re
3333
import shlex
34-
from subprocess import check_output, Popen
34+
from subprocess import check_output, Popen, PIPE
3535

3636

3737
def _make_scales(notes):
@@ -294,20 +294,22 @@ def speak(self, text, espeak_opts='-a 200 -s 130', volume=100, play_type=PLAY_WA
294294
self.set_volume(volume)
295295

296296
with open(os.devnull, 'w') as n:
297-
cmd_line = '/usr/bin/espeak --stdout {0} "{1}" | /usr/bin/aplay -q'.format(
298-
espeak_opts, text
299-
)
297+
cmd_line = ['/usr/bin/espeak', '--stdout'] + shlex.split(espeak_opts) + [shlex.quote(text)]
298+
aplay_cmd_line = shlex.split('/usr/bin/aplay -q')
300299

301300
if play_type == Sound.PLAY_WAIT_FOR_COMPLETE:
302-
play = Popen(cmd_line, stdout=n, shell=True)
301+
espeak = Popen(cmd_line, stdout=PIPE)
302+
play = Popen(aplay_cmd_line, stdin=espeak.stdout, stdout=n)
303303
play.wait()
304304

305305
elif play_type == Sound.PLAY_NO_WAIT_FOR_COMPLETE:
306-
return Popen(cmd_line, stdout=n, shell=True)
306+
espeak = Popen(cmd_line, stdout=PIPE)
307+
return Popen(aplay_cmd_line, stdin=espeak.stdout, stdout=n)
307308

308309
elif play_type == Sound.PLAY_LOOP:
309310
while True:
310-
play = Popen(cmd_line, stdout=n, shell=True)
311+
espeak = Popen(cmd_line, stdout=PIPE)
312+
play = Popen(aplay_cmd_line, stdin=espeak.stdout, stdout=n)
311313
play.wait()
312314

313315
def _get_channel(self):

0 commit comments

Comments
 (0)