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

Piping to stdin sometimes does not work #113

Open
ddribin opened this issue Apr 10, 2023 · 2 comments
Open

Piping to stdin sometimes does not work #113

ddribin opened this issue Apr 10, 2023 · 2 comments

Comments

@ddribin
Copy link

ddribin commented Apr 10, 2023

Piping to stdin sometimes does not work. Let's take the httpbin.org example:

> cat helloip.tmpl
Your IP address is {{ origin }}.
> curl -s http://httpbin.org/ip | jinja2 helloip.tmpl
Your IP address is .

Yet, the curl command does output JSON:

> curl -s http://httpbin.org/ip
{
  "origin": "xx.xx.xx.127"
}

In fact saving this to a file and then using cat to pipe it to jinja2 does work:

> curl -s http://httpbin.org/ip > httpbin.json
> cat httpbin.json | jinja2 helloip.tmpl
Your IP address is xx.xx.xx.127.

Not sure why piping cat would be any different than piping curl.

I'm on macOS Ventura 13.3.1 (Intel) running Python installed from Homebrew and jinja2-cli installed into a venv.

> jinja2 --version
jinja2-cli v0.8.2
 - Jinja2 v3.1.2

> python --version
Python 3.11.3

> pip list
Package    Version
---------- -------
Jinja2     3.1.2
jinja2-cli 0.8.2
MarkupSafe 2.1.2
pip        23.0.1
setuptools 67.6.1

(Edited to remove my real IP address.)

@ddribin
Copy link
Author

ddribin commented Apr 10, 2023

Oddly using - as the input works:

> curl -s http://httpbin.org/ip | jinja2 helloip.tmpl
Your IP address is .
> curl -s http://httpbin.org/ip | jinja2 helloip.tmpl -
Your IP address is xx.xx.xx.127.

(Edited to remove my real IP address.)

@ece-mohammad
Copy link
Contributor

if data == "-" or (data == "" and is_fd_alive(sys.stdin)):

yeah, looks like is_fd_alive(sys.stdin) returns False because select() returns a empty lists [], [], []. I guess sys.stdin.isatty() should work here, but I'm not too sure.

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

2 participants