[gobject-introspection: 1/2] gi_source_scanner_parse_file: use a filename instead of a FILE struct



commit 953b45b14e2749be239ff3fdb0040998106d6758
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Thu Dec 6 09:01:02 2018 +0100

    gi_source_scanner_parse_file: use a filename instead of a FILE struct
    
    This allows us to get rid of the msvc hacks which are needed in case Python
    is built with a different msvc than g-i. By passing a filename the FILE struct never
    passes over library boundaries.

 giscanner/giscannermodule.c | 106 ++------------------------------------------
 giscanner/scannerparser.y   |  31 +++++++++----
 giscanner/sourcescanner.h   |   2 +-
 giscanner/sourcescanner.py  |   5 +--
 4 files changed, 27 insertions(+), 117 deletions(-)
---
diff --git a/giscanner/giscannermodule.c b/giscanner/giscannermodule.c
index c37357bd..45701d17 100644
--- a/giscanner/giscannermodule.c
+++ b/giscanner/giscannermodule.c
@@ -25,14 +25,6 @@
 #include <Python.h>
 #include "sourcescanner.h"
 
-#ifdef _WIN32
-#include <fcntl.h>
-#include <io.h>
-#define WIN32_LEAN_AND_MEAN
-#define STRICT
-#include <windows.h>
-#endif
-
 #include <glib-object.h>
 
 #ifndef Py_TYPE
@@ -441,104 +433,12 @@ static PyObject *
 pygi_source_scanner_parse_file (PyGISourceScanner *self,
                                PyObject          *args)
 {
-  int fd;
-  FILE *fp;
+  char *filename;
 
-  if (!PyArg_ParseTuple (args, "i:SourceScanner.parse_file", &fd))
+  if (!PyArg_ParseTuple (args, "s:SourceScanner.parse_file", &filename))
     return NULL;
 
-#ifdef _WIN32
-  /* The file descriptor passed to us is from the C library Python
-   * uses. That is msvcr71.dll for Python 2.5 and msvcr90.dll for
-   * Python 2.6, 2.7, 3.2 etc; and msvcr100.dll for Python 3.3 and 3.4.
-   * Python 3.5 and later is built with Visual Studio 2015, which uses
-   * the universal CRT, so we need to deal with urtbase.dll here instead.
-   * This code, at least if compiled with mingw, uses
-   * msvcrt.dll, so we cannot use the file descriptor directly. So
-   * perform appropriate magic.
-   */
-
-  /* If we are using the following combinations,
-   * we can use the file descriptors directly
-   * (Not if a build using WDK is used):
-   * Python 2.6.x/2.7.x with Visual C++ 2008
-   * Python 3.1.x/3.2.x with Visual C++ 2008
-   * Python 3.3.x/3.4.x with Visual C++ 2010
-   */
-
-  /* XXX: Somehow we cannot use the FD directly on Python 3.5+ even when
-   *      using Visual Studio 2015, so we currently need to use _get_osfhandle() when
-   *      in all cases on Python 3.5+
-   */
-
-#if (defined(_MSC_VER) && !defined(USE_WIN_DDK))
-#if (PY_MAJOR_VERSION==2 && PY_MINOR_VERSION>=6 && (_MSC_VER >= 1500 && _MSC_VER < 1600))
-#define MSVC_USE_FD_DIRECTLY 1
-#elif (PY_MAJOR_VERSION==3 && PY_MINOR_VERSION<=2 && (_MSC_VER >= 1500 && _MSC_VER < 1600))
-#define MSVC_USE_FD_DIRECTLY 1
-#elif (PY_MAJOR_VERSION==3 && PY_MINOR_VERSION<=4 && (_MSC_VER >= 1600 && _MSC_VER < 1700))
-#define MSVC_USE_FD_DIRECTLY 1
-#endif
-#endif
-
-#if !defined(MSVC_USE_FD_DIRECTLY) && !defined(__MINGW64_VERSION_MAJOR)
-  {
-#if defined(PY_MAJOR_VERSION) && PY_MAJOR_VERSION==2 && PY_MINOR_VERSION==5
-#define PYTHON_MSVCRXX_DLL "msvcr71.dll"
-#elif defined(PY_MAJOR_VERSION) && PY_MAJOR_VERSION==2 && PY_MINOR_VERSION==7
-#define PYTHON_MSVCRXX_DLL "msvcr90.dll"
-#elif defined(PY_MAJOR_VERSION) && PY_MAJOR_VERSION==3 && PY_MINOR_VERSION<=2
-#define PYTHON_MSVCRXX_DLL "msvcr90.dll"
-#elif defined(PY_MAJOR_VERSION) && PY_MAJOR_VERSION==3 &&  PY_MINOR_VERSION<=4
-#define PYTHON_MSVCRXX_DLL "msvcr100.dll"
-#elif defined(PY_MAJOR_VERSION) && PY_MAJOR_VERSION==3 && PY_MINOR_VERSION>=5
-#define PYTHON_MSVCRXX_DLL "ucrtbase.dll"
-#else
-#error This Python version not handled
-#endif
-    HMODULE msvcrxx;
-    intptr_t (*p__get_osfhandle) (int);
-    HANDLE handle;
-
-    msvcrxx = GetModuleHandle (PYTHON_MSVCRXX_DLL);
-    if (!msvcrxx)
-    {
-      g_print ("No " PYTHON_MSVCRXX_DLL " loaded.\n");
-      return NULL;
-    }
-
-    p__get_osfhandle = (intptr_t (*) (int)) GetProcAddress (msvcrxx, "_get_osfhandle");
-    if (!p__get_osfhandle)
-    {
-      g_print ("No _get_osfhandle found in " PYTHON_MSVCRXX_DLL ".\n");
-      return NULL;
-    }
-
-    handle = (HANDLE) p__get_osfhandle (fd);
-    if (!p__get_osfhandle)
-    {
-      g_print ("Could not get OS handle from " PYTHON_MSVCRXX_DLL " fd.\n");
-      return NULL;
-    }
-
-    fd = _open_osfhandle ((intptr_t) handle, _O_RDONLY);
-    if (fd == -1)
-    {
-      g_print ("Could not open C fd from OS handle.\n");
-      return NULL;
-    }
-  }
-#endif
-#endif
-
-  fp = fdopen (fd, "r");
-  if (!fp)
-    {
-      PyErr_SetFromErrno (PyExc_OSError);
-      return NULL;
-    }
-
-  if (!gi_source_scanner_parse_file (self->scanner, fp))
+  if (!gi_source_scanner_parse_file (self->scanner, filename))
     {
       g_print ("Something went wrong during parsing.\n");
       return NULL;
diff --git a/giscanner/scannerparser.y b/giscanner/scannerparser.y
index 9b4e1368..bf7bb37f 100644
--- a/giscanner/scannerparser.y
+++ b/giscanner/scannerparser.y
@@ -1626,6 +1626,19 @@ read_identifier (FILE * f, int c, char **identifier)
   return c;
 }
 
+static gboolean
+parse_file (GISourceScanner *scanner, FILE *file)
+{
+  g_return_val_if_fail (file != NULL, FALSE);
+
+  lineno = 1;
+  yyin = file;
+  yyparse (scanner);
+  yyin = NULL;
+
+  return TRUE;
+}
+
 void
 gi_source_scanner_parse_macros (GISourceScanner *scanner, GList *filenames)
 {
@@ -1772,29 +1785,29 @@ gi_source_scanner_parse_macros (GISourceScanner *scanner, GList *filenames)
     }
 
   rewind (fmacros);
-  gi_source_scanner_parse_file (scanner, fmacros);
+  parse_file (scanner, fmacros);
   fclose (fmacros);
   g_unlink (tmp_name);
 }
 
 gboolean
-gi_source_scanner_parse_file (GISourceScanner *scanner, FILE *file)
+gi_source_scanner_parse_file (GISourceScanner *scanner, const gchar *filename)
 {
-  g_return_val_if_fail (file != NULL, FALSE);
+  FILE *file;
+  gboolean result;
 
-  lineno = 1;
-  yyin = file;
-  yyparse (scanner);
-  yyin = NULL;
+  file = g_fopen (filename, "r");
+  result = parse_file (scanner, file);
+  fclose (file);
 
-  return TRUE;
+  return result;
 }
 
 gboolean
 gi_source_scanner_lex_filename (GISourceScanner *scanner, const gchar *filename)
 {
   lineno = 1;
-  yyin = fopen (filename, "r");
+  yyin = g_fopen (filename, "r");
 
   while (yylex (scanner) != YYEOF)
     ;
diff --git a/giscanner/sourcescanner.h b/giscanner/sourcescanner.h
index c3e9c655..e9fa5421 100644
--- a/giscanner/sourcescanner.h
+++ b/giscanner/sourcescanner.h
@@ -155,7 +155,7 @@ GISourceScanner *   gi_source_scanner_new              (void);
 gboolean            gi_source_scanner_lex_filename     (GISourceScanner  *igenerator,
                                                        const gchar      *filename);
 gboolean            gi_source_scanner_parse_file       (GISourceScanner  *igenerator,
-                                                       FILE             *file);
+                                                       const gchar      *filename);
 void                gi_source_scanner_parse_macros     (GISourceScanner  *scanner,
                                                        GList            *filenames);
 void                gi_source_scanner_set_macro_scan   (GISourceScanner  *scanner,
diff --git a/giscanner/sourcescanner.py b/giscanner/sourcescanner.py
index aea05e65..d867a4e9 100644
--- a/giscanner/sourcescanner.py
+++ b/giscanner/sourcescanner.py
@@ -307,10 +307,7 @@ class SourceScanner(object):
                       self._cpp_options)
 
         os.unlink(tmp_name_cpp)
-        fp = open(tmpfile_output, 'r')
-
-        self._scanner.parse_file(fp.fileno())
-        fp.close()
+        self._scanner.parse_file(tmpfile_output)
         os.unlink(tmpfile_output)
 
     def _write_preprocess_src(self, fp, defines, undefs, filenames):


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