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