Skip to content

Commit

Permalink
fix: 🐛 read_text encoding in Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhaoQi99 committed Mar 26, 2024
1 parent 0b6e0c2 commit f9d73b4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
20 changes: 13 additions & 7 deletions pyencrypt/encrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down

0 comments on commit f9d73b4

Please sign in to comment.