cext.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. /* ------------------------------------------------------------------------- */
  2. #include "Python.h"
  3. #include "structmember.h"
  4. #define Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(object) \
  5. if (PyObject_TypeCheck(object, &Proxy_Type)) { \
  6. object = Proxy__ensure_wrapped((ProxyObject *)object); \
  7. if (!object) return NULL; \
  8. }
  9. #define Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self) if (!Proxy__ensure_wrapped(self)) return NULL;
  10. #define Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self) if (!Proxy__ensure_wrapped(self)) return -1;
  11. /* ------------------------------------------------------------------------- */
  12. typedef struct {
  13. PyObject_HEAD
  14. PyObject *dict;
  15. PyObject *wrapped;
  16. PyObject *factory;
  17. } ProxyObject;
  18. PyTypeObject Proxy_Type;
  19. /* ------------------------------------------------------------------------- */
  20. static PyObject *identity_ref = NULL;
  21. static PyObject *await_ref = NULL;
  22. static PyObject *
  23. identity(PyObject *self, PyObject *value)
  24. {
  25. Py_INCREF(value);
  26. return value;
  27. }
  28. /* ------------------------------------------------------------------------- */
  29. PyDoc_STRVAR(identity_doc, "Indentity function: returns the single argument.");
  30. static struct PyMethodDef module_functions[] = {
  31. {"identity", identity, METH_O, identity_doc},
  32. {NULL, NULL}
  33. };
  34. /* ------------------------------------------------------------------------- */
  35. static PyObject *Proxy__ensure_wrapped(ProxyObject *self)
  36. {
  37. PyObject *wrapped;
  38. if (self->wrapped) {
  39. return self->wrapped;
  40. } else {
  41. if (self->factory) {
  42. wrapped = PyObject_CallFunctionObjArgs(self->factory, NULL);
  43. if (wrapped) {
  44. self->wrapped = wrapped;
  45. return wrapped;
  46. } else {
  47. return NULL;
  48. }
  49. } else {
  50. PyErr_SetString(PyExc_ValueError, "Proxy hasn't been initiated: __factory__ is missing.");
  51. return NULL;
  52. }
  53. }
  54. }
  55. /* ------------------------------------------------------------------------- */
  56. static PyObject *Proxy_new(PyTypeObject *type,
  57. PyObject *args, PyObject *kwds)
  58. {
  59. ProxyObject *self;
  60. self = (ProxyObject *)type->tp_alloc(type, 0);
  61. if (!self)
  62. return NULL;
  63. self->dict = PyDict_New();
  64. self->wrapped = NULL;
  65. self->factory = NULL;
  66. return (PyObject *)self;
  67. }
  68. /* ------------------------------------------------------------------------- */
  69. static int Proxy_raw_init(ProxyObject *self,
  70. PyObject *factory)
  71. {
  72. Py_INCREF(factory);
  73. Py_XDECREF(self->wrapped);
  74. Py_XDECREF(self->factory);
  75. self->factory = factory;
  76. return 0;
  77. }
  78. /* ------------------------------------------------------------------------- */
  79. static int Proxy_init(ProxyObject *self,
  80. PyObject *args, PyObject *kwds)
  81. {
  82. PyObject *wrapped = NULL;
  83. static char *kwlist[] = { "wrapped", NULL };
  84. if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:ObjectProxy",
  85. kwlist, &wrapped)) {
  86. return -1;
  87. }
  88. return Proxy_raw_init(self, wrapped);
  89. }
  90. /* ------------------------------------------------------------------------- */
  91. static int Proxy_traverse(ProxyObject *self,
  92. visitproc visit, void *arg)
  93. {
  94. Py_VISIT(self->dict);
  95. Py_VISIT(self->wrapped);
  96. Py_VISIT(self->factory);
  97. return 0;
  98. }
  99. /* ------------------------------------------------------------------------- */
  100. static int Proxy_clear(ProxyObject *self)
  101. {
  102. Py_CLEAR(self->dict);
  103. Py_CLEAR(self->wrapped);
  104. Py_CLEAR(self->factory);
  105. return 0;
  106. }
  107. /* ------------------------------------------------------------------------- */
  108. static void Proxy_dealloc(ProxyObject *self)
  109. {
  110. PyObject_GC_UnTrack(self);
  111. Proxy_clear(self);
  112. Py_TYPE(self)->tp_free(self);
  113. }
  114. /* ------------------------------------------------------------------------- */
  115. static PyObject *Proxy_repr(ProxyObject *self)
  116. {
  117. if (self->wrapped) {
  118. return PyUnicode_FromFormat("<%s at %p wrapping %R at %p with factory %R>",
  119. Py_TYPE(self)->tp_name, self,
  120. self->wrapped, self->wrapped,
  121. self->factory);
  122. } else {
  123. return PyUnicode_FromFormat("<%s at %p with factory %R>",
  124. Py_TYPE(self)->tp_name, self,
  125. self->factory);
  126. }
  127. }
  128. /* ------------------------------------------------------------------------- */
  129. static Py_hash_t Proxy_hash(ProxyObject *self)
  130. {
  131. Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
  132. return PyObject_Hash(self->wrapped);
  133. }
  134. /* ------------------------------------------------------------------------- */
  135. static PyObject *Proxy_str(ProxyObject *self)
  136. {
  137. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  138. return PyObject_Str(self->wrapped);
  139. }
  140. /* ------------------------------------------------------------------------- */
  141. static PyObject *Proxy_fspath(ProxyObject *self)
  142. {
  143. PyObject *method = NULL;
  144. PyObject *fspath = NULL;
  145. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  146. if (PyUnicode_Check(self->wrapped) || PyBytes_Check(self->wrapped)) {
  147. Py_INCREF(self->wrapped);
  148. return self->wrapped;
  149. }
  150. method = PyObject_GetAttrString(self->wrapped, "__fspath__");
  151. if (!method) {
  152. PyErr_Clear();
  153. Py_INCREF(self->wrapped);
  154. return self->wrapped;
  155. }
  156. fspath = PyObject_CallFunctionObjArgs(method, NULL);
  157. Py_DECREF(method);
  158. return fspath;
  159. }
  160. /* ------------------------------------------------------------------------- */
  161. static PyObject *Proxy_add(PyObject *o1, PyObject *o2)
  162. {
  163. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
  164. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
  165. return PyNumber_Add(o1, o2);
  166. }
  167. /* ------------------------------------------------------------------------- */
  168. static PyObject *Proxy_subtract(PyObject *o1, PyObject *o2)
  169. {
  170. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
  171. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
  172. return PyNumber_Subtract(o1, o2);
  173. }
  174. /* ------------------------------------------------------------------------- */
  175. static PyObject *Proxy_multiply(PyObject *o1, PyObject *o2)
  176. {
  177. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
  178. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
  179. return PyNumber_Multiply(o1, o2);
  180. }
  181. /* ------------------------------------------------------------------------- */
  182. static PyObject *Proxy_matrix_multiply(PyObject *o1, PyObject *o2)
  183. {
  184. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
  185. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
  186. return PyNumber_MatrixMultiply(o1, o2);
  187. }
  188. /* ------------------------------------------------------------------------- */
  189. static PyObject *Proxy_remainder(PyObject *o1, PyObject *o2)
  190. {
  191. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
  192. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
  193. return PyNumber_Remainder(o1, o2);
  194. }
  195. /* ------------------------------------------------------------------------- */
  196. static PyObject *Proxy_divmod(PyObject *o1, PyObject *o2)
  197. {
  198. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
  199. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
  200. return PyNumber_Divmod(o1, o2);
  201. }
  202. /* ------------------------------------------------------------------------- */
  203. static PyObject *Proxy_power(PyObject *o1, PyObject *o2,
  204. PyObject *modulo)
  205. {
  206. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
  207. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
  208. return PyNumber_Power(o1, o2, modulo);
  209. }
  210. /* ------------------------------------------------------------------------- */
  211. static PyObject *Proxy_negative(ProxyObject *self)
  212. {
  213. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  214. return PyNumber_Negative(self->wrapped);
  215. }
  216. /* ------------------------------------------------------------------------- */
  217. static PyObject *Proxy_positive(ProxyObject *self)
  218. {
  219. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  220. return PyNumber_Positive(self->wrapped);
  221. }
  222. /* ------------------------------------------------------------------------- */
  223. static PyObject *Proxy_absolute(ProxyObject *self)
  224. {
  225. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  226. return PyNumber_Absolute(self->wrapped);
  227. }
  228. /* ------------------------------------------------------------------------- */
  229. static int Proxy_bool(ProxyObject *self)
  230. {
  231. Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
  232. return PyObject_IsTrue(self->wrapped);
  233. }
  234. /* ------------------------------------------------------------------------- */
  235. static PyObject *Proxy_invert(ProxyObject *self)
  236. {
  237. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  238. return PyNumber_Invert(self->wrapped);
  239. }
  240. /* ------------------------------------------------------------------------- */
  241. static PyObject *Proxy_lshift(PyObject *o1, PyObject *o2)
  242. {
  243. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
  244. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
  245. return PyNumber_Lshift(o1, o2);
  246. }
  247. /* ------------------------------------------------------------------------- */
  248. static PyObject *Proxy_rshift(PyObject *o1, PyObject *o2)
  249. {
  250. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
  251. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
  252. return PyNumber_Rshift(o1, o2);
  253. }
  254. /* ------------------------------------------------------------------------- */
  255. static PyObject *Proxy_and(PyObject *o1, PyObject *o2)
  256. {
  257. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
  258. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
  259. return PyNumber_And(o1, o2);
  260. }
  261. /* ------------------------------------------------------------------------- */
  262. static PyObject *Proxy_xor(PyObject *o1, PyObject *o2)
  263. {
  264. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
  265. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
  266. return PyNumber_Xor(o1, o2);
  267. }
  268. /* ------------------------------------------------------------------------- */
  269. static PyObject *Proxy_or(PyObject *o1, PyObject *o2)
  270. {
  271. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
  272. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
  273. return PyNumber_Or(o1, o2);
  274. }
  275. /* ------------------------------------------------------------------------- */
  276. static PyObject *Proxy_long(ProxyObject *self)
  277. {
  278. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  279. return PyNumber_Long(self->wrapped);
  280. }
  281. /* ------------------------------------------------------------------------- */
  282. static PyObject *Proxy_float(ProxyObject *self)
  283. {
  284. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  285. return PyNumber_Float(self->wrapped);
  286. }
  287. /* ------------------------------------------------------------------------- */
  288. static PyObject *Proxy_inplace_add(ProxyObject *self,
  289. PyObject *other)
  290. {
  291. PyObject *object = NULL;
  292. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  293. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
  294. object = PyNumber_InPlaceAdd(self->wrapped, other);
  295. if (!object)
  296. return NULL;
  297. Py_DECREF(self->wrapped);
  298. self->wrapped = object;
  299. Py_INCREF(self);
  300. return (PyObject *)self;
  301. }
  302. /* ------------------------------------------------------------------------- */
  303. static PyObject *Proxy_inplace_subtract(
  304. ProxyObject *self, PyObject *other)
  305. {
  306. PyObject *object = NULL;
  307. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  308. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
  309. object = PyNumber_InPlaceSubtract(self->wrapped, other);
  310. if (!object)
  311. return NULL;
  312. Py_DECREF(self->wrapped);
  313. self->wrapped = object;
  314. Py_INCREF(self);
  315. return (PyObject *)self;
  316. }
  317. /* ------------------------------------------------------------------------- */
  318. static PyObject *Proxy_inplace_multiply(
  319. ProxyObject *self, PyObject *other)
  320. {
  321. PyObject *object = NULL;
  322. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  323. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
  324. object = PyNumber_InPlaceMultiply(self->wrapped, other);
  325. if (!object)
  326. return NULL;
  327. Py_DECREF(self->wrapped);
  328. self->wrapped = object;
  329. Py_INCREF(self);
  330. return (PyObject *)self;
  331. }
  332. /* ------------------------------------------------------------------------- */
  333. static PyObject *Proxy_inplace_matrix_multiply(
  334. ProxyObject *self, PyObject *other)
  335. {
  336. PyObject *object = NULL;
  337. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  338. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
  339. object = PyNumber_InPlaceMatrixMultiply(self->wrapped, other);
  340. if (!object)
  341. return NULL;
  342. Py_DECREF(self->wrapped);
  343. self->wrapped = object;
  344. Py_INCREF(self);
  345. return (PyObject *)self;
  346. }
  347. /* ------------------------------------------------------------------------- */
  348. static PyObject *Proxy_inplace_remainder(
  349. ProxyObject *self, PyObject *other)
  350. {
  351. PyObject *object = NULL;
  352. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  353. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
  354. object = PyNumber_InPlaceRemainder(self->wrapped, other);
  355. if (!object)
  356. return NULL;
  357. Py_DECREF(self->wrapped);
  358. self->wrapped = object;
  359. Py_INCREF(self);
  360. return (PyObject *)self;
  361. }
  362. /* ------------------------------------------------------------------------- */
  363. static PyObject *Proxy_inplace_power(ProxyObject *self,
  364. PyObject *other, PyObject *modulo)
  365. {
  366. PyObject *object = NULL;
  367. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  368. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
  369. object = PyNumber_InPlacePower(self->wrapped, other, modulo);
  370. if (!object)
  371. return NULL;
  372. Py_DECREF(self->wrapped);
  373. self->wrapped = object;
  374. Py_INCREF(self);
  375. return (PyObject *)self;
  376. }
  377. /* ------------------------------------------------------------------------- */
  378. static PyObject *Proxy_inplace_lshift(ProxyObject *self,
  379. PyObject *other)
  380. {
  381. PyObject *object = NULL;
  382. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  383. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
  384. object = PyNumber_InPlaceLshift(self->wrapped, other);
  385. if (!object)
  386. return NULL;
  387. Py_DECREF(self->wrapped);
  388. self->wrapped = object;
  389. Py_INCREF(self);
  390. return (PyObject *)self;
  391. }
  392. /* ------------------------------------------------------------------------- */
  393. static PyObject *Proxy_inplace_rshift(ProxyObject *self,
  394. PyObject *other)
  395. {
  396. PyObject *object = NULL;
  397. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  398. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
  399. object = PyNumber_InPlaceRshift(self->wrapped, other);
  400. if (!object)
  401. return NULL;
  402. Py_DECREF(self->wrapped);
  403. self->wrapped = object;
  404. Py_INCREF(self);
  405. return (PyObject *)self;
  406. }
  407. /* ------------------------------------------------------------------------- */
  408. static PyObject *Proxy_inplace_and(ProxyObject *self,
  409. PyObject *other)
  410. {
  411. PyObject *object = NULL;
  412. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  413. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
  414. object = PyNumber_InPlaceAnd(self->wrapped, other);
  415. if (!object)
  416. return NULL;
  417. Py_DECREF(self->wrapped);
  418. self->wrapped = object;
  419. Py_INCREF(self);
  420. return (PyObject *)self;
  421. }
  422. /* ------------------------------------------------------------------------- */
  423. static PyObject *Proxy_inplace_xor(ProxyObject *self,
  424. PyObject *other)
  425. {
  426. PyObject *object = NULL;
  427. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  428. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
  429. object = PyNumber_InPlaceXor(self->wrapped, other);
  430. if (!object)
  431. return NULL;
  432. Py_DECREF(self->wrapped);
  433. self->wrapped = object;
  434. Py_INCREF(self);
  435. return (PyObject *)self;
  436. }
  437. /* ------------------------------------------------------------------------- */
  438. static PyObject *Proxy_inplace_or(ProxyObject *self,
  439. PyObject *other)
  440. {
  441. PyObject *object = NULL;
  442. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  443. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
  444. object = PyNumber_InPlaceOr(self->wrapped, other);
  445. Py_DECREF(self->wrapped);
  446. self->wrapped = object;
  447. Py_INCREF(self);
  448. return (PyObject *)self;
  449. }
  450. /* ------------------------------------------------------------------------- */
  451. static PyObject *Proxy_floor_divide(PyObject *o1, PyObject *o2)
  452. {
  453. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
  454. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
  455. return PyNumber_FloorDivide(o1, o2);
  456. }
  457. /* ------------------------------------------------------------------------- */
  458. static PyObject *Proxy_true_divide(PyObject *o1, PyObject *o2)
  459. {
  460. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
  461. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);
  462. return PyNumber_TrueDivide(o1, o2);
  463. }
  464. /* ------------------------------------------------------------------------- */
  465. static PyObject *Proxy_inplace_floor_divide(
  466. ProxyObject *self, PyObject *other)
  467. {
  468. PyObject *object = NULL;
  469. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  470. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
  471. object = PyNumber_InPlaceFloorDivide(self->wrapped, other);
  472. if (!object)
  473. return NULL;
  474. Py_DECREF(self->wrapped);
  475. self->wrapped = object;
  476. Py_INCREF(self);
  477. return (PyObject *)self;
  478. }
  479. /* ------------------------------------------------------------------------- */
  480. static PyObject *Proxy_inplace_true_divide(
  481. ProxyObject *self, PyObject *other)
  482. {
  483. PyObject *object = NULL;
  484. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  485. Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(other);
  486. object = PyNumber_InPlaceTrueDivide(self->wrapped, other);
  487. if (!object)
  488. return NULL;
  489. Py_DECREF(self->wrapped);
  490. self->wrapped = object;
  491. Py_INCREF(self);
  492. return (PyObject *)self;
  493. }
  494. /* ------------------------------------------------------------------------- */
  495. static PyObject *Proxy_index(ProxyObject *self)
  496. {
  497. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  498. return PyNumber_Index(self->wrapped);
  499. }
  500. /* ------------------------------------------------------------------------- */
  501. static Py_ssize_t Proxy_length(ProxyObject *self)
  502. {
  503. Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
  504. return PyObject_Length(self->wrapped);
  505. }
  506. /* ------------------------------------------------------------------------- */
  507. static int Proxy_contains(ProxyObject *self,
  508. PyObject *value)
  509. {
  510. Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
  511. return PySequence_Contains(self->wrapped, value);
  512. }
  513. /* ------------------------------------------------------------------------- */
  514. static PyObject *Proxy_getitem(ProxyObject *self,
  515. PyObject *key)
  516. {
  517. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  518. return PyObject_GetItem(self->wrapped, key);
  519. }
  520. /* ------------------------------------------------------------------------- */
  521. static int Proxy_setitem(ProxyObject *self,
  522. PyObject *key, PyObject* value)
  523. {
  524. Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
  525. if (value == NULL)
  526. return PyObject_DelItem(self->wrapped, key);
  527. else
  528. return PyObject_SetItem(self->wrapped, key, value);
  529. }
  530. /* ------------------------------------------------------------------------- */
  531. static PyObject *Proxy_dir(
  532. ProxyObject *self, PyObject *args)
  533. {
  534. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  535. return PyObject_Dir(self->wrapped);
  536. }
  537. /* ------------------------------------------------------------------------- */
  538. static PyObject *Proxy_enter(ProxyObject *self)
  539. {
  540. PyObject *method = NULL;
  541. PyObject *result = NULL;
  542. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  543. method = PyObject_GetAttrString(self->wrapped, "__enter__");
  544. if (!method)
  545. return NULL;
  546. result = PyObject_CallObject(method, NULL);
  547. Py_DECREF(method);
  548. return result;
  549. }
  550. /* ------------------------------------------------------------------------- */
  551. static PyObject *Proxy_exit(
  552. ProxyObject *self, PyObject *args, PyObject *kwds)
  553. {
  554. PyObject *method = NULL;
  555. PyObject *result = NULL;
  556. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  557. method = PyObject_GetAttrString(self->wrapped, "__exit__");
  558. if (!method)
  559. return NULL;
  560. result = PyObject_Call(method, args, kwds);
  561. Py_DECREF(method);
  562. return result;
  563. }
  564. /* ------------------------------------------------------------------------- */
  565. static PyObject *Proxy_bytes(
  566. ProxyObject *self, PyObject *args)
  567. {
  568. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  569. return PyObject_Bytes(self->wrapped);
  570. }
  571. /* ------------------------------------------------------------------------- */
  572. static PyObject *Proxy_reversed(
  573. ProxyObject *self, PyObject *args)
  574. {
  575. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  576. return PyObject_CallFunctionObjArgs((PyObject *)&PyReversed_Type,
  577. self->wrapped, NULL);
  578. }
  579. /* ------------------------------------------------------------------------- */
  580. static PyObject *Proxy_reduce(
  581. ProxyObject *self, PyObject *args)
  582. {
  583. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  584. return Py_BuildValue("(O(O))", identity_ref, self->wrapped);
  585. }
  586. /* ------------------------------------------------------------------------- */
  587. static PyObject *Proxy_round(
  588. ProxyObject *self, PyObject *args)
  589. {
  590. PyObject *module = NULL;
  591. PyObject *dict = NULL;
  592. PyObject *round = NULL;
  593. PyObject *result = NULL;
  594. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  595. module = PyImport_ImportModule("builtins");
  596. if (!module)
  597. return NULL;
  598. dict = PyModule_GetDict(module);
  599. round = PyDict_GetItemString(dict, "round");
  600. if (!round) {
  601. Py_DECREF(module);
  602. return NULL;
  603. }
  604. Py_INCREF(round);
  605. Py_DECREF(module);
  606. result = PyObject_CallFunctionObjArgs(round, self->wrapped, NULL);
  607. Py_DECREF(round);
  608. return result;
  609. }
  610. /* ------------------------------------------------------------------------- */
  611. static PyObject *Proxy_get_name(
  612. ProxyObject *self)
  613. {
  614. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  615. return PyObject_GetAttrString(self->wrapped, "__name__");
  616. }
  617. /* ------------------------------------------------------------------------- */
  618. static int Proxy_set_name(ProxyObject *self,
  619. PyObject *value)
  620. {
  621. Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
  622. return PyObject_SetAttrString(self->wrapped, "__name__", value);
  623. }
  624. /* ------------------------------------------------------------------------- */
  625. static PyObject *Proxy_get_qualname(
  626. ProxyObject *self)
  627. {
  628. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  629. return PyObject_GetAttrString(self->wrapped, "__qualname__");
  630. }
  631. /* ------------------------------------------------------------------------- */
  632. static int Proxy_set_qualname(ProxyObject *self,
  633. PyObject *value)
  634. {
  635. Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
  636. return PyObject_SetAttrString(self->wrapped, "__qualname__", value);
  637. }
  638. /* ------------------------------------------------------------------------- */
  639. static PyObject *Proxy_get_module(
  640. ProxyObject *self)
  641. {
  642. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  643. return PyObject_GetAttrString(self->wrapped, "__module__");
  644. }
  645. /* ------------------------------------------------------------------------- */
  646. static int Proxy_set_module(ProxyObject *self,
  647. PyObject *value)
  648. {
  649. Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
  650. if (PyObject_SetAttrString(self->wrapped, "__module__", value) == -1)
  651. return -1;
  652. return PyDict_SetItemString(self->dict, "__module__", value);
  653. }
  654. /* ------------------------------------------------------------------------- */
  655. static PyObject *Proxy_get_doc(
  656. ProxyObject *self)
  657. {
  658. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  659. return PyObject_GetAttrString(self->wrapped, "__doc__");
  660. }
  661. /* ------------------------------------------------------------------------- */
  662. static int Proxy_set_doc(ProxyObject *self,
  663. PyObject *value)
  664. {
  665. Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
  666. if (PyObject_SetAttrString(self->wrapped, "__doc__", value) == -1)
  667. return -1;
  668. return PyDict_SetItemString(self->dict, "__doc__", value);
  669. }
  670. /* ------------------------------------------------------------------------- */
  671. static PyObject *Proxy_get_class(
  672. ProxyObject *self)
  673. {
  674. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  675. return PyObject_GetAttrString(self->wrapped, "__class__");
  676. }
  677. /* ------------------------------------------------------------------------- */
  678. static PyObject *Proxy_get_annotations(
  679. ProxyObject *self)
  680. {
  681. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  682. return PyObject_GetAttrString(self->wrapped, "__annotations__");
  683. }
  684. /* ------------------------------------------------------------------------- */
  685. static int Proxy_set_annotations(ProxyObject *self,
  686. PyObject *value)
  687. {
  688. Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
  689. return PyObject_SetAttrString(self->wrapped, "__annotations__", value);
  690. }
  691. /* ------------------------------------------------------------------------- */
  692. static PyObject *Proxy_get_resolved(
  693. ProxyObject *self)
  694. {
  695. PyObject *result;
  696. result = self->wrapped ? Py_True : Py_False;
  697. Py_INCREF(result);
  698. return result;
  699. }
  700. /* ------------------------------------------------------------------------- */
  701. static PyObject *Proxy_get_wrapped(
  702. ProxyObject *self)
  703. {
  704. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  705. Py_INCREF(self->wrapped);
  706. return self->wrapped;
  707. }
  708. /* ------------------------------------------------------------------------- */
  709. static int Proxy_set_wrapped(ProxyObject *self,
  710. PyObject *value)
  711. {
  712. if (value) Py_INCREF(value);
  713. Py_XDECREF(self->wrapped);
  714. self->wrapped = value;
  715. return 0;
  716. }
  717. /* ------------------------------------------------------------------------- */
  718. static PyObject *Proxy_get_factory(
  719. ProxyObject *self)
  720. {
  721. Py_INCREF(self->factory);
  722. return self->factory;
  723. }
  724. /* ------------------------------------------------------------------------- */
  725. static int Proxy_set_factory(ProxyObject *self,
  726. PyObject *value)
  727. {
  728. if (value) Py_INCREF(value);
  729. Py_XDECREF(self->factory);
  730. self->factory = value;
  731. return 0;
  732. }
  733. /* ------------------------------------------------------------------------- */
  734. static PyObject *Proxy_getattro(
  735. ProxyObject *self, PyObject *name)
  736. {
  737. PyObject *object = NULL;
  738. PyObject *result = NULL;
  739. static PyObject *getattr_str = NULL;
  740. object = PyObject_GenericGetAttr((PyObject *)self, name);
  741. if (object)
  742. return object;
  743. PyErr_Clear();
  744. if (!getattr_str) {
  745. getattr_str = PyUnicode_InternFromString("__getattr__");
  746. }
  747. object = PyObject_GenericGetAttr((PyObject *)self, getattr_str);
  748. if (!object)
  749. return NULL;
  750. result = PyObject_CallFunctionObjArgs(object, name, NULL);
  751. Py_DECREF(object);
  752. return result;
  753. }
  754. /* ------------------------------------------------------------------------- */
  755. static PyObject *Proxy_getattr(
  756. ProxyObject *self, PyObject *args)
  757. {
  758. PyObject *name = NULL;
  759. if (!PyArg_ParseTuple(args, "U:__getattr__", &name))
  760. return NULL;
  761. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  762. return PyObject_GetAttr(self->wrapped, name);
  763. }
  764. /* ------------------------------------------------------------------------- */
  765. static int Proxy_setattro(
  766. ProxyObject *self, PyObject *name, PyObject *value)
  767. {
  768. if (PyObject_HasAttr((PyObject *)Py_TYPE(self), name))
  769. return PyObject_GenericSetAttr((PyObject *)self, name, value);
  770. Proxy__ENSURE_WRAPPED_OR_RETURN_MINUS1(self);
  771. return PyObject_SetAttr(self->wrapped, name, value);
  772. }
  773. /* ------------------------------------------------------------------------- */
  774. static PyObject *Proxy_richcompare(ProxyObject *self,
  775. PyObject *other, int opcode)
  776. {
  777. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  778. return PyObject_RichCompare(self->wrapped, other, opcode);
  779. }
  780. /* ------------------------------------------------------------------------- */
  781. static PyObject *Proxy_iter(ProxyObject *self)
  782. {
  783. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  784. return PyObject_GetIter(self->wrapped);
  785. }
  786. /* ------------------------------------------------------------------------- */
  787. static PyObject *Proxy_call(
  788. ProxyObject *self, PyObject *args, PyObject *kwds)
  789. {
  790. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  791. return PyObject_Call(self->wrapped, args, kwds);
  792. }
  793. /* ------------------------------------------------------------------------- */;
  794. static PyObject *Proxy_aenter(ProxyObject *self)
  795. {
  796. PyObject *method = NULL;
  797. PyObject *result = NULL;
  798. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  799. method = PyObject_GetAttrString(self->wrapped, "__aenter__");
  800. if (!method)
  801. return NULL;
  802. result = PyObject_CallObject(method, NULL);
  803. Py_DECREF(method);
  804. return result;
  805. }
  806. /* ------------------------------------------------------------------------- */
  807. static PyObject *Proxy_aexit(
  808. ProxyObject *self, PyObject *args, PyObject *kwds)
  809. {
  810. PyObject *method = NULL;
  811. PyObject *result = NULL;
  812. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  813. method = PyObject_GetAttrString(self->wrapped, "__aexit__");
  814. if (!method)
  815. return NULL;
  816. result = PyObject_Call(method, args, kwds);
  817. Py_DECREF(method);
  818. return result;
  819. }
  820. /* ------------------------------------------------------------------------- */
  821. static PyObject *Proxy_await(ProxyObject *self)
  822. {
  823. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  824. return PyObject_CallFunctionObjArgs(await_ref, self->wrapped, NULL);
  825. }
  826. /* ------------------------------------------------------------------------- */;
  827. static PyObject *Proxy_aiter(ProxyObject *self)
  828. {
  829. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  830. unaryfunc meth = NULL;
  831. PyObject *wrapped = self->wrapped;
  832. PyTypeObject *type = Py_TYPE(wrapped);
  833. if (type->tp_as_async != NULL) {
  834. meth = type->tp_as_async->am_aiter;
  835. }
  836. if (meth != NULL) {
  837. return (*meth)(wrapped);
  838. }
  839. PyErr_Format(PyExc_AttributeError, "'%.100s' object has no attribute '__aiter__'", type->tp_name);
  840. return NULL;
  841. }
  842. /* ------------------------------------------------------------------------- */;
  843. static PyObject *Proxy_anext(ProxyObject *self)
  844. {
  845. Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);
  846. unaryfunc meth = NULL;
  847. PyObject *wrapped = self->wrapped;
  848. PyTypeObject *type = Py_TYPE(wrapped);
  849. if (type->tp_as_async != NULL) {
  850. meth = type->tp_as_async->am_anext;
  851. }
  852. if (meth != NULL) {
  853. return (*meth)(wrapped);
  854. }
  855. PyErr_Format(PyExc_TypeError, "'%.100s' is missing the __anext__ method", type->tp_name);
  856. return NULL;
  857. }
  858. /* ------------------------------------------------------------------------- */;
  859. static PyNumberMethods Proxy_as_number = {
  860. (binaryfunc)Proxy_add, /*nb_add*/
  861. (binaryfunc)Proxy_subtract, /*nb_subtract*/
  862. (binaryfunc)Proxy_multiply, /*nb_multiply*/
  863. (binaryfunc)Proxy_remainder, /*nb_remainder*/
  864. (binaryfunc)Proxy_divmod, /*nb_divmod*/
  865. (ternaryfunc)Proxy_power, /*nb_power*/
  866. (unaryfunc)Proxy_negative, /*nb_negative*/
  867. (unaryfunc)Proxy_positive, /*nb_positive*/
  868. (unaryfunc)Proxy_absolute, /*nb_absolute*/
  869. (inquiry)Proxy_bool, /*nb_nonzero/nb_bool*/
  870. (unaryfunc)Proxy_invert, /*nb_invert*/
  871. (binaryfunc)Proxy_lshift, /*nb_lshift*/
  872. (binaryfunc)Proxy_rshift, /*nb_rshift*/
  873. (binaryfunc)Proxy_and, /*nb_and*/
  874. (binaryfunc)Proxy_xor, /*nb_xor*/
  875. (binaryfunc)Proxy_or, /*nb_or*/
  876. (unaryfunc)Proxy_long, /*nb_int*/
  877. 0, /*nb_long/nb_reserved*/
  878. (unaryfunc)Proxy_float, /*nb_float*/
  879. (binaryfunc)Proxy_inplace_add, /*nb_inplace_add*/
  880. (binaryfunc)Proxy_inplace_subtract, /*nb_inplace_subtract*/
  881. (binaryfunc)Proxy_inplace_multiply, /*nb_inplace_multiply*/
  882. (binaryfunc)Proxy_inplace_remainder, /*nb_inplace_remainder*/
  883. (ternaryfunc)Proxy_inplace_power, /*nb_inplace_power*/
  884. (binaryfunc)Proxy_inplace_lshift, /*nb_inplace_lshift*/
  885. (binaryfunc)Proxy_inplace_rshift, /*nb_inplace_rshift*/
  886. (binaryfunc)Proxy_inplace_and, /*nb_inplace_and*/
  887. (binaryfunc)Proxy_inplace_xor, /*nb_inplace_xor*/
  888. (binaryfunc)Proxy_inplace_or, /*nb_inplace_or*/
  889. (binaryfunc)Proxy_floor_divide, /*nb_floor_divide*/
  890. (binaryfunc)Proxy_true_divide, /*nb_true_divide*/
  891. (binaryfunc)Proxy_inplace_floor_divide, /*nb_inplace_floor_divide*/
  892. (binaryfunc)Proxy_inplace_true_divide, /*nb_inplace_true_divide*/
  893. (unaryfunc)Proxy_index, /*nb_index*/
  894. (binaryfunc)Proxy_matrix_multiply, /*nb_matrix_multiply*/
  895. (binaryfunc)Proxy_inplace_matrix_multiply, /*nb_inplace_matrix_multiply*/
  896. };
  897. static PySequenceMethods Proxy_as_sequence = {
  898. (lenfunc)Proxy_length, /*sq_length*/
  899. 0, /*sq_concat*/
  900. 0, /*sq_repeat*/
  901. 0, /*sq_item*/
  902. 0, /*sq_slice*/
  903. 0, /*sq_ass_item*/
  904. 0, /*sq_ass_slice*/
  905. (objobjproc)Proxy_contains, /* sq_contains */
  906. };
  907. static PyMappingMethods Proxy_as_mapping = {
  908. (lenfunc)Proxy_length, /*mp_length*/
  909. (binaryfunc)Proxy_getitem, /*mp_subscript*/
  910. (objobjargproc)Proxy_setitem, /*mp_ass_subscript*/
  911. };
  912. static PyAsyncMethods Proxy_as_async = {
  913. (unaryfunc)Proxy_await, /* am_await */
  914. (unaryfunc)Proxy_aiter, /* am_aiter */
  915. (unaryfunc)Proxy_anext, /* am_anext */
  916. };
  917. static PyMethodDef Proxy_methods[] = {
  918. { "__dir__", (PyCFunction)Proxy_dir, METH_NOARGS, 0 },
  919. { "__enter__", (PyCFunction)Proxy_enter, METH_NOARGS, 0 },
  920. { "__exit__", (PyCFunction)Proxy_exit, METH_VARARGS | METH_KEYWORDS, 0 },
  921. { "__getattr__", (PyCFunction)Proxy_getattr, METH_VARARGS , 0 },
  922. { "__bytes__", (PyCFunction)Proxy_bytes, METH_NOARGS, 0 },
  923. { "__reversed__", (PyCFunction)Proxy_reversed, METH_NOARGS, 0 },
  924. { "__reduce__", (PyCFunction)Proxy_reduce, METH_NOARGS, 0 },
  925. { "__reduce_ex__", (PyCFunction)Proxy_reduce, METH_O, 0 },
  926. { "__fspath__", (PyCFunction)Proxy_fspath, METH_NOARGS, 0 },
  927. { "__round__", (PyCFunction)Proxy_round, METH_NOARGS, 0 },
  928. { "__aenter__", (PyCFunction)Proxy_aenter, METH_NOARGS, 0 },
  929. { "__aexit__", (PyCFunction)Proxy_aexit, METH_VARARGS | METH_KEYWORDS, 0 },
  930. { NULL, NULL },
  931. };
  932. static PyGetSetDef Proxy_getset[] = {
  933. { "__name__", (getter)Proxy_get_name, (setter)Proxy_set_name, 0 },
  934. { "__qualname__", (getter)Proxy_get_qualname, (setter)Proxy_set_qualname, 0 },
  935. { "__module__", (getter)Proxy_get_module, (setter)Proxy_set_module, 0 },
  936. { "__doc__", (getter)Proxy_get_doc, (setter)Proxy_set_doc, 0 },
  937. { "__class__", (getter)Proxy_get_class, NULL, 0 },
  938. { "__annotations__", (getter)Proxy_get_annotations, (setter)Proxy_set_annotations, 0 },
  939. { "__wrapped__", (getter)Proxy_get_wrapped, (setter)Proxy_set_wrapped, 0 },
  940. { "__factory__", (getter)Proxy_get_factory, (setter)Proxy_set_factory, 0 },
  941. { "__resolved__", (getter)Proxy_get_resolved, NULL, 0 },
  942. { NULL },
  943. };
  944. PyTypeObject Proxy_Type = {
  945. PyVarObject_HEAD_INIT(NULL, 0)
  946. "Proxy", /*tp_name*/
  947. sizeof(ProxyObject), /*tp_basicsize*/
  948. 0, /*tp_itemsize*/
  949. /* methods */
  950. (destructor)Proxy_dealloc, /*tp_dealloc*/
  951. 0, /*tp_print*/
  952. 0, /*tp_getattr*/
  953. 0, /*tp_setattr*/
  954. &Proxy_as_async, /* tp_as_async */
  955. (unaryfunc)Proxy_repr, /*tp_repr*/
  956. &Proxy_as_number, /*tp_as_number*/
  957. &Proxy_as_sequence, /*tp_as_sequence*/
  958. &Proxy_as_mapping, /*tp_as_mapping*/
  959. (hashfunc)Proxy_hash, /*tp_hash*/
  960. (ternaryfunc)Proxy_call, /*tp_call*/
  961. (unaryfunc)Proxy_str, /*tp_str*/
  962. (getattrofunc)Proxy_getattro, /*tp_getattro*/
  963. (setattrofunc)Proxy_setattro, /*tp_setattro*/
  964. 0, /*tp_as_buffer*/
  965. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
  966. /*tp_flags*/
  967. 0, /*tp_doc*/
  968. (traverseproc)Proxy_traverse, /*tp_traverse*/
  969. (inquiry)Proxy_clear, /*tp_clear*/
  970. (richcmpfunc)Proxy_richcompare, /*tp_richcompare*/
  971. 0, /*tp_weaklistoffset*/
  972. (getiterfunc)Proxy_iter, /*tp_iter*/
  973. 0, /*tp_iternext*/
  974. Proxy_methods, /*tp_methods*/
  975. 0, /*tp_members*/
  976. Proxy_getset, /*tp_getset*/
  977. 0, /*tp_base*/
  978. 0, /*tp_dict*/
  979. 0, /*tp_descr_get*/
  980. 0, /*tp_descr_set*/
  981. offsetof(ProxyObject, dict), /*tp_dictoffset*/
  982. (initproc)Proxy_init, /*tp_init*/
  983. PyType_GenericAlloc, /*tp_alloc*/
  984. Proxy_new, /*tp_new*/
  985. PyObject_GC_Del, /*tp_free*/
  986. 0, /*tp_is_gc*/
  987. };
  988. /* ------------------------------------------------------------------------- */
  989. static struct PyModuleDef moduledef = {
  990. PyModuleDef_HEAD_INIT,
  991. "lazy_object_proxy.cext", /* m_name */
  992. NULL, /* m_doc */
  993. -1, /* m_size */
  994. module_functions, /* m_methods */
  995. NULL, /* m_reload */
  996. NULL, /* m_traverse */
  997. NULL, /* m_clear */
  998. NULL, /* m_free */
  999. };
  1000. static PyObject *
  1001. moduleinit(void)
  1002. {
  1003. PyObject *module;
  1004. PyObject *dict;
  1005. module = PyModule_Create(&moduledef);
  1006. if (module == NULL)
  1007. return NULL;
  1008. if (PyType_Ready(&Proxy_Type) < 0)
  1009. return NULL;
  1010. dict = PyModule_GetDict(module);
  1011. if (dict == NULL)
  1012. return NULL;
  1013. identity_ref = PyDict_GetItemString(dict, "identity");
  1014. if (identity_ref == NULL)
  1015. return NULL;
  1016. Py_INCREF(identity_ref);
  1017. PyObject *utils_module = PyImport_ImportModule("lazy_object_proxy.utils");
  1018. if (utils_module == NULL)
  1019. return NULL;
  1020. await_ref = PyObject_GetAttrString(utils_module, "await_");
  1021. Py_DECREF(utils_module);
  1022. if (await_ref == NULL)
  1023. return NULL;
  1024. Py_INCREF(&Proxy_Type);
  1025. PyModule_AddObject(module, "Proxy",
  1026. (PyObject *)&Proxy_Type);
  1027. return module;
  1028. }
  1029. PyMODINIT_FUNC PyInit_cext(void)
  1030. {
  1031. return moduleinit();
  1032. }
  1033. /* ------------------------------------------------------------------------- */