在Python 3中将对象转换为迭代器?

时间:2015-01-08 23:36:44

标签: python python-3.x python-2.x

我试图将库移植到Python 3.它有一个用于PDF流的标记器。读者类在这些令牌上调用next()。这在Python 2中有效,但是当我在Python 3中运行时,我得到TypeError: 'PdfTokens' object is not an iterator

tokens.py关于迭代器的选择:

class PdfTokens(object):
    def __init__(self, fdata, startloc=0, strip_comments=True):
        self.fdata = fdata
        self.iterator = iterator = self._gettoks(startloc)
        self.next = next(iterator)

    def __iter__(self):
        return self.iterator

    def _gettoks(self, startloc, cacheobj=_cacheobj,
                       delimiters=delimiters, findtok=findtok, findparen=findparen,
                       PdfString=PdfString, PdfObject=PdfObject):
        fdata = self.fdata
        current = self.current = [(startloc, startloc)]
        namehandler = (cacheobj, self.fixname)
        cache = {}
        while 1:
            for match in findtok(fdata, current[0][1]):
                current[0] = tokspan = match.span()
                token = match.group(1)
                firstch = token[0]
                if firstch not in delimiters:
                    token = cacheobj(cache, token, PdfObject)
                elif firstch in '/<(%':
                    if firstch == '/':
                        # PDF Name
                        token = namehandler['#' in token](cache, token, PdfObject)
                    elif firstch == '<':
                        # << dict delim, or < hex string >
                        if token[1:2] != '<':
                            token = cacheobj(cache, token, PdfString)
                    elif firstch == '(':
                        ends = None  # For broken strings
                        if fdata[match.end(1)-1] != ')':
                            nest = 2
                            m_start, loc = tokspan
                            for match in findparen(fdata, loc):
                                loc = match.end(1)
                                ending = fdata[loc-1] == ')'
                                nest += 1 - ending * 2
                                if not nest:
                                    break
                                if ending and ends is None:
                                    ends = loc, match.end(), nest
                            token = fdata[m_start:loc]
                            current[0] = m_start, match.end()
                            if nest:
                                (self.error, self.exception)[not ends]('Unterminated literal string')
                                loc, ends, nest = ends
                                token = fdata[m_start:loc] + ')' * nest
                                current[0] = m_start, ends
                        token = cacheobj(cache, token, PdfString)
                    elif firstch == '%':
                        # Comment
                        if self.strip_comments:
                            continue
                    else:
                        self.exception('Tokenizer logic incorrect -- should never get here')

                yield token
                if current[0] is not tokspan:
                    break
            else:
                if self.strip_comments:
                    break
                raise StopIteration

pdfreader文件中违规方法的开头引发了错误:

def findxref(fdata):
    ''' Find the cross reference section at the end of a file
    '''
    startloc = fdata.rfind('startxref')
    if startloc < 0:
        raise PdfParseError('Did not find "startxref" at end of file')
    source = PdfTokens(fdata, startloc, False)
    tok = next(source)

我的印象是,定义自定义迭代器对象所需的只是.__iter__方法,.next()方法并引发StopIteration错误。这个类有所有这些东西,但它仍然会引发TypeError。

此外,这个库及其方法在Python 2.7中有效,并且已经停止在Python 3环境中工作。那么Python 3又有什么不同呢?我该怎么做才能使PdfTokens对象可迭代?

1 个答案:

答案 0 :(得分:2)

您无法直接在next的实例上调用PdfTokens,您需要首先通过调用iter()来获取其迭代器。这正是for循环所做的那样*,它首先在对象上调用iter()并获得一个迭代器,然后在该迭代器上调用循环__next__,直到它不是耗尽:

instance = PdfTokens(fdata, startloc, False)
source = iter(instance)
tok = next(source)

并非总是如此,如果在类上定义了__iter__,那么迭代器协议如果定义则会回退到__getitem__

相关问题