]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/PyOSDMap: check parameter in constructor
authorKefu Chai <kchai@redhat.com>
Fri, 18 Jun 2021 13:26:25 +0000 (21:26 +0800)
committerKefu Chai <kchai@redhat.com>
Fri, 18 Jun 2021 13:28:50 +0000 (21:28 +0800)
* instead abort(), just return a failure, if the input parameter is
  missing
* set a TypeError if the input parameter's type is not expected

a typical session looks like:

[mgr self-test eval] >>> osdmap = OSDMap()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: function missing required argument 'osdmap_capsule' (pos 1)
[mgr self-test eval] >>> osdmap = OSDMap(1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: BasePyOSDMap.__init__() requires a PyCapsule_Type not int

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/mgr/PyOSDMap.cc

index 70813ca528773dd60d242e21ae9739c315455ce2..44be8e9423ceeb5ad98722f29f35755ed914ee94 100644 (file)
@@ -191,13 +191,17 @@ BasePyOSDMap_init(BasePyOSDMap *self, PyObject *args, PyObject *kwds)
     PyObject *osdmap_capsule = nullptr;
     static const char *kwlist[] = {"osdmap_capsule", NULL};
 
-    if (! PyArg_ParseTupleAndKeywords(args, kwds, "O",
-                                      const_cast<char**>(kwlist),
-                                      &osdmap_capsule)) {
-      ceph_abort();
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O",
+                                    const_cast<char**>(kwlist),
+                                    &osdmap_capsule)) {
+      return -1;
+    }
+    if (!PyObject_TypeCheck(osdmap_capsule, &PyCapsule_Type)) {
+      PyErr_Format(PyExc_TypeError,
+                  "Expected a PyCapsule_Type, not %s",
+                  Py_TYPE(osdmap_capsule)->tp_name);
       return -1;
     }
-    ceph_assert(PyObject_TypeCheck(osdmap_capsule, &PyCapsule_Type));
 
     self->osdmap = (OSDMap*)PyCapsule_GetPointer(
         osdmap_capsule, nullptr);