From: Sage Weil Date: Tue, 18 Dec 2018 20:11:30 +0000 (-0600) Subject: mgr: return options as appropriate python type X-Git-Tag: v14.1.0~532^2~12 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=2e8f9e690141641c06a6dc8e62ec4c63a2efebe9;p=ceph.git mgr: return options as appropriate python type Signed-off-by: Sage Weil --- diff --git a/src/mgr/BaseMgrModule.cc b/src/mgr/BaseMgrModule.cc index 20a4e9f434fe..d9e7a33ca0e5 100644 --- a/src/mgr/BaseMgrModule.cc +++ b/src/mgr/BaseMgrModule.cc @@ -399,7 +399,7 @@ ceph_get_module_option(BaseMgrModule *self, PyObject *args) what, &value); if (found) { dout(10) << __func__ << " " << what << " found: " << value.c_str() << dendl; - return PyString_FromString(value.c_str()); + return self->this_module->py_module->get_typed_option_value(what, value); } else { dout(4) << __func__ << " " << what << " not found " << dendl; Py_RETURN_NONE; diff --git a/src/mgr/BaseMgrStandbyModule.cc b/src/mgr/BaseMgrStandbyModule.cc index 0f1725fe8767..ee923552f40f 100644 --- a/src/mgr/BaseMgrStandbyModule.cc +++ b/src/mgr/BaseMgrStandbyModule.cc @@ -73,7 +73,7 @@ ceph_get_module_option(BaseMgrStandbyModule *self, PyObject *args) bool found = self->this_module->get_config(what, &value); if (found) { dout(10) << __func__ << " " << what << " found: " << value.c_str() << dendl; - return PyString_FromString(value.c_str()); + return self->this_module->py_module->get_typed_option_value(what, value); } else { dout(4) << __func__ << " " << what << " not found " << dendl; Py_RETURN_NONE; diff --git a/src/mgr/PyModule.cc b/src/mgr/PyModule.cc index 1233e72d9fe1..562c6d418381 100644 --- a/src/mgr/PyModule.cc +++ b/src/mgr/PyModule.cc @@ -609,6 +609,38 @@ bool PyModule::is_option(const std::string &option_name) return options.count(option_name) > 0; } +PyObject *PyModule::get_typed_option_value(const std::string& name, + const std::string& value) +{ + auto p = options.find(name); + if (p != options.end()) { + switch (p->second.type) { + case Option::TYPE_INT: + case Option::TYPE_UINT: + case Option::TYPE_SIZE: + return PyInt_FromString((char *)value.c_str(), nullptr, 0); + case Option::TYPE_SECS: + case Option::TYPE_FLOAT: + { + PyObject *s = PyString_FromString(value.c_str()); + PyObject *f = PyFloat_FromString(s, nullptr); + Py_DECREF(s); + return f; + } + case Option::TYPE_BOOL: + if (value == "1" || value == "true" || value == "True" || + value == "on" || value == "yes") { + Py_INCREF(Py_True); + return Py_True; + } else { + Py_INCREF(Py_False); + return Py_False; + } + } + } + return PyString_FromString(value.c_str()); +} + int PyModule::load_subclass_of(const char* base_class, PyObject** py_class) { // load the base class diff --git a/src/mgr/PyModule.h b/src/mgr/PyModule.h index 05b8b0fc121a..412fa38f088a 100644 --- a/src/mgr/PyModule.h +++ b/src/mgr/PyModule.h @@ -103,6 +103,10 @@ public: return options; } + PyObject *get_typed_option_value( + const std::string& option, + const std::string& value); + int load(PyThreadState *pMainThreadState); #if PY_MAJOR_VERSION >= 3 static PyObject* init_ceph_logger(); diff --git a/src/mgr/PyModuleRunner.h b/src/mgr/PyModuleRunner.h index fb58bf0c2323..08c85a132636 100644 --- a/src/mgr/PyModuleRunner.h +++ b/src/mgr/PyModuleRunner.h @@ -26,10 +26,11 @@ */ class PyModuleRunner { -protected: +public: // Info about the module we're going to run PyModuleRef py_module; +protected: // Populated by descendent class PyObject *pClassInstance = nullptr; diff --git a/src/pybind/mgr/mgr_module.py b/src/pybind/mgr/mgr_module.py index b620f630a463..cec00ed8493a 100644 --- a/src/pybind/mgr/mgr_module.py +++ b/src/pybind/mgr/mgr_module.py @@ -243,7 +243,7 @@ class MgrStandbyModule(ceph_module.BaseMgrStandbyModule): r = self._ceph_get_module_option(key) if r is None: final_key = key.split('/')[-1] - return str(self.MODULE_OPTION_DEFAULTS.get(final_key, default)) + return self.MODULE_OPTION_DEFAULTS.get(final_key, default) else: return r @@ -701,7 +701,7 @@ class MgrModule(ceph_module.BaseMgrModule): r = self._ceph_get_module_option(key) if r is None: final_key = key.split('/')[-1] - return str(self.MODULE_OPTION_DEFAULTS.get(final_key, default)) + return self.MODULE_OPTION_DEFAULTS.get(final_key, default) else: return r