Skip to content
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
18 changes: 9 additions & 9 deletions src/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ def encode(self, *args, **kwargs) -> _BinDataBox:
return self


class Helpers:
@staticmethod
def _make_get_default_encoding_func():
# locale.getencoding is added in Python 3.11
if hasattr(locale, 'getencoding'):
return locale.getencoding
def _make_get_default_encoding_func() -> typing.Callable:
# locale.getencoding is added in Python 3.11
if hasattr(locale, 'getencoding'):
return locale.getencoding

# It must exist
return locale.getpreferredencoding
# It must exist
return locale.getpreferredencoding


class Helpers:
# Prepared pointer on function to get a name of system codepage
_get_default_encoding_func = _make_get_default_encoding_func.__func__()
_get_default_encoding_func = _make_get_default_encoding_func()

@staticmethod
def get_default_encoding():
Expand Down
104 changes: 89 additions & 15 deletions src/local_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def _run_command__generic(
text=get_process and (encoding is not None),
encoding=encoding if get_process else None,
**extParams,
)
) # type: subprocess.Popen[typing.Any]
assert process is not None
assert isinstance(process, subprocess.Popen)
if get_process:
Expand All @@ -400,10 +400,16 @@ def _run_command__generic(
assert type(output) is bytes
assert type(error) is bytes

if encoding:
output = output.decode(encoding)
error = error.decode(encoding)
return process.returncode, output, error
if not encoding:
return process.returncode, output, error

output_s = output.decode(encoding)
error_s = error.decode(encoding)

assert type(output_s) is str
assert type(error_s) is str
assert type(process.returncode) is int
return process.returncode, output_s, error_s

def _run_command(
self,
Expand Down Expand Up @@ -841,34 +847,102 @@ def write(
encoding: Code page of text data.
"""
assert type(filename) is str
assert encoding is None or type(encoding) is str
assert data is not None
assert type(data) in [str, bytes, list]
assert type(truncate) is bool
assert type(binary) is bool
assert type(read_and_write) is bool
assert encoding is None or type(encoding) is str

if binary:
return self._write_binary(
filename,
data,
truncate,
read_and_write,
encoding,
)

return self._write_text(
filename,
data,
truncate,
read_and_write,
encoding,
)

def _write_binary(
self,
filename: str,
data: OsOperations.T_WRITE_DATA,
truncate: bool,
read_and_write: bool,
encoding: typing.Optional[str],
) -> None:
assert type(filename) is str
assert data is not None
assert type(data) in [str, bytes, list]
assert type(truncate) is bool
assert type(read_and_write) is bool
assert encoding is None or type(encoding) is str

if not encoding:
encoding = get_default_encoding()

mode = "w" if truncate else "a"
file = open(
filename,
"wb" if truncate else "ab",
)
assert file is not None

# If it is a bytes str or list
if binary:
mode += "b"
with file:
if isinstance(data, list):
for s in data:
data2 = __class__._prepare_data_to_write(s, True, encoding)
assert type(data2) is bytes
file.write(data2)
continue
else:
data2 = __class__._prepare_data_to_write(data, True, encoding)
assert type(data2) is bytes
file.write(data2)
return

def _write_text(
self,
filename: str,
data: OsOperations.T_WRITE_DATA,
truncate: bool,
read_and_write: bool,
encoding: typing.Optional[str],
) -> None:
assert type(filename) is str
assert data is not None
assert type(data) in [str, bytes, list]
assert type(truncate) is bool
assert type(read_and_write) is bool
assert encoding is None or type(encoding) is str

if not encoding:
encoding = get_default_encoding()

assert type(mode) is str
assert mode != ""
file = open(
filename,
mode="w" if truncate else "a",
encoding=encoding,
)
assert file is not None

with open(filename, mode, encoding=None if binary else encoding) as file:
with file:
if isinstance(data, list):
for s in data:
data2 = __class__._prepare_data_to_write(s, binary, encoding)
data2 = __class__._prepare_data_to_write(s, False, encoding)
assert type(data2) is str
file.write(data2)
continue
else:
data2 = __class__._prepare_data_to_write(data, binary, encoding)
data2 = __class__._prepare_data_to_write(data, False, encoding)
assert type(data2) is str
file.write(data2)
return

Expand Down
4 changes: 2 additions & 2 deletions tests/test_os_ops_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5007,9 +5007,9 @@ def test_popen_garbage_collection(

# Запоминаем локальный процесс транспорта, чтобы проверить его смерть в конце
if type(controller).__name__ == "LocalProcessController":
local_p = controller._local_process
local_p = getattr(controller, "_local_process")
elif type(controller).__name__ == "RemoteProcessController":
local_p = controller._local_process
local_p = getattr(controller, "_local_process")
else:
raise RuntimeError("Unknown controller type: {}.".format(
type(controller).__name__,
Expand Down