Repository: RustPython/RustPython (language: rust)

Issue:
Be silent even with no file using stdin
https://github.com/RustPython/RustPython/blob/3660ac20cff4152af3281040b5949eef9667493f/src/lib.rs#L187

```sh
echo -e "import json\njson.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])" | rustpython -q
```

### Actual

```
Welcome to the magnificent Rust Python 0.1.2 interpreter 😱 🖖
'["foo", {"bar": ["baz", null, 1.0, 2]}]'
```

### Expected

```
'["foo", {"bar": ["baz", null, 1.0, 2]}]'
```


Discussion / hints:
Thanks! Message also gets printed even if `-q` is not passed, i.e we don't check if `stdin` is from a pipe or connected to a terminal. The correct solution involves checking for `-q` and then also checking if `isatty(stdin)`. 

The relevant function in CPython is [`pymain_header`](https://github.com/python/cpython/blob/199507b81a302ea19f93593965b1e5088195a6c5/Modules/main.c#L185).

As a small related aside: this shouldn't actually print anything either way, [`PRINT_EXPR`](https://docs.python.org/3/library/dis.html#opcode-PRINT_EXPR) is used during interactive mode so this should only print if entered via `-i` or normally via `rustpython/python` (this should be a separate issue though since during ast traversal the opcode is injected I believe).

Fix patch:
diff --git a/Cargo.lock b/Cargo.lock
index fd5f36fe38..bfc1b62bad 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1897,6 +1897,7 @@ dependencies = [
 name = "rustpython"
 version = "0.1.2"
 dependencies = [
+ "atty",
  "cfg-if",
  "clap",
  "cpython",
diff --git a/Cargo.toml b/Cargo.toml
index 69337db4a0..774174f2bc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -48,6 +48,7 @@ flamescope = { version = "0.1.2", optional = true }
 libc = "0.2.133"
 log = "0.4.16"
 num-traits = "0.2.14"
+atty = "0.2.14"
 
 [target.'cfg(not(target_arch = "wasm32"))'.dependencies]
 rustyline = "10.0.0"
diff --git a/src/lib.rs b/src/lib.rs
index 4036f0922c..8603c55d81 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -50,6 +50,7 @@ mod interpreter;
 mod settings;
 mod shell;
 
+use atty::Stream;
 use rustpython_vm::{scope::Scope, PyResult, VirtualMachine};
 use std::{env, process::ExitCode};
 
@@ -64,6 +65,9 @@ pub fn run(init: impl FnOnce(&mut VirtualMachine) + 'static) -> ExitCode {
 
     let (settings, run_mode) = opts_with_clap();
 
+    // Be quiet if "quiet" arg is set OR stdin is not connected to a terminal
+    let quiet_var = settings.quiet || !atty::is(Stream::Stdin);
+
     // don't translate newlines (\r\n <=> \n)
     #[cfg(windows)]
     {
@@ -85,7 +89,7 @@ pub fn run(init: impl FnOnce(&mut VirtualMachine) + 'static) -> ExitCode {
     config = config.init_hook(Box::new(init));
 
     let interp = config.interpreter();
-    let exitcode = interp.run(move |vm| run_rustpython(vm, run_mode));
+    let exitcode = interp.run(move |vm| run_rustpython(vm, run_mode, quiet_var));
 
     ExitCode::from(exitcode)
 }
@@ -151,7 +155,7 @@ fn install_pip(_installer: &str, _scope: Scope, vm: &VirtualMachine) -> PyResult
     ))
 }
 
-fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> {
+fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode, quiet: bool) -> PyResult<()> {
     #[cfg(feature = "flame-it")]
     let main_guard = flame::start_guard("RustPython main");
 
@@ -182,7 +186,7 @@ fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> {
             if let Some(script) = script {
                 debug!("Running script {}", &script);
                 vm.run_script(scope.clone(), &script)?;
-            } else {
+            } else if !quiet {
                 println!(
                     "Welcome to the magnificent Rust Python {} interpreter \u{1f631} \u{1f596}",
                     crate_version!()


Test patch: