MCPcopy Index your code
hub / github.com/RustPython/RustPython / PthFile

Class PthFile

Lib/test/test_site.py:405–458  ·  view source on GitHub ↗

Helper class for handling testing of .pth files

Source from the content-addressed store, hash-verified

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