Skip to content

Commit cc4b1ba

Browse files
committed
add compatibility for Python 3.10+
1 parent d65342e commit cc4b1ba

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

venvipy/get_data.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def ensure_active_file():
6161
"""
6262
ensure_confdir()
6363
if not os.path.exists(ACTIVE_FILE):
64-
with open(ACTIVE_FILE, "w+") as f:
64+
with open(ACTIVE_FILE, "w+", encoding="utf-8") as f:
6565
f.write("")
6666

6767

@@ -83,12 +83,14 @@ def get_python_installs(relaunching=False):
8383
Write the found Python versions to `py-installs`. Create
8484
a new database if `relaunching=True`.
8585
"""
86-
versions = ["3.9", "3.8", "3.7", "3.6", "3.5", "3.4", "3.3"]
86+
versions = [
87+
"3.11", "3.10", "3.9", "3.8", "3.7", "3.6", "3.5", "3.4", "3.3"
88+
]
8789
py_info_list = []
8890
ensure_confdir()
8991

9092
if not os.path.exists(DB_FILE) or relaunching:
91-
with open(DB_FILE, "w", newline="") as cf:
93+
with open(DB_FILE, "w", newline="", encoding="utf-8") as cf:
9294
fields = ["PYTHON_VERSION", "PYTHON_PATH"]
9395
writer = csv.DictWriter(
9496
cf,
@@ -126,7 +128,7 @@ def add_python(py_path):
126128
"""
127129
ensure_dbfile()
128130

129-
with open(DB_FILE, "a", newline="") as cf:
131+
with open(DB_FILE, "a", newline="", encoding="utf-8") as cf:
130132
fields = ["PYTHON_VERSION", "PYTHON_PATH"]
131133
writer = csv.DictWriter(
132134
cf,
@@ -150,9 +152,9 @@ def remove_env():
150152
Remove our interpreter if we're running in a virtual
151153
environment.
152154
"""
153-
with open(DB_FILE, "r") as f:
155+
with open(DB_FILE, "r", encoding="utf-8") as f:
154156
lines = f.readlines()
155-
with open(DB_FILE, "w") as f:
157+
with open(DB_FILE, "w", encoding="utf-8") as f:
156158
for line in lines:
157159
if sys.executable not in line.strip("\n"):
158160
f.write(line)
@@ -212,11 +214,16 @@ def get_pyvenv_cfg(cfg_file, cfg):
212214
Values for `cfg` can be strings: `version`, `py_path`,
213215
`site_packages` or `installed`.
214216
"""
215-
with open(cfg_file, "r") as f:
217+
with open(cfg_file, "r", encoding="utf-8") as f:
216218
lines = f.readlines()
217219

220+
if lines[2][13] == ".":
221+
version = lines[2][10:13].strip() # python 3.x
222+
else:
223+
version = lines[2][10:14].strip() # python 3.10+
224+
218225
version_str = to_version(lines[2][10:].strip())
219-
binary_path = to_path(lines[0][7:].strip(), lines[2][10:13].strip())
226+
binary_path = to_path(lines[0][7:].strip(), version)
220227
site_packages = lines[1][31:].strip()
221228

222229
if cfg == "version":
@@ -234,7 +241,7 @@ def get_pyvenv_cfg(cfg_file, cfg):
234241

235242
if cfg == "installed":
236243
ensure_dbfile()
237-
with open(DB_FILE, newline="") as cf:
244+
with open(DB_FILE, newline="", encoding="utf-8") as cf:
238245
reader = csv.DictReader(cf, delimiter=",")
239246
for info in reader:
240247
if binary_path == info["PYTHON_PATH"]:
@@ -248,7 +255,7 @@ def get_active_dir_str():
248255
"""Get the default venv directory string from `active` file.
249256
"""
250257
ensure_active_file()
251-
with open(ACTIVE_FILE, "r") as f:
258+
with open(ACTIVE_FILE, "r", encoding="utf-8") as f:
252259
active_dir = f.read()
253260
return active_dir
254261
return ""

0 commit comments

Comments
 (0)