-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tdl.parse() should allow a filename or open file as its argument #104
Comments
It seems we didn't test this adequately. Calling
The bug is a bit sneaky: def parse(f):
if hasattr(f, 'read'):
return _parse(f)
else:
with open(f) as fh:
return _parse(fh) The |
I've been having trouble recreating this error. The tdl file I have parses without errors with both python 2 and 3 as long as the comment strings are properly placed in the file. |
This is not a Python 2 vs 3 issue, as it affects both. If you just run >>> from delphin import tdl
>>> defs = tdl.parse('/home/goodmami/grammars/erg-trunk/fundamentals.tdl')
>>> defs # no error
<generator object _parse at 0x7fa4d3bc2af0>
>>> next(defs) # once you run the generator, you find the file is closed
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "delphin/tdl.py", line 465, in _parse
for line_no, event, data in lex(f):
File "delphin/tdl.py", line 393, in lex
lines = enumerate(stream)
ValueError: I/O operation on closed file Does the above code (up to |
Okay, now I'm getting the error! Thanks. From what I've been reading, it seems that the else:
fh = open(f, 'r')
return _parse(fh) |
The |
I should clarify: Python closes open files when there are no more references to the open file object. If you store that And were my hints above about turning |
I don't think I understand your last comment about also returning non-None values. My understanding at the moment is that the items from the |
Generators use >>> def gen1():
... yield 1
...
>>> def gen2():
... yield 1
... return # this is exactly equivalent to gen1()
... yield 2 # this value is never yielded
...
>>> def gen3():
... yield 1
... return None # this is also equivalent, but Python2 doesn't like it
...
File "<stdin>", line 3
SyntaxError: 'return' with argument inside generator In regular functions, reaching the end of the function without a >>> def gen4():
... yield 1
... return 1
...
>>> g = gen4()
>>> next(g)
1
>>> next(g)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration: 1 So look again at def parse(f):
if hasattr(f, 'read'):
return _parse(f)
else:
with open(f) as fh:
return _parse(fh) If you change that second
Yes. So the ...
for event in _parse(fh):
yield event If we drop Python 2 support (which I'm thinking we could do for the next release... I'm getting tired of supporting both), then there's a better form: ...
yield from _parse(fh) |
delphin.tdl.parse()
could be more user-friendly if can take a filename as its argument in addition to open file objects.Note that currently
tdl.parse()
can work with any argument that iterates lines:io.StringIO
-wrapped stringsI don't think the last one is really necessary, but if we do a check like the following, it will be excluded:
Alternatively the check could be for string types, e.g.,
Update:
tdl.lex()
for now.The text was updated successfully, but these errors were encountered: