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

Discussion in 'Python' started by BigHand, Mar 7, 2009.

  1. BigHand

    BigHand Guest

    Here is copy from my IDLE of python 3.0

    >>> import sys
    >>> import traceback
    >>> def a():

    b()


    >>> def b():

    return tuple()[0]

    >>> try:

    a()
    except:
    exc_typ, exc_val, exc_tb = sys.exc_info()


    >>> traceback.print_tb(exc_tb)

    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?
     
    BigHand, Mar 7, 2009
    #1
    1. Advertising

  2. En Sat, 07 Mar 2009 11:46:08 -0200, BigHand <> escribió:

    > Here is copy from my IDLE of python 3.0
    >
    >
    >>>> traceback.print_tb(exc_tb)

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

    --
    Gabriel Genellina
     
    Gabriel Genellina, Mar 7, 2009
    #2
    1. Advertising

  3. BigHand

    BigHand Guest

    On 3月7æ—¥, 下åˆ11æ—¶21分, "Gabriel Genellina" <> wrote:
    > En Sat, 07 Mar 2009 11:46:08 -0200, BigHand <> escribió:
    >
    >
    >
    > > Here is copy from my IDLE of python 3.0

    >
    > >>>> traceback.print_tb(exc_tb)

    > >   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.
    >
    > --
    > Gabriel Genellina


    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.
     
    BigHand, Mar 7, 2009
    #3
  4. En Sat, 07 Mar 2009 21:18:22 -0200, BigHand <> escribió:
    > On 3月7æ—¥, 下åˆ11æ—¶21分, "Gabriel Genellina" <>
    > wrote:
    >> En Sat, 07 Mar 2009 11:46:08 -0200, BigHand <>
    >> 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>


    --
    Gabriel Genellina
     
    Gabriel Genellina, Mar 8, 2009
    #4
  5. BigHand

    BigHand Guest

    On Mar 8, 9:49 pm, "Gabriel Genellina" <> wrote:
    > En Sat, 07 Mar 2009 21:18:22 -0200, BigHand <> escribió:
    >
    > > On 3月7æ—¥, 下åˆ11æ—¶21分, "Gabriel Genellina" <>  
    > > wrote:
    > >> En Sat, 07 Mar 2009 11:46:08 -0200, BigHand <>  
    > >> 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>
    >
    > --
    > Gabriel Genellina


    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.
     
    BigHand, Mar 9, 2009
    #5
  6. BigHand

    BigHand Guest

    On Mar 9, 9:02 am, BigHand <> wrote:
    > On Mar 8, 9:49 pm, "Gabriel Genellina" <> wrote:
    >
    >
    >
    > > En Sat, 07 Mar 2009 21:18:22 -0200, BigHand <> escribió:

    >
    > > > On 3月7æ—¥, 下åˆ11æ—¶21分, "Gabriel Genellina" <>  
    > > > wrote:
    > > >> En Sat, 07 Mar 2009 11:46:08 -0200, BigHand <>  
    > > >> 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>

    >
    > > --
    > > Gabriel Genellina

    >
    > 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.
     
    BigHand, Mar 9, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmlsbCBCb3Jn?=

    How can .GetType cause exception?

    =?Utf-8?B?QmlsbCBCb3Jn?=, Sep 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    341
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Sep 29, 2004
  2. aaa
    Replies:
    0
    Views:
    465
  3. keto
    Replies:
    0
    Views:
    1,022
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    388
    David Cournapeau
    Dec 30, 2008
  5. Andrew Berg
    Replies:
    0
    Views:
    355
    Andrew Berg
    Jun 16, 2012
Loading...

Share This Page