Mercurial > cpython-lru_cache-weakref
annotate Lib/site.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 82c4f094f811 |
| children | be3b4aa2ad28 |
| rev | line source |
|---|---|
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
1 """Append module search paths for third-party packages to sys.path. |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
2 |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
3 **************************************************************** |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
4 * This module is automatically imported during initialization. * |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
5 **************************************************************** |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
6 |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
7 In earlier versions of Python (up to 1.5a3), scripts or modules that |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
8 needed to use site-specific modules would place ``import site'' |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
9 somewhere near the top of their code. Because of the automatic |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
10 import, this is no longer necessary (but code that does it still |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
11 works). |
| 4010 | 12 |
| 30723 | 13 This will append site-specific paths to the module search path. On |
|
38776
8fe373fb21d4
Fix site module docstring to match the code for Mac OSX, too
Nick Coghlan <ncoghlan@gmail.com>
parents:
38775
diff
changeset
|
14 Unix (including Mac OSX), it starts with sys.prefix and |
|
8fe373fb21d4
Fix site module docstring to match the code for Mac OSX, too
Nick Coghlan <ncoghlan@gmail.com>
parents:
38775
diff
changeset
|
15 sys.exec_prefix (if different) and appends |
|
8fe373fb21d4
Fix site module docstring to match the code for Mac OSX, too
Nick Coghlan <ncoghlan@gmail.com>
parents:
38775
diff
changeset
|
16 lib/python<version>/site-packages as well as lib/site-python. |
|
8fe373fb21d4
Fix site module docstring to match the code for Mac OSX, too
Nick Coghlan <ncoghlan@gmail.com>
parents:
38775
diff
changeset
|
17 On other platforms (such as Windows), it tries each of the |
|
38775
51732e81c796
Fix site module docstring to match the code
Nick Coghlan <ncoghlan@gmail.com>
parents:
37594
diff
changeset
|
18 prefixes directly, as well as with lib/site-packages appended. The |
|
6267
eb8d1a098b9d
Change packages -> site-packages
Guido van Rossum <guido@python.org>
parents:
6231
diff
changeset
|
19 resulting directories, if they exist, are appended to sys.path, and |
|
eb8d1a098b9d
Change packages -> site-packages
Guido van Rossum <guido@python.org>
parents:
6231
diff
changeset
|
20 also inspected for path configuration files. |
| 4010 | 21 |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
22 A path configuration file is a file whose name has the form |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
23 <package>.pth; its contents are additional directories (one per line) |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
24 to be added to sys.path. Non-existing directories (or |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
25 non-directories) are never added to sys.path; no directory is added to |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
26 sys.path more than once. Blank lines and lines beginning with |
|
21450
fa0bde3c86b2
Don't use Latex \code{...} in docstrings.
Guido van Rossum <guido@python.org>
parents:
21449
diff
changeset
|
27 '#' are skipped. Lines starting with 'import' are executed. |
| 4010 | 28 |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
29 For example, suppose sys.prefix and sys.exec_prefix are set to |
|
39506
e6fc4f99a61b
Update the docstring to use a version a little newer than 1999. This was
Neal Norwitz <nnorwitz@gmail.com>
parents:
38776
diff
changeset
|
30 /usr/local and there is a directory /usr/local/lib/python2.5/site-packages |
|
6267
eb8d1a098b9d
Change packages -> site-packages
Guido van Rossum <guido@python.org>
parents:
6231
diff
changeset
|
31 with three subdirectories, foo, bar and spam, and two path |
|
eb8d1a098b9d
Change packages -> site-packages
Guido van Rossum <guido@python.org>
parents:
6231
diff
changeset
|
32 configuration files, foo.pth and bar.pth. Assume foo.pth contains the |
|
eb8d1a098b9d
Change packages -> site-packages
Guido van Rossum <guido@python.org>
parents:
6231
diff
changeset
|
33 following: |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
34 |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
35 # foo package configuration |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
36 foo |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
37 bar |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
38 bletch |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
39 |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
40 and bar.pth contains: |
| 4010 | 41 |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
42 # bar package configuration |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
43 bar |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
44 |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
45 Then the following directories are added to sys.path, in this order: |
| 4010 | 46 |
|
39506
e6fc4f99a61b
Update the docstring to use a version a little newer than 1999. This was
Neal Norwitz <nnorwitz@gmail.com>
parents:
38776
diff
changeset
|
47 /usr/local/lib/python2.5/site-packages/bar |
|
e6fc4f99a61b
Update the docstring to use a version a little newer than 1999. This was
Neal Norwitz <nnorwitz@gmail.com>
parents:
38776
diff
changeset
|
48 /usr/local/lib/python2.5/site-packages/foo |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
49 |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
50 Note that bletch is omitted because it doesn't exist; bar precedes foo |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
51 because bar.pth comes alphabetically before foo.pth; and spam is |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
52 omitted because it is not mentioned in either path configuration file. |
| 4010 | 53 |
| 54 After these path manipulations, an attempt is made to import a module | |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
55 named sitecustomize, which can perform arbitrary additional |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
56 site-specific customizations. If this import fails with an |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
57 ImportError exception, it is silently ignored. |
| 4010 | 58 |
| 59 """ | |
| 60 | |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
61 import sys |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
62 import os |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
63 import __builtin__ |
|
67145
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
64 import traceback |
| 4010 | 65 |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
66 # Prefixes for site-packages; add additional prefixes like /usr/local here |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
67 PREFIXES = [sys.prefix, sys.exec_prefix] |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
68 # Enable per user site-packages directory |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
69 # set it to False to disable the feature or True to force the feature |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
70 ENABLE_USER_SITE = None |
|
55705
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
71 |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
72 # for distutils.commands.install |
|
55705
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
73 # These values are initialized by the getuserbase() and getusersitepackages() |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
74 # functions, through the main() function when Python starts. |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
75 USER_SITE = None |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
76 USER_BASE = None |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
77 |
|
16949
09ba15ee1961
RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents:
16050
diff
changeset
|
78 |
|
14911
af8d77aa90f8
Convert all entries on sys.path to absolute paths, and also update the
Fred Drake <fdrake@acm.org>
parents:
14658
diff
changeset
|
79 def makepath(*paths): |
|
65229
2ffbfbc0d3cb
Merged revisions 85386-85387,85389 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62056
diff
changeset
|
80 dir = os.path.join(*paths) |
|
2ffbfbc0d3cb
Merged revisions 85386-85387,85389 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62056
diff
changeset
|
81 try: |
|
2ffbfbc0d3cb
Merged revisions 85386-85387,85389 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62056
diff
changeset
|
82 dir = os.path.abspath(dir) |
|
2ffbfbc0d3cb
Merged revisions 85386-85387,85389 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62056
diff
changeset
|
83 except OSError: |
|
2ffbfbc0d3cb
Merged revisions 85386-85387,85389 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62056
diff
changeset
|
84 pass |
|
18364
2e1adbe89e4a
Avoid using os.path.normcase() on sys.path elements; doing so causes paths
Fred Drake <fdrake@acm.org>
parents:
18090
diff
changeset
|
85 return dir, os.path.normcase(dir) |
|
14911
af8d77aa90f8
Convert all entries on sys.path to absolute paths, and also update the
Fred Drake <fdrake@acm.org>
parents:
14658
diff
changeset
|
86 |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
87 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
88 def abs__file__(): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
89 """Set all module' __file__ attribute to an absolute path""" |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
90 for m in sys.modules.values(): |
| 37594 | 91 if hasattr(m, '__loader__'): |
|
37576
d11366f7e1e7
Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents:
36937
diff
changeset
|
92 continue # don't mess with a PEP 302-supplied __file__ |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
93 try: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
94 m.__file__ = os.path.abspath(m.__file__) |
|
65229
2ffbfbc0d3cb
Merged revisions 85386-85387,85389 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62056
diff
changeset
|
95 except (AttributeError, OSError): |
|
2ffbfbc0d3cb
Merged revisions 85386-85387,85389 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62056
diff
changeset
|
96 pass |
|
14911
af8d77aa90f8
Convert all entries on sys.path to absolute paths, and also update the
Fred Drake <fdrake@acm.org>
parents:
14658
diff
changeset
|
97 |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
98 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
99 def removeduppaths(): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
100 """ Remove duplicate entries from sys.path along with making them |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
101 absolute""" |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
102 # This ensures that the initial path provided by the interpreter contains |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
103 # only absolute pathnames, even if we're running from the build directory. |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
104 L = [] |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
105 known_paths = set() |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
106 for dir in sys.path: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
107 # Filter out duplicate paths (on case-insensitive file systems also |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
108 # if they only differ in case); turn relative paths into absolute |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
109 # paths. |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
110 dir, dircase = makepath(dir) |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
111 if not dircase in known_paths: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
112 L.append(dir) |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
113 known_paths.add(dircase) |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
114 sys.path[:] = L |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
115 return known_paths |
|
14911
af8d77aa90f8
Convert all entries on sys.path to absolute paths, and also update the
Fred Drake <fdrake@acm.org>
parents:
14658
diff
changeset
|
116 |
|
24382
4156bf9b0043
Add an XXX comment and a pointer to a full bug report.
Fred Drake <fdrake@acm.org>
parents:
23620
diff
changeset
|
117 # XXX This should not be part of site.py, since it is needed even when |
|
4156bf9b0043
Add an XXX comment and a pointer to a full bug report.
Fred Drake <fdrake@acm.org>
parents:
23620
diff
changeset
|
118 # using the -S option for Python. See http://www.python.org/sf/586680 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
119 def addbuilddir(): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
120 """Append ./build/lib.<platform> in case we're running in the build dir |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
121 (especially for Guido :-)""" |
|
58450
fa69e891edf4
taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
55705
diff
changeset
|
122 from sysconfig import get_platform |
|
29943
9013d3c1d655
Repair operator error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29942
diff
changeset
|
123 s = "build/lib.%s-%.3s" % (get_platform(), sys.version) |
|
44862
101d86a1af85
Add the correct build dir when building with pydebug.
Georg Brandl <georg@python.org>
parents:
41781
diff
changeset
|
124 if hasattr(sys, 'gettotalrefcount'): |
|
101d86a1af85
Add the correct build dir when building with pydebug.
Georg Brandl <georg@python.org>
parents:
41781
diff
changeset
|
125 s += '-pydebug' |
|
59923
477d36e5d913
Issue #8205: Remove the "Modules" directory from sys.path when Python is running from the build directory (POSIX only).
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59500
diff
changeset
|
126 s = os.path.join(os.path.dirname(sys.path.pop()), s) |
|
16050
e62d5aff0c3a
A hack to augment sys.path with the build/lib.<platform> directory
Guido van Rossum <guido@python.org>
parents:
15815
diff
changeset
|
127 sys.path.append(s) |
|
e62d5aff0c3a
A hack to augment sys.path with the build/lib.<platform> directory
Guido van Rossum <guido@python.org>
parents:
15815
diff
changeset
|
128 |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
129 |
|
18783
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
130 def _init_pathinfo(): |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
131 """Return a set containing all existing directory entries from sys.path""" |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
132 d = set() |
|
18783
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
133 for dir in sys.path: |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
134 try: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
135 if os.path.isdir(dir): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
136 dir, dircase = makepath(dir) |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
137 d.add(dircase) |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
138 except TypeError: |
|
18783
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
139 continue |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
140 return d |
|
18783
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
141 |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
142 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
143 def addpackage(sitedir, name, known_paths): |
|
41781
f14a5b005a1e
Fix docstring for add_package in site.py.
Georg Brandl <georg@python.org>
parents:
41123
diff
changeset
|
144 """Process a .pth file within the site-packages directory: |
|
f14a5b005a1e
Fix docstring for add_package in site.py.
Georg Brandl <georg@python.org>
parents:
41123
diff
changeset
|
145 For each line in the file, either combine it with sitedir to a path |
|
f14a5b005a1e
Fix docstring for add_package in site.py.
Georg Brandl <georg@python.org>
parents:
41123
diff
changeset
|
146 and add that to known_paths, or execute it if it starts with 'import '. |
|
f14a5b005a1e
Fix docstring for add_package in site.py.
Georg Brandl <georg@python.org>
parents:
41123
diff
changeset
|
147 """ |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
148 if known_paths is None: |
|
18783
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
149 _init_pathinfo() |
|
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
150 reset = 1 |
|
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
151 else: |
|
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
152 reset = 0 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
153 fullname = os.path.join(sitedir, name) |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
154 try: |
|
32766
d15517f2abe8
Fix bug in addsitedir() to properly handle the lack of a second argument.
Brett Cannon <bcannon@gmail.com>
parents:
32566
diff
changeset
|
155 f = open(fullname, "rU") |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
156 except IOError: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
157 return |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
158 with f: |
|
67145
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
159 for n, line in enumerate(f): |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
160 if line.startswith("#"): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
161 continue |
|
67145
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
162 try: |
|
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
163 if line.startswith(("import ", "import\t")): |
|
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
164 exec line |
|
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
165 continue |
|
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
166 line = line.rstrip() |
|
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
167 dir, dircase = makepath(sitedir, line) |
|
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
168 if not dircase in known_paths and os.path.exists(dir): |
|
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
169 sys.path.append(dir) |
|
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
170 known_paths.add(dircase) |
|
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
171 except Exception as err: |
|
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
172 print >>sys.stderr, "Error processing line {:d} of {}:\n".format( |
|
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
173 n+1, fullname) |
|
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
174 for record in traceback.format_exception(*sys.exc_info()): |
|
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
175 for line in record.splitlines(): |
|
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
176 print >>sys.stderr, ' '+line |
|
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
177 print >>sys.stderr, "\nRemainder of file ignored" |
|
dd6d1ecbeb56
Merged revisions 87497 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
65229
diff
changeset
|
178 break |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
179 if reset: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
180 known_paths = None |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
181 return known_paths |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
182 |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
183 |
|
32566
3aadfc3fc4e6
Change argument list for addsitedir() to not require a second argument and thus
Brett Cannon <bcannon@gmail.com>
parents:
32528
diff
changeset
|
184 def addsitedir(sitedir, known_paths=None): |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
185 """Add 'sitedir' argument to sys.path if missing and handle .pth files in |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
186 'sitedir'""" |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
187 if known_paths is None: |
|
32766
d15517f2abe8
Fix bug in addsitedir() to properly handle the lack of a second argument.
Brett Cannon <bcannon@gmail.com>
parents:
32566
diff
changeset
|
188 known_paths = _init_pathinfo() |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
189 reset = 1 |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
190 else: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
191 reset = 0 |
|
18364
2e1adbe89e4a
Avoid using os.path.normcase() on sys.path elements; doing so causes paths
Fred Drake <fdrake@acm.org>
parents:
18090
diff
changeset
|
192 sitedir, sitedircase = makepath(sitedir) |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
193 if not sitedircase in known_paths: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7077
diff
changeset
|
194 sys.path.append(sitedir) # Add path component |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
195 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7077
diff
changeset
|
196 names = os.listdir(sitedir) |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
197 except os.error: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
7077
diff
changeset
|
198 return |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
199 dotpth = os.extsep + "pth" |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
200 names = [name for name in names if name.endswith(dotpth)] |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
201 for name in sorted(names): |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
202 addpackage(sitedir, name, known_paths) |
|
18783
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
203 if reset: |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
204 known_paths = None |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
205 return known_paths |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
206 |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
207 |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
208 def check_enableusersite(): |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
209 """Check if user site directory is safe for inclusion |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
210 |
| 46830 | 211 The function tests for the command line flag (including environment var), |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
212 process uid/gid equal to effective uid/gid. |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
213 |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
214 None: Disabled for security reasons |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
215 False: Disabled by user (command line option) |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
216 True: Safe and enabled |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
217 """ |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
218 if sys.flags.no_user_site: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
219 return False |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
220 |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
221 if hasattr(os, "getuid") and hasattr(os, "geteuid"): |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
222 # check process uid == effective uid |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
223 if os.geteuid() != os.getuid(): |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
224 return None |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
225 if hasattr(os, "getgid") and hasattr(os, "getegid"): |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
226 # check process gid == effective gid |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
227 if os.getegid() != os.getgid(): |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
228 return None |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
229 |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
230 return True |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
231 |
|
55705
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
232 def getuserbase(): |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
233 """Returns the `user base` directory path. |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
234 |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
235 The `user base` directory can be used to store data. If the global |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
236 variable ``USER_BASE`` is not initialized yet, this function will also set |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
237 it. |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
238 """ |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
239 global USER_BASE |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
240 if USER_BASE is not None: |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
241 return USER_BASE |
|
58450
fa69e891edf4
taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
55705
diff
changeset
|
242 from sysconfig import get_config_var |
|
fa69e891edf4
taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
55705
diff
changeset
|
243 USER_BASE = get_config_var('userbase') |
|
55705
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
244 return USER_BASE |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
245 |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
246 def getusersitepackages(): |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
247 """Returns the user-specific site-packages directory path. |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
248 |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
249 If the global variable ``USER_SITE`` is not initialized yet, this |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
250 function will also set it. |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
251 """ |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
252 global USER_SITE |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
253 user_base = getuserbase() # this will also set USER_BASE |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
254 |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
255 if USER_SITE is not None: |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
256 return USER_SITE |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
257 |
|
58450
fa69e891edf4
taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
55705
diff
changeset
|
258 from sysconfig import get_path |
|
fa69e891edf4
taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
55705
diff
changeset
|
259 import os |
|
61402
930395b2de90
Issue #8084: ensure that the --user directory
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60569
diff
changeset
|
260 |
|
930395b2de90
Issue #8084: ensure that the --user directory
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60569
diff
changeset
|
261 if sys.platform == 'darwin': |
|
930395b2de90
Issue #8084: ensure that the --user directory
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60569
diff
changeset
|
262 from sysconfig import get_config_var |
|
930395b2de90
Issue #8084: ensure that the --user directory
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60569
diff
changeset
|
263 if get_config_var('PYTHONFRAMEWORK'): |
|
930395b2de90
Issue #8084: ensure that the --user directory
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60569
diff
changeset
|
264 USER_SITE = get_path('purelib', 'osx_framework_user') |
|
930395b2de90
Issue #8084: ensure that the --user directory
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60569
diff
changeset
|
265 return USER_SITE |
|
930395b2de90
Issue #8084: ensure that the --user directory
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60569
diff
changeset
|
266 |
|
58450
fa69e891edf4
taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
55705
diff
changeset
|
267 USER_SITE = get_path('purelib', '%s_user' % os.name) |
|
55705
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
268 return USER_SITE |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
269 |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
270 def addusersitepackages(known_paths): |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
271 """Add a per user site-package to sys.path |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
272 |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
273 Each user has its own python directory with site-packages in the |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
274 home directory. |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
275 """ |
|
55705
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
276 # get the per user site-package path |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
277 # this call will also make sure USER_BASE and USER_SITE are set |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
278 user_site = getusersitepackages() |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
279 |
|
55705
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
280 if ENABLE_USER_SITE and os.path.isdir(user_site): |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
281 addsitedir(user_site, known_paths) |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
282 return known_paths |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
283 |
|
55705
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
284 def getsitepackages(): |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
285 """Returns a list containing all global site-packages directories |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
286 (and possibly site-python). |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
287 |
|
55705
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
288 For each directory present in the global ``PREFIXES``, this function |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
289 will find its `site-packages` subdirectory depending on the system |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
290 environment, and will return a list of full paths. |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
291 """ |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
292 sitepackages = [] |
|
62056
11acc57f5804
use a set for membership testing
Benjamin Peterson <benjamin@python.org>
parents:
61402
diff
changeset
|
293 seen = set() |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
294 |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
295 for prefix in PREFIXES: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
296 if not prefix or prefix in seen: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
297 continue |
|
62056
11acc57f5804
use a set for membership testing
Benjamin Peterson <benjamin@python.org>
parents:
61402
diff
changeset
|
298 seen.add(prefix) |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
299 |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
300 if sys.platform in ('os2emx', 'riscos'): |
|
55705
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
301 sitepackages.append(os.path.join(prefix, "Lib", "site-packages")) |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
302 elif os.sep == '/': |
|
55705
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
303 sitepackages.append(os.path.join(prefix, "lib", |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
304 "python" + sys.version[:3], |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
305 "site-packages")) |
|
55705
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
306 sitepackages.append(os.path.join(prefix, "lib", "site-python")) |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
307 else: |
|
55705
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
308 sitepackages.append(prefix) |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
309 sitepackages.append(os.path.join(prefix, "lib", "site-packages")) |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
310 if sys.platform == "darwin": |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
311 # for framework builds *only* we add the standard Apple |
|
52494
8976ec2cdfe5
Fix issue #4865: add /Library/Python/2.7/site-packages to
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
46830
diff
changeset
|
312 # locations. |
|
61402
930395b2de90
Issue #8084: ensure that the --user directory
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60569
diff
changeset
|
313 from sysconfig import get_config_var |
|
930395b2de90
Issue #8084: ensure that the --user directory
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60569
diff
changeset
|
314 framework = get_config_var("PYTHONFRAMEWORK") |
|
74802
82c4f094f811
Issue #10881: Fix test_site failures with OS X framework builds.
Ned Deily <nad@acm.org>
parents:
68256
diff
changeset
|
315 if framework: |
|
55705
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
316 sitepackages.append( |
|
61402
930395b2de90
Issue #8084: ensure that the --user directory
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
60569
diff
changeset
|
317 os.path.join("/Library", framework, |
|
52494
8976ec2cdfe5
Fix issue #4865: add /Library/Python/2.7/site-packages to
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
46830
diff
changeset
|
318 sys.version[:3], "site-packages")) |
|
55705
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
319 return sitepackages |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
320 |
|
55705
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
321 def addsitepackages(known_paths): |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
322 """Add site-packages (and possibly site-python) to sys.path""" |
|
974553fb59dc
#6693: New functions in site.py to get user/global site packages paths.
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
52494
diff
changeset
|
323 for sitedir in getsitepackages(): |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
324 if os.path.isdir(sitedir): |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
325 addsitedir(sitedir, known_paths) |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
326 |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
327 return known_paths |
|
18783
ac13a6ce13e2
Make the add*() helper functions more robust for use after intialization
Fred Drake <fdrake@acm.org>
parents:
18771
diff
changeset
|
328 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
329 def setBEGINLIBPATH(): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
330 """The OS/2 EMX port has optional extension modules that do double duty |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
331 as DLLs (and must use the .DLL file extension) for other extensions. |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
332 The library search path needs to be amended so these will be found |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
333 during module import. Use BEGINLIBPATH so that these are at the start |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
334 of the library search path. |
|
32528
67539cfcf09f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
32246
diff
changeset
|
335 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
336 """ |
|
31065
41f4ce6c4e50
To find the curses extension as a DLL (on OS/2), we need to adjust the
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
30723
diff
changeset
|
337 dllpath = os.path.join(sys.prefix, "Lib", "lib-dynload") |
|
41f4ce6c4e50
To find the curses extension as a DLL (on OS/2), we need to adjust the
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
30723
diff
changeset
|
338 libpath = os.environ['BEGINLIBPATH'].split(';') |
|
41f4ce6c4e50
To find the curses extension as a DLL (on OS/2), we need to adjust the
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
30723
diff
changeset
|
339 if libpath[-1]: |
|
41f4ce6c4e50
To find the curses extension as a DLL (on OS/2), we need to adjust the
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
30723
diff
changeset
|
340 libpath.append(dllpath) |
|
41f4ce6c4e50
To find the curses extension as a DLL (on OS/2), we need to adjust the
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
30723
diff
changeset
|
341 else: |
|
41f4ce6c4e50
To find the curses extension as a DLL (on OS/2), we need to adjust the
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
30723
diff
changeset
|
342 libpath[-1] = dllpath |
|
41f4ce6c4e50
To find the curses extension as a DLL (on OS/2), we need to adjust the
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
30723
diff
changeset
|
343 os.environ['BEGINLIBPATH'] = ';'.join(libpath) |
|
41f4ce6c4e50
To find the curses extension as a DLL (on OS/2), we need to adjust the
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
30723
diff
changeset
|
344 |
|
41f4ce6c4e50
To find the curses extension as a DLL (on OS/2), we need to adjust the
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
30723
diff
changeset
|
345 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
346 def setquit(): |
|
60569
63b58124a554
Fix #8364. Update the setquit docstring and change a built-in to builtin.
Brian Curtin <brian.curtin@gmail.com>
parents:
59923
diff
changeset
|
347 """Define new builtins 'quit' and 'exit'. |
|
63b58124a554
Fix #8364. Update the setquit docstring and change a built-in to builtin.
Brian Curtin <brian.curtin@gmail.com>
parents:
59923
diff
changeset
|
348 |
|
63b58124a554
Fix #8364. Update the setquit docstring and change a built-in to builtin.
Brian Curtin <brian.curtin@gmail.com>
parents:
59923
diff
changeset
|
349 These are objects which make the interpreter exit when called. |
|
63b58124a554
Fix #8364. Update the setquit docstring and change a built-in to builtin.
Brian Curtin <brian.curtin@gmail.com>
parents:
59923
diff
changeset
|
350 The repr of each object contains a hint at how it works. |
|
8878
813f527f3bec
Add built-in string variables 'quit' and 'exit' that display a hint on
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
351 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
352 """ |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
353 if os.sep == ':': |
|
36922
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
354 eof = 'Cmd-Q' |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
355 elif os.sep == '\\': |
|
36922
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
356 eof = 'Ctrl-Z plus Return' |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
357 else: |
|
36922
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
358 eof = 'Ctrl-D (i.e. EOF)' |
|
36937
df43c04d2554
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36922
diff
changeset
|
359 |
|
36922
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
360 class Quitter(object): |
|
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
361 def __init__(self, name): |
|
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
362 self.name = name |
|
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
363 def __repr__(self): |
|
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
364 return 'Use %s() or %s to exit' % (self.name, eof) |
|
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
365 def __call__(self, code=None): |
|
39512
6e7b3dd58f98
Patch #1540892: site.py Quitter() class attempts to close sys.stdin
Kurt B. Kaiser <kbk@shore.net>
parents:
39506
diff
changeset
|
366 # Shells like IDLE catch the SystemExit, but listen when their |
|
6e7b3dd58f98
Patch #1540892: site.py Quitter() class attempts to close sys.stdin
Kurt B. Kaiser <kbk@shore.net>
parents:
39506
diff
changeset
|
367 # stdin wrapper is closed. |
|
6e7b3dd58f98
Patch #1540892: site.py Quitter() class attempts to close sys.stdin
Kurt B. Kaiser <kbk@shore.net>
parents:
39506
diff
changeset
|
368 try: |
|
6e7b3dd58f98
Patch #1540892: site.py Quitter() class attempts to close sys.stdin
Kurt B. Kaiser <kbk@shore.net>
parents:
39506
diff
changeset
|
369 sys.stdin.close() |
|
6e7b3dd58f98
Patch #1540892: site.py Quitter() class attempts to close sys.stdin
Kurt B. Kaiser <kbk@shore.net>
parents:
39506
diff
changeset
|
370 except: |
|
6e7b3dd58f98
Patch #1540892: site.py Quitter() class attempts to close sys.stdin
Kurt B. Kaiser <kbk@shore.net>
parents:
39506
diff
changeset
|
371 pass |
|
36922
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
372 raise SystemExit(code) |
|
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
373 __builtin__.quit = Quitter('quit') |
|
d3d2f9ffdc06
Patch #1446372: quit and exit can now be called from the interactive
Georg Brandl <georg@python.org>
parents:
32766
diff
changeset
|
374 __builtin__.exit = Quitter('exit') |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
375 |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
376 |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
377 class _Printer(object): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
378 """interactive prompt objects for printing the license text, a list of |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
379 contributors and the copyright notice.""" |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
380 |
|
14409
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
381 MAXLINES = 23 |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
382 |
|
14996
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
383 def __init__(self, name, data, files=(), dirs=()): |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
384 self.__name = name |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
385 self.__data = data |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
386 self.__files = files |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
387 self.__dirs = dirs |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
388 self.__lines = None |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
389 |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
390 def __setup(self): |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
391 if self.__lines: |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
392 return |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
393 data = None |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
394 for dir in self.__dirs: |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
395 for filename in self.__files: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
396 filename = os.path.join(dir, filename) |
|
14996
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
397 try: |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
398 fp = file(filename, "rU") |
|
14996
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
399 data = fp.read() |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
400 fp.close() |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
401 break |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
402 except IOError: |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
403 pass |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
404 if data: |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
405 break |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
406 if not data: |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
407 data = self.__data |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
408 self.__lines = data.split('\n') |
|
14409
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
409 self.__linecnt = len(self.__lines) |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
410 |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
411 def __repr__(self): |
|
14996
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
412 self.__setup() |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
413 if len(self.__lines) <= self.MAXLINES: |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
414 return "\n".join(self.__lines) |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
415 else: |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
416 return "Type %s() to see the full %s text" % ((self.__name,)*2) |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
417 |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
418 def __call__(self): |
|
5e4eed062b54
Fix a few problems with the _Printer class and the license variable.
Guido van Rossum <guido@python.org>
parents:
14911
diff
changeset
|
419 self.__setup() |
|
14409
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
420 prompt = 'Hit Return for more, or q (and Return) to quit: ' |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
421 lineno = 0 |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
422 while 1: |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
423 try: |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
424 for i in range(lineno, lineno + self.MAXLINES): |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
425 print self.__lines[i] |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
426 except IndexError: |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
427 break |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
428 else: |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
429 lineno += self.MAXLINES |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
430 key = None |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
431 while key is None: |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
432 key = raw_input(prompt) |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
433 if key not in ('', 'q'): |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
434 key = None |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
435 if key == 'q': |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
436 break |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
437 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
438 def setcopyright(): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
439 """Set 'copyright' and 'credits' in __builtin__""" |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
440 __builtin__.copyright = _Printer("copyright", sys.copyright) |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
441 if sys.platform[:4] == 'java': |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
442 __builtin__.credits = _Printer( |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
443 "credits", |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
444 "Jython is maintained by the Jython developers (www.jython.org).") |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
445 else: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
446 __builtin__.credits = _Printer("credits", """\ |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
447 Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
448 for supporting Python development. See www.python.org for more information.""") |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
449 here = os.path.dirname(os.__file__) |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
450 __builtin__.license = _Printer( |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
451 "license", "See http://www.python.org/%.3s/license.html" % sys.version, |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
452 ["LICENSE.txt", "LICENSE"], |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
453 [os.path.join(here, os.pardir), here, os.curdir]) |
|
14409
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
454 |
|
a76ab7c8fc29
Add new builtin commands "copyright", "license", "credits" which
Guido van Rossum <guido@python.org>
parents:
13614
diff
changeset
|
455 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
456 class _Helper(object): |
|
60569
63b58124a554
Fix #8364. Update the setquit docstring and change a built-in to builtin.
Brian Curtin <brian.curtin@gmail.com>
parents:
59923
diff
changeset
|
457 """Define the builtin 'help'. |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
458 This is a wrapper around pydoc.help (with a twist). |
|
18090
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
459 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
460 """ |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
461 |
|
18090
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
462 def __repr__(self): |
|
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
463 return "Type help() for interactive help, " \ |
|
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
464 "or help(object) for help about object." |
|
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
465 def __call__(self, *args, **kwds): |
|
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
466 import pydoc |
|
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
467 return pydoc.help(*args, **kwds) |
|
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
468 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
469 def sethelper(): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
470 __builtin__.help = _Helper() |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
471 |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
472 def aliasmbcs(): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
473 """On Windows, some default encodings are not provided by Python, |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
474 while they are always available as "mbcs" in each locale. Make |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
475 them usable by aliasing to "mbcs" in such a case.""" |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
476 if sys.platform == 'win32': |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
477 import locale, codecs |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
478 enc = locale.getdefaultlocale()[1] |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
479 if enc.startswith('cp'): # "cp***" ? |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
480 try: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
481 codecs.lookup(enc) |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
482 except LookupError: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
483 import encodings |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
484 encodings._cache[enc] = encodings._unknown |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
485 encodings.aliases.aliases[enc] = 'mbcs' |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
486 |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
487 def setencoding(): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
488 """Set the string encoding used by the Unicode implementation. The |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
489 default is 'ascii', but if you're willing to experiment, you can |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
490 change this.""" |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
491 encoding = "ascii" # Default value set by _PyUnicode_Init() |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
492 if 0: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
493 # Enable to support locale aware default string encodings. |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
494 import locale |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
495 loc = locale.getdefaultlocale() |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
496 if loc[1]: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
497 encoding = loc[1] |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
498 if 0: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
499 # Enable to switch off string to Unicode coercion and implicit |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
500 # Unicode to string conversion. |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
501 encoding = "undefined" |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
502 if encoding != "ascii": |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
503 # On Non-Unicode builds this will raise an AttributeError... |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
504 sys.setdefaultencoding(encoding) # Needs Python Unicode build ! |
|
18090
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
505 |
|
41016675002f
Add new built-in 'help' which invokes pydoc.help (with a twist).
Guido van Rossum <guido@python.org>
parents:
17211
diff
changeset
|
506 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
507 def execsitecustomize(): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
508 """Run custom site specific code, if available.""" |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
509 try: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
510 import sitecustomize |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
511 except ImportError: |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
512 pass |
|
59822
8d8c9bf2a04e
Partial backport of r78826: leave import site error handler unchanged (print
Victor Stinner <victor.stinner@haypocalc.com>
parents:
46830
diff
changeset
|
513 except Exception: |
|
8d8c9bf2a04e
Partial backport of r78826: leave import site error handler unchanged (print
Victor Stinner <victor.stinner@haypocalc.com>
parents:
46830
diff
changeset
|
514 if sys.flags.verbose: |
|
8d8c9bf2a04e
Partial backport of r78826: leave import site error handler unchanged (print
Victor Stinner <victor.stinner@haypocalc.com>
parents:
46830
diff
changeset
|
515 sys.excepthook(*sys.exc_info()) |
|
8d8c9bf2a04e
Partial backport of r78826: leave import site error handler unchanged (print
Victor Stinner <victor.stinner@haypocalc.com>
parents:
46830
diff
changeset
|
516 else: |
|
8d8c9bf2a04e
Partial backport of r78826: leave import site error handler unchanged (print
Victor Stinner <victor.stinner@haypocalc.com>
parents:
46830
diff
changeset
|
517 print >>sys.stderr, \ |
|
8d8c9bf2a04e
Partial backport of r78826: leave import site error handler unchanged (print
Victor Stinner <victor.stinner@haypocalc.com>
parents:
46830
diff
changeset
|
518 "'import sitecustomize' failed; use -v for traceback" |
|
28141
a3cf07bac373
Patch #671666: Alias ANSI code page to "mbcs".
Martin v. Löwis <martin@v.loewis.de>
parents:
26885
diff
changeset
|
519 |
|
13612
f3401ed132a1
-- changed default encoding to "ascii". you can still change
Fredrik Lundh <fredrik@pythonware.com>
parents:
13523
diff
changeset
|
520 |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
521 def execusercustomize(): |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
522 """Run custom user specific code, if available.""" |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
523 try: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
524 import usercustomize |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
525 except ImportError: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
526 pass |
|
59822
8d8c9bf2a04e
Partial backport of r78826: leave import site error handler unchanged (print
Victor Stinner <victor.stinner@haypocalc.com>
parents:
46830
diff
changeset
|
527 except Exception: |
|
8d8c9bf2a04e
Partial backport of r78826: leave import site error handler unchanged (print
Victor Stinner <victor.stinner@haypocalc.com>
parents:
46830
diff
changeset
|
528 if sys.flags.verbose: |
|
8d8c9bf2a04e
Partial backport of r78826: leave import site error handler unchanged (print
Victor Stinner <victor.stinner@haypocalc.com>
parents:
46830
diff
changeset
|
529 sys.excepthook(*sys.exc_info()) |
|
8d8c9bf2a04e
Partial backport of r78826: leave import site error handler unchanged (print
Victor Stinner <victor.stinner@haypocalc.com>
parents:
46830
diff
changeset
|
530 else: |
|
8d8c9bf2a04e
Partial backport of r78826: leave import site error handler unchanged (print
Victor Stinner <victor.stinner@haypocalc.com>
parents:
46830
diff
changeset
|
531 print>>sys.stderr, \ |
|
8d8c9bf2a04e
Partial backport of r78826: leave import site error handler unchanged (print
Victor Stinner <victor.stinner@haypocalc.com>
parents:
46830
diff
changeset
|
532 "'import usercustomize' failed; use -v for traceback" |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
533 |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
534 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
535 def main(): |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
536 global ENABLE_USER_SITE |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
537 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
538 abs__file__() |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
539 known_paths = removeduppaths() |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
540 if (os.name == "posix" and sys.path and |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
541 os.path.basename(sys.path[-1]) == "Modules"): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
542 addbuilddir() |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
543 if ENABLE_USER_SITE is None: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
544 ENABLE_USER_SITE = check_enableusersite() |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
545 known_paths = addusersitepackages(known_paths) |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
546 known_paths = addsitepackages(known_paths) |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
547 if sys.platform == 'os2emx': |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
548 setBEGINLIBPATH() |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
549 setquit() |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
550 setcopyright() |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
551 sethelper() |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
552 aliasmbcs() |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
553 setencoding() |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
554 execsitecustomize() |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
555 if ENABLE_USER_SITE: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
556 execusercustomize() |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
557 # Remove sys.setdefaultencoding() so that users cannot change the |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
558 # encoding after initialization. The test for presence is needed when |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
559 # this module is run as a script, because this code is executed twice. |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
560 if hasattr(sys, "setdefaultencoding"): |
|
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
561 del sys.setdefaultencoding |
|
12873
efc6d7789d9e
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
9520
diff
changeset
|
562 |
|
32246
70cfe065eac5
Refactored site.py into functions. Also moved over to using sets.
Brett Cannon <bcannon@gmail.com>
parents:
31726
diff
changeset
|
563 main() |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
564 |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
565 def _script(): |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
566 help = """\ |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
567 %s [--user-base] [--user-site] |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
568 |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
569 Without arguments print some useful information |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
570 With arguments print the value of USER_BASE and/or USER_SITE separated |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
571 by '%s'. |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
572 |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
573 Exit codes with --user-base or --user-site: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
574 0 - user site directory is enabled |
|
46788
f3f6a895a555
Add --user option to build_ext
Christian Heimes <christian@cheimes.de>
parents:
46698
diff
changeset
|
575 1 - user site directory is disabled by user |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
576 2 - uses site directory is disabled by super user |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
577 or for security reasons |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
578 >2 - unknown error |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
579 """ |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
580 args = sys.argv[1:] |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
581 if not args: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
582 print "sys.path = [" |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
583 for dir in sys.path: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
584 print " %r," % (dir,) |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
585 print "]" |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
586 print "USER_BASE: %r (%s)" % (USER_BASE, |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
587 "exists" if os.path.isdir(USER_BASE) else "doesn't exist") |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
588 print "USER_SITE: %r (%s)" % (USER_SITE, |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
589 "exists" if os.path.isdir(USER_SITE) else "doesn't exist") |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
590 print "ENABLE_USER_SITE: %r" % ENABLE_USER_SITE |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
591 sys.exit(0) |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
592 |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
593 buffer = [] |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
594 if '--user-base' in args: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
595 buffer.append(USER_BASE) |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
596 if '--user-site' in args: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
597 buffer.append(USER_SITE) |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
598 |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
599 if buffer: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
600 print os.pathsep.join(buffer) |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
601 if ENABLE_USER_SITE: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
602 sys.exit(0) |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
603 elif ENABLE_USER_SITE is False: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
604 sys.exit(1) |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
605 elif ENABLE_USER_SITE is None: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
606 sys.exit(2) |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
607 else: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
608 sys.exit(3) |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
609 else: |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
610 import textwrap |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
611 print textwrap.dedent(help % (sys.argv[0], os.pathsep)) |
|
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
612 sys.exit(10) |
|
6200
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
613 |
|
c57cede3c7c0
New site.py semantics. Searches in
Guido van Rossum <guido@python.org>
parents:
4010
diff
changeset
|
614 if __name__ == '__main__': |
|
46698
1a2dbc84bdfc
Implemented PEP 370
Christian Heimes <christian@cheimes.de>
parents:
44862
diff
changeset
|
615 _script() |
