From 07cae6214b6c12e4b118f5142e90a37386b446b2 Mon Sep 17 00:00:00 2001 From: John Spray Date: Sun, 5 Mar 2017 10:31:49 -0500 Subject: [PATCH] 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 --- src/mgr/PyModules.cc | 13 +++++++++++++ src/mgr/PyModules.h | 1 + src/mgr/PyState.cc | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/src/mgr/PyModules.cc b/src/mgr/PyModules.cc index 0c28ef2e91f11..7501ac55e2949 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 0005dabb1b0b6..b5fdde8a7761e 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 20f9336c0c0bc..cbc28bb36bcf5 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} }; -- 2.39.5