| 1 |
import copy |
| 2 |
import ntpath |
| 3 |
import pathlib |
| 4 |
import posixpath |
| 5 |
import unittest |
| 6 |
|
| 7 |
from test.support import verbose |
| 8 |
|
| 9 |
try: |
| 10 |
# If we are in a source tree, use the original source file for tests |
| 11 |
SOURCE = (pathlib.Path(__file__).absolute().parent.parent.parent / "Modules/getpath.py").read_bytes() |
| 12 |
except FileNotFoundError: |
| 13 |
# Try from _testcapimodule instead |
| 14 |
from _testinternalcapi import get_getpath_codeobject |
| 15 |
SOURCE = get_getpath_codeobject() |
| 16 |
|
| 17 |
|
| 18 |
class MockGetPathTests(unittest.TestCase): |
| 19 |
def __init__(self, *a, **kw): |
| 20 |
super().__init__(*a, **kw) |
| 21 |
self.maxDiff = None |
| 22 |
|
| 23 |
def test_normal_win32(self): |
| 24 |
"Test a 'standard' install layout on Windows." |
| 25 |
ns = MockNTNamespace( |
| 26 |
argv0=r"C:\Python\python.exe", |
| 27 |
real_executable=r"C:\Python\python.exe", |
| 28 |
) |
| 29 |
ns.add_known_xfile(r"C:\Python\python.exe") |
| 30 |
ns.add_known_file(r"C:\Python\Lib\os.py") |
| 31 |
ns.add_known_dir(r"C:\Python\DLLs") |
| 32 |
expected = dict( |
| 33 |
executable=r"C:\Python\python.exe", |
| 34 |
base_executable=r"C:\Python\python.exe", |
| 35 |
prefix=r"C:\Python", |
| 36 |
exec_prefix=r"C:\Python", |
| 37 |
module_search_paths_set=1, |
| 38 |
module_search_paths=[ |
| 39 |
r"C:\Python\python98.zip", |
| 40 |
r"C:\Python\DLLs", |
| 41 |
r"C:\Python\Lib", |
| 42 |
r"C:\Python", |
| 43 |
], |
| 44 |
) |
| 45 |
actual = getpath(ns, expected) |
| 46 |
self.assertEqual(expected, actual) |
| 47 |
|
| 48 |
def test_buildtree_win32(self): |
| 49 |
"Test an in-build-tree layout on Windows." |
| 50 |
ns = MockNTNamespace( |
| 51 |
argv0=r"C:\CPython\PCbuild\amd64\python.exe", |
| 52 |
real_executable=r"C:\CPython\PCbuild\amd64\python.exe", |
| 53 |
) |
| 54 |
ns.add_known_xfile(r"C:\CPython\PCbuild\amd64\python.exe") |
| 55 |
ns.add_known_file(r"C:\CPython\Lib\os.py") |
| 56 |
ns.add_known_file(r"C:\CPython\PCbuild\amd64\pybuilddir.txt", [""]) |
| 57 |
expected = dict( |
| 58 |
executable=r"C:\CPython\PCbuild\amd64\python.exe", |
| 59 |
base_executable=r"C:\CPython\PCbuild\amd64\python.exe", |
| 60 |
prefix=r"C:\CPython", |
| 61 |
exec_prefix=r"C:\CPython", |
| 62 |
build_prefix=r"C:\CPython", |
| 63 |
_is_python_build=1, |
| 64 |
module_search_paths_set=1, |
| 65 |
module_search_paths=[ |
| 66 |
r"C:\CPython\PCbuild\amd64\python98.zip", |
| 67 |
r"C:\CPython\PCbuild\amd64", |
| 68 |
r"C:\CPython\Lib", |
| 69 |
], |
| 70 |
) |
| 71 |
actual = getpath(ns, expected) |
| 72 |
self.assertEqual(expected, actual) |
| 73 |
|
| 74 |
def test_venv_win32(self): |
| 75 |
"""Test a venv layout on Windows. |
| 76 |
|
| 77 |
This layout is discovered by the presence of %__PYVENV_LAUNCHER__%, |
| 78 |
specifying the original launcher executable. site.py is responsible |
| 79 |
for updating prefix and exec_prefix. |
| 80 |
""" |
| 81 |
ns = MockNTNamespace( |
| 82 |
argv0=r"C:\Python\python.exe", |
| 83 |
ENV___PYVENV_LAUNCHER__=r"C:\venv\Scripts\python.exe", |
| 84 |
real_executable=r"C:\Python\python.exe", |
| 85 |
) |
| 86 |
ns.add_known_xfile(r"C:\Python\python.exe") |
| 87 |
ns.add_known_xfile(r"C:\venv\Scripts\python.exe") |
| 88 |
ns.add_known_file(r"C:\Python\Lib\os.py") |
| 89 |
ns.add_known_dir(r"C:\Python\DLLs") |
| 90 |
ns.add_known_file(r"C:\venv\pyvenv.cfg", [ |
| 91 |
r"home = C:\Python" |
| 92 |
]) |
| 93 |
expected = dict( |
| 94 |
executable=r"C:\venv\Scripts\python.exe", |
| 95 |
prefix=r"C:\venv", |
| 96 |
exec_prefix=r"C:\venv", |
| 97 |
base_executable=r"C:\Python\python.exe", |
| 98 |
base_prefix=r"C:\Python", |
| 99 |
base_exec_prefix=r"C:\Python", |
| 100 |
module_search_paths_set=1, |
| 101 |
module_search_paths=[ |
| 102 |
r"C:\Python\python98.zip", |
| 103 |
r"C:\Python\DLLs", |
| 104 |
r"C:\Python\Lib", |
| 105 |
r"C:\Python", |
| 106 |
], |
| 107 |
) |
| 108 |
actual = getpath(ns, expected) |
| 109 |
self.assertEqual(expected, actual) |
| 110 |
|
| 111 |
def test_registry_win32(self): |
| 112 |
"""Test registry lookup on Windows. |
| 113 |
|
| 114 |
On Windows there are registry entries that are intended for other |
| 115 |
applications to register search paths. |
| 116 |
""" |
| 117 |
hkey = rf"HKLM\Software\Python\PythonCore\9.8-XY\PythonPath" |
| 118 |
winreg = MockWinreg({ |
| 119 |
hkey: None, |
| 120 |
f"{hkey}\\Path1": "path1-dir", |
| 121 |
f"{hkey}\\Path1\\Subdir": "not-subdirs", |
| 122 |
}) |
| 123 |
ns = MockNTNamespace( |
| 124 |
argv0=r"C:\Python\python.exe", |
| 125 |
real_executable=r"C:\Python\python.exe", |
| 126 |
winreg=winreg, |
| 127 |
) |
| 128 |
ns.add_known_xfile(r"C:\Python\python.exe") |
| 129 |
ns.add_known_file(r"C:\Python\Lib\os.py") |
| 130 |
ns.add_known_dir(r"C:\Python\DLLs") |
| 131 |
expected = dict( |
| 132 |
module_search_paths_set=1, |
| 133 |
module_search_paths=[ |
| 134 |
r"C:\Python\python98.zip", |
| 135 |
"path1-dir", |
| 136 |
# should not contain not-subdirs |
| 137 |
r"C:\Python\DLLs", |
| 138 |
r"C:\Python\Lib", |
| 139 |
r"C:\Python", |
| 140 |
], |
| 141 |
) |
| 142 |
actual = getpath(ns, expected) |
| 143 |
self.assertEqual(expected, actual) |
| 144 |
|
| 145 |
ns["config"]["use_environment"] = 0 |
| 146 |
ns["config"]["module_search_paths_set"] = 0 |
| 147 |
ns["config"]["module_search_paths"] = None |
| 148 |
expected = dict( |
| 149 |
module_search_paths_set=1, |
| 150 |
module_search_paths=[ |
| 151 |
r"C:\Python\python98.zip", |
| 152 |
r"C:\Python\DLLs", |
| 153 |
r"C:\Python\Lib", |
| 154 |
r"C:\Python", |
| 155 |
], |
| 156 |
) |
| 157 |
actual = getpath(ns, expected) |
| 158 |
self.assertEqual(expected, actual) |
| 159 |
|
| 160 |
def test_symlink_normal_win32(self): |
| 161 |
"Test a 'standard' install layout via symlink on Windows." |
| 162 |
ns = MockNTNamespace( |
| 163 |
argv0=r"C:\LinkedFrom\python.exe", |
| 164 |
real_executable=r"C:\Python\python.exe", |
| 165 |
) |
| 166 |
ns.add_known_xfile(r"C:\LinkedFrom\python.exe") |
| 167 |
ns.add_known_xfile(r"C:\Python\python.exe") |
| 168 |
ns.add_known_link(r"C:\LinkedFrom\python.exe", r"C:\Python\python.exe") |
| 169 |
ns.add_known_file(r"C:\Python\Lib\os.py") |
| 170 |
ns.add_known_dir(r"C:\Python\DLLs") |
| 171 |
expected = dict( |
| 172 |
executable=r"C:\LinkedFrom\python.exe", |
| 173 |
base_executable=r"C:\LinkedFrom\python.exe", |
| 174 |
prefix=r"C:\Python", |
| 175 |
exec_prefix=r"C:\Python", |
| 176 |
module_search_paths_set=1, |
| 177 |
module_search_paths=[ |
| 178 |
r"C:\Python\python98.zip", |
| 179 |
r"C:\Python\DLLs", |
| 180 |
r"C:\Python\Lib", |
| 181 |
r"C:\Python", |
| 182 |
], |
| 183 |
) |
| 184 |
actual = getpath(ns, expected) |
| 185 |
self.assertEqual(expected, actual) |
| 186 |
|
| 187 |
def test_symlink_buildtree_win32(self): |
| 188 |
"Test an in-build-tree layout via symlink on Windows." |
| 189 |
ns = MockNTNamespace( |
| 190 |
argv0=r"C:\LinkedFrom\python.exe", |
| 191 |
real_executable=r"C:\CPython\PCbuild\amd64\python.exe", |
| 192 |
) |
| 193 |
ns.add_known_xfile(r"C:\LinkedFrom\python.exe") |
| 194 |
ns.add_known_xfile(r"C:\CPython\PCbuild\amd64\python.exe") |
| 195 |
ns.add_known_link(r"C:\LinkedFrom\python.exe", r"C:\CPython\PCbuild\amd64\python.exe") |
| 196 |
ns.add_known_file(r"C:\CPython\Lib\os.py") |
| 197 |
ns.add_known_file(r"C:\CPython\PCbuild\amd64\pybuilddir.txt", [""]) |
| 198 |
expected = dict( |
| 199 |
executable=r"C:\LinkedFrom\python.exe", |
| 200 |
base_executable=r"C:\LinkedFrom\python.exe", |
| 201 |
prefix=r"C:\CPython", |
| 202 |
exec_prefix=r"C:\CPython", |
| 203 |
build_prefix=r"C:\CPython", |
| 204 |
_is_python_build=1, |
| 205 |
module_search_paths_set=1, |
| 206 |
module_search_paths=[ |
| 207 |
r"C:\CPython\PCbuild\amd64\python98.zip", |
| 208 |
r"C:\CPython\PCbuild\amd64", |
| 209 |
r"C:\CPython\Lib", |
| 210 |
], |
| 211 |
) |
| 212 |
actual = getpath(ns, expected) |
| 213 |
self.assertEqual(expected, actual) |
| 214 |
|
| 215 |
def test_buildtree_pythonhome_win32(self): |
| 216 |
"Test an out-of-build-tree layout on Windows with PYTHONHOME override." |
| 217 |
ns = MockNTNamespace( |
| 218 |
argv0=r"C:\Out\python.exe", |
| 219 |
real_executable=r"C:\Out\python.exe", |
| 220 |
ENV_PYTHONHOME=r"C:\CPython", |
| 221 |
) |
| 222 |
ns.add_known_xfile(r"C:\Out\python.exe") |
| 223 |
ns.add_known_file(r"C:\CPython\Lib\os.py") |
| 224 |
ns.add_known_file(r"C:\Out\pybuilddir.txt", [""]) |
| 225 |
expected = dict( |
| 226 |
executable=r"C:\Out\python.exe", |
| 227 |
base_executable=r"C:\Out\python.exe", |
| 228 |
prefix=r"C:\CPython", |
| 229 |
exec_prefix=r"C:\CPython", |
| 230 |
# This build_prefix is a miscalculation, because we have |
| 231 |
# moved the output direction out of the prefix. |
| 232 |
# Specify PYTHONHOME to get the correct prefix/exec_prefix |
| 233 |
build_prefix="C:\\", |
| 234 |
_is_python_build=1, |
| 235 |
module_search_paths_set=1, |
| 236 |
module_search_paths=[ |
| 237 |
r"C:\Out\python98.zip", |
| 238 |
r"C:\Out", |
| 239 |
r"C:\CPython\Lib", |
| 240 |
], |
| 241 |
) |
| 242 |
actual = getpath(ns, expected) |
| 243 |
self.assertEqual(expected, actual) |
| 244 |
|
| 245 |
def test_no_dlls_win32(self): |
| 246 |
"Test a layout on Windows with no DLLs directory." |
| 247 |
ns = MockNTNamespace( |
| 248 |
argv0=r"C:\Python\python.exe", |
| 249 |
real_executable=r"C:\Python\python.exe", |
| 250 |
) |
| 251 |
ns.add_known_xfile(r"C:\Python\python.exe") |
| 252 |
ns.add_known_file(r"C:\Python\Lib\os.py") |
| 253 |
expected = dict( |
| 254 |
executable=r"C:\Python\python.exe", |
| 255 |
base_executable=r"C:\Python\python.exe", |
| 256 |
prefix=r"C:\Python", |
| 257 |
exec_prefix=r"C:\Python", |
| 258 |
module_search_paths_set=1, |
| 259 |
module_search_paths=[ |
| 260 |
r"C:\Python\python98.zip", |
| 261 |
r"C:\Python", |
| 262 |
r"C:\Python\Lib", |
| 263 |
], |
| 264 |
) |
| 265 |
actual = getpath(ns, expected) |
| 266 |
self.assertEqual(expected, actual) |
| 267 |
|
| 268 |
def test_normal_posix(self): |
| 269 |
"Test a 'standard' install layout on *nix" |
| 270 |
ns = MockPosixNamespace( |
| 271 |
PREFIX="/usr", |
| 272 |
argv0="python", |
| 273 |
ENV_PATH="/usr/bin", |
| 274 |
) |
| 275 |
ns.add_known_xfile("/usr/bin/python") |
| 276 |
ns.add_known_file("/usr/lib/python9.8/os.py") |
| 277 |
ns.add_known_dir("/usr/lib/python9.8/lib-dynload") |
| 278 |
expected = dict( |
| 279 |
executable="/usr/bin/python", |
| 280 |
base_executable="/usr/bin/python", |
| 281 |
prefix="/usr", |
| 282 |
exec_prefix="/usr", |
| 283 |
module_search_paths_set=1, |
| 284 |
module_search_paths=[ |
| 285 |
"/usr/lib/python98.zip", |
| 286 |
"/usr/lib/python9.8", |
| 287 |
"/usr/lib/python9.8/lib-dynload", |
| 288 |
], |
| 289 |
) |
| 290 |
actual = getpath(ns, expected) |
| 291 |
self.assertEqual(expected, actual) |
| 292 |
|
| 293 |
def test_buildpath_posix(self): |
| 294 |
"""Test an in-build-tree layout on POSIX. |
| 295 |
|
| 296 |
This layout is discovered from the presence of pybuilddir.txt, which |
| 297 |
contains the relative path from the executable's directory to the |
| 298 |
platstdlib path. |
| 299 |
""" |
| 300 |
ns = MockPosixNamespace( |
| 301 |
argv0=r"/home/cpython/python", |
| 302 |
PREFIX="/usr/local", |
| 303 |
) |
| 304 |
ns.add_known_xfile("/home/cpython/python") |
| 305 |
ns.add_known_xfile("/usr/local/bin/python") |
| 306 |
ns.add_known_file("/home/cpython/pybuilddir.txt", ["build/lib.linux-x86_64-9.8"]) |
| 307 |
ns.add_known_file("/home/cpython/Lib/os.py") |
| 308 |
ns.add_known_dir("/home/cpython/lib-dynload") |
| 309 |
expected = dict( |
| 310 |
executable="/home/cpython/python", |
| 311 |
prefix="/usr/local", |
| 312 |
exec_prefix="/usr/local", |
| 313 |
base_executable="/home/cpython/python", |
| 314 |
build_prefix="/home/cpython", |
| 315 |
_is_python_build=1, |
| 316 |
module_search_paths_set=1, |
| 317 |
module_search_paths=[ |
| 318 |
"/usr/local/lib/python98.zip", |
| 319 |
"/home/cpython/Lib", |
| 320 |
"/home/cpython/build/lib.linux-x86_64-9.8", |
| 321 |
], |
| 322 |
) |
| 323 |
actual = getpath(ns, expected) |
| 324 |
self.assertEqual(expected, actual) |
| 325 |
|
| 326 |
def test_venv_posix(self): |
| 327 |
"Test a venv layout on *nix." |
| 328 |
ns = MockPosixNamespace( |
| 329 |
argv0="python", |
| 330 |
PREFIX="/usr", |
| 331 |
ENV_PATH="/venv/bin:/usr/bin", |
| 332 |
) |
| 333 |
ns.add_known_xfile("/usr/bin/python") |
| 334 |
ns.add_known_xfile("/venv/bin/python") |
| 335 |
ns.add_known_file("/usr/lib/python9.8/os.py") |
| 336 |
ns.add_known_dir("/usr/lib/python9.8/lib-dynload") |
| 337 |
ns.add_known_file("/venv/pyvenv.cfg", [ |
| 338 |
r"home = /usr/bin" |
| 339 |
]) |
| 340 |
expected = dict( |
| 341 |
executable="/venv/bin/python", |
| 342 |
prefix="/venv", |
| 343 |
exec_prefix="/venv", |
| 344 |
base_executable="/usr/bin/python", |
| 345 |
base_prefix="/usr", |
| 346 |
base_exec_prefix="/usr", |
| 347 |
module_search_paths_set=1, |
| 348 |
module_search_paths=[ |
| 349 |
"/usr/lib/python98.zip", |
| 350 |
"/usr/lib/python9.8", |
| 351 |
"/usr/lib/python9.8/lib-dynload", |
| 352 |
], |
| 353 |
) |
| 354 |
actual = getpath(ns, expected) |
| 355 |
self.assertEqual(expected, actual) |
| 356 |
|
| 357 |
def test_venv_posix_without_home_key(self): |
| 358 |
ns = MockPosixNamespace( |
| 359 |
argv0="/venv/bin/python3", |
| 360 |
PREFIX="/usr", |
| 361 |
ENV_PATH="/usr/bin", |
| 362 |
) |
| 363 |
# Setup the bare minimum venv |
| 364 |
ns.add_known_xfile("/usr/bin/python3") |
| 365 |
ns.add_known_xfile("/venv/bin/python3") |
| 366 |
ns.add_known_link("/venv/bin/python3", "/usr/bin/python3") |
| 367 |
ns.add_known_file("/venv/pyvenv.cfg", [ |
| 368 |
# home = key intentionally omitted |
| 369 |
]) |
| 370 |
expected = dict( |
| 371 |
executable="/venv/bin/python3", |
| 372 |
prefix="/venv", |
| 373 |
base_prefix="/usr", |
| 374 |
) |
| 375 |
actual = getpath(ns, expected) |
| 376 |
self.assertEqual(expected, actual) |
| 377 |
|
| 378 |
def test_venv_changed_name_posix(self): |
| 379 |
"Test a venv layout on *nix." |
| 380 |
ns = MockPosixNamespace( |
| 381 |
argv0="python", |
| 382 |
PREFIX="/usr", |
| 383 |
ENV_PATH="/venv/bin:/usr/bin", |
| 384 |
) |
| 385 |
ns.add_known_xfile("/usr/bin/python3") |
| 386 |
ns.add_known_xfile("/venv/bin/python") |
| 387 |
ns.add_known_link("/venv/bin/python", "/usr/bin/python3") |
| 388 |
ns.add_known_file("/usr/lib/python9.8/os.py") |
| 389 |
ns.add_known_dir("/usr/lib/python9.8/lib-dynload") |
| 390 |
ns.add_known_file("/venv/pyvenv.cfg", [ |
| 391 |
r"home = /usr/bin" |
| 392 |
]) |
| 393 |
expected = dict( |
| 394 |
executable="/venv/bin/python", |
| 395 |
prefix="/venv", |
| 396 |
exec_prefix="/venv", |
| 397 |
base_executable="/usr/bin/python3", |
| 398 |
base_prefix="/usr", |
| 399 |
base_exec_prefix="/usr", |
| 400 |
module_search_paths_set=1, |
| 401 |
module_search_paths=[ |
| 402 |
"/usr/lib/python98.zip", |
| 403 |
"/usr/lib/python9.8", |
| 404 |
"/usr/lib/python9.8/lib-dynload", |
| 405 |
], |
| 406 |
) |
| 407 |
actual = getpath(ns, expected) |
| 408 |
self.assertEqual(expected, actual) |
| 409 |
|
| 410 |
def test_venv_non_installed_zip_path_posix(self): |
| 411 |
"Test a venv created from non-installed python has correct zip path.""" |
| 412 |
ns = MockPosixNamespace( |
| 413 |
argv0="/venv/bin/python", |
| 414 |
PREFIX="/usr", |
| 415 |
ENV_PATH="/venv/bin:/usr/bin", |
| 416 |
) |
| 417 |
ns.add_known_xfile("/path/to/non-installed/bin/python") |
| 418 |
ns.add_known_xfile("/venv/bin/python") |
| 419 |
ns.add_known_link("/venv/bin/python", |
| 420 |
"/path/to/non-installed/bin/python") |
| 421 |
ns.add_known_file("/path/to/non-installed/lib/python9.8/os.py") |
| 422 |
ns.add_known_dir("/path/to/non-installed/lib/python9.8/lib-dynload") |
| 423 |
ns.add_known_file("/venv/pyvenv.cfg", [ |
| 424 |
r"home = /path/to/non-installed" |
| 425 |
]) |
| 426 |
expected = dict( |
| 427 |
executable="/venv/bin/python", |
| 428 |
prefix="/venv", |
| 429 |
exec_prefix="/venv", |
| 430 |
base_executable="/path/to/non-installed/bin/python", |
| 431 |
base_prefix="/path/to/non-installed", |
| 432 |
base_exec_prefix="/path/to/non-installed", |
| 433 |
module_search_paths_set=1, |
| 434 |
module_search_paths=[ |
| 435 |
"/path/to/non-installed/lib/python98.zip", |
| 436 |
"/path/to/non-installed/lib/python9.8", |
| 437 |
"/path/to/non-installed/lib/python9.8/lib-dynload", |
| 438 |
], |
| 439 |
) |
| 440 |
actual = getpath(ns, expected) |
| 441 |
self.assertEqual(expected, actual) |
| 442 |
|
| 443 |
def test_venv_changed_name_copy_posix(self): |
| 444 |
"Test a venv --copies layout on *nix that lacks a distributed 'python'" |
| 445 |
ns = MockPosixNamespace( |
| 446 |
argv0="python", |
| 447 |
PREFIX="/usr", |
| 448 |
ENV_PATH="/venv/bin:/usr/bin", |
| 449 |
) |
| 450 |
ns.add_known_xfile("/usr/bin/python9") |
| 451 |
ns.add_known_xfile("/venv/bin/python") |
| 452 |
ns.add_known_file("/usr/lib/python9.8/os.py") |
| 453 |
ns.add_known_dir("/usr/lib/python9.8/lib-dynload") |
| 454 |
ns.add_known_file("/venv/pyvenv.cfg", [ |
| 455 |
r"home = /usr/bin" |
| 456 |
]) |
| 457 |
expected = dict( |
| 458 |
executable="/venv/bin/python", |
| 459 |
prefix="/venv", |
| 460 |
exec_prefix="/venv", |
| 461 |
base_executable="/usr/bin/python9", |
| 462 |
base_prefix="/usr", |
| 463 |
base_exec_prefix="/usr", |
| 464 |
module_search_paths_set=1, |
| 465 |
module_search_paths=[ |
| 466 |
"/usr/lib/python98.zip", |
| 467 |
"/usr/lib/python9.8", |
| 468 |
"/usr/lib/python9.8/lib-dynload", |
| 469 |
], |
| 470 |
) |
| 471 |
actual = getpath(ns, expected) |
| 472 |
self.assertEqual(expected, actual) |
| 473 |
|
| 474 |
def test_symlink_normal_posix(self): |
| 475 |
"Test a 'standard' install layout via symlink on *nix" |
| 476 |
ns = MockPosixNamespace( |
| 477 |
PREFIX="/usr", |
| 478 |
argv0="/linkfrom/python", |
| 479 |
) |
| 480 |
ns.add_known_xfile("/linkfrom/python") |
| 481 |
ns.add_known_xfile("/usr/bin/python") |
| 482 |
ns.add_known_link("/linkfrom/python", "/usr/bin/python") |
| 483 |
ns.add_known_file("/usr/lib/python9.8/os.py") |
| 484 |
ns.add_known_dir("/usr/lib/python9.8/lib-dynload") |
| 485 |
expected = dict( |
| 486 |
executable="/linkfrom/python", |
| 487 |
base_executable="/linkfrom/python", |
| 488 |
prefix="/usr", |
| 489 |
exec_prefix="/usr", |
| 490 |
module_search_paths_set=1, |
| 491 |
module_search_paths=[ |
| 492 |
"/usr/lib/python98.zip", |
| 493 |
"/usr/lib/python9.8", |
| 494 |
"/usr/lib/python9.8/lib-dynload", |
| 495 |
], |
| 496 |
) |
| 497 |
actual = getpath(ns, expected) |
| 498 |
self.assertEqual(expected, actual) |
| 499 |
|
| 500 |
def test_symlink_buildpath_posix(self): |
| 501 |
"""Test an in-build-tree layout on POSIX. |
| 502 |
|
| 503 |
This layout is discovered from the presence of pybuilddir.txt, which |
| 504 |
contains the relative path from the executable's directory to the |
| 505 |
platstdlib path. |
| 506 |
""" |
| 507 |
ns = MockPosixNamespace( |
| 508 |
argv0=r"/linkfrom/python", |
| 509 |
PREFIX="/usr/local", |
| 510 |
) |
| 511 |
ns.add_known_xfile("/linkfrom/python") |
| 512 |
ns.add_known_xfile("/home/cpython/python") |
| 513 |
ns.add_known_link("/linkfrom/python", "/home/cpython/python") |
| 514 |
ns.add_known_xfile("/usr/local/bin/python") |
| 515 |
ns.add_known_file("/home/cpython/pybuilddir.txt", ["build/lib.linux-x86_64-9.8"]) |
| 516 |
ns.add_known_file("/home/cpython/Lib/os.py") |
| 517 |
ns.add_known_dir("/home/cpython/lib-dynload") |
| 518 |
expected = dict( |
| 519 |
executable="/linkfrom/python", |
| 520 |
prefix="/usr/local", |
| 521 |
exec_prefix="/usr/local", |
| 522 |
base_executable="/linkfrom/python", |
| 523 |
build_prefix="/home/cpython", |
| 524 |
_is_python_build=1, |
| 525 |
module_search_paths_set=1, |
| 526 |
module_search_paths=[ |
| 527 |
"/usr/local/lib/python98.zip", |
| 528 |
"/home/cpython/Lib", |
| 529 |
"/home/cpython/build/lib.linux-x86_64-9.8", |
| 530 |
], |
| 531 |
) |
| 532 |
actual = getpath(ns, expected) |
| 533 |
self.assertEqual(expected, actual) |
| 534 |
|
| 535 |
def test_custom_platlibdir_posix(self): |
| 536 |
"Test an install with custom platlibdir on *nix" |
| 537 |
ns = MockPosixNamespace( |
| 538 |
PREFIX="/usr", |
| 539 |
argv0="/linkfrom/python", |
| 540 |
PLATLIBDIR="lib64", |
| 541 |
) |
| 542 |
ns.add_known_xfile("/usr/bin/python") |
| 543 |
ns.add_known_file("/usr/lib64/python9.8/os.py") |
| 544 |
ns.add_known_dir("/usr/lib64/python9.8/lib-dynload") |
| 545 |
expected = dict( |
| 546 |
executable="/linkfrom/python", |
| 547 |
base_executable="/linkfrom/python", |
| 548 |
prefix="/usr", |
| 549 |
exec_prefix="/usr", |
| 550 |
module_search_paths_set=1, |
| 551 |
module_search_paths=[ |
| 552 |
"/usr/lib64/python98.zip", |
| 553 |
"/usr/lib64/python9.8", |
| 554 |
"/usr/lib64/python9.8/lib-dynload", |
| 555 |
], |
| 556 |
) |
| 557 |
actual = getpath(ns, expected) |
| 558 |
self.assertEqual(expected, actual) |
| 559 |
|
| 560 |
def test_framework_macos(self): |
| 561 |
""" Test framework layout on macOS |
| 562 |
|
| 563 |
This layout is primarily detected using a compile-time option |
| 564 |
(WITH_NEXT_FRAMEWORK). |
| 565 |
""" |
| 566 |
ns = MockPosixNamespace( |
| 567 |
os_name="darwin", |
| 568 |
argv0="/Library/Frameworks/Python.framework/Versions/9.8/Resources/Python.app/Contents/MacOS/Python", |
| 569 |
WITH_NEXT_FRAMEWORK=1, |
| 570 |
PREFIX="/Library/Frameworks/Python.framework/Versions/9.8", |
| 571 |
EXEC_PREFIX="/Library/Frameworks/Python.framework/Versions/9.8", |
| 572 |
ENV___PYVENV_LAUNCHER__="/Library/Frameworks/Python.framework/Versions/9.8/bin/python9.8", |
| 573 |
real_executable="/Library/Frameworks/Python.framework/Versions/9.8/Resources/Python.app/Contents/MacOS/Python", |
| 574 |
library="/Library/Frameworks/Python.framework/Versions/9.8/Python", |
| 575 |
) |
| 576 |
ns.add_known_xfile("/Library/Frameworks/Python.framework/Versions/9.8/Resources/Python.app/Contents/MacOS/Python") |
| 577 |
ns.add_known_xfile("/Library/Frameworks/Python.framework/Versions/9.8/bin/python9.8") |
| 578 |
ns.add_known_dir("/Library/Frameworks/Python.framework/Versions/9.8/lib/python9.8/lib-dynload") |
| 579 |
ns.add_known_file("/Library/Frameworks/Python.framework/Versions/9.8/lib/python9.8/os.py") |
| 580 |
|
| 581 |
# This is definitely not the stdlib (see discussion in bpo-46890) |
| 582 |
#ns.add_known_file("/Library/Frameworks/lib/python98.zip") |
| 583 |
|
| 584 |
expected = dict( |
| 585 |
executable="/Library/Frameworks/Python.framework/Versions/9.8/bin/python9.8", |
| 586 |
prefix="/Library/Frameworks/Python.framework/Versions/9.8", |
| 587 |
exec_prefix="/Library/Frameworks/Python.framework/Versions/9.8", |
| 588 |
base_executable="/Library/Frameworks/Python.framework/Versions/9.8/bin/python9.8", |
| 589 |
base_prefix="/Library/Frameworks/Python.framework/Versions/9.8", |
| 590 |
base_exec_prefix="/Library/Frameworks/Python.framework/Versions/9.8", |
| 591 |
module_search_paths_set=1, |
| 592 |
module_search_paths=[ |
| 593 |
"/Library/Frameworks/Python.framework/Versions/9.8/lib/python98.zip", |
| 594 |
"/Library/Frameworks/Python.framework/Versions/9.8/lib/python9.8", |
| 595 |
"/Library/Frameworks/Python.framework/Versions/9.8/lib/python9.8/lib-dynload", |
| 596 |
], |
| 597 |
) |
| 598 |
actual = getpath(ns, expected) |
| 599 |
self.assertEqual(expected, actual) |
| 600 |
|
| 601 |
def test_alt_framework_macos(self): |
| 602 |
""" Test framework layout on macOS with alternate framework name |
| 603 |
|
| 604 |
``--with-framework-name=DebugPython`` |
| 605 |
|
| 606 |
This layout is primarily detected using a compile-time option |
| 607 |
(WITH_NEXT_FRAMEWORK). |
| 608 |
""" |
| 609 |
ns = MockPosixNamespace( |
| 610 |
argv0="/Library/Frameworks/DebugPython.framework/Versions/9.8/Resources/Python.app/Contents/MacOS/DebugPython", |
| 611 |
os_name="darwin", |
| 612 |
WITH_NEXT_FRAMEWORK=1, |
| 613 |
PREFIX="/Library/Frameworks/DebugPython.framework/Versions/9.8", |
| 614 |
EXEC_PREFIX="/Library/Frameworks/DebugPython.framework/Versions/9.8", |
| 615 |
ENV___PYVENV_LAUNCHER__="/Library/Frameworks/DebugPython.framework/Versions/9.8/bin/python9.8", |
| 616 |
real_executable="/Library/Frameworks/DebugPython.framework/Versions/9.8/Resources/Python.app/Contents/MacOS/DebugPython", |
| 617 |
library="/Library/Frameworks/DebugPython.framework/Versions/9.8/DebugPython", |
| 618 |
PYTHONPATH=None, |
| 619 |
ENV_PYTHONHOME=None, |
| 620 |
ENV_PYTHONEXECUTABLE=None, |
| 621 |
executable_dir=None, |
| 622 |
py_setpath=None, |
| 623 |
) |
| 624 |
ns.add_known_xfile("/Library/Frameworks/DebugPython.framework/Versions/9.8/Resources/Python.app/Contents/MacOS/DebugPython") |
| 625 |
ns.add_known_xfile("/Library/Frameworks/DebugPython.framework/Versions/9.8/bin/python9.8") |
| 626 |
ns.add_known_dir("/Library/Frameworks/DebugPython.framework/Versions/9.8/lib/python9.8/lib-dynload") |
| 627 |
ns.add_known_xfile("/Library/Frameworks/DebugPython.framework/Versions/9.8/lib/python9.8/os.py") |
| 628 |
|
| 629 |
# This is definitely not the stdlib (see discussion in bpo-46890) |
| 630 |
#ns.add_known_xfile("/Library/lib/python98.zip") |
| 631 |
expected = dict( |
| 632 |
executable="/Library/Frameworks/DebugPython.framework/Versions/9.8/bin/python9.8", |
| 633 |
prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8", |
| 634 |
exec_prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8", |
| 635 |
base_executable="/Library/Frameworks/DebugPython.framework/Versions/9.8/bin/python9.8", |
| 636 |
base_prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8", |
| 637 |
base_exec_prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8", |
| 638 |
module_search_paths_set=1, |
| 639 |
module_search_paths=[ |
| 640 |
"/Library/Frameworks/DebugPython.framework/Versions/9.8/lib/python98.zip", |
| 641 |
"/Library/Frameworks/DebugPython.framework/Versions/9.8/lib/python9.8", |
| 642 |
"/Library/Frameworks/DebugPython.framework/Versions/9.8/lib/python9.8/lib-dynload", |
| 643 |
], |
| 644 |
) |
| 645 |
actual = getpath(ns, expected) |
| 646 |
self.assertEqual(expected, actual) |
| 647 |
|
| 648 |
def test_venv_framework_macos(self): |
| 649 |
"""Test a venv layout on macOS using a framework build |
| 650 |
""" |
| 651 |
venv_path = "/tmp/workdir/venv" |
| 652 |
ns = MockPosixNamespace( |
| 653 |
os_name="darwin", |
| 654 |
argv0="/Library/Frameworks/Python.framework/Versions/9.8/Resources/Python.app/Contents/MacOS/Python", |
| 655 |
WITH_NEXT_FRAMEWORK=1, |
| 656 |
PREFIX="/Library/Frameworks/Python.framework/Versions/9.8", |
| 657 |
EXEC_PREFIX="/Library/Frameworks/Python.framework/Versions/9.8", |
| 658 |
ENV___PYVENV_LAUNCHER__=f"{venv_path}/bin/python", |
| 659 |
real_executable="/Library/Frameworks/Python.framework/Versions/9.8/Resources/Python.app/Contents/MacOS/Python", |
| 660 |
library="/Library/Frameworks/Python.framework/Versions/9.8/Python", |
| 661 |
) |
| 662 |
ns.add_known_dir(venv_path) |
| 663 |
ns.add_known_dir(f"{venv_path}/bin") |
| 664 |
ns.add_known_dir(f"{venv_path}/lib") |
| 665 |
ns.add_known_dir(f"{venv_path}/lib/python9.8") |
| 666 |
ns.add_known_xfile(f"{venv_path}/bin/python") |
| 667 |
ns.add_known_xfile("/Library/Frameworks/Python.framework/Versions/9.8/Resources/Python.app/Contents/MacOS/Python") |
| 668 |
ns.add_known_xfile("/Library/Frameworks/Python.framework/Versions/9.8/bin/python9.8") |
| 669 |
ns.add_known_dir("/Library/Frameworks/Python.framework/Versions/9.8/lib/python9.8/lib-dynload") |
| 670 |
ns.add_known_xfile("/Library/Frameworks/Python.framework/Versions/9.8/lib/python9.8/os.py") |
| 671 |
ns.add_known_file(f"{venv_path}/pyvenv.cfg", [ |
| 672 |
"home = /Library/Frameworks/Python.framework/Versions/9.8/bin" |
| 673 |
]) |
| 674 |
expected = dict( |
| 675 |
executable=f"{venv_path}/bin/python", |
| 676 |
prefix=venv_path, |
| 677 |
exec_prefix=venv_path, |
| 678 |
base_executable="/Library/Frameworks/Python.framework/Versions/9.8/bin/python9.8", |
| 679 |
base_prefix="/Library/Frameworks/Python.framework/Versions/9.8", |
| 680 |
base_exec_prefix="/Library/Frameworks/Python.framework/Versions/9.8", |
| 681 |
module_search_paths_set=1, |
| 682 |
module_search_paths=[ |
| 683 |
"/Library/Frameworks/Python.framework/Versions/9.8/lib/python98.zip", |
| 684 |
"/Library/Frameworks/Python.framework/Versions/9.8/lib/python9.8", |
| 685 |
"/Library/Frameworks/Python.framework/Versions/9.8/lib/python9.8/lib-dynload", |
| 686 |
], |
| 687 |
) |
| 688 |
actual = getpath(ns, expected) |
| 689 |
self.assertEqual(expected, actual) |
| 690 |
|
| 691 |
def test_venv_alt_framework_macos(self): |
| 692 |
"""Test a venv layout on macOS using a framework build |
| 693 |
|
| 694 |
``--with-framework-name=DebugPython`` |
| 695 |
""" |
| 696 |
venv_path = "/tmp/workdir/venv" |
| 697 |
ns = MockPosixNamespace( |
| 698 |
os_name="darwin", |
| 699 |
argv0="/Library/Frameworks/DebugPython.framework/Versions/9.8/Resources/Python.app/Contents/MacOS/DebugPython", |
| 700 |
WITH_NEXT_FRAMEWORK=1, |
| 701 |
PREFIX="/Library/Frameworks/DebugPython.framework/Versions/9.8", |
| 702 |
EXEC_PREFIX="/Library/Frameworks/DebugPython.framework/Versions/9.8", |
| 703 |
ENV___PYVENV_LAUNCHER__=f"{venv_path}/bin/python", |
| 704 |
real_executable="/Library/Frameworks/DebugPython.framework/Versions/9.8/Resources/Python.app/Contents/MacOS/DebugPython", |
| 705 |
library="/Library/Frameworks/DebugPython.framework/Versions/9.8/DebugPython", |
| 706 |
) |
| 707 |
ns.add_known_dir(venv_path) |
| 708 |
ns.add_known_dir(f"{venv_path}/bin") |
| 709 |
ns.add_known_dir(f"{venv_path}/lib") |
| 710 |
ns.add_known_dir(f"{venv_path}/lib/python9.8") |
| 711 |
ns.add_known_xfile(f"{venv_path}/bin/python") |
| 712 |
ns.add_known_xfile("/Library/Frameworks/DebugPython.framework/Versions/9.8/Resources/Python.app/Contents/MacOS/DebugPython") |
| 713 |
ns.add_known_xfile("/Library/Frameworks/DebugPython.framework/Versions/9.8/bin/python9.8") |
| 714 |
ns.add_known_dir("/Library/Frameworks/DebugPython.framework/Versions/9.8/lib/python9.8/lib-dynload") |
| 715 |
ns.add_known_xfile("/Library/Frameworks/DebugPython.framework/Versions/9.8/lib/python9.8/os.py") |
| 716 |
ns.add_known_file(f"{venv_path}/pyvenv.cfg", [ |
| 717 |
"home = /Library/Frameworks/DebugPython.framework/Versions/9.8/bin" |
| 718 |
]) |
| 719 |
expected = dict( |
| 720 |
executable=f"{venv_path}/bin/python", |
| 721 |
prefix=venv_path, |
| 722 |
exec_prefix=venv_path, |
| 723 |
base_executable="/Library/Frameworks/DebugPython.framework/Versions/9.8/bin/python9.8", |
| 724 |
base_prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8", |
| 725 |
base_exec_prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8", |
| 726 |
module_search_paths_set=1, |
| 727 |
module_search_paths=[ |
| 728 |
"/Library/Frameworks/DebugPython.framework/Versions/9.8/lib/python98.zip", |
| 729 |
"/Library/Frameworks/DebugPython.framework/Versions/9.8/lib/python9.8", |
| 730 |
"/Library/Frameworks/DebugPython.framework/Versions/9.8/lib/python9.8/lib-dynload", |
| 731 |
], |
| 732 |
) |
| 733 |
actual = getpath(ns, expected) |
| 734 |
self.assertEqual(expected, actual) |
| 735 |
|
| 736 |
def test_venv_macos(self): |
| 737 |
"""Test a venv layout on macOS. |
| 738 |
|
| 739 |
This layout is discovered when 'executable' and 'real_executable' match, |
| 740 |
but $__PYVENV_LAUNCHER__ has been set to the original process. |
| 741 |
""" |
| 742 |
ns = MockPosixNamespace( |
| 743 |
os_name="darwin", |
| 744 |
argv0="/usr/bin/python", |
| 745 |
PREFIX="/usr", |
| 746 |
ENV___PYVENV_LAUNCHER__="/framework/Python9.8/python", |
| 747 |
real_executable="/usr/bin/python", |
| 748 |
) |
| 749 |
ns.add_known_xfile("/usr/bin/python") |
| 750 |
ns.add_known_xfile("/framework/Python9.8/python") |
| 751 |
ns.add_known_file("/usr/lib/python9.8/os.py") |
| 752 |
ns.add_known_dir("/usr/lib/python9.8/lib-dynload") |
| 753 |
ns.add_known_file("/framework/Python9.8/pyvenv.cfg", [ |
| 754 |
"home = /usr/bin" |
| 755 |
]) |
| 756 |
expected = dict( |
| 757 |
executable="/framework/Python9.8/python", |
| 758 |
prefix="/framework/Python9.8", |
| 759 |
exec_prefix="/framework/Python9.8", |
| 760 |
base_executable="/usr/bin/python", |
| 761 |
base_prefix="/usr", |
| 762 |
base_exec_prefix="/usr", |
| 763 |
module_search_paths_set=1, |
| 764 |
module_search_paths=[ |
| 765 |
"/usr/lib/python98.zip", |
| 766 |
"/usr/lib/python9.8", |
| 767 |
"/usr/lib/python9.8/lib-dynload", |
| 768 |
], |
| 769 |
) |
| 770 |
actual = getpath(ns, expected) |
| 771 |
self.assertEqual(expected, actual) |
| 772 |
|
| 773 |
def test_symlink_normal_macos(self): |
| 774 |
"Test a 'standard' install layout via symlink on macOS" |
| 775 |
ns = MockPosixNamespace( |
| 776 |
os_name="darwin", |
| 777 |
PREFIX="/usr", |
| 778 |
argv0="python", |
| 779 |
ENV_PATH="/linkfrom:/usr/bin", |
| 780 |
# real_executable on macOS matches the invocation path |
| 781 |
real_executable="/linkfrom/python", |
| 782 |
) |
| 783 |
ns.add_known_xfile("/linkfrom/python") |
| 784 |
ns.add_known_xfile("/usr/bin/python") |
| 785 |
ns.add_known_link("/linkfrom/python", "/usr/bin/python") |
| 786 |
ns.add_known_file("/usr/lib/python9.8/os.py") |
| 787 |
ns.add_known_dir("/usr/lib/python9.8/lib-dynload") |
| 788 |
expected = dict( |
| 789 |
executable="/linkfrom/python", |
| 790 |
base_executable="/linkfrom/python", |
| 791 |
prefix="/usr", |
| 792 |
exec_prefix="/usr", |
| 793 |
module_search_paths_set=1, |
| 794 |
module_search_paths=[ |
| 795 |
"/usr/lib/python98.zip", |
| 796 |
"/usr/lib/python9.8", |
| 797 |
"/usr/lib/python9.8/lib-dynload", |
| 798 |
], |
| 799 |
) |
| 800 |
actual = getpath(ns, expected) |
| 801 |
self.assertEqual(expected, actual) |
| 802 |
|
| 803 |
def test_symlink_buildpath_macos(self): |
| 804 |
"""Test an in-build-tree layout via symlink on macOS. |
| 805 |
|
| 806 |
This layout is discovered from the presence of pybuilddir.txt, which |
| 807 |
contains the relative path from the executable's directory to the |
| 808 |
platstdlib path. |
| 809 |
""" |
| 810 |
ns = MockPosixNamespace( |
| 811 |
os_name="darwin", |
| 812 |
argv0=r"python", |
| 813 |
ENV_PATH="/linkfrom:/usr/bin", |
| 814 |
PREFIX="/usr/local", |
| 815 |
# real_executable on macOS matches the invocation path |
| 816 |
real_executable="/linkfrom/python", |
| 817 |
) |
| 818 |
ns.add_known_xfile("/linkfrom/python") |
| 819 |
ns.add_known_xfile("/home/cpython/python") |
| 820 |
ns.add_known_link("/linkfrom/python", "/home/cpython/python") |
| 821 |
ns.add_known_xfile("/usr/local/bin/python") |
| 822 |
ns.add_known_file("/home/cpython/pybuilddir.txt", ["build/lib.macos-9.8"]) |
| 823 |
ns.add_known_file("/home/cpython/Lib/os.py") |
| 824 |
ns.add_known_dir("/home/cpython/lib-dynload") |
| 825 |
expected = dict( |
| 826 |
executable="/linkfrom/python", |
| 827 |
prefix="/usr/local", |
| 828 |
exec_prefix="/usr/local", |
| 829 |
base_executable="/linkfrom/python", |
| 830 |
build_prefix="/home/cpython", |
| 831 |
_is_python_build=1, |
| 832 |
module_search_paths_set=1, |
| 833 |
module_search_paths=[ |
| 834 |
"/usr/local/lib/python98.zip", |
| 835 |
"/home/cpython/Lib", |
| 836 |
"/home/cpython/build/lib.macos-9.8", |
| 837 |
], |
| 838 |
) |
| 839 |
actual = getpath(ns, expected) |
| 840 |
self.assertEqual(expected, actual) |
| 841 |
|
| 842 |
def test_explicitly_set_stdlib_dir(self): |
| 843 |
"""Test the explicitly set stdlib_dir in the config is respected.""" |
| 844 |
ns = MockPosixNamespace( |
| 845 |
PREFIX="/usr", |
| 846 |
argv0="python", |
| 847 |
ENV_PATH="/usr/bin", |
| 848 |
) |
| 849 |
ns["config"]["stdlib_dir"] = "/custom_stdlib_dir" |
| 850 |
expected = dict( |
| 851 |
stdlib_dir="/custom_stdlib_dir", |
| 852 |
) |
| 853 |
actual = getpath(ns, expected) |
| 854 |
self.assertEqual(expected, actual) |
| 855 |
|
| 856 |
def test_PYTHONHOME_in_venv(self): |
| 857 |
"Make sure prefix/exec_prefix still point to the venv if PYTHONHOME was used." |
| 858 |
ns = MockPosixNamespace( |
| 859 |
argv0="/venv/bin/python", |
| 860 |
PREFIX="/usr", |
| 861 |
ENV_PYTHONHOME="/pythonhome", |
| 862 |
) |
| 863 |
# Setup venv |
| 864 |
ns.add_known_xfile("/venv/bin/python") |
| 865 |
ns.add_known_file("/venv/pyvenv.cfg", [ |
| 866 |
r"home = /usr/bin" |
| 867 |
]) |
| 868 |
# Seutup PYTHONHOME |
| 869 |
ns.add_known_file("/pythonhome/lib/python9.8/os.py") |
| 870 |
ns.add_known_dir("/pythonhome/lib/python9.8/lib-dynload") |
| 871 |
|
| 872 |
expected = dict( |
| 873 |
executable="/venv/bin/python", |
| 874 |
prefix="/venv", |
| 875 |
exec_prefix="/venv", |
| 876 |
base_prefix="/pythonhome", |
| 877 |
base_exec_prefix="/pythonhome", |
| 878 |
module_search_paths_set=1, |
| 879 |
module_search_paths=[ |
| 880 |
"/pythonhome/lib/python98.zip", |
| 881 |
"/pythonhome/lib/python9.8", |
| 882 |
"/pythonhome/lib/python9.8/lib-dynload", |
| 883 |
], |
| 884 |
) |
| 885 |
actual = getpath(ns, expected) |
| 886 |
self.assertEqual(expected, actual) |
| 887 |
|
| 888 |
# ****************************************************************************** |
| 889 |
|
| 890 |
DEFAULT_NAMESPACE = dict( |
| 891 |
PREFIX="", |
| 892 |
EXEC_PREFIX="", |
| 893 |
PYTHONPATH="", |
| 894 |
VPATH="", |
| 895 |
PLATLIBDIR="", |
| 896 |
PYDEBUGEXT="", |
| 897 |
VERSION_MAJOR=9, # fixed version number for ease |
| 898 |
VERSION_MINOR=8, # of testing |
| 899 |
ABI_THREAD="", |
| 900 |
PYWINVER=None, |
| 901 |
EXE_SUFFIX=None, |
| 902 |
|
| 903 |
ENV_PATH="", |
| 904 |
ENV_PYTHONHOME="", |
| 905 |
ENV_PYTHONEXECUTABLE="", |
| 906 |
ENV___PYVENV_LAUNCHER__="", |
| 907 |
argv0="", |
| 908 |
py_setpath="", |
| 909 |
real_executable="", |
| 910 |
executable_dir="", |
| 911 |
library="", |
| 912 |
winreg=None, |
| 913 |
build_prefix=None, |
| 914 |
venv_prefix=None, |
| 915 |
) |
| 916 |
|
| 917 |
DEFAULT_CONFIG = dict( |
| 918 |
home=None, |
| 919 |
platlibdir=None, |
| 920 |
pythonpath=None, |
| 921 |
program_name=None, |
| 922 |
prefix=None, |
| 923 |
exec_prefix=None, |
| 924 |
base_prefix=None, |
| 925 |
base_exec_prefix=None, |
| 926 |
executable=None, |
| 927 |
base_executable="", |
| 928 |
stdlib_dir=None, |
| 929 |
platstdlib_dir=None, |
| 930 |
module_search_paths=None, |
| 931 |
module_search_paths_set=0, |
| 932 |
pythonpath_env=None, |
| 933 |
argv=None, |
| 934 |
orig_argv=None, |
| 935 |
|
| 936 |
isolated=0, |
| 937 |
use_environment=1, |
| 938 |
use_site=1, |
| 939 |
) |
| 940 |
|
| 941 |
class MockNTNamespace(dict): |
| 942 |
def __init__(self, *a, argv0=None, config=None, **kw): |
| 943 |
self.update(DEFAULT_NAMESPACE) |
| 944 |
self["config"] = DEFAULT_CONFIG.copy() |
| 945 |
self["os_name"] = "nt" |
| 946 |
self["PLATLIBDIR"] = "DLLs" |
| 947 |
self["PYWINVER"] = "9.8-XY" |
| 948 |
self["VPATH"] = r"..\.." |
| 949 |
super().__init__(*a, **kw) |
| 950 |
if argv0: |
| 951 |
self["config"]["orig_argv"] = [argv0] |
| 952 |
if config: |
| 953 |
self["config"].update(config) |
| 954 |
self._files = {} |
| 955 |
self._links = {} |
| 956 |
self._dirs = set() |
| 957 |
self._warnings = [] |
| 958 |
|
| 959 |
def add_known_file(self, path, lines=None): |
| 960 |
self._files[path.casefold()] = list(lines or ()) |
| 961 |
self.add_known_dir(path.rpartition("\\")[0]) |
| 962 |
|
| 963 |
def add_known_xfile(self, path): |
| 964 |
self.add_known_file(path) |
| 965 |
|
| 966 |
def add_known_link(self, path, target): |
| 967 |
self._links[path.casefold()] = target |
| 968 |
|
| 969 |
def add_known_dir(self, path): |
| 970 |
p = path.rstrip("\\").casefold() |
| 971 |
while p: |
| 972 |
self._dirs.add(p) |
| 973 |
p = p.rpartition("\\")[0] |
| 974 |
|
| 975 |
def __missing__(self, key): |
| 976 |
try: |
| 977 |
return getattr(self, key) |
| 978 |
except AttributeError: |
| 979 |
raise KeyError(key) from None |
| 980 |
|
| 981 |
def abspath(self, path): |
| 982 |
if self.isabs(path): |
| 983 |
return path |
| 984 |
return self.joinpath("C:\\Absolute", path) |
| 985 |
|
| 986 |
def basename(self, path): |
| 987 |
return path.rpartition("\\")[2] |
| 988 |
|
| 989 |
def dirname(self, path): |
| 990 |
name = path.rstrip("\\").rpartition("\\")[0] |
| 991 |
if name[1:] == ":": |
| 992 |
return name + "\\" |
| 993 |
return name |
| 994 |
|
| 995 |
def hassuffix(self, path, suffix): |
| 996 |
return path.casefold().endswith(suffix.casefold()) |
| 997 |
|
| 998 |
def isabs(self, path): |
| 999 |
return path[1:3] == ":\\" |
| 1000 |
|
| 1001 |
def isdir(self, path): |
| 1002 |
if verbose: |
| 1003 |
print("Check if", path, "is a dir") |
| 1004 |
return path.casefold() in self._dirs |
| 1005 |
|
| 1006 |
def isfile(self, path): |
| 1007 |
if verbose: |
| 1008 |
print("Check if", path, "is a file") |
| 1009 |
return path.casefold() in self._files |
| 1010 |
|
| 1011 |
def ismodule(self, path): |
| 1012 |
if verbose: |
| 1013 |
print("Check if", path, "is a module") |
| 1014 |
path = path.casefold() |
| 1015 |
return path in self._files and path.rpartition(".")[2] == "py".casefold() |
| 1016 |
|
| 1017 |
def isxfile(self, path): |
| 1018 |
if verbose: |
| 1019 |
print("Check if", path, "is a executable") |
| 1020 |
path = path.casefold() |
| 1021 |
return path in self._files and path.rpartition(".")[2] == "exe".casefold() |
| 1022 |
|
| 1023 |
def joinpath(self, *path): |
| 1024 |
return ntpath.normpath(ntpath.join(*path)) |
| 1025 |
|
| 1026 |
def readlines(self, path): |
| 1027 |
try: |
| 1028 |
return self._files[path.casefold()] |
| 1029 |
except KeyError: |
| 1030 |
raise FileNotFoundError(path) from None |
| 1031 |
|
| 1032 |
def realpath(self, path, _trail=None): |
| 1033 |
if verbose: |
| 1034 |
print("Read link from", path) |
| 1035 |
try: |
| 1036 |
link = self._links[path.casefold()] |
| 1037 |
except KeyError: |
| 1038 |
return path |
| 1039 |
if _trail is None: |
| 1040 |
_trail = set() |
| 1041 |
elif link.casefold() in _trail: |
| 1042 |
raise OSError("circular link") |
| 1043 |
_trail.add(link.casefold()) |
| 1044 |
return self.realpath(link, _trail) |
| 1045 |
|
| 1046 |
def warn(self, message): |
| 1047 |
self._warnings.append(message) |
| 1048 |
if verbose: |
| 1049 |
print(message) |
| 1050 |
|
| 1051 |
|
| 1052 |
class MockWinreg: |
| 1053 |
HKEY_LOCAL_MACHINE = "HKLM" |
| 1054 |
HKEY_CURRENT_USER = "HKCU" |
| 1055 |
|
| 1056 |
def __init__(self, keys): |
| 1057 |
self.keys = {k.casefold(): v for k, v in keys.items()} |
| 1058 |
self.open = {} |
| 1059 |
|
| 1060 |
def __repr__(self): |
| 1061 |
return "<MockWinreg>" |
| 1062 |
|
| 1063 |
def __eq__(self, other): |
| 1064 |
return isinstance(other, type(self)) |
| 1065 |
|
| 1066 |
def open_keys(self): |
| 1067 |
return list(self.open) |
| 1068 |
|
| 1069 |
def OpenKeyEx(self, hkey, subkey): |
| 1070 |
if verbose: |
| 1071 |
print(f"OpenKeyEx({hkey}, {subkey})") |
| 1072 |
key = f"{hkey}\\{subkey}".casefold() |
| 1073 |
if key in self.keys: |
| 1074 |
self.open[key] = self.open.get(key, 0) + 1 |
| 1075 |
return key |
| 1076 |
raise FileNotFoundError() |
| 1077 |
|
| 1078 |
def CloseKey(self, hkey): |
| 1079 |
if verbose: |
| 1080 |
print(f"CloseKey({hkey})") |
| 1081 |
hkey = hkey.casefold() |
| 1082 |
if hkey not in self.open: |
| 1083 |
raise RuntimeError("key is not open") |
| 1084 |
self.open[hkey] -= 1 |
| 1085 |
if not self.open[hkey]: |
| 1086 |
del self.open[hkey] |
| 1087 |
|
| 1088 |
def EnumKey(self, hkey, i): |
| 1089 |
if verbose: |
| 1090 |
print(f"EnumKey({hkey}, {i})") |
| 1091 |
hkey = hkey.casefold() |
| 1092 |
if hkey not in self.open: |
| 1093 |
raise RuntimeError("key is not open") |
| 1094 |
prefix = f'{hkey}\\' |
| 1095 |
subkeys = [k[len(prefix):] for k in sorted(self.keys) if k.startswith(prefix)] |
| 1096 |
subkeys[:] = [k for k in subkeys if '\\' not in k] |
| 1097 |
for j, n in enumerate(subkeys): |
| 1098 |
if j == i: |
| 1099 |
return n.removeprefix(prefix) |
| 1100 |
raise OSError("end of enumeration") |
| 1101 |
|
| 1102 |
def QueryValue(self, hkey, subkey): |
| 1103 |
if verbose: |
| 1104 |
print(f"QueryValue({hkey}, {subkey})") |
| 1105 |
hkey = hkey.casefold() |
| 1106 |
if hkey not in self.open: |
| 1107 |
raise RuntimeError("key is not open") |
| 1108 |
if subkey: |
| 1109 |
subkey = subkey.casefold() |
| 1110 |
hkey = f'{hkey}\\{subkey}' |
| 1111 |
try: |
| 1112 |
return self.keys[hkey] |
| 1113 |
except KeyError: |
| 1114 |
raise OSError() |
| 1115 |
|
| 1116 |
|
| 1117 |
class MockPosixNamespace(dict): |
| 1118 |
def __init__(self, *a, argv0=None, config=None, **kw): |
| 1119 |
self.update(DEFAULT_NAMESPACE) |
| 1120 |
self["config"] = DEFAULT_CONFIG.copy() |
| 1121 |
self["os_name"] = "posix" |
| 1122 |
self["PLATLIBDIR"] = "lib" |
| 1123 |
self["WITH_NEXT_FRAMEWORK"] = 0 |
| 1124 |
super().__init__(*a, **kw) |
| 1125 |
if argv0: |
| 1126 |
self["config"]["orig_argv"] = [argv0] |
| 1127 |
if config: |
| 1128 |
self["config"].update(config) |
| 1129 |
self._files = {} |
| 1130 |
self._xfiles = set() |
| 1131 |
self._links = {} |
| 1132 |
self._dirs = set() |
| 1133 |
self._warnings = [] |
| 1134 |
|
| 1135 |
def add_known_file(self, path, lines=None): |
| 1136 |
self._files[path] = list(lines or ()) |
| 1137 |
self.add_known_dir(path.rpartition("/")[0]) |
| 1138 |
|
| 1139 |
def add_known_xfile(self, path): |
| 1140 |
self.add_known_file(path) |
| 1141 |
self._xfiles.add(path) |
| 1142 |
|
| 1143 |
def add_known_link(self, path, target): |
| 1144 |
self._links[path] = target |
| 1145 |
|
| 1146 |
def add_known_dir(self, path): |
| 1147 |
p = path.rstrip("/") |
| 1148 |
while p: |
| 1149 |
self._dirs.add(p) |
| 1150 |
p = p.rpartition("/")[0] |
| 1151 |
|
| 1152 |
def __missing__(self, key): |
| 1153 |
try: |
| 1154 |
return getattr(self, key) |
| 1155 |
except AttributeError: |
| 1156 |
raise KeyError(key) from None |
| 1157 |
|
| 1158 |
def abspath(self, path): |
| 1159 |
if self.isabs(path): |
| 1160 |
return path |
| 1161 |
return self.joinpath("/Absolute", path) |
| 1162 |
|
| 1163 |
def basename(self, path): |
| 1164 |
return path.rpartition("/")[2] |
| 1165 |
|
| 1166 |
def dirname(self, path): |
| 1167 |
return path.rstrip("/").rpartition("/")[0] |
| 1168 |
|
| 1169 |
def hassuffix(self, path, suffix): |
| 1170 |
return path.endswith(suffix) |
| 1171 |
|
| 1172 |
def isabs(self, path): |
| 1173 |
return path[0:1] == "/" |
| 1174 |
|
| 1175 |
def isdir(self, path): |
| 1176 |
if verbose: |
| 1177 |
print("Check if", path, "is a dir") |
| 1178 |
return path in self._dirs |
| 1179 |
|
| 1180 |
def isfile(self, path): |
| 1181 |
if verbose: |
| 1182 |
print("Check if", path, "is a file") |
| 1183 |
return path in self._files |
| 1184 |
|
| 1185 |
def ismodule(self, path): |
| 1186 |
if verbose: |
| 1187 |
print("Check if", path, "is a module") |
| 1188 |
return path in self._files and path.rpartition(".")[2] == "py" |
| 1189 |
|
| 1190 |
def isxfile(self, path): |
| 1191 |
if verbose: |
| 1192 |
print("Check if", path, "is an xfile") |
| 1193 |
return path in self._xfiles |
| 1194 |
|
| 1195 |
def joinpath(self, *path): |
| 1196 |
return posixpath.normpath(posixpath.join(*path)) |
| 1197 |
|
| 1198 |
def readlines(self, path): |
| 1199 |
try: |
| 1200 |
return self._files[path] |
| 1201 |
except KeyError: |
| 1202 |
raise FileNotFoundError(path) from None |
| 1203 |
|
| 1204 |
def realpath(self, path, _trail=None): |
| 1205 |
if verbose: |
| 1206 |
print("Read link from", path) |
| 1207 |
try: |
| 1208 |
link = self._links[path] |
| 1209 |
except KeyError: |
| 1210 |
return path |
| 1211 |
if _trail is None: |
| 1212 |
_trail = set() |
| 1213 |
elif link in _trail: |
| 1214 |
raise OSError("circular link") |
| 1215 |
_trail.add(link) |
| 1216 |
return self.realpath(link, _trail) |
| 1217 |
|
| 1218 |
def warn(self, message): |
| 1219 |
self._warnings.append(message) |
| 1220 |
if verbose: |
| 1221 |
print(message) |
| 1222 |
|
| 1223 |
|
| 1224 |
def diff_dict(before, after, prefix="global"): |
| 1225 |
diff = [] |
| 1226 |
for k in sorted(before): |
| 1227 |
if k[:2] == "__": |
| 1228 |
continue |
| 1229 |
if k == "config": |
| 1230 |
diff_dict(before[k], after[k], prefix="config") |
| 1231 |
continue |
| 1232 |
if k in after and after[k] != before[k]: |
| 1233 |
diff.append((k, before[k], after[k])) |
| 1234 |
if not diff: |
| 1235 |
return |
| 1236 |
max_k = max(len(k) for k, _, _ in diff) |
| 1237 |
indent = " " * (len(prefix) + 1 + max_k) |
| 1238 |
if verbose: |
| 1239 |
for k, b, a in diff: |
| 1240 |
if b: |
| 1241 |
print("{}.{} -{!r}\n{} +{!r}".format(prefix, k.ljust(max_k), b, indent, a)) |
| 1242 |
else: |
| 1243 |
print("{}.{} +{!r}".format(prefix, k.ljust(max_k), a)) |
| 1244 |
|
| 1245 |
|
| 1246 |
def dump_dict(before, after, prefix="global"): |
| 1247 |
if not verbose or not after: |
| 1248 |
return |
| 1249 |
max_k = max(len(k) for k in after) |
| 1250 |
for k, v in sorted(after.items(), key=lambda i: i[0]): |
| 1251 |
if k[:2] == "__": |
| 1252 |
continue |
| 1253 |
if k == "config": |
| 1254 |
dump_dict(before[k], after[k], prefix="config") |
| 1255 |
continue |
| 1256 |
try: |
| 1257 |
if v != before[k]: |
| 1258 |
print("{}.{} {!r} (was {!r})".format(prefix, k.ljust(max_k), v, before[k])) |
| 1259 |
continue |
| 1260 |
except KeyError: |
| 1261 |
pass |
| 1262 |
print("{}.{} {!r}".format(prefix, k.ljust(max_k), v)) |
| 1263 |
|
| 1264 |
|
| 1265 |
def getpath(ns, keys): |
| 1266 |
before = copy.deepcopy(ns) |
| 1267 |
failed = True |
| 1268 |
try: |
| 1269 |
exec(SOURCE, ns) |
| 1270 |
failed = False |
| 1271 |
finally: |
| 1272 |
if failed: |
| 1273 |
dump_dict(before, ns) |
| 1274 |
else: |
| 1275 |
diff_dict(before, ns) |
| 1276 |
return { |
| 1277 |
k: ns['config'].get(k, ns.get(k, ...)) |
| 1278 |
for k in keys |
| 1279 |
} |