Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f19a7ac

Browse files
committed
Fix a few problems with the _Printer class and the license variable.
1. repr(license) will no longer print to stdout and read from stdin; you have to use license(). `license` is a short message explaining this. 2. Use lazy initialization so that startup isn't slowed down by the search for the LICENSE file. 3. repr(license) actually returns the desired string, rather than printing to stdout and returning ''. (Why didn't we think of this before?) 4. Use the pythonlabs license URL as the license fallback instead of the CNRI license handle.
1 parent 12e1595 commit f19a7ac

1 file changed

Lines changed: 41 additions & 23 deletions

File tree

Lib/site.py

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,43 @@ def addpackage(sitedir, name):
145145
class _Printer:
146146
MAXLINES = 23
147147

148-
def __init__(self, s):
149-
self.__lines = s.split('\n')
148+
def __init__(self, name, data, files=(), dirs=()):
149+
self.__name = name
150+
self.__data = data
151+
self.__files = files
152+
self.__dirs = dirs
153+
self.__lines = None
154+
155+
def __setup(self):
156+
if self.__lines:
157+
return
158+
data = None
159+
for dir in self.__dirs:
160+
for file in self.__files:
161+
file = os.path.join(dir, file)
162+
try:
163+
fp = open(file)
164+
data = fp.read()
165+
fp.close()
166+
break
167+
except IOError:
168+
pass
169+
if data:
170+
break
171+
if not data:
172+
data = self.__data
173+
self.__lines = data.split('\n')
150174
self.__linecnt = len(self.__lines)
151175

152176
def __repr__(self):
177+
self.__setup()
178+
if len(self.__lines) <= self.MAXLINES:
179+
return "\n".join(self.__lines)
180+
else:
181+
return "Type %s() to see the full %s text" % ((self.__name,)*2)
182+
183+
def __call__(self):
184+
self.__setup()
153185
prompt = 'Hit Return for more, or q (and Return) to quit: '
154186
lineno = 0
155187
while 1:
@@ -167,29 +199,15 @@ def __repr__(self):
167199
key = None
168200
if key == 'q':
169201
break
170-
return ''
171-
172-
__builtin__.copyright = _Printer(sys.copyright)
173-
__builtin__.credits = _Printer(
174-
'''Python development is led by BeOpen PythonLabs (www.pythonlabs.com).''')
175-
176-
def make_license(filename):
177-
try:
178-
return _Printer(open(filename).read())
179-
except IOError:
180-
return None
181202

203+
__builtin__.copyright = _Printer("copyright", sys.copyright)
204+
__builtin__.credits = _Printer("credits",
205+
"Python development is led by BeOpen PythonLabs (www.pythonlabs.com).")
182206
here = os.path.dirname(os.__file__)
183-
for dir in here, os.path.join(here, os.pardir), os.curdir:
184-
for file in "LICENSE.txt", "LICENSE":
185-
lic = make_license(os.path.join(dir, file))
186-
if lic:
187-
break
188-
if lic:
189-
__builtin__.license = lic
190-
break
191-
else:
192-
__builtin__.license = _Printer('See http://hdl.handle.net/1895.22/1012')
207+
__builtin__.license = _Printer(
208+
"license", "See http://www.pythonlabs.com/products/python2.0/license.html",
209+
["LICENSE.txt", "LICENSE"],
210+
[here, os.path.join(here, os.pardir), os.curdir])
193211

194212

195213
# Set the string encoding used by the Unicode implementation. The

0 commit comments

Comments
 (0)