Skip to content

Commit 0a01874

Browse files
committed
fixes to chapter 6
1 parent a1cd757 commit 0a01874

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

doc/source/6_exceptions.rst

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -529,32 +529,31 @@ happens if we run this code? Let's try:
529529

530530
.. code-block:: ipython
531531
532-
In [5]: gcd(10, 12)
533-
---------------------------------------------------------------------------
534-
ZeroDivisionError Traceback (most recent call last)
535-
<ipython-input-5-d0750d9f2658> in <module>
532+
In [2]: gcd(10, 12)
533+
--------------------------------------------------------------------------
534+
ZeroDivisionError Traceback (most recent call last)
535+
Cell In[2], line 1
536536
----> 1 gcd(10, 12)
537537
538-
<ipython-input-4-1ab7512041a6> in gcd(a, b)
539-
1 def gcd(a, b):
538+
Cell In[1], line 2, in gcd(a, b)
539+
1 def gcd(a, b):
540540
----> 2 return gcd(b, a % b)
541541
542-
<ipython-input-4-1ab7512041a6> in gcd(a, b)
543-
1 def gcd(a, b):
542+
Cell In[1], line 2, in gcd(a, b)
543+
1 def gcd(a, b):
544544
----> 2 return gcd(b, a % b)
545545
546-
<ipython-input-4-1ab7512041a6> in gcd(a, b)
547-
1 def gcd(a, b):
548-
----> 2 return gcd(b, a % b)
546+
[... skipping similar frames: gcd at line 2 (1 times)]
549547
550-
<ipython-input-4-1ab7512041a6> in gcd(a, b)
551-
1 def gcd(a, b):
548+
Cell In[1], line 2, in gcd(a, b)
549+
1 def gcd(a, b):
552550
----> 2 return gcd(b, a % b)
553551
554-
ZeroDivisionError: integer division or modulo by zero
552+
ZeroDivisionError: integer modulo by zero
555553
556554
Notice how the recursive call to :func:`gcd` causes several
557-
:term:`stack frames <stack frame>` that look the same. That makes
555+
:term:`stack frames <stack frame>` that look the same. Indeed, the Python
556+
interpreter even notices the similarity and skips over one. That makes
558557
sense: :func:`gcd` calls itself until `b` is zero, and then we get a
559558
:class:`ZeroDivisionError` because modulo zero is undefined. To
560559
complete this function, what we need to do is to tell Python to stop
@@ -725,11 +724,17 @@ clause. Indeed, some of the functions called might themselves contain
725724
:keyword:`try` blocks with the result that an exception is raised at a
726725
point which is ultimately inside several :keyword:`try` blocks.
727726

728-
The :term:`Python interpreter` deals with this situation by starting
729-
from the current :term:`stack frame` and working upwards, a process
730-
known as *unwinding the stack*. In pseudocode, the algorithm is:
727+
The :term:`Python interpreter` deals with this situation by starting from the
728+
current :term:`stack frame` and working upwards, a process known as *unwinding
729+
the stack*. :numref:`unwind` shows pseudocode for this process.
730+
731+
.. _unwind:
731732

732733
.. code-block:: python3
734+
:caption: Pseudocode for the process of *unwinding the stack*, in which the
735+
interpreter successively looks through higher stack frames to search
736+
for an :keyword:`except` clause matching the exception that has just
737+
been raised.
733738
734739
while call stack not empty:
735740
if current execution point is in a try block \

0 commit comments

Comments
 (0)