Blob Blame History Raw
From 3c57c9c2482c39c9f5f18f41bda93090d2fc9a17 Mon Sep 17 00:00:00 2001
From: Jeremy Tan <jtanx@outlook.com>
Date: Tue, 4 Oct 2016 20:42:15 +0800
Subject: [PATCH] Python3 scripting: Don't call RegisterAllPyModules from
 imported modules

This calls PyImport_AppendInittab. The behaviour has changed between
Python 2 and 3. In 3, you cannot call this function anymore after
Py_Initialize has been called. Meaning since the module has already
been imported, Py_Initialize must have already been called before.

This change means that FFPY_PYTHON_ENTRY_FUNCTION will now be called
more than once (once for each module imported) on Python 3, so ensure
that the initialisation code only runs once.

Fixes #2886. See also #1731.
---
 fontforge/python.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/fontforge/python.c b/fontforge/python.c
index 538b907..238f0dd 100644
--- a/fontforge/python.c
+++ b/fontforge/python.c
@@ -18893,11 +18893,18 @@ return;
 ** function.
 */
 PyMODINIT_FUNC FFPY_PYTHON_ENTRY_FUNCTION(const char* modulename) {
-    doinitFontForgeMain();
-    no_windowing_ui = running_script = true;
+    static int initted = false;
 
-    RegisterAllPyModules();
-    CreateAllPyModules();
+    if (!initted) {
+        doinitFontForgeMain();
+        no_windowing_ui = running_script = true;
+
+#if PY_MAJOR_VERSION <= 2
+        RegisterAllPyModules();
+#endif
+        CreateAllPyModules();
+        initted = true;
+    }
 
 #if PY_MAJOR_VERSION >= 3
     /* Python 3 expects the module object to be returned */