File 0001-Add-support-for-Python-3.patch of Package kvirc

From dbe8ef6dad916124c3714abc469403ed9991261c Mon Sep 17 00:00:00 2001
From: wodim <neikokz@gmail.com>
Date: Sat, 26 Aug 2017 15:02:56 +0200
Subject: [PATCH] Add support for Python 3

---
 CMakeLists.txt                               |  7 ++----
 src/modules/python/libkvipython.cpp          | 26 ++++++++++++++++++++
 src/modules/pythoncore/kvircmodule.cpp       |  2 +-
 src/modules/pythoncore/pythonheaderwrapper.h |  6 +++++
 4 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 57df293..1e07dd1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -67,9 +67,6 @@ set(CMAKE_KVIRC_BUILD_CPU ${CMAKE_SYSTEM_PROCESSOR})
 set(CMAKE_KVIRC_BUILD_COMPILER ${CMAKE_CXX_COMPILER})
 set(CMAKE_KVIRC_BUILD_COMPILER_FLAGS ${CMAKE_CXX_FLAGS})
 
-# Prefer Python 2.7 over 3.x (which is currently incompatible) - GitHub issue #2020
-set(Python_ADDITIONAL_VERSIONS "2.7")
-
 # Suffix for GNU/Linux
 set(LIB_SUFFIX
 	CACHE STRING "Define suffix of directory name (32/64)"
@@ -751,10 +748,10 @@ endif()
 # Check for Python support
 option(WANT_PYTHON "Compile Python support" ON)
 if(WANT_PYTHON)
-	find_package(PythonLibs 2.7)
+	find_package(PythonLibs)
 	if(PYTHONLIBS_FOUND)
 		set(COMPILE_PYTHON_SUPPORT 1)
-		set(CMAKE_STATUS_PYTHON_SUPPORT "Yes")
+		set(CMAKE_STATUS_PYTHON_SUPPORT "Yes, Python ${PYTHONLIBS_VERSION_STRING}")
 		list(APPEND LIBS ${PYTHON_LIBRARIES})
 		include_directories(${PYTHON_INCLUDE_DIRS})
 	else()
diff --git a/src/modules/python/libkvipython.cpp b/src/modules/python/libkvipython.cpp
index 6bdd56a80..700e8939b 100644
--- a/src/modules/python/libkvipython.cpp
+++ b/src/modules/python/libkvipython.cpp
@@ -502,6 +502,31 @@ static bool python_kvs_fnc_isAvailable(KviKvsModuleFunctionCall * c)
 	return true;
 }
 
+/*
+	@doc: python.version
+	@type:
+		function
+	@title:
+		$python.version
+	@short:
+		Check which version of Python is supported in this build of KVIrc
+	@syntax:
+		$python.version
+	@description:
+		Returns which major version of Python is KVIrc linked to ([b]2[/b] or [b]3[/b])
+		or [b]0[/b] if Python is not supported at all.
+*/
+
+static bool python_kvs_fnc_version(KviKvsModuleFunctionCall * c)
+{
+#ifdef COMPILE_PYTHON_SUPPORT
+	c->returnValue()->setInteger(PY_MAJOR_VERSION);
+#else
+	c->returnValue()->setBoolean(false);
+#endif
+	return true;
+}
+
 static bool python_module_init(KviModule * m)
 {
 	// register the command anyway
@@ -509,6 +534,7 @@ static bool python_module_init(KviModule * m)
 	KVSM_REGISTER_SIMPLE_COMMAND(m, "destroy", python_kvs_cmd_destroy);
 
 	KVSM_REGISTER_FUNCTION(m, "isAvailable", python_kvs_fnc_isAvailable);
+	KVSM_REGISTER_FUNCTION(m, "version", python_kvs_fnc_version);
 #ifdef COMPILE_PYTHON_SUPPORT
 	g_pPythonCoreModule = g_pModuleManager->getModule("pythoncore");
 #endif
diff --git a/src/modules/pythoncore/kvircmodule.cpp b/src/modules/pythoncore/kvircmodule.cpp
index 8937b6a63..659345d7e 100644
--- a/src/modules/pythoncore/kvircmodule.cpp
+++ b/src/modules/pythoncore/kvircmodule.cpp
@@ -423,7 +423,7 @@ PyMODINIT_FUNC python_init()
 	else
 	{
 		// Create a CObject containing the API pointer array's address
-		PyObject * pC_API_Object = PyCObject_FromVoidPtr(PyKVIrc_API, nullptr);
+		PyObject * pC_API_Object = PyCapsule_New((void *)PyKVIrc_API, "kvirc._C_API", nullptr);
 		if(pC_API_Object)
 			PyModule_AddObject(pModule, "_C_API", pC_API_Object);
 	}
diff --git a/src/modules/pythoncore/pythonheaderwrapper.h b/src/modules/pythoncore/pythonheaderwrapper.h
index 47f60d361..2b34066c3 100644
--- a/src/modules/pythoncore/pythonheaderwrapper.h
+++ b/src/modules/pythoncore/pythonheaderwrapper.h
@@ -1,6 +1,12 @@
 #ifndef _PYTHONHEADERWRAPPER_H_
 #define _PYTHONHEADERWRAPPER_H_
 
+// As of Python 3, something inside <Python.h> defines a struct with a member
+// called "slots" which conflicts with the builtin Qt keyword. But since we
+// include stuff from KVIrc itself back into the python module, we can't just
+// use QT_NO_KEYWORDS.
+#undef slots
+
 // See http://stackoverflow.com/questions/16200997/why-doesnt-include-python-h-work and http://stackoverflow.com/questions/19716859/puzzling-dependency-of-boost-python-1-54-debug-build-to-python27-lib-on-window
 
 #if defined(_DEBUG) && defined(_MSC_VER)
-- 
2.25.0
openSUSE Build Service is sponsored by