Commit cefa7ec2 authored by frmdstryr's avatar frmdstryr Committed by Sven Brauch
Browse files

Add slice and use PyObjectRef instead of decrefs

parent 7026ff59
......@@ -52,17 +52,6 @@ QString PyUnicodeObjectToQString(PyObject* obj) {
Q_UNREACHABLE();
}
class PyObjectRef {
public:
PyObjectRef(PyObject* py_obj): obj(py_obj) {}
PyObject* get() {return obj;}
~PyObjectRef() {
Py_XDECREF(obj);
obj = nullptr;
}
PyObject* obj = nullptr;
};
struct PythonParser : private QMutexLocker
{
PyObject* m_parser_mod = nullptr;
......@@ -87,14 +76,14 @@ struct PythonParser : private QMutexLocker
QString supportDir = parserFile.absoluteDir().path();
Q_ASSERT(supportDir.size());
PyObjectRef sys = PyImport_ImportModule("sys");
if (!sys.get()) return;
PyObjectRef path = PyObject_GetAttrString(sys.get(), "path");
if (!path.get()) return;
PyObjectRef append = PyObject_GetAttrString(path.get(), "append");
if (!append.get()) return;
if (!sys) return;
PyObjectRef path = PyObject_GetAttrString(sys, "path");
if (!path) return;
PyObjectRef append = PyObject_GetAttrString(path, "append");
if (!append) return;
PyObjectRef arg = PyUnicode_FromString(supportDir.toUtf8().data());
if (!arg.get()) return;
PyObjectRef r = PyObject_CallOneArg(append.get(), arg.get());
if (!arg) return;
PyObjectRef r = PyObject_CallOneArg(append, arg);
}
// Call parser function and return the python ast.Module.
......
This diff is collapsed.
......@@ -5,6 +5,20 @@
namespace Python
{
class PyObjectRef {
public:
PyObjectRef(PyObject* py_obj): obj(py_obj) {}
operator PyObject*() const {
return obj;
}
~PyObjectRef() {
Py_XDECREF(obj);
obj = nullptr;
}
PyObject* obj = nullptr;
};
class AstTransformer {
public:
CodeAst* ast;
......
......@@ -148,6 +148,9 @@ public:
// withitem
PyObject* ast_withitem;
// slice
PyObject* ast_slice;
#if PYTHON_VERSION >= QT_VERSION_CHECK(3, 10, 0)
// match_case
PyObject* ast_match_case;
......@@ -295,6 +298,7 @@ public:
Py_GRAMMAR_GET(mod, keyword);
Py_GRAMMAR_GET(mod, alias);
Py_GRAMMAR_GET(mod, withitem);
Py_GRAMMAR_GET(mod, slice);
#if PYTHON_VERSION >= QT_VERSION_CHECK(3, 10, 0)
Py_GRAMMAR_GET(mod, match_case);
......@@ -437,6 +441,7 @@ public:
Py_XDECREF(ast_keyword);
Py_XDECREF(ast_alias);
Py_XDECREF(ast_withitem);
Py_XDECREF(ast_slice);
#if PYTHON_VERSION >= QT_VERSION_CHECK(3, 10, 0)
Py_XDECREF(ast_match_case);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment