Skip to content

Fix some bugs with pipe on Windows #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions testgres/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,8 +865,7 @@ def psql(self,
"-X", # no .psqlrc
"-A", # unaligned output
"-t", # print rows only
"-q", # run quietly
dbname
"-q" # run quietly
] # yapf: disable

# set variables before execution
Expand All @@ -881,6 +880,9 @@ def psql(self,
else:
raise QueryException('Query or filename must be provided')

# should be the last one
psql_params.append(dbname)

# start psql process
process = subprocess.Popen(
psql_params,
Expand Down
34 changes: 27 additions & 7 deletions testgres/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import port_for
import subprocess
import sys
import tempfile

from contextlib import contextmanager
from distutils.version import LooseVersion
Expand Down Expand Up @@ -59,13 +60,32 @@ def execute_utility(args, logfile=None):
"""

# run utility
process = subprocess.Popen(
args, # util + params
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)

# get result and decode it
out, _ = process.communicate()
if os.name == 'nt':
# using output to a temporary file in Windows
buf = tempfile.NamedTemporaryFile()

process = subprocess.Popen(
args, # util + params
stdout=buf,
stderr=subprocess.STDOUT
)
process.communicate()

# get result
buf.file.flush()
buf.file.seek(0)
out = buf.file.read()
buf.close()
else:
process = subprocess.Popen(
args, # util + params
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)

# get result
out, _ = process.communicate()

# decode result
out = '' if not out else out.decode('utf-8')

# format command
Expand Down