FIXED. try import except want lineno from Exception

Ben Gerblich benji_75 at hotmail.com
Thu Sep 19 01:09:04 EDT 2002


In the file "progfile.py", replace between the print start/end with:
<a more glorified version>
--<snip>--
self.userDefs[searchPath][file[:-3]] = {}
try:
    self.userDefs[searchPath][file[:-3]]['class'] = __import__(file[:-3])
    code = "object = self.userDefs[searchPath][file[:-3]]['class'].%s()" % 
file[:-3]
    exec code
    self.userDefs[searchPath][file[:-3]]['obj'] = object
except Exception, e:
    errortext = e
    for badfile, badlineno, badfunction, badtext in traceback.extract_tb( 
sys.exc_info()[2]):
        pass

    # ask the user to 1.'skip' this MDF continue loading other MDF's
    #              or 2. quit the application
    message = "The Module Definition File has an error.\n\n"
    message = message + "Filename: '%s'\n" % fullfilename
    message = message + "Line number: %s\n" % badlineno
    message = message + "Source Code: %s\n" % badtext
    message = message + "Function: %s\n" % badfunction
    message = message + "Error message: %s\n" % e
    message = message + "\n\nSkip this module, but load others?\n"
    message = message + "\nOr CANCEL the application?"

    response = wxMessageBox(message , "Confirm", wxOK | wxCANCEL| 
wxICON_EXCLAMATION, self)
    if response == wxOK:
        print '   ** Did not load this Module Definition File **'
        del self.userDefs[searchPath][file[:-3]]
    elif response == wxCANCEL:
        sys.exit(1)


>From: "Ben Gerblich" <benji_75 at hotmail.com>
>To: python-list at python.org
>Subject: try import except want lineno from Exception
>Date: Thu, 19 Sep 2002 13:53:39 +0930
>
>I am importing module files, and want to catch an error (if any) and
>print the filename and line number of the error.
>The "Exception ,e" does not return, or does not have the attirbutes listed 
>above. It only returns e="line() takes exactly 2 arguments (3 given)"
>
>I think I want to use SyntaxError as it has the attributes: filename, 
>lineno, offset and text.
>I have found no info on traceback.
>With the code below, how do I find the **line_number**?
>Thanks.
>--<snip>--
>[13:45:40]~/py/dev/import2:cat runme.py
>from progfile import *
>
>[13:45:44]~/py/dev/import2:cat progfile.py
>class Foo:
>    def __init__(self):
>        self.lines = []
>
>    def line(self,x):
>        self.lines.append( x )
>
>print 'start'
>try:
>    klass = __import__('module')
>    code = "object = klass.%s()" % 'Bar'
>    exec code
>except TypeError, e:
>    print 'TypeError\n%s' % e
>except Exception, e:
>    print 'Exception\n%s' % e
>print 'end'
>
>[13:45:51]~/py/dev/import2:cat module.py
>from progfile import Foo
>
>class Bar(Foo):
>    def __init__(self):
>        Foo.__init__(self)
>        print 'line-1'; self.line(20)
>        print 'line-2'; self.line(40,60) # <--- an error!!!
>        print 'line-3'; self.line(80)
>
>[13:45:57]~/py/dev/import2:python2 runme.py
>start
>line-1
>line-2
>TypeError
>line() takes exactly 2 arguments (3 given)
>end
>
>[13:46:07]~/py/dev/import2:
>
>
>_________________________________________________________________
>Join the world?s largest e-mail service with MSN Hotmail. 
>http://www.hotmail.com
>
>
>--
>http://mail.python.org/mailman/listinfo/python-list




_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: 
http://photos.msn.com/support/worldwide.aspx





More information about the Python-list mailing list