can't print the exception cause/context in Python3.0?

B

BigHand

Here is copy from my IDLE of python 3.0
return tuple()[0]
a()
except:
exc_typ, exc_val, exc_tb = sys.exc_info()

File "<pyshell#14>", line 2, in <module>
File "<pyshell#6>", line 2, in a
File "<pyshell#9>", line 2, in b

but this doesn't output the cause/context like 2.6: or on the sample
of:
http://docs.python.org/3.0/library/...t=format_exception#traceback.format_exception

I want the exception printted like this:
File "<pyshell#14>", line 2, in <module> "a()"
File "<pyshell#6>", line 2, in a "b()"
File "<pyshell#9>", line 2, in b "return tuple()[0]"


how should I do?
 
G

Gabriel Genellina

Here is copy from my IDLE of python 3.0

File "<pyshell#14>", line 2, in <module>
File "<pyshell#6>", line 2, in a
File "<pyshell#9>", line 2, in b

but this doesn't output the cause/context like 2.6: or on the sample
of:
http://docs.python.org/3.0/library/...t=format_exception#traceback.format_exception

I want the exception printted like this:
File "<pyshell#14>", line 2, in <module> "a()"
File "<pyshell#6>", line 2, in a "b()"
File "<pyshell#9>", line 2, in b "return tuple()[0]"

Put your code in a true module stored in a file, so the source lines can
be retrieved.
 
B

BigHand

En Sat, 07 Mar 2009 11:46:08 -0200, BigHand <[email protected]> escribió:


Here is copy from my IDLE of python 3.0
  File "<pyshell#14>", line 2, in <module>
  File "<pyshell#6>", line 2, in a
  File "<pyshell#9>", line 2, in b
but this doesn't output the cause/context like 2.6: or on the sample
of:
http://docs.python.org/3.0/library/traceback.html?highlight=format_ex....
I want the exception printted like this:
  File "<pyshell#14>", line 2, in <module>  "a()"
  File "<pyshell#6>", line 2, in a        "b()"
  File "<pyshell#9>", line 2, in b       "return tuple()[0]"

Put your code in a true module stored in a file, so the source lines can  
be retrieved.

hello,Gabriel,
I don't understand you.could you give me more details?
http://docs.python.org/3.0/library/...t=format_exception#traceback.format_exception
the up page is from python 3.0's doc. maybe from a older version of
Python,

even I excute these file in a py script file, it's can't retrieve the
source line either.
 
G

Gabriel Genellina

I want the exception printted like this:
  File "<pyshell#14>", line 2, in <module>  "a()"
  File "<pyshell#6>", line 2, in a        "b()"
  File "<pyshell#9>", line 2, in b       "return tuple()[0]"

Put your code in a true module stored in a file, so the source lines
can  
be retrieved.
I don't understand you.could you give me more details?


C:\TEMP>type tbtest.py
import sys
import traceback

def a(): b()

def b(): raise ValueError

print("\none\n")
try: a()
except:
exc_typ, exc_val, exc_tb = sys.exc_info()
traceback.print_tb(exc_tb)

print("\ntwo\n")
try: a()
except:
exc_typ, exc_val, exc_tb = sys.exc_info()
traceback.print_tb(exc_tb)

print("\nthree\n")
a()

C:\TEMP>python30 tbtest.py

one

File "tbtest.py", line 9, in <module>
try: a()
File "tbtest.py", line 4, in a
def a(): b()
File "tbtest.py", line 6, in b
def b(): raise ValueError

two

File "tbtest.py", line 15, in <module>
try: a()
File "tbtest.py", line 4, in a
def a(): b()
File "tbtest.py", line 6, in b
def b(): raise ValueError

three

Traceback (most recent call last):
File "tbtest.py", line 21, in <module>
a()
File "tbtest.py", line 4, in a
def a(): b()
File "tbtest.py", line 6, in b
def b(): raise ValueError
ValueError

C:\TEMP>
 
B

BigHand

En Sat, 07 Mar 2009 11:46:08 -0200, BigHand <[email protected]>  
escribió:
I want the exception printted like this:
  File "<pyshell#14>", line 2, in <module>  "a()"
  File "<pyshell#6>", line 2, in a        "b()"
  File "<pyshell#9>", line 2, in b       "return tuple()[0]"
Put your code in a true module stored in a file, so the source lines  
can  
be retrieved.
    I don't understand you.could you give me more details?

C:\TEMP>type tbtest.py
import sys
import traceback

def a(): b()

def b(): raise ValueError

print("\none\n")
try: a()
except:
   exc_typ, exc_val, exc_tb = sys.exc_info()
   traceback.print_tb(exc_tb)

print("\ntwo\n")
try: a()
except:
   exc_typ, exc_val, exc_tb = sys.exc_info()
traceback.print_tb(exc_tb)

print("\nthree\n")
a()

C:\TEMP>python30 tbtest.py

one

   File "tbtest.py", line 9, in <module>
     try: a()
   File "tbtest.py", line 4, in a
     def a(): b()
   File "tbtest.py", line 6, in b
     def b(): raise ValueError

two

   File "tbtest.py", line 15, in <module>
     try: a()
   File "tbtest.py", line 4, in a
     def a(): b()
   File "tbtest.py", line 6, in b
     def b(): raise ValueError

three

Traceback (most recent call last):
   File "tbtest.py", line 21, in <module>
     a()
   File "tbtest.py", line 4, in a
     def a(): b()
   File "tbtest.py", line 6, in b
     def b(): raise ValueError
ValueError

C:\TEMP>

hello.Gabriel,
thanks very much!

My another issue is that, I have an embedded python3.0 in my MFC app,
use the PyRun_File to execute the Py script file, and call the python
function traceback.print_tb to print the traceback info,

but I can't get souce showed in the traceback.
 
B

BigHand

En Sat, 07 Mar 2009 21:18:22 -0200, BigHand <[email protected]> escribió:
On 3月7æ—¥, 下åˆ11æ—¶21分, "Gabriel Genellina" <[email protected]>  
wrote:
En Sat, 07 Mar 2009 11:46:08 -0200, BigHand <[email protected]>  
escribió:
I want the exception printted like this:
  File "<pyshell#14>", line 2, in <module>  "a()"
  File "<pyshell#6>", line 2, in a        "b()"
  File "<pyshell#9>", line 2, in b       "return tuple()[0]"
Put your code in a true module stored in a file, so the source lines  
can  
be retrieved.
    I don't understand you.could you give me more details?
C:\TEMP>type tbtest.py
import sys
import traceback
def a(): b()
def b(): raise ValueError
print("\none\n")
try: a()
except:
   exc_typ, exc_val, exc_tb = sys.exc_info()
   traceback.print_tb(exc_tb)
print("\ntwo\n")
try: a()
except:
   exc_typ, exc_val, exc_tb = sys.exc_info()
traceback.print_tb(exc_tb)

C:\TEMP>python30 tbtest.py

   File "tbtest.py", line 9, in <module>
     try: a()
   File "tbtest.py", line 4, in a
     def a(): b()
   File "tbtest.py", line 6, in b
     def b(): raise ValueError

   File "tbtest.py", line 15, in <module>
     try: a()
   File "tbtest.py", line 4, in a
     def a(): b()
   File "tbtest.py", line 6, in b
     def b(): raise ValueError

Traceback (most recent call last):
   File "tbtest.py", line 21, in <module>
     a()
   File "tbtest.py", line 4, in a
     def a(): b()
   File "tbtest.py", line 6, in b
     def b(): raise ValueError
ValueError

hello.Gabriel,
thanks very much!

My another issue is that, I have an embedded python3.0 in my MFC app,
use the PyRun_File to execute the Py script file, and call the python
function traceback.print_tb to print the traceback info,

but I can't get souce showed in the traceback.

solve it.
I need to "import traceback" first in the script file. or I can't get
the source retrieved later.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top