diff --git a/src/helpers.py b/src/helpers.py index 19f2972..501eb6c 100644 --- a/src/helpers.py +++ b/src/helpers.py @@ -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(): diff --git a/src/local_ops.py b/src/local_ops.py index aaa5ac8..7e59c89 100644 --- a/src/local_ops.py +++ b/src/local_ops.py @@ -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: @@ -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, @@ -841,7 +847,6 @@ 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 @@ -849,26 +854,95 @@ def write( 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 diff --git a/tests/test_os_ops_common.py b/tests/test_os_ops_common.py index b86bafe..2a085b2 100644 --- a/tests/test_os_ops_common.py +++ b/tests/test_os_ops_common.py @@ -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__,