From b0ce0b4e139e9eea5629929e8e0b5a7b22dcf8a2 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 23 Aug 2019 10:36:44 +0800 Subject: [PATCH] mgr/BaseMgrModule: use PyInt_Check() to compatible with py2 in python2, we have both `PyLongObject` and `PyIntObject` for presenting `long` and `int` types. and in python3, `PyIntObject` is dropped, `int` is represented using `PyLongObject`. and the related functions like `PyInt_Check()` and `PyInt_AsLong()` are removed along with `PyIntObject`. so we should use different set of functions for converting an `int` python object to native type in C++. since we don't use `long` for "count" when calling `MgrModule.set_health_checks()`, in this change, we just define a macro to unify the difference between python2 and python3. the same applies to "max_count" parameter of `MgrModule.add_osd_perf_query()` call. Signed-off-by: Kefu Chai --- src/mgr/BaseMgrModule.cc | 14 ++++---------- src/mgr/PythonCompat.h | 3 +++ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/mgr/BaseMgrModule.cc b/src/mgr/BaseMgrModule.cc index af47bde60fb..3380250bf0b 100644 --- a/src/mgr/BaseMgrModule.cc +++ b/src/mgr/BaseMgrModule.cc @@ -303,13 +303,11 @@ ceph_set_health_checks(BaseMgrModule *self, PyObject *args) summary = std::move(vs); } } else if (ks == "count") { - if (PyLong_Check(v)) { - count = PyLong_AsLong(v); - } else if (PyInt_Check(v)) { + if (PyInt_Check(v)) { count = PyInt_AsLong(v); } else { derr << __func__ << " check " << check_name - << " count value not long or int" << dendl; + << " count value not int" << dendl; continue; } } else if (ks == "detail") { @@ -946,16 +944,12 @@ ceph_add_osd_perf_query(BaseMgrModule *self, PyObject *args) } limit->order_by = it->second; } else if (limit_param_name == NAME_LIMIT_MAX_COUNT) { -#if PY_MAJOR_VERSION <= 2 - if (!PyInt_Check(limit_param_val) && !PyLong_Check(limit_param_val)) { -#else - if (!PyLong_Check(limit_param_val)) { -#endif + if (!PyInt_Check(limit_param_val)) { derr << __func__ << " " << limit_param_name << " not an int" << dendl; Py_RETURN_NONE; } - limit->max_count = PyLong_AsLong(limit_param_val); + limit->max_count = PyInt_AsLong(limit_param_val); } else { derr << __func__ << " unknown limit param: " << limit_param_name << dendl; diff --git a/src/mgr/PythonCompat.h b/src/mgr/PythonCompat.h index c2929851de8..14f98ef9c1c 100644 --- a/src/mgr/PythonCompat.h +++ b/src/mgr/PythonCompat.h @@ -20,6 +20,9 @@ inline PyObject* PyString_FromString(const char *v) { inline const char* PyString_AsString(PyObject *string) { return PyUnicode_AsUTF8(string); } +inline int PyInt_Check(PyObject *o) { + return PyLong_Check(o); +} inline long PyInt_AsLong(PyObject *io) { return PyLong_AsLong(io); } -- 2.39.5