diff --git a/pyencrypt/encrypt.py b/pyencrypt/encrypt.py index 9d4dc28..48c6f7d 100644 --- a/pyencrypt/encrypt.py +++ b/pyencrypt/encrypt.py @@ -57,13 +57,14 @@ def generate_so_file(cipher_key: str, d: int, n: int, base_dir: Optional[Path] = need_import_files = ['ntt.py', 'aes.py', 'decrypt.py', 'license.py'] for file in need_import_files: file_path = path / file - decrypt_source_ls.append(REMOVE_SELF_IMPORT.sub('', file_path.read_text())) + decrypt_source_ls.append(REMOVE_SELF_IMPORT.sub('', file_path.read_text(encoding="utf-8"))) loader_source_path = path / 'loader.py' - loader_source = REMOVE_SELF_IMPORT.sub('', loader_source_path.read_text()).replace( - "__private_key = ''", f"__private_key = '{private_key}'", 1 - ).replace("__cipher_key = ''", f"__cipher_key = '{cipher_key}'", 1).replace( - 'license = None', f'license = {license}', 1 + loader_source = ( + REMOVE_SELF_IMPORT.sub("", loader_source_path.read_text(encoding="utf-8")) + .replace("__private_key = ''", f"__private_key = '{private_key}'", 1) + .replace("__cipher_key = ''", f"__cipher_key = '{cipher_key}'", 1) + .replace("license = None", f"license = {license}", 1) ) if base_dir is None: @@ -79,9 +80,14 @@ def generate_so_file(cipher_key: str, d: int, n: int, base_dir: Optional[Path] = # Origin file loader_origin_file_path = temp_dir / 'loader_origin.py' loader_origin_file_path.touch(exist_ok=True) - loader_origin_file_path.write_text(f"{decrypt_source}\n{loader_source}") + loader_origin_file_path.write_text( + f"{decrypt_source}\n{loader_source}", encoding="utf-8" + ) - loader_file_path.write_text(python_minifier.minify(loader_origin_file_path.read_text())) + loader_file_path.write_text( + python_minifier.minify(loader_origin_file_path.read_text(encoding="utf-8")), + encoding="utf-8", + ) from setuptools import setup # isort:skip from Cython.Build import cythonize diff --git a/tests/conftest.py b/tests/conftest.py index 6f15770..87ff186 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -30,7 +30,7 @@ def file_and_loader(request, tmp_path_factory): file_path.write_text("""\ def {function_name}(): {code} - """.format(function_name=function_name, code=code)) + """.format(function_name=function_name, code=code), encoding='utf-8') # generate loader.so key = generate_aes_key() new_path = file_path.with_suffix('.pye') @@ -74,7 +74,7 @@ def package_and_loader(request, tmp_path_factory): file_path.write_text("""\ def {function_name}(): {code} - """.format(function_name=function_name, code=code)) + """.format(function_name=function_name, code=code), encoding='utf-8') new_path = file_path.with_suffix('.pye') key = generate_aes_key() diff --git a/tests/test_license.py b/tests/test_license.py index 2ecd133..df5db90 100644 --- a/tests/test_license.py +++ b/tests/test_license.py @@ -81,7 +81,7 @@ def test_check_license_invalid(self, key, tmp_path): license_file_path = generate_license_file(key, path=tmp_path) license_data = json.loads(license_file_path.read_text()) license_data['signature'] = 'invalid' - license_file_path.write_text(json.dumps(license_data)) + license_file_path.write_text(json.dumps(license_data), encoding='utf-8') with pytest.raises(Exception) as excinfo: check_license(license_file_path, key) assert str(excinfo.value) == 'License signature is invalid.'