diff options
Diffstat (limited to 'Lib/site.py')
-rw-r--r-- | Lib/site.py | 202 |
1 files changed, 77 insertions, 125 deletions
diff --git a/Lib/site.py b/Lib/site.py index 7e09701..4fb1058 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -58,17 +58,20 @@ Note that bletch is omitted because it doesn't exist; bar precedes foo because bar.pth comes alphabetically before foo.pth; and spam is omitted because it is not mentioned in either path configuration file. -After these path manipulations, an attempt is made to import a module +The readline module is also automatically configured to enable +completion for systems that support it. This can be overriden in +sitecustomize, usercustomize or PYTHONSTARTUP. + +After these operations, an attempt is made to import a module named sitecustomize, which can perform arbitrary additional site-specific customizations. If this import fails with an ImportError exception, it is silently ignored. - """ import sys import os -import re import builtins +import _sitebuiltins # Prefixes for site-packages; add additional prefixes like /usr/local here PREFIXES = [sys.prefix, sys.exec_prefix] @@ -146,14 +149,14 @@ def addpackage(sitedir, name, known_paths): and add that to known_paths, or execute it if it starts with 'import '. """ if known_paths is None: - _init_pathinfo() + known_paths = _init_pathinfo() reset = 1 else: reset = 0 fullname = os.path.join(sitedir, name) try: f = open(fullname, "r") - except IOError: + except OSError: return with f: for n, line in enumerate(f): @@ -196,7 +199,7 @@ def addsitedir(sitedir, known_paths=None): known_paths.add(sitedircase) try: names = os.listdir(sitedir) - except os.error: + except OSError: return names = [name for name in names if name.endswith(".pth")] for name in sorted(names): @@ -300,9 +303,7 @@ def getsitepackages(prefixes=None): continue seen.add(prefix) - if sys.platform in ('os2emx', 'riscos'): - sitepackages.append(os.path.join(prefix, "Lib", "site-packages")) - elif os.sep == '/': + if os.sep == '/': sitepackages.append(os.path.join(prefix, "lib", "python" + sys.version[:3], "site-packages")) @@ -325,27 +326,15 @@ def addsitepackages(known_paths, prefixes=None): """Add site-packages (and possibly site-python) to sys.path""" for sitedir in getsitepackages(prefixes): if os.path.isdir(sitedir): + if "site-python" in sitedir: + import warnings + warnings.warn('"site-python" directories will not be ' + 'supported in 3.5 anymore', + DeprecationWarning) addsitedir(sitedir, known_paths) return known_paths -def setBEGINLIBPATH(): - """The OS/2 EMX port has optional extension modules that do double duty - as DLLs (and must use the .DLL file extension) for other extensions. - The library search path needs to be amended so these will be found - during module import. Use BEGINLIBPATH so that these are at the start - of the library search path. - - """ - dllpath = os.path.join(sys.prefix, "Lib", "lib-dynload") - libpath = os.environ['BEGINLIBPATH'].split(';') - if libpath[-1]: - libpath.append(dllpath) - else: - libpath[-1] = dllpath - os.environ['BEGINLIBPATH'] = ';'.join(libpath) - - def setquit(): """Define new builtins 'quit' and 'exit'. @@ -360,126 +349,89 @@ def setquit(): else: eof = 'Ctrl-D (i.e. EOF)' - class Quitter(object): - def __init__(self, name): - self.name = name - def __repr__(self): - return 'Use %s() or %s to exit' % (self.name, eof) - def __call__(self, code=None): - # Shells like IDLE catch the SystemExit, but listen when their - # stdin wrapper is closed. - try: - sys.stdin.close() - except: - pass - raise SystemExit(code) - builtins.quit = Quitter('quit') - builtins.exit = Quitter('exit') - - -class _Printer(object): - """interactive prompt objects for printing the license text, a list of - contributors and the copyright notice.""" + builtins.quit = _sitebuiltins.Quitter('quit', eof) + builtins.exit = _sitebuiltins.Quitter('exit', eof) - MAXLINES = 23 - - def __init__(self, name, data, files=(), dirs=()): - self.__name = name - self.__data = data - self.__files = files - self.__dirs = dirs - self.__lines = None - - def __setup(self): - if self.__lines: - return - data = None - for dir in self.__dirs: - for filename in self.__files: - filename = os.path.join(dir, filename) - try: - fp = open(filename, "r") - data = fp.read() - fp.close() - break - except IOError: - pass - if data: - break - if not data: - data = self.__data - self.__lines = data.split('\n') - self.__linecnt = len(self.__lines) - - def __repr__(self): - self.__setup() - if len(self.__lines) <= self.MAXLINES: - return "\n".join(self.__lines) - else: - return "Type %s() to see the full %s text" % ((self.__name,)*2) - - def __call__(self): - self.__setup() - prompt = 'Hit Return for more, or q (and Return) to quit: ' - lineno = 0 - while 1: - try: - for i in range(lineno, lineno + self.MAXLINES): - print(self.__lines[i]) - except IndexError: - break - else: - lineno += self.MAXLINES - key = None - while key is None: - key = input(prompt) - if key not in ('', 'q'): - key = None - if key == 'q': - break def setcopyright(): """Set 'copyright' and 'credits' in builtins""" - builtins.copyright = _Printer("copyright", sys.copyright) + builtins.copyright = _sitebuiltins._Printer("copyright", sys.copyright) if sys.platform[:4] == 'java': - builtins.credits = _Printer( + builtins.credits = _sitebuiltins._Printer( "credits", "Jython is maintained by the Jython developers (www.jython.org).") else: - builtins.credits = _Printer("credits", """\ + builtins.credits = _sitebuiltins._Printer("credits", """\ Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands for supporting Python development. See www.python.org for more information.""") here = os.path.dirname(os.__file__) - builtins.license = _Printer( + builtins.license = _sitebuiltins._Printer( "license", - "See http://www.python.org/download/releases/%.5s/license/" % sys.version, + "See http://www.python.org/download/releases/%.5s/license" % sys.version, ["LICENSE.txt", "LICENSE"], [os.path.join(here, os.pardir), here, os.curdir]) -class _Helper(object): - """Define the builtin 'help'. - This is a wrapper around pydoc.help (with a twist). +def sethelper(): + builtins.help = _sitebuiltins._Helper() +def enablerlcompleter(): + """Enable default readline configuration on interactive prompts, by + registering a sys.__interactivehook__. + + If the readline module can be imported, the hook will set the Tab key + as completion key and register ~/.python_history as history file. + This can be overriden in the sitecustomize or usercustomize module, + or in a PYTHONSTARTUP file. """ + def register_readline(): + import atexit + try: + import readline + import rlcompleter + except ImportError: + return - def __repr__(self): - return "Type help() for interactive help, " \ - "or help(object) for help about object." - def __call__(self, *args, **kwds): - import pydoc - return pydoc.help(*args, **kwds) + # Reading the initialization (config) file may not be enough to set a + # completion key, so we set one first and then read the file. + readline_doc = getattr(readline, '__doc__', '') + if readline_doc is not None and 'libedit' in readline_doc: + readline.parse_and_bind('bind ^I rl_complete') + else: + readline.parse_and_bind('tab: complete') -def sethelper(): - builtins.help = _Helper() + try: + readline.read_init_file() + except OSError: + # An OSError here could have many causes, but the most likely one + # is that there's no .inputrc file (or .editrc file in the case of + # Mac OS X + libedit) in the expected location. In that case, we + # want to ignore the exception. + pass + + if readline.get_current_history_length() == 0: + # If no history was loaded, default to .python_history. + # The guard is necessary to avoid doubling history size at + # each interpreter exit when readline was already configured + # through a PYTHONSTARTUP hook, see: + # http://bugs.python.org/issue5845#msg198636 + history = os.path.join(os.path.expanduser('~'), + '.python_history') + try: + readline.read_history_file(history) + except IOError: + pass + atexit.register(readline.write_history_file, history) + + sys.__interactivehook__ = register_readline def aliasmbcs(): """On Windows, some default encodings are not provided by Python, while they are always available as "mbcs" in each locale. Make them usable by aliasing to "mbcs" in such a case.""" if sys.platform == 'win32': - import locale, codecs - enc = locale.getdefaultlocale()[1] + import _bootlocale, codecs + enc = _bootlocale.getpreferredencoding(False) if enc.startswith('cp'): # "cp***" ? try: codecs.lookup(enc) @@ -488,8 +440,7 @@ def aliasmbcs(): encodings._cache[enc] = encodings._unknown encodings.aliases.aliases[enc] = 'mbcs' - -CONFIG_LINE = re.compile(r'^(?P<key>(\w|[-_])+)\s*=\s*(?P<value>.*)\s*$') +CONFIG_LINE = r'^(?P<key>(\w|[-_])+)\s*=\s*(?P<value>.*)\s*$' def venv(known_paths): global PREFIXES, ENABLE_USER_SITE @@ -512,12 +463,14 @@ def venv(known_paths): ] if candidate_confs: + import re + config_line = re.compile(CONFIG_LINE) virtual_conf = candidate_confs[0] system_site = "true" with open(virtual_conf) as f: for line in f: line = line.strip() - m = CONFIG_LINE.match(line) + m = config_line.match(line) if m: d = m.groupdict() key, value = d['key'].lower(), d['value'] @@ -589,11 +542,10 @@ def main(): ENABLE_USER_SITE = check_enableusersite() known_paths = addusersitepackages(known_paths) known_paths = addsitepackages(known_paths) - if sys.platform == 'os2emx': - setBEGINLIBPATH() setquit() setcopyright() sethelper() + enablerlcompleter() aliasmbcs() execsitecustomize() if ENABLE_USER_SITE: |