By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,273 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

Reading in external file - error checking and line numbers...

P: n/a
I'm writing a tool at the moment that reads in an external file (which
can use any Python syntax)

At the moment, I'm reading the file in using:

scriptLines = open(baseRippleScript).read()
exec scriptLines

However, if I raise an exception in my main code, in a function that is
called from the external script, the stack trace just has:

File "<string>", line 8, in ?

Ideally, I'd want to be able to avoid throwing exceptions and would
like to, from my main code, print out an error that included the script
name (easily accessible) and the line number (less easily accessible).

Is there a better way of executing an external script that would let me
access at any time the line number from the external script that is
being executed.
More specifically, if a function is called from an external script with
an invalid parameter type, I want to be able to flag it accurately to
the user....

Hope this made sense - let me know if I've confused you at all.....
--
Hugh Macdonald

Sep 7 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hugh Macdonald wrote:
I'm writing a tool at the moment that reads in an external file (which
can use any Python syntax)

At the moment, I'm reading the file in using:

scriptLines = open(baseRippleScript).read()
exec scriptLines

However, if I raise an exception in my main code, in a function that is
called from the external script, the stack trace just has:

File "<string>", line 8, in ?

Ideally, I'd want to be able to avoid throwing exceptions and would
like to, from my main code, print out an error that included the script
name (easily accessible) and the line number (less easily accessible).


exec compile(scriptLines, baseRippleScript, "exec")

(but in this case, you might as well use the "execfile" built-in)

</F>

Sep 7 '05 #2

P: n/a
Thankyou! That was much easier than I expected.....

One more thing on a similar note..... When raising exceptions, is it
possible to remove a few items from the top of the stack trace?

My stack trace is looking something like:

File "ripple", line 160, in ?
File "ripple", line 94, in executeRipple
File "test.rip", line 8, in ?
dependsOnFrame = new)
File "ripple", line 133, in __init__
File "ripple", line 148, in addDependsOnFrame
__main__.RippleError: 'Cannot add frame dependency to non frame-based
node'

I'd like to be able to remove the last two items in the stack so that
it just shows the user:

File "ripple", line 160, in ?
File "ripple", line 94, in executeRipple
File "test.rip", line 8, in ?
dependsOnFrame = new)
__main__.RippleError: 'Cannot add frame dependency to non frame-based
node'

Unfortunately, I don't know how many 'ripple' stack items there will
be...

This is why I'd much rather, if I can, do this without exceptions and
just be able to print out my own error message with the problem line
number marked....

Or am I asking too much? ;)

--
Hugh Macdonald

Sep 7 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.