Skip to content

Commit c7d2de3

Browse files
committed
Force UTF-8 encoding when r/w'ing text files
1 parent 564725a commit c7d2de3

5 files changed

Lines changed: 28 additions & 28 deletions

File tree

compiler/api/compiler.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ def start():
7070
shutil.rmtree("{}/types".format(DESTINATION), ignore_errors=True)
7171
shutil.rmtree("{}/functions".format(DESTINATION), ignore_errors=True)
7272

73-
with open("{}/source/auth_key.tl".format(HOME)) as auth, \
74-
open("{}/source/sys_msgs.tl".format(HOME)) as system, \
75-
open("{}/source/main_api.tl".format(HOME)) as api:
73+
with open("{}/source/auth_key.tl".format(HOME), encoding="utf-8") as auth, \
74+
open("{}/source/sys_msgs.tl".format(HOME), encoding="utf-8") as system, \
75+
open("{}/source/main_api.tl".format(HOME), encoding="utf-8") as api:
7676
schema = (auth.read() + system.read() + api.read()).splitlines()
7777

78-
with open("{}/template/class.txt".format(HOME)) as f:
78+
with open("{}/template/class.txt".format(HOME), encoding="utf-8") as f:
7979
template = f.read()
8080

81-
with open(notice_path) as f:
81+
with open(notice_path, encoding="utf-8") as f:
8282
notice = []
8383

8484
for line in f.readlines():
@@ -162,10 +162,10 @@ def start():
162162
init = "{}/__init__.py".format(path)
163163

164164
if not os.path.exists(init):
165-
with open(init, "w") as f:
165+
with open(init, "w", encoding="utf-8") as f:
166166
f.write(notice + "\n\n")
167167

168-
with open(init, "a") as f:
168+
with open(init, "a", encoding="utf-8") as f:
169169
f.write("from .{} import {}\n".format(snek(c.name), capit(c.name)))
170170

171171
sorted_args = sort_args(c.args)
@@ -397,7 +397,7 @@ def start():
397397
read_types += "\n "
398398
read_types += "{} = Object.read(b)\n ".format(arg_name)
399399

400-
with open("{}/{}.py".format(path, snek(c.name)), "w") as f:
400+
with open("{}/{}.py".format(path, snek(c.name)), "w", encoding="utf-8") as f:
401401
f.write(
402402
template.format(
403403
notice=notice,
@@ -414,7 +414,7 @@ def start():
414414
)
415415
)
416416

417-
with open("{}/all.py".format(DESTINATION), "w") as f:
417+
with open("{}/all.py".format(DESTINATION), "w", encoding="utf-8") as f:
418418
f.write(notice + "\n\n")
419419
f.write("layer = {}\n\n".format(layer))
420420
f.write("objects = {")
@@ -436,7 +436,7 @@ def start():
436436
f.write("\n}\n")
437437

438438
for k, v in namespaces.items():
439-
with open("{}/{}/__init__.py".format(DESTINATION, k), "a") as f:
439+
with open("{}/{}/__init__.py".format(DESTINATION, k), "a", encoding="utf-8") as f:
440440
f.write("from . import {}\n".format(", ".join([i for i in v])) if v else "")
441441

442442

compiler/docs/compiler.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def build(path, level=0):
4444
if not i.startswith("__"):
4545
build("/".join([path, i]), level=level + 1)
4646
except NotADirectoryError:
47-
with open(path + "/" + i) as f:
47+
with open(path + "/" + i, encoding="utf-8") as f:
4848
p = ast.parse(f.read())
4949

5050
for node in ast.walk(p):
@@ -59,7 +59,7 @@ def build(path, level=0):
5959

6060
os.makedirs(os.path.dirname(destination + "/" + full_path), exist_ok=True)
6161

62-
with open(destination + "/" + full_path, "w") as f:
62+
with open(destination + "/" + full_path, "w", encoding="utf-8") as f:
6363
f.write(
6464
page_template.format(
6565
title=name,
@@ -94,7 +94,7 @@ def build(path, level=0):
9494
inner_path = base + "/index" + ".rst"
9595
module = "pyrogram.api.{}".format(base)
9696

97-
with open(destination + "/" + inner_path, "w") as f:
97+
with open(destination + "/" + inner_path, "w", encoding="utf-8") as f:
9898
f.write(
9999
toctree.format(
100100
title=k.title(),
@@ -111,10 +111,10 @@ def start():
111111
global page_template
112112
global toctree
113113

114-
with open(home + "/template/page.txt") as f:
114+
with open(home + "/template/page.txt", encoding="utf-8") as f:
115115
page_template = f.read()
116116

117-
with open(home + "/template/toctree.txt") as f:
117+
with open(home + "/template/toctree.txt", encoding="utf-8") as f:
118118
toctree = f.read()
119119

120120
generate(types_path, types_base)

compiler/error/compiler.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ def start():
4343

4444
files = [i for i in os.listdir("{}/source".format(home))]
4545

46-
with open(notice_path) as f:
46+
with open(notice_path, encoding="utf-8") as f:
4747
notice = []
4848

4949
for line in f.readlines():
5050
notice.append("# {}".format(line).strip())
5151

5252
notice = "\n".join(notice)
5353

54-
with open("{}/all.py".format(dest), "w") as f_all:
54+
with open("{}/all.py".format(dest), "w", encoding="utf-8") as f_all:
5555
f_all.write(notice + "\n\n")
5656
f_all.write("count = {count}\n\n")
5757
f_all.write("exceptions = {\n")
@@ -66,14 +66,14 @@ def start():
6666
init = "{}/__init__.py".format(dest)
6767

6868
if not os.path.exists(init):
69-
with open(init, "w") as f_init:
69+
with open(init, "w", encoding="utf-8") as f_init:
7070
f_init.write(notice + "\n\n")
7171

72-
with open(init, "a") as f_init:
72+
with open(init, "a", encoding="utf-8") as f_init:
7373
f_init.write("from .{}_{} import *\n".format(name.lower(), code))
7474

75-
with open("{}/source/{}".format(home, i)) as f_csv, \
76-
open("{}/{}_{}.py".format(dest, name.lower(), code), "w") as f_class:
75+
with open("{}/source/{}".format(home, i), encoding="utf-8") as f_csv, \
76+
open("{}/{}_{}.py".format(dest, name.lower(), code), "w", encoding="utf-8") as f_class:
7777
reader = csv.reader(f_csv, delimiter="\t")
7878

7979
super_class = caml(name)
@@ -98,10 +98,10 @@ def start():
9898

9999
sub_classes.append((sub_class, id, message))
100100

101-
with open("{}/template/class.txt".format(home), "r") as f_class_template:
101+
with open("{}/template/class.txt".format(home), "r", encoding="utf-8") as f_class_template:
102102
class_template = f_class_template.read()
103103

104-
with open("{}/template/sub_class.txt".format(home), "r") as f_sub_class_template:
104+
with open("{}/template/sub_class.txt".format(home), "r", encoding="utf-8") as f_sub_class_template:
105105
sub_class_template = f_sub_class_template.read()
106106

107107
class_template = class_template.format(
@@ -123,10 +123,10 @@ def start():
123123

124124
f_all.write("}\n")
125125

126-
with open("{}/all.py".format(dest)) as f:
126+
with open("{}/all.py".format(dest), encoding="utf-8") as f:
127127
content = f.read()
128128

129-
with open("{}/all.py".format(dest), "w") as f:
129+
with open("{}/all.py".format(dest), "w", encoding="utf-8") as f:
130130
f.write(re.sub("{count}", str(count), content))
131131

132132
print("Compiling Errors: [100%]")

pyrogram/api/errors/error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __init__(self, x: int or RpcError = None, query_type: type = None):
4242

4343
# TODO: Proper log unknown errors
4444
if self.CODE == 520:
45-
with open("unknown_errors.txt", "a") as f:
45+
with open("unknown_errors.txt", "a", encoding="utf-8") as f:
4646
f.write("{}\t{}\t{}\n".format(x.error_code, x.error_message, query_type))
4747

4848
@staticmethod

pyrogram/client/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ def load_config(self):
311311

312312
def load_session(self, session_name):
313313
try:
314-
with open("{}.session".format(session_name)) as f:
314+
with open("{}.session".format(session_name), encoding="utf-8") as f:
315315
s = json.load(f)
316316
except FileNotFoundError:
317317
self.dc_id = 1
@@ -326,7 +326,7 @@ def save_session(self):
326326
auth_key = base64.b64encode(self.auth_key).decode()
327327
auth_key = [auth_key[i: i + 43] for i in range(0, len(auth_key), 43)]
328328

329-
with open("{}.session".format(self.session_name), "w") as f:
329+
with open("{}.session".format(self.session_name), "w", encoding="utf-8") as f:
330330
json.dump(
331331
dict(
332332
dc_id=self.dc_id,

0 commit comments

Comments
 (0)