Skip to content

Commit c3b92ba

Browse files
authored
Fix Context Managers examples
`\begincenter` and `\endcenter` don't exist in LaTeX and the example code does not produce the expected result due to missing escape sequences for the backslash and curly braces. The example changes once you switch to raw strings, where backslashes do not need to be escaped, but curly braces still do. I have fixed the strings and outputs, and changed the 2nd and 3rd example to use simple strings instead of raw strings. Readers may not catch that the switch happens which adds to confusion since backslashes need to be escaped in simple strings, but not in raw strings. You also may want to consider adding a note, that even through the output will display two backslashes, the actual string only has one backslash. Finally the `may_error()` part could become very confusing, especially when people execute the code and get different results from the ones you printed and, even worse, potentially varying results if they run the example multiple times.
1 parent 8c4d8e4 commit c3b92ba

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

python.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,10 +2074,10 @@ If we were writing a Python module to write TeX, we might do something like this
20742074
the environments are closed properly::
20752075

20762076
>>> def start(env):
2077-
... return '\begin{}'.format(env)
2077+
... return '\\begin{{{}}}'.format(env)
20782078

20792079
>>> def end(env):
2080-
... return '\end{}'.format(env)
2080+
... return '\\end{{{}}}'.format(env)
20812081

20822082
>>> def may_error():
20832083
... import random
@@ -2102,18 +2102,18 @@ Function Based Context Managers
21022102
-------------------------------
21032103

21042104
To create a context manager with a function, decorate with
2105-
``contextlib.contextmanager``, and yield where you want to bookend::
2105+
``contextlib.contextmanager``, and yield where you want to insert your block::
21062106

21072107
>>> import contextlib
21082108
>>> @contextlib.contextmanager
21092109
... def env(name, content):
2110-
... content.append(r'\begin{}'.format(name))
2110+
... content.append('\\begin{{{}}}'.format(name))
21112111
... try:
21122112
... yield
21132113
... except ValueError:
21142114
... pass
21152115
... finally:
2116-
... content.append(r'\end{}'.format(name))
2116+
... content.append('\\end{{{}}}'.format(name))
21172117

21182118
Our code looks better now, and there will always be a closing tag::
21192119

@@ -2122,7 +2122,7 @@ Our code looks better now, and there will always be a closing tag::
21222122
... out.append(may_error())
21232123

21242124
>>> out
2125-
['\\begincenter', 'content', '\\endcenter']
2125+
['\\begin{center}', 'content', '\\end{center}']
21262126

21272127
Class Based Context Managers
21282128
----------------------------
@@ -2135,14 +2135,14 @@ To create a class based context manager, implement the ``__enter__`` and ``__exi
21352135
... self.content = content
21362136
...
21372137
... def __enter__(self):
2138-
... self.content.append(r'\begin{}'.format(
2138+
... self.content.append('\\begin{{{}}}'.format(
21392139
... self.name))
21402140
...
21412141
... def __exit__(self, type, value, tb):
21422142
... # if error in block, t, v, & tb
21432143
... # have non None values
21442144
... # return True to hide exception
2145-
... self.content.append(r'\end{}'.format(
2145+
... self.content.append('\\end{{{}}}'.format(
21462146
... self.name))
21472147
... return True
21482148

@@ -2153,7 +2153,7 @@ The code looks the same as using the function based context manager::
21532153
... out.append(may_error())
21542154

21552155
>>> out # may_error had an issue
2156-
['\\begincenter', '\\endcenter']
2156+
['\\begin{center}', '\\end{center}']
21572157

21582158

21592159
Context objects

0 commit comments

Comments
 (0)