1 /*
2  * Copyright (C) 2012-2014 Red Hat, Inc.
3  *
4  * Licensed under the GNU Lesser General Public License Version 2.1
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef IUTIL_PY_H
22 #define IUTIL_PY_H
23 
24 #include <vector>
25 
26 #include "hy-types.h"
27 #include "dnf-sack.h"
28 #include "sack/advisorypkg.hpp"
29 #include "sack/changelog.hpp"
30 
31 #define TEST_COND(cond) \
32     ((cond) ? Py_True : Py_False)
33 
34 /**
35 * @brief Smart pointer to PyObject
36 *
37 * Owns and manages PyObject. Decrements the reference count for the PyObject
38 * (calls Py_XDECREF on it) when  UniquePtrPyObject goes out of scope.
39 *
40 * Implements subset of standard std::unique_ptr methods.
41 */
42 class UniquePtrPyObject {
43 public:
UniquePtrPyObject()44     constexpr UniquePtrPyObject() noexcept : pyObj(NULL) {}
UniquePtrPyObject(PyObject * pyObj)45     explicit UniquePtrPyObject(PyObject * pyObj) noexcept : pyObj(pyObj) {}
UniquePtrPyObject(UniquePtrPyObject && src)46     UniquePtrPyObject(UniquePtrPyObject && src) noexcept : pyObj(src.pyObj) { src.pyObj = NULL; }
47     UniquePtrPyObject & operator =(UniquePtrPyObject && src) noexcept;
operator bool() const48     explicit operator bool() const noexcept { return pyObj != NULL; }
get() const49     PyObject * get() const noexcept { return pyObj; }
50     PyObject * release() noexcept;
51     void reset(PyObject * pyObj = NULL) noexcept;
52     ~UniquePtrPyObject();
53 private:
54     PyObject * pyObj;
55 };
56 
release()57 inline PyObject * UniquePtrPyObject::release() noexcept
58 {
59     auto tmpObj = pyObj;
60     pyObj = NULL;
61     return tmpObj;
62 }
63 
64 std::vector<std::string> pySequenceConverter(PyObject * pySequence);
65 PyObject *advisorylist_to_pylist(const GPtrArray *advisorylist, PyObject *sack);
66 PyObject *advisoryPkgVectorToPylist(const std::vector<libdnf::AdvisoryPkg> & advisorypkgs);
67 PyObject *advisoryRefVectorToPylist(const std::vector<libdnf::AdvisoryRef> & advisoryRefs,
68                                     PyObject *sack);
69 PyObject *changelogslist_to_pylist(const std::vector<libdnf::Changelog> & changelogslist);
70 PyObject *packagelist_to_pylist(GPtrArray *plist, PyObject *sack);
71 PyObject * packageset_to_pylist(const DnfPackageSet * pset, PyObject * sack);
72 std::unique_ptr<DnfPackageSet> pyseq_to_packageset(PyObject * sequence, DnfSack * sack);
73 std::unique_ptr<DnfReldepList> pyseq_to_reldeplist(PyObject *sequence, DnfSack *sack, int cmp_type);
74 PyObject *strlist_to_pylist(const char **slist);
75 PyObject * strCpplist_to_pylist(const std::vector<std::string> & cppList);
76 PyObject *reldeplist_to_pylist(DnfReldepList *reldeplist, PyObject *sack);
77 PyObject * problemRulesPyConverter(std::vector<std::vector<std::string>> & allProblems);
78 
79 #endif // IUTIL_PY_H
80