# Buggy code
def do_keyword(self, pos, token):
    if token is self.KEYWORD_BEGINCMAP:
        self._in_cmap = True
        self.popall()
        return
    elif token is self.KEYWORD_ENDCMAP:
        self._in_cmap = False
        return
    if not self._in_cmap:
        return
    #
    if token is self.KEYWORD_DEF:
        try:
            ((_, k), (_, v)) = self.pop(2)
            self.cmap.set_attr(literal_name(k), v)
        except PSSyntaxError:
            pass
        return

    if token is self.KEYWORD_USECMAP:
        try:
            ((_, cmapname),) = self.pop(1)
            self.cmap.use_cmap(CMapDB.get_cmap(literal_name(cmapname)))
        except PSSyntaxError:
            pass
        except CMapDB.CMapNotFound:
            pass
        return

    if token is self.KEYWORD_BEGINCODESPACERANGE:
        self.popall()
        return
    if token is self.KEYWORD_ENDCODESPACERANGE:
        self.popall()
        return

    if token is self.KEYWORD_BEGINCIDRANGE:
        self.popall()
        return
    if token is self.KEYWORD_ENDCIDRANGE:
        objs = [obj for (__, obj) in self.popall()]
        for s, e, cid in choplist(3, objs):
            if (
                not isinstance(s, str)
                or not isinstance(e, str)
                or not isinstance(cid, int)
                or len(s) != len(e)
            ):
                continue
            sprefix = s[:-4]
            eprefix = e[:-4]
            if sprefix != eprefix:
                continue
            svar = s[-4:]
            evar = e[-4:]
            s1 = nunpack(svar)
            e1 = nunpack(evar)
            vlen = len(svar)
            # assert s1 <= e1, str((s1, e1))
            for i in range(e1 - s1 + 1):
                x = sprefix + struct.pack(">L", s1 + i)[-vlen:]
                self.cmap.add_code2cid(x, cid + i)
        return

    if token is self.KEYWORD_BEGINCIDCHAR:
        self.popall()
        return
    if token is self.KEYWORD_ENDCIDCHAR:
        objs = [obj for (__, obj) in self.popall()]
        for cid, code in choplist(2, objs):
            if isinstance(code, str) and isinstance(cid, str):
                self.cmap.add_code2cid(code, nunpack(cid))
        return

    if token is self.KEYWORD_BEGINBFRANGE:
        self.popall()
        return
    if token is self.KEYWORD_ENDBFRANGE:
        objs = [obj for (__, obj) in self.popall()]
        for s, e, code in choplist(3, objs):
            if not isinstance(s, bytes) or not isinstance(e, bytes) or len(s) != len(e):
                continue
            s1 = nunpack(s)
            e1 = nunpack(e)
            # assert s1 <= e1, str((s1, e1))
            if isinstance(code, list):
                for i in range(e1 - s1 + 1):
                    self.cmap.add_cid2unichr(s1 + i, code[i])
            else:
                var = code[-4:]
                base = nunpack(var)
                prefix = code[:-4]
                vlen = len(var)
                for i in range(e1 - s1 + 1):
                    x = prefix + struct.pack(">L", base + i)[-vlen:]
                    self.cmap.add_cid2unichr(s1 + i, x)
        return

    if token is self.KEYWORD_BEGINBFCHAR:
        self.popall()
        return
    if token is self.KEYWORD_ENDBFCHAR:
        objs = [obj for (__, obj) in self.popall()]
        for cid, code in choplist(2, objs):
            if isinstance(cid, bytes) and isinstance(code, bytes):
                self.cmap.add_cid2unichr(nunpack(cid), code)
        return

    if token is self.KEYWORD_BEGINNOTDEFRANGE:
        self.popall()
        return
    if token is self.KEYWORD_ENDNOTDEFRANGE:
        self.popall()
        return

    self.push((pos, token))
    return


# Traceback (AttributeError)
Traceback (most recent call last):
File "/home/felix/anaconda3/bin/pdf2txt.py", line 136, in <module>
if __name__ == '__main__': sys.exit(main())
File "/home/felix/anaconda3/bin/pdf2txt.py", line 131, in main
outfp = extract_text(**vars(A))
File "/home/felix/anaconda3/bin/pdf2txt.py", line 63, in extract_text
pdfminer.high_level.extract_text_to_fp(fp, **locals())
File "/home/felix/anaconda3/lib/python3.6/site-packages/pdfminer/high_level.py", line 82, in extract_text_to_fp
interpreter.process_page(page)
File "/home/felix/anaconda3/lib/python3.6/site-packages/pdfminer/pdfinterp.py", line 852, in process_page
self.render_contents(page.resources, page.contents, ctm=ctm)
File "/home/felix/anaconda3/lib/python3.6/site-packages/pdfminer/pdfinterp.py", line 862, in render_contents
self.init_resources(resources)
File "/home/felix/anaconda3/lib/python3.6/site-packages/pdfminer/pdfinterp.py", line 362, in init_resources
self.fontmap[fontid] = self.rsrcmgr.get_font(objid, spec)
File "/home/felix/anaconda3/lib/python3.6/site-packages/pdfminer/pdfinterp.py", line 212, in get_font
font = self.get_font(None, subspec)
File "/home/felix/anaconda3/lib/python3.6/site-packages/pdfminer/pdfinterp.py", line 203, in get_font
font = PDFCIDFont(self, spec)
File "/home/felix/anaconda3/lib/python3.6/site-packages/pdfminer/pdffont.py", line 658, in __init__
self.cmap = CMapDB.get_cmap(name)
File "/home/felix/anaconda3/lib/python3.6/site-packages/pdfminer/cmapdb.py", line 259, in get_cmap
data = klass._load_data(name)
File "/home/felix/anaconda3/lib/python3.6/site-packages/pdfminer/cmapdb.py", line 233, in _load_data
name = name.replace("\0", "")
AttributeError: 'PDFStream' object has no attribute 'replace'

# Fixed code
def do_keyword(self, pos, token):
    if token is self.KEYWORD_BEGINCMAP:
        self._in_cmap = True
        self.popall()
        return
    elif token is self.KEYWORD_ENDCMAP:
        self._in_cmap = False
        return
    if not self._in_cmap:
        return
    #
    if token is self.KEYWORD_DEF:
        try:
            ((_, k), (_, v)) = self.pop(2)
            self.cmap.set_attr(literal_name(k), v)
        except PSSyntaxError:
            pass
        return

    if token is self.KEYWORD_USECMAP:
        try:
            ((_, cmapname),) = self.pop(1)
            self.cmap.use_cmap(CMapDB.get_cmap(literal_name(cmapname)))
        except PSSyntaxError:
            pass
        except CMapDB.CMapNotFound:
            pass
        return

    if token is self.KEYWORD_BEGINCODESPACERANGE:
        self.popall()
        return
    if token is self.KEYWORD_ENDCODESPACERANGE:
        self.popall()
        return

    if token is self.KEYWORD_BEGINCIDRANGE:
        self.popall()
        return
    if token is self.KEYWORD_ENDCIDRANGE:
        objs = [obj for (__, obj) in self.popall()]
        for s, e, cid in choplist(3, objs):
            if (
                not isinstance(s, str)
                or not isinstance(e, str)
                or not isinstance(cid, int)
                or len(s) != len(e)
            ):
                continue
            sprefix = s[:-4]
            eprefix = e[:-4]
            if sprefix != eprefix:
                continue
            svar = s[-4:]
            evar = e[-4:]
            s1 = nunpack(svar)
            e1 = nunpack(evar)
            vlen = len(svar)
            for i in range(e1 - s1 + 1):
                x = sprefix + struct.pack(">L", s1 + i)[-vlen:]
                self.cmap.add_code2cid(x, cid + i)
        return

    if token is self.KEYWORD_BEGINCIDCHAR:
        self.popall()
        return
    if token is self.KEYWORD_ENDCIDCHAR:
        objs = [obj for (__, obj) in self.popall()]
        for cid, code in choplist(2, objs):
            if isinstance(code, str) and isinstance(cid, str):
                self.cmap.add_code2cid(code, nunpack(cid))
        return

    if token is self.KEYWORD_BEGINBFRANGE:
        self.popall()
        return
    if token is self.KEYWORD_ENDBFRANGE:
        objs = [obj for (__, obj) in self.popall()]
        for s, e, code in choplist(3, objs):
            if not isinstance(s, bytes) or not isinstance(e, bytes) or len(s) != len(e):
                continue
            s1 = nunpack(s)
            e1 = nunpack(e)
            if isinstance(code, list):
                for i in range(e1 - s1 + 1):
                    self.cmap.add_cid2unichr(s1 + i, code[i])
            else:
                var = code[-4:]
                base = nunpack(var)
                prefix = code[:-4]
                vlen = len(var)
                for i in range(e1 - s1 + 1):
                    x = prefix + struct.pack(">L", base + i)[-vlen:]
                    self.cmap.add_cid2unichr(s1 + i, x)
        return

    if token is self.KEYWORD_BEGINBFCHAR:
        self.popall()
        return
    if token is self.KEYWORD_ENDBFCHAR:
        objs = [obj for (__, obj) in self.popall()]
        for cid, code in choplist(2, objs):
            if isinstance(cid, bytes) and isinstance(code, bytes):
                self.cmap.add_cid2unichr(nunpack(cid), code)
        return

    if token is self.KEYWORD_BEGINNOTDEFRANGE:
        self.popall()
        return
    if token is self.KEYWORD_ENDNOTDEFRANGE:
        self.popall()
        return

    self.push((pos, token))
    return