This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new e033afab423 Make is_stdout work when sys.stdout does not have a fd 
(#50328)
e033afab423 is described below

commit e033afab4230720352a9f547e2b7490e83224881
Author: Quentin Sommer <[email protected]>
AuthorDate: Thu May 22 06:49:35 2025 -0400

    Make is_stdout work when sys.stdout does not have a fd (#50328)
    
    Co-authored-by: Jarek Potiuk <[email protected]>
---
 airflow-core/src/airflow/cli/utils.py              |  5 ++--
 .../unit/cli/commands/test_connection_command.py   | 33 ++++++++++++++++++++++
 2 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/airflow-core/src/airflow/cli/utils.py 
b/airflow-core/src/airflow/cli/utils.py
index 605244ee71e..275bfbb7b07 100644
--- a/airflow-core/src/airflow/cli/utils.py
+++ b/airflow-core/src/airflow/cli/utils.py
@@ -44,13 +44,12 @@ def is_stdout(fileio: IOBase) -> bool:
     with argparse.FileType points to stdout (by setting the path to ``-``). 
This
     is why there is no equivalent for stderr; argparse does not allow using it.
 
-    .. warning:: *fileio* must be open for this check to be successful.
     """
-    return fileio.fileno() == sys.stdout.fileno()
+    return fileio is sys.stdout
 
 
 def print_export_output(command_type: str, exported_items: Collection, file: 
TextIOWrapper):
-    if not file.closed and is_stdout(file):
+    if is_stdout(file):
         print(f"\n{len(exported_items)} {command_type} successfully 
exported.", file=sys.stderr)
     else:
         print(f"{len(exported_items)} {command_type} successfully exported to 
{file.name}.")
diff --git a/airflow-core/tests/unit/cli/commands/test_connection_command.py 
b/airflow-core/tests/unit/cli/commands/test_connection_command.py
index 8e92574b461..1ebb415aa0f 100644
--- a/airflow-core/tests/unit/cli/commands/test_connection_command.py
+++ b/airflow-core/tests/unit/cli/commands/test_connection_command.py
@@ -16,6 +16,7 @@
 # under the License.
 from __future__ import annotations
 
+import io
 import json
 import os
 import re
@@ -333,6 +334,38 @@ class TestCliExportConnections:
         }
         assert json.loads(output_filepath.read_text()) == expected_connections
 
+    def 
test_cli_connections_export_should_work_when_stdout_is_not_a_real_fd(self, 
tmp_path):
+        class FakeFileStringIO(io.StringIO):
+            """
+            Buffer the contents of a StringIO
+            to make them accessible after close
+            """
+
+            def __init__(self):
+                super().__init__()
+                self.content = ""
+
+            def write(self, s: str) -> int:
+                self.content += s
+                return len(s)
+
+        tmp_stdout = FakeFileStringIO()
+        with redirect_stdout(tmp_stdout):
+            args = self.parser.parse_args(
+                [
+                    "connections",
+                    "export",
+                    "--format",
+                    "env",
+                    "-",
+                ]
+            )
+            connection_command.connections_export(args)
+        assert tmp_stdout.content.splitlines() == [
+            "airflow_db=mysql://root:plainpassword@mysql/airflow",
+            
"druid_broker_default=druid://druid-broker:8082/?endpoint=druid%2Fv2%2Fsql",
+        ]
+
 
 TEST_URL = "postgresql://airflow:airflow@host:5432/airflow"
 TEST_JSON = json.dumps(

Reply via email to