|
220 | 220 | This process termination can be observed in program \programUrl{exceptions:use_sqrt_raise} shown as \cref{lst:exceptions:use_sqrt_raise}. |
221 | 221 | In the program, we iteratively apply our new \pythonil{sqrt} function to the values inside a \pythonil{tuple} using a \pythonilIdx{for}~loop. |
222 | 222 | We write the results of these computations to the \glsFull{stdout} using \pgls{fstring} and \pythonil{print}. |
223 | | -We here use the optional parameter \pythonilIdx{flush} of \pythonilIdx{print}, which has the default value~\pythonil{False}. |
224 | | -We set it to \pythonil{flush=True}, though, which forces that the output of \pythonilIdx{print} is directly and immediately written to the \pgls{stdout} and not cached. |
| 223 | +We here use the optional parameter \pythonilIdx{flush}\pythonIdx{print!flush} of \pythonilIdx{print}, which has the default value~\pythonil{False}. |
| 224 | +We set it to \pythonil{flush=True}\pythonIdx{print!flush}, though, which forces that the output of \pythonilIdx{print} is directly and immediately written to the \pgls{stdout} and not cached. |
225 | 225 | This is useful in this special example to prevent mix-ups between the \pgls{stdout} stream receiving the normal output of the program and the \glsFull{stderr} stream where error messages appear. |
226 | 226 | Both streams are captured and presented in \cref{exec:exceptions:use_sqrt_raise} and the order of the text could otherwise get mixed up. |
227 | 227 |
|
|
976 | 976 | Of course, we still have to test that our function behaves as expected for valid arguments. |
977 | 977 |
|
978 | 978 | In the second test function, \pythonil{test_sqrt_raises_arithmetic_error}, we check arguments that are floating point numbers but have illegal values. |
979 | | -In a \pythonil{for}-loop, we let a variable~\pythonil{number} iterate over the values \pythonil{[-1.0, inf, -inf, nan]}. |
| 979 | +In a \pythonil{for}~loop, we let a variable~\pythonil{number} iterate over the values \pythonil{[-1.0, inf, -inf, nan]}. |
980 | 980 | The first value is negative and the others are not finite. |
981 | 981 | The sanity check \pythonil{(not isfinite(number)) or (number < 0.0)} inside our function should therefore catch these values, unless we made an error and it somehow does not do what we thing it should be doing. |
982 | 982 | Thus, all of these values should cause our \pythonil{sqrt} function to raise an \pythonilIdx{ArithmeticError}. |
|
997 | 997 | \begin{sloppypar}% |
998 | 998 | Finally, in our test function \pythonil{test_sqrt_raises_type_error}, we check for \pythonilsIdx{TypeError} being raised. |
999 | 999 | We use exactly the same pattern as in the previous test function. |
1000 | | -In a \pythonil{for}-loop, we let a variable~\pythonil{number} iterate over the values \pythonil{[True, "x", None]}. |
| 1000 | +In a \pythonil{for}~loop, we let a variable~\pythonil{number} iterate over the values \pythonil{[True, "x", None]}. |
1001 | 1001 | Obviously, none of these values are of type \pythonil{float}. |
1002 | 1002 | The check \pythonil{isinstance(number, float)} should prevent these values from entering the actual computation. |
1003 | 1003 | Unless, of course, we misunderstood how \pythonilIdx{isinstance} works. |
|
0 commit comments