| 1 |
import collections.abc |
| 2 |
import contextlib |
| 3 |
import errno |
| 4 |
import logging |
| 5 |
import os |
| 6 |
import re |
| 7 |
import stat |
| 8 |
import string |
| 9 |
import sys |
| 10 |
import time |
| 11 |
import unittest |
| 12 |
import warnings |
| 13 |
|
| 14 |
from test import support |
| 15 |
|
| 16 |
|
| 17 |
# Filename used for testing |
| 18 |
TESTFN_ASCII = '@test' |
| 19 |
|
| 20 |
# Disambiguate TESTFN for parallel testing, while letting it remain a valid |
| 21 |
# module name. |
| 22 |
TESTFN_ASCII = "{}_{}_tmp".format(TESTFN_ASCII, os.getpid()) |
| 23 |
|
| 24 |
# TESTFN_UNICODE is a non-ascii filename |
| 25 |
TESTFN_UNICODE = TESTFN_ASCII + "-\xe0\xf2\u0258\u0141\u011f" |
| 26 |
if support.is_apple: |
| 27 |
# On Apple's VFS API file names are, by definition, canonically |
| 28 |
# decomposed Unicode, encoded using UTF-8. See QA1173: |
| 29 |
# http://developer.apple.com/mac/library/qa/qa2001/qa1173.html |
| 30 |
import unicodedata |
| 31 |
TESTFN_UNICODE = unicodedata.normalize('NFD', TESTFN_UNICODE) |
| 32 |
|
| 33 |
# TESTFN_UNENCODABLE is a filename (str type) that should *not* be able to be |
| 34 |
# encoded by the filesystem encoding (in strict mode). It can be None if we |
| 35 |
# cannot generate such filename. |
| 36 |
TESTFN_UNENCODABLE = None |
| 37 |
if os.name == 'nt': |
| 38 |
# skip win32s (0) or Windows 9x/ME (1) |
| 39 |
if sys.getwindowsversion().platform >= 2: |
| 40 |
# Different kinds of characters from various languages to minimize the |
| 41 |
# probability that the whole name is encodable to MBCS (issue #9819) |
| 42 |
TESTFN_UNENCODABLE = TESTFN_ASCII + "-\u5171\u0141\u2661\u0363\uDC80" |
| 43 |
try: |
| 44 |
TESTFN_UNENCODABLE.encode(sys.getfilesystemencoding()) |
| 45 |
except UnicodeEncodeError: |
| 46 |
pass |
| 47 |
else: |
| 48 |
print('WARNING: The filename %r CAN be encoded by the filesystem ' |
| 49 |
'encoding (%s). Unicode filename tests may not be effective' |
| 50 |
% (TESTFN_UNENCODABLE, sys.getfilesystemencoding())) |
| 51 |
TESTFN_UNENCODABLE = None |
| 52 |
# Apple and Emscripten deny unencodable filenames (invalid utf-8) |
| 53 |
elif not support.is_apple and sys.platform not in {"emscripten", "wasi"}: |
| 54 |
try: |
| 55 |
# ascii and utf-8 cannot encode the byte 0xff |
| 56 |
b'\xff'.decode(sys.getfilesystemencoding()) |
| 57 |
except UnicodeDecodeError: |
| 58 |
# 0xff will be encoded using the surrogate character u+DCFF |
| 59 |
TESTFN_UNENCODABLE = TESTFN_ASCII \ |
| 60 |
+ b'-\xff'.decode(sys.getfilesystemencoding(), 'surrogateescape') |
| 61 |
else: |
| 62 |
# File system encoding (eg. ISO-8859-* encodings) can encode |
| 63 |
# the byte 0xff. Skip some unicode filename tests. |
| 64 |
pass |
| 65 |
|
| 66 |
# FS_NONASCII: non-ASCII character encodable by os.fsencode(), |
| 67 |
# or an empty string if there is no such character. |
| 68 |
FS_NONASCII = '' |
| 69 |
for character in ( |
| 70 |
# First try printable and common characters to have a readable filename. |
| 71 |
# For each character, the encoding list are just example of encodings able |
| 72 |
# to encode the character (the list is not exhaustive). |
| 73 |
|
| 74 |
# U+00E6 (Latin Small Letter Ae): cp1252, iso-8859-1 |
| 75 |
'\u00E6', |
| 76 |
# U+0130 (Latin Capital Letter I With Dot Above): cp1254, iso8859_3 |
| 77 |
'\u0130', |
| 78 |
# U+0141 (Latin Capital Letter L With Stroke): cp1250, cp1257 |
| 79 |
'\u0141', |
| 80 |
# U+03C6 (Greek Small Letter Phi): cp1253 |
| 81 |
'\u03C6', |
| 82 |
# U+041A (Cyrillic Capital Letter Ka): cp1251 |
| 83 |
'\u041A', |
| 84 |
# U+05D0 (Hebrew Letter Alef): Encodable to cp424 |
| 85 |
'\u05D0', |
| 86 |
# U+060C (Arabic Comma): cp864, cp1006, iso8859_6, mac_arabic |
| 87 |
'\u060C', |
| 88 |
# U+062A (Arabic Letter Teh): cp720 |
| 89 |
'\u062A', |
| 90 |
# U+0E01 (Thai Character Ko Kai): cp874 |
| 91 |
'\u0E01', |
| 92 |
|
| 93 |
# Then try more "special" characters. "special" because they may be |
| 94 |
# interpreted or displayed differently depending on the exact locale |
| 95 |
# encoding and the font. |
| 96 |
|
| 97 |
# U+00A0 (No-Break Space) |
| 98 |
'\u00A0', |
| 99 |
# U+20AC (Euro Sign) |
| 100 |
'\u20AC', |
| 101 |
): |
| 102 |
try: |
| 103 |
# If Python is set up to use the legacy 'mbcs' in Windows, |
| 104 |
# 'replace' error mode is used, and encode() returns b'?' |
| 105 |
# for characters missing in the ANSI codepage |
| 106 |
if os.fsdecode(os.fsencode(character)) != character: |
| 107 |
raise UnicodeError |
| 108 |
except UnicodeError: |
| 109 |
pass |
| 110 |
else: |
| 111 |
FS_NONASCII = character |
| 112 |
break |
| 113 |
|
| 114 |
# Save the initial cwd |
| 115 |
SAVEDCWD = os.getcwd() |
| 116 |
|
| 117 |
# TESTFN_UNDECODABLE is a filename (bytes type) that should *not* be able to be |
| 118 |
# decoded from the filesystem encoding (in strict mode). It can be None if we |
| 119 |
# cannot generate such filename (ex: the latin1 encoding can decode any byte |
| 120 |
# sequence). On UNIX, TESTFN_UNDECODABLE can be decoded by os.fsdecode() thanks |
| 121 |
# to the surrogateescape error handler (PEP 383), but not from the filesystem |
| 122 |
# encoding in strict mode. |
| 123 |
TESTFN_UNDECODABLE = None |
| 124 |
for name in ( |
| 125 |
# b'\xff' is not decodable by os.fsdecode() with code page 932. Windows |
| 126 |
# accepts it to create a file or a directory, or don't accept to enter to |
| 127 |
# such directory (when the bytes name is used). So test b'\xe7' first: |
| 128 |
# it is not decodable from cp932. |
| 129 |
b'\xe7w\xf0', |
| 130 |
# undecodable from ASCII, UTF-8 |
| 131 |
b'\xff', |
| 132 |
# undecodable from iso8859-3, iso8859-6, iso8859-7, cp424, iso8859-8, cp856 |
| 133 |
# and cp857 |
| 134 |
b'\xae\xd5' |
| 135 |
# undecodable from UTF-8 (UNIX and Mac OS X) |
| 136 |
b'\xed\xb2\x80', b'\xed\xb4\x80', |
| 137 |
# undecodable from shift_jis, cp869, cp874, cp932, cp1250, cp1251, cp1252, |
| 138 |
# cp1253, cp1254, cp1255, cp1257, cp1258 |
| 139 |
b'\x81\x98', |
| 140 |
): |
| 141 |
try: |
| 142 |
name.decode(sys.getfilesystemencoding()) |
| 143 |
except UnicodeDecodeError: |
| 144 |
try: |
| 145 |
name.decode(sys.getfilesystemencoding(), |
| 146 |
sys.getfilesystemencodeerrors()) |
| 147 |
except UnicodeDecodeError: |
| 148 |
continue |
| 149 |
TESTFN_UNDECODABLE = os.fsencode(TESTFN_ASCII) + name |
| 150 |
break |
| 151 |
|
| 152 |
if FS_NONASCII: |
| 153 |
TESTFN_NONASCII = TESTFN_ASCII + FS_NONASCII |
| 154 |
else: |
| 155 |
TESTFN_NONASCII = None |
| 156 |
TESTFN = TESTFN_NONASCII or TESTFN_ASCII |
| 157 |
|
| 158 |
|
| 159 |
def make_bad_fd(): |
| 160 |
""" |
| 161 |
Create an invalid file descriptor by opening and closing a file and return |
| 162 |
its fd. |
| 163 |
""" |
| 164 |
file = open(TESTFN, "wb") |
| 165 |
try: |
| 166 |
return file.fileno() |
| 167 |
finally: |
| 168 |
file.close() |
| 169 |
unlink(TESTFN) |
| 170 |
|
| 171 |
|
| 172 |
_can_symlink = None |
| 173 |
|
| 174 |
|
| 175 |
def can_symlink(): |
| 176 |
global _can_symlink |
| 177 |
if _can_symlink is not None: |
| 178 |
return _can_symlink |
| 179 |
# WASI / wasmtime prevents symlinks with absolute paths, see man |
| 180 |
# openat2(2) RESOLVE_BENEATH. Almost all symlink tests use absolute |
| 181 |
# paths. Skip symlink tests on WASI for now. |
| 182 |
src = os.path.abspath(TESTFN) |
| 183 |
symlink_path = src + "can_symlink" |
| 184 |
try: |
| 185 |
os.symlink(src, symlink_path) |
| 186 |
can = True |
| 187 |
except (OSError, NotImplementedError, AttributeError): |
| 188 |
can = False |
| 189 |
else: |
| 190 |
os.remove(symlink_path) |
| 191 |
_can_symlink = can |
| 192 |
return can |
| 193 |
|
| 194 |
|
| 195 |
def skip_unless_symlink(test): |
| 196 |
"""Skip decorator for tests that require functional symlink""" |
| 197 |
ok = can_symlink() |
| 198 |
msg = "Requires functional symlink implementation" |
| 199 |
return test if ok else unittest.skip(msg)(test) |
| 200 |
|
| 201 |
|
| 202 |
_can_hardlink = None |
| 203 |
|
| 204 |
def can_hardlink(): |
| 205 |
global _can_hardlink |
| 206 |
if _can_hardlink is None: |
| 207 |
# Android blocks hard links using SELinux |
| 208 |
# (https://stackoverflow.com/q/32365690). |
| 209 |
_can_hardlink = hasattr(os, "link") and not support.is_android |
| 210 |
return _can_hardlink |
| 211 |
|
| 212 |
|
| 213 |
def skip_unless_hardlink(test): |
| 214 |
ok = can_hardlink() |
| 215 |
msg = "requires hardlink support" |
| 216 |
return test if ok else unittest.skip(msg)(test) |
| 217 |
|
| 218 |
|
| 219 |
_can_xattr = None |
| 220 |
|
| 221 |
|
| 222 |
def can_xattr(): |
| 223 |
import tempfile |
| 224 |
global _can_xattr |
| 225 |
if _can_xattr is not None: |
| 226 |
return _can_xattr |
| 227 |
if not hasattr(os, "setxattr"): |
| 228 |
can = False |
| 229 |
else: |
| 230 |
import platform |
| 231 |
tmp_dir = tempfile.mkdtemp() |
| 232 |
tmp_fp, tmp_name = tempfile.mkstemp(dir=tmp_dir) |
| 233 |
try: |
| 234 |
with open(TESTFN, "wb") as fp: |
| 235 |
try: |
| 236 |
# TESTFN & tempfile may use different file systems with |
| 237 |
# different capabilities |
| 238 |
os.setxattr(tmp_fp, b"user.test", b"") |
| 239 |
os.setxattr(tmp_name, b"trusted.foo", b"42") |
| 240 |
os.setxattr(fp.fileno(), b"user.test", b"") |
| 241 |
# Kernels < 2.6.39 don't respect setxattr flags. |
| 242 |
kernel_version = platform.release() |
| 243 |
m = re.match(r"2.6.(\d{1,2})", kernel_version) |
| 244 |
can = m is None or int(m.group(1)) >= 39 |
| 245 |
except OSError: |
| 246 |
can = False |
| 247 |
finally: |
| 248 |
unlink(TESTFN) |
| 249 |
unlink(tmp_name) |
| 250 |
rmdir(tmp_dir) |
| 251 |
_can_xattr = can |
| 252 |
return can |
| 253 |
|
| 254 |
|
| 255 |
def skip_unless_xattr(test): |
| 256 |
"""Skip decorator for tests that require functional extended attributes""" |
| 257 |
ok = can_xattr() |
| 258 |
msg = "no non-broken extended attribute support" |
| 259 |
return test if ok else unittest.skip(msg)(test) |
| 260 |
|
| 261 |
|
| 262 |
_can_chmod = None |
| 263 |
|
| 264 |
def can_chmod(): |
| 265 |
global _can_chmod |
| 266 |
if _can_chmod is not None: |
| 267 |
return _can_chmod |
| 268 |
if not hasattr(os, "chmod"): |
| 269 |
_can_chmod = False |
| 270 |
return _can_chmod |
| 271 |
try: |
| 272 |
with open(TESTFN, "wb") as f: |
| 273 |
try: |
| 274 |
os.chmod(TESTFN, 0o555) |
| 275 |
mode1 = os.stat(TESTFN).st_mode |
| 276 |
os.chmod(TESTFN, 0o777) |
| 277 |
mode2 = os.stat(TESTFN).st_mode |
| 278 |
except OSError as e: |
| 279 |
can = False |
| 280 |
else: |
| 281 |
can = stat.S_IMODE(mode1) != stat.S_IMODE(mode2) |
| 282 |
finally: |
| 283 |
unlink(TESTFN) |
| 284 |
_can_chmod = can |
| 285 |
return can |
| 286 |
|
| 287 |
|
| 288 |
def skip_unless_working_chmod(test): |
| 289 |
"""Skip tests that require working os.chmod() |
| 290 |
|
| 291 |
WASI SDK 15.0 cannot change file mode bits. |
| 292 |
""" |
| 293 |
ok = can_chmod() |
| 294 |
msg = "requires working os.chmod()" |
| 295 |
return test if ok else unittest.skip(msg)(test) |
| 296 |
|
| 297 |
|
| 298 |
@contextlib.contextmanager |
| 299 |
def save_mode(path, *, quiet=False): |
| 300 |
"""Context manager that restores the mode (permissions) of *path* on exit. |
| 301 |
|
| 302 |
Arguments: |
| 303 |
|
| 304 |
path: Path of the file to restore the mode of. |
| 305 |
|
| 306 |
quiet: if False (the default), the context manager raises an exception |
| 307 |
on error. Otherwise, it issues only a warning and keeps the current |
| 308 |
working directory the same. |
| 309 |
|
| 310 |
""" |
| 311 |
saved_mode = os.stat(path) |
| 312 |
try: |
| 313 |
yield |
| 314 |
finally: |
| 315 |
try: |
| 316 |
os.chmod(path, saved_mode.st_mode) |
| 317 |
except OSError as exc: |
| 318 |
if not quiet: |
| 319 |
raise |
| 320 |
warnings.warn(f'tests may fail, unable to restore the mode of ' |
| 321 |
f'{path!r} to {saved_mode.st_mode}: {exc}', |
| 322 |
RuntimeWarning, stacklevel=3) |
| 323 |
|
| 324 |
|
| 325 |
# Check whether the current effective user has the capability to override |
| 326 |
# DAC (discretionary access control). Typically user root is able to |
| 327 |
# bypass file read, write, and execute permission checks. The capability |
| 328 |
# is independent of the effective user. See capabilities(7). |
| 329 |
_can_dac_override = None |
| 330 |
|
| 331 |
def can_dac_override(): |
| 332 |
global _can_dac_override |
| 333 |
|
| 334 |
if not can_chmod(): |
| 335 |
_can_dac_override = False |
| 336 |
if _can_dac_override is not None: |
| 337 |
return _can_dac_override |
| 338 |
|
| 339 |
try: |
| 340 |
with open(TESTFN, "wb") as f: |
| 341 |
os.chmod(TESTFN, 0o400) |
| 342 |
try: |
| 343 |
with open(TESTFN, "wb"): |
| 344 |
pass |
| 345 |
except OSError: |
| 346 |
_can_dac_override = False |
| 347 |
else: |
| 348 |
_can_dac_override = True |
| 349 |
finally: |
| 350 |
try: |
| 351 |
os.chmod(TESTFN, 0o700) |
| 352 |
except OSError: |
| 353 |
pass |
| 354 |
unlink(TESTFN) |
| 355 |
|
| 356 |
return _can_dac_override |
| 357 |
|
| 358 |
|
| 359 |
def skip_if_dac_override(test): |
| 360 |
ok = not can_dac_override() |
| 361 |
msg = "incompatible with CAP_DAC_OVERRIDE" |
| 362 |
return test if ok else unittest.skip(msg)(test) |
| 363 |
|
| 364 |
|
| 365 |
def skip_unless_dac_override(test): |
| 366 |
ok = can_dac_override() |
| 367 |
msg = "requires CAP_DAC_OVERRIDE" |
| 368 |
return test if ok else unittest.skip(msg)(test) |
| 369 |
|
| 370 |
|
| 371 |
def unlink(filename): |
| 372 |
try: |
| 373 |
_unlink(filename) |
| 374 |
except (FileNotFoundError, NotADirectoryError): |
| 375 |
pass |
| 376 |
|
| 377 |
|
| 378 |
if sys.platform.startswith("win"): |
| 379 |
def _waitfor(func, pathname, waitall=False): |
| 380 |
# Perform the operation |
| 381 |
func(pathname) |
| 382 |
# Now setup the wait loop |
| 383 |
if waitall: |
| 384 |
dirname = pathname |
| 385 |
else: |
| 386 |
dirname, name = os.path.split(pathname) |
| 387 |
dirname = dirname or '.' |
| 388 |
# Check for `pathname` to be removed from the filesystem. |
| 389 |
# The exponential backoff of the timeout amounts to a total |
| 390 |
# of ~1 second after which the deletion is probably an error |
| 391 |
# anyway. |
| 392 |
# Testing on an [email protected] shows that usually only 1 iteration is |
| 393 |
# required when contention occurs. |
| 394 |
timeout = 0.001 |
| 395 |
while timeout < 1.0: |
| 396 |
# Note we are only testing for the existence of the file(s) in |
| 397 |
# the contents of the directory regardless of any security or |
| 398 |
# access rights. If we have made it this far, we have sufficient |
| 399 |
# permissions to do that much using Python's equivalent of the |
| 400 |
# Windows API FindFirstFile. |
| 401 |
# Other Windows APIs can fail or give incorrect results when |
| 402 |
# dealing with files that are pending deletion. |
| 403 |
L = os.listdir(dirname) |
| 404 |
if not (L if waitall else name in L): |
| 405 |
return |
| 406 |
# Increase the timeout and try again |
| 407 |
time.sleep(timeout) |
| 408 |
timeout *= 2 |
| 409 |
logging.getLogger(__name__).warning( |
| 410 |
'tests may fail, delete still pending for %s', |
| 411 |
pathname, |
| 412 |
stack_info=True, |
| 413 |
stacklevel=4, |
| 414 |
) |
| 415 |
|
| 416 |
def _unlink(filename): |
| 417 |
_waitfor(os.unlink, filename) |
| 418 |
|
| 419 |
def _rmdir(dirname): |
| 420 |
_waitfor(os.rmdir, dirname) |
| 421 |
|
| 422 |
def _rmtree(path): |
| 423 |
from test.support import _force_run |
| 424 |
|
| 425 |
def _rmtree_inner(path): |
| 426 |
for name in _force_run(path, os.listdir, path): |
| 427 |
fullname = os.path.join(path, name) |
| 428 |
try: |
| 429 |
mode = os.lstat(fullname).st_mode |
| 430 |
except OSError as exc: |
| 431 |
print("support.rmtree(): os.lstat(%r) failed with %s" |
| 432 |
% (fullname, exc), |
| 433 |
file=sys.__stderr__) |
| 434 |
mode = 0 |
| 435 |
if stat.S_ISDIR(mode): |
| 436 |
_waitfor(_rmtree_inner, fullname, waitall=True) |
| 437 |
_force_run(fullname, os.rmdir, fullname) |
| 438 |
else: |
| 439 |
_force_run(fullname, os.unlink, fullname) |
| 440 |
_waitfor(_rmtree_inner, path, waitall=True) |
| 441 |
_waitfor(lambda p: _force_run(p, os.rmdir, p), path) |
| 442 |
|
| 443 |
def _longpath(path): |
| 444 |
try: |
| 445 |
import ctypes |
| 446 |
except ImportError: |
| 447 |
# No ctypes means we can't expands paths. |
| 448 |
pass |
| 449 |
else: |
| 450 |
buffer = ctypes.create_unicode_buffer(len(path) * 2) |
| 451 |
length = ctypes.windll.kernel32.GetLongPathNameW(path, buffer, |
| 452 |
len(buffer)) |
| 453 |
if length: |
| 454 |
return buffer[:length] |
| 455 |
return path |
| 456 |
else: |
| 457 |
_unlink = os.unlink |
| 458 |
_rmdir = os.rmdir |
| 459 |
|
| 460 |
def _rmtree(path): |
| 461 |
import shutil |
| 462 |
try: |
| 463 |
shutil.rmtree(path) |
| 464 |
return |
| 465 |
except OSError: |
| 466 |
pass |
| 467 |
|
| 468 |
def _rmtree_inner(path): |
| 469 |
from test.support import _force_run |
| 470 |
for name in _force_run(path, os.listdir, path): |
| 471 |
fullname = os.path.join(path, name) |
| 472 |
try: |
| 473 |
mode = os.lstat(fullname).st_mode |
| 474 |
except OSError: |
| 475 |
mode = 0 |
| 476 |
if stat.S_ISDIR(mode): |
| 477 |
_rmtree_inner(fullname) |
| 478 |
_force_run(path, os.rmdir, fullname) |
| 479 |
else: |
| 480 |
_force_run(path, os.unlink, fullname) |
| 481 |
_rmtree_inner(path) |
| 482 |
os.rmdir(path) |
| 483 |
|
| 484 |
def _longpath(path): |
| 485 |
return path |
| 486 |
|
| 487 |
|
| 488 |
def rmdir(dirname): |
| 489 |
try: |
| 490 |
_rmdir(dirname) |
| 491 |
except FileNotFoundError: |
| 492 |
pass |
| 493 |
|
| 494 |
|
| 495 |
def rmtree(path): |
| 496 |
try: |
| 497 |
_rmtree(path) |
| 498 |
except FileNotFoundError: |
| 499 |
pass |
| 500 |
|
| 501 |
|
| 502 |
@contextlib.contextmanager |
| 503 |
def temp_dir(path=None, quiet=False): |
| 504 |
"""Return a context manager that creates a temporary directory. |
| 505 |
|
| 506 |
Arguments: |
| 507 |
|
| 508 |
path: the directory to create temporarily. If omitted or None, |
| 509 |
defaults to creating a temporary directory using tempfile.mkdtemp. |
| 510 |
|
| 511 |
quiet: if False (the default), the context manager raises an exception |
| 512 |
on error. Otherwise, if the path is specified and cannot be |
| 513 |
created, only a warning is issued. |
| 514 |
|
| 515 |
""" |
| 516 |
import tempfile |
| 517 |
dir_created = False |
| 518 |
if path is None: |
| 519 |
path = tempfile.mkdtemp() |
| 520 |
dir_created = True |
| 521 |
path = os.path.realpath(path) |
| 522 |
else: |
| 523 |
try: |
| 524 |
os.mkdir(path) |
| 525 |
dir_created = True |
| 526 |
except OSError as exc: |
| 527 |
if not quiet: |
| 528 |
raise |
| 529 |
logging.getLogger(__name__).warning( |
| 530 |
"tests may fail, unable to create temporary directory %r: %s", |
| 531 |
path, |
| 532 |
exc, |
| 533 |
exc_info=exc, |
| 534 |
stack_info=True, |
| 535 |
stacklevel=3, |
| 536 |
) |
| 537 |
if dir_created: |
| 538 |
pid = os.getpid() |
| 539 |
try: |
| 540 |
yield path |
| 541 |
finally: |
| 542 |
# In case the process forks, let only the parent remove the |
| 543 |
# directory. The child has a different process id. (bpo-30028) |
| 544 |
if dir_created and pid == os.getpid(): |
| 545 |
rmtree(path) |
| 546 |
|
| 547 |
|
| 548 |
@contextlib.contextmanager |
| 549 |
def change_cwd(path, quiet=False): |
| 550 |
"""Return a context manager that changes the current working directory. |
| 551 |
|
| 552 |
Arguments: |
| 553 |
|
| 554 |
path: the directory to use as the temporary current working directory. |
| 555 |
|
| 556 |
quiet: if False (the default), the context manager raises an exception |
| 557 |
on error. Otherwise, it issues only a warning and keeps the current |
| 558 |
working directory the same. |
| 559 |
|
| 560 |
""" |
| 561 |
saved_dir = os.getcwd() |
| 562 |
try: |
| 563 |
os.chdir(os.path.realpath(path)) |
| 564 |
except OSError as exc: |
| 565 |
if not quiet: |
| 566 |
raise |
| 567 |
logging.getLogger(__name__).warning( |
| 568 |
'tests may fail, unable to change the current working directory ' |
| 569 |
'to %r: %s', |
| 570 |
path, |
| 571 |
exc, |
| 572 |
exc_info=exc, |
| 573 |
stack_info=True, |
| 574 |
stacklevel=3, |
| 575 |
) |
| 576 |
try: |
| 577 |
yield os.getcwd() |
| 578 |
finally: |
| 579 |
os.chdir(saved_dir) |
| 580 |
|
| 581 |
|
| 582 |
@contextlib.contextmanager |
| 583 |
def temp_cwd(name='tempcwd', quiet=False): |
| 584 |
""" |
| 585 |
Context manager that temporarily creates and changes the CWD. |
| 586 |
|
| 587 |
The function temporarily changes the current working directory |
| 588 |
after creating a temporary directory in the current directory with |
| 589 |
name *name*. If *name* is None, the temporary directory is |
| 590 |
created using tempfile.mkdtemp. |
| 591 |
|
| 592 |
If *quiet* is False (default) and it is not possible to |
| 593 |
create or change the CWD, an error is raised. If *quiet* is True, |
| 594 |
only a warning is raised and the original CWD is used. |
| 595 |
|
| 596 |
""" |
| 597 |
with temp_dir(path=name, quiet=quiet) as temp_path: |
| 598 |
with change_cwd(temp_path, quiet=quiet) as cwd_dir: |
| 599 |
yield cwd_dir |
| 600 |
|
| 601 |
|
| 602 |
def create_empty_file(filename): |
| 603 |
"""Create an empty file. If the file already exists, truncate it.""" |
| 604 |
fd = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) |
| 605 |
os.close(fd) |
| 606 |
|
| 607 |
|
| 608 |
@contextlib.contextmanager |
| 609 |
def open_dir_fd(path): |
| 610 |
"""Open a file descriptor to a directory.""" |
| 611 |
assert os.path.isdir(path) |
| 612 |
flags = os.O_RDONLY |
| 613 |
if hasattr(os, "O_DIRECTORY"): |
| 614 |
flags |= os.O_DIRECTORY |
| 615 |
dir_fd = os.open(path, flags) |
| 616 |
try: |
| 617 |
yield dir_fd |
| 618 |
finally: |
| 619 |
os.close(dir_fd) |
| 620 |
|
| 621 |
|
| 622 |
def fs_is_case_insensitive(directory): |
| 623 |
"""Detects if the file system for the specified directory |
| 624 |
is case-insensitive.""" |
| 625 |
import tempfile |
| 626 |
with tempfile.NamedTemporaryFile(dir=directory) as base: |
| 627 |
base_path = base.name |
| 628 |
case_path = base_path.upper() |
| 629 |
if case_path == base_path: |
| 630 |
case_path = base_path.lower() |
| 631 |
try: |
| 632 |
return os.path.samefile(base_path, case_path) |
| 633 |
except FileNotFoundError: |
| 634 |
return False |
| 635 |
|
| 636 |
|
| 637 |
class FakePath: |
| 638 |
"""Simple implementation of the path protocol. |
| 639 |
""" |
| 640 |
def __init__(self, path): |
| 641 |
self.path = path |
| 642 |
|
| 643 |
def __repr__(self): |
| 644 |
return f'<FakePath {self.path!r}>' |
| 645 |
|
| 646 |
def __fspath__(self): |
| 647 |
if (isinstance(self.path, BaseException) or |
| 648 |
isinstance(self.path, type) and |
| 649 |
issubclass(self.path, BaseException)): |
| 650 |
raise self.path |
| 651 |
else: |
| 652 |
return self.path |
| 653 |
|
| 654 |
|
| 655 |
def fd_count(): |
| 656 |
"""Count the number of open file descriptors. |
| 657 |
""" |
| 658 |
if sys.platform.startswith(('linux', 'android', 'freebsd', 'emscripten')): |
| 659 |
fd_path = "/proc/self/fd" |
| 660 |
elif support.is_apple: |
| 661 |
fd_path = "/dev/fd" |
| 662 |
else: |
| 663 |
fd_path = None |
| 664 |
|
| 665 |
if fd_path is not None: |
| 666 |
try: |
| 667 |
names = os.listdir(fd_path) |
| 668 |
# Subtract one because listdir() internally opens a file |
| 669 |
# descriptor to list the content of the directory. |
| 670 |
return len(names) - 1 |
| 671 |
except FileNotFoundError: |
| 672 |
pass |
| 673 |
|
| 674 |
MAXFD = 256 |
| 675 |
if hasattr(os, 'sysconf'): |
| 676 |
try: |
| 677 |
MAXFD = os.sysconf("SC_OPEN_MAX") |
| 678 |
except OSError: |
| 679 |
pass |
| 680 |
|
| 681 |
old_modes = None |
| 682 |
if sys.platform == 'win32': |
| 683 |
# bpo-25306, bpo-31009: Call CrtSetReportMode() to not kill the process |
| 684 |
# on invalid file descriptor if Python is compiled in debug mode |
| 685 |
try: |
| 686 |
import msvcrt |
| 687 |
msvcrt.CrtSetReportMode |
| 688 |
except (AttributeError, ImportError): |
| 689 |
# no msvcrt or a release build |
| 690 |
pass |
| 691 |
else: |
| 692 |
old_modes = {} |
| 693 |
for report_type in (msvcrt.CRT_WARN, |
| 694 |
msvcrt.CRT_ERROR, |
| 695 |
msvcrt.CRT_ASSERT): |
| 696 |
old_modes[report_type] = msvcrt.CrtSetReportMode(report_type, |
| 697 |
0) |
| 698 |
|
| 699 |
try: |
| 700 |
count = 0 |
| 701 |
for fd in range(MAXFD): |
| 702 |
try: |
| 703 |
# Prefer dup() over fstat(). fstat() can require input/output |
| 704 |
# whereas dup() doesn't. |
| 705 |
fd2 = os.dup(fd) |
| 706 |
except OSError as e: |
| 707 |
if e.errno != errno.EBADF: |
| 708 |
raise |
| 709 |
else: |
| 710 |
os.close(fd2) |
| 711 |
count += 1 |
| 712 |
finally: |
| 713 |
if old_modes is not None: |
| 714 |
for report_type in (msvcrt.CRT_WARN, |
| 715 |
msvcrt.CRT_ERROR, |
| 716 |
msvcrt.CRT_ASSERT): |
| 717 |
msvcrt.CrtSetReportMode(report_type, old_modes[report_type]) |
| 718 |
|
| 719 |
return count |
| 720 |
|
| 721 |
|
| 722 |
if hasattr(os, "umask"): |
| 723 |
@contextlib.contextmanager |
| 724 |
def temp_umask(umask): |
| 725 |
"""Context manager that temporarily sets the process umask.""" |
| 726 |
oldmask = os.umask(umask) |
| 727 |
try: |
| 728 |
yield |
| 729 |
finally: |
| 730 |
os.umask(oldmask) |
| 731 |
else: |
| 732 |
@contextlib.contextmanager |
| 733 |
def temp_umask(umask): |
| 734 |
"""no-op on platforms without umask()""" |
| 735 |
yield |
| 736 |
|
| 737 |
|
| 738 |
class EnvironmentVarGuard(collections.abc.MutableMapping): |
| 739 |
"""Class to help protect the environment variable properly. |
| 740 |
|
| 741 |
Can be used as a context manager. |
| 742 |
""" |
| 743 |
|
| 744 |
def __init__(self): |
| 745 |
self._environ = os.environ |
| 746 |
self._changed = {} |
| 747 |
|
| 748 |
def __getitem__(self, envvar): |
| 749 |
return self._environ[envvar] |
| 750 |
|
| 751 |
def __setitem__(self, envvar, value): |
| 752 |
# Remember the initial value on the first access |
| 753 |
if envvar not in self._changed: |
| 754 |
self._changed[envvar] = self._environ.get(envvar) |
| 755 |
self._environ[envvar] = value |
| 756 |
|
| 757 |
def __delitem__(self, envvar): |
| 758 |
# Remember the initial value on the first access |
| 759 |
if envvar not in self._changed: |
| 760 |
self._changed[envvar] = self._environ.get(envvar) |
| 761 |
if envvar in self._environ: |
| 762 |
del self._environ[envvar] |
| 763 |
|
| 764 |
def keys(self): |
| 765 |
return self._environ.keys() |
| 766 |
|
| 767 |
def __iter__(self): |
| 768 |
return iter(self._environ) |
| 769 |
|
| 770 |
def __len__(self): |
| 771 |
return len(self._environ) |
| 772 |
|
| 773 |
def set(self, envvar, value): |
| 774 |
self[envvar] = value |
| 775 |
|
| 776 |
def unset(self, envvar, /, *envvars): |
| 777 |
"""Unset one or more environment variables.""" |
| 778 |
for ev in (envvar, *envvars): |
| 779 |
del self[ev] |
| 780 |
|
| 781 |
def copy(self): |
| 782 |
# We do what os.environ.copy() does. |
| 783 |
return dict(self) |
| 784 |
|
| 785 |
def __enter__(self): |
| 786 |
return self |
| 787 |
|
| 788 |
def __exit__(self, *ignore_exc): |
| 789 |
for (k, v) in self._changed.items(): |
| 790 |
if v is None: |
| 791 |
if k in self._environ: |
| 792 |
del self._environ[k] |
| 793 |
else: |
| 794 |
self._environ[k] = v |
| 795 |
os.environ = self._environ |
| 796 |
|
| 797 |
|
| 798 |
try: |
| 799 |
if support.MS_WINDOWS: |
| 800 |
import ctypes |
| 801 |
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) |
| 802 |
|
| 803 |
ERROR_FILE_NOT_FOUND = 2 |
| 804 |
DDD_REMOVE_DEFINITION = 2 |
| 805 |
DDD_EXACT_MATCH_ON_REMOVE = 4 |
| 806 |
DDD_NO_BROADCAST_SYSTEM = 8 |
| 807 |
else: |
| 808 |
raise AttributeError |
| 809 |
except (ImportError, AttributeError): |
| 810 |
def subst_drive(path): |
| 811 |
raise unittest.SkipTest('ctypes or kernel32 is not available') |
| 812 |
else: |
| 813 |
@contextlib.contextmanager |
| 814 |
def subst_drive(path): |
| 815 |
"""Temporarily yield a substitute drive for a given path.""" |
| 816 |
for c in reversed(string.ascii_uppercase): |
| 817 |
drive = f'{c}:' |
| 818 |
if (not kernel32.QueryDosDeviceW(drive, None, 0) and |
| 819 |
ctypes.get_last_error() == ERROR_FILE_NOT_FOUND): |
| 820 |
break |
| 821 |
else: |
| 822 |
raise unittest.SkipTest('no available logical drive') |
| 823 |
if not kernel32.DefineDosDeviceW( |
| 824 |
DDD_NO_BROADCAST_SYSTEM, drive, path): |
| 825 |
raise ctypes.WinError(ctypes.get_last_error()) |
| 826 |
try: |
| 827 |
yield drive |
| 828 |
finally: |
| 829 |
if not kernel32.DefineDosDeviceW( |
| 830 |
DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE, |
| 831 |
drive, path): |
| 832 |
raise ctypes.WinError(ctypes.get_last_error()) |