#define dout_prefix *_dout << "mgr[py] "
// definition for non-const static member
-std::string PyModule::config_prefix = "mgr/";
+std::string PyModule::mgr_store_prefix = "mgr/";
// Courtesy of http://stackoverflow.com/questions/1418015/how-to-get-python-exception-text
-#include <boost/python.hpp>
+#define BOOST_BIND_GLOBAL_PLACEHOLDERS
+// Boost apparently can't be bothered to fix its own usage of its own
+// deprecated features.
+#include <boost/python/extract.hpp>
+#include <boost/python/import.hpp>
+#include <boost/python/object.hpp>
+#undef BOOST_BIND_GLOBAL_PLACEHOLDERS
#include <boost/algorithm/string/predicate.hpp>
#include "include/ceph_assert.h" // boost clobbers this
// decode a Python exception into a string
{nullptr, nullptr, 0, nullptr}
};
-#if PY_MAJOR_VERSION >= 3
static PyModuleDef ceph_logger_module = {
PyModuleDef_HEAD_INIT,
"ceph_logger",
-1,
log_methods,
};
-#endif
}
PyModuleConfig::PyModuleConfig() = default;
const std::string &module_name,
const std::string &key, const boost::optional<std::string>& val)
{
- const std::string global_key = PyModule::config_prefix
- + module_name + "/" + key;
+ const std::string global_key = "mgr/" + module_name + "/" + key;
Command set_cmd;
{
std::ostringstream cmd_json;
return site_packages.str();
}
-#if PY_MAJOR_VERSION >= 3
PyObject* PyModule::init_ceph_logger()
{
auto py_logger = PyModule_Create(&ceph_logger_module);
PySys_SetObject("stdout", py_logger);
return py_logger;
}
-#else
-void PyModule::init_ceph_logger()
-{
- 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
-#if PY_MAJOR_VERSION >= 3
PyObject* PyModule::init_ceph_module()
-#else
-void PyModule::init_ceph_module()
-#endif
{
static PyMethodDef module_methods[] = {
{nullptr, nullptr, 0, nullptr}
};
-#if PY_MAJOR_VERSION >= 3
static PyModuleDef ceph_module_def = {
PyModuleDef_HEAD_INIT,
"ceph_module",
nullptr
};
PyObject *ceph_module = PyModule_Create(&ceph_module_def);
-#else
- PyObject *ceph_module = Py_InitModule("ceph_module", module_methods);
-#endif
ceph_assert(ceph_module != nullptr);
std::map<const char*, PyTypeObject*> classes{
{{"BaseMgrModule", &BaseMgrModuleType},
PyModule_AddObject(ceph_module, name, (PyObject *)type);
}
-#if PY_MAJOR_VERSION >= 3
return ceph_module;
-#endif
}
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
// Configure sys.path to include mgr_module_path
- string paths = (":" + g_conf().get_val<std::string>("mgr_module_path") +
- ":" + get_site_packages());
-#if PY_MAJOR_VERSION >= 3
- wstring sys_path(Py_GetPath() + wstring(begin(paths), end(paths)));
+ string paths = (g_conf().get_val<std::string>("mgr_module_path") + ':' +
+ get_site_packages() + ':');
+ wstring sys_path(wstring(begin(paths), end(paths)) + Py_GetPath());
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
}
}
// Environment is all good, import the external module
command.perm = PyUnicode_AsUTF8(pPerm);
command.polling = false;
- PyObject *pPoll = PyDict_GetItemString(pCommand, "poll");
- if (pPoll) {
- std::string polling = PyUnicode_AsUTF8(pPoll);
- if (boost::iequals(polling, "true")) {
- command.polling = true;
- }
+ if (PyObject *pPoll = PyDict_GetItemString(pCommand, "poll");
+ pPoll && PyObject_IsTrue(pPoll)) {
+ command.polling = true;
}
command.module_name = module_name;