blob: 3b51e81d4a1be2acd3b50960e50884626d5c3761 [file] [log] [blame]
Guido van Rossumf30bec71997-08-29 22:30:45 +00001"""Append module search paths for third-party packages to sys.path.
Guido van Rossume57c96e1996-08-17 19:56:26 +00002
Guido van Rossumf30bec71997-08-29 22:30:45 +00003****************************************************************
4* This module is automatically imported during initialization. *
5****************************************************************
Guido van Rossume57c96e1996-08-17 19:56:26 +00006
Guido van Rossumf30bec71997-08-29 22:30:45 +00007In earlier versions of Python (up to 1.5a3), scripts or modules that
8needed to use site-specific modules would place ``import site''
9somewhere near the top of their code. Because of the automatic
10import, this is no longer necessary (but code that does it still
11works).
Guido van Rossume57c96e1996-08-17 19:56:26 +000012
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +000013This will append site-specific paths to the module search path. On
Nick Coghlanf2b16f32006-06-12 08:23:02 +000014Unix (including Mac OSX), it starts with sys.prefix and
15sys.exec_prefix (if different) and appends
16lib/python<version>/site-packages as well as lib/site-python.
17On other platforms (such as Windows), it tries each of the
Nick Coghlan3fb55ca2006-06-12 08:19:37 +000018prefixes directly, as well as with lib/site-packages appended. The
Guido van Rossum62b297b1997-09-08 02:14:09 +000019resulting directories, if they exist, are appended to sys.path, and
20also inspected for path configuration files.
Guido van Rossume57c96e1996-08-17 19:56:26 +000021
Guido van Rossumf30bec71997-08-29 22:30:45 +000022A path configuration file is a file whose name has the form
23<package>.pth; its contents are additional directories (one per line)
24to be added to sys.path. Non-existing directories (or
25non-directories) are never added to sys.path; no directory is added to
26sys.path more than once. Blank lines and lines beginning with
Guido van Rossumfacf24b2001-12-17 16:07:06 +000027'#' are skipped. Lines starting with 'import' are executed.
Guido van Rossume57c96e1996-08-17 19:56:26 +000028
Guido van Rossumf30bec71997-08-29 22:30:45 +000029For example, suppose sys.prefix and sys.exec_prefix are set to
Neal Norwitz6e482562006-08-15 04:59:30 +000030/usr/local and there is a directory /usr/local/lib/python2.5/site-packages
Guido van Rossum62b297b1997-09-08 02:14:09 +000031with three subdirectories, foo, bar and spam, and two path
32configuration files, foo.pth and bar.pth. Assume foo.pth contains the
33following:
Guido van Rossumf30bec71997-08-29 22:30:45 +000034
35 # foo package configuration
36 foo
37 bar
38 bletch
39
40and bar.pth contains:
41
42 # bar package configuration
43 bar
44
45Then the following directories are added to sys.path, in this order:
46
Neal Norwitz6e482562006-08-15 04:59:30 +000047 /usr/local/lib/python2.5/site-packages/bar
48 /usr/local/lib/python2.5/site-packages/foo
Guido van Rossumf30bec71997-08-29 22:30:45 +000049
50Note that bletch is omitted because it doesn't exist; bar precedes foo
51because bar.pth comes alphabetically before foo.pth; and spam is
52omitted because it is not mentioned in either path configuration file.
Guido van Rossume57c96e1996-08-17 19:56:26 +000053
54After these path manipulations, an attempt is made to import a module
Guido van Rossumf30bec71997-08-29 22:30:45 +000055named sitecustomize, which can perform arbitrary additional
56site-specific customizations. If this import fails with an
57ImportError exception, it is silently ignored.
Guido van Rossume57c96e1996-08-17 19:56:26 +000058
Guido van Rossume57c96e1996-08-17 19:56:26 +000059"""
60
Brett Cannon0096e262004-06-05 01:12:51 +000061import sys
62import os
63import __builtin__
R. David Murray5874ed62010-12-26 22:29:53 +000064import traceback
Guido van Rossume57c96e1996-08-17 19:56:26 +000065
Christian Heimesaf748c32008-05-06 22:41:46 +000066# Prefixes for site-packages; add additional prefixes like /usr/local here
67PREFIXES = [sys.prefix, sys.exec_prefix]
68# Enable per user site-packages directory
69# set it to False to disable the feature or True to force the feature
70ENABLE_USER_SITE = None
Tarek Ziadé764fc232009-08-20 21:23:13 +000071
Christian Heimesaf748c32008-05-06 22:41:46 +000072# for distutils.commands.install
Tarek Ziadé764fc232009-08-20 21:23:13 +000073# These values are initialized by the getuserbase() and getusersitepackages()
74# functions, through the main() function when Python starts.
Christian Heimesaf748c32008-05-06 22:41:46 +000075USER_SITE = None
76USER_BASE = None
77
Guido van Rossumd74fb6b2001-03-02 06:43:49 +000078
Fred Drake38cb9f12000-09-28 16:52:36 +000079def makepath(*paths):
Victor Stinnerd2f6ae62010-10-12 22:53:51 +000080 dir = os.path.join(*paths)
81 try:
82 dir = os.path.abspath(dir)
83 except OSError:
84 pass
Fred Drake1fb5ce02001-07-02 16:55:42 +000085 return dir, os.path.normcase(dir)
Fred Drake38cb9f12000-09-28 16:52:36 +000086
Christian Heimesaf748c32008-05-06 22:41:46 +000087
Brett Cannon0096e262004-06-05 01:12:51 +000088def abs__file__():
89 """Set all module' __file__ attribute to an absolute path"""
90 for m in sys.modules.values():
Neal Norwitz0c469852006-04-11 07:21:20 +000091 if hasattr(m, '__loader__'):
Phillip J. Eby47032112006-04-11 01:07:43 +000092 continue # don't mess with a PEP 302-supplied __file__
Brett Cannon0096e262004-06-05 01:12:51 +000093 try:
94 m.__file__ = os.path.abspath(m.__file__)
Victor Stinnerd2f6ae62010-10-12 22:53:51 +000095 except (AttributeError, OSError):
96 pass
Fred Drake38cb9f12000-09-28 16:52:36 +000097
Christian Heimesaf748c32008-05-06 22:41:46 +000098
Brett Cannon0096e262004-06-05 01:12:51 +000099def removeduppaths():
100 """ Remove duplicate entries from sys.path along with making them
101 absolute"""
102 # This ensures that the initial path provided by the interpreter contains
103 # only absolute pathnames, even if we're running from the build directory.
104 L = []
105 known_paths = set()
106 for dir in sys.path:
107 # Filter out duplicate paths (on case-insensitive file systems also
108 # if they only differ in case); turn relative paths into absolute
109 # paths.
110 dir, dircase = makepath(dir)
111 if not dircase in known_paths:
112 L.append(dir)
113 known_paths.add(dircase)
114 sys.path[:] = L
115 return known_paths
Fred Drake38cb9f12000-09-28 16:52:36 +0000116
Christian Heimesaf748c32008-05-06 22:41:46 +0000117
Fred Drake7f5296e2001-07-20 20:06:17 +0000118def _init_pathinfo():
Brett Cannon0096e262004-06-05 01:12:51 +0000119 """Return a set containing all existing directory entries from sys.path"""
120 d = set()
Fred Drake7f5296e2001-07-20 20:06:17 +0000121 for dir in sys.path:
Brett Cannon0096e262004-06-05 01:12:51 +0000122 try:
123 if os.path.isdir(dir):
124 dir, dircase = makepath(dir)
125 d.add(dircase)
126 except TypeError:
Fred Drake7f5296e2001-07-20 20:06:17 +0000127 continue
Brett Cannon0096e262004-06-05 01:12:51 +0000128 return d
Fred Drake7f5296e2001-07-20 20:06:17 +0000129
Christian Heimesaf748c32008-05-06 22:41:46 +0000130
Brett Cannon0096e262004-06-05 01:12:51 +0000131def addpackage(sitedir, name, known_paths):
Georg Brandl8d76cca2007-05-19 18:09:26 +0000132 """Process a .pth file within the site-packages directory:
133 For each line in the file, either combine it with sitedir to a path
134 and add that to known_paths, or execute it if it starts with 'import '.
135 """
Brett Cannon0096e262004-06-05 01:12:51 +0000136 if known_paths is None:
Fred Drake7f5296e2001-07-20 20:06:17 +0000137 _init_pathinfo()
138 reset = 1
139 else:
140 reset = 0
Brett Cannon0096e262004-06-05 01:12:51 +0000141 fullname = os.path.join(sitedir, name)
142 try:
Brett Cannon4d0bddf2004-07-20 02:28:28 +0000143 f = open(fullname, "rU")
Brett Cannon0096e262004-06-05 01:12:51 +0000144 except IOError:
145 return
Christian Heimesaf748c32008-05-06 22:41:46 +0000146 with f:
R. David Murray5874ed62010-12-26 22:29:53 +0000147 for n, line in enumerate(f):
Brett Cannon0096e262004-06-05 01:12:51 +0000148 if line.startswith("#"):
149 continue
R. David Murray5874ed62010-12-26 22:29:53 +0000150 try:
151 if line.startswith(("import ", "import\t")):
152 exec line
153 continue
154 line = line.rstrip()
155 dir, dircase = makepath(sitedir, line)
156 if not dircase in known_paths and os.path.exists(dir):
157 sys.path.append(dir)
158 known_paths.add(dircase)
159 except Exception as err:
160 print >>sys.stderr, "Error processing line {:d} of {}:\n".format(
161 n+1, fullname)
162 for record in traceback.format_exception(*sys.exc_info()):
163 for line in record.splitlines():
164 print >>sys.stderr, ' '+line
165 print >>sys.stderr, "\nRemainder of file ignored"
166 break
Brett Cannon0096e262004-06-05 01:12:51 +0000167 if reset:
168 known_paths = None
169 return known_paths
170
Christian Heimesaf748c32008-05-06 22:41:46 +0000171
Brett Cannon12f8c4d2004-07-09 23:38:18 +0000172def addsitedir(sitedir, known_paths=None):
Brett Cannon0096e262004-06-05 01:12:51 +0000173 """Add 'sitedir' argument to sys.path if missing and handle .pth files in
174 'sitedir'"""
175 if known_paths is None:
Brett Cannon4d0bddf2004-07-20 02:28:28 +0000176 known_paths = _init_pathinfo()
Brett Cannon0096e262004-06-05 01:12:51 +0000177 reset = 1
178 else:
179 reset = 0
Fred Drake1fb5ce02001-07-02 16:55:42 +0000180 sitedir, sitedircase = makepath(sitedir)
Brett Cannon0096e262004-06-05 01:12:51 +0000181 if not sitedircase in known_paths:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000182 sys.path.append(sitedir) # Add path component
Guido van Rossumf30bec71997-08-29 22:30:45 +0000183 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000184 names = os.listdir(sitedir)
Guido van Rossumf30bec71997-08-29 22:30:45 +0000185 except os.error:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000186 return
Christian Heimesaf748c32008-05-06 22:41:46 +0000187 dotpth = os.extsep + "pth"
188 names = [name for name in names if name.endswith(dotpth)]
189 for name in sorted(names):
190 addpackage(sitedir, name, known_paths)
Fred Drake7f5296e2001-07-20 20:06:17 +0000191 if reset:
Brett Cannon0096e262004-06-05 01:12:51 +0000192 known_paths = None
193 return known_paths
Guido van Rossumf30bec71997-08-29 22:30:45 +0000194
Christian Heimesaf748c32008-05-06 22:41:46 +0000195
196def check_enableusersite():
197 """Check if user site directory is safe for inclusion
198
Andrew M. Kuchling5217d5d2008-05-10 17:36:24 +0000199 The function tests for the command line flag (including environment var),
Christian Heimesaf748c32008-05-06 22:41:46 +0000200 process uid/gid equal to effective uid/gid.
201
202 None: Disabled for security reasons
203 False: Disabled by user (command line option)
204 True: Safe and enabled
205 """
206 if sys.flags.no_user_site:
207 return False
208
209 if hasattr(os, "getuid") and hasattr(os, "geteuid"):
210 # check process uid == effective uid
211 if os.geteuid() != os.getuid():
212 return None
213 if hasattr(os, "getgid") and hasattr(os, "getegid"):
214 # check process gid == effective gid
215 if os.getegid() != os.getgid():
216 return None
217
218 return True
219
Tarek Ziadé764fc232009-08-20 21:23:13 +0000220def getuserbase():
221 """Returns the `user base` directory path.
222
223 The `user base` directory can be used to store data. If the global
224 variable ``USER_BASE`` is not initialized yet, this function will also set
225 it.
226 """
227 global USER_BASE
228 if USER_BASE is not None:
229 return USER_BASE
Tarek Ziadé5633a802010-01-23 09:23:15 +0000230 from sysconfig import get_config_var
231 USER_BASE = get_config_var('userbase')
Tarek Ziadé764fc232009-08-20 21:23:13 +0000232 return USER_BASE
233
234def getusersitepackages():
235 """Returns the user-specific site-packages directory path.
236
237 If the global variable ``USER_SITE`` is not initialized yet, this
238 function will also set it.
239 """
240 global USER_SITE
241 user_base = getuserbase() # this will also set USER_BASE
242
243 if USER_SITE is not None:
244 return USER_SITE
245
Tarek Ziadé5633a802010-01-23 09:23:15 +0000246 from sysconfig import get_path
247 import os
Ronald Oussoren2f88bfd2010-05-08 10:29:06 +0000248
249 if sys.platform == 'darwin':
250 from sysconfig import get_config_var
251 if get_config_var('PYTHONFRAMEWORK'):
252 USER_SITE = get_path('purelib', 'osx_framework_user')
253 return USER_SITE
254
Tarek Ziadé5633a802010-01-23 09:23:15 +0000255 USER_SITE = get_path('purelib', '%s_user' % os.name)
Tarek Ziadé764fc232009-08-20 21:23:13 +0000256 return USER_SITE
Christian Heimesaf748c32008-05-06 22:41:46 +0000257
258def addusersitepackages(known_paths):
259 """Add a per user site-package to sys.path
260
261 Each user has its own python directory with site-packages in the
262 home directory.
Christian Heimesaf748c32008-05-06 22:41:46 +0000263 """
Tarek Ziadé764fc232009-08-20 21:23:13 +0000264 # get the per user site-package path
265 # this call will also make sure USER_BASE and USER_SITE are set
266 user_site = getusersitepackages()
Christian Heimesaf748c32008-05-06 22:41:46 +0000267
Tarek Ziadé764fc232009-08-20 21:23:13 +0000268 if ENABLE_USER_SITE and os.path.isdir(user_site):
269 addsitedir(user_site, known_paths)
Christian Heimesaf748c32008-05-06 22:41:46 +0000270 return known_paths
271
Tarek Ziadé764fc232009-08-20 21:23:13 +0000272def getsitepackages():
273 """Returns a list containing all global site-packages directories
274 (and possibly site-python).
Christian Heimesaf748c32008-05-06 22:41:46 +0000275
Tarek Ziadé764fc232009-08-20 21:23:13 +0000276 For each directory present in the global ``PREFIXES``, this function
277 will find its `site-packages` subdirectory depending on the system
278 environment, and will return a list of full paths.
279 """
280 sitepackages = []
Benjamin Peterson3b959342010-06-03 21:21:03 +0000281 seen = set()
Christian Heimesaf748c32008-05-06 22:41:46 +0000282
283 for prefix in PREFIXES:
284 if not prefix or prefix in seen:
285 continue
Benjamin Peterson3b959342010-06-03 21:21:03 +0000286 seen.add(prefix)
Christian Heimesaf748c32008-05-06 22:41:46 +0000287
288 if sys.platform in ('os2emx', 'riscos'):
Tarek Ziadé764fc232009-08-20 21:23:13 +0000289 sitepackages.append(os.path.join(prefix, "Lib", "site-packages"))
Christian Heimesaf748c32008-05-06 22:41:46 +0000290 elif os.sep == '/':
Tarek Ziadé764fc232009-08-20 21:23:13 +0000291 sitepackages.append(os.path.join(prefix, "lib",
Christian Heimesaf748c32008-05-06 22:41:46 +0000292 "python" + sys.version[:3],
293 "site-packages"))
Tarek Ziadé764fc232009-08-20 21:23:13 +0000294 sitepackages.append(os.path.join(prefix, "lib", "site-python"))
Christian Heimesaf748c32008-05-06 22:41:46 +0000295 else:
Tarek Ziadé764fc232009-08-20 21:23:13 +0000296 sitepackages.append(prefix)
297 sitepackages.append(os.path.join(prefix, "lib", "site-packages"))
Tarek Ziadé764fc232009-08-20 21:23:13 +0000298 return sitepackages
Christian Heimesaf748c32008-05-06 22:41:46 +0000299
Tarek Ziadé764fc232009-08-20 21:23:13 +0000300def addsitepackages(known_paths):
301 """Add site-packages (and possibly site-python) to sys.path"""
302 for sitedir in getsitepackages():
Christian Heimesaf748c32008-05-06 22:41:46 +0000303 if os.path.isdir(sitedir):
304 addsitedir(sitedir, known_paths)
305
306 return known_paths
Fred Drake7f5296e2001-07-20 20:06:17 +0000307
Brett Cannon0096e262004-06-05 01:12:51 +0000308def setBEGINLIBPATH():
309 """The OS/2 EMX port has optional extension modules that do double duty
310 as DLLs (and must use the .DLL file extension) for other extensions.
311 The library search path needs to be amended so these will be found
312 during module import. Use BEGINLIBPATH so that these are at the start
313 of the library search path.
Tim Peters4e0e1b62004-07-07 20:54:48 +0000314
Brett Cannon0096e262004-06-05 01:12:51 +0000315 """
Andrew MacIntyre2e8a6e02003-12-02 12:27:25 +0000316 dllpath = os.path.join(sys.prefix, "Lib", "lib-dynload")
317 libpath = os.environ['BEGINLIBPATH'].split(';')
318 if libpath[-1]:
319 libpath.append(dllpath)
320 else:
321 libpath[-1] = dllpath
322 os.environ['BEGINLIBPATH'] = ';'.join(libpath)
323
324
Brett Cannon0096e262004-06-05 01:12:51 +0000325def setquit():
Brian Curtinbc96f322010-04-12 23:30:49 +0000326 """Define new builtins 'quit' and 'exit'.
327
328 These are objects which make the interpreter exit when called.
329 The repr of each object contains a hint at how it works.
Guido van Rossumd89fa0c1998-08-07 18:01:14 +0000330
Brett Cannon0096e262004-06-05 01:12:51 +0000331 """
332 if os.sep == ':':
Georg Brandl24cb0532006-03-09 23:22:06 +0000333 eof = 'Cmd-Q'
Brett Cannon0096e262004-06-05 01:12:51 +0000334 elif os.sep == '\\':
Georg Brandl24cb0532006-03-09 23:22:06 +0000335 eof = 'Ctrl-Z plus Return'
Brett Cannon0096e262004-06-05 01:12:51 +0000336 else:
Georg Brandl24cb0532006-03-09 23:22:06 +0000337 eof = 'Ctrl-D (i.e. EOF)'
Tim Peters88ca4672006-03-10 23:39:56 +0000338
Georg Brandl24cb0532006-03-09 23:22:06 +0000339 class Quitter(object):
340 def __init__(self, name):
341 self.name = name
342 def __repr__(self):
343 return 'Use %s() or %s to exit' % (self.name, eof)
344 def __call__(self, code=None):
Kurt B. Kaiserd112bc72006-08-16 05:01:42 +0000345 # Shells like IDLE catch the SystemExit, but listen when their
346 # stdin wrapper is closed.
347 try:
348 sys.stdin.close()
349 except:
350 pass
Georg Brandl24cb0532006-03-09 23:22:06 +0000351 raise SystemExit(code)
352 __builtin__.quit = Quitter('quit')
353 __builtin__.exit = Quitter('exit')
Brett Cannon0096e262004-06-05 01:12:51 +0000354
355
356class _Printer(object):
357 """interactive prompt objects for printing the license text, a list of
358 contributors and the copyright notice."""
359
Guido van Rossumd1252392000-09-05 04:39:55 +0000360 MAXLINES = 23
361
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000362 def __init__(self, name, data, files=(), dirs=()):
363 self.__name = name
364 self.__data = data
365 self.__files = files
366 self.__dirs = dirs
367 self.__lines = None
368
369 def __setup(self):
370 if self.__lines:
371 return
372 data = None
373 for dir in self.__dirs:
Brett Cannon0096e262004-06-05 01:12:51 +0000374 for filename in self.__files:
375 filename = os.path.join(dir, filename)
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000376 try:
Brett Cannon0096e262004-06-05 01:12:51 +0000377 fp = file(filename, "rU")
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000378 data = fp.read()
379 fp.close()
380 break
381 except IOError:
382 pass
383 if data:
384 break
385 if not data:
386 data = self.__data
387 self.__lines = data.split('\n')
Guido van Rossumd1252392000-09-05 04:39:55 +0000388 self.__linecnt = len(self.__lines)
389
390 def __repr__(self):
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000391 self.__setup()
392 if len(self.__lines) <= self.MAXLINES:
393 return "\n".join(self.__lines)
394 else:
395 return "Type %s() to see the full %s text" % ((self.__name,)*2)
396
397 def __call__(self):
398 self.__setup()
Guido van Rossumd1252392000-09-05 04:39:55 +0000399 prompt = 'Hit Return for more, or q (and Return) to quit: '
400 lineno = 0
401 while 1:
402 try:
403 for i in range(lineno, lineno + self.MAXLINES):
404 print self.__lines[i]
405 except IndexError:
406 break
407 else:
408 lineno += self.MAXLINES
409 key = None
410 while key is None:
411 key = raw_input(prompt)
412 if key not in ('', 'q'):
413 key = None
414 if key == 'q':
415 break
Guido van Rossumd1252392000-09-05 04:39:55 +0000416
Brett Cannon0096e262004-06-05 01:12:51 +0000417def setcopyright():
418 """Set 'copyright' and 'credits' in __builtin__"""
419 __builtin__.copyright = _Printer("copyright", sys.copyright)
420 if sys.platform[:4] == 'java':
421 __builtin__.credits = _Printer(
422 "credits",
423 "Jython is maintained by the Jython developers (www.jython.org).")
424 else:
425 __builtin__.credits = _Printer("credits", """\
426 Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
427 for supporting Python development. See www.python.org for more information.""")
428 here = os.path.dirname(os.__file__)
429 __builtin__.license = _Printer(
Benjamin Peterson75461e32015-02-01 20:17:22 -0500430 "license", "See https://www.python.org/psf/license/",
Brett Cannon0096e262004-06-05 01:12:51 +0000431 ["LICENSE.txt", "LICENSE"],
432 [os.path.join(here, os.pardir), here, os.curdir])
Guido van Rossumd1252392000-09-05 04:39:55 +0000433
434
Brett Cannon0096e262004-06-05 01:12:51 +0000435class _Helper(object):
Brian Curtinbc96f322010-04-12 23:30:49 +0000436 """Define the builtin 'help'.
Brett Cannon0096e262004-06-05 01:12:51 +0000437 This is a wrapper around pydoc.help (with a twist).
Guido van Rossum83213cc2001-06-12 16:48:52 +0000438
Brett Cannon0096e262004-06-05 01:12:51 +0000439 """
440
Guido van Rossum83213cc2001-06-12 16:48:52 +0000441 def __repr__(self):
442 return "Type help() for interactive help, " \
443 "or help(object) for help about object."
444 def __call__(self, *args, **kwds):
445 import pydoc
446 return pydoc.help(*args, **kwds)
447
Brett Cannon0096e262004-06-05 01:12:51 +0000448def sethelper():
449 __builtin__.help = _Helper()
450
451def aliasmbcs():
452 """On Windows, some default encodings are not provided by Python,
453 while they are always available as "mbcs" in each locale. Make
454 them usable by aliasing to "mbcs" in such a case."""
455 if sys.platform == 'win32':
456 import locale, codecs
457 enc = locale.getdefaultlocale()[1]
458 if enc.startswith('cp'): # "cp***" ?
459 try:
460 codecs.lookup(enc)
461 except LookupError:
462 import encodings
463 encodings._cache[enc] = encodings._unknown
464 encodings.aliases.aliases[enc] = 'mbcs'
465
466def setencoding():
467 """Set the string encoding used by the Unicode implementation. The
468 default is 'ascii', but if you're willing to experiment, you can
469 change this."""
470 encoding = "ascii" # Default value set by _PyUnicode_Init()
471 if 0:
472 # Enable to support locale aware default string encodings.
473 import locale
474 loc = locale.getdefaultlocale()
475 if loc[1]:
476 encoding = loc[1]
477 if 0:
478 # Enable to switch off string to Unicode coercion and implicit
479 # Unicode to string conversion.
480 encoding = "undefined"
481 if encoding != "ascii":
482 # On Non-Unicode builds this will raise an AttributeError...
483 sys.setdefaultencoding(encoding) # Needs Python Unicode build !
Guido van Rossum83213cc2001-06-12 16:48:52 +0000484
485
Brett Cannon0096e262004-06-05 01:12:51 +0000486def execsitecustomize():
487 """Run custom site specific code, if available."""
488 try:
489 import sitecustomize
490 except ImportError:
491 pass
Victor Stinner66644262010-03-10 22:30:19 +0000492 except Exception:
493 if sys.flags.verbose:
494 sys.excepthook(*sys.exc_info())
495 else:
496 print >>sys.stderr, \
497 "'import sitecustomize' failed; use -v for traceback"
Martin v. Löwis4eab4862003-03-03 09:34:01 +0000498
Martin v. Löwis4eab4862003-03-03 09:34:01 +0000499
Christian Heimesaf748c32008-05-06 22:41:46 +0000500def execusercustomize():
501 """Run custom user specific code, if available."""
502 try:
503 import usercustomize
504 except ImportError:
505 pass
Victor Stinner66644262010-03-10 22:30:19 +0000506 except Exception:
507 if sys.flags.verbose:
508 sys.excepthook(*sys.exc_info())
509 else:
510 print>>sys.stderr, \
Victor Stinner3ec32002010-03-10 22:45:04 +0000511 "'import usercustomize' failed; use -v for traceback"
Christian Heimesaf748c32008-05-06 22:41:46 +0000512
513
Brett Cannon0096e262004-06-05 01:12:51 +0000514def main():
Christian Heimesaf748c32008-05-06 22:41:46 +0000515 global ENABLE_USER_SITE
516
Brett Cannon0096e262004-06-05 01:12:51 +0000517 abs__file__()
Christian Heimesaf748c32008-05-06 22:41:46 +0000518 known_paths = removeduppaths()
Christian Heimesaf748c32008-05-06 22:41:46 +0000519 if ENABLE_USER_SITE is None:
520 ENABLE_USER_SITE = check_enableusersite()
521 known_paths = addusersitepackages(known_paths)
522 known_paths = addsitepackages(known_paths)
Brett Cannon0096e262004-06-05 01:12:51 +0000523 if sys.platform == 'os2emx':
524 setBEGINLIBPATH()
525 setquit()
526 setcopyright()
527 sethelper()
528 aliasmbcs()
529 setencoding()
530 execsitecustomize()
Christian Heimesaf748c32008-05-06 22:41:46 +0000531 if ENABLE_USER_SITE:
532 execusercustomize()
Brett Cannon0096e262004-06-05 01:12:51 +0000533 # Remove sys.setdefaultencoding() so that users cannot change the
534 # encoding after initialization. The test for presence is needed when
535 # this module is run as a script, because this code is executed twice.
536 if hasattr(sys, "setdefaultencoding"):
537 del sys.setdefaultencoding
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000538
Brett Cannon0096e262004-06-05 01:12:51 +0000539main()
Guido van Rossumf30bec71997-08-29 22:30:45 +0000540
Christian Heimesaf748c32008-05-06 22:41:46 +0000541def _script():
542 help = """\
543 %s [--user-base] [--user-site]
544
545 Without arguments print some useful information
546 With arguments print the value of USER_BASE and/or USER_SITE separated
547 by '%s'.
548
549 Exit codes with --user-base or --user-site:
550 0 - user site directory is enabled
Christian Heimes17433d22008-05-09 12:19:09 +0000551 1 - user site directory is disabled by user
Christian Heimesaf748c32008-05-06 22:41:46 +0000552 2 - uses site directory is disabled by super user
553 or for security reasons
554 >2 - unknown error
555 """
556 args = sys.argv[1:]
557 if not args:
558 print "sys.path = ["
559 for dir in sys.path:
560 print " %r," % (dir,)
561 print "]"
562 print "USER_BASE: %r (%s)" % (USER_BASE,
563 "exists" if os.path.isdir(USER_BASE) else "doesn't exist")
564 print "USER_SITE: %r (%s)" % (USER_SITE,
565 "exists" if os.path.isdir(USER_SITE) else "doesn't exist")
566 print "ENABLE_USER_SITE: %r" % ENABLE_USER_SITE
567 sys.exit(0)
568
569 buffer = []
570 if '--user-base' in args:
571 buffer.append(USER_BASE)
572 if '--user-site' in args:
573 buffer.append(USER_SITE)
574
575 if buffer:
576 print os.pathsep.join(buffer)
577 if ENABLE_USER_SITE:
578 sys.exit(0)
579 elif ENABLE_USER_SITE is False:
580 sys.exit(1)
581 elif ENABLE_USER_SITE is None:
582 sys.exit(2)
583 else:
584 sys.exit(3)
585 else:
586 import textwrap
587 print textwrap.dedent(help % (sys.argv[0], os.pathsep))
588 sys.exit(10)
Guido van Rossumf30bec71997-08-29 22:30:45 +0000589
590if __name__ == '__main__':
Christian Heimesaf748c32008-05-06 22:41:46 +0000591 _script()