quentin-sommer opened a new issue, #50240:
URL: https://github.com/apache/airflow/issues/50240

   ### Apache Airflow version
   
   2.10.5
   
   ### If "Other Airflow 2 version" selected, which one?
   
   _No response_
   
   ### What happened?
   
   The logic for connections export in 2.10 
   
https://github.com/apache/airflow/blob/ea61ff7b6f8e7353704ca2f7aafd316ac5515336/airflow-core/src/airflow/cli/utils.py#L39C5-L49
   
   ```
   def is_stdout(fileio: IOBase) -> bool:
       return fileio.fileno() == sys.stdout.fileno()
   ```
   
   fails on my server because stdout doesn't have a fileno
   
   ```
       connections_export(args)
     File 
"/opt/python3.11/lib/python3.11/site-packages/airflow/utils/providers_configuration_loader.py",
 line 55, in wrapped_function
       return func(*args, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^^
     File 
"/opt/python3.11/lib/python3.11/site-packages/airflow/cli/commands/connection_command.py",
 line 176, in connections_export
       if is_stdout(f):
          ^^^^^^^^^^^^
     File "/opt/python3.11/lib/python3.11/site-packages/airflow/cli/utils.py", 
line 44, in is_stdout
       return fileio.fileno() == sys.stdout.fileno()
                                 ^^^^^^^^^^^^^^^^^^^
   io.UnsupportedOperation: fileno
   ```
   
   I was using 2.7.3 before and the implementation was
   
https://github.com/apache/airflow/blob/f1243537838516b8bb8156130bc001595bfbeb01/airflow/cli/commands/connection_command.py#L144-L145
   ```
   def _is_stdout(fileio: io.TextIOWrapper) -> bool:
       return fileio.name == "<stdout>"
   ```
   
   I am relying on this public method to export connections to the same way the 
CLI would, without having to spawn a process to actually run the export
   
   ### What you think should happen instead?
   
   _No response_
   
   ### How to reproduce
   
   I see the issue when running `from airflow.cli.commands.connection_command 
import connections_export` from a kubernetes container.
   Complete snippet to export to dict:
   ```
   def _get_airflow_connections() -> Dict[str, Any]:
       """
       Retrieve airflow connections using
       the same code as `airflow connections export`
       """
   
       class FakeFileStringIO(io.StringIO):
           """
           Airflow expects a real file. StringIO has the same interface
           but lacks the `name` attribute, so we patch it
           """
   
           def __init__(self):
               super().__init__()
               self.content = ""
   
           def write(self, s: str) -> int:
               self.content += s
               return len(s)
   
           def fileno(self):
               return None
   
           name = "FakeFileStringIO"
   
       class Args:
           """
           This class builds an object expected by `connections_export`
           with a fake file io that writes to a string stream.
           """
   
           def __init__(self, f):
               self.file = f
               self.format = None
               self.file_format = "json"
               self.serialization_format = None
   
       with FakeFileStringIO() as file:
           args = Args(file)
           connections_export(args)
           return json.loads(args.file.content)
   ```
   
   ### Operating System
   
   google cloud composer 2
   
   ### Versions of Apache Airflow Providers
   
   _No response_
   
   ### Deployment
   
   Google Cloud Composer
   
   ### Deployment details
   
   _No response_
   
   ### Anything else?
   
   _No response_
   
   ### Are you willing to submit PR?
   
   - [x] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to