Skip to content

Commit 45b8dc0

Browse files
[BUG FIX] A problem in psql/safe_psql and 'input' data was fixed [local_op]
Both LocalOperations::exec_command and RemoteOperations::exec_command were updated.
1 parent 2bb38dc commit 45b8dc0

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

testgres/node.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,18 @@ def _psql(
10051005
username=None,
10061006
input=None,
10071007
**variables):
1008+
assert type(variables) == dict # noqa: E721
1009+
1010+
#
1011+
# We do not support encoding. It may be added later. Ok?
1012+
#
1013+
if input is None:
1014+
pass
1015+
elif type(input) == bytes: # noqa: E721
1016+
pass
1017+
else:
1018+
raise Exception("Input data must be None or bytes.")
1019+
10081020
dbname = dbname or default_dbname()
10091021

10101022
psql_params = [

testgres/operations/helpers.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Helpers:
2+
def PrepareProcessInput(input, encoding):
3+
if not input:
4+
return None
5+
6+
if type(input) == str: # noqa: E721
7+
if encoding is None:
8+
return input.encode()
9+
10+
assert type(encoding) == str # noqa: E721
11+
return input.encode(encoding)
12+
13+
# It is expected!
14+
assert type(input) == bytes # noqa: E721
15+
return input

testgres/operations/local_ops.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from ..exceptions import ExecUtilException
1313
from .os_ops import ConnectionParams, OsOperations, pglib, get_default_encoding
14+
from .helpers import Helpers
1415
from ..helpers.raise_error import RaiseError
1516

1617
try:
@@ -58,6 +59,8 @@ def _process_output(encoding, temp_file_path):
5859
return output, None # In Windows stderr writing in stdout
5960

6061
def _run_command__nt(self, cmd, shell, input, stdin, stdout, stderr, get_process, timeout, encoding):
62+
# TODO: why don't we use the data from input?
63+
6164
with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as temp_file:
6265
stdout = temp_file
6366
stderr = subprocess.STDOUT
@@ -79,6 +82,10 @@ def _run_command__nt(self, cmd, shell, input, stdin, stdout, stderr, get_process
7982
return process, output, error
8083

8184
def _run_command__generic(self, cmd, shell, input, stdin, stdout, stderr, get_process, timeout, encoding):
85+
input_prepared = None
86+
if not get_process:
87+
input_prepared = Helpers.PrepareProcessInput(input, encoding) # throw
88+
8289
process = subprocess.Popen(
8390
cmd,
8491
shell=shell,
@@ -89,7 +96,7 @@ def _run_command__generic(self, cmd, shell, input, stdin, stdout, stderr, get_pr
8996
if get_process:
9097
return process, None, None
9198
try:
92-
output, error = process.communicate(input=input.encode(encoding) if input else None, timeout=timeout)
99+
output, error = process.communicate(input=input_prepared, timeout=timeout)
93100
if encoding:
94101
output = output.decode(encoding)
95102
error = error.decode(encoding)

testgres/operations/remote_ops.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from ..exceptions import ExecUtilException
1717
from ..helpers.raise_error import RaiseError
1818
from .os_ops import OsOperations, ConnectionParams, get_default_encoding
19+
from .helpers import Helpers
1920

2021
error_markers = [b'error', b'Permission denied', b'fatal', b'No such file or directory']
2122

@@ -70,6 +71,10 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
7071
assert type(expect_error) == bool # noqa: E721
7172
assert type(ignore_errors) == bool # noqa: E721
7273

74+
input_prepared = None
75+
if not get_process:
76+
input_prepared = Helpers.PrepareProcessInput(input, encoding) # throw
77+
7378
ssh_cmd = []
7479
if isinstance(cmd, str):
7580
ssh_cmd = ['ssh', self.ssh_dest] + self.ssh_args + [cmd]
@@ -80,7 +85,7 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
8085
return process
8186

8287
try:
83-
result, error = process.communicate(input, timeout=timeout)
88+
result, error = process.communicate(input=input_prepared, timeout=timeout)
8489
except subprocess.TimeoutExpired:
8590
process.kill()
8691
raise ExecUtilException("Command timed out after {} seconds.".format(timeout))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy