]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr: add get_context() to python interface
authorJohn Spray <john.spray@redhat.com>
Sun, 5 Mar 2017 15:31:49 +0000 (10:31 -0500)
committerKefu Chai <kchai@redhat.com>
Wed, 3 May 2017 02:37:17 +0000 (10:37 +0800)
For modules that would like a CephContext* inside
a capsule, suitable for passing to librados constructor.

Signed-off-by: John Spray <john.spray@redhat.com>
src/mgr/PyModules.cc
src/mgr/PyModules.h
src/mgr/PyState.cc

index 0c28ef2e91f1156dbbf9f3137c40513793db4a56..7501ac55e294985b17a05077b525709a886c2ab1 100644 (file)
@@ -660,3 +660,16 @@ PyObject* PyModules::get_counter_python(
   return f.get();
 }
 
+PyObject *PyModules::get_context()
+{
+  PyThreadState *tstate = PyEval_SaveThread();
+  Mutex::Locker l(lock);
+  PyEval_RestoreThread(tstate);
+
+  // Construct a capsule containing ceph context.
+  // Not incrementing/decrementing ref count on the context because
+  // it's the global one and it has process lifetime.
+  auto capsule = PyCapsule_New(g_ceph_context, nullptr, nullptr);
+  return capsule;
+}
+
index 0005dabb1b0b6b0f0a588cba4c2b895236c1e797..b5fdde8a7761eed6c583548e2b33b32e189f22a5 100644 (file)
@@ -60,6 +60,7 @@ public:
   PyObject *get_counter_python(std::string const &handle,
       entity_type_t svc_type, const std::string &svc_id,
       const std::string &path);
+  PyObject *get_context();
 
   std::map<std::string, std::string> config_cache;
 
index 20f9336c0c0bce947120fba872ddafc50f3d5ed7..cbc28bb36bcf59ab1de93358adb189f27e4aa315 100644 (file)
@@ -264,6 +264,12 @@ ceph_get_version(PyObject *self, PyObject *args)
   return PyString_FromString(pretty_version_to_str().c_str());
 }
 
+static PyObject *
+ceph_get_context(PyObject *self, PyObject *args)
+{
+  return global_handle->get_context();
+}
+
 static PyObject*
 get_counter(PyObject *self, PyObject *args)
 {
@@ -305,6 +311,8 @@ PyMethodDef CephStateMethods[] = {
      "Emit a (local) log message"},
     {"get_version", ceph_get_version, METH_VARARGS,
      "Get the ceph version of this process"},
+    {"get_context", ceph_get_context, METH_NOARGS,
+      "Get a CephContext* in a python capsule"},
     {NULL, NULL, 0, NULL}
 };