Skip to content

Commit

Permalink
mgr: be compatible with py3
Browse files Browse the repository at this point in the history
so ceph-mgr can be compiled using py3

Signed-off-by: Kefu Chai <kchai@redhat.com>
  • Loading branch information
tchaikov committed Jan 24, 2018
1 parent f455b48 commit 438136d
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/mgr/BaseMgrStandbyModule.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#pragma once

#include "Python.h"
#include "PythonCompat.h"

extern PyTypeObject BaseMgrStandbyModuleType;

8 changes: 1 addition & 7 deletions src/mgr/Mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@
#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"
Expand Down
8 changes: 1 addition & 7 deletions src/mgr/PyFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
#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>
Expand Down
52 changes: 46 additions & 6 deletions src/mgr/PyModuleRegistry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ namespace {
{"flush", log_flush, METH_VARARGS, "flush"},
{nullptr, nullptr, 0, nullptr}
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef ceph_logger_module = {
PyModuleDef_HEAD_INIT,
"ceph_logger",
nullptr,
-1,
log_methods,
};
#endif
}

#undef dout_prefix
Expand Down Expand Up @@ -143,7 +152,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
Expand Down Expand Up @@ -203,34 +218,59 @@ 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[] = {
{nullptr}
};

#if PY_MAJOR_VERSION >= 3
PyModuleDef ceph_module_def = {
PyModuleDef_HEAD_INIT,
"ceph_module",
nullptr,
-1,
ModuleMethods,
};
#endif
// Initialize module
#if PY_MAJOR_VERSION >= 3
PyObject *ceph_module = PyModule_Create(&ceph_module_def);
#else
PyObject *ceph_module = Py_InitModule("ceph_module", ModuleMethods);
#endif
assert(ceph_module != nullptr);

auto load_class = [ceph_module](const char *name, PyTypeObject *type)
Expand Down
2 changes: 1 addition & 1 deletion src/mgr/PyModuleRunner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


// Python.h comes first because otherwise it clobbers ceph's assert
#include "Python.h"
#include "PythonCompat.h"

#include "common/debug.h"
#include "mgr/Gil.h"
Expand Down
2 changes: 1 addition & 1 deletion src/mgr/PyOSDMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <string>

#include "Python.h"
#include "PythonCompat.h"



Expand Down
28 changes: 28 additions & 0 deletions src/mgr/PythonCompat.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/mgr/StandbyPyModules.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#pragma once

#include "Python.h"
#include "PythonCompat.h"

#include <string>
#include <map>
Expand Down

0 comments on commit 438136d

Please sign in to comment.