Austin Mohr
Austin Mohr

Reputation: 170

SageMathCell ValueError When Printing String (Sometimes)

When I execute this SageMathCell with the specified parameters, sometimes it executes correctly and sometimes I get the following error:

ValueError                                Traceback (most recent call last)
<ipython-input-1-253427808e04> in <module>()
    117             show(plot(t,title=heading,vertex_labels=False,layout='tree',figsize=Integer(4)))
    118         else:
--> 119             print heading
    120         L = successor(L)
    121     else:

/home/sc_serv/sage/local/lib/python2.7/site-packages/ipykernel/iostream.py in write(self, string)
    315 
    316             is_child = (not self._is_master_process())
--> 317             self._buffer.write(string)
    318             if is_child:
    319                 # newlines imply flush in subprocesses

ValueError: I/O operation on closed file

The error always refers to line 119, which merely prints a string. This line occurs in a loop that is executed many times, and the particular iteration at which the error takes place seems to be completely random. Sometimes the loop nearly finishes, sometimes it crashes after only a few iterations. Even more strangely, if I set print_images to true (which causes heading to be printed as part of a figure rather than a simple string), the error never occurs. What is going on?

Upvotes: 1

Views: 187

Answers (2)

Andrey Novoseltsev
Andrey Novoseltsev

Reputation: 26

It turned out that the problem was fixed in the past, but came back due to current ipykernel master having version 4.3.0dev while the latest release is 4.3.1. I've adjusted installation command and can no longer reproduce the issue - the error line is now properly wrapped. Thanks for reporting!

Upvotes: 1

Austin Mohr
Austin Mohr

Reputation: 170

Here's a workaround: Rather than print the output at each iteration of the loop, store the entire output in a single string and print once at the end of execution. This can be accomplished by replacing all instances of

print heading

with

output = output + '\n' + heading

Upvotes: 0

Related Questions