[libxml2] Fix libxml_PyFileGet with stdout on macOS



commit ce8f3d11957f44bc9bf901a3d7809cd5919caa7a
Author: Nick Wellnhofer <wellnhofer aevum de>
Date:   Mon Aug 29 23:53:40 2022 +0200

    Fix libxml_PyFileGet with stdout on macOS
    
    macOS returns O_RDWR for standard file descriptors, but fails to write
    to stdout or stderr when opened with fdopen(dup_fd, "rw").

 python/types.c | 60 ++++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 37 insertions(+), 23 deletions(-)
---
diff --git a/python/types.c b/python/types.c
index 27ad5008..8d690472 100644
--- a/python/types.c
+++ b/python/types.c
@@ -155,30 +155,44 @@ libxml_PyFileGet(PyObject *f) {
         return(NULL);
 #else
     /*
-     * Get the flags on the fd to understand how it was opened
+     * macOS returns O_RDWR for standard streams, but fails to write to
+     * stdout or stderr when opened with fdopen(dup_fd, "rw").
      */
-    flags = fcntl(fd, F_GETFL, 0);
-    switch (flags & O_ACCMODE) {
-        case O_RDWR:
-           if (flags & O_APPEND)
-               mode = "a+";
-           else
-               mode = "rw";
-           break;
-        case O_RDONLY:
-           if (flags & O_APPEND)
-               mode = "r+";
-           else
-               mode = "r";
-           break;
-       case O_WRONLY:
-           if (flags & O_APPEND)
-               mode = "a";
-           else
-               mode = "w";
-           break;
-       default:
-           return(NULL);
+    switch (fd) {
+        case STDIN_FILENO:
+            mode = "r";
+            break;
+        case STDOUT_FILENO:
+        case STDERR_FILENO:
+            mode = "w";
+            break;
+        default:
+            /*
+             * Get the flags on the fd to understand how it was opened
+             */
+            flags = fcntl(fd, F_GETFL, 0);
+            switch (flags & O_ACCMODE) {
+                case O_RDWR:
+                    if (flags & O_APPEND)
+                        mode = "a+";
+                    else
+                        mode = "rw";
+                    break;
+                case O_RDONLY:
+                    if (flags & O_APPEND)
+                        mode = "r+";
+                    else
+                        mode = "r";
+                    break;
+                case O_WRONLY:
+                    if (flags & O_APPEND)
+                        mode = "a";
+                    else
+                        mode = "w";
+                    break;
+                default:
+                    return(NULL);
+            }
     }
 #endif
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]