MCPcopy Index your code
hub / github.com/python/cpython / PthFile

Class PthFile

Lib/test/test_site.py:403–456  ·  view source on GitHub ↗

Helper class for handling testing of .pth files

Source from the content-addressed store, hash-verified

401
402
403class PthFile(object):
404 """Helper class for handling testing of .pth files"""
405
406 def __init__(self, filename_base=TESTFN, imported="time",
407 good_dirname="__testdir__", bad_dirname="__bad"):
408 """Initialize instance variables"""
409 self.filename = filename_base + ".pth"
410 self.base_dir = os.path.abspath('')
411 self.file_path = os.path.join(self.base_dir, self.filename)
412 self.imported = imported
413 self.good_dirname = good_dirname
414 self.bad_dirname = bad_dirname
415 self.good_dir_path = os.path.join(self.base_dir, self.good_dirname)
416 self.bad_dir_path = os.path.join(self.base_dir, self.bad_dirname)
417
418 def create(self):
419 """Create a .pth file with a comment, blank lines, an ``import
420 <self.imported>``, a line with self.good_dirname, and a line with
421 self.bad_dirname.
422
423 Creation of the directory for self.good_dir_path (based off of
424 self.good_dirname) is also performed.
425
426 Make sure to call self.cleanup() to undo anything done by this method.
427
428 """
429 FILE = open(self.file_path, 'w')
430 try:
431 print("#import @bad module name", file=FILE)
432 print("\n", file=FILE)
433 print("import %s" % self.imported, file=FILE)
434 print(self.good_dirname, file=FILE)
435 print(self.bad_dirname, file=FILE)
436 finally:
437 FILE.close()
438 os.mkdir(self.good_dir_path)
439
440 def cleanup(self, prep=False):
441 """Make sure that the .pth file is deleted, self.imported is not in
442 sys.modules, and that both self.good_dirname and self.bad_dirname are
443 not existing directories."""
444 if os.path.exists(self.file_path):
445 os.remove(self.file_path)
446 if prep:
447 self.imported_module = sys.modules.get(self.imported)
448 if self.imported_module:
449 del sys.modules[self.imported]
450 else:
451 if self.imported_module:
452 sys.modules[self.imported] = self.imported_module
453 if os.path.exists(self.good_dir_path):
454 os.rmdir(self.good_dir_path)
455 if os.path.exists(self.bad_dir_path):
456 os.rmdir(self.bad_dir_path)
457
458class ImportSideEffectTests(unittest.TestCase):
459 """Test side-effects from importing 'site'."""

Callers 5

test_addpackageMethod · 0.85
test_addsitedirMethod · 0.85

Calls

no outgoing calls

Tested by 5

test_addpackageMethod · 0.68
test_addsitedirMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…