Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 1 | """Append module search paths for third-party packages to sys.path. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 3 | **************************************************************** |
| 4 | * This module is automatically imported during initialization. * |
| 5 | **************************************************************** |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 7 | In earlier versions of Python (up to 1.5a3), scripts or modules that |
| 8 | needed to use site-specific modules would place ``import site'' |
| 9 | somewhere near the top of their code. Because of the automatic |
| 10 | import, this is no longer necessary (but code that does it still |
| 11 | works). |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 12 | |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 13 | This will append site-specific paths to the module search path. On |
Nick Coghlan | f2b16f3 | 2006-06-12 08:23:02 +0000 | [diff] [blame] | 14 | Unix (including Mac OSX), it starts with sys.prefix and |
| 15 | sys.exec_prefix (if different) and appends |
| 16 | lib/python<version>/site-packages as well as lib/site-python. |
| 17 | On other platforms (such as Windows), it tries each of the |
Nick Coghlan | 3fb55ca | 2006-06-12 08:19:37 +0000 | [diff] [blame] | 18 | prefixes directly, as well as with lib/site-packages appended. The |
Guido van Rossum | 62b297b | 1997-09-08 02:14:09 +0000 | [diff] [blame] | 19 | resulting directories, if they exist, are appended to sys.path, and |
| 20 | also inspected for path configuration files. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 22 | A path configuration file is a file whose name has the form |
| 23 | <package>.pth; its contents are additional directories (one per line) |
| 24 | to be added to sys.path. Non-existing directories (or |
| 25 | non-directories) are never added to sys.path; no directory is added to |
| 26 | sys.path more than once. Blank lines and lines beginning with |
Guido van Rossum | facf24b | 2001-12-17 16:07:06 +0000 | [diff] [blame] | 27 | '#' are skipped. Lines starting with 'import' are executed. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 29 | For example, suppose sys.prefix and sys.exec_prefix are set to |
Neal Norwitz | 6e48256 | 2006-08-15 04:59:30 +0000 | [diff] [blame] | 30 | /usr/local and there is a directory /usr/local/lib/python2.5/site-packages |
Guido van Rossum | 62b297b | 1997-09-08 02:14:09 +0000 | [diff] [blame] | 31 | with three subdirectories, foo, bar and spam, and two path |
| 32 | configuration files, foo.pth and bar.pth. Assume foo.pth contains the |
| 33 | following: |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 34 | |
| 35 | # foo package configuration |
| 36 | foo |
| 37 | bar |
| 38 | bletch |
| 39 | |
| 40 | and bar.pth contains: |
| 41 | |
| 42 | # bar package configuration |
| 43 | bar |
| 44 | |
| 45 | Then the following directories are added to sys.path, in this order: |
| 46 | |
Neal Norwitz | 6e48256 | 2006-08-15 04:59:30 +0000 | [diff] [blame] | 47 | /usr/local/lib/python2.5/site-packages/bar |
| 48 | /usr/local/lib/python2.5/site-packages/foo |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 49 | |
| 50 | Note that bletch is omitted because it doesn't exist; bar precedes foo |
| 51 | because bar.pth comes alphabetically before foo.pth; and spam is |
| 52 | omitted because it is not mentioned in either path configuration file. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 53 | |
| 54 | After these path manipulations, an attempt is made to import a module |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 55 | named sitecustomize, which can perform arbitrary additional |
| 56 | site-specific customizations. If this import fails with an |
| 57 | ImportError exception, it is silently ignored. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 59 | """ |
| 60 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 61 | import sys |
| 62 | import os |
| 63 | import __builtin__ |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | d74fb6b | 2001-03-02 06:43:49 +0000 | [diff] [blame] | 65 | |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 66 | def makepath(*paths): |
Fred Drake | 1fb5ce0 | 2001-07-02 16:55:42 +0000 | [diff] [blame] | 67 | dir = os.path.abspath(os.path.join(*paths)) |
| 68 | return dir, os.path.normcase(dir) |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 69 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 70 | def abs__file__(): |
| 71 | """Set all module' __file__ attribute to an absolute path""" |
| 72 | for m in sys.modules.values(): |
Neal Norwitz | 0c46985 | 2006-04-11 07:21:20 +0000 | [diff] [blame] | 73 | if hasattr(m, '__loader__'): |
Phillip J. Eby | 4703211 | 2006-04-11 01:07:43 +0000 | [diff] [blame] | 74 | continue # don't mess with a PEP 302-supplied __file__ |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 75 | try: |
| 76 | m.__file__ = os.path.abspath(m.__file__) |
| 77 | except AttributeError: |
| 78 | continue |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 79 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 80 | def removeduppaths(): |
| 81 | """ Remove duplicate entries from sys.path along with making them |
| 82 | absolute""" |
| 83 | # This ensures that the initial path provided by the interpreter contains |
| 84 | # only absolute pathnames, even if we're running from the build directory. |
| 85 | L = [] |
| 86 | known_paths = set() |
| 87 | for dir in sys.path: |
| 88 | # Filter out duplicate paths (on case-insensitive file systems also |
| 89 | # if they only differ in case); turn relative paths into absolute |
| 90 | # paths. |
| 91 | dir, dircase = makepath(dir) |
| 92 | if not dircase in known_paths: |
| 93 | L.append(dir) |
| 94 | known_paths.add(dircase) |
| 95 | sys.path[:] = L |
| 96 | return known_paths |
Fred Drake | 38cb9f1 | 2000-09-28 16:52:36 +0000 | [diff] [blame] | 97 | |
Fred Drake | e80c0d3 | 2002-07-25 20:13:03 +0000 | [diff] [blame] | 98 | # XXX This should not be part of site.py, since it is needed even when |
| 99 | # using the -S option for Python. See http://www.python.org/sf/586680 |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 100 | def addbuilddir(): |
| 101 | """Append ./build/lib.<platform> in case we're running in the build dir |
| 102 | (especially for Guido :-)""" |
Jeremy Hylton | 6d58bf6 | 2003-07-18 17:45:33 +0000 | [diff] [blame] | 103 | from distutils.util import get_platform |
| 104 | s = "build/lib.%s-%.3s" % (get_platform(), sys.version) |
Guido van Rossum | 48eb9cd | 2001-01-19 21:54:59 +0000 | [diff] [blame] | 105 | s = os.path.join(os.path.dirname(sys.path[-1]), s) |
| 106 | sys.path.append(s) |
Guido van Rossum | 48eb9cd | 2001-01-19 21:54:59 +0000 | [diff] [blame] | 107 | |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 108 | def _init_pathinfo(): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 109 | """Return a set containing all existing directory entries from sys.path""" |
| 110 | d = set() |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 111 | for dir in sys.path: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 112 | try: |
| 113 | if os.path.isdir(dir): |
| 114 | dir, dircase = makepath(dir) |
| 115 | d.add(dircase) |
| 116 | except TypeError: |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 117 | continue |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 118 | return d |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 119 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 120 | def addpackage(sitedir, name, known_paths): |
| 121 | """Add a new path to known_paths by combining sitedir and 'name' or execute |
| 122 | sitedir if it starts with 'import'""" |
| 123 | if known_paths is None: |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 124 | _init_pathinfo() |
| 125 | reset = 1 |
| 126 | else: |
| 127 | reset = 0 |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 128 | fullname = os.path.join(sitedir, name) |
| 129 | try: |
Brett Cannon | 4d0bddf | 2004-07-20 02:28:28 +0000 | [diff] [blame] | 130 | f = open(fullname, "rU") |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 131 | except IOError: |
| 132 | return |
| 133 | try: |
| 134 | for line in f: |
| 135 | if line.startswith("#"): |
| 136 | continue |
| 137 | if line.startswith("import"): |
| 138 | exec line |
| 139 | continue |
| 140 | line = line.rstrip() |
| 141 | dir, dircase = makepath(sitedir, line) |
| 142 | if not dircase in known_paths and os.path.exists(dir): |
| 143 | sys.path.append(dir) |
| 144 | known_paths.add(dircase) |
| 145 | finally: |
| 146 | f.close() |
| 147 | if reset: |
| 148 | known_paths = None |
| 149 | return known_paths |
| 150 | |
Brett Cannon | 12f8c4d | 2004-07-09 23:38:18 +0000 | [diff] [blame] | 151 | def addsitedir(sitedir, known_paths=None): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 152 | """Add 'sitedir' argument to sys.path if missing and handle .pth files in |
| 153 | 'sitedir'""" |
| 154 | if known_paths is None: |
Brett Cannon | 4d0bddf | 2004-07-20 02:28:28 +0000 | [diff] [blame] | 155 | known_paths = _init_pathinfo() |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 156 | reset = 1 |
| 157 | else: |
| 158 | reset = 0 |
Fred Drake | 1fb5ce0 | 2001-07-02 16:55:42 +0000 | [diff] [blame] | 159 | sitedir, sitedircase = makepath(sitedir) |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 160 | if not sitedircase in known_paths: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 161 | sys.path.append(sitedir) # Add path component |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 162 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 163 | names = os.listdir(sitedir) |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 164 | except os.error: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 165 | return |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 166 | names.sort() |
| 167 | for name in names: |
Brett Cannon | 4d0bddf | 2004-07-20 02:28:28 +0000 | [diff] [blame] | 168 | if name.endswith(os.extsep + "pth"): |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 169 | addpackage(sitedir, name, known_paths) |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 170 | if reset: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 171 | known_paths = None |
| 172 | return known_paths |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 173 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 174 | def addsitepackages(known_paths): |
| 175 | """Add site-packages (and possibly site-python) to sys.path""" |
| 176 | prefixes = [sys.prefix] |
| 177 | if sys.exec_prefix != sys.prefix: |
| 178 | prefixes.append(sys.exec_prefix) |
| 179 | for prefix in prefixes: |
| 180 | if prefix: |
| 181 | if sys.platform in ('os2emx', 'riscos'): |
| 182 | sitedirs = [os.path.join(prefix, "Lib", "site-packages")] |
| 183 | elif os.sep == '/': |
| 184 | sitedirs = [os.path.join(prefix, |
| 185 | "lib", |
| 186 | "python" + sys.version[:3], |
| 187 | "site-packages"), |
| 188 | os.path.join(prefix, "lib", "site-python")] |
| 189 | else: |
| 190 | sitedirs = [prefix, os.path.join(prefix, "lib", "site-packages")] |
| 191 | if sys.platform == 'darwin': |
| 192 | # for framework builds *only* we add the standard Apple |
| 193 | # locations. Currently only per-user, but /Library and |
| 194 | # /Network/Library could be added too |
| 195 | if 'Python.framework' in prefix: |
| 196 | home = os.environ.get('HOME') |
| 197 | if home: |
| 198 | sitedirs.append( |
| 199 | os.path.join(home, |
| 200 | 'Library', |
| 201 | 'Python', |
| 202 | sys.version[:3], |
| 203 | 'site-packages')) |
| 204 | for sitedir in sitedirs: |
| 205 | if os.path.isdir(sitedir): |
| 206 | addsitedir(sitedir, known_paths) |
| 207 | return None |
Fred Drake | 7f5296e | 2001-07-20 20:06:17 +0000 | [diff] [blame] | 208 | |
Fred Drake | 1fb5ce0 | 2001-07-02 16:55:42 +0000 | [diff] [blame] | 209 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 210 | def setBEGINLIBPATH(): |
| 211 | """The OS/2 EMX port has optional extension modules that do double duty |
| 212 | as DLLs (and must use the .DLL file extension) for other extensions. |
| 213 | The library search path needs to be amended so these will be found |
| 214 | during module import. Use BEGINLIBPATH so that these are at the start |
| 215 | of the library search path. |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 216 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 217 | """ |
Andrew MacIntyre | 2e8a6e0 | 2003-12-02 12:27:25 +0000 | [diff] [blame] | 218 | dllpath = os.path.join(sys.prefix, "Lib", "lib-dynload") |
| 219 | libpath = os.environ['BEGINLIBPATH'].split(';') |
| 220 | if libpath[-1]: |
| 221 | libpath.append(dllpath) |
| 222 | else: |
| 223 | libpath[-1] = dllpath |
| 224 | os.environ['BEGINLIBPATH'] = ';'.join(libpath) |
| 225 | |
| 226 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 227 | def setquit(): |
| 228 | """Define new built-ins 'quit' and 'exit'. |
| 229 | These are simply strings that display a hint on how to exit. |
Guido van Rossum | d89fa0c | 1998-08-07 18:01:14 +0000 | [diff] [blame] | 230 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 231 | """ |
| 232 | if os.sep == ':': |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 233 | eof = 'Cmd-Q' |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 234 | elif os.sep == '\\': |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 235 | eof = 'Ctrl-Z plus Return' |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 236 | else: |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 237 | eof = 'Ctrl-D (i.e. EOF)' |
Tim Peters | 88ca467 | 2006-03-10 23:39:56 +0000 | [diff] [blame] | 238 | |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 239 | class Quitter(object): |
| 240 | def __init__(self, name): |
| 241 | self.name = name |
| 242 | def __repr__(self): |
| 243 | return 'Use %s() or %s to exit' % (self.name, eof) |
| 244 | def __call__(self, code=None): |
Kurt B. Kaiser | d112bc7 | 2006-08-16 05:01:42 +0000 | [diff] [blame] | 245 | # Shells like IDLE catch the SystemExit, but listen when their |
| 246 | # stdin wrapper is closed. |
| 247 | try: |
| 248 | sys.stdin.close() |
| 249 | except: |
| 250 | pass |
Georg Brandl | 24cb053 | 2006-03-09 23:22:06 +0000 | [diff] [blame] | 251 | raise SystemExit(code) |
| 252 | __builtin__.quit = Quitter('quit') |
| 253 | __builtin__.exit = Quitter('exit') |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 254 | |
| 255 | |
| 256 | class _Printer(object): |
| 257 | """interactive prompt objects for printing the license text, a list of |
| 258 | contributors and the copyright notice.""" |
| 259 | |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 260 | MAXLINES = 23 |
| 261 | |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 262 | def __init__(self, name, data, files=(), dirs=()): |
| 263 | self.__name = name |
| 264 | self.__data = data |
| 265 | self.__files = files |
| 266 | self.__dirs = dirs |
| 267 | self.__lines = None |
| 268 | |
| 269 | def __setup(self): |
| 270 | if self.__lines: |
| 271 | return |
| 272 | data = None |
| 273 | for dir in self.__dirs: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 274 | for filename in self.__files: |
| 275 | filename = os.path.join(dir, filename) |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 276 | try: |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 277 | fp = file(filename, "rU") |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 278 | data = fp.read() |
| 279 | fp.close() |
| 280 | break |
| 281 | except IOError: |
| 282 | pass |
| 283 | if data: |
| 284 | break |
| 285 | if not data: |
| 286 | data = self.__data |
| 287 | self.__lines = data.split('\n') |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 288 | self.__linecnt = len(self.__lines) |
| 289 | |
| 290 | def __repr__(self): |
Guido van Rossum | f19a7ac | 2000-10-03 17:11:37 +0000 | [diff] [blame] | 291 | self.__setup() |
| 292 | if len(self.__lines) <= self.MAXLINES: |
| 293 | return "\n".join(self.__lines) |
| 294 | else: |
| 295 | return "Type %s() to see the full %s text" % ((self.__name,)*2) |
| 296 | |
| 297 | def __call__(self): |
| 298 | self.__setup() |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 299 | prompt = 'Hit Return for more, or q (and Return) to quit: ' |
| 300 | lineno = 0 |
| 301 | while 1: |
| 302 | try: |
| 303 | for i in range(lineno, lineno + self.MAXLINES): |
| 304 | print self.__lines[i] |
| 305 | except IndexError: |
| 306 | break |
| 307 | else: |
| 308 | lineno += self.MAXLINES |
| 309 | key = None |
| 310 | while key is None: |
| 311 | key = raw_input(prompt) |
| 312 | if key not in ('', 'q'): |
| 313 | key = None |
| 314 | if key == 'q': |
| 315 | break |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 316 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 317 | def setcopyright(): |
| 318 | """Set 'copyright' and 'credits' in __builtin__""" |
| 319 | __builtin__.copyright = _Printer("copyright", sys.copyright) |
| 320 | if sys.platform[:4] == 'java': |
| 321 | __builtin__.credits = _Printer( |
| 322 | "credits", |
| 323 | "Jython is maintained by the Jython developers (www.jython.org).") |
| 324 | else: |
| 325 | __builtin__.credits = _Printer("credits", """\ |
| 326 | Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands |
| 327 | for supporting Python development. See www.python.org for more information.""") |
| 328 | here = os.path.dirname(os.__file__) |
| 329 | __builtin__.license = _Printer( |
| 330 | "license", "See http://www.python.org/%.3s/license.html" % sys.version, |
| 331 | ["LICENSE.txt", "LICENSE"], |
| 332 | [os.path.join(here, os.pardir), here, os.curdir]) |
Guido van Rossum | d125239 | 2000-09-05 04:39:55 +0000 | [diff] [blame] | 333 | |
| 334 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 335 | class _Helper(object): |
| 336 | """Define the built-in 'help'. |
| 337 | This is a wrapper around pydoc.help (with a twist). |
Guido van Rossum | 83213cc | 2001-06-12 16:48:52 +0000 | [diff] [blame] | 338 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 339 | """ |
| 340 | |
Guido van Rossum | 83213cc | 2001-06-12 16:48:52 +0000 | [diff] [blame] | 341 | def __repr__(self): |
| 342 | return "Type help() for interactive help, " \ |
| 343 | "or help(object) for help about object." |
| 344 | def __call__(self, *args, **kwds): |
| 345 | import pydoc |
| 346 | return pydoc.help(*args, **kwds) |
| 347 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 348 | def sethelper(): |
| 349 | __builtin__.help = _Helper() |
| 350 | |
| 351 | def aliasmbcs(): |
| 352 | """On Windows, some default encodings are not provided by Python, |
| 353 | while they are always available as "mbcs" in each locale. Make |
| 354 | them usable by aliasing to "mbcs" in such a case.""" |
| 355 | if sys.platform == 'win32': |
| 356 | import locale, codecs |
| 357 | enc = locale.getdefaultlocale()[1] |
| 358 | if enc.startswith('cp'): # "cp***" ? |
| 359 | try: |
| 360 | codecs.lookup(enc) |
| 361 | except LookupError: |
| 362 | import encodings |
| 363 | encodings._cache[enc] = encodings._unknown |
| 364 | encodings.aliases.aliases[enc] = 'mbcs' |
| 365 | |
| 366 | def setencoding(): |
| 367 | """Set the string encoding used by the Unicode implementation. The |
| 368 | default is 'ascii', but if you're willing to experiment, you can |
| 369 | change this.""" |
| 370 | encoding = "ascii" # Default value set by _PyUnicode_Init() |
| 371 | if 0: |
| 372 | # Enable to support locale aware default string encodings. |
| 373 | import locale |
| 374 | loc = locale.getdefaultlocale() |
| 375 | if loc[1]: |
| 376 | encoding = loc[1] |
| 377 | if 0: |
| 378 | # Enable to switch off string to Unicode coercion and implicit |
| 379 | # Unicode to string conversion. |
| 380 | encoding = "undefined" |
| 381 | if encoding != "ascii": |
| 382 | # On Non-Unicode builds this will raise an AttributeError... |
| 383 | sys.setdefaultencoding(encoding) # Needs Python Unicode build ! |
Guido van Rossum | 83213cc | 2001-06-12 16:48:52 +0000 | [diff] [blame] | 384 | |
| 385 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 386 | def execsitecustomize(): |
| 387 | """Run custom site specific code, if available.""" |
| 388 | try: |
| 389 | import sitecustomize |
| 390 | except ImportError: |
| 391 | pass |
Martin v. Löwis | 4eab486 | 2003-03-03 09:34:01 +0000 | [diff] [blame] | 392 | |
Martin v. Löwis | 4eab486 | 2003-03-03 09:34:01 +0000 | [diff] [blame] | 393 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 394 | def main(): |
| 395 | abs__file__() |
| 396 | paths_in_sys = removeduppaths() |
| 397 | if (os.name == "posix" and sys.path and |
| 398 | os.path.basename(sys.path[-1]) == "Modules"): |
| 399 | addbuilddir() |
| 400 | paths_in_sys = addsitepackages(paths_in_sys) |
| 401 | if sys.platform == 'os2emx': |
| 402 | setBEGINLIBPATH() |
| 403 | setquit() |
| 404 | setcopyright() |
| 405 | sethelper() |
| 406 | aliasmbcs() |
| 407 | setencoding() |
| 408 | execsitecustomize() |
| 409 | # Remove sys.setdefaultencoding() so that users cannot change the |
| 410 | # encoding after initialization. The test for presence is needed when |
| 411 | # this module is run as a script, because this code is executed twice. |
| 412 | if hasattr(sys, "setdefaultencoding"): |
| 413 | del sys.setdefaultencoding |
Marc-André Lemburg | 990bbe9 | 2000-06-07 09:12:09 +0000 | [diff] [blame] | 414 | |
Brett Cannon | 0096e26 | 2004-06-05 01:12:51 +0000 | [diff] [blame] | 415 | main() |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 416 | |
| 417 | def _test(): |
| 418 | print "sys.path = [" |
| 419 | for dir in sys.path: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 420 | print " %r," % (dir,) |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 421 | print "]" |
| 422 | |
| 423 | if __name__ == '__main__': |
| 424 | _test() |