mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 07:31:38 +00:00 
			
		
		
		
	Update to 3.13 the output of exceptions raised (#123888)
This commit is contained in:
		
							parent
							
								
									fe49e8f32f
								
							
						
					
					
						commit
						39612103dd
					
				
					 1 changed files with 42 additions and 3 deletions
				
			
		| 
						 | 
					@ -45,14 +45,20 @@ programs, however, and result in error messages as shown here::
 | 
				
			||||||
   >>> 10 * (1/0)
 | 
					   >>> 10 * (1/0)
 | 
				
			||||||
   Traceback (most recent call last):
 | 
					   Traceback (most recent call last):
 | 
				
			||||||
     File "<stdin>", line 1, in <module>
 | 
					     File "<stdin>", line 1, in <module>
 | 
				
			||||||
 | 
					       10 * (1/0)
 | 
				
			||||||
 | 
					             ~^~
 | 
				
			||||||
   ZeroDivisionError: division by zero
 | 
					   ZeroDivisionError: division by zero
 | 
				
			||||||
   >>> 4 + spam*3
 | 
					   >>> 4 + spam*3
 | 
				
			||||||
   Traceback (most recent call last):
 | 
					   Traceback (most recent call last):
 | 
				
			||||||
     File "<stdin>", line 1, in <module>
 | 
					     File "<stdin>", line 1, in <module>
 | 
				
			||||||
 | 
					       4 + spam*3
 | 
				
			||||||
 | 
					           ^^^^
 | 
				
			||||||
   NameError: name 'spam' is not defined
 | 
					   NameError: name 'spam' is not defined
 | 
				
			||||||
   >>> '2' + 2
 | 
					   >>> '2' + 2
 | 
				
			||||||
   Traceback (most recent call last):
 | 
					   Traceback (most recent call last):
 | 
				
			||||||
     File "<stdin>", line 1, in <module>
 | 
					     File "<stdin>", line 1, in <module>
 | 
				
			||||||
 | 
					       '2' + 2
 | 
				
			||||||
 | 
					       ~~~~^~~
 | 
				
			||||||
   TypeError: can only concatenate str (not "int") to str
 | 
					   TypeError: can only concatenate str (not "int") to str
 | 
				
			||||||
 | 
					
 | 
				
			||||||
The last line of the error message indicates what happened. Exceptions come in
 | 
					The last line of the error message indicates what happened. Exceptions come in
 | 
				
			||||||
| 
						 | 
					@ -252,6 +258,7 @@ exception to occur. For example::
 | 
				
			||||||
   >>> raise NameError('HiThere')
 | 
					   >>> raise NameError('HiThere')
 | 
				
			||||||
   Traceback (most recent call last):
 | 
					   Traceback (most recent call last):
 | 
				
			||||||
     File "<stdin>", line 1, in <module>
 | 
					     File "<stdin>", line 1, in <module>
 | 
				
			||||||
 | 
					       raise NameError('HiThere')
 | 
				
			||||||
   NameError: HiThere
 | 
					   NameError: HiThere
 | 
				
			||||||
 | 
					
 | 
				
			||||||
The sole argument to :keyword:`raise` indicates the exception to be raised.
 | 
					The sole argument to :keyword:`raise` indicates the exception to be raised.
 | 
				
			||||||
| 
						 | 
					@ -275,6 +282,7 @@ re-raise the exception::
 | 
				
			||||||
   An exception flew by!
 | 
					   An exception flew by!
 | 
				
			||||||
   Traceback (most recent call last):
 | 
					   Traceback (most recent call last):
 | 
				
			||||||
     File "<stdin>", line 2, in <module>
 | 
					     File "<stdin>", line 2, in <module>
 | 
				
			||||||
 | 
					       raise NameError('HiThere')
 | 
				
			||||||
   NameError: HiThere
 | 
					   NameError: HiThere
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -294,12 +302,15 @@ message::
 | 
				
			||||||
    ...
 | 
					    ...
 | 
				
			||||||
    Traceback (most recent call last):
 | 
					    Traceback (most recent call last):
 | 
				
			||||||
      File "<stdin>", line 2, in <module>
 | 
					      File "<stdin>", line 2, in <module>
 | 
				
			||||||
 | 
					        open("database.sqlite")
 | 
				
			||||||
 | 
					        ~~~~^^^^^^^^^^^^^^^^^^^
 | 
				
			||||||
    FileNotFoundError: [Errno 2] No such file or directory: 'database.sqlite'
 | 
					    FileNotFoundError: [Errno 2] No such file or directory: 'database.sqlite'
 | 
				
			||||||
    <BLANKLINE>
 | 
					    <BLANKLINE>
 | 
				
			||||||
    During handling of the above exception, another exception occurred:
 | 
					    During handling of the above exception, another exception occurred:
 | 
				
			||||||
    <BLANKLINE>
 | 
					    <BLANKLINE>
 | 
				
			||||||
    Traceback (most recent call last):
 | 
					    Traceback (most recent call last):
 | 
				
			||||||
      File "<stdin>", line 4, in <module>
 | 
					      File "<stdin>", line 4, in <module>
 | 
				
			||||||
 | 
					        raise RuntimeError("unable to handle error")
 | 
				
			||||||
    RuntimeError: unable to handle error
 | 
					    RuntimeError: unable to handle error
 | 
				
			||||||
 | 
					
 | 
				
			||||||
To indicate that an exception is a direct consequence of another, the
 | 
					To indicate that an exception is a direct consequence of another, the
 | 
				
			||||||
| 
						 | 
					@ -320,6 +331,8 @@ This can be useful when you are transforming exceptions. For example::
 | 
				
			||||||
    ...
 | 
					    ...
 | 
				
			||||||
    Traceback (most recent call last):
 | 
					    Traceback (most recent call last):
 | 
				
			||||||
      File "<stdin>", line 2, in <module>
 | 
					      File "<stdin>", line 2, in <module>
 | 
				
			||||||
 | 
					        func()
 | 
				
			||||||
 | 
					        ~~~~^^
 | 
				
			||||||
      File "<stdin>", line 2, in func
 | 
					      File "<stdin>", line 2, in func
 | 
				
			||||||
    ConnectionError
 | 
					    ConnectionError
 | 
				
			||||||
    <BLANKLINE>
 | 
					    <BLANKLINE>
 | 
				
			||||||
| 
						 | 
					@ -327,6 +340,7 @@ This can be useful when you are transforming exceptions. For example::
 | 
				
			||||||
    <BLANKLINE>
 | 
					    <BLANKLINE>
 | 
				
			||||||
    Traceback (most recent call last):
 | 
					    Traceback (most recent call last):
 | 
				
			||||||
      File "<stdin>", line 4, in <module>
 | 
					      File "<stdin>", line 4, in <module>
 | 
				
			||||||
 | 
					        raise RuntimeError('Failed to open database') from exc
 | 
				
			||||||
    RuntimeError: Failed to open database
 | 
					    RuntimeError: Failed to open database
 | 
				
			||||||
 | 
					
 | 
				
			||||||
It also allows disabling automatic exception chaining using the ``from None``
 | 
					It also allows disabling automatic exception chaining using the ``from None``
 | 
				
			||||||
| 
						 | 
					@ -339,6 +353,7 @@ idiom::
 | 
				
			||||||
    ...
 | 
					    ...
 | 
				
			||||||
    Traceback (most recent call last):
 | 
					    Traceback (most recent call last):
 | 
				
			||||||
      File "<stdin>", line 4, in <module>
 | 
					      File "<stdin>", line 4, in <module>
 | 
				
			||||||
 | 
					        raise RuntimeError from None
 | 
				
			||||||
    RuntimeError
 | 
					    RuntimeError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
For more information about chaining mechanics, see :ref:`bltin-exceptions`.
 | 
					For more information about chaining mechanics, see :ref:`bltin-exceptions`.
 | 
				
			||||||
| 
						 | 
					@ -381,6 +396,7 @@ example::
 | 
				
			||||||
   Goodbye, world!
 | 
					   Goodbye, world!
 | 
				
			||||||
   Traceback (most recent call last):
 | 
					   Traceback (most recent call last):
 | 
				
			||||||
     File "<stdin>", line 2, in <module>
 | 
					     File "<stdin>", line 2, in <module>
 | 
				
			||||||
 | 
					       raise KeyboardInterrupt
 | 
				
			||||||
   KeyboardInterrupt
 | 
					   KeyboardInterrupt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
If a :keyword:`finally` clause is present, the :keyword:`!finally`
 | 
					If a :keyword:`finally` clause is present, the :keyword:`!finally`
 | 
				
			||||||
| 
						 | 
					@ -448,7 +464,11 @@ A more complicated example::
 | 
				
			||||||
   executing finally clause
 | 
					   executing finally clause
 | 
				
			||||||
   Traceback (most recent call last):
 | 
					   Traceback (most recent call last):
 | 
				
			||||||
     File "<stdin>", line 1, in <module>
 | 
					     File "<stdin>", line 1, in <module>
 | 
				
			||||||
 | 
					       divide("2", "0")
 | 
				
			||||||
 | 
					       ~~~~~~^^^^^^^^^^
 | 
				
			||||||
     File "<stdin>", line 3, in divide
 | 
					     File "<stdin>", line 3, in divide
 | 
				
			||||||
 | 
					       result = x / y
 | 
				
			||||||
 | 
					                ~~^~~
 | 
				
			||||||
   TypeError: unsupported operand type(s) for /: 'str' and 'str'
 | 
					   TypeError: unsupported operand type(s) for /: 'str' and 'str'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
As you can see, the :keyword:`finally` clause is executed in any event.  The
 | 
					As you can see, the :keyword:`finally` clause is executed in any event.  The
 | 
				
			||||||
| 
						 | 
					@ -511,8 +531,11 @@ caught like any other exception. ::
 | 
				
			||||||
   >>> f()
 | 
					   >>> f()
 | 
				
			||||||
     + Exception Group Traceback (most recent call last):
 | 
					     + Exception Group Traceback (most recent call last):
 | 
				
			||||||
     |   File "<stdin>", line 1, in <module>
 | 
					     |   File "<stdin>", line 1, in <module>
 | 
				
			||||||
 | 
					     |     f()
 | 
				
			||||||
 | 
					     |     ~^^
 | 
				
			||||||
     |   File "<stdin>", line 3, in f
 | 
					     |   File "<stdin>", line 3, in f
 | 
				
			||||||
     | ExceptionGroup: there were problems
 | 
					     |     raise ExceptionGroup('there were problems', excs)
 | 
				
			||||||
 | 
					     | ExceptionGroup: there were problems (2 sub-exceptions)
 | 
				
			||||||
     +-+---------------- 1 ----------------
 | 
					     +-+---------------- 1 ----------------
 | 
				
			||||||
       | OSError: error 1
 | 
					       | OSError: error 1
 | 
				
			||||||
       +---------------- 2 ----------------
 | 
					       +---------------- 2 ----------------
 | 
				
			||||||
| 
						 | 
					@ -560,10 +583,15 @@ other clauses and eventually to be reraised. ::
 | 
				
			||||||
   There were SystemErrors
 | 
					   There were SystemErrors
 | 
				
			||||||
     + Exception Group Traceback (most recent call last):
 | 
					     + Exception Group Traceback (most recent call last):
 | 
				
			||||||
     |   File "<stdin>", line 2, in <module>
 | 
					     |   File "<stdin>", line 2, in <module>
 | 
				
			||||||
 | 
					     |     f()
 | 
				
			||||||
 | 
					     |     ~^^
 | 
				
			||||||
     |   File "<stdin>", line 2, in f
 | 
					     |   File "<stdin>", line 2, in f
 | 
				
			||||||
     | ExceptionGroup: group1
 | 
					     |     raise ExceptionGroup(
 | 
				
			||||||
 | 
					     |     ...<12 lines>...
 | 
				
			||||||
 | 
					     |     )
 | 
				
			||||||
 | 
					     | ExceptionGroup: group1 (1 sub-exception)
 | 
				
			||||||
     +-+---------------- 1 ----------------
 | 
					     +-+---------------- 1 ----------------
 | 
				
			||||||
       | ExceptionGroup: group2
 | 
					       | ExceptionGroup: group2 (1 sub-exception)
 | 
				
			||||||
       +-+---------------- 1 ----------------
 | 
					       +-+---------------- 1 ----------------
 | 
				
			||||||
         | RecursionError: 4
 | 
					         | RecursionError: 4
 | 
				
			||||||
         +------------------------------------
 | 
					         +------------------------------------
 | 
				
			||||||
| 
						 | 
					@ -607,6 +635,7 @@ includes all notes, in the order they were added, after the exception. ::
 | 
				
			||||||
   ...
 | 
					   ...
 | 
				
			||||||
   Traceback (most recent call last):
 | 
					   Traceback (most recent call last):
 | 
				
			||||||
     File "<stdin>", line 2, in <module>
 | 
					     File "<stdin>", line 2, in <module>
 | 
				
			||||||
 | 
					       raise TypeError('bad type')
 | 
				
			||||||
   TypeError: bad type
 | 
					   TypeError: bad type
 | 
				
			||||||
   Add some information
 | 
					   Add some information
 | 
				
			||||||
   Add some more information
 | 
					   Add some more information
 | 
				
			||||||
| 
						 | 
					@ -630,23 +659,33 @@ exception in the group has a note indicating when this error has occurred. ::
 | 
				
			||||||
   >>> raise ExceptionGroup('We have some problems', excs)
 | 
					   >>> raise ExceptionGroup('We have some problems', excs)
 | 
				
			||||||
     + Exception Group Traceback (most recent call last):
 | 
					     + Exception Group Traceback (most recent call last):
 | 
				
			||||||
     |   File "<stdin>", line 1, in <module>
 | 
					     |   File "<stdin>", line 1, in <module>
 | 
				
			||||||
 | 
					     |     raise ExceptionGroup('We have some problems', excs)
 | 
				
			||||||
     | ExceptionGroup: We have some problems (3 sub-exceptions)
 | 
					     | ExceptionGroup: We have some problems (3 sub-exceptions)
 | 
				
			||||||
     +-+---------------- 1 ----------------
 | 
					     +-+---------------- 1 ----------------
 | 
				
			||||||
       | Traceback (most recent call last):
 | 
					       | Traceback (most recent call last):
 | 
				
			||||||
       |   File "<stdin>", line 3, in <module>
 | 
					       |   File "<stdin>", line 3, in <module>
 | 
				
			||||||
 | 
					       |     f()
 | 
				
			||||||
 | 
					       |     ~^^
 | 
				
			||||||
       |   File "<stdin>", line 2, in f
 | 
					       |   File "<stdin>", line 2, in f
 | 
				
			||||||
 | 
					       |     raise OSError('operation failed')
 | 
				
			||||||
       | OSError: operation failed
 | 
					       | OSError: operation failed
 | 
				
			||||||
       | Happened in Iteration 1
 | 
					       | Happened in Iteration 1
 | 
				
			||||||
       +---------------- 2 ----------------
 | 
					       +---------------- 2 ----------------
 | 
				
			||||||
       | Traceback (most recent call last):
 | 
					       | Traceback (most recent call last):
 | 
				
			||||||
       |   File "<stdin>", line 3, in <module>
 | 
					       |   File "<stdin>", line 3, in <module>
 | 
				
			||||||
 | 
					       |     f()
 | 
				
			||||||
 | 
					       |     ~^^
 | 
				
			||||||
       |   File "<stdin>", line 2, in f
 | 
					       |   File "<stdin>", line 2, in f
 | 
				
			||||||
 | 
					       |     raise OSError('operation failed')
 | 
				
			||||||
       | OSError: operation failed
 | 
					       | OSError: operation failed
 | 
				
			||||||
       | Happened in Iteration 2
 | 
					       | Happened in Iteration 2
 | 
				
			||||||
       +---------------- 3 ----------------
 | 
					       +---------------- 3 ----------------
 | 
				
			||||||
       | Traceback (most recent call last):
 | 
					       | Traceback (most recent call last):
 | 
				
			||||||
       |   File "<stdin>", line 3, in <module>
 | 
					       |   File "<stdin>", line 3, in <module>
 | 
				
			||||||
 | 
					       |     f()
 | 
				
			||||||
 | 
					       |     ~^^
 | 
				
			||||||
       |   File "<stdin>", line 2, in f
 | 
					       |   File "<stdin>", line 2, in f
 | 
				
			||||||
 | 
					       |     raise OSError('operation failed')
 | 
				
			||||||
       | OSError: operation failed
 | 
					       | OSError: operation failed
 | 
				
			||||||
       | Happened in Iteration 3
 | 
					       | Happened in Iteration 3
 | 
				
			||||||
       +------------------------------------
 | 
					       +------------------------------------
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue