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