From: Kefu Chai Date: Wed, 18 Dec 2019 11:03:29 +0000 (+0800) Subject: mgr: drop the compatibility with python2 X-Git-Tag: v15.1.0~436^2~5 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=48c4bc445fcacd7980e84b4f135a3115b88b05f1;p=ceph.git mgr: drop the compatibility with python2 Signed-off-by: Kefu Chai --- diff --git a/src/mgr/ActivePyModule.cc b/src/mgr/ActivePyModule.cc index 78704f876912..2110dea57252 100644 --- a/src/mgr/ActivePyModule.cc +++ b/src/mgr/ActivePyModule.cc @@ -34,7 +34,7 @@ int ActivePyModule::load(ActivePyModules *py_modules) // with us in logging etc. auto pThisPtr = PyCapsule_New(this, nullptr, nullptr); auto pPyModules = PyCapsule_New(py_modules, nullptr, nullptr); - auto pModuleName = PyString_FromString(get_name().c_str()); + auto pModuleName = PyUnicode_FromString(get_name().c_str()); auto pArgs = PyTuple_Pack(3, pModuleName, pPyModules, pThisPtr); pClassInstance = PyObject_CallObject(py_module->pClass, pArgs); @@ -230,9 +230,9 @@ int ActivePyModule::handle_command( "returned wrong type!" << dendl; r = -EINVAL; } else { - r = PyInt_AsLong(PyTuple_GetItem(pResult, 0)); - *ds << PyString_AsString(PyTuple_GetItem(pResult, 1)); - *ss << PyString_AsString(PyTuple_GetItem(pResult, 2)); + r = PyLong_AsLong(PyTuple_GetItem(pResult, 0)); + *ds << PyUnicode_AsUTF8(PyTuple_GetItem(pResult, 1)); + *ss << PyUnicode_AsUTF8(PyTuple_GetItem(pResult, 2)); } Py_DECREF(pResult); diff --git a/src/mgr/ActivePyModules.cc b/src/mgr/ActivePyModules.cc index 4d38a6e1e0f2..5595a2e66077 100644 --- a/src/mgr/ActivePyModules.cc +++ b/src/mgr/ActivePyModules.cc @@ -183,7 +183,7 @@ PyObject *ActivePyModules::get_python(const std::string &what) osd_map.crush->encode(rdata, CEPH_FEATURES_SUPPORTED_DEFAULT); }); std::string crush_text = rdata.to_str(); - return PyString_FromString(crush_text.c_str()); + return PyUnicode_FromString(crush_text.c_str()); } else if (what.substr(0, 7) == "osd_map") { cluster_state.with_osdmap([&f, &what, &tstate](const OSDMap &osd_map){ PyEval_RestoreThread(tstate); diff --git a/src/mgr/BaseMgrModule.cc b/src/mgr/BaseMgrModule.cc index 7d603d45a6e3..baa8426a42a5 100644 --- a/src/mgr/BaseMgrModule.cc +++ b/src/mgr/BaseMgrModule.cc @@ -91,9 +91,9 @@ public: auto set_fn = PyObject_GetAttrString(python_completion, "complete"); ceph_assert(set_fn != nullptr); - auto pyR = PyInt_FromLong(r); - auto pyOutBl = PyString_FromString(outbl.to_str().c_str()); - auto pyOutS = PyString_FromString(outs.c_str()); + auto pyR = PyLong_FromLong(r); + auto pyOutBl = PyUnicode_FromString(outbl.to_str().c_str()); + auto pyOutS = PyUnicode_FromString(outs.c_str()); auto args = PyTuple_Pack(3, pyR, pyOutBl, pyOutS); Py_DECREF(pyR); Py_DECREF(pyOutBl); @@ -287,26 +287,27 @@ ceph_set_health_checks(BaseMgrModule *self, PyObject *args) } string ks(k); if (ks == "severity") { - if (auto [vs, valid] = PyString_ToString(v); !valid) { + if (!PyUnicode_Check(v)) { derr << __func__ << " check " << check_name << " severity value not string" << dendl; continue; - } else if (vs == "warning") { + } + if (const string vs = PyUnicode_AsUTF8(v); vs == "warning") { severity = HEALTH_WARN; } else if (vs == "error") { severity = HEALTH_ERR; } } else if (ks == "summary") { - if (auto [vs, valid] = PyString_ToString(v); !valid) { + if (!PyUnicode_Check(v)) { derr << __func__ << " check " << check_name << " summary value not [unicode] string" << dendl; continue; } else { - summary = std::move(vs); + summary = PyUnicode_AsUTF8(v); } } else if (ks == "count") { - if (PyInt_Check(v)) { - count = PyInt_AsLong(v); + if (PyLong_Check(v)) { + count = PyLong_AsLong(v); } else { derr << __func__ << " check " << check_name << " count value not int" << dendl; @@ -320,12 +321,12 @@ ceph_set_health_checks(BaseMgrModule *self, PyObject *args) } for (int k = 0; k < PyList_Size(v); ++k) { PyObject *di = PyList_GET_ITEM(v, k); - if (auto [vs, valid] = PyString_ToString(di); !valid) { + if (!PyUnicode_Check(di)) { derr << __func__ << " check " << check_name << " detail item " << k << " not a [unicode] string" << dendl; continue; } else { - detail.push_back(std::move(vs)); + detail.push_back(PyUnicode_AsUTF8(di)); } } } else { @@ -383,7 +384,7 @@ ceph_get_server(BaseMgrModule *self, PyObject *args) static PyObject* ceph_get_mgr_id(BaseMgrModule *self, PyObject *args) { - return PyString_FromString(g_conf()->name.get_id().c_str()); + return PyUnicode_FromString(g_conf()->name.get_id().c_str()); } static PyObject* @@ -477,7 +478,7 @@ ceph_store_get(BaseMgrModule *self, PyObject *args) what, &value); if (found) { dout(10) << "ceph_store_get " << what << " found: " << value.c_str() << dendl; - return PyString_FromString(value.c_str()); + return PyUnicode_FromString(value.c_str()); } else { dout(4) << "ceph_store_get " << what << " not found " << dendl; Py_RETURN_NONE; @@ -570,13 +571,13 @@ ceph_cluster_log(BaseMgrModule *self, PyObject *args) static PyObject * ceph_get_version(BaseMgrModule *self, PyObject *args) { - return PyString_FromString(pretty_version_to_str().c_str()); + return PyUnicode_FromString(pretty_version_to_str().c_str()); } static PyObject * ceph_get_release_name(BaseMgrModule *self, PyObject *args) { - return PyString_FromString(ceph_release_to_str()); + return PyUnicode_FromString(ceph_release_to_str()); } static PyObject * @@ -849,12 +850,12 @@ ceph_add_osd_perf_query(BaseMgrModule *self, PyObject *args) Py_RETURN_NONE; } if (param_name == NAME_SUB_KEY_TYPE) { - if (!PyString_Check(param_value)) { + if (!PyUnicode_Check(param_value)) { derr << __func__ << " query " << query_param_name << " item " << j << " contains invalid param " << param_name << dendl; Py_RETURN_NONE; } - auto type = PyString_AsString(param_value); + auto type = PyUnicode_AsUTF8(param_value); auto it = sub_key_types.find(type); if (it == sub_key_types.end()) { derr << __func__ << " query " << query_param_name << " item " << j @@ -863,12 +864,12 @@ ceph_add_osd_perf_query(BaseMgrModule *self, PyObject *args) } d.type = it->second; } else if (param_name == NAME_SUB_KEY_REGEX) { - if (!PyString_Check(param_value)) { + if (!PyUnicode_Check(param_value)) { derr << __func__ << " query " << query_param_name << " item " << j << " contains invalid param " << param_name << dendl; Py_RETURN_NONE; } - d.regex_str = PyString_AsString(param_value); + d.regex_str = PyUnicode_AsUTF8(param_value); try { d.regex = d.regex_str.c_str(); } catch (const std::regex_error& e) { @@ -903,12 +904,12 @@ ceph_add_osd_perf_query(BaseMgrModule *self, PyObject *args) } for (int j = 0; j < PyList_Size(query_param_val); j++) { PyObject *py_type = PyList_GET_ITEM(query_param_val, j); - if (!PyString_Check(py_type)) { + if (!PyUnicode_Check(py_type)) { derr << __func__ << " query " << query_param_name << " item " << j << " not a string" << dendl; Py_RETURN_NONE; } - auto type = PyString_AsString(py_type); + auto type = PyUnicode_AsUTF8(py_type); auto it = counter_types.find(type); if (it == counter_types.end()) { derr << __func__ << " query " << query_param_name << " item " << type @@ -939,12 +940,12 @@ ceph_add_osd_perf_query(BaseMgrModule *self, PyObject *args) } if (limit_param_name == NAME_LIMIT_ORDER_BY) { - if (!PyString_Check(limit_param_val)) { + if (!PyUnicode_Check(limit_param_val)) { derr << __func__ << " " << limit_param_name << " not a string" << dendl; Py_RETURN_NONE; } - auto order_by = PyString_AsString(limit_param_val); + auto order_by = PyUnicode_AsUTF8(limit_param_val); auto it = counter_types.find(order_by); if (it == counter_types.end()) { derr << __func__ << " limit " << limit_param_name @@ -953,12 +954,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 (!PyInt_Check(limit_param_val)) { + if (!PyLong_Check(limit_param_val)) { derr << __func__ << " " << limit_param_name << " not an int" << dendl; Py_RETURN_NONE; } - limit->max_count = PyInt_AsLong(limit_param_val); + limit->max_count = PyLong_AsLong(limit_param_val); } else { derr << __func__ << " unknown limit param: " << limit_param_name << dendl; diff --git a/src/mgr/BaseMgrStandbyModule.cc b/src/mgr/BaseMgrStandbyModule.cc index 59f97d8172b9..84c9b79f2f7b 100644 --- a/src/mgr/BaseMgrStandbyModule.cc +++ b/src/mgr/BaseMgrStandbyModule.cc @@ -57,7 +57,7 @@ BaseMgrStandbyModule_init(BaseMgrStandbyModule *self, PyObject *args, PyObject * static PyObject* ceph_get_mgr_id(BaseMgrStandbyModule *self, PyObject *args) { - return PyString_FromString(g_conf()->name.get_id().c_str()); + return PyUnicode_FromString(g_conf()->name.get_id().c_str()); } static PyObject* @@ -110,7 +110,7 @@ ceph_option_get(BaseMgrStandbyModule *self, PyObject *args) int r = g_conf().get_val(string(what), &value); if (r >= 0) { dout(10) << "ceph_option_get " << what << " found: " << value << dendl; - return PyString_FromString(value.c_str()); + return PyUnicode_FromString(value.c_str()); } else { dout(4) << "ceph_option_get " << what << " not found " << dendl; Py_RETURN_NONE; @@ -136,7 +136,7 @@ ceph_store_get(BaseMgrStandbyModule *self, PyObject *args) if (found) { dout(10) << "ceph_store_get " << what << " found: " << value.c_str() << dendl; - return PyString_FromString(value.c_str()); + return PyUnicode_FromString(value.c_str()); } else { dout(4) << "ceph_store_get " << what << " not found " << dendl; Py_RETURN_NONE; @@ -146,7 +146,7 @@ ceph_store_get(BaseMgrStandbyModule *self, PyObject *args) static PyObject* ceph_get_active_uri(BaseMgrStandbyModule *self, PyObject *args) { - return PyString_FromString(self->this_module->get_active_uri().c_str()); + return PyUnicode_FromString(self->this_module->get_active_uri().c_str()); } static PyObject* diff --git a/src/mgr/BaseMgrStandbyModule.h b/src/mgr/BaseMgrStandbyModule.h index c4cf267304c9..82bda9105b8c 100644 --- a/src/mgr/BaseMgrStandbyModule.h +++ b/src/mgr/BaseMgrStandbyModule.h @@ -1,7 +1,6 @@ - #pragma once -#include "PythonCompat.h" +#include extern PyTypeObject BaseMgrStandbyModuleType; diff --git a/src/mgr/Mgr.h b/src/mgr/Mgr.h index d38a75cac931..9a712edcc764 100644 --- a/src/mgr/Mgr.h +++ b/src/mgr/Mgr.h @@ -15,7 +15,7 @@ #define CEPH_MGR_H_ // Python.h comes first because otherwise it clobbers ceph's assert -#include "PythonCompat.h" +#include #include "mds/FSMap.h" #include "messages/MFSMap.h" diff --git a/src/mgr/PyFormatter.cc b/src/mgr/PyFormatter.cc index a90674734eac..5dc64a1f4e5d 100644 --- a/src/mgr/PyFormatter.cc +++ b/src/mgr/PyFormatter.cc @@ -57,7 +57,7 @@ void PyFormatter::dump_float(const char *name, double d) void PyFormatter::dump_string(const char *name, std::string_view s) { - dump_pyobject(name, PyString_FromString(s.data())); + dump_pyobject(name, PyUnicode_FromString(s.data())); } void PyFormatter::dump_bool(const char *name, bool b) @@ -90,7 +90,7 @@ void PyFormatter::dump_format_va(const char *name, const char *ns, bool quoted, char buf[LARGE_SIZE]; vsnprintf(buf, LARGE_SIZE, fmt, ap); - dump_pyobject(name, PyString_FromString(buf)); + dump_pyobject(name, PyUnicode_FromString(buf)); } /** @@ -102,7 +102,7 @@ void PyFormatter::dump_pyobject(const char *name, PyObject *p) PyList_Append(cursor, p); Py_DECREF(p); } else if (PyDict_Check(cursor)) { - PyObject *key = PyString_FromString(name); + PyObject *key = PyUnicode_FromString(name); PyDict_SetItem(cursor, key, p); Py_DECREF(key); Py_DECREF(p); @@ -118,7 +118,7 @@ void PyFormatter::finish_pending_streams() cursor = i->cursor; dump_pyobject( i->name.c_str(), - PyString_FromString(i->stream.str().c_str())); + PyUnicode_FromString(i->stream.str().c_str())); cursor = tmp_cur; } diff --git a/src/mgr/PyFormatter.h b/src/mgr/PyFormatter.h index e30ae9f054ab..e7503cc15399 100644 --- a/src/mgr/PyFormatter.h +++ b/src/mgr/PyFormatter.h @@ -18,7 +18,7 @@ #define PY_FORMATTER_H_ // Python.h comes first because otherwise it clobbers ceph's assert -#include "PythonCompat.h" +#include #include #include diff --git a/src/mgr/PyModule.cc b/src/mgr/PyModule.cc index 30df62e75fb0..004325a3c6f5 100644 --- a/src/mgr/PyModule.cc +++ b/src/mgr/PyModule.cc @@ -54,7 +54,7 @@ std::string handle_pyerror() // returning only the exception string value PyObject *name_attr = PyObject_GetAttrString(exc, "__name__"); std::stringstream ss; - ss << PyString_AsString(name_attr) << ": " << PyString_AsString(val); + ss << PyUnicode_AsUTF8(name_attr) << ": " << PyUnicode_AsUTF8(val); Py_XDECREF(name_attr); return ss.str(); } @@ -67,7 +67,7 @@ std::string handle_pyerror() // returning only the exception string value PyObject *name_attr = PyObject_GetAttrString(exc, "__name__"); std::stringstream ss; - ss << PyString_AsString(name_attr) << ": " << PyString_AsString(val); + ss << PyUnicode_AsUTF8(name_attr) << ": " << PyUnicode_AsUTF8(val); Py_XDECREF(name_attr); return ss.str(); } @@ -87,7 +87,7 @@ std::string peek_pyerror() ceph_assert(ptype); ceph_assert(pvalue); PyObject *pvalue_str = PyObject_Str(pvalue); - std::string exc_msg = PyString_AsString(pvalue_str); + std::string exc_msg = PyUnicode_AsUTF8(pvalue_str); Py_DECREF(pvalue_str); PyErr_Restore(ptype, pvalue, ptraceback); @@ -202,7 +202,7 @@ std::string PyModule::get_site_packages() if (i != 0) { site_packages << ":"; } - site_packages << PyString_AsString(PyList_GetItem(site_packages_list, i)); + site_packages << PyUnicode_AsUTF8(PyList_GetItem(site_packages_list, i)); } Py_DECREF(site_packages_list); @@ -231,13 +231,13 @@ std::string PyModule::get_site_packages() auto n = PyList_Size(sys_path); bool first = true; for (Py_ssize_t i = 0; i < n; ++i) { - dout(1) << " " << PyString_AsString(PyList_GetItem(sys_path, i)) << dendl; + dout(1) << " " << PyUnicode_AsUTF8(PyList_GetItem(sys_path, i)) << dendl; if (first) { first = false; } else { site_packages << ":"; } - site_packages << PyString_AsString(PyList_GetItem(sys_path, i)); + site_packages << PyUnicode_AsUTF8(PyList_GetItem(sys_path, i)); } Py_DECREF(sys_path); @@ -401,7 +401,7 @@ int PyModule::load(PyThreadState *pMainThreadState) if (PyTuple_Check(pCanRunTuple) && PyTuple_Size(pCanRunTuple) == 2) { PyObject *pCanRun = PyTuple_GetItem(pCanRunTuple, 0); PyObject *can_run_str = PyTuple_GetItem(pCanRunTuple, 1); - if (!PyBool_Check(pCanRun) || !PyString_Check(can_run_str)) { + if (!PyBool_Check(pCanRun) || !PyUnicode_Check(can_run_str)) { derr << "Module " << get_name() << " returned wrong type in can_run" << dendl; error_string = "wrong type returned from can_run"; @@ -409,7 +409,7 @@ int PyModule::load(PyThreadState *pMainThreadState) } else { can_run = (pCanRun == Py_True); if (!can_run) { - error_string = PyString_AsString(can_run_str); + error_string = PyUnicode_AsUTF8(can_run_str); dout(4) << "Module " << get_name() << " reported that it cannot run: " << error_string << dendl; @@ -491,22 +491,22 @@ int PyModule::load_commands() PyObject *pCmd = PyDict_GetItemString(pCommand, "cmd"); ceph_assert(pCmd != nullptr); - command.cmdstring = PyString_AsString(pCmd); + command.cmdstring = PyUnicode_AsUTF8(pCmd); dout(20) << "loaded command " << command.cmdstring << dendl; PyObject *pDesc = PyDict_GetItemString(pCommand, "desc"); ceph_assert(pDesc != nullptr); - command.helpstring = PyString_AsString(pDesc); + command.helpstring = PyUnicode_AsUTF8(pDesc); PyObject *pPerm = PyDict_GetItemString(pCommand, "perm"); ceph_assert(pPerm != nullptr); - command.perm = PyString_AsString(pPerm); + command.perm = PyUnicode_AsUTF8(pPerm); command.polling = false; PyObject *pPoll = PyDict_GetItemString(pCommand, "poll"); if (pPoll) { - std::string polling = PyString_AsString(pPoll); + std::string polling = PyUnicode_AsUTF8(pPoll); if (boost::iequals(polling, "true")) { command.polling = true; } @@ -531,40 +531,40 @@ int PyModule::load_options() PyObject *p; p = PyDict_GetItemString(pOption, "name"); ceph_assert(p != nullptr); - option.name = PyString_AsString(p); + option.name = PyUnicode_AsUTF8(p); option.type = Option::TYPE_STR; p = PyDict_GetItemString(pOption, "type"); - if (p && PyObject_TypeCheck(p, &PyString_Type)) { - std::string s = PyString_AsString(p); + if (p && PyObject_TypeCheck(p, &PyUnicode_Type)) { + std::string s = PyUnicode_AsUTF8(p); int t = Option::str_to_type(s); if (t >= 0) { option.type = t; } } p = PyDict_GetItemString(pOption, "desc"); - if (p && PyObject_TypeCheck(p, &PyString_Type)) { - option.desc = PyString_AsString(p); + if (p && PyObject_TypeCheck(p, &PyUnicode_Type)) { + option.desc = PyUnicode_AsUTF8(p); } p = PyDict_GetItemString(pOption, "long_desc"); - if (p && PyObject_TypeCheck(p, &PyString_Type)) { - option.long_desc = PyString_AsString(p); + if (p && PyObject_TypeCheck(p, &PyUnicode_Type)) { + option.long_desc = PyUnicode_AsUTF8(p); } p = PyDict_GetItemString(pOption, "default"); if (p) { auto q = PyObject_Str(p); - option.default_value = PyString_AsString(q); + option.default_value = PyUnicode_AsUTF8(q); Py_DECREF(q); } p = PyDict_GetItemString(pOption, "min"); if (p) { auto q = PyObject_Str(p); - option.min = PyString_AsString(q); + option.min = PyUnicode_AsUTF8(q); Py_DECREF(q); } p = PyDict_GetItemString(pOption, "max"); if (p) { auto q = PyObject_Str(p); - option.max = PyString_AsString(q); + option.max = PyUnicode_AsUTF8(q); Py_DECREF(q); } p = PyDict_GetItemString(pOption, "enum_allowed"); @@ -573,7 +573,7 @@ int PyModule::load_options() auto q = PyList_GetItem(p, i); if (q) { auto r = PyObject_Str(q); - option.enum_allowed.insert(PyString_AsString(r)); + option.enum_allowed.insert(PyUnicode_AsUTF8(r)); Py_DECREF(r); } } @@ -582,8 +582,8 @@ int PyModule::load_options() if (p && PyObject_TypeCheck(p, &PyList_Type)) { for (unsigned i = 0; i < PyList_Size(p); ++i) { auto q = PyList_GetItem(p, i); - if (q && PyObject_TypeCheck(q, &PyString_Type)) { - option.see_also.insert(PyString_AsString(q)); + if (q && PyObject_TypeCheck(q, &PyUnicode_Type)) { + option.see_also.insert(PyUnicode_AsUTF8(q)); } } } @@ -591,8 +591,8 @@ int PyModule::load_options() if (p && PyObject_TypeCheck(p, &PyList_Type)) { for (unsigned i = 0; i < PyList_Size(p); ++i) { auto q = PyList_GetItem(p, i); - if (q && PyObject_TypeCheck(q, &PyString_Type)) { - option.tags.insert(PyString_AsString(q)); + if (q && PyObject_TypeCheck(q, &PyUnicode_Type)) { + option.tags.insert(PyUnicode_AsUTF8(q)); } } } @@ -630,7 +630,7 @@ PyObject *PyModule::get_typed_option_value(const std::string& name, if (p != options.end()) { return get_python_typed_option_value((Option::type_t)p->second.type, value); } - return PyString_FromString(value.c_str()); + return PyUnicode_FromString(value.c_str()); } int PyModule::load_subclass_of(const char* base_class, PyObject** py_class) @@ -675,7 +675,7 @@ int PyModule::load_subclass_of(const char* base_class, PyObject** py_class) if (PyObject_RichCompareBool(value, mgr_module_type, Py_EQ)) { continue; } - auto class_name = PyString_AsString(key); + auto class_name = PyUnicode_AsUTF8(key); if (*py_class) { derr << __func__ << ": ignoring '" << module_name << "." << class_name << "'" diff --git a/src/mgr/PyModuleRunner.cc b/src/mgr/PyModuleRunner.cc index c5408bd9da27..e27f7f405513 100644 --- a/src/mgr/PyModuleRunner.cc +++ b/src/mgr/PyModuleRunner.cc @@ -13,7 +13,7 @@ // Python.h comes first because otherwise it clobbers ceph's assert -#include "PythonCompat.h" +#include #include "PyModule.h" diff --git a/src/mgr/PyOSDMap.cc b/src/mgr/PyOSDMap.cc index 681546a8f7f8..a4715d673521 100644 --- a/src/mgr/PyOSDMap.cc +++ b/src/mgr/PyOSDMap.cc @@ -35,12 +35,12 @@ typedef struct { static PyObject *osdmap_get_epoch(BasePyOSDMap *self, PyObject *obj) { - return PyInt_FromLong(self->osdmap->get_epoch()); + return PyLong_FromLong(self->osdmap->get_epoch()); } static PyObject *osdmap_get_crush_version(BasePyOSDMap* self, PyObject *obj) { - return PyInt_FromLong(self->osdmap->get_crush_version()); + return PyLong_FromLong(self->osdmap->get_crush_version()); } static PyObject *osdmap_dump(BasePyOSDMap* self, PyObject *obj) @@ -128,14 +128,14 @@ static PyObject *osdmap_calc_pg_upmaps(BasePyOSDMap* self, PyObject *args) set pools; for (auto i = 0; i < PyList_Size(pool_list); ++i) { PyObject *pool_name = PyList_GET_ITEM(pool_list, i); - if (!PyString_Check(pool_name)) { + if (!PyUnicode_Check(pool_name)) { derr << __func__ << " " << pool_name << " not a string" << dendl; return nullptr; } auto pool_id = self->osdmap->lookup_pg_pool_name( - PyString_AsString(pool_name)); + PyUnicode_AsUTF8(pool_name)); if (pool_id < 0) { - derr << __func__ << " pool '" << PyString_AsString(pool_name) + derr << __func__ << " pool '" << PyUnicode_AsUTF8(pool_name) << "' does not exist" << dendl; return nullptr; } @@ -155,7 +155,7 @@ static PyObject *osdmap_calc_pg_upmaps(BasePyOSDMap* self, PyObject *args) incobj->inc); PyEval_RestoreThread(tstate); dout(10) << __func__ << " r = " << r << dendl; - return PyInt_FromLong(r); + return PyLong_FromLong(r); } static PyObject *osdmap_map_pool_pgs_up(BasePyOSDMap* self, PyObject *args) @@ -377,7 +377,7 @@ BasePyOSDMapIncremental_dealloc(BasePyOSDMapIncremental *self) static PyObject *osdmap_inc_get_epoch(BasePyOSDMapIncremental *self, PyObject *obj) { - return PyInt_FromLong(self->inc->epoch); + return PyLong_FromLong(self->inc->epoch); } static PyObject *osdmap_inc_dump(BasePyOSDMapIncremental *self, @@ -560,7 +560,7 @@ static PyObject *crush_get_item_name(BasePyCRUSH *self, PyObject *args) if (!self->crush->item_exists(item)) { Py_RETURN_NONE; } - return PyString_FromString(self->crush->get_item_name(item)); + return PyUnicode_FromString(self->crush->get_item_name(item)); } static PyObject *crush_get_item_weight(BasePyCRUSH *self, PyObject *args) diff --git a/src/mgr/PyOSDMap.h b/src/mgr/PyOSDMap.h index 9d737424913b..2cc30dfe2666 100644 --- a/src/mgr/PyOSDMap.h +++ b/src/mgr/PyOSDMap.h @@ -3,11 +3,9 @@ #pragma once -#include - -#include "PythonCompat.h" - +#include +#include extern PyTypeObject BasePyOSDMapType; extern PyTypeObject BasePyOSDMapIncrementalType; diff --git a/src/mgr/PyUtil.cc b/src/mgr/PyUtil.cc index 04a3a5d1fa52..bfecdb354230 100644 --- a/src/mgr/PyUtil.cc +++ b/src/mgr/PyUtil.cc @@ -1,8 +1,9 @@ // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- // vim: ts=8 sw=2 smarttab +#include + #include "PyUtil.h" -#include "PythonCompat.h" PyObject *get_python_typed_option_value( Option::type_t type, @@ -12,12 +13,12 @@ PyObject *get_python_typed_option_value( case Option::TYPE_INT: case Option::TYPE_UINT: case Option::TYPE_SIZE: - return PyInt_FromString((char *)value.c_str(), nullptr, 0); + return PyLong_FromString((char *)value.c_str(), nullptr, 0); case Option::TYPE_SECS: case Option::TYPE_FLOAT: { - PyObject *s = PyString_FromString(value.c_str()); - PyObject *f = PyFloat_FromString(s, nullptr); + PyObject *s = PyUnicode_FromString(value.c_str()); + PyObject *f = PyFloat_FromString(s); Py_DECREF(s); return f; } @@ -36,5 +37,5 @@ PyObject *get_python_typed_option_value( case Option::TYPE_UUID: break; } - return PyString_FromString(value.c_str()); + return PyUnicode_FromString(value.c_str()); } diff --git a/src/mgr/PyUtil.h b/src/mgr/PyUtil.h index 3d8bc7b984fd..188b3d28fc3b 100644 --- a/src/mgr/PyUtil.h +++ b/src/mgr/PyUtil.h @@ -5,7 +5,8 @@ #include -#include "Python.h" +#include + #include "common/options.h" PyObject *get_python_typed_option_value( diff --git a/src/mgr/PythonCompat.h b/src/mgr/PythonCompat.h deleted file mode 100644 index 14f98ef9c1c3..000000000000 --- a/src/mgr/PythonCompat.h +++ /dev/null @@ -1,58 +0,0 @@ -// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- -// vim: ts=8 sw=2 smarttab - -#pragma once - -#include -#include - -// Python's pyconfig-64.h conflicts with ceph's acconfig.h -#undef HAVE_SYS_WAIT_H -#undef HAVE_UNISTD_H -#undef HAVE_UTIME_H -#undef _POSIX_C_SOURCE -#undef _XOPEN_SOURCE - -#if PY_MAJOR_VERSION >= 3 -inline PyObject* PyString_FromString(const char *v) { - return PyUnicode_FromFormat("%s", 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); -} -inline PyObject* PyInt_FromLong(long ival) { - return PyLong_FromLong(ival); -} -inline int PyString_Check(PyObject *o) { - return PyUnicode_Check(o); -} -inline PyObject* PyFloat_FromString(PyObject *s, void *arg) { - return PyFloat_FromString(s); -} -inline PyObject* PyInt_FromString(const char *str, char **pend, int base) { - return PyLong_FromString(str, pend, base); -} -#define PyString_Type PyUnicode_Type -#endif - -inline std::pair PyString_ToString(PyObject *o) { -#if PY_MAJOR_VERSION >= 3 - if (PyUnicode_Check(o)) { - return {PyUnicode_AsUTF8(o), true}; - } else { - return {{}, false}; - } -#else - if (PyString_Check(o) || PyUnicode_Check(o)) { - return {PyString_AsString(o), true}; - } else { - return {{}, false}; - } -#endif -} diff --git a/src/mgr/StandbyPyModules.cc b/src/mgr/StandbyPyModules.cc index cff60aded2ab..62f063a14039 100644 --- a/src/mgr/StandbyPyModules.cc +++ b/src/mgr/StandbyPyModules.cc @@ -109,7 +109,7 @@ int StandbyPyModule::load() // with us in logging etc. auto pThisPtr = PyCapsule_New(this, nullptr, nullptr); ceph_assert(pThisPtr != nullptr); - auto pModuleName = PyString_FromString(get_name().c_str()); + auto pModuleName = PyUnicode_FromString(get_name().c_str()); ceph_assert(pModuleName != nullptr); auto pArgs = PyTuple_Pack(2, pModuleName, pThisPtr); Py_DECREF(pThisPtr); diff --git a/src/mgr/StandbyPyModules.h b/src/mgr/StandbyPyModules.h index d8d5e1a07ef6..b434c917c7ae 100644 --- a/src/mgr/StandbyPyModules.h +++ b/src/mgr/StandbyPyModules.h @@ -13,11 +13,11 @@ #pragma once -#include "PythonCompat.h" - #include #include +#include + #include "common/Thread.h" #include "common/ceph_mutex.h"