From: John Spray Date: Sun, 5 Mar 2017 15:31:49 +0000 (-0500) Subject: mgr: add get_context() to python interface X-Git-Tag: v12.0.3~79^2~4 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=07cae6214b6c12e4b118f5142e90a37386b446b2;p=ceph.git mgr: add get_context() to python interface For modules that would like a CephContext* inside a capsule, suitable for passing to librados constructor. Signed-off-by: John Spray --- diff --git a/src/mgr/PyModules.cc b/src/mgr/PyModules.cc index 0c28ef2e91f1..7501ac55e294 100644 --- a/src/mgr/PyModules.cc +++ b/src/mgr/PyModules.cc @@ -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; +} + diff --git a/src/mgr/PyModules.h b/src/mgr/PyModules.h index 0005dabb1b0b..b5fdde8a7761 100644 --- a/src/mgr/PyModules.h +++ b/src/mgr/PyModules.h @@ -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 config_cache; diff --git a/src/mgr/PyState.cc b/src/mgr/PyState.cc index 20f9336c0c0b..cbc28bb36bcf 100644 --- a/src/mgr/PyState.cc +++ b/src/mgr/PyState.cc @@ -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} };