prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Cannot identify XBM file created with filename including underscore
  Pillow 2.8.1, Python 2.7.6 (Anaconda 2.2.0), Windows 7 64bit
  
  When I create git_hub.xbm (with ImageMagick), created file's header contains lines like this.
  
  ``` C
  #define git_hub_width 32
  #define git_hub_height 32
  ```
  
  In XbmImagePlugin.py, regular expression to extract XBM header doesn't match defined macro with more than two underscores like above.This causes an IOError.
  
  ``` python
  # XBM header
  xbm_head = re.compile(
      b"\s*#define[ \t]+[^_]*_width[ \t]+(?P<width>[0-9]+)[\r\n]+"
      b"#define[ \t]+[^_]*_height[ \t]+(?P<height>[0-9]+)[\r\n]+"
      b"(?P<hotspot>"
      b"#define[ \t]+[^_]*_x_hot[ \t]+(?P<xhot>[0-9]+)[\r\n]+"
      b"#define[ \t]+[^_]*_y_hot[ \t]+(?P<yhot>[0-9]+)[\r\n]+"
      b")?"
      b"[\\000-\\377]*_bits\\[\\]"
  )
  ```
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `PIL/XbmImagePlugin.py`
  Content:
  ```
  1 #
  2 # The Python Imaging Library.
  3 # $Id$
  4 #
  5 # XBM File handling
  6 #
  7 # History:
  8 # 1995-09-08 fl   Created
  9 # 1996-11-01 fl   Added save support
  10 # 1997-07-07 fl   Made header parser more tolerant
  11 # 1997-07-22 fl   Fixed yet another parser bug
  12 # 2001-02-17 fl   Use 're' instead of 'regex' (Python 2.1) (0.4)
  13 # 2001-05-13 fl   Added hotspot handling (based on code from Bernhard Herzog)
  14 # 2004-02-24 fl   Allow some whitespace before first #define
  15 #
  16 # Copyright (c) 1997-2004 by Secret Labs AB
  17 # Copyright (c) 1996-1997 by Fredrik Lundh
  18 #
  19 # See the README file for information on usage and redistribution.
  20 #
  21 
  22 __version__ = "0.6"
  23 
  24 import re
  25 from PIL import Image, ImageFile
  26 
  27 # XBM header
  28 xbm_head = re.compile(
  29     b"\s*#define[ \t]+[^_]*_width[ \t]+(?P<width>[0-9]+)[\r\n]+"
  30     b"#define[ \t]+[^_]*_height[ \t]+(?P<height>[0-9]+)[\r\n]+"
  31     b"(?P<hotspot>"
  32     b"#define[ \t]+[^_]*_x_hot[ \t]+(?P<xhot>[0-9]+)[\r\n]+"
  33     b"#define[ \t]+[^_]*_y_hot[ \t]+(?P<yhot>[0-9]+)[\r\n]+"
  34     b")?"
  35     b"[\\000-\\377]*_bits\\[\\]"
  36 )
  37 
  38 
  39 def _accept(prefix):
  40     return prefix.lstrip()[:7] == b"#define"
  41 
  42 
  43 ##
  44 # Image plugin for X11 bitmaps.
  45 
  46 class XbmImageFile(ImageFile.ImageFile):
  47 
  48     format = "XBM"
  49     format_description = "X11 Bitmap"
  50 
  51     def _open(self):
  52 
  53         m = xbm_head.match(self.fp.read(512))
  54 
  55         if m:
  56 
  57             xsize = int(m.group("width"))
  58             ysize = int(m.group("height"))
  59 
  60             if m.group("hotspot"):
  61                 self.info["hotspot"] = (
  62                     int(m.group("xhot")), int(m.group("yhot"))
  63                     )
  64 
  65             self.mode = "1"
  66             self.size = xsize, ysize
  67 
  68             self.tile = [("xbm", (0, 0)+self.size, m.end(), None)]
  69 
  70 
  71 def _save(im, fp, filename):
  72 
  73     if im.mode != "1":
  74         raise IOError("cannot write mode %s as XBM" % im.mode)
  75 
  76     fp.write(("#define im_width %d\n" % im.size[0]).encode('ascii'))
  77     fp.write(("#define im_height %d\n" % im.size[1]).encode('ascii'))
  78 
  79     hotspot = im.encoderinfo.get("hotspot")
  80     if hotspot:
  81         fp.write(("#define im_x_hot %d\n" % hotspot[0]).encode('ascii'))
  82         fp.write(("#define im_y_hot %d\n" % hotspot[1]).encode('ascii'))
  83 
  84     fp.write(b"static char im_bits[] = {\n")
  85 
  86     ImageFile._save(im, fp, [("xbm", (0, 0)+im.size, 0, None)])
  87 
  88     fp.write(b"};\n")
  89 
  90 
  91 Image.register_open("XBM", XbmImageFile, _accept)
  92 Image.register_save("XBM", _save)
  93 
  94 Image.register_extension("XBM", ".xbm")
  95 
  96 Image.register_mime("XBM", "image/xbm")
  97
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/PIL/XbmImagePlugin.py b/PIL/XbmImagePlugin.py
  --- a/PIL/XbmImagePlugin.py
  +++ b/PIL/XbmImagePlugin.py
  @@ -26,8 +26,8 @@
   
   # XBM header
   xbm_head = re.compile(
  -    b"\s*#define[ \t]+[^_]*_width[ \t]+(?P<width>[0-9]+)[\r\n]+"
  -    b"#define[ \t]+[^_]*_height[ \t]+(?P<height>[0-9]+)[\r\n]+"
  +    b"\s*#define[ \t]+.*_width[ \t]+(?P<width>[0-9]+)[\r\n]+"
  +    b"#define[ \t]+.*_height[ \t]+(?P<height>[0-9]+)[\r\n]+"
       b"(?P<hotspot>"
       b"#define[ \t]+[^_]*_x_hot[ \t]+(?P<xhot>[0-9]+)[\r\n]+"
       b"#define[ \t]+[^_]*_y_hot[ \t]+(?P<yhot>[0-9]+)[\r\n]+"
  
verification_info: {"golden_diff": "diff --git a/PIL/XbmImagePlugin.py b/PIL/XbmImagePlugin.py\n--- a/PIL/XbmImagePlugin.py\n+++ b/PIL/XbmImagePlugin.py\n@@ -26,8 +26,8 @@\n \n # XBM header\n xbm_head = re.compile(\n-    b\"\\s*#define[ \\t]+[^_]*_width[ \\t]+(?P<width>[0-9]+)[\\r\\n]+\"\n-    b\"#define[ \\t]+[^_]*_height[ \\t]+(?P<height>[0-9]+)[\\r\\n]+\"\n+    b\"\\s*#define[ \\t]+.*_width[ \\t]+(?P<width>[0-9]+)[\\r\\n]+\"\n+    b\"#define[ \\t]+.*_height[ \\t]+(?P<height>[0-9]+)[\\r\\n]+\"\n     b\"(?P<hotspot>\"\n     b\"#define[ \\t]+[^_]*_x_hot[ \\t]+(?P<xhot>[0-9]+)[\\r\\n]+\"\n     b\"#define[ \\t]+[^_]*_y_hot[ \\t]+(?P<yhot>[0-9]+)[\\r\\n]+\"\n", "issue": "Cannot identify XBM file created with filename including underscore\nPillow 2.8.1, Python 2.7.6 (Anaconda 2.2.0), Windows 7 64bit\n\nWhen I create git_hub.xbm (with ImageMagick), created file's header contains lines like this.\n\n``` C\n#define git_hub_width 32\n#define git_hub_height 32\n```\n\nIn XbmImagePlugin.py, regular expression to extract XBM header doesn't match defined macro with more than two underscores like above.This causes an IOError.\n\n``` python\n# XBM header\nxbm_head = re.compile(\n    b\"\\s*#define[ \\t]+[^_]*_width[ \\t]+(?P<width>[0-9]+)[\\r\\n]+\"\n    b\"#define[ \\t]+[^_]*_height[ \\t]+(?P<height>[0-9]+)[\\r\\n]+\"\n    b\"(?P<hotspot>\"\n    b\"#define[ \\t]+[^_]*_x_hot[ \\t]+(?P<xhot>[0-9]+)[\\r\\n]+\"\n    b\"#define[ \\t]+[^_]*_y_hot[ \\t]+(?P<yhot>[0-9]+)[\\r\\n]+\"\n    b\")?\"\n    b\"[\\\\000-\\\\377]*_bits\\\\[\\\\]\"\n)\n```\n\n", "before_files": [{"content": "#\n# The Python Imaging Library.\n# $Id$\n#\n# XBM File handling\n#\n# History:\n# 1995-09-08 fl   Created\n# 1996-11-01 fl   Added save support\n# 1997-07-07 fl   Made header parser more tolerant\n# 1997-07-22 fl   Fixed yet another parser bug\n# 2001-02-17 fl   Use 're' instead of 'regex' (Python 2.1) (0.4)\n# 2001-05-13 fl   Added hotspot handling (based on code from Bernhard Herzog)\n# 2004-02-24 fl   Allow some whitespace before first #define\n#\n# Copyright (c) 1997-2004 by Secret Labs AB\n# Copyright (c) 1996-1997 by Fredrik Lundh\n#\n# See the README file for information on usage and redistribution.\n#\n\n__version__ = \"0.6\"\n\nimport re\nfrom PIL import Image, ImageFile\n\n# XBM header\nxbm_head = re.compile(\n    b\"\\s*#define[ \\t]+[^_]*_width[ \\t]+(?P<width>[0-9]+)[\\r\\n]+\"\n    b\"#define[ \\t]+[^_]*_height[ \\t]+(?P<height>[0-9]+)[\\r\\n]+\"\n    b\"(?P<hotspot>\"\n    b\"#define[ \\t]+[^_]*_x_hot[ \\t]+(?P<xhot>[0-9]+)[\\r\\n]+\"\n    b\"#define[ \\t]+[^_]*_y_hot[ \\t]+(?P<yhot>[0-9]+)[\\r\\n]+\"\n    b\")?\"\n    b\"[\\\\000-\\\\377]*_bits\\\\[\\\\]\"\n)\n\n\ndef _accept(prefix):\n    return prefix.lstrip()[:7] == b\"#define\"\n\n\n##\n# Image plugin for X11 bitmaps.\n\nclass XbmImageFile(ImageFile.ImageFile):\n\n    format = \"XBM\"\n    format_description = \"X11 Bitmap\"\n\n    def _open(self):\n\n        m = xbm_head.match(self.fp.read(512))\n\n        if m:\n\n            xsize = int(m.group(\"width\"))\n            ysize = int(m.group(\"height\"))\n\n            if m.group(\"hotspot\"):\n                self.info[\"hotspot\"] = (\n                    int(m.group(\"xhot\")), int(m.group(\"yhot\"))\n                    )\n\n            self.mode = \"1\"\n            self.size = xsize, ysize\n\n            self.tile = [(\"xbm\", (0, 0)+self.size, m.end(), None)]\n\n\ndef _save(im, fp, filename):\n\n    if im.mode != \"1\":\n        raise IOError(\"cannot write mode %s as XBM\" % im.mode)\n\n    fp.write((\"#define im_width %d\\n\" % im.size[0]).encode('ascii'))\n    fp.write((\"#define im_height %d\\n\" % im.size[1]).encode('ascii'))\n\n    hotspot = im.encoderinfo.get(\"hotspot\")\n    if hotspot:\n        fp.write((\"#define im_x_hot %d\\n\" % hotspot[0]).encode('ascii'))\n        fp.write((\"#define im_y_hot %d\\n\" % hotspot[1]).encode('ascii'))\n\n    fp.write(b\"static char im_bits[] = {\\n\")\n\n    ImageFile._save(im, fp, [(\"xbm\", (0, 0)+im.size, 0, None)])\n\n    fp.write(b\"};\\n\")\n\n\nImage.register_open(\"XBM\", XbmImageFile, _accept)\nImage.register_save(\"XBM\", _save)\n\nImage.register_extension(\"XBM\", \".xbm\")\n\nImage.register_mime(\"XBM\", \"image/xbm\")\n", "path": "PIL/XbmImagePlugin.py"}], "after_files": [{"content": "#\n# The Python Imaging Library.\n# $Id$\n#\n# XBM File handling\n#\n# History:\n# 1995-09-08 fl   Created\n# 1996-11-01 fl   Added save support\n# 1997-07-07 fl   Made header parser more tolerant\n# 1997-07-22 fl   Fixed yet another parser bug\n# 2001-02-17 fl   Use 're' instead of 'regex' (Python 2.1) (0.4)\n# 2001-05-13 fl   Added hotspot handling (based on code from Bernhard Herzog)\n# 2004-02-24 fl   Allow some whitespace before first #define\n#\n# Copyright (c) 1997-2004 by Secret Labs AB\n# Copyright (c) 1996-1997 by Fredrik Lundh\n#\n# See the README file for information on usage and redistribution.\n#\n\n__version__ = \"0.6\"\n\nimport re\nfrom PIL import Image, ImageFile\n\n# XBM header\nxbm_head = re.compile(\n    b\"\\s*#define[ \\t]+.*_width[ \\t]+(?P<width>[0-9]+)[\\r\\n]+\"\n    b\"#define[ \\t]+.*_height[ \\t]+(?P<height>[0-9]+)[\\r\\n]+\"\n    b\"(?P<hotspot>\"\n    b\"#define[ \\t]+[^_]*_x_hot[ \\t]+(?P<xhot>[0-9]+)[\\r\\n]+\"\n    b\"#define[ \\t]+[^_]*_y_hot[ \\t]+(?P<yhot>[0-9]+)[\\r\\n]+\"\n    b\")?\"\n    b\"[\\\\000-\\\\377]*_bits\\\\[\\\\]\"\n)\n\n\ndef _accept(prefix):\n    return prefix.lstrip()[:7] == b\"#define\"\n\n\n##\n# Image plugin for X11 bitmaps.\n\nclass XbmImageFile(ImageFile.ImageFile):\n\n    format = \"XBM\"\n    format_description = \"X11 Bitmap\"\n\n    def _open(self):\n\n        m = xbm_head.match(self.fp.read(512))\n\n        if m:\n\n            xsize = int(m.group(\"width\"))\n            ysize = int(m.group(\"height\"))\n\n            if m.group(\"hotspot\"):\n                self.info[\"hotspot\"] = (\n                    int(m.group(\"xhot\")), int(m.group(\"yhot\"))\n                    )\n\n            self.mode = \"1\"\n            self.size = xsize, ysize\n\n            self.tile = [(\"xbm\", (0, 0)+self.size, m.end(), None)]\n\n\ndef _save(im, fp, filename):\n\n    if im.mode != \"1\":\n        raise IOError(\"cannot write mode %s as XBM\" % im.mode)\n\n    fp.write((\"#define im_width %d\\n\" % im.size[0]).encode('ascii'))\n    fp.write((\"#define im_height %d\\n\" % im.size[1]).encode('ascii'))\n\n    hotspot = im.encoderinfo.get(\"hotspot\")\n    if hotspot:\n        fp.write((\"#define im_x_hot %d\\n\" % hotspot[0]).encode('ascii'))\n        fp.write((\"#define im_y_hot %d\\n\" % hotspot[1]).encode('ascii'))\n\n    fp.write(b\"static char im_bits[] = {\\n\")\n\n    ImageFile._save(im, fp, [(\"xbm\", (0, 0)+im.size, 0, None)])\n\n    fp.write(b\"};\\n\")\n\n\nImage.register_open(\"XBM\", XbmImageFile, _accept)\nImage.register_save(\"XBM\", _save)\n\nImage.register_extension(\"XBM\", \".xbm\")\n\nImage.register_mime(\"XBM\", \"image/xbm\")\n", "path": "PIL/XbmImagePlugin.py"}]}