From 4c3a326c53c27f67f7ce8e1bbabc684c74a06634 Mon Sep 17 00:00:00 2001
From: Bram Moolenaar <Bram@vim.org>
Date: Sat, 24 Jul 2010 15:42:14 +0200
Subject: [PATCH] Temporary solution for crashing when using both :py and :py3:
 disallow both in one session.

---
 src/if_python.c          | 18 ++++++++++++++++++
 src/if_python3.c         | 22 ++++++++++++++++++++--
 src/proto/if_python.pro  |  1 +
 src/proto/if_python3.pro |  1 +
 4 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/src/if_python.c b/src/if_python.c
index 8185fc136f..95be357245 100644
--- a/src/if_python.c
+++ b/src/if_python.c
@@ -343,6 +343,16 @@ python_runtime_link_init(char *libname, int verbose)
 {
     int i;
 
+#if defined(UNIX) && defined(FEAT_PYTHON3)
+    /* Can't have Python and Python3 loaded at the same time, it may cause a
+     * crash. */
+    if (python3_loaded())
+    {
+	EMSG(_("E999: Python: Cannot use :py and :py3 in one session"));
+	return FAIL;
+    }
+#endif
+
     if (hinstPython)
 	return OK;
     hinstPython = load_dll(libname);
@@ -519,6 +529,14 @@ python_end()
     --recurse;
 }
 
+#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
+    int
+python_loaded()
+{
+    return (hinstPython != 0);
+}
+#endif
+
     static int
 Python_Init(void)
 {
diff --git a/src/if_python3.c b/src/if_python3.c
index 238f968b3f..8958e0ae29 100644
--- a/src/if_python3.c
+++ b/src/if_python3.c
@@ -306,7 +306,7 @@ static struct
  */
 static void end_dynamic_python3(void)
 {
-    if (hinstPy3)
+    if (hinstPy3 != 0)
     {
 	close_dll(hinstPy3);
 	hinstPy3 = 0;
@@ -323,7 +323,17 @@ static int py3_runtime_link_init(char *libname, int verbose)
     int i;
     void *ucs_from_string, *ucs_from_string_and_size;
 
-    if (hinstPy3)
+#if defined(UNIX) && defined(FEAT_PYTHON)
+    /* Can't have Python and Python3 loaded at the same time, it may cause a
+     * crash. */
+    if (python_loaded())
+    {
+	EMSG(_("E999: Python: Cannot use :py and :py3 in one session"));
+	return FAIL;
+    }
+#endif
+
+    if (hinstPy3 != 0)
 	return OK;
     hinstPy3 = load_dll(libname);
 
@@ -506,6 +516,14 @@ void python3_end()
     --recurse;
 }
 
+#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
+    int
+python3_loaded()
+{
+    return (hinstPy3 != 0);
+}
+#endif
+
 static int Python3_Init(void)
 {
     if (!py3initialised)
diff --git a/src/proto/if_python.pro b/src/proto/if_python.pro
index 793b6eeb5d..ec12402501 100644
--- a/src/proto/if_python.pro
+++ b/src/proto/if_python.pro
@@ -1,6 +1,7 @@
 /* if_python.c */
 int python_enabled __ARGS((int verbose));
 void python_end __ARGS((void));
+int python_loaded __ARGS((void));
 void ex_python __ARGS((exarg_T *eap));
 void ex_pyfile __ARGS((exarg_T *eap));
 void python_buffer_free __ARGS((buf_T *buf));
diff --git a/src/proto/if_python3.pro b/src/proto/if_python3.pro
index f9921e1c9a..9c132c3dce 100644
--- a/src/proto/if_python3.pro
+++ b/src/proto/if_python3.pro
@@ -1,6 +1,7 @@
 /* if_python3.c */
 int python3_enabled __ARGS((int verbose));
 void python3_end __ARGS((void));
+int python3_loaded __ARGS((void));
 void ex_py3 __ARGS((exarg_T *eap));
 void ex_py3file __ARGS((exarg_T *eap));
 void python3_buffer_free __ARGS((buf_T *buf));
-- 
GitLab