udapi / udapi-python

Python framework for processing Universal Dependencies data
GNU General Public License v3.0
57 stars 31 forks source link

Output to Conll-u string #88

Closed francescomambrini closed 3 years ago

francescomambrini commented 3 years ago

I ran into the following error when I tried to use the to_conllu_string method of the Document class:

File "[...]/udapi/core/document.py", line 49, in to_conllu_string
    return fh.getvalue()
ValueError: I/O operation on closed file

What is happening is that, if we pass a io.StringIO as filehandle to a ConlluWriter (as in document.py, line 47), then the I/O object is closed in line 75 of basewriter.py:

    def after_process_document(self, document):
        sys.stdout.flush()
        if sys.stdout != self.orig_stdout:
-->        sys.stdout.close()
            sys.stdout = self.orig_stdout

So after that it's not possible to use getvalue() to get the content of the closed I/O stream.

One can always use the workaround of changing the sys.output in the code, using a ConlluWriter to write to the redirected stdout, instead of passing a filehandle to the writer. That works without a problem. But I think the use case of a writer method that saves the output to a variable is an interesting and useful one. However, I am afraid I can't suggest a fix, because I wouldn't dare to touch the BaseWriter class.

So I am just reporting the issue for the moment...

martinpopel commented 3 years ago

It seems you have an older version of udapi. The following code works with udapi==0.2.3:

import udapi
doc = udapi.Document("sample.conllu")
print(doc.to_conllu_string())
francescomambrini commented 3 years ago

Ooouch! I forgot to fetch a few changes to my branch... I confirm that it works as expected. Sorry for my untimely report.