为什么我的生成器挂起而不是抛出异常?

ber*_*uic 1 python python-2.7

我有一个生成器,通过过滤器返回来自多个文件的行.它看起来像这样:

def line_generator(self):
    # Find the relevant files
    files = self.get_files()

    # Read lines
    input_object = fileinput.input(files)
    for line in input_object:

        # Apply filter and yield if it is not *None*
        filtered = self.__line_filter(input_object.filename(), line)
        if filtered is not None:
            yield filtered

    input_object.close()
Run Code Online (Sandbox Code Playgroud)

该方法self.get_files()返回文件路径列表或空列表.我试过做s = fileinput.input([]),然后打电话s.next().这就是它挂起的地方,我无法理解为什么.我试图成为pythonic,而不是自己处理所有错误,但我想这是一个没有办法解决的问题.还是有吗?

不幸的是我现在无法在Linux上测试这个,但有人可以在Linux上尝试以下内容,并评论他们得到的内容吗?

import fileinput
s = fileinput.input([])
s.next()
Run Code Online (Sandbox Code Playgroud)

我在Windows上使用Python 2.7.5(64位).

总而言之,我真的很想知道:

这是Python中的错误,还是我做错了什么?不应该.next()总是返回一些东西,或者提出一个StopIteration

geo*_*org 5

fileinput 如果列表为空,则默认为stdin,因此它只是等待您键入内容.

一个明显的解决方法是摆脱fileinput(无论如何都不是非常有用)并且要明确,因为python zen暗示:

for path in self.get_files():
    with open(path) as fp:
      for line in fp:
         etc
Run Code Online (Sandbox Code Playgroud)