Skip to content
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

"ValueError: I/O operation on closed file." when using print() with file argument #5284

Open
Harding-Stardust opened this issue Feb 21, 2024 · 1 comment
Labels

Comments

@Harding-Stardust
Copy link

Harding-Stardust commented Feb 21, 2024

Steps to Reproduce

  1. Create bug_xonsh.py and paste the following code:
import datetime
import time
import sys

def now_nice_format(arg_utc: bool = False) -> str:
    """ Helper function for timestamped_line() """

    dt = datetime.datetime.now(datetime.UTC) if arg_utc else datetime.datetime.now()
    res = time.strftime("%Y-%m-%d %H:%M:%S", datetime.datetime.timetuple(dt))
    return res

def timestamped_line(arg_str: str = "") -> str:
    return f"[{now_nice_format()}] {arg_str}"

def timestamped_print(arg_str: str = "", arg_file = sys.stdout, arg_force_flush: bool = False):
    print(timestamped_line(arg_str), file=arg_file, flush=arg_force_flush) 

In xonsh, run the following code:

import bug_xonsh
bug_xonsh.timestamped_print("test")
xonsh: To log full traceback to a file set: $XONSH_TRACEBACK_LOGFILE = <filename>
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "P:\bug_xonsh.py", line 16, in timestamped_print
    print(timestamped_line(arg_str), file=arg_file, flush=arg_force_flush)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\Users\<username>\Appdata\Local\Programs\Python\Python312\Lib\site-packages\xonsh\base_shell.py", line 182, in write
    self.mem.write(s)
ValueError: I/O operation on closed file.

+------------------+---------------------------+
| xonsh            | 0.14.4                    |
| Python           | 3.12.1                    |
| PLY              | 3.11                      |
| have readline    | False                     |
| prompt toolkit   | 3.0.43                    |
| shell type       | prompt_toolkit            |
| history backend  | json                      |
| pygments         | 2.17.2                    |
| on posix         | False                     |
| on linux         | False                     |
| on darwin        | False                     |
| on windows       | True                      |
| on cygwin        | False                     |
| on msys2         | False                     |
| is superuser     | False                     |
| default encoding | utf-8                     |
| xonsh encoding   | utf-8                     |
| encoding errors  | surrogateescape           |
| xontrib 1        | coreutils                 |
| RC file 1        | C:\Users\<redacted>/.xonshrc |
+------------------+---------------------------+

For community

⬇️ Please click the 👍 reaction instead of leaving a +1 or 👍 comment

@anki-code
Copy link
Member

I can't repeat this in Python 3.11.6.
I can repeat this in xonsh 0.14.4.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants