]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/BaseMgrModule: use PyInt_Check() to compatible with py2 29831/head
authorKefu Chai <kchai@redhat.com>
Fri, 23 Aug 2019 02:36:44 +0000 (10:36 +0800)
committerKefu Chai <kchai@redhat.com>
Fri, 23 Aug 2019 14:30:22 +0000 (22:30 +0800)
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 <kchai@redhat.com>
src/mgr/BaseMgrModule.cc
src/mgr/PythonCompat.h

index af47bde60fb42ffc422a99df38b725ae31b445a4..3380250bf0bea9127a287fa4c16f2964c317c8ca 100644 (file)
@@ -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;
index c2929851de83e60531e0bf0b35942479bab2a652..14f98ef9c1c3c1adc6343dc42f4b8e335a4d0fb3 100644 (file)
@@ -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);
 }