prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Recaser bug: adding fixed-case inside tex-math markup
  `<tex-math><fixed-case>O</fixed-case>(<fixed-case>M</fixed-case>(n^2))</tex-math>` caused the build to fail in #892
  
  </issue>
  <code>
  [start of bin/fixedcase/protect.py]
  1 #!/usr/bin/env python3
  2 
  3 # protect.py <infile> <outfile>
  4 # looks for file "truelist" in current dir
  5 
  6 # cd data/xml
  7 # for i in *xml ; do (cd ../../tools/fixedcase/ ; python3 ./protect.py ../../data/xml/$i /tmp/$i ; echo $i ); done > log
  8 
  9 
  10 import lxml.etree as ET
  11 import os
  12 import sys
  13 import copy
  14 import itertools
  15 import inspect
  16 
  17 from collections import defaultdict
  18 
  19 if __name__ == "__main__":
  20     from common import *
  21 else:
  22     from .common import *
  23 
  24 # recursive helper called by protect
  25 # protect text of "node", including children, and tails of children
  26 def protect_recurse(node, recased):
  27     if node.tag == "fixed-case":  # already protected
  28         newnode = copy.deepcopy(node)  # don't need to modify descendents
  29         newnode.tail = None  # tail will be protected by caller
  30         return newnode
  31     newnode = ET.Element(node.tag, node.attrib)
  32 
  33     def process(text, rc):
  34         i = 0
  35         for upper, chars in itertools.groupby(rc[: len(text)], lambda c: c.isupper()):
  36             charstr = "".join(chars)
  37             if upper:
  38                 p = ET.Element("fixed-case")
  39                 p.text = charstr
  40                 newnode.append(p)
  41             else:
  42                 append_text(newnode, text[i : i + len(charstr)])
  43 
  44             assert text[i : i + len(charstr)].lower() == charstr.lower(), (
  45                 i,
  46                 text,
  47                 charstr,
  48             )
  49             i += len(charstr)
  50 
  51     if node.text:
  52         process(node.text, recased)
  53         recased = recased[len(node.text) :]
  54     for child in node:
  55         protected_child = protect_recurse(child, recased)
  56         recased = recased[len(get_text(protected_child)) :]
  57         newnode.append(protected_child)
  58         if child.tail:
  59             process(child.tail, recased)
  60             recased = recased[len(child.tail) :]
  61 
  62     return newnode
  63 
  64 
  65 def protect(node):
  66     rawtext = get_text(node).strip()
  67     recased = None
  68     if rawtext.lower() in special_titles:
  69         recased = special_titles[rawtext.lower()]
  70     else:
  71         text = tokenize(rawtext)
  72         fixed = fixedcase_title(
  73             text,
  74             truelist=truelist,
  75             phrase_truelist=phrase_truelist,
  76             amodifiers=amodifiers,
  77             ndescriptors=ndescriptors,
  78         )
  79         if any(fixed):
  80             # Generate the recased string so we know where to look in the XML
  81             # to apply fixed-case
  82             recasedtoks = [(w if b else w.lower()) for w, b in zip(text, fixed)]
  83             recased = TreebankWordDetokenizer().detokenize(recasedtoks)
  84             # PTB (de)tokenizer doesn't think of hyphens as separate tokens,
  85             # so we need to manually detokenize them.
  86             # Assuming the only edits that need to be made are adding/deleting
  87             # spaces, the following will work:
  88             i = 0
  89             while i < len(rawtext):
  90                 # scan rawtext from left to right and adjust recased by adding/removing
  91                 # spaces until it matches
  92                 t = rawtext[i]
  93                 assert i < len(recased), ((i, t), rawtext, recased)
  94                 c = recased[i]
  95                 if t.isspace() and not c.isspace():  # may be ' ' or '\n'
  96                     # add space to recased
  97                     recased = recased[:i] + t + recased[i:]
  98                     i += 1
  99                 elif c.isspace() and not t.isspace():
  100                     # remove space from recased
  101                     recased = recased[:i] + recased[i + 1 :]
  102                     # don't increment i
  103                 elif t != c and t.isspace() and c.isspace():
  104                     recased = recased[:i] + t + recased[i + 1 :]
  105                     i += 1
  106                 else:
  107                     assert t == c or t.lower() == c.lower(), (
  108                         (i, t, c),
  109                         rawtext,
  110                         recased,
  111                         text,
  112                     )
  113                     i += 1
  114             if len(recased) > len(rawtext):
  115                 recased = recased[: len(rawtext)]
  116             assert rawtext.lower() == recased.lower(), (rawtext, recased)
  117 
  118     if recased:
  119         newnode = protect_recurse(node, recased)
  120         newnode.tail = node.tail  # tail of top level is not protected
  121         replace_node(node, newnode)
  122 
  123 
  124 # Read in the truelist (list of words that should always be protected)
  125 truelist, phrase_truelist, special_titles, amodifiers, ndescriptors = load_lists()
  126 
  127 if __name__ == "__main__":
  128     infile, outfile = sys.argv[1:]
  129 
  130     tree = ET.parse(infile)
  131     if not tree.getroot().tail:
  132         tree.getroot().tail = "\n"
  133     for paper in tree.getroot().findall(".//paper"):
  134         for title in paper.xpath("./title|./booktitle"):
  135             protect(title)
  136     tree.write(outfile, encoding="UTF-8", xml_declaration=True)
  137
  [end of bin/fixedcase/protect.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/bin/fixedcase/protect.py b/bin/fixedcase/protect.py
  --- a/bin/fixedcase/protect.py
  +++ b/bin/fixedcase/protect.py
  @@ -24,7 +24,7 @@
   # recursive helper called by protect
   # protect text of "node", including children, and tails of children
   def protect_recurse(node, recased):
  -    if node.tag == "fixed-case":  # already protected
  +    if node.tag in ("fixed-case", "tex-math"):  # already protected text, or math
           newnode = copy.deepcopy(node)  # don't need to modify descendents
           newnode.tail = None  # tail will be protected by caller
           return newnode
  
verification_info: {"golden_diff": "diff --git a/bin/fixedcase/protect.py b/bin/fixedcase/protect.py\n--- a/bin/fixedcase/protect.py\n+++ b/bin/fixedcase/protect.py\n@@ -24,7 +24,7 @@\n # recursive helper called by protect\n # protect text of \"node\", including children, and tails of children\n def protect_recurse(node, recased):\n-    if node.tag == \"fixed-case\":  # already protected\n+    if node.tag in (\"fixed-case\", \"tex-math\"):  # already protected text, or math\n         newnode = copy.deepcopy(node)  # don't need to modify descendents\n         newnode.tail = None  # tail will be protected by caller\n         return newnode\n", "issue": "Recaser bug: adding fixed-case inside tex-math markup\n`<tex-math><fixed-case>O</fixed-case>(<fixed-case>M</fixed-case>(n^2))</tex-math>` caused the build to fail in #892\n", "before_files": [{"content": "#!/usr/bin/env python3\n\n# protect.py <infile> <outfile>\n# looks for file \"truelist\" in current dir\n\n# cd data/xml\n# for i in *xml ; do (cd ../../tools/fixedcase/ ; python3 ./protect.py ../../data/xml/$i /tmp/$i ; echo $i ); done > log\n\n\nimport lxml.etree as ET\nimport os\nimport sys\nimport copy\nimport itertools\nimport inspect\n\nfrom collections import defaultdict\n\nif __name__ == \"__main__\":\n    from common import *\nelse:\n    from .common import *\n\n# recursive helper called by protect\n# protect text of \"node\", including children, and tails of children\ndef protect_recurse(node, recased):\n    if node.tag == \"fixed-case\":  # already protected\n        newnode = copy.deepcopy(node)  # don't need to modify descendents\n        newnode.tail = None  # tail will be protected by caller\n        return newnode\n    newnode = ET.Element(node.tag, node.attrib)\n\n    def process(text, rc):\n        i = 0\n        for upper, chars in itertools.groupby(rc[: len(text)], lambda c: c.isupper()):\n            charstr = \"\".join(chars)\n            if upper:\n                p = ET.Element(\"fixed-case\")\n                p.text = charstr\n                newnode.append(p)\n            else:\n                append_text(newnode, text[i : i + len(charstr)])\n\n            assert text[i : i + len(charstr)].lower() == charstr.lower(), (\n                i,\n                text,\n                charstr,\n            )\n            i += len(charstr)\n\n    if node.text:\n        process(node.text, recased)\n        recased = recased[len(node.text) :]\n    for child in node:\n        protected_child = protect_recurse(child, recased)\n        recased = recased[len(get_text(protected_child)) :]\n        newnode.append(protected_child)\n        if child.tail:\n            process(child.tail, recased)\n            recased = recased[len(child.tail) :]\n\n    return newnode\n\n\ndef protect(node):\n    rawtext = get_text(node).strip()\n    recased = None\n    if rawtext.lower() in special_titles:\n        recased = special_titles[rawtext.lower()]\n    else:\n        text = tokenize(rawtext)\n        fixed = fixedcase_title(\n            text,\n            truelist=truelist,\n            phrase_truelist=phrase_truelist,\n            amodifiers=amodifiers,\n            ndescriptors=ndescriptors,\n        )\n        if any(fixed):\n            # Generate the recased string so we know where to look in the XML\n            # to apply fixed-case\n            recasedtoks = [(w if b else w.lower()) for w, b in zip(text, fixed)]\n            recased = TreebankWordDetokenizer().detokenize(recasedtoks)\n            # PTB (de)tokenizer doesn't think of hyphens as separate tokens,\n            # so we need to manually detokenize them.\n            # Assuming the only edits that need to be made are adding/deleting\n            # spaces, the following will work:\n            i = 0\n            while i < len(rawtext):\n                # scan rawtext from left to right and adjust recased by adding/removing\n                # spaces until it matches\n                t = rawtext[i]\n                assert i < len(recased), ((i, t), rawtext, recased)\n                c = recased[i]\n                if t.isspace() and not c.isspace():  # may be ' ' or '\\n'\n                    # add space to recased\n                    recased = recased[:i] + t + recased[i:]\n                    i += 1\n                elif c.isspace() and not t.isspace():\n                    # remove space from recased\n                    recased = recased[:i] + recased[i + 1 :]\n                    # don't increment i\n                elif t != c and t.isspace() and c.isspace():\n                    recased = recased[:i] + t + recased[i + 1 :]\n                    i += 1\n                else:\n                    assert t == c or t.lower() == c.lower(), (\n                        (i, t, c),\n                        rawtext,\n                        recased,\n                        text,\n                    )\n                    i += 1\n            if len(recased) > len(rawtext):\n                recased = recased[: len(rawtext)]\n            assert rawtext.lower() == recased.lower(), (rawtext, recased)\n\n    if recased:\n        newnode = protect_recurse(node, recased)\n        newnode.tail = node.tail  # tail of top level is not protected\n        replace_node(node, newnode)\n\n\n# Read in the truelist (list of words that should always be protected)\ntruelist, phrase_truelist, special_titles, amodifiers, ndescriptors = load_lists()\n\nif __name__ == \"__main__\":\n    infile, outfile = sys.argv[1:]\n\n    tree = ET.parse(infile)\n    if not tree.getroot().tail:\n        tree.getroot().tail = \"\\n\"\n    for paper in tree.getroot().findall(\".//paper\"):\n        for title in paper.xpath(\"./title|./booktitle\"):\n            protect(title)\n    tree.write(outfile, encoding=\"UTF-8\", xml_declaration=True)\n", "path": "bin/fixedcase/protect.py"}]}