@@ -205,8 +205,20 @@ def _run_query(self, query, custom_auth=None, env_vars=None):
205205 :raises FileNotFoundError: If the StackQL binary isn't found.
206206 :raises Exception: For any other exceptions during the execution, providing a generic error message.
207207 """
208+
208209 local_params = self .params .copy ()
209- local_params .insert (1 , f'"{ query } "' )
210+ script_path = None
211+
212+ if self .platform .startswith ("Windows" ):
213+ # Escape double quotes and wrap in double quotes for Windows
214+ escaped_query = query .replace ('"' , '\\ "' ) # Escape double quotes properly
215+ safe_query = f'"{ escaped_query } "'
216+ else :
217+ # Use shlex.quote for Unix-like systems
218+ import shlex
219+ safe_query = shlex .quote (query )
220+
221+ local_params .insert (1 , safe_query )
210222 script_path = None
211223
212224 # Handle custom authentication if provided
@@ -240,19 +252,32 @@ def _run_query(self, query, custom_auth=None, env_vars=None):
240252 full_command = " " .join ([self .bin_path ] + local_params )
241253
242254 try :
243- with subprocess .Popen (full_command , shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE ) as iqlPopen :
244- stdout , stderr = iqlPopen .communicate ()
245-
246- if self .debug :
247- self ._debug_log (f"query: { query } " )
248- self ._debug_log (f"stdout: { stdout } " )
249- self ._debug_log (f"stderr: { stderr } " )
250-
251- # Process stdout and stderr
252- if stderr :
253- output ["error" ] = stderr .decode ('utf-8' ) if isinstance (stderr , bytes ) else str (stderr )
254- if stdout :
255- output ["data" ] = stdout .decode ('utf-8' ) if isinstance (stdout , bytes ) else str (stdout )
255+
256+ full_command = full_command .replace ("\n " , " " )
257+
258+ result = subprocess .run (
259+ full_command ,
260+ shell = True ,
261+ text = True ,
262+ capture_output = True
263+ )
264+
265+ stdout = result .stdout
266+ stderr = result .stderr
267+ returncode = result .returncode
268+
269+ if self .debug :
270+ self ._debug_log (f"fullcommand: { full_command } " )
271+ self ._debug_log (f"returncode: { returncode } " )
272+ self ._debug_log (f"stdout: { stdout } " )
273+ self ._debug_log (f"stderr: { stderr } " )
274+
275+ # Process stdout and stderr
276+ if stderr :
277+ output ["error" ] = stderr .decode ('utf-8' ) if isinstance (stderr , bytes ) else str (stderr )
278+ if stdout :
279+ output ["data" ] = stdout .decode ('utf-8' ) if isinstance (stdout , bytes ) else str (stdout )
280+
256281
257282 except FileNotFoundError :
258283 output ["exception" ] = f"ERROR: { self .bin_path } not found"
0 commit comments