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

H

Hugh Macdonald

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.....
 
F

Fredrik Lundh

Hugh said:
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>
 
H

Hugh Macdonald

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? ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top