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

"ParseError: end of file reached" when using Textual in a pipe #3831

Closed
8cylinder opened this issue Dec 7, 2023 · 6 comments
Closed

"ParseError: end of file reached" when using Textual in a pipe #3831

8cylinder opened this issue Dec 7, 2023 · 6 comments

Comments

@8cylinder
Copy link

8cylinder commented Dec 7, 2023

When piping to a textual app, I get a ParseError: end of file reached error. There is also some random text at the beginning of the error msg (^[[?2026;4$y) and there is also some left on the command line after (2026;4$yec^C).

Everything works correctly if I don't pipe to the app.

Using this code will cause the error:

# test.py
from textual.app import App

class Test(App):
    pass

if __name__ == "__main__":
    app = Test()
    app.run()

Calling that file using any of these commands will cause the error:

echo 'x' | ./test.py
echo 'x' | python test.py
echo 'x' | textual run ./test.py

This also happens if I pipe to a demo app.

echo 'x' | textual borders

Textual Diagnostics

Versions

Name Value
Textual 0.41.0
Rich 13.6.0

Python

Name Value
Version 3.11.6
Implementation CPython
Compiler GCC 13.2.0
Executable /home/sm/projects/prism/prism/.venv/bin/python

Operating System

Name Value
System Linux
Release 6.5.0-13-generic
Version #13-Ubuntu SMP PREEMPT_DYNAMIC Fri Nov 3 12:16:05 UTC 2023

Terminal

Name Value
Terminal Application Unknown
TERM xterm-256color
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=153, height=90
legacy_windows False
min_width 1
max_width 153
is_terminal True
encoding utf-8
max_height 90
justify None
overflow None
no_wrap False
highlight None
markup None
height None

Here is a screenshot and copy/paste of the output:
Screenshot from 2023-12-07 15-01-01

/home/sm/projects/prism/prism/.venv/lib/python3.11/site-packages/textual/drivers/linux_driver.py
:243 in _run_input_thread                                                                       
                                                                                                
  240 │   │   an exception                                                                      
  241 │   │   """                                                                               
  242 │   │   try:                                                                              
❱ 243 │   │   │   self.run_input_thread()                                                       
  244 │   │   except BaseException as error:                                                    
  245 │   │   │   self._app.call_later(                                                         
  246 │   │   │   │   self._app.panic,                                                          
                                                                                                
/home/sm/projects/prism/prism/.venv/lib/python3.11/site-packages/textual/drivers/linux_driver.py
:279 in run_input_thread                                                                        
                                                                                                
  276 │   │   │   │   for _selector_key, mask in selector_events:                               
  277 │   │   │   │   │   if mask & EVENT_READ:                                                 
  278 │   │   │   │   │   │   unicode_data = decode(read(fileno, 1024))                         
❱ 279 │   │   │   │   │   │   for event in feed(unicode_data):                                  
  280 │   │   │   │   │   │   │   self.process_event(event)                                     
  281 │   │   finally:                                                                          
  282 │   │   │   selector.close()                                                              
                                                                                                
/home/sm/projects/prism/prism/.venv/lib/python3.11/site-packages/textual/_parser.py:81 in feed  
                                                                                                
   78 │   │   │   try:                                                                          
   79 │   │   │   │   self._gen.send(self._buffer.getvalue())                                   
   80 │   │   │   except StopIteration:                                                         
❱  81 │   │   │   │   raise ParseError("end of file reached") from None                         
   82 │   │   │   while self._tokens:                                                           
   83 │   │   │   │   yield self._tokens.popleft()                                              
   84                                                                                           
Copy link

github-actions bot commented Dec 7, 2023

We found the following entry in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

@davep
Copy link
Contributor

davep commented Dec 7, 2023

You may find this discussion helpful: #3818

@8cylinder
Copy link
Author

Ah, that's what's going on. Thanks.

Copy link

github-actions bot commented Dec 7, 2023

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

@8cylinder
Copy link
Author

8cylinder commented Dec 8, 2023

I'm adding this here for anyone else who gets this error.

Adding sys.stdin = open('/dev/tty', 'r') after reading stdin fixed it for me.

Discussed in #620 and #153.

@ValentijnvdBeek
Copy link

This work around was broken in 9a9b002, where the internal implementation switched from sys.stdin to sys.__stdin__. So starting from 0.52.0, you need to reopen sys.__stdin__

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

No branches or pull requests

3 participants