10
10
11
11
# We first record the starting time, in case we're being run as a CGI script.
12
12
from time import time , localtime , asctime
13
- serverStartTime = time ()
13
+ serverStartTime = time ()
14
14
15
15
# Some imports
16
16
import cgi , os , sys , traceback
17
- from types import ClassType , DictType , FloatType
18
17
from random import randint
19
18
try :
20
19
from cStringIO import StringIO
@@ -118,7 +117,7 @@ def userConfig(self):
118
117
config = f .read ()
119
118
config = eval ('dict(%s)' % config )
120
119
f .close ()
121
- assert isinstance (config , DictType )
120
+ assert isinstance (config , dict )
122
121
return config
123
122
124
123
def config (self ):
@@ -231,8 +230,9 @@ def writeScriptLog(self):
231
230
values = []
232
231
for column in self .setting ('ScriptLogColumns' ):
233
232
value = valueForName (self , column )
234
- if isinstance (value , FloatType ):
235
- value = '%0.4f' % value # might need more flexibility in the future
233
+ if isinstance (value , float ):
234
+ # might need more flexibility in the future
235
+ value = '%0.4f' % value
236
236
else :
237
237
value = str (value )
238
238
values .append (value )
@@ -257,17 +257,18 @@ def handleException(self, excInfo):
257
257
self ._scriptEndTime = time ()
258
258
self .logExceptionToConsole ()
259
259
self .reset ()
260
- print self .htmlErrorPage (showDebugInfo = self .setting ('ShowDebugInfoOnErrors' ))
260
+ print self .htmlErrorPage (
261
+ showDebugInfo = self .setting ('ShowDebugInfoOnErrors' ))
261
262
fullErrorMsg = None
262
263
if self .setting ('SaveErrorMessages' ):
263
- fullErrorMsg = self .htmlErrorPage (showDebugInfo = 1 )
264
+ fullErrorMsg = self .htmlErrorPage (showDebugInfo = True )
264
265
filename = self .saveHTMLErrorPage (fullErrorMsg )
265
266
else :
266
267
filename = ''
267
268
self .logExceptionToDisk (filename )
268
269
if self .setting ('EmailErrors' ):
269
270
if fullErrorMsg is None :
270
- fullErrorMsg = self .htmlErrorPage (showDebugInfo = 1 )
271
+ fullErrorMsg = self .htmlErrorPage (showDebugInfo = True )
271
272
self .emailException (fullErrorMsg )
272
273
273
274
def logExceptionToConsole (self , stderr = sys .stderr ):
@@ -279,7 +280,8 @@ def logExceptionToConsole(self, stderr=sys.stderr):
279
280
280
281
"""
281
282
# stderr logging
282
- stderr .write ('[%s] [error] CGI Wrapper: Error while executing script %s\n ' % (
283
+ stderr .write ('[%s] [error] CGI Wrapper:'
284
+ ' Error while executing script %s\n ' % (
283
285
asctime (localtime (self ._scriptEndTime )), self ._scriptPathname ))
284
286
traceback .print_exc (file = stderr )
285
287
@@ -291,7 +293,8 @@ def reset(self):
291
293
Currently resets headers and deletes cookies, if present.
292
294
293
295
"""
294
- # Set headers to basic text/html. We don't want stray headers from a script that failed.
296
+ # Set headers to basic text/html. We don't want stray headers
297
+ # from a script that failed.
295
298
self ._headers = self .makeHeaders ()
296
299
# Get rid of cookies, too
297
300
if 'cookies' in self ._namespace :
@@ -300,8 +303,8 @@ def reset(self):
300
303
def htmlErrorPage (self , showDebugInfo = True ):
301
304
"""Return an HTML page explaining that there is an error.
302
305
303
- There could be more options in the future so using named arguments
304
- (e.g., ' showDebugInfo=1' ) is recommended. Invoked by handleException().
306
+ There could be more options in the future, so using named arguments
307
+ (e.g. showDebugInfo=False ) is recommended. Invoked by handleException().
305
308
306
309
"""
307
310
html = ['''%s
@@ -387,16 +390,16 @@ def logExceptionToDisk(self, errorMsgFilename=None, excInfo=None):
387
390
if not excInfo :
388
391
excInfo = sys .exc_info ()
389
392
err , msg = excInfo [:2 ]
390
- if isinstance (err , ClassType ):
391
- err , msg = err .__name__ , str (msg )
392
- else : # string exception
393
+ if isinstance (err , basestring ): # string exception
393
394
err , msg = '' , str (msg or err )
395
+ else :
396
+ err , msg = err .__name__ , str (msg )
394
397
logline = (asctime (localtime (self ._scriptEndTime )),
395
398
os .path .split (self ._scriptPathname )[1 ], self ._scriptPathname ,
396
399
err , msg , errorMsgFilename or '' )
397
400
def fixElement (element ):
398
401
element = str (element )
399
- if element . find ( ',' ) >= 0 or element . find ( '"' ) >= 0 :
402
+ if ',' in element or '"' in element :
400
403
element = element .replace ('"' , '""' )
401
404
element = '"%s"' % element
402
405
return element
0 commit comments