greenlet_refs.hpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. #ifndef GREENLET_REFS_HPP
  2. #define GREENLET_REFS_HPP
  3. #define PY_SSIZE_T_CLEAN
  4. #include <Python.h>
  5. #include <string>
  6. //#include "greenlet_internal.hpp"
  7. #include "greenlet_compiler_compat.hpp"
  8. #include "greenlet_cpython_compat.hpp"
  9. #include "greenlet_exceptions.hpp"
  10. struct _greenlet;
  11. struct _PyMainGreenlet;
  12. typedef struct _greenlet PyGreenlet;
  13. extern PyTypeObject PyGreenlet_Type;
  14. #ifdef GREENLET_USE_STDIO
  15. #include <iostream>
  16. using std::cerr;
  17. using std::endl;
  18. #endif
  19. namespace greenlet
  20. {
  21. class Greenlet;
  22. namespace refs
  23. {
  24. // Type checkers throw a TypeError if the argument is not
  25. // null, and isn't of the required Python type.
  26. // (We can't use most of the defined type checkers
  27. // like PyList_Check, etc, directly, because they are
  28. // implemented as macros.)
  29. typedef void (*TypeChecker)(void*);
  30. void
  31. NoOpChecker(void*)
  32. {
  33. return;
  34. }
  35. void
  36. GreenletChecker(void *p)
  37. {
  38. if (!p) {
  39. return;
  40. }
  41. PyTypeObject* typ = Py_TYPE(p);
  42. // fast, common path. (PyObject_TypeCheck is a macro or
  43. // static inline function, and it also does a
  44. // direct comparison of the type pointers, but its fast
  45. // path only handles one type)
  46. if (typ == &PyGreenlet_Type) {
  47. return;
  48. }
  49. if (!PyObject_TypeCheck(p, &PyGreenlet_Type)) {
  50. std::string err("GreenletChecker: Expected any type of greenlet, not ");
  51. err += Py_TYPE(p)->tp_name;
  52. throw TypeError(err);
  53. }
  54. }
  55. void
  56. MainGreenletExactChecker(void *p);
  57. template <typename T, TypeChecker>
  58. class PyObjectPointer;
  59. template<typename T, TypeChecker>
  60. class OwnedReference;
  61. template<typename T, TypeChecker>
  62. class BorrowedReference;
  63. typedef BorrowedReference<PyObject, NoOpChecker> BorrowedObject;
  64. typedef OwnedReference<PyObject, NoOpChecker> OwnedObject;
  65. class ImmortalObject;
  66. class ImmortalString;
  67. template<typename T, TypeChecker TC>
  68. class _OwnedGreenlet;
  69. typedef _OwnedGreenlet<PyGreenlet, GreenletChecker> OwnedGreenlet;
  70. typedef _OwnedGreenlet<PyGreenlet, MainGreenletExactChecker> OwnedMainGreenlet;
  71. template<typename T, TypeChecker TC>
  72. class _BorrowedGreenlet;
  73. typedef _BorrowedGreenlet<PyGreenlet, GreenletChecker> BorrowedGreenlet;
  74. void
  75. ContextExactChecker(void *p)
  76. {
  77. if (!p) {
  78. return;
  79. }
  80. if (!PyContext_CheckExact(p)) {
  81. throw TypeError(
  82. "greenlet context must be a contextvars.Context or None"
  83. );
  84. }
  85. }
  86. typedef OwnedReference<PyObject, ContextExactChecker> OwnedContext;
  87. }
  88. }
  89. namespace greenlet {
  90. namespace refs {
  91. // A set of classes to make reference counting rules in python
  92. // code explicit.
  93. //
  94. // Rules of use:
  95. // (1) Functions returning a new reference that the caller of the
  96. // function is expected to dispose of should return a
  97. // ``OwnedObject`` object. This object automatically releases its
  98. // reference when it goes out of scope. It works like a ``std::shared_ptr``
  99. // and can be copied or used as a function parameter (but don't do
  100. // that). Note that constructing a ``OwnedObject`` from a
  101. // PyObject* steals the reference.
  102. // (2) Parameters to functions should be either a
  103. // ``OwnedObject&``, or, more generally, a ``PyObjectPointer&``.
  104. // If the function needs to create its own new reference, it can
  105. // do so by copying to a local ``OwnedObject``.
  106. // (3) Functions returning an existing pointer that is NOT
  107. // incref'd, and which the caller MUST NOT decref,
  108. // should return a ``BorrowedObject``.
  109. // XXX: The following two paragraphs do not hold for all platforms.
  110. // Notably, 32-bit PPC Linux passes structs by reference, not by
  111. // value, so this actually doesn't work. (Although that's the only
  112. // platform that doesn't work on.) DO NOT ATTEMPT IT. The
  113. // unfortunate consequence of that is that the slots which we
  114. // *know* are already type safe will wind up calling the type
  115. // checker function (when we had the slots accepting
  116. // BorrowedGreenlet, this was bypassed), so this slows us down.
  117. // TODO: Optimize this again.
  118. // For a class with a single pointer member, whose constructor
  119. // does nothing but copy a pointer parameter into the member, and
  120. // which can then be converted back to the pointer type, compilers
  121. // generate code that's the same as just passing the pointer.
  122. // That is, func(BorrowedObject x) called like ``PyObject* p =
  123. // ...; f(p)`` has 0 overhead. Similarly, they "unpack" to the
  124. // pointer type with 0 overhead.
  125. //
  126. // If there are no virtual functions, no complex inheritance (maybe?) and
  127. // no destructor, these can be directly used as parameters in
  128. // Python callbacks like tp_init: the layout is the same as a
  129. // single pointer. Only subclasses with trivial constructors that
  130. // do nothing but set the single pointer member are safe to use
  131. // that way.
  132. // This is the base class for things that can be done with a
  133. // PyObject pointer. It assumes nothing about memory management.
  134. // NOTE: Nothing is virtual, so subclasses shouldn't add new
  135. // storage fields or try to override these methods.
  136. template <typename T=PyObject, TypeChecker TC=NoOpChecker>
  137. class PyObjectPointer
  138. {
  139. public:
  140. typedef T PyType;
  141. protected:
  142. T* p;
  143. public:
  144. PyObjectPointer(T* it=nullptr) : p(it)
  145. {
  146. TC(p);
  147. }
  148. // We don't allow automatic casting to PyObject* at this
  149. // level, because then we could be passed to Py_DECREF/INCREF,
  150. // but we want nothing to do with memory management. If you
  151. // know better, then you can use the get() method, like on a
  152. // std::shared_ptr. Except we name it borrow() to clarify that
  153. // if this is a reference-tracked object, the pointer you get
  154. // back will go away when the object does.
  155. // TODO: This should probably not exist here, but be moved
  156. // down to relevant sub-types.
  157. T* borrow() const noexcept
  158. {
  159. return this->p;
  160. }
  161. PyObject* borrow_o() const noexcept
  162. {
  163. return reinterpret_cast<PyObject*>(this->p);
  164. }
  165. T* operator->() const noexcept
  166. {
  167. return this->p;
  168. }
  169. bool is_None() const noexcept
  170. {
  171. return this->p == Py_None;
  172. }
  173. PyObject* acquire_or_None() const noexcept
  174. {
  175. PyObject* result = this->p ? reinterpret_cast<PyObject*>(this->p) : Py_None;
  176. Py_INCREF(result);
  177. return result;
  178. }
  179. explicit operator bool() const noexcept
  180. {
  181. return this->p != nullptr;
  182. }
  183. bool operator!() const noexcept
  184. {
  185. return this->p == nullptr;
  186. }
  187. Py_ssize_t REFCNT() const noexcept
  188. {
  189. return p ? Py_REFCNT(p) : -42;
  190. }
  191. PyTypeObject* TYPE() const noexcept
  192. {
  193. return p ? Py_TYPE(p) : nullptr;
  194. }
  195. inline OwnedObject PyStr() const noexcept;
  196. inline const std::string as_str() const noexcept;
  197. inline OwnedObject PyGetAttr(const ImmortalObject& name) const noexcept;
  198. inline OwnedObject PyRequireAttr(const char* const name) const;
  199. inline OwnedObject PyRequireAttr(const ImmortalString& name) const;
  200. inline OwnedObject PyCall(const BorrowedObject& arg) const;
  201. inline OwnedObject PyCall(PyGreenlet* arg) const ;
  202. inline OwnedObject PyCall(PyObject* arg) const ;
  203. // PyObject_Call(this, args, kwargs);
  204. inline OwnedObject PyCall(const BorrowedObject args,
  205. const BorrowedObject kwargs) const;
  206. inline OwnedObject PyCall(const OwnedObject& args,
  207. const OwnedObject& kwargs) const;
  208. protected:
  209. void _set_raw_pointer(void* t)
  210. {
  211. TC(t);
  212. p = reinterpret_cast<T*>(t);
  213. }
  214. void* _get_raw_pointer() const
  215. {
  216. return p;
  217. }
  218. };
  219. #ifdef GREENLET_USE_STDIO
  220. template<typename T, TypeChecker TC>
  221. std::ostream& operator<<(std::ostream& os, const PyObjectPointer<T, TC>& s)
  222. {
  223. const std::type_info& t = typeid(s);
  224. os << t.name()
  225. << "(addr=" << s.borrow()
  226. << ", refcnt=" << s.REFCNT()
  227. << ", value=" << s.as_str()
  228. << ")";
  229. return os;
  230. }
  231. #endif
  232. template<typename T, TypeChecker TC>
  233. inline bool operator==(const PyObjectPointer<T, TC>& lhs, const PyObject* const rhs) noexcept
  234. {
  235. return static_cast<const void*>(lhs.borrow_o()) == static_cast<const void*>(rhs);
  236. }
  237. template<typename T, TypeChecker TC, typename X, TypeChecker XC>
  238. inline bool operator==(const PyObjectPointer<T, TC>& lhs, const PyObjectPointer<X, XC>& rhs) noexcept
  239. {
  240. return lhs.borrow_o() == rhs.borrow_o();
  241. }
  242. template<typename T, TypeChecker TC, typename X, TypeChecker XC>
  243. inline bool operator!=(const PyObjectPointer<T, TC>& lhs,
  244. const PyObjectPointer<X, XC>& rhs) noexcept
  245. {
  246. return lhs.borrow_o() != rhs.borrow_o();
  247. }
  248. template<typename T=PyObject, TypeChecker TC=NoOpChecker>
  249. class OwnedReference : public PyObjectPointer<T, TC>
  250. {
  251. private:
  252. friend class OwnedList;
  253. protected:
  254. explicit OwnedReference(T* it) : PyObjectPointer<T, TC>(it)
  255. {
  256. }
  257. public:
  258. // Constructors
  259. static OwnedReference<T, TC> consuming(PyObject* p)
  260. {
  261. return OwnedReference<T, TC>(reinterpret_cast<T*>(p));
  262. }
  263. static OwnedReference<T, TC> owning(T* p)
  264. {
  265. OwnedReference<T, TC> result(p);
  266. Py_XINCREF(result.p);
  267. return result;
  268. }
  269. OwnedReference() : PyObjectPointer<T, TC>(nullptr)
  270. {}
  271. explicit OwnedReference(const PyObjectPointer<>& other)
  272. : PyObjectPointer<T, TC>(nullptr)
  273. {
  274. T* op = other.borrow();
  275. TC(op);
  276. this->p = other.borrow();
  277. Py_XINCREF(this->p);
  278. }
  279. // It would be good to make use of the C++11 distinction
  280. // between move and copy operations, e.g., constructing from a
  281. // pointer should be a move operation.
  282. // In the common case of ``OwnedObject x = Py_SomeFunction()``,
  283. // the call to the copy constructor will be elided completely.
  284. OwnedReference(const OwnedReference<T, TC>& other)
  285. : PyObjectPointer<T, TC>(other.p)
  286. {
  287. Py_XINCREF(this->p);
  288. }
  289. static OwnedReference<PyObject> None()
  290. {
  291. Py_INCREF(Py_None);
  292. return OwnedReference<PyObject>(Py_None);
  293. }
  294. // We can assign from exactly our type without any extra checking
  295. OwnedReference<T, TC>& operator=(const OwnedReference<T, TC>& other)
  296. {
  297. Py_XINCREF(other.p);
  298. const T* tmp = this->p;
  299. this->p = other.p;
  300. Py_XDECREF(tmp);
  301. return *this;
  302. }
  303. OwnedReference<T, TC>& operator=(const BorrowedReference<T, TC> other)
  304. {
  305. return this->operator=(other.borrow());
  306. }
  307. OwnedReference<T, TC>& operator=(T* const other)
  308. {
  309. TC(other);
  310. Py_XINCREF(other);
  311. T* tmp = this->p;
  312. this->p = other;
  313. Py_XDECREF(tmp);
  314. return *this;
  315. }
  316. // We can assign from an arbitrary reference type
  317. // if it passes our check.
  318. template<typename X, TypeChecker XC>
  319. OwnedReference<T, TC>& operator=(const OwnedReference<X, XC>& other)
  320. {
  321. X* op = other.borrow();
  322. TC(op);
  323. return this->operator=(reinterpret_cast<T*>(op));
  324. }
  325. inline void steal(T* other)
  326. {
  327. assert(this->p == nullptr);
  328. TC(other);
  329. this->p = other;
  330. }
  331. T* relinquish_ownership()
  332. {
  333. T* result = this->p;
  334. this->p = nullptr;
  335. return result;
  336. }
  337. T* acquire() const
  338. {
  339. // Return a new reference.
  340. // TODO: This may go away when we have reference objects
  341. // throughout the code.
  342. Py_XINCREF(this->p);
  343. return this->p;
  344. }
  345. // Nothing else declares a destructor, we're the leaf, so we
  346. // should be able to get away without virtual.
  347. ~OwnedReference()
  348. {
  349. Py_CLEAR(this->p);
  350. }
  351. void CLEAR()
  352. {
  353. Py_CLEAR(this->p);
  354. assert(this->p == nullptr);
  355. }
  356. };
  357. static inline
  358. void operator<<=(PyObject*& target, OwnedObject& o)
  359. {
  360. target = o.relinquish_ownership();
  361. }
  362. class NewReference : public OwnedObject
  363. {
  364. private:
  365. G_NO_COPIES_OF_CLS(NewReference);
  366. public:
  367. // Consumes the reference. Only use this
  368. // for API return values.
  369. NewReference(PyObject* it) : OwnedObject(it)
  370. {
  371. }
  372. };
  373. class NewDictReference : public NewReference
  374. {
  375. private:
  376. G_NO_COPIES_OF_CLS(NewDictReference);
  377. public:
  378. NewDictReference() : NewReference(PyDict_New())
  379. {
  380. if (!this->p) {
  381. throw PyErrOccurred();
  382. }
  383. }
  384. void SetItem(const char* const key, PyObject* value)
  385. {
  386. Require(PyDict_SetItemString(this->p, key, value));
  387. }
  388. void SetItem(const PyObjectPointer<>& key, PyObject* value)
  389. {
  390. Require(PyDict_SetItem(this->p, key.borrow_o(), value));
  391. }
  392. };
  393. template<typename T=PyGreenlet, TypeChecker TC=GreenletChecker>
  394. class _OwnedGreenlet: public OwnedReference<T, TC>
  395. {
  396. private:
  397. protected:
  398. _OwnedGreenlet(T* it) : OwnedReference<T, TC>(it)
  399. {}
  400. public:
  401. _OwnedGreenlet() : OwnedReference<T, TC>()
  402. {}
  403. _OwnedGreenlet(const _OwnedGreenlet<T, TC>& other) : OwnedReference<T, TC>(other)
  404. {
  405. }
  406. _OwnedGreenlet(OwnedMainGreenlet& other) :
  407. OwnedReference<T, TC>(reinterpret_cast<T*>(other.acquire()))
  408. {
  409. }
  410. _OwnedGreenlet(const BorrowedGreenlet& other);
  411. // Steals a reference.
  412. static _OwnedGreenlet<T, TC> consuming(PyGreenlet* it)
  413. {
  414. return _OwnedGreenlet<T, TC>(reinterpret_cast<T*>(it));
  415. }
  416. inline _OwnedGreenlet<T, TC>& operator=(const OwnedGreenlet& other)
  417. {
  418. return this->operator=(other.borrow());
  419. }
  420. inline _OwnedGreenlet<T, TC>& operator=(const BorrowedGreenlet& other);
  421. _OwnedGreenlet<T, TC>& operator=(const OwnedMainGreenlet& other)
  422. {
  423. PyGreenlet* owned = other.acquire();
  424. Py_XDECREF(this->p);
  425. this->p = reinterpret_cast<T*>(owned);
  426. return *this;
  427. }
  428. _OwnedGreenlet<T, TC>& operator=(T* const other)
  429. {
  430. OwnedReference<T, TC>::operator=(other);
  431. return *this;
  432. }
  433. T* relinquish_ownership()
  434. {
  435. T* result = this->p;
  436. this->p = nullptr;
  437. return result;
  438. }
  439. PyObject* relinquish_ownership_o()
  440. {
  441. return reinterpret_cast<PyObject*>(relinquish_ownership());
  442. }
  443. inline Greenlet* operator->() const noexcept;
  444. inline operator Greenlet*() const noexcept;
  445. };
  446. template <typename T=PyObject, TypeChecker TC=NoOpChecker>
  447. class BorrowedReference : public PyObjectPointer<T, TC>
  448. {
  449. public:
  450. // Allow implicit creation from PyObject* pointers as we
  451. // transition to using these classes. Also allow automatic
  452. // conversion to PyObject* for passing to C API calls and even
  453. // for Py_INCREF/DECREF, because we ourselves do no memory management.
  454. BorrowedReference(T* it) : PyObjectPointer<T, TC>(it)
  455. {}
  456. BorrowedReference(const PyObjectPointer<T>& ref) : PyObjectPointer<T, TC>(ref.borrow())
  457. {}
  458. BorrowedReference() : PyObjectPointer<T, TC>(nullptr)
  459. {}
  460. operator T*() const
  461. {
  462. return this->p;
  463. }
  464. };
  465. typedef BorrowedReference<PyObject> BorrowedObject;
  466. //typedef BorrowedReference<PyGreenlet> BorrowedGreenlet;
  467. template<typename T=PyGreenlet, TypeChecker TC=GreenletChecker>
  468. class _BorrowedGreenlet : public BorrowedReference<T, TC>
  469. {
  470. public:
  471. _BorrowedGreenlet() :
  472. BorrowedReference<T, TC>(nullptr)
  473. {}
  474. _BorrowedGreenlet(T* it) :
  475. BorrowedReference<T, TC>(it)
  476. {}
  477. _BorrowedGreenlet(const BorrowedObject& it);
  478. _BorrowedGreenlet(const OwnedGreenlet& it) :
  479. BorrowedReference<T, TC>(it.borrow())
  480. {}
  481. _BorrowedGreenlet<T, TC>& operator=(const BorrowedObject& other);
  482. // We get one of these for PyGreenlet, but one for PyObject
  483. // is handy as well
  484. operator PyObject*() const
  485. {
  486. return reinterpret_cast<PyObject*>(this->p);
  487. }
  488. Greenlet* operator->() const noexcept;
  489. operator Greenlet*() const noexcept;
  490. };
  491. typedef _BorrowedGreenlet<PyGreenlet> BorrowedGreenlet;
  492. template<typename T, TypeChecker TC>
  493. _OwnedGreenlet<T, TC>::_OwnedGreenlet(const BorrowedGreenlet& other)
  494. : OwnedReference<T, TC>(reinterpret_cast<T*>(other.borrow()))
  495. {
  496. Py_XINCREF(this->p);
  497. }
  498. class BorrowedMainGreenlet
  499. : public _BorrowedGreenlet<PyGreenlet, MainGreenletExactChecker>
  500. {
  501. public:
  502. BorrowedMainGreenlet(const OwnedMainGreenlet& it) :
  503. _BorrowedGreenlet<PyGreenlet, MainGreenletExactChecker>(it.borrow())
  504. {}
  505. BorrowedMainGreenlet(PyGreenlet* it=nullptr)
  506. : _BorrowedGreenlet<PyGreenlet, MainGreenletExactChecker>(it)
  507. {}
  508. };
  509. template<typename T, TypeChecker TC>
  510. _OwnedGreenlet<T, TC>& _OwnedGreenlet<T, TC>::operator=(const BorrowedGreenlet& other)
  511. {
  512. return this->operator=(other.borrow());
  513. }
  514. class ImmortalObject : public PyObjectPointer<>
  515. {
  516. private:
  517. G_NO_ASSIGNMENT_OF_CLS(ImmortalObject);
  518. public:
  519. explicit ImmortalObject(PyObject* it) : PyObjectPointer<>(it)
  520. {
  521. }
  522. ImmortalObject(const ImmortalObject& other)
  523. : PyObjectPointer<>(other.p)
  524. {
  525. }
  526. /**
  527. * Become the new owner of the object. Does not change the
  528. * reference count.
  529. */
  530. ImmortalObject& operator=(PyObject* it)
  531. {
  532. assert(this->p == nullptr);
  533. this->p = it;
  534. return *this;
  535. }
  536. static ImmortalObject consuming(PyObject* it)
  537. {
  538. return ImmortalObject(it);
  539. }
  540. inline operator PyObject*() const
  541. {
  542. return this->p;
  543. }
  544. };
  545. class ImmortalString : public ImmortalObject
  546. {
  547. private:
  548. G_NO_COPIES_OF_CLS(ImmortalString);
  549. const char* str;
  550. public:
  551. ImmortalString(const char* const str) :
  552. ImmortalObject(str ? Require(PyUnicode_InternFromString(str)) : nullptr)
  553. {
  554. this->str = str;
  555. }
  556. inline ImmortalString& operator=(const char* const str)
  557. {
  558. if (!this->p) {
  559. this->p = Require(PyUnicode_InternFromString(str));
  560. this->str = str;
  561. }
  562. else {
  563. assert(this->str == str);
  564. }
  565. return *this;
  566. }
  567. inline operator std::string() const
  568. {
  569. return this->str;
  570. }
  571. };
  572. class ImmortalEventName : public ImmortalString
  573. {
  574. private:
  575. G_NO_COPIES_OF_CLS(ImmortalEventName);
  576. public:
  577. ImmortalEventName(const char* const str) : ImmortalString(str)
  578. {}
  579. };
  580. class ImmortalException : public ImmortalObject
  581. {
  582. private:
  583. G_NO_COPIES_OF_CLS(ImmortalException);
  584. public:
  585. ImmortalException(const char* const name, PyObject* base=nullptr) :
  586. ImmortalObject(name
  587. // Python 2.7 isn't const correct
  588. ? Require(PyErr_NewException((char*)name, base, nullptr))
  589. : nullptr)
  590. {}
  591. inline bool PyExceptionMatches() const
  592. {
  593. return PyErr_ExceptionMatches(this->p) > 0;
  594. }
  595. };
  596. template<typename T, TypeChecker TC>
  597. inline OwnedObject PyObjectPointer<T, TC>::PyStr() const noexcept
  598. {
  599. if (!this->p) {
  600. return OwnedObject();
  601. }
  602. return OwnedObject::consuming(PyObject_Str(reinterpret_cast<PyObject*>(this->p)));
  603. }
  604. template<typename T, TypeChecker TC>
  605. inline const std::string PyObjectPointer<T, TC>::as_str() const noexcept
  606. {
  607. // NOTE: This is not Python exception safe.
  608. if (this->p) {
  609. // The Python APIs return a cached char* value that's only valid
  610. // as long as the original object stays around, and we're
  611. // about to (probably) toss it. Hence the copy to std::string.
  612. OwnedObject py_str = this->PyStr();
  613. if (!py_str) {
  614. return "(nil)";
  615. }
  616. return PyUnicode_AsUTF8(py_str.borrow());
  617. }
  618. return "(nil)";
  619. }
  620. template<typename T, TypeChecker TC>
  621. inline OwnedObject PyObjectPointer<T, TC>::PyGetAttr(const ImmortalObject& name) const noexcept
  622. {
  623. assert(this->p);
  624. return OwnedObject::consuming(PyObject_GetAttr(reinterpret_cast<PyObject*>(this->p), name));
  625. }
  626. template<typename T, TypeChecker TC>
  627. inline OwnedObject PyObjectPointer<T, TC>::PyRequireAttr(const char* const name) const
  628. {
  629. assert(this->p);
  630. return OwnedObject::consuming(Require(PyObject_GetAttrString(this->p, name), name));
  631. }
  632. template<typename T, TypeChecker TC>
  633. inline OwnedObject PyObjectPointer<T, TC>::PyRequireAttr(const ImmortalString& name) const
  634. {
  635. assert(this->p);
  636. return OwnedObject::consuming(Require(
  637. PyObject_GetAttr(
  638. reinterpret_cast<PyObject*>(this->p),
  639. name
  640. ),
  641. name
  642. ));
  643. }
  644. template<typename T, TypeChecker TC>
  645. inline OwnedObject PyObjectPointer<T, TC>::PyCall(const BorrowedObject& arg) const
  646. {
  647. return this->PyCall(arg.borrow());
  648. }
  649. template<typename T, TypeChecker TC>
  650. inline OwnedObject PyObjectPointer<T, TC>::PyCall(PyGreenlet* arg) const
  651. {
  652. return this->PyCall(reinterpret_cast<PyObject*>(arg));
  653. }
  654. template<typename T, TypeChecker TC>
  655. inline OwnedObject PyObjectPointer<T, TC>::PyCall(PyObject* arg) const
  656. {
  657. assert(this->p);
  658. return OwnedObject::consuming(PyObject_CallFunctionObjArgs(this->p, arg, NULL));
  659. }
  660. template<typename T, TypeChecker TC>
  661. inline OwnedObject PyObjectPointer<T, TC>::PyCall(const BorrowedObject args,
  662. const BorrowedObject kwargs) const
  663. {
  664. assert(this->p);
  665. return OwnedObject::consuming(PyObject_Call(this->p, args, kwargs));
  666. }
  667. template<typename T, TypeChecker TC>
  668. inline OwnedObject PyObjectPointer<T, TC>::PyCall(const OwnedObject& args,
  669. const OwnedObject& kwargs) const
  670. {
  671. assert(this->p);
  672. return OwnedObject::consuming(PyObject_Call(this->p, args.borrow(), kwargs.borrow()));
  673. }
  674. inline void
  675. ListChecker(void * p)
  676. {
  677. if (!p) {
  678. return;
  679. }
  680. if (!PyList_Check(p)) {
  681. throw TypeError("Expected a list");
  682. }
  683. }
  684. class OwnedList : public OwnedReference<PyObject, ListChecker>
  685. {
  686. private:
  687. G_NO_ASSIGNMENT_OF_CLS(OwnedList);
  688. public:
  689. // TODO: Would like to use move.
  690. explicit OwnedList(const OwnedObject& other)
  691. : OwnedReference<PyObject, ListChecker>(other)
  692. {
  693. }
  694. OwnedList& operator=(const OwnedObject& other)
  695. {
  696. if (other && PyList_Check(other.p)) {
  697. // Valid list. Own a new reference to it, discard the
  698. // reference to what we did own.
  699. PyObject* new_ptr = other.p;
  700. Py_INCREF(new_ptr);
  701. Py_XDECREF(this->p);
  702. this->p = new_ptr;
  703. }
  704. else {
  705. // Either the other object was NULL (an error) or it
  706. // wasn't a list. Either way, we're now invalidated.
  707. Py_XDECREF(this->p);
  708. this->p = nullptr;
  709. }
  710. return *this;
  711. }
  712. inline bool empty() const
  713. {
  714. return PyList_GET_SIZE(p) == 0;
  715. }
  716. inline Py_ssize_t size() const
  717. {
  718. return PyList_GET_SIZE(p);
  719. }
  720. inline BorrowedObject at(const Py_ssize_t index) const
  721. {
  722. return PyList_GET_ITEM(p, index);
  723. }
  724. inline void clear()
  725. {
  726. PyList_SetSlice(p, 0, PyList_GET_SIZE(p), NULL);
  727. }
  728. };
  729. // Use this to represent the module object used at module init
  730. // time.
  731. // This could either be a borrowed (Py2) or new (Py3) reference;
  732. // either way, we don't want to do any memory management
  733. // on it here, Python itself will handle that.
  734. // XXX: Actually, that's not quite right. On Python 3, if an
  735. // exception occurs before we return to the interpreter, this will
  736. // leak; but all previous versions also had that problem.
  737. class CreatedModule : public PyObjectPointer<>
  738. {
  739. private:
  740. G_NO_COPIES_OF_CLS(CreatedModule);
  741. public:
  742. CreatedModule(PyModuleDef& mod_def) : PyObjectPointer<>(
  743. Require(PyModule_Create(&mod_def)))
  744. {
  745. }
  746. // PyAddObject(): Add a reference to the object to the module.
  747. // On return, the reference count of the object is unchanged.
  748. //
  749. // The docs warn that PyModule_AddObject only steals the
  750. // reference on success, so if it fails after we've incref'd
  751. // or allocated, we're responsible for the decref.
  752. void PyAddObject(const char* name, const long new_bool)
  753. {
  754. OwnedObject p = OwnedObject::consuming(Require(PyBool_FromLong(new_bool)));
  755. this->PyAddObject(name, p);
  756. }
  757. void PyAddObject(const char* name, const OwnedObject& new_object)
  758. {
  759. // The caller already owns a reference they will decref
  760. // when their variable goes out of scope, we still need to
  761. // incref/decref.
  762. this->PyAddObject(name, new_object.borrow());
  763. }
  764. void PyAddObject(const char* name, const ImmortalObject& new_object)
  765. {
  766. this->PyAddObject(name, new_object.borrow());
  767. }
  768. void PyAddObject(const char* name, PyTypeObject& type)
  769. {
  770. this->PyAddObject(name, reinterpret_cast<PyObject*>(&type));
  771. }
  772. void PyAddObject(const char* name, PyObject* new_object)
  773. {
  774. Py_INCREF(new_object);
  775. try {
  776. Require(PyModule_AddObject(this->p, name, new_object));
  777. }
  778. catch (const PyErrOccurred&) {
  779. Py_DECREF(p);
  780. throw;
  781. }
  782. }
  783. };
  784. class PyErrFetchParam : public PyObjectPointer<>
  785. {
  786. // Not an owned object, because we can't be initialized with
  787. // one, and we only sometimes acquire ownership.
  788. private:
  789. G_NO_COPIES_OF_CLS(PyErrFetchParam);
  790. public:
  791. // To allow declaring these and passing them to
  792. // PyErr_Fetch we implement the empty constructor,
  793. // and the address operator.
  794. PyErrFetchParam() : PyObjectPointer<>(nullptr)
  795. {
  796. }
  797. PyObject** operator&()
  798. {
  799. return &this->p;
  800. }
  801. // This allows us to pass one directly without the &,
  802. // BUT it has higher precedence than the bool operator
  803. // if it's not explicit.
  804. operator PyObject**()
  805. {
  806. return &this->p;
  807. }
  808. // We don't want to be able to pass these to Py_DECREF and
  809. // such so we don't have the implicit PyObject* conversion.
  810. inline PyObject* relinquish_ownership()
  811. {
  812. PyObject* result = this->p;
  813. this->p = nullptr;
  814. return result;
  815. }
  816. ~PyErrFetchParam()
  817. {
  818. Py_XDECREF(p);
  819. }
  820. };
  821. class OwnedErrPiece : public OwnedObject
  822. {
  823. private:
  824. public:
  825. // Unlike OwnedObject, this increments the refcount.
  826. OwnedErrPiece(PyObject* p=nullptr) : OwnedObject(p)
  827. {
  828. this->acquire();
  829. }
  830. PyObject** operator&()
  831. {
  832. return &this->p;
  833. }
  834. inline operator PyObject*() const
  835. {
  836. return this->p;
  837. }
  838. operator PyTypeObject*() const
  839. {
  840. return reinterpret_cast<PyTypeObject*>(this->p);
  841. }
  842. };
  843. class PyErrPieces
  844. {
  845. private:
  846. OwnedErrPiece type;
  847. OwnedErrPiece instance;
  848. OwnedErrPiece traceback;
  849. bool restored;
  850. public:
  851. // Takes new references; if we're destroyed before
  852. // restoring the error, we drop the references.
  853. PyErrPieces(PyObject* t, PyObject* v, PyObject* tb) :
  854. type(t),
  855. instance(v),
  856. traceback(tb),
  857. restored(0)
  858. {
  859. this->normalize();
  860. }
  861. PyErrPieces() :
  862. restored(0)
  863. {
  864. // PyErr_Fetch transfers ownership to us, so
  865. // we don't actually need to INCREF; but we *do*
  866. // need to DECREF if we're not restored.
  867. PyErrFetchParam t, v, tb;
  868. PyErr_Fetch(&t, &v, &tb);
  869. type.steal(t.relinquish_ownership());
  870. instance.steal(v.relinquish_ownership());
  871. traceback.steal(tb.relinquish_ownership());
  872. }
  873. void PyErrRestore()
  874. {
  875. // can only do this once
  876. assert(!this->restored);
  877. this->restored = true;
  878. PyErr_Restore(
  879. this->type.relinquish_ownership(),
  880. this->instance.relinquish_ownership(),
  881. this->traceback.relinquish_ownership());
  882. assert(!this->type && !this->instance && !this->traceback);
  883. }
  884. private:
  885. void normalize()
  886. {
  887. // First, check the traceback argument, replacing None,
  888. // with NULL
  889. if (traceback.is_None()) {
  890. traceback = nullptr;
  891. }
  892. if (traceback && !PyTraceBack_Check(traceback.borrow())) {
  893. throw PyErrOccurred(PyExc_TypeError,
  894. "throw() third argument must be a traceback object");
  895. }
  896. if (PyExceptionClass_Check(type)) {
  897. // If we just had a type, we'll now have a type and
  898. // instance.
  899. // The type's refcount will have gone up by one
  900. // because of the instance and the instance will have
  901. // a refcount of one. Either way, we owned, and still
  902. // do own, exactly one reference.
  903. PyErr_NormalizeException(&type, &instance, &traceback);
  904. }
  905. else if (PyExceptionInstance_Check(type)) {
  906. /* Raising an instance --- usually that means an
  907. object that is a subclass of BaseException, but on
  908. Python 2, that can also mean an arbitrary old-style
  909. object. The value should be a dummy. */
  910. if (instance && !instance.is_None()) {
  911. throw PyErrOccurred(
  912. PyExc_TypeError,
  913. "instance exception may not have a separate value");
  914. }
  915. /* Normalize to raise <class>, <instance> */
  916. this->instance = this->type;
  917. this->type = PyExceptionInstance_Class(instance.borrow());
  918. /*
  919. It would be tempting to do this:
  920. Py_ssize_t type_count = Py_REFCNT(Py_TYPE(instance.borrow()));
  921. this->type = PyExceptionInstance_Class(instance.borrow());
  922. assert(this->type.REFCNT() == type_count + 1);
  923. But that doesn't work on Python 2 in the case of
  924. old-style instances: The result of Py_TYPE is going to
  925. be the global shared <type instance> that all
  926. old-style classes have, while the return of Instance_Class()
  927. will be the Python-level class object. The two are unrelated.
  928. */
  929. }
  930. else {
  931. /* Not something you can raise. throw() fails. */
  932. PyErr_Format(PyExc_TypeError,
  933. "exceptions must be classes, or instances, not %s",
  934. Py_TYPE(type.borrow())->tp_name);
  935. throw PyErrOccurred();
  936. }
  937. }
  938. };
  939. // PyArg_Parse's O argument returns a borrowed reference.
  940. class PyArgParseParam : public BorrowedObject
  941. {
  942. private:
  943. G_NO_COPIES_OF_CLS(PyArgParseParam);
  944. public:
  945. explicit PyArgParseParam(PyObject* p=nullptr) : BorrowedObject(p)
  946. {
  947. }
  948. inline PyObject** operator&()
  949. {
  950. return &this->p;
  951. }
  952. };
  953. };};
  954. #endif