Should ast wait for stdin if we didn't pass anthing in `python3 -m ast`

why should it wait?

▶ ./python -m ast           
Namespace(infile=<_io.BufferedReader name='<stdin>'>, mode='exec', no_type_comments=True, include_attributes=False, indent=3)
<_io.BufferedReader name='<stdin>'>

it’s stuck here, I guess we can wait for user input and can signal(signal of pressing enter key) the script to continue but should we make it stuck? is this an intended behavior?

What’s the problem here? Reading stdin when called without argument is pretty common for shell utilities in general.

like they keep on waiting for input?, how do they know that the user has entered the input, and I need to process the input now?

Whenever something (Python, cat, grep, whatever) is waiting on stdin, you can press Control-D to terminate the input.

but they didn’t hang right they generate --help message

▶ grep
Usage: grep [OPTION]... PATTERNS [FILE]...
Try 'grep --help' for more information.

Try grep a (grep is called as grep pattern file, grep pattern file1 file2 ... or grep pattern, the latter reads input to search on stdin).

Or just cat.

got you, I should have posted this to help, I was not aware of the intended use-case of running ast as a script, and tools like these work like this only.

(I’ve moved the topic to Python Help.)

1 Like

Hi, can you move back to the idea I want to hear others views as well?

▶ grep "fike"
sadk
asd
sad
sads
fike
**fike**
as
dsa
dasd

here they are constantly looking at the input and processing it ,same for cat

▶ cat 
g
**g**
h
**h**

ast should at least generate a --help message or process the stdin after enter is pressed

So you want python -m ast to emit output on the fly while reading the input? That’s not possible: grep for example can compute part of the output with only part of the input (just search for the pattern in that part of the input), but python -m ast needs a full module to process. Just look at

$ python -m ast
2+2
3+3
Module(
   body=[
      Expr(
         value=BinOp(
            left=Constant(value=2),
            op=Add(),
            right=Constant(value=2))),
      Expr(
         value=BinOp(
            left=Constant(value=3),
            op=Add(),
            right=Constant(value=3)))],
   type_ignores=[])

which is not the concatenation of

$ python -m ast
2+2
Module(
   body=[
      Expr(
         value=BinOp(
            left=Constant(value=2),
            op=Add(),
            right=Constant(value=2)))],
   type_ignores=[])

and

$ python -m ast
3+3
Module(
   body=[
      Expr(
         value=BinOp(
            left=Constant(value=3),
            op=Add(),
            right=Constant(value=3)))],
   type_ignores=[])

Such cases are also common (sort, gzip -f, git apply, pandoc --from markdown --to html come to mind).

2 Likes

Thanks, I think I got it now.

1 Like