Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cext.c 38KB

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