From 845ca9c10df70ed1f441b47dcdbc2d1eb5da697e Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Fri, 23 Aug 2019 12:14:24 -0500 Subject: [PATCH] mgr: return get_ceph_option result as typed Py object (not string) We already return module options as the proper Py type; do the same for native ceph options. Signed-off-by: Sage Weil --- src/mgr/BaseMgrModule.cc | 11 +++++++---- src/mgr/CMakeLists.txt | 1 + src/mgr/PyModule.cc | 25 ++----------------------- src/mgr/PyUtil.cc | 40 ++++++++++++++++++++++++++++++++++++++++ src/mgr/PyUtil.h | 13 +++++++++++++ 5 files changed, 63 insertions(+), 27 deletions(-) create mode 100644 src/mgr/PyUtil.cc create mode 100644 src/mgr/PyUtil.h diff --git a/src/mgr/BaseMgrModule.cc b/src/mgr/BaseMgrModule.cc index 3380250bf0b..e69d28c3cfd 100644 --- a/src/mgr/BaseMgrModule.cc +++ b/src/mgr/BaseMgrModule.cc @@ -26,6 +26,7 @@ #include "common/errno.h" #include "common/version.h" +#include "PyUtil.h" #include "BaseMgrModule.h" #include "Gil.h" @@ -393,11 +394,13 @@ ceph_option_get(BaseMgrModule *self, PyObject *args) return nullptr; } - std::string value; - int r = g_conf().get_val(string(what), &value); - if (r >= 0) { + const Option *opt = g_conf().find_option(string(what)); + if (opt) { + std::string value; + int r = g_conf().get_val(string(what), &value); + assert(r >= 0); dout(10) << "ceph_option_get " << what << " found: " << value << dendl; - return PyString_FromString(value.c_str()); + return get_python_typed_option_value(opt->type, value); } else { dout(4) << "ceph_option_get " << what << " not found " << dendl; Py_RETURN_NONE; diff --git a/src/mgr/CMakeLists.txt b/src/mgr/CMakeLists.txt index 3c527a396e3..6636e0a6f57 100644 --- a/src/mgr/CMakeLists.txt +++ b/src/mgr/CMakeLists.txt @@ -15,6 +15,7 @@ set(mgr_srcs OSDPerfMetricTypes.cc OSDPerfMetricCollector.cc PyFormatter.cc + PyUtil.cc PyModule.cc PyModuleRegistry.cc PyModuleRunner.cc diff --git a/src/mgr/PyModule.cc b/src/mgr/PyModule.cc index 53d9e2b95fa..e65b46befff 100644 --- a/src/mgr/PyModule.cc +++ b/src/mgr/PyModule.cc @@ -15,6 +15,7 @@ #include "BaseMgrStandbyModule.h" #include "PyOSDMap.h" #include "MgrContext.h" +#include "PyUtil.h" #include "PyModule.h" @@ -626,29 +627,7 @@ PyObject *PyModule::get_typed_option_value(const std::string& name, // are set up exactly once during startup. 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 get_python_typed_option_value((Option::type_t)p->second.type, value); } return PyString_FromString(value.c_str()); } diff --git a/src/mgr/PyUtil.cc b/src/mgr/PyUtil.cc new file mode 100644 index 00000000000..04a3a5d1fa5 --- /dev/null +++ b/src/mgr/PyUtil.cc @@ -0,0 +1,40 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#include "PyUtil.h" +#include "PythonCompat.h" + +PyObject *get_python_typed_option_value( + Option::type_t type, + const std::string& value) +{ + switch (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; + } + case Option::TYPE_STR: + case Option::TYPE_ADDR: + case Option::TYPE_ADDRVEC: + case Option::TYPE_UUID: + break; + } + return PyString_FromString(value.c_str()); +} diff --git a/src/mgr/PyUtil.h b/src/mgr/PyUtil.h new file mode 100644 index 00000000000..3d8bc7b984f --- /dev/null +++ b/src/mgr/PyUtil.h @@ -0,0 +1,13 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#pragma once + +#include + +#include "Python.h" +#include "common/options.h" + +PyObject *get_python_typed_option_value( + Option::type_t type, + const std::string& value); -- 2.39.5