2004-06-04 22:12:51 -03:00
|
|
|
"""Tests for 'site'.
|
|
|
|
|
|
|
|
Tests assume the initial paths in sys.path once the interpreter has begun
|
|
|
|
executing have not been removed.
|
|
|
|
|
|
|
|
"""
|
|
|
|
import unittest
|
2008-05-20 18:35:26 -03:00
|
|
|
from test.support import TestSkipped, run_unittest, TESTFN
|
2007-12-02 05:40:06 -04:00
|
|
|
import builtins
|
2004-06-04 22:12:51 -03:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import encodings
|
2008-05-06 20:45:46 -03:00
|
|
|
import subprocess
|
2004-06-04 22:12:51 -03:00
|
|
|
# Need to make sure to not import 'site' if someone specified ``-S`` at the
|
|
|
|
# command-line. Detect this by just making sure 'site' has not been imported
|
|
|
|
# already.
|
|
|
|
if "site" in sys.modules:
|
|
|
|
import site
|
|
|
|
else:
|
|
|
|
raise TestSkipped("importation of site.py suppressed")
|
|
|
|
|
2008-05-06 20:45:46 -03:00
|
|
|
if not os.path.isdir(site.USER_SITE):
|
|
|
|
# need to add user site directory for tests
|
|
|
|
os.makedirs(site.USER_SITE)
|
|
|
|
site.addsitedir(site.USER_SITE)
|
|
|
|
|
2004-06-04 22:12:51 -03:00
|
|
|
class HelperFunctionsTests(unittest.TestCase):
|
|
|
|
"""Tests for helper functions.
|
2004-06-27 00:02:18 -03:00
|
|
|
|
2004-06-04 22:12:51 -03:00
|
|
|
The setting of the encoding (set using sys.setdefaultencoding) used by
|
|
|
|
the Unicode implementation is not tested.
|
2004-06-27 00:02:18 -03:00
|
|
|
|
2004-06-04 22:12:51 -03:00
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Save a copy of sys.path"""
|
|
|
|
self.sys_path = sys.path[:]
|
|
|
|
|
Merged revisions 63208-63209,63211-63212,63214-63217,63219-63224,63226-63227,63229-63232,63234-63235,63237-63239,63241,63243-63246,63250-63254,63256-63259,63261,63263-63264,63266-63267,63269-63270,63272-63273,63275-63276,63278,63280-63281,63283-63284,63286-63287,63289-63290,63292-63293,63295-63296,63298-63299,63301-63302,63304-63305,63307,63309-63314,63316-63322,63324-63325,63327-63335,63337-63338,63340-63342,63344-63346,63348 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r63208 | georg.brandl | 2008-05-13 15:04:54 -0400 (Tue, 13 May 2008) | 2 lines
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
........
r63209 | marc-andre.lemburg | 2008-05-13 15:10:45 -0400 (Tue, 13 May 2008) | 3 lines
Remove leftovers from reverted setuptools checkin (they were added in r45525).
........
r63211 | georg.brandl | 2008-05-13 17:32:03 -0400 (Tue, 13 May 2008) | 2 lines
Fix a refleak in the _warnings module.
........
r63212 | andrew.kuchling | 2008-05-13 20:46:41 -0400 (Tue, 13 May 2008) | 1 line
List all the removes and renamed modules
........
r63214 | brett.cannon | 2008-05-13 21:09:40 -0400 (Tue, 13 May 2008) | 2 lines
Rewrap some lines in test_py3kwarn.
........
r63219 | georg.brandl | 2008-05-14 02:34:15 -0400 (Wed, 14 May 2008) | 2 lines
Add NEWS entry for #2831.
........
r63220 | neal.norwitz | 2008-05-14 02:47:56 -0400 (Wed, 14 May 2008) | 3 lines
Fix "refleak" by restoring the tearDown method removed by accident (AFAICT)
in r62788.
........
r63221 | georg.brandl | 2008-05-14 03:18:22 -0400 (Wed, 14 May 2008) | 2 lines
Fix another "refleak" by clearing the filters after test.
........
r63222 | neal.norwitz | 2008-05-14 03:21:42 -0400 (Wed, 14 May 2008) | 5 lines
Install the json package and tests as well as the lib2to3 tests
so the tests work when run from an install directory.
They are currently skipped on the daily runs (not from the buildbots)
for checking refleaks, etc.
........
r63256 | andrew.kuchling | 2008-05-14 21:10:24 -0400 (Wed, 14 May 2008) | 1 line
Note some removals and a rename
........
r63311 | brett.cannon | 2008-05-15 00:36:53 -0400 (Thu, 15 May 2008) | 2 lines
Add a snippet for the deprecation directive for docs.
........
r63313 | gregory.p.smith | 2008-05-15 00:56:18 -0400 (Thu, 15 May 2008) | 5 lines
disable the crashing test. I will also file a bug. This crash does
not appear to be a new bug, its just that the test coverage went up
recently exposing it. (I verified that by testing this test code on
an older Modules/_bsddb.c)
........
r63320 | georg.brandl | 2008-05-15 11:08:32 -0400 (Thu, 15 May 2008) | 2 lines
#2863: add gen.__name__ and add this name to generator repr().
........
r63324 | andrew.kuchling | 2008-05-15 16:07:39 -0400 (Thu, 15 May 2008) | 1 line
Import class from distutils.cmd, not .core, to avoid circular import
........
r63327 | alexandre.vassalotti | 2008-05-15 16:31:42 -0400 (Thu, 15 May 2008) | 2 lines
Fixed typo in a doctest of test_genexps.
........
r63332 | benjamin.peterson | 2008-05-15 18:34:33 -0400 (Thu, 15 May 2008) | 2 lines
add Mac modules to the list of deprecated ones
........
r63333 | benjamin.peterson | 2008-05-15 18:41:16 -0400 (Thu, 15 May 2008) | 2 lines
fix typos in whatsnew
........
r63348 | benjamin.peterson | 2008-05-15 22:24:49 -0400 (Thu, 15 May 2008) | 2 lines
make test_platform a bit more assertive (We'll see what the buildbots say.)
........
2008-05-16 01:39:54 -03:00
|
|
|
def tearDown(self):
|
2004-06-04 22:12:51 -03:00
|
|
|
"""Restore sys.path"""
|
|
|
|
sys.path = self.sys_path
|
2004-06-27 00:02:18 -03:00
|
|
|
|
2004-06-04 22:12:51 -03:00
|
|
|
def test_makepath(self):
|
|
|
|
# Test makepath() have an absolute path for its first return value
|
|
|
|
# and a case-normalized version of the absolute path for its
|
|
|
|
# second value.
|
|
|
|
path_parts = ("Beginning", "End")
|
|
|
|
original_dir = os.path.join(*path_parts)
|
|
|
|
abs_dir, norm_dir = site.makepath(*path_parts)
|
|
|
|
self.failUnlessEqual(os.path.abspath(original_dir), abs_dir)
|
|
|
|
if original_dir == os.path.normcase(original_dir):
|
|
|
|
self.failUnlessEqual(abs_dir, norm_dir)
|
|
|
|
else:
|
|
|
|
self.failUnlessEqual(os.path.normcase(abs_dir), norm_dir)
|
|
|
|
|
|
|
|
def test_init_pathinfo(self):
|
|
|
|
dir_set = site._init_pathinfo()
|
|
|
|
for entry in [site.makepath(path)[1] for path in sys.path
|
|
|
|
if path and os.path.isdir(path)]:
|
|
|
|
self.failUnless(entry in dir_set,
|
|
|
|
"%s from sys.path not found in set returned "
|
|
|
|
"by _init_pathinfo(): %s" % (entry, dir_set))
|
2004-06-27 00:02:18 -03:00
|
|
|
|
2004-07-13 04:12:25 -03:00
|
|
|
def pth_file_tests(self, pth_file):
|
|
|
|
"""Contain common code for testing results of reading a .pth file"""
|
|
|
|
self.failUnless(pth_file.imported in sys.modules,
|
|
|
|
"%s not in sys.path" % pth_file.imported)
|
|
|
|
self.failUnless(site.makepath(pth_file.good_dir_path)[0] in sys.path)
|
|
|
|
self.failUnless(not os.path.exists(pth_file.bad_dir_path))
|
|
|
|
|
2004-06-04 22:12:51 -03:00
|
|
|
def test_addpackage(self):
|
|
|
|
# Make sure addpackage() imports if the line starts with 'import',
|
2004-07-09 23:10:45 -03:00
|
|
|
# adds directories to sys.path for any line in the file that is not a
|
|
|
|
# comment or import that is a valid directory name for where the .pth
|
|
|
|
# file resides; invalid directories are not added
|
|
|
|
pth_file = PthFile()
|
2004-07-13 04:12:25 -03:00
|
|
|
pth_file.cleanup(prep=True) # to make sure that nothing is
|
|
|
|
# pre-existing that shouldn't be
|
2004-06-04 22:12:51 -03:00
|
|
|
try:
|
2004-07-09 23:10:45 -03:00
|
|
|
pth_file.create()
|
|
|
|
site.addpackage(pth_file.base_dir, pth_file.filename, set())
|
2004-07-13 04:12:25 -03:00
|
|
|
self.pth_file_tests(pth_file)
|
2004-06-04 22:12:51 -03:00
|
|
|
finally:
|
2004-07-09 23:10:45 -03:00
|
|
|
pth_file.cleanup()
|
2004-06-27 00:02:18 -03:00
|
|
|
|
2004-06-04 22:12:51 -03:00
|
|
|
def test_addsitedir(self):
|
2004-07-09 23:10:45 -03:00
|
|
|
# Same tests for test_addpackage since addsitedir() essentially just
|
|
|
|
# calls addpackage() for every .pth file in the directory
|
|
|
|
pth_file = PthFile()
|
2004-07-13 04:12:25 -03:00
|
|
|
pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
|
|
|
|
# that is tested for
|
2004-06-04 22:12:51 -03:00
|
|
|
try:
|
2004-07-13 04:12:25 -03:00
|
|
|
pth_file.create()
|
2004-07-09 23:10:45 -03:00
|
|
|
site.addsitedir(pth_file.base_dir, set())
|
2004-07-13 04:12:25 -03:00
|
|
|
self.pth_file_tests(pth_file)
|
2004-06-04 22:12:51 -03:00
|
|
|
finally:
|
2004-07-09 23:10:45 -03:00
|
|
|
pth_file.cleanup()
|
|
|
|
|
2008-05-06 20:45:46 -03:00
|
|
|
def test_s_option(self):
|
|
|
|
usersite = site.USER_SITE
|
|
|
|
self.assert_(usersite in sys.path)
|
|
|
|
|
|
|
|
rc = subprocess.call([sys.executable, '-c',
|
|
|
|
'import sys; sys.exit("%s" in sys.path)' % usersite])
|
|
|
|
self.assertEqual(rc, 1)
|
|
|
|
|
|
|
|
rc = subprocess.call([sys.executable, '-s', '-c',
|
|
|
|
'import sys; sys.exit("%s" in sys.path)' % usersite])
|
|
|
|
self.assertEqual(rc, 0)
|
|
|
|
|
|
|
|
env = os.environ.copy()
|
|
|
|
env["PYTHONNOUSERSITE"] = "1"
|
|
|
|
rc = subprocess.call([sys.executable, '-c',
|
|
|
|
'import sys; sys.exit("%s" in sys.path)' % usersite],
|
|
|
|
env=env)
|
|
|
|
self.assertEqual(rc, 0)
|
|
|
|
|
|
|
|
env = os.environ.copy()
|
|
|
|
env["PYTHONUSERBASE"] = "/tmp"
|
|
|
|
rc = subprocess.call([sys.executable, '-c',
|
|
|
|
'import sys, site; sys.exit(site.USER_BASE.startswith("/tmp"))'],
|
|
|
|
env=env)
|
|
|
|
self.assertEqual(rc, 1)
|
|
|
|
|
|
|
|
|
2004-07-09 23:10:45 -03:00
|
|
|
class PthFile(object):
|
|
|
|
"""Helper class for handling testing of .pth files"""
|
|
|
|
|
|
|
|
def __init__(self, filename_base=TESTFN, imported="time",
|
|
|
|
good_dirname="__testdir__", bad_dirname="__bad"):
|
|
|
|
"""Initialize instance variables"""
|
|
|
|
self.filename = filename_base + ".pth"
|
|
|
|
self.base_dir = os.path.abspath('')
|
|
|
|
self.file_path = os.path.join(self.base_dir, self.filename)
|
2004-07-13 04:12:25 -03:00
|
|
|
self.imported = imported
|
2004-07-09 23:10:45 -03:00
|
|
|
self.good_dirname = good_dirname
|
|
|
|
self.bad_dirname = bad_dirname
|
|
|
|
self.good_dir_path = os.path.join(self.base_dir, self.good_dirname)
|
|
|
|
self.bad_dir_path = os.path.join(self.base_dir, self.bad_dirname)
|
|
|
|
|
|
|
|
def create(self):
|
|
|
|
"""Create a .pth file with a comment, blank lines, an ``import
|
|
|
|
<self.imported>``, a line with self.good_dirname, and a line with
|
|
|
|
self.bad_dirname.
|
2004-07-18 03:16:08 -03:00
|
|
|
|
2004-07-09 23:10:45 -03:00
|
|
|
Creation of the directory for self.good_dir_path (based off of
|
|
|
|
self.good_dirname) is also performed.
|
|
|
|
|
|
|
|
Make sure to call self.cleanup() to undo anything done by this method.
|
2004-07-18 03:16:08 -03:00
|
|
|
|
2004-07-09 23:10:45 -03:00
|
|
|
"""
|
2005-05-27 11:58:06 -03:00
|
|
|
FILE = open(self.file_path, 'w')
|
2004-07-09 23:10:45 -03:00
|
|
|
try:
|
2007-02-09 01:37:30 -04:00
|
|
|
print("#import @bad module name", file=FILE)
|
|
|
|
print("\n", file=FILE)
|
|
|
|
print("import %s" % self.imported, file=FILE)
|
|
|
|
print(self.good_dirname, file=FILE)
|
|
|
|
print(self.bad_dirname, file=FILE)
|
2004-07-09 23:10:45 -03:00
|
|
|
finally:
|
|
|
|
FILE.close()
|
|
|
|
os.mkdir(self.good_dir_path)
|
2004-06-04 22:12:51 -03:00
|
|
|
|
2004-07-13 04:12:25 -03:00
|
|
|
def cleanup(self, prep=False):
|
2004-07-09 23:10:45 -03:00
|
|
|
"""Make sure that the .pth file is deleted, self.imported is not in
|
|
|
|
sys.modules, and that both self.good_dirname and self.bad_dirname are
|
|
|
|
not existing directories."""
|
2004-07-13 04:12:25 -03:00
|
|
|
if os.path.exists(self.file_path):
|
2004-07-09 23:10:45 -03:00
|
|
|
os.remove(self.file_path)
|
2004-07-13 04:12:25 -03:00
|
|
|
if prep:
|
|
|
|
self.imported_module = sys.modules.get(self.imported)
|
|
|
|
if self.imported_module:
|
|
|
|
del sys.modules[self.imported]
|
|
|
|
else:
|
|
|
|
if self.imported_module:
|
|
|
|
sys.modules[self.imported] = self.imported_module
|
|
|
|
if os.path.exists(self.good_dir_path):
|
2004-07-09 23:10:45 -03:00
|
|
|
os.rmdir(self.good_dir_path)
|
2004-07-13 04:12:25 -03:00
|
|
|
if os.path.exists(self.bad_dir_path):
|
2004-07-09 23:10:45 -03:00
|
|
|
os.rmdir(self.bad_dir_path)
|
2004-06-04 22:12:51 -03:00
|
|
|
|
|
|
|
class ImportSideEffectTests(unittest.TestCase):
|
|
|
|
"""Test side-effects from importing 'site'."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Make a copy of sys.path"""
|
|
|
|
self.sys_path = sys.path[:]
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Restore sys.path"""
|
|
|
|
sys.path = self.sys_path
|
|
|
|
|
|
|
|
def test_abs__file__(self):
|
|
|
|
# Make sure all imported modules have their __file__ attribute
|
|
|
|
# as an absolute path.
|
|
|
|
# Handled by abs__file__()
|
|
|
|
site.abs__file__()
|
2007-12-02 05:40:06 -04:00
|
|
|
for module in (sys, os, builtins):
|
2004-06-04 22:12:51 -03:00
|
|
|
try:
|
2006-08-24 23:59:59 -03:00
|
|
|
self.failUnless(os.path.isabs(module.__file__), repr(module))
|
2004-06-04 22:12:51 -03:00
|
|
|
except AttributeError:
|
|
|
|
continue
|
2004-06-27 00:02:18 -03:00
|
|
|
# We could try everything in sys.modules; however, when regrtest.py
|
|
|
|
# runs something like test_frozen before test_site, then we will
|
|
|
|
# be testing things loaded *after* test_site did path normalization
|
2004-06-04 22:12:51 -03:00
|
|
|
|
|
|
|
def test_no_duplicate_paths(self):
|
|
|
|
# No duplicate paths should exist in sys.path
|
|
|
|
# Handled by removeduppaths()
|
|
|
|
site.removeduppaths()
|
|
|
|
seen_paths = set()
|
|
|
|
for path in sys.path:
|
|
|
|
self.failUnless(path not in seen_paths)
|
|
|
|
seen_paths.add(path)
|
|
|
|
|
|
|
|
def test_add_build_dir(self):
|
|
|
|
# Test that the build directory's Modules directory is used when it
|
|
|
|
# should be.
|
|
|
|
# XXX: implement
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_setting_quit(self):
|
2007-12-02 05:40:06 -04:00
|
|
|
# 'quit' and 'exit' should be injected into builtins
|
|
|
|
self.failUnless(hasattr(builtins, "quit"))
|
|
|
|
self.failUnless(hasattr(builtins, "exit"))
|
2004-06-04 22:12:51 -03:00
|
|
|
|
|
|
|
def test_setting_copyright(self):
|
2007-12-02 05:40:06 -04:00
|
|
|
# 'copyright' and 'credits' should be in builtins
|
|
|
|
self.failUnless(hasattr(builtins, "copyright"))
|
|
|
|
self.failUnless(hasattr(builtins, "credits"))
|
2004-06-04 22:12:51 -03:00
|
|
|
|
|
|
|
def test_setting_help(self):
|
2007-12-02 05:40:06 -04:00
|
|
|
# 'help' should be set in builtins
|
|
|
|
self.failUnless(hasattr(builtins, "help"))
|
2004-06-04 22:12:51 -03:00
|
|
|
|
|
|
|
def test_aliasing_mbcs(self):
|
|
|
|
if sys.platform == "win32":
|
|
|
|
import locale
|
|
|
|
if locale.getdefaultlocale()[1].startswith('cp'):
|
2007-02-11 02:12:03 -04:00
|
|
|
for value in encodings.aliases.aliases.values():
|
2004-06-04 22:12:51 -03:00
|
|
|
if value == "mbcs":
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.fail("did not alias mbcs")
|
|
|
|
|
|
|
|
def test_setdefaultencoding_removed(self):
|
|
|
|
# Make sure sys.setdefaultencoding is gone
|
|
|
|
self.failUnless(not hasattr(sys, "setdefaultencoding"))
|
|
|
|
|
|
|
|
def test_sitecustomize_executed(self):
|
|
|
|
# If sitecustomize is available, it should have been imported.
|
2006-08-18 19:13:04 -03:00
|
|
|
if "sitecustomize" not in sys.modules:
|
2004-06-04 22:12:51 -03:00
|
|
|
try:
|
|
|
|
import sitecustomize
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail("sitecustomize not imported automatically")
|
|
|
|
|
|
|
|
def test_main():
|
|
|
|
run_unittest(HelperFunctionsTests, ImportSideEffectTests)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|