]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr: be compatible with py3 20064/head
authorKefu Chai <kchai@redhat.com>
Tue, 23 Jan 2018 17:13:50 +0000 (01:13 +0800)
committerKefu Chai <kchai@redhat.com>
Fri, 26 Jan 2018 12:24:54 +0000 (20:24 +0800)
so ceph-mgr can be compiled using py3

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/mgr/BaseMgrStandbyModule.h
src/mgr/Mgr.h
src/mgr/PyFormatter.h
src/mgr/PyModule.cc
src/mgr/PyModuleRegistry.cc
src/mgr/PyModuleRunner.cc
src/mgr/PyOSDMap.h
src/mgr/PythonCompat.h [new file with mode: 0644]
src/mgr/StandbyPyModules.h

index c5c6beb1d3c91d35f6cb699532ba139f1e67f992..c4cf267304c94a212fc65f348f6cce6e98e76e15 100644 (file)
@@ -1,7 +1,7 @@
 
 #pragma once
 
-#include "Python.h"
+#include "PythonCompat.h"
 
 extern PyTypeObject BaseMgrStandbyModuleType;
 
index 912114c380b9746bec9ea75ec149f2d4af7c2e8e..173fe04a1d5426e1a7491a554140654c48d04375 100644 (file)
 #define CEPH_MGR_H_
 
 // Python.h comes first because otherwise it clobbers ceph's assert
-#include "Python.h"
-// Python's pyconfig-64.h conflicts with ceph's acconfig.h
-#undef HAVE_SYS_WAIT_H
-#undef HAVE_UNISTD_H
-#undef HAVE_UTIME_H
-#undef _POSIX_C_SOURCE
-#undef _XOPEN_SOURCE
+#include "PythonCompat.h"
 
 #include "mds/FSMap.h"
 #include "messages/MFSMap.h"
index ec4dad81c2901985a59070ae6916704591164b7d..e74f2fe7c49e0768dcb247e5ef827a62b0416cb8 100644 (file)
 #define PY_FORMATTER_H_
 
 // Python.h comes first because otherwise it clobbers ceph's assert
-#include "Python.h"
-// Python's pyconfig-64.h conflicts with ceph's acconfig.h
-#undef HAVE_SYS_WAIT_H
-#undef HAVE_UNISTD_H
-#undef HAVE_UTIME_H
-#undef _POSIX_C_SOURCE
-#undef _XOPEN_SOURCE
+#include "PythonCompat.h"
 
 #include <stack>
 #include <memory>
index d2d825ca0d5a9dcbe15f4124b77afb96214b41ff..f3fede8de6e50386d8c4301ce34f09337f173ca5 100644 (file)
@@ -72,6 +72,16 @@ namespace {
     {"flush", log_flush, METH_VARARGS, "flush"},
     {nullptr, nullptr, 0, nullptr}
   };
+
+#if PY_MAJOR_VERSION >= 3
+  static PyModuleDef ceph_logger_module = {
+    PyModuleDef_HEAD_INIT,
+    "ceph_logger",
+    nullptr,
+    -1,
+    log_methods,
+  };
+#endif
 }
 
 
@@ -159,34 +169,60 @@ int PyModule::load(PyThreadState *pMainThreadState)
       pMyThreadState.set(thread_state);
       // Some python modules do not cope with an unpopulated argv, so lets
       // fake one.  This step also picks up site-packages into sys.path.
+#if PY_MAJOR_VERSION >= 3
+      const wchar_t *argv[] = {L"ceph-mgr"};
+      PySys_SetArgv(1, (wchar_t**)argv);
+#else
       const char *argv[] = {"ceph-mgr"};
       PySys_SetArgv(1, (char**)argv);
+#endif
 
       if (g_conf->daemonize) {
-        auto py_logger = Py_InitModule("ceph_logger", log_methods);
 #if PY_MAJOR_VERSION >= 3
+        auto py_logger = PyModule_Create(&ceph_logger_module);
         PySys_SetObject("stderr", py_logger);
         PySys_SetObject("stdout", py_logger);
 #else
+        auto py_logger = Py_InitModule("ceph_logger", log_methods);
         PySys_SetObject(const_cast<char*>("stderr"), py_logger);
         PySys_SetObject(const_cast<char*>("stdout"), py_logger);
 #endif
       }
 
       // Configure sys.path to include mgr_module_path
-      std::string sys_path = std::string(Py_GetPath()) + ":" + get_site_packages()
-                             + ":" + g_conf->get_val<std::string>("mgr_module_path");
-      dout(10) << "Computed sys.path '" << sys_path << "'" << dendl;
-
+      string paths = (":" + get_site_packages() +
+                     ":" + g_conf->get_val<std::string>("mgr_module_path"));
+#if PY_MAJOR_VERSION >= 3
+      wstring sys_path(Py_GetPath() + wstring(begin(paths), end(paths)));
+      PySys_SetPath(const_cast<wchar_t*>(sys_path.c_str()));
+      dout(10) << "Computed sys.path '"
+              << string(begin(sys_path), end(sys_path)) << "'" << dendl;
+#else
+      string sys_path(Py_GetPath() + paths);
       PySys_SetPath(const_cast<char*>(sys_path.c_str()));
+      dout(10) << "Computed sys.path '" << sys_path << "'" << dendl;
+#endif
     }
 
-    PyMethodDef ModuleMethods[] = {
+    static PyMethodDef module_methods[] = {
       {nullptr}
     };
+#if PY_MAJOR_VERSION >= 3
+    static PyModuleDef ceph_module_def = {
+      PyModuleDef_HEAD_INIT,
+      "ceph_module",
+      nullptr,
+      -1,
+      module_methods,
+    };
+#endif
 
     // Initialize module
-    PyObject *ceph_module = Py_InitModule("ceph_module", ModuleMethods);
+#if PY_MAJOR_VERSION >= 3
+    PyObject *ceph_module = PyModule_Create(&ceph_module_def);
+#else
+    PyObject *ceph_module = Py_InitModule("ceph_module", module_methods);
+#endif
     assert(ceph_module != nullptr);
 
     auto load_class = [ceph_module](const char *name, PyTypeObject *type)
index 76ad1577075fadbc4e8c827a9a3efbf218661266..867814bfa16f146fb4bb2f141d04091d2534c7a0 100644 (file)
@@ -37,9 +37,6 @@ std::string PyModuleRegistry::config_prefix;
 
 
 
-#undef dout_prefix
-#define dout_prefix *_dout << "mgr " << __func__ << " "
-
 int PyModuleRegistry::init(const MgrMap &map)
 {
   Mutex::Locker locker(lock);
@@ -53,7 +50,13 @@ int PyModuleRegistry::init(const MgrMap &map)
   config_prefix = std::string(g_conf->name.get_type_str()) + "/";
 
   // Set up global python interpreter
+#if PY_MAJOR_VERSION >= 3
+#define WCHAR(s) L ## #s
+  Py_SetProgramName(const_cast<wchar_t*>(WCHAR(PYTHON_EXECUTABLE)));
+#undef WCHAR
+#else
   Py_SetProgramName(const_cast<char*>(PYTHON_EXECUTABLE));
+#endif
   Py_InitializeEx(0);
 
   // Let CPython know that we will be calling it back from other
index 81d744735042c338f5c1d5299c811a7843eac853..5b98a70279a45d4287cb00d66ce7e07691e65d2d 100644 (file)
@@ -13,7 +13,7 @@
 
 
 // Python.h comes first because otherwise it clobbers ceph's assert
-#include "Python.h"
+#include "PythonCompat.h"
 
 #include "PyModule.h"
 
index 09e5b041c87c9589a6ebd200aaafd40ca5555a7a..9d737424913b896b570902396ba9f6af382a2d3b 100644 (file)
@@ -5,7 +5,7 @@
 
 #include <string>
 
-#include "Python.h"
+#include "PythonCompat.h"
 
 
 
diff --git a/src/mgr/PythonCompat.h b/src/mgr/PythonCompat.h
new file mode 100644 (file)
index 0000000..745dad1
--- /dev/null
@@ -0,0 +1,28 @@
+#pragma once
+
+#include <Python.h>
+
+// Python's pyconfig-64.h conflicts with ceph's acconfig.h
+#undef HAVE_SYS_WAIT_H
+#undef HAVE_UNISTD_H
+#undef HAVE_UTIME_H
+#undef _POSIX_C_SOURCE
+#undef _XOPEN_SOURCE
+
+#if PY_MAJOR_VERSION >= 3
+inline PyObject* PyString_FromString(const char *v) {
+  return PyUnicode_FromFormat("%s", v);
+}
+inline char* PyString_AsString(PyObject *string) {
+  return PyUnicode_AsUTF8(string);
+}
+inline long PyInt_AsLong(PyObject *io) {
+  return PyLong_AsLong(io);
+}
+inline PyObject* PyInt_FromLong(long ival) {
+  return PyLong_FromLong(ival);
+}
+inline int PyString_Check(PyObject *o) {
+  return PyUnicode_Check(o);
+}
+#endif
index a22887bdeae9de06a7e42ec451ae31e23a572145..14d968ec3b39c0ea2fdeae330e1bb19971f8292e 100644 (file)
@@ -13,7 +13,7 @@
 
 #pragma once
 
-#include "Python.h"
+#include "PythonCompat.h"
 
 #include <string>
 #include <map>