python.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. #include "python.hpp"
  2. #include <stdexcept>
  3. #include "Messages.hpp"
  4. py::error::error(py::object type, py::object value, py::object traceback)
  5. {
  6. this->type = type.get("__name__").to<std::string>();
  7. this->value = value;
  8. (void)traceback;
  9. if (value == none) {
  10. msg = this->type;
  11. } else {
  12. msg = value.to<std::string>() + " [" + this->type + "]";
  13. }
  14. }
  15. const char* py::error::what() const throw()
  16. {
  17. return msg.c_str();
  18. }
  19. py::object::object() : raw(Py_None) {}
  20. py::object::object(py::object&& move) : raw(move.raw)
  21. {
  22. move.raw = nullptr;
  23. }
  24. py::object& py::object::operator=(py::object&& move)
  25. {
  26. raw = move.raw;
  27. move.raw = nullptr;
  28. return *this;
  29. }
  30. py::object::~object()
  31. {
  32. QMutexLocker locker(&mutex);
  33. if (raw != nullptr && raw != Py_None) {
  34. Py_DECREF(raw);
  35. }
  36. }
  37. py::object::object(const py::object& copy)
  38. {
  39. QMutexLocker locker(&mutex);
  40. raw = copy.raw;
  41. Py_INCREF(raw);
  42. }
  43. py::object& py::object::operator=(const py::object& copy)
  44. {
  45. QMutexLocker locker(&mutex);
  46. raw = copy.raw;
  47. Py_INCREF(raw);
  48. return *this;
  49. }
  50. py::object::Iterator::Iterator(py::object iter)
  51. {
  52. this->iter = iter;
  53. ++(*this);
  54. }
  55. void py::object::Iterator::operator++()
  56. {
  57. try {
  58. v = iter.call("__next__");
  59. } catch (error& e) {
  60. if (e.type != "StopIteration") throw e;
  61. end = true;
  62. }
  63. }
  64. py::object& py::object::Iterator::operator*() { return v; }
  65. bool py::object::Iterator::operator!=(EndIterator) { return !end; }
  66. void py::fetchException() {
  67. PyObject *type, *value, *traceback;
  68. PyErr_Fetch(&type, &value, &traceback);
  69. if (type == nullptr) return;
  70. PyErr_Clear();
  71. throw error(type, value? value : nullptr, traceback? traceback : nullptr);
  72. }
  73. PyObject* py::maybe_exception(PyObject* a) {
  74. if (a == nullptr) {
  75. fetchException();
  76. }
  77. return a;
  78. }
  79. void py::maybe_exception(int a) {
  80. if (a != 0) {
  81. fetchException();
  82. }
  83. }
  84. PyObject* py::toPyObject(PyObject* o)
  85. {
  86. if (o == nullptr) {
  87. return Py_None;
  88. }
  89. Py_INCREF(o);
  90. return o;
  91. }
  92. PyObject* py::toPyObject(const char* s)
  93. {
  94. return PyUnicode_FromString(s);
  95. }
  96. PyObject* py::toPyObject(const std::string& s)
  97. {
  98. return PyUnicode_FromString(s.c_str());
  99. }
  100. PyObject* py::toPyObject(const QString& s)
  101. {
  102. return PyUnicode_FromString(s.toUtf8().data());
  103. }
  104. PyObject* py::toPyObject(const std::initializer_list<py::object>& tuple)
  105. {
  106. PyObject* ty = PyTuple_New(tuple.size());
  107. int i = 0;
  108. for (auto& e : tuple) {
  109. Py_INCREF(e.raw);
  110. PyTuple_SetItem(ty, i, e.raw);
  111. ++i;
  112. }
  113. return ty;
  114. }
  115. PyObject* py::toPyObject(long long v)
  116. {
  117. return PyLong_FromLongLong(v);
  118. }
  119. PyObject* py::toPyObject(int v)
  120. {
  121. return PyLong_FromLong(v);
  122. }
  123. PyObject* py::toPyObject(bool v)
  124. {
  125. return PyBool_FromLong(v);
  126. }
  127. PyObject* py::toPyObject(std::nullptr_t)
  128. {
  129. return Py_None; // TODO: nullptr
  130. }
  131. PyObject* py::toPyObject(py::none_t)
  132. {
  133. return Py_None;
  134. }
  135. PyObject* py::toPyObject(const std::vector<py::object>& v)
  136. {
  137. PyObject* res = PyList_New(v.size());
  138. int i = 0;
  139. for (auto& e : v) {
  140. Py_INCREF(e.raw);
  141. PyList_SetItem(res, i, e.raw);
  142. ++i;
  143. }
  144. return res;
  145. }
  146. PyObject* py::toPyObject(const std::map<std::string, py::object>& v)
  147. {
  148. PyObject* res = PyDict_New();
  149. for (auto& e : v) {
  150. Py_INCREF(e.second.raw);
  151. PyDict_SetItem(res, object(e.first).raw, e.second.raw);
  152. }
  153. return res;
  154. }
  155. void py::fromPyObject(const py::object& a, py::object& res)
  156. {
  157. Py_INCREF(a.raw);
  158. res.raw = a.raw;
  159. }
  160. void py::fromPyObject(const py::object& a, std::string& res)
  161. {
  162. if (a == none) {
  163. res = "";
  164. return;
  165. }
  166. auto repr = PyObject_Str(a.raw);
  167. if (!repr) repr = PyObject_Repr(a.raw);
  168. auto enc = PyUnicode_AsEncodedString(repr, "utf-8", "~E~");
  169. res = std::string(PyBytes_AS_STRING(enc));
  170. Py_DECREF(enc);
  171. Py_DECREF(repr);
  172. }
  173. void py::fromPyObject(const py::object& a, QString& res)
  174. {
  175. if (a == none) {
  176. res = "";
  177. return;
  178. }
  179. auto repr = PyObject_Str(a.raw);
  180. if (!repr) repr = PyObject_Repr(a.raw);
  181. auto enc = PyUnicode_AsEncodedString(repr, "utf-8", "~E~");
  182. res = QString(PyBytes_AS_STRING(enc));
  183. Py_DECREF(enc);
  184. Py_DECREF(repr);
  185. }
  186. void py::fromPyObject(const py::object& a, bool& res)
  187. {
  188. QMutexLocker locker(&mutex);
  189. res = PyObject_IsTrue(a.raw);
  190. }
  191. void py::fromPyObject(const py::object& a, int& res)
  192. {
  193. if (PyLong_Check(a.raw)) {
  194. res = PyLong_AsLong(a.raw);
  195. } else if (PyUnicode_Check(a.raw)) {
  196. auto p = PyLong_FromUnicodeObject(a.raw, 10);
  197. if (p) {
  198. res = PyLong_AsLong(p);
  199. Py_DECREF(p);
  200. }
  201. else res = 0;
  202. } else if (a == none || a == nullptr) {
  203. res = 0;
  204. } else {
  205. throw std::runtime_error("unimplemented cast to int (" + a.to<std::string>() + ")");
  206. }
  207. }
  208. void py::fromPyObject(const py::object& a, qint64& res)
  209. {
  210. if (PyLong_Check(a.raw)) {
  211. res = PyLong_AsLong(a.raw);
  212. } else if (PyUnicode_Check(a.raw)) {
  213. auto p = PyLong_FromUnicodeObject(a.raw, 10);
  214. if (p) {
  215. res = PyLong_AsLong(p);
  216. Py_DECREF(p);
  217. }
  218. else res = 0;
  219. } else if (a == none || a == nullptr) {
  220. res = 0;
  221. } else {
  222. throw std::runtime_error("unimplemented cast to qint64 (" + a.to<std::string>() + ")");
  223. }
  224. }
  225. py::object py::object::attr(py::object name) const
  226. {
  227. QMutexLocker locker(&mutex);
  228. return maybe_exception(PyObject_GetAttr(raw, name.raw));
  229. }
  230. bool py::object::has(py::object name) const
  231. {
  232. QMutexLocker locker(&mutex);
  233. return PyObject_HasAttr(raw, name.raw);
  234. }
  235. void py::object::set(py::object name, py::object value)
  236. {
  237. QMutexLocker locker(&mutex);
  238. maybe_exception(PyObject_SetAttr(raw, name.raw, value.raw));
  239. }
  240. void py::object::del_attr(py::object name)
  241. {
  242. QMutexLocker locker(&mutex);
  243. maybe_exception(PyObject_DelAttr(raw, name.raw));
  244. }
  245. py::object py::object::operator()(const std::initializer_list<py::object>& args) const
  246. {
  247. QMutexLocker locker(&mutex);
  248. return maybe_exception(PyObject_Call(raw, object(args).raw, nullptr));
  249. }
  250. py::object py::object::operator()(py::object arg) const
  251. {
  252. QMutexLocker locker(&mutex);
  253. return maybe_exception(PyObject_CallOneArg(raw, arg.raw));
  254. }
  255. py::object py::object::operator()() const
  256. {
  257. QMutexLocker locker(&mutex);
  258. return maybe_exception(PyObject_CallNoArgs(raw));
  259. }
  260. py::object py::object::operator()(const std::initializer_list<py::object>& args, const std::map<std::string, py::object>& kwargs) const
  261. {
  262. QMutexLocker locker(&mutex);
  263. return maybe_exception(PyObject_Call(raw, object(args).raw, object(kwargs).raw));
  264. }
  265. py::object py::object::operator()(py::object arg, const std::map<std::string, py::object>& kwargs) const
  266. {
  267. QMutexLocker locker(&mutex);
  268. return maybe_exception(PyObject_Call(raw, object(std::initializer_list<object>{arg}).raw, object(kwargs).raw));
  269. }
  270. py::object py::object::call(py::object name, const std::initializer_list<py::object>& args) const
  271. {
  272. return attr(name)(args);
  273. }
  274. py::object py::object::call(py::object name, py::object arg) const
  275. {
  276. return attr(name)(arg);
  277. }
  278. py::object py::object::call(py::object name) const
  279. {
  280. return attr(name)();
  281. }
  282. py::object py::object::call(py::object name, const std::initializer_list<py::object>& args, const std::map<std::string, py::object>& kwargs) const
  283. {
  284. return attr(name)(args, kwargs);
  285. }
  286. py::object py::object::call(py::object name, py::object arg, const std::map<std::string, py::object>& kwargs) const
  287. {
  288. return attr(name)(arg, kwargs);
  289. }
  290. py::object py::object::operator[](size_t i) const
  291. {
  292. QMutexLocker locker(&mutex);
  293. return maybe_exception(PySequence_GetItem(raw, i));
  294. }
  295. py::object py::object::contains(py::object a) const
  296. {
  297. return call("__contains__", a);
  298. }
  299. py::object py::object::len() const
  300. {
  301. return call("__len__");
  302. }
  303. py::object py::object::copy() const
  304. {
  305. return module("copy").call("copy", *this);
  306. }
  307. py::object py::object::deepcopy() const
  308. {
  309. return module("copy").call("deepcopy", *this);
  310. }
  311. py::object& py::object::print()
  312. {
  313. QMutexLocker locker(&mutex);
  314. PyObject_Print(raw, stdout, 0);
  315. return *this;
  316. }
  317. py::object& py::object::throw_repr()
  318. {
  319. throw std::runtime_error(to<std::string>());
  320. return *this;
  321. }
  322. py::object py::object::operator!()
  323. {
  324. QMutexLocker locker(&mutex);
  325. return PyObject_Not(raw);
  326. }
  327. py::object py::object::operator<(const py::object& a) const
  328. {
  329. return this->call("__lt__", a);
  330. }
  331. bool py::object::operator==(py::none_t) const
  332. {
  333. return raw == Py_None;
  334. }
  335. bool py::object::operator==(std::nullptr_t) const
  336. {
  337. return raw == nullptr;
  338. }
  339. bool py::object::operator!=(py::none_t) const
  340. {
  341. return raw != Py_None;
  342. }
  343. bool py::object::operator!=(std::nullptr_t) const
  344. {
  345. return raw != nullptr;
  346. }
  347. py::object::Iterator py::object::begin() const
  348. {
  349. return {call("__iter__")};
  350. }
  351. py::object::EndIterator py::object::end() const
  352. {
  353. return {};
  354. }
  355. py::module::~module()
  356. {
  357. raw = nullptr; // утечка памяти?
  358. }
  359. py::module::module(const py::module& copy) : object()
  360. {
  361. QMutexLocker locker(&mutex);
  362. raw = copy.raw;
  363. Py_INCREF(raw);
  364. }
  365. py::module::module(const char* name, bool autoInstall)
  366. {
  367. QMutexLocker locker(&mutex);
  368. raw = PyImport_ImportModule(name);
  369. if (raw == nullptr) {
  370. Messages::message(QObject::tr("Failed to import python module '%1', it will be auto-installed").arg(name));
  371. if (autoInstall) {
  372. if (module::autoInstall(name)) {
  373. raw = PyImport_ImportModule(name);
  374. if (raw != nullptr) return;
  375. }
  376. }
  377. throw std::runtime_error(std::string("python: can't import module '") + name + "'");
  378. }
  379. }
  380. py::module::module(py::object name, bool autoInstall) : module(name.to<std::string>().c_str(), autoInstall) {}
  381. py::module::module(PyObject* raw) : object(raw) {}
  382. py::module& py::module::operator=(const py::module& copy)
  383. {
  384. raw = copy.raw;
  385. return *this;
  386. }
  387. py::module py::module::submodule(py::object name)
  388. {
  389. return module(get(name).raw);
  390. }
  391. py::module py::module::main()
  392. {
  393. QMutexLocker locker(&mutex);
  394. return PyImport_AddModule("__main__");
  395. }
  396. bool py::module::autoInstall(py::object name)
  397. {
  398. try {
  399. auto pip = module("pip");
  400. pip.call("main", std::vector<object>{"install", name});
  401. return true;
  402. } catch (std::exception const& e) {
  403. Messages::error(QObject::tr("Failed to auto-install python module '%1'").arg(name), e.what());
  404. return false;
  405. }
  406. }