Back to index

python3.2  3.2.2
Public Member Functions
test.test_zipfile.PyZipFileTests Class Reference

List of all members.

Public Member Functions

def test_write_pyfile
def test_write_python_package
def test_write_with_optimization
def test_write_python_directory
def test_write_non_pyfile

Detailed Description

Definition at line 638 of file test_zipfile.py.


Member Function Documentation

Definition at line 724 of file test_zipfile.py.

00724 
00725     def test_write_non_pyfile(self):
00726         with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
00727             with open(TESTFN, 'w') as f:
00728                 f.write('most definitely not a python file')
00729             self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
00730             os.remove(TESTFN)
00731 

Here is the call graph for this function:

Definition at line 639 of file test_zipfile.py.

00639 
00640     def test_write_pyfile(self):
00641         with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
00642             fn = __file__
00643             if fn.endswith('.pyc') or fn.endswith('.pyo'):
00644                 path_split = fn.split(os.sep)
00645                 if os.altsep is not None:
00646                     path_split.extend(fn.split(os.altsep))
00647                 if '__pycache__' in path_split:
00648                     fn = imp.source_from_cache(fn)
00649                 else:
00650                     fn = fn[:-1]
00651 
00652             zipfp.writepy(fn)
00653 
00654             bn = os.path.basename(fn)
00655             self.assertNotIn(bn, zipfp.namelist())
00656             self.assertTrue(bn + 'o' in zipfp.namelist() or
00657                             bn + 'c' in zipfp.namelist())
00658 
00659         with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
00660             fn = __file__
00661             if fn.endswith(('.pyc', '.pyo')):
00662                 fn = fn[:-1]
00663 
00664             zipfp.writepy(fn, "testpackage")
00665 
00666             bn = "%s/%s" % ("testpackage", os.path.basename(fn))
00667             self.assertNotIn(bn, zipfp.namelist())
00668             self.assertTrue(bn + 'o' in zipfp.namelist() or
00669                             bn + 'c' in zipfp.namelist())

Here is the call graph for this function:

Definition at line 701 of file test_zipfile.py.

00701 
00702     def test_write_python_directory(self):
00703         os.mkdir(TESTFN2)
00704         try:
00705             with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
00706                 fp.write("print(42)\n")
00707 
00708             with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp:
00709                 fp.write("print(42 * 42)\n")
00710 
00711             with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp:
00712                 fp.write("bla bla bla\n")
00713 
00714             with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
00715                 zipfp.writepy(TESTFN2)
00716 
00717                 names = zipfp.namelist()
00718                 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
00719                 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
00720                 self.assertNotIn('mod2.txt', names)
00721 
00722         finally:
00723             shutil.rmtree(TESTFN2)

Here is the call graph for this function:

Definition at line 670 of file test_zipfile.py.

00670 
00671     def test_write_python_package(self):
00672         import email
00673         packagedir = os.path.dirname(email.__file__)
00674 
00675         with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
00676             zipfp.writepy(packagedir)
00677 
00678             # Check for a couple of modules at different levels of the
00679             # hierarchy
00680             names = zipfp.namelist()
00681             self.assertTrue('email/__init__.pyo' in names or
00682                             'email/__init__.pyc' in names)
00683             self.assertTrue('email/mime/text.pyo' in names or
00684                             'email/mime/text.pyc' in names)

Here is the call graph for this function:

Definition at line 685 of file test_zipfile.py.

00685 
00686     def test_write_with_optimization(self):
00687         import email
00688         packagedir = os.path.dirname(email.__file__)
00689         # use .pyc if running test in optimization mode,
00690         # use .pyo if running test in debug mode
00691         optlevel = 1 if __debug__ else 0
00692         ext = '.pyo' if optlevel == 1 else '.pyc'
00693 
00694         with TemporaryFile() as t, \
00695                  zipfile.PyZipFile(t, "w", optimize=optlevel) as zipfp:
00696             zipfp.writepy(packagedir)
00697 
00698             names = zipfp.namelist()
00699             self.assertIn('email/__init__' + ext, names)
00700             self.assertIn('email/mime/text' + ext, names)

Here is the call graph for this function:


The documentation for this class was generated from the following file: