From 5bba3f93b1c55a439e49c89ce447bcda24656711 Mon Sep 17 00:00:00 2001 From: Taras Kopets Date: Mon, 15 May 2017 14:49:07 +0300 Subject: [PATCH] improved process, stdout, stderr handling --- .bumpversion.cfg | 2 +- SQLTools.py | 2 +- SQLToolsAPI/Command.py | 21 ++++++++++++++++++++- messages.json | 3 ++- messages/v0.9.1.md | 11 +++++++++++ 5 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 messages/v0.9.1.md diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 4e18f2a..3036774 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.9.0 +current_version = 0.9.1 files = SQLTools.py tag = True commit = True diff --git a/SQLTools.py b/SQLTools.py index d6f07ab..4606956 100644 --- a/SQLTools.py +++ b/SQLTools.py @@ -1,4 +1,4 @@ -__version__ = "v0.9.0" +__version__ = "v0.9.1" import sys import os diff --git a/SQLToolsAPI/Command.py b/SQLToolsAPI/Command.py index d740858..8cf7ad5 100644 --- a/SQLToolsAPI/Command.py +++ b/SQLToolsAPI/Command.py @@ -37,9 +37,17 @@ def run(self): si = subprocess.STARTUPINFO() si.dwFlags |= subprocess.STARTF_USESHOWWINDOW + # select appropriate file handle for stderr + # usually we want to redirect stderr to stdout, so erros are shown + # in the output in the right place (where they actually occurred) + # only if silenceErrors=True, we separate stderr from stdout and discard it + stderrHandle = subprocess.STDOUT + if self.silenceErrors: + stderrHandle = subprocess.PIPE + self.process = subprocess.Popen(self.args, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + stderr=stderrHandle, stdin=subprocess.PIPE, env=os.environ.copy(), startupinfo=si) @@ -52,6 +60,9 @@ def run(self): 'replace').replace('\r', '')) queryTimerEnd = time.time() + # we are done with the output, terminate the process + self.process.terminate() + if 'show_query' in self.options and self.options['show_query']: resultInfo = "/*\n-- Executed querie(s) at {0} took {1:.3f}ms --".format( str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(queryTimerStart))), @@ -61,6 +72,10 @@ def run(self): resultInfo, resultLine, self.query, resultLine) return self.callback(resultString) + return + + # regular mode is handled with more reliable Popen.communicate + # which also terminates the process afterwards results, errors = self.process.communicate(input=self.query.encode()) queryTimerEnd = time.time() @@ -110,6 +125,10 @@ def stop(self): if not self.process: return + # if poll returns None - proc still running, otherwise returns process return code + if self.process.poll() is not None: + return + try: # Windows does not provide SIGKILL, go with SIGTERM sig = getattr(signal, 'SIGKILL', signal.SIGTERM) diff --git a/messages.json b/messages.json index 54908cd..a410c39 100644 --- a/messages.json +++ b/messages.json @@ -5,5 +5,6 @@ "0.3.0": "messages/v0.3.0.md", "0.3.1": "messages/v0.3.0.md", "0.8.2": "messages/v0.8.2.md", - "0.9.0": "messages/v0.9.0.md" + "0.9.0": "messages/v0.9.0.md", + "0.9.1": "messages/v0.9.1.md" } diff --git a/messages/v0.9.1.md b/messages/v0.9.1.md new file mode 100644 index 0000000..7a71ded --- /dev/null +++ b/messages/v0.9.1.md @@ -0,0 +1,11 @@ +## v0.9.1 Notes + +### Improvements + +* Display errors inline instead of appending them at the bottom [#92](https://github.com/mtxr/SQLTools/issues/92) + + +### Fixes + +* Thread timeout is always triggered when streaming results output (MacOS) [#90](https://github.com/mtxr/SQLTools/issues/90) +* stderr output is ignored when streaming results output [#91](https://github.com/mtxr/SQLTools/issues/91)