@@ -266,9 +266,17 @@ def _win_write_to_shared_named_memory(python_code, pid):
266266 finally :
267267 CloseHandle (filemap )
268268
269+ def escape_for_gdb_and_lldb (s ):
270+ """Returns a version of given string suitable for use as gdb
271+ and lldb function argument"""
272+ if not s :
273+ return '""'
274+
275+ # wrap string in double qoutes, and escape any inner double
276+ # quotes
277+ return '"' + s .replace ('"' , '\\ "' ) + '"'
269278
270279def run_python_code_linux (pid , python_code , connect_debugger_tracing = False , show_debug_info = 0 ):
271- assert '\' ' not in python_code , 'Having a single quote messes with our command.'
272280 filedir = os .path .dirname (__file__ )
273281
274282 # Valid arguments for arch are i386, i386:x86-64, i386:x64-32, i8086,
@@ -304,14 +312,14 @@ def run_python_code_linux(pid, python_code, connect_debugger_tracing=False, show
304312# '--batch-silent',
305313 ]
306314
307- cmd .extend (["--eval-command=' set scheduler-locking off' " ]) # If on we'll deadlock.
315+ cmd .extend (["--eval-command=set scheduler-locking off" ]) # If on we'll deadlock.
308316
309- cmd .extend (["--eval-command=' set architecture %s' " % arch ])
317+ cmd .extend (["--eval-command=set architecture %s" % arch ])
310318
311319 cmd .extend ([
312- "--eval-command=' call (void*)dlopen(\" %s\" , 2)' " % target_dll ,
313- " --eval-command=' call (int)DoAttach(%s, \" %s \" , %s)'" % (
314- is_debug , python_code , show_debug_info )
320+ "--eval-command=call (void*)dlopen(\" %s\" , 2)" % target_dll ,
321+ ' --eval-command=call (int)DoAttach(%s, %s , %s)' % (
322+ is_debug , escape_for_gdb_and_lldb ( python_code ) , show_debug_info )
315323 ])
316324
317325 # print ' '.join(cmd)
@@ -323,8 +331,7 @@ def run_python_code_linux(pid, python_code, connect_debugger_tracing=False, show
323331 env .pop ('PYTHONPATH' , None )
324332 print ('Running: %s' % (' ' .join (cmd )))
325333 p = subprocess .Popen (
326- ' ' .join (cmd ),
327- shell = True ,
334+ cmd ,
328335 env = env ,
329336 stdout = subprocess .PIPE ,
330337 stderr = subprocess .PIPE ,
@@ -346,7 +353,6 @@ def find_helper_script(filedir, script_name):
346353
347354
348355def run_python_code_mac (pid , python_code , connect_debugger_tracing = False , show_debug_info = 0 ):
349- assert '\' ' not in python_code , 'Having a single quote messes with our command.'
350356 filedir = os .path .dirname (__file__ )
351357
352358 # Valid arguments for arch are i386, i386:x86-64, i386:x64-32, i8086,
@@ -363,7 +369,7 @@ def run_python_code_mac(pid, python_code, connect_debugger_tracing=False, show_d
363369 print ('Attaching with arch: %s' % (arch ,))
364370
365371 target_dll = os .path .join (filedir , 'attach_%s' % suffix )
366- target_dll = os .path .normpath (target_dll )
372+ target_dll = os .path .abspath (target_dll )
367373 if not os .path .exists (target_dll ):
368374 raise RuntimeError ('Could not find dll file to inject: %s' % target_dll )
369375
@@ -385,15 +391,15 @@ def run_python_code_mac(pid, python_code, connect_debugger_tracing=False, show_d
385391 ]
386392
387393 cmd .extend ([
388- "-o 'process attach --pid %d'" % pid ,
389- "-o 'command script import \ " %s\" '" % (lldb_prepare_file ,),
390- "-o 'load_lib_and_attach \ " %s\ " %s \" %s \" %s'" % (target_dll ,
391- is_debug , python_code , show_debug_info ),
394+ "-o" , 'process attach --pid %d' % pid ,
395+ "-o" , 'command script import "%s"' % (lldb_prepare_file ,),
396+ "-o" , 'load_lib_and_attach "%s" %s %s %s' % (target_dll ,
397+ is_debug , escape_for_gdb_and_lldb ( python_code ) , show_debug_info ),
392398 ])
393399
394400 cmd .extend ([
395- "-o 'process detach'" ,
396- "-o 'script import os; os._exit(1)'" ,
401+ "-o" , 'process detach' ,
402+ "-o" , 'script import os; os._exit(1)' ,
397403 ])
398404
399405 # print ' '.join(cmd)
@@ -405,8 +411,7 @@ def run_python_code_mac(pid, python_code, connect_debugger_tracing=False, show_d
405411 env .pop ('PYTHONPATH' , None )
406412 print ('Running: %s' % (' ' .join (cmd )))
407413 p = subprocess .Popen (
408- ' ' .join (cmd ),
409- shell = True ,
414+ cmd ,
410415 env = env ,
411416 stdout = subprocess .PIPE ,
412417 stderr = subprocess .PIPE ,
0 commit comments