_operator.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877
  1. #include "Python.h"
  2. #include "pycore_moduleobject.h" // _PyModule_GetState()
  3. #include "structmember.h" // PyMemberDef
  4. #include "pycore_runtime.h" // _Py_ID()
  5. #include "clinic/_operator.c.h"
  6. typedef struct {
  7. PyObject *itemgetter_type;
  8. PyObject *attrgetter_type;
  9. PyObject *methodcaller_type;
  10. } _operator_state;
  11. static inline _operator_state*
  12. get_operator_state(PyObject *module)
  13. {
  14. void *state = _PyModule_GetState(module);
  15. assert(state != NULL);
  16. return (_operator_state *)state;
  17. }
  18. /*[clinic input]
  19. module _operator
  20. [clinic start generated code]*/
  21. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=672ecf48487521e7]*/
  22. PyDoc_STRVAR(operator_doc,
  23. "Operator interface.\n\
  24. \n\
  25. This module exports a set of functions implemented in C corresponding\n\
  26. to the intrinsic operators of Python. For example, operator.add(x, y)\n\
  27. is equivalent to the expression x+y. The function names are those\n\
  28. used for special methods; variants without leading and trailing\n\
  29. '__' are also provided for convenience.");
  30. /*[clinic input]
  31. _operator.truth -> bool
  32. a: object
  33. /
  34. Return True if a is true, False otherwise.
  35. [clinic start generated code]*/
  36. static int
  37. _operator_truth_impl(PyObject *module, PyObject *a)
  38. /*[clinic end generated code: output=eaf87767234fa5d7 input=bc74a4cd90235875]*/
  39. {
  40. return PyObject_IsTrue(a);
  41. }
  42. /*[clinic input]
  43. _operator.add
  44. a: object
  45. b: object
  46. /
  47. Same as a + b.
  48. [clinic start generated code]*/
  49. static PyObject *
  50. _operator_add_impl(PyObject *module, PyObject *a, PyObject *b)
  51. /*[clinic end generated code: output=8292984204f45164 input=5efe3bff856ac215]*/
  52. {
  53. return PyNumber_Add(a, b);
  54. }
  55. /*[clinic input]
  56. _operator.sub = _operator.add
  57. Same as a - b.
  58. [clinic start generated code]*/
  59. static PyObject *
  60. _operator_sub_impl(PyObject *module, PyObject *a, PyObject *b)
  61. /*[clinic end generated code: output=4adfc3b888c1ee2e input=6494c6b100b8e795]*/
  62. {
  63. return PyNumber_Subtract(a, b);
  64. }
  65. /*[clinic input]
  66. _operator.mul = _operator.add
  67. Same as a * b.
  68. [clinic start generated code]*/
  69. static PyObject *
  70. _operator_mul_impl(PyObject *module, PyObject *a, PyObject *b)
  71. /*[clinic end generated code: output=d24d66f55a01944c input=2368615b4358b70d]*/
  72. {
  73. return PyNumber_Multiply(a, b);
  74. }
  75. /*[clinic input]
  76. _operator.matmul = _operator.add
  77. Same as a @ b.
  78. [clinic start generated code]*/
  79. static PyObject *
  80. _operator_matmul_impl(PyObject *module, PyObject *a, PyObject *b)
  81. /*[clinic end generated code: output=a20d917eb35d0101 input=9ab304e37fb42dd4]*/
  82. {
  83. return PyNumber_MatrixMultiply(a, b);
  84. }
  85. /*[clinic input]
  86. _operator.floordiv = _operator.add
  87. Same as a // b.
  88. [clinic start generated code]*/
  89. static PyObject *
  90. _operator_floordiv_impl(PyObject *module, PyObject *a, PyObject *b)
  91. /*[clinic end generated code: output=df26b71a60589f99 input=bb2e88ba446c612c]*/
  92. {
  93. return PyNumber_FloorDivide(a, b);
  94. }
  95. /*[clinic input]
  96. _operator.truediv = _operator.add
  97. Same as a / b.
  98. [clinic start generated code]*/
  99. static PyObject *
  100. _operator_truediv_impl(PyObject *module, PyObject *a, PyObject *b)
  101. /*[clinic end generated code: output=0e6a959944d77719 input=ecbb947673f4eb1f]*/
  102. {
  103. return PyNumber_TrueDivide(a, b);
  104. }
  105. /*[clinic input]
  106. _operator.mod = _operator.add
  107. Same as a % b.
  108. [clinic start generated code]*/
  109. static PyObject *
  110. _operator_mod_impl(PyObject *module, PyObject *a, PyObject *b)
  111. /*[clinic end generated code: output=9519822f0bbec166 input=102e19b422342ac1]*/
  112. {
  113. return PyNumber_Remainder(a, b);
  114. }
  115. /*[clinic input]
  116. _operator.neg
  117. a: object
  118. /
  119. Same as -a.
  120. [clinic start generated code]*/
  121. static PyObject *
  122. _operator_neg(PyObject *module, PyObject *a)
  123. /*[clinic end generated code: output=36e08ecfc6a1c08c input=84f09bdcf27c96ec]*/
  124. {
  125. return PyNumber_Negative(a);
  126. }
  127. /*[clinic input]
  128. _operator.pos = _operator.neg
  129. Same as +a.
  130. [clinic start generated code]*/
  131. static PyObject *
  132. _operator_pos(PyObject *module, PyObject *a)
  133. /*[clinic end generated code: output=dad7a126221dd091 input=b6445b63fddb8772]*/
  134. {
  135. return PyNumber_Positive(a);
  136. }
  137. /*[clinic input]
  138. _operator.abs = _operator.neg
  139. Same as abs(a).
  140. [clinic start generated code]*/
  141. static PyObject *
  142. _operator_abs(PyObject *module, PyObject *a)
  143. /*[clinic end generated code: output=1389a93ba053ea3e input=341d07ba86f58039]*/
  144. {
  145. return PyNumber_Absolute(a);
  146. }
  147. /*[clinic input]
  148. _operator.inv = _operator.neg
  149. Same as ~a.
  150. [clinic start generated code]*/
  151. static PyObject *
  152. _operator_inv(PyObject *module, PyObject *a)
  153. /*[clinic end generated code: output=a56875ba075ee06d input=b01a4677739f6eb2]*/
  154. {
  155. return PyNumber_Invert(a);
  156. }
  157. /*[clinic input]
  158. _operator.invert = _operator.neg
  159. Same as ~a.
  160. [clinic start generated code]*/
  161. static PyObject *
  162. _operator_invert(PyObject *module, PyObject *a)
  163. /*[clinic end generated code: output=406b5aa030545fcc input=7f2d607176672e55]*/
  164. {
  165. return PyNumber_Invert(a);
  166. }
  167. /*[clinic input]
  168. _operator.lshift = _operator.add
  169. Same as a << b.
  170. [clinic start generated code]*/
  171. static PyObject *
  172. _operator_lshift_impl(PyObject *module, PyObject *a, PyObject *b)
  173. /*[clinic end generated code: output=37f7e52c41435bd8 input=746e8a160cbbc9eb]*/
  174. {
  175. return PyNumber_Lshift(a, b);
  176. }
  177. /*[clinic input]
  178. _operator.rshift = _operator.add
  179. Same as a >> b.
  180. [clinic start generated code]*/
  181. static PyObject *
  182. _operator_rshift_impl(PyObject *module, PyObject *a, PyObject *b)
  183. /*[clinic end generated code: output=4593c7ef30ec2ee3 input=d2c85bb5a64504c2]*/
  184. {
  185. return PyNumber_Rshift(a, b);
  186. }
  187. /*[clinic input]
  188. _operator.not_ = _operator.truth
  189. Same as not a.
  190. [clinic start generated code]*/
  191. static int
  192. _operator_not__impl(PyObject *module, PyObject *a)
  193. /*[clinic end generated code: output=743f9c24a09759ef input=854156d50804d9b8]*/
  194. {
  195. return PyObject_Not(a);
  196. }
  197. /*[clinic input]
  198. _operator.and_ = _operator.add
  199. Same as a & b.
  200. [clinic start generated code]*/
  201. static PyObject *
  202. _operator_and__impl(PyObject *module, PyObject *a, PyObject *b)
  203. /*[clinic end generated code: output=93c4fe88f7b76d9e input=4f3057c90ec4c99f]*/
  204. {
  205. return PyNumber_And(a, b);
  206. }
  207. /*[clinic input]
  208. _operator.xor = _operator.add
  209. Same as a ^ b.
  210. [clinic start generated code]*/
  211. static PyObject *
  212. _operator_xor_impl(PyObject *module, PyObject *a, PyObject *b)
  213. /*[clinic end generated code: output=b24cd8b79fde0004 input=3c5cfa7253d808dd]*/
  214. {
  215. return PyNumber_Xor(a, b);
  216. }
  217. /*[clinic input]
  218. _operator.or_ = _operator.add
  219. Same as a | b.
  220. [clinic start generated code]*/
  221. static PyObject *
  222. _operator_or__impl(PyObject *module, PyObject *a, PyObject *b)
  223. /*[clinic end generated code: output=58024867b8d90461 input=b40c6c44f7c79c09]*/
  224. {
  225. return PyNumber_Or(a, b);
  226. }
  227. /*[clinic input]
  228. _operator.iadd = _operator.add
  229. Same as a += b.
  230. [clinic start generated code]*/
  231. static PyObject *
  232. _operator_iadd_impl(PyObject *module, PyObject *a, PyObject *b)
  233. /*[clinic end generated code: output=07dc627832526eb5 input=d22a91c07ac69227]*/
  234. {
  235. return PyNumber_InPlaceAdd(a, b);
  236. }
  237. /*[clinic input]
  238. _operator.isub = _operator.add
  239. Same as a -= b.
  240. [clinic start generated code]*/
  241. static PyObject *
  242. _operator_isub_impl(PyObject *module, PyObject *a, PyObject *b)
  243. /*[clinic end generated code: output=4513467d23b5e0b1 input=4591b00d0a0ccafd]*/
  244. {
  245. return PyNumber_InPlaceSubtract(a, b);
  246. }
  247. /*[clinic input]
  248. _operator.imul = _operator.add
  249. Same as a *= b.
  250. [clinic start generated code]*/
  251. static PyObject *
  252. _operator_imul_impl(PyObject *module, PyObject *a, PyObject *b)
  253. /*[clinic end generated code: output=5e87dacd19a71eab input=0e01fb8631e1b76f]*/
  254. {
  255. return PyNumber_InPlaceMultiply(a, b);
  256. }
  257. /*[clinic input]
  258. _operator.imatmul = _operator.add
  259. Same as a @= b.
  260. [clinic start generated code]*/
  261. static PyObject *
  262. _operator_imatmul_impl(PyObject *module, PyObject *a, PyObject *b)
  263. /*[clinic end generated code: output=d603cbdf716ce519 input=bb614026372cd542]*/
  264. {
  265. return PyNumber_InPlaceMatrixMultiply(a, b);
  266. }
  267. /*[clinic input]
  268. _operator.ifloordiv = _operator.add
  269. Same as a //= b.
  270. [clinic start generated code]*/
  271. static PyObject *
  272. _operator_ifloordiv_impl(PyObject *module, PyObject *a, PyObject *b)
  273. /*[clinic end generated code: output=535336048c681794 input=9df3b5021cff4ca1]*/
  274. {
  275. return PyNumber_InPlaceFloorDivide(a, b);
  276. }
  277. /*[clinic input]
  278. _operator.itruediv = _operator.add
  279. Same as a /= b.
  280. [clinic start generated code]*/
  281. static PyObject *
  282. _operator_itruediv_impl(PyObject *module, PyObject *a, PyObject *b)
  283. /*[clinic end generated code: output=28017fbd3563952f input=9a1ee01608f5f590]*/
  284. {
  285. return PyNumber_InPlaceTrueDivide(a, b);
  286. }
  287. /*[clinic input]
  288. _operator.imod = _operator.add
  289. Same as a %= b.
  290. [clinic start generated code]*/
  291. static PyObject *
  292. _operator_imod_impl(PyObject *module, PyObject *a, PyObject *b)
  293. /*[clinic end generated code: output=f7c540ae0fc70904 input=d0c384a3ce38e1dd]*/
  294. {
  295. return PyNumber_InPlaceRemainder(a, b);
  296. }
  297. /*[clinic input]
  298. _operator.ilshift = _operator.add
  299. Same as a <<= b.
  300. [clinic start generated code]*/
  301. static PyObject *
  302. _operator_ilshift_impl(PyObject *module, PyObject *a, PyObject *b)
  303. /*[clinic end generated code: output=e73a8fee1ac18749 input=e21b6b310f54572e]*/
  304. {
  305. return PyNumber_InPlaceLshift(a, b);
  306. }
  307. /*[clinic input]
  308. _operator.irshift = _operator.add
  309. Same as a >>= b.
  310. [clinic start generated code]*/
  311. static PyObject *
  312. _operator_irshift_impl(PyObject *module, PyObject *a, PyObject *b)
  313. /*[clinic end generated code: output=97f2af6b5ff2ed81 input=6778dbd0f6e1ec16]*/
  314. {
  315. return PyNumber_InPlaceRshift(a, b);
  316. }
  317. /*[clinic input]
  318. _operator.iand = _operator.add
  319. Same as a &= b.
  320. [clinic start generated code]*/
  321. static PyObject *
  322. _operator_iand_impl(PyObject *module, PyObject *a, PyObject *b)
  323. /*[clinic end generated code: output=4599e9d40cbf7d00 input=71dfd8e70c156a7b]*/
  324. {
  325. return PyNumber_InPlaceAnd(a, b);
  326. }
  327. /*[clinic input]
  328. _operator.ixor = _operator.add
  329. Same as a ^= b.
  330. [clinic start generated code]*/
  331. static PyObject *
  332. _operator_ixor_impl(PyObject *module, PyObject *a, PyObject *b)
  333. /*[clinic end generated code: output=5ff881766872be03 input=695c32bec0604d86]*/
  334. {
  335. return PyNumber_InPlaceXor(a, b);
  336. }
  337. /*[clinic input]
  338. _operator.ior = _operator.add
  339. Same as a |= b.
  340. [clinic start generated code]*/
  341. static PyObject *
  342. _operator_ior_impl(PyObject *module, PyObject *a, PyObject *b)
  343. /*[clinic end generated code: output=48aac319445bf759 input=8f01d03eda9920cf]*/
  344. {
  345. return PyNumber_InPlaceOr(a, b);
  346. }
  347. /*[clinic input]
  348. _operator.concat = _operator.add
  349. Same as a + b, for a and b sequences.
  350. [clinic start generated code]*/
  351. static PyObject *
  352. _operator_concat_impl(PyObject *module, PyObject *a, PyObject *b)
  353. /*[clinic end generated code: output=80028390942c5f11 input=8544ccd5341a3658]*/
  354. {
  355. return PySequence_Concat(a, b);
  356. }
  357. /*[clinic input]
  358. _operator.iconcat = _operator.add
  359. Same as a += b, for a and b sequences.
  360. [clinic start generated code]*/
  361. static PyObject *
  362. _operator_iconcat_impl(PyObject *module, PyObject *a, PyObject *b)
  363. /*[clinic end generated code: output=3ea0a162ebb2e26d input=8f5fe5722fcd837e]*/
  364. {
  365. return PySequence_InPlaceConcat(a, b);
  366. }
  367. /*[clinic input]
  368. _operator.contains -> bool
  369. a: object
  370. b: object
  371. /
  372. Same as b in a (note reversed operands).
  373. [clinic start generated code]*/
  374. static int
  375. _operator_contains_impl(PyObject *module, PyObject *a, PyObject *b)
  376. /*[clinic end generated code: output=413b4dbe82b6ffc1 input=9122a69b505fde13]*/
  377. {
  378. return PySequence_Contains(a, b);
  379. }
  380. /*[clinic input]
  381. _operator.indexOf -> Py_ssize_t
  382. a: object
  383. b: object
  384. /
  385. Return the first index of b in a.
  386. [clinic start generated code]*/
  387. static Py_ssize_t
  388. _operator_indexOf_impl(PyObject *module, PyObject *a, PyObject *b)
  389. /*[clinic end generated code: output=c6226d8e0fb60fa6 input=8be2e43b6a6fffe3]*/
  390. {
  391. return PySequence_Index(a, b);
  392. }
  393. /*[clinic input]
  394. _operator.countOf = _operator.indexOf
  395. Return the number of items in a which are, or which equal, b.
  396. [clinic start generated code]*/
  397. static Py_ssize_t
  398. _operator_countOf_impl(PyObject *module, PyObject *a, PyObject *b)
  399. /*[clinic end generated code: output=9e1623197daf3382 input=93ea57f170f3f0bb]*/
  400. {
  401. return PySequence_Count(a, b);
  402. }
  403. /*[clinic input]
  404. _operator.getitem
  405. a: object
  406. b: object
  407. /
  408. Same as a[b].
  409. [clinic start generated code]*/
  410. static PyObject *
  411. _operator_getitem_impl(PyObject *module, PyObject *a, PyObject *b)
  412. /*[clinic end generated code: output=6c8d8101a676e594 input=6682797320e48845]*/
  413. {
  414. return PyObject_GetItem(a, b);
  415. }
  416. /*[clinic input]
  417. _operator.setitem
  418. a: object
  419. b: object
  420. c: object
  421. /
  422. Same as a[b] = c.
  423. [clinic start generated code]*/
  424. static PyObject *
  425. _operator_setitem_impl(PyObject *module, PyObject *a, PyObject *b,
  426. PyObject *c)
  427. /*[clinic end generated code: output=1324f9061ae99e25 input=ceaf453c4d3a58df]*/
  428. {
  429. if (-1 == PyObject_SetItem(a, b, c))
  430. return NULL;
  431. Py_RETURN_NONE;
  432. }
  433. /*[clinic input]
  434. _operator.delitem = _operator.getitem
  435. Same as del a[b].
  436. [clinic start generated code]*/
  437. static PyObject *
  438. _operator_delitem_impl(PyObject *module, PyObject *a, PyObject *b)
  439. /*[clinic end generated code: output=db18f61506295799 input=991bec56a0d3ec7f]*/
  440. {
  441. if (-1 == PyObject_DelItem(a, b))
  442. return NULL;
  443. Py_RETURN_NONE;
  444. }
  445. /*[clinic input]
  446. _operator.eq
  447. a: object
  448. b: object
  449. /
  450. Same as a == b.
  451. [clinic start generated code]*/
  452. static PyObject *
  453. _operator_eq_impl(PyObject *module, PyObject *a, PyObject *b)
  454. /*[clinic end generated code: output=8d7d46ed4135677c input=586fca687a95a83f]*/
  455. {
  456. return PyObject_RichCompare(a, b, Py_EQ);
  457. }
  458. /*[clinic input]
  459. _operator.ne = _operator.eq
  460. Same as a != b.
  461. [clinic start generated code]*/
  462. static PyObject *
  463. _operator_ne_impl(PyObject *module, PyObject *a, PyObject *b)
  464. /*[clinic end generated code: output=c99bd0c3a4c01297 input=5d88f23d35e9abac]*/
  465. {
  466. return PyObject_RichCompare(a, b, Py_NE);
  467. }
  468. /*[clinic input]
  469. _operator.lt = _operator.eq
  470. Same as a < b.
  471. [clinic start generated code]*/
  472. static PyObject *
  473. _operator_lt_impl(PyObject *module, PyObject *a, PyObject *b)
  474. /*[clinic end generated code: output=082d7c45c440e535 input=34a59ad6d39d3a2b]*/
  475. {
  476. return PyObject_RichCompare(a, b, Py_LT);
  477. }
  478. /*[clinic input]
  479. _operator.le = _operator.eq
  480. Same as a <= b.
  481. [clinic start generated code]*/
  482. static PyObject *
  483. _operator_le_impl(PyObject *module, PyObject *a, PyObject *b)
  484. /*[clinic end generated code: output=00970a2923d0ae17 input=b812a7860a0bef44]*/
  485. {
  486. return PyObject_RichCompare(a, b, Py_LE);
  487. }
  488. /*[clinic input]
  489. _operator.gt = _operator.eq
  490. Same as a > b.
  491. [clinic start generated code]*/
  492. static PyObject *
  493. _operator_gt_impl(PyObject *module, PyObject *a, PyObject *b)
  494. /*[clinic end generated code: output=8d373349ecf25641 input=9bdb45b995ada35b]*/
  495. {
  496. return PyObject_RichCompare(a, b, Py_GT);
  497. }
  498. /*[clinic input]
  499. _operator.ge = _operator.eq
  500. Same as a >= b.
  501. [clinic start generated code]*/
  502. static PyObject *
  503. _operator_ge_impl(PyObject *module, PyObject *a, PyObject *b)
  504. /*[clinic end generated code: output=7ce3882256d4b137 input=cf1dc4a5ca9c35f5]*/
  505. {
  506. return PyObject_RichCompare(a, b, Py_GE);
  507. }
  508. /*[clinic input]
  509. _operator.pow = _operator.add
  510. Same as a ** b.
  511. [clinic start generated code]*/
  512. static PyObject *
  513. _operator_pow_impl(PyObject *module, PyObject *a, PyObject *b)
  514. /*[clinic end generated code: output=09e668ad50036120 input=690b40f097ab1637]*/
  515. {
  516. return PyNumber_Power(a, b, Py_None);
  517. }
  518. /*[clinic input]
  519. _operator.ipow = _operator.add
  520. Same as a **= b.
  521. [clinic start generated code]*/
  522. static PyObject *
  523. _operator_ipow_impl(PyObject *module, PyObject *a, PyObject *b)
  524. /*[clinic end generated code: output=7189ff4d4367c808 input=f00623899d07499a]*/
  525. {
  526. return PyNumber_InPlacePower(a, b, Py_None);
  527. }
  528. /*[clinic input]
  529. _operator.index
  530. a: object
  531. /
  532. Same as a.__index__()
  533. [clinic start generated code]*/
  534. static PyObject *
  535. _operator_index(PyObject *module, PyObject *a)
  536. /*[clinic end generated code: output=d972b0764ac305fc input=6f54d50ea64a579c]*/
  537. {
  538. return PyNumber_Index(a);
  539. }
  540. /*[clinic input]
  541. _operator.is_ = _operator.add
  542. Same as a is b.
  543. [clinic start generated code]*/
  544. static PyObject *
  545. _operator_is__impl(PyObject *module, PyObject *a, PyObject *b)
  546. /*[clinic end generated code: output=bcd47a402e482e1d input=5fa9b97df03c427f]*/
  547. {
  548. PyObject *result = Py_Is(a, b) ? Py_True : Py_False;
  549. return Py_NewRef(result);
  550. }
  551. /*[clinic input]
  552. _operator.is_not = _operator.add
  553. Same as a is not b.
  554. [clinic start generated code]*/
  555. static PyObject *
  556. _operator_is_not_impl(PyObject *module, PyObject *a, PyObject *b)
  557. /*[clinic end generated code: output=491a1f2f81f6c7f9 input=5a93f7e1a93535f1]*/
  558. {
  559. PyObject *result;
  560. result = (a != b) ? Py_True : Py_False;
  561. return Py_NewRef(result);
  562. }
  563. /* compare_digest **********************************************************/
  564. /*
  565. * timing safe compare
  566. *
  567. * Returns 1 if the strings are equal.
  568. * In case of len(a) != len(b) the function tries to keep the timing
  569. * dependent on the length of b. CPU cache locality may still alter timing
  570. * a bit.
  571. */
  572. static int
  573. _tscmp(const unsigned char *a, const unsigned char *b,
  574. Py_ssize_t len_a, Py_ssize_t len_b)
  575. {
  576. /* The volatile type declarations make sure that the compiler has no
  577. * chance to optimize and fold the code in any way that may change
  578. * the timing.
  579. */
  580. volatile Py_ssize_t length;
  581. volatile const unsigned char *left;
  582. volatile const unsigned char *right;
  583. Py_ssize_t i;
  584. volatile unsigned char result;
  585. /* loop count depends on length of b */
  586. length = len_b;
  587. left = NULL;
  588. right = b;
  589. /* don't use else here to keep the amount of CPU instructions constant,
  590. * volatile forces re-evaluation
  591. * */
  592. if (len_a == length) {
  593. left = *((volatile const unsigned char**)&a);
  594. result = 0;
  595. }
  596. if (len_a != length) {
  597. left = b;
  598. result = 1;
  599. }
  600. for (i=0; i < length; i++) {
  601. result |= *left++ ^ *right++;
  602. }
  603. return (result == 0);
  604. }
  605. /*[clinic input]
  606. _operator.length_hint -> Py_ssize_t
  607. obj: object
  608. default: Py_ssize_t = 0
  609. /
  610. Return an estimate of the number of items in obj.
  611. This is useful for presizing containers when building from an iterable.
  612. If the object supports len(), the result will be exact.
  613. Otherwise, it may over- or under-estimate by an arbitrary amount.
  614. The result will be an integer >= 0.
  615. [clinic start generated code]*/
  616. static Py_ssize_t
  617. _operator_length_hint_impl(PyObject *module, PyObject *obj,
  618. Py_ssize_t default_value)
  619. /*[clinic end generated code: output=01d469edc1d612ad input=65ed29f04401e96a]*/
  620. {
  621. return PyObject_LengthHint(obj, default_value);
  622. }
  623. /* NOTE: Keep in sync with _hashopenssl.c implementation. */
  624. /*[clinic input]
  625. _operator._compare_digest = _operator.eq
  626. Return 'a == b'.
  627. This function uses an approach designed to prevent
  628. timing analysis, making it appropriate for cryptography.
  629. a and b must both be of the same type: either str (ASCII only),
  630. or any bytes-like object.
  631. Note: If a and b are of different lengths, or if an error occurs,
  632. a timing attack could theoretically reveal information about the
  633. types and lengths of a and b--but not their values.
  634. [clinic start generated code]*/
  635. static PyObject *
  636. _operator__compare_digest_impl(PyObject *module, PyObject *a, PyObject *b)
  637. /*[clinic end generated code: output=11d452bdd3a23cbc input=9ac7e2c4e30bc356]*/
  638. {
  639. int rc;
  640. /* ASCII unicode string */
  641. if(PyUnicode_Check(a) && PyUnicode_Check(b)) {
  642. if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
  643. return NULL;
  644. }
  645. if (!PyUnicode_IS_ASCII(a) || !PyUnicode_IS_ASCII(b)) {
  646. PyErr_SetString(PyExc_TypeError,
  647. "comparing strings with non-ASCII characters is "
  648. "not supported");
  649. return NULL;
  650. }
  651. rc = _tscmp(PyUnicode_DATA(a),
  652. PyUnicode_DATA(b),
  653. PyUnicode_GET_LENGTH(a),
  654. PyUnicode_GET_LENGTH(b));
  655. }
  656. /* fallback to buffer interface for bytes, bytearray and other */
  657. else {
  658. Py_buffer view_a;
  659. Py_buffer view_b;
  660. if (PyObject_CheckBuffer(a) == 0 && PyObject_CheckBuffer(b) == 0) {
  661. PyErr_Format(PyExc_TypeError,
  662. "unsupported operand types(s) or combination of types: "
  663. "'%.100s' and '%.100s'",
  664. Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
  665. return NULL;
  666. }
  667. if (PyObject_GetBuffer(a, &view_a, PyBUF_SIMPLE) == -1) {
  668. return NULL;
  669. }
  670. if (view_a.ndim > 1) {
  671. PyErr_SetString(PyExc_BufferError,
  672. "Buffer must be single dimension");
  673. PyBuffer_Release(&view_a);
  674. return NULL;
  675. }
  676. if (PyObject_GetBuffer(b, &view_b, PyBUF_SIMPLE) == -1) {
  677. PyBuffer_Release(&view_a);
  678. return NULL;
  679. }
  680. if (view_b.ndim > 1) {
  681. PyErr_SetString(PyExc_BufferError,
  682. "Buffer must be single dimension");
  683. PyBuffer_Release(&view_a);
  684. PyBuffer_Release(&view_b);
  685. return NULL;
  686. }
  687. rc = _tscmp((const unsigned char*)view_a.buf,
  688. (const unsigned char*)view_b.buf,
  689. view_a.len,
  690. view_b.len);
  691. PyBuffer_Release(&view_a);
  692. PyBuffer_Release(&view_b);
  693. }
  694. return PyBool_FromLong(rc);
  695. }
  696. PyDoc_STRVAR(_operator_call__doc__,
  697. "call($module, obj, /, *args, **kwargs)\n"
  698. "--\n"
  699. "\n"
  700. "Same as obj(*args, **kwargs).");
  701. #define _OPERATOR_CALL_METHODDEF \
  702. {"call", _PyCFunction_CAST(_operator_call), METH_FASTCALL | METH_KEYWORDS, _operator_call__doc__},
  703. static PyObject *
  704. _operator_call(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  705. {
  706. if (!_PyArg_CheckPositional("call", nargs, 1, PY_SSIZE_T_MAX)) {
  707. return NULL;
  708. }
  709. return PyObject_Vectorcall(
  710. args[0],
  711. &args[1], (PyVectorcall_NARGS(nargs) - 1) | PY_VECTORCALL_ARGUMENTS_OFFSET,
  712. kwnames);
  713. }
  714. /* operator methods **********************************************************/
  715. static struct PyMethodDef operator_methods[] = {
  716. _OPERATOR_TRUTH_METHODDEF
  717. _OPERATOR_CONTAINS_METHODDEF
  718. _OPERATOR_INDEXOF_METHODDEF
  719. _OPERATOR_COUNTOF_METHODDEF
  720. _OPERATOR_IS__METHODDEF
  721. _OPERATOR_IS_NOT_METHODDEF
  722. _OPERATOR_INDEX_METHODDEF
  723. _OPERATOR_ADD_METHODDEF
  724. _OPERATOR_SUB_METHODDEF
  725. _OPERATOR_MUL_METHODDEF
  726. _OPERATOR_MATMUL_METHODDEF
  727. _OPERATOR_FLOORDIV_METHODDEF
  728. _OPERATOR_TRUEDIV_METHODDEF
  729. _OPERATOR_MOD_METHODDEF
  730. _OPERATOR_NEG_METHODDEF
  731. _OPERATOR_POS_METHODDEF
  732. _OPERATOR_ABS_METHODDEF
  733. _OPERATOR_INV_METHODDEF
  734. _OPERATOR_INVERT_METHODDEF
  735. _OPERATOR_LSHIFT_METHODDEF
  736. _OPERATOR_RSHIFT_METHODDEF
  737. _OPERATOR_NOT__METHODDEF
  738. _OPERATOR_AND__METHODDEF
  739. _OPERATOR_XOR_METHODDEF
  740. _OPERATOR_OR__METHODDEF
  741. _OPERATOR_IADD_METHODDEF
  742. _OPERATOR_ISUB_METHODDEF
  743. _OPERATOR_IMUL_METHODDEF
  744. _OPERATOR_IMATMUL_METHODDEF
  745. _OPERATOR_IFLOORDIV_METHODDEF
  746. _OPERATOR_ITRUEDIV_METHODDEF
  747. _OPERATOR_IMOD_METHODDEF
  748. _OPERATOR_ILSHIFT_METHODDEF
  749. _OPERATOR_IRSHIFT_METHODDEF
  750. _OPERATOR_IAND_METHODDEF
  751. _OPERATOR_IXOR_METHODDEF
  752. _OPERATOR_IOR_METHODDEF
  753. _OPERATOR_CONCAT_METHODDEF
  754. _OPERATOR_ICONCAT_METHODDEF
  755. _OPERATOR_GETITEM_METHODDEF
  756. _OPERATOR_SETITEM_METHODDEF
  757. _OPERATOR_DELITEM_METHODDEF
  758. _OPERATOR_POW_METHODDEF
  759. _OPERATOR_IPOW_METHODDEF
  760. _OPERATOR_EQ_METHODDEF
  761. _OPERATOR_NE_METHODDEF
  762. _OPERATOR_LT_METHODDEF
  763. _OPERATOR_LE_METHODDEF
  764. _OPERATOR_GT_METHODDEF
  765. _OPERATOR_GE_METHODDEF
  766. _OPERATOR__COMPARE_DIGEST_METHODDEF
  767. _OPERATOR_LENGTH_HINT_METHODDEF
  768. _OPERATOR_CALL_METHODDEF
  769. {NULL, NULL} /* sentinel */
  770. };
  771. /* itemgetter object **********************************************************/
  772. typedef struct {
  773. PyObject_HEAD
  774. Py_ssize_t nitems;
  775. PyObject *item;
  776. Py_ssize_t index; // -1 unless *item* is a single non-negative integer index
  777. vectorcallfunc vectorcall;
  778. } itemgetterobject;
  779. // Forward declarations
  780. static PyObject *
  781. itemgetter_vectorcall(PyObject *, PyObject *const *, size_t, PyObject *);
  782. static PyObject *
  783. itemgetter_call_impl(itemgetterobject *, PyObject *);
  784. /* AC 3.5: treats first argument as an iterable, otherwise uses *args */
  785. static PyObject *
  786. itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  787. {
  788. itemgetterobject *ig;
  789. PyObject *item;
  790. Py_ssize_t nitems;
  791. Py_ssize_t index;
  792. if (!_PyArg_NoKeywords("itemgetter", kwds))
  793. return NULL;
  794. nitems = PyTuple_GET_SIZE(args);
  795. if (nitems <= 1) {
  796. if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
  797. return NULL;
  798. } else {
  799. item = args;
  800. }
  801. _operator_state *state = _PyType_GetModuleState(type);
  802. /* create itemgetterobject structure */
  803. ig = PyObject_GC_New(itemgetterobject, (PyTypeObject *) state->itemgetter_type);
  804. if (ig == NULL) {
  805. return NULL;
  806. }
  807. ig->item = Py_NewRef(item);
  808. ig->nitems = nitems;
  809. ig->index = -1;
  810. if (PyLong_CheckExact(item)) {
  811. index = PyLong_AsSsize_t(item);
  812. if (index < 0) {
  813. /* If we get here, then either the index conversion failed
  814. * due to being out of range, or the index was a negative
  815. * integer. Either way, we clear any possible exception
  816. * and fall back to the slow path, where ig->index is -1.
  817. */
  818. PyErr_Clear();
  819. }
  820. else {
  821. ig->index = index;
  822. }
  823. }
  824. ig->vectorcall = (vectorcallfunc)itemgetter_vectorcall;
  825. PyObject_GC_Track(ig);
  826. return (PyObject *)ig;
  827. }
  828. static int
  829. itemgetter_clear(itemgetterobject *ig)
  830. {
  831. Py_CLEAR(ig->item);
  832. return 0;
  833. }
  834. static void
  835. itemgetter_dealloc(itemgetterobject *ig)
  836. {
  837. PyTypeObject *tp = Py_TYPE(ig);
  838. PyObject_GC_UnTrack(ig);
  839. (void)itemgetter_clear(ig);
  840. tp->tp_free(ig);
  841. Py_DECREF(tp);
  842. }
  843. static int
  844. itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
  845. {
  846. Py_VISIT(Py_TYPE(ig));
  847. Py_VISIT(ig->item);
  848. return 0;
  849. }
  850. static PyObject *
  851. itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
  852. {
  853. assert(PyTuple_CheckExact(args));
  854. if (!_PyArg_NoKeywords("itemgetter", kw))
  855. return NULL;
  856. if (!_PyArg_CheckPositional("itemgetter", PyTuple_GET_SIZE(args), 1, 1))
  857. return NULL;
  858. return itemgetter_call_impl(ig, PyTuple_GET_ITEM(args, 0));
  859. }
  860. static PyObject *
  861. itemgetter_vectorcall(PyObject *ig, PyObject *const *args,
  862. size_t nargsf, PyObject *kwnames)
  863. {
  864. if (!_PyArg_NoKwnames("itemgetter", kwnames)) {
  865. return NULL;
  866. }
  867. Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
  868. if (!_PyArg_CheckPositional("itemgetter", nargs, 1, 1)) {
  869. return NULL;
  870. }
  871. return itemgetter_call_impl((itemgetterobject *)ig, args[0]);
  872. }
  873. static PyObject *
  874. itemgetter_call_impl(itemgetterobject *ig, PyObject *obj)
  875. {
  876. PyObject *result;
  877. Py_ssize_t i, nitems=ig->nitems;
  878. if (nitems == 1) {
  879. if (ig->index >= 0
  880. && PyTuple_CheckExact(obj)
  881. && ig->index < PyTuple_GET_SIZE(obj))
  882. {
  883. result = PyTuple_GET_ITEM(obj, ig->index);
  884. return Py_NewRef(result);
  885. }
  886. return PyObject_GetItem(obj, ig->item);
  887. }
  888. assert(PyTuple_Check(ig->item));
  889. assert(PyTuple_GET_SIZE(ig->item) == nitems);
  890. result = PyTuple_New(nitems);
  891. if (result == NULL)
  892. return NULL;
  893. for (i=0 ; i < nitems ; i++) {
  894. PyObject *item, *val;
  895. item = PyTuple_GET_ITEM(ig->item, i);
  896. val = PyObject_GetItem(obj, item);
  897. if (val == NULL) {
  898. Py_DECREF(result);
  899. return NULL;
  900. }
  901. PyTuple_SET_ITEM(result, i, val);
  902. }
  903. return result;
  904. }
  905. static PyObject *
  906. itemgetter_repr(itemgetterobject *ig)
  907. {
  908. PyObject *repr;
  909. const char *reprfmt;
  910. int status = Py_ReprEnter((PyObject *)ig);
  911. if (status != 0) {
  912. if (status < 0)
  913. return NULL;
  914. return PyUnicode_FromFormat("%s(...)", Py_TYPE(ig)->tp_name);
  915. }
  916. reprfmt = ig->nitems == 1 ? "%s(%R)" : "%s%R";
  917. repr = PyUnicode_FromFormat(reprfmt, Py_TYPE(ig)->tp_name, ig->item);
  918. Py_ReprLeave((PyObject *)ig);
  919. return repr;
  920. }
  921. static PyObject *
  922. itemgetter_reduce(itemgetterobject *ig, PyObject *Py_UNUSED(ignored))
  923. {
  924. if (ig->nitems == 1)
  925. return Py_BuildValue("O(O)", Py_TYPE(ig), ig->item);
  926. return PyTuple_Pack(2, Py_TYPE(ig), ig->item);
  927. }
  928. PyDoc_STRVAR(reduce_doc, "Return state information for pickling");
  929. static PyMethodDef itemgetter_methods[] = {
  930. {"__reduce__", (PyCFunction)itemgetter_reduce, METH_NOARGS,
  931. reduce_doc},
  932. {NULL}
  933. };
  934. static PyMemberDef itemgetter_members[] = {
  935. {"__vectorcalloffset__", T_PYSSIZET, offsetof(itemgetterobject, vectorcall), READONLY},
  936. {NULL} /* Sentinel */
  937. };
  938. PyDoc_STRVAR(itemgetter_doc,
  939. "itemgetter(item, /, *items)\n--\n\n\
  940. Return a callable object that fetches the given item(s) from its operand.\n\
  941. After f = itemgetter(2), the call f(r) returns r[2].\n\
  942. After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])");
  943. static PyType_Slot itemgetter_type_slots[] = {
  944. {Py_tp_doc, (void *)itemgetter_doc},
  945. {Py_tp_dealloc, itemgetter_dealloc},
  946. {Py_tp_call, itemgetter_call},
  947. {Py_tp_traverse, itemgetter_traverse},
  948. {Py_tp_clear, itemgetter_clear},
  949. {Py_tp_methods, itemgetter_methods},
  950. {Py_tp_members, itemgetter_members},
  951. {Py_tp_new, itemgetter_new},
  952. {Py_tp_getattro, PyObject_GenericGetAttr},
  953. {Py_tp_repr, itemgetter_repr},
  954. {0, 0}
  955. };
  956. static PyType_Spec itemgetter_type_spec = {
  957. .name = "operator.itemgetter",
  958. .basicsize = sizeof(itemgetterobject),
  959. .itemsize = 0,
  960. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  961. Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_VECTORCALL),
  962. .slots = itemgetter_type_slots,
  963. };
  964. /* attrgetter object **********************************************************/
  965. typedef struct {
  966. PyObject_HEAD
  967. Py_ssize_t nattrs;
  968. PyObject *attr;
  969. vectorcallfunc vectorcall;
  970. } attrgetterobject;
  971. // Forward declarations
  972. static PyObject *
  973. attrgetter_vectorcall(PyObject *, PyObject *const *, size_t, PyObject *);
  974. static PyObject *
  975. attrgetter_call_impl(attrgetterobject *, PyObject *);
  976. /* AC 3.5: treats first argument as an iterable, otherwise uses *args */
  977. static PyObject *
  978. attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  979. {
  980. attrgetterobject *ag;
  981. PyObject *attr;
  982. Py_ssize_t nattrs, idx, char_idx;
  983. if (!_PyArg_NoKeywords("attrgetter", kwds))
  984. return NULL;
  985. nattrs = PyTuple_GET_SIZE(args);
  986. if (nattrs <= 1) {
  987. if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
  988. return NULL;
  989. }
  990. attr = PyTuple_New(nattrs);
  991. if (attr == NULL)
  992. return NULL;
  993. /* prepare attr while checking args */
  994. for (idx = 0; idx < nattrs; ++idx) {
  995. PyObject *item = PyTuple_GET_ITEM(args, idx);
  996. int dot_count;
  997. if (!PyUnicode_Check(item)) {
  998. PyErr_SetString(PyExc_TypeError,
  999. "attribute name must be a string");
  1000. Py_DECREF(attr);
  1001. return NULL;
  1002. }
  1003. if (PyUnicode_READY(item)) {
  1004. Py_DECREF(attr);
  1005. return NULL;
  1006. }
  1007. Py_ssize_t item_len = PyUnicode_GET_LENGTH(item);
  1008. int kind = PyUnicode_KIND(item);
  1009. const void *data = PyUnicode_DATA(item);
  1010. /* check whether the string is dotted */
  1011. dot_count = 0;
  1012. for (char_idx = 0; char_idx < item_len; ++char_idx) {
  1013. if (PyUnicode_READ(kind, data, char_idx) == '.')
  1014. ++dot_count;
  1015. }
  1016. if (dot_count == 0) {
  1017. Py_INCREF(item);
  1018. PyUnicode_InternInPlace(&item);
  1019. PyTuple_SET_ITEM(attr, idx, item);
  1020. } else { /* make it a tuple of non-dotted attrnames */
  1021. PyObject *attr_chain = PyTuple_New(dot_count + 1);
  1022. PyObject *attr_chain_item;
  1023. Py_ssize_t unibuff_from = 0;
  1024. Py_ssize_t unibuff_till = 0;
  1025. Py_ssize_t attr_chain_idx = 0;
  1026. if (attr_chain == NULL) {
  1027. Py_DECREF(attr);
  1028. return NULL;
  1029. }
  1030. for (; dot_count > 0; --dot_count) {
  1031. while (PyUnicode_READ(kind, data, unibuff_till) != '.') {
  1032. ++unibuff_till;
  1033. }
  1034. attr_chain_item = PyUnicode_Substring(item,
  1035. unibuff_from,
  1036. unibuff_till);
  1037. if (attr_chain_item == NULL) {
  1038. Py_DECREF(attr_chain);
  1039. Py_DECREF(attr);
  1040. return NULL;
  1041. }
  1042. PyUnicode_InternInPlace(&attr_chain_item);
  1043. PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item);
  1044. ++attr_chain_idx;
  1045. unibuff_till = unibuff_from = unibuff_till + 1;
  1046. }
  1047. /* now add the last dotless name */
  1048. attr_chain_item = PyUnicode_Substring(item,
  1049. unibuff_from, item_len);
  1050. if (attr_chain_item == NULL) {
  1051. Py_DECREF(attr_chain);
  1052. Py_DECREF(attr);
  1053. return NULL;
  1054. }
  1055. PyUnicode_InternInPlace(&attr_chain_item);
  1056. PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item);
  1057. PyTuple_SET_ITEM(attr, idx, attr_chain);
  1058. }
  1059. }
  1060. _operator_state *state = _PyType_GetModuleState(type);
  1061. /* create attrgetterobject structure */
  1062. ag = PyObject_GC_New(attrgetterobject, (PyTypeObject *)state->attrgetter_type);
  1063. if (ag == NULL) {
  1064. Py_DECREF(attr);
  1065. return NULL;
  1066. }
  1067. ag->attr = attr;
  1068. ag->nattrs = nattrs;
  1069. ag->vectorcall = (vectorcallfunc)attrgetter_vectorcall;
  1070. PyObject_GC_Track(ag);
  1071. return (PyObject *)ag;
  1072. }
  1073. static int
  1074. attrgetter_clear(attrgetterobject *ag)
  1075. {
  1076. Py_CLEAR(ag->attr);
  1077. return 0;
  1078. }
  1079. static void
  1080. attrgetter_dealloc(attrgetterobject *ag)
  1081. {
  1082. PyTypeObject *tp = Py_TYPE(ag);
  1083. PyObject_GC_UnTrack(ag);
  1084. (void)attrgetter_clear(ag);
  1085. tp->tp_free(ag);
  1086. Py_DECREF(tp);
  1087. }
  1088. static int
  1089. attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
  1090. {
  1091. Py_VISIT(ag->attr);
  1092. Py_VISIT(Py_TYPE(ag));
  1093. return 0;
  1094. }
  1095. static PyObject *
  1096. dotted_getattr(PyObject *obj, PyObject *attr)
  1097. {
  1098. PyObject *newobj;
  1099. /* attr is either a tuple or instance of str.
  1100. Ensured by the setup code of attrgetter_new */
  1101. if (PyTuple_CheckExact(attr)) { /* chained getattr */
  1102. Py_ssize_t name_idx = 0, name_count;
  1103. PyObject *attr_name;
  1104. name_count = PyTuple_GET_SIZE(attr);
  1105. Py_INCREF(obj);
  1106. for (name_idx = 0; name_idx < name_count; ++name_idx) {
  1107. attr_name = PyTuple_GET_ITEM(attr, name_idx);
  1108. newobj = PyObject_GetAttr(obj, attr_name);
  1109. Py_DECREF(obj);
  1110. if (newobj == NULL) {
  1111. return NULL;
  1112. }
  1113. /* here */
  1114. obj = newobj;
  1115. }
  1116. } else { /* single getattr */
  1117. newobj = PyObject_GetAttr(obj, attr);
  1118. if (newobj == NULL)
  1119. return NULL;
  1120. obj = newobj;
  1121. }
  1122. return obj;
  1123. }
  1124. static PyObject *
  1125. attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
  1126. {
  1127. if (!_PyArg_NoKeywords("attrgetter", kw))
  1128. return NULL;
  1129. if (!_PyArg_CheckPositional("attrgetter", PyTuple_GET_SIZE(args), 1, 1))
  1130. return NULL;
  1131. return attrgetter_call_impl(ag, PyTuple_GET_ITEM(args, 0));
  1132. }
  1133. static PyObject *
  1134. attrgetter_vectorcall(PyObject *ag, PyObject *const *args, size_t nargsf, PyObject *kwnames)
  1135. {
  1136. if (!_PyArg_NoKwnames("attrgetter", kwnames)) {
  1137. return NULL;
  1138. }
  1139. Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
  1140. if (!_PyArg_CheckPositional("attrgetter", nargs, 1, 1)) {
  1141. return NULL;
  1142. }
  1143. return attrgetter_call_impl((attrgetterobject *)ag, args[0]);
  1144. }
  1145. static PyObject *
  1146. attrgetter_call_impl(attrgetterobject *ag, PyObject *obj)
  1147. {
  1148. PyObject *result;
  1149. Py_ssize_t i, nattrs=ag->nattrs;
  1150. if (ag->nattrs == 1) {
  1151. /* ag->attr is always a tuple */
  1152. return dotted_getattr(obj, PyTuple_GET_ITEM(ag->attr, 0));
  1153. }
  1154. assert(PyTuple_Check(ag->attr));
  1155. assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
  1156. result = PyTuple_New(nattrs);
  1157. if (result == NULL)
  1158. return NULL;
  1159. for (i=0 ; i < nattrs ; i++) {
  1160. PyObject *attr, *val;
  1161. attr = PyTuple_GET_ITEM(ag->attr, i);
  1162. val = dotted_getattr(obj, attr);
  1163. if (val == NULL) {
  1164. Py_DECREF(result);
  1165. return NULL;
  1166. }
  1167. PyTuple_SET_ITEM(result, i, val);
  1168. }
  1169. return result;
  1170. }
  1171. static PyObject *
  1172. dotjoinattr(PyObject *attr, PyObject **attrsep)
  1173. {
  1174. if (PyTuple_CheckExact(attr)) {
  1175. if (*attrsep == NULL) {
  1176. *attrsep = PyUnicode_FromString(".");
  1177. if (*attrsep == NULL)
  1178. return NULL;
  1179. }
  1180. return PyUnicode_Join(*attrsep, attr);
  1181. } else {
  1182. return Py_NewRef(attr);
  1183. }
  1184. }
  1185. static PyObject *
  1186. attrgetter_args(attrgetterobject *ag)
  1187. {
  1188. Py_ssize_t i;
  1189. PyObject *attrsep = NULL;
  1190. PyObject *attrstrings = PyTuple_New(ag->nattrs);
  1191. if (attrstrings == NULL)
  1192. return NULL;
  1193. for (i = 0; i < ag->nattrs; ++i) {
  1194. PyObject *attr = PyTuple_GET_ITEM(ag->attr, i);
  1195. PyObject *attrstr = dotjoinattr(attr, &attrsep);
  1196. if (attrstr == NULL) {
  1197. Py_XDECREF(attrsep);
  1198. Py_DECREF(attrstrings);
  1199. return NULL;
  1200. }
  1201. PyTuple_SET_ITEM(attrstrings, i, attrstr);
  1202. }
  1203. Py_XDECREF(attrsep);
  1204. return attrstrings;
  1205. }
  1206. static PyObject *
  1207. attrgetter_repr(attrgetterobject *ag)
  1208. {
  1209. PyObject *repr = NULL;
  1210. int status = Py_ReprEnter((PyObject *)ag);
  1211. if (status != 0) {
  1212. if (status < 0)
  1213. return NULL;
  1214. return PyUnicode_FromFormat("%s(...)", Py_TYPE(ag)->tp_name);
  1215. }
  1216. if (ag->nattrs == 1) {
  1217. PyObject *attrsep = NULL;
  1218. PyObject *attr = dotjoinattr(PyTuple_GET_ITEM(ag->attr, 0), &attrsep);
  1219. if (attr != NULL) {
  1220. repr = PyUnicode_FromFormat("%s(%R)", Py_TYPE(ag)->tp_name, attr);
  1221. Py_DECREF(attr);
  1222. }
  1223. Py_XDECREF(attrsep);
  1224. }
  1225. else {
  1226. PyObject *attrstrings = attrgetter_args(ag);
  1227. if (attrstrings != NULL) {
  1228. repr = PyUnicode_FromFormat("%s%R",
  1229. Py_TYPE(ag)->tp_name, attrstrings);
  1230. Py_DECREF(attrstrings);
  1231. }
  1232. }
  1233. Py_ReprLeave((PyObject *)ag);
  1234. return repr;
  1235. }
  1236. static PyObject *
  1237. attrgetter_reduce(attrgetterobject *ag, PyObject *Py_UNUSED(ignored))
  1238. {
  1239. PyObject *attrstrings = attrgetter_args(ag);
  1240. if (attrstrings == NULL)
  1241. return NULL;
  1242. return Py_BuildValue("ON", Py_TYPE(ag), attrstrings);
  1243. }
  1244. static PyMethodDef attrgetter_methods[] = {
  1245. {"__reduce__", (PyCFunction)attrgetter_reduce, METH_NOARGS,
  1246. reduce_doc},
  1247. {NULL}
  1248. };
  1249. static PyMemberDef attrgetter_members[] = {
  1250. {"__vectorcalloffset__", T_PYSSIZET, offsetof(attrgetterobject, vectorcall), READONLY},
  1251. {NULL} /* Sentinel*/
  1252. };
  1253. PyDoc_STRVAR(attrgetter_doc,
  1254. "attrgetter(attr, /, *attrs)\n--\n\n\
  1255. Return a callable object that fetches the given attribute(s) from its operand.\n\
  1256. After f = attrgetter('name'), the call f(r) returns r.name.\n\
  1257. After g = attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
  1258. After h = attrgetter('name.first', 'name.last'), the call h(r) returns\n\
  1259. (r.name.first, r.name.last).");
  1260. static PyType_Slot attrgetter_type_slots[] = {
  1261. {Py_tp_doc, (void *)attrgetter_doc},
  1262. {Py_tp_dealloc, attrgetter_dealloc},
  1263. {Py_tp_call, attrgetter_call},
  1264. {Py_tp_traverse, attrgetter_traverse},
  1265. {Py_tp_clear, attrgetter_clear},
  1266. {Py_tp_methods, attrgetter_methods},
  1267. {Py_tp_members, attrgetter_members},
  1268. {Py_tp_new, attrgetter_new},
  1269. {Py_tp_getattro, PyObject_GenericGetAttr},
  1270. {Py_tp_repr, attrgetter_repr},
  1271. {0, 0}
  1272. };
  1273. static PyType_Spec attrgetter_type_spec = {
  1274. .name = "operator.attrgetter",
  1275. .basicsize = sizeof(attrgetterobject),
  1276. .itemsize = 0,
  1277. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  1278. Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_VECTORCALL),
  1279. .slots = attrgetter_type_slots,
  1280. };
  1281. /* methodcaller object **********************************************************/
  1282. typedef struct {
  1283. PyObject_HEAD
  1284. PyObject *name;
  1285. PyObject *args;
  1286. PyObject *kwds;
  1287. } methodcallerobject;
  1288. /* AC 3.5: variable number of arguments, not currently support by AC */
  1289. static PyObject *
  1290. methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  1291. {
  1292. methodcallerobject *mc;
  1293. PyObject *name;
  1294. if (PyTuple_GET_SIZE(args) < 1) {
  1295. PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
  1296. "one argument, the method name");
  1297. return NULL;
  1298. }
  1299. name = PyTuple_GET_ITEM(args, 0);
  1300. if (!PyUnicode_Check(name)) {
  1301. PyErr_SetString(PyExc_TypeError,
  1302. "method name must be a string");
  1303. return NULL;
  1304. }
  1305. _operator_state *state = _PyType_GetModuleState(type);
  1306. /* create methodcallerobject structure */
  1307. mc = PyObject_GC_New(methodcallerobject, (PyTypeObject *)state->methodcaller_type);
  1308. if (mc == NULL) {
  1309. return NULL;
  1310. }
  1311. name = PyTuple_GET_ITEM(args, 0);
  1312. Py_INCREF(name);
  1313. PyUnicode_InternInPlace(&name);
  1314. mc->name = name;
  1315. mc->kwds = Py_XNewRef(kwds);
  1316. mc->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
  1317. if (mc->args == NULL) {
  1318. Py_DECREF(mc);
  1319. return NULL;
  1320. }
  1321. PyObject_GC_Track(mc);
  1322. return (PyObject *)mc;
  1323. }
  1324. static int
  1325. methodcaller_clear(methodcallerobject *mc)
  1326. {
  1327. Py_CLEAR(mc->name);
  1328. Py_CLEAR(mc->args);
  1329. Py_CLEAR(mc->kwds);
  1330. return 0;
  1331. }
  1332. static void
  1333. methodcaller_dealloc(methodcallerobject *mc)
  1334. {
  1335. PyTypeObject *tp = Py_TYPE(mc);
  1336. PyObject_GC_UnTrack(mc);
  1337. (void)methodcaller_clear(mc);
  1338. tp->tp_free(mc);
  1339. Py_DECREF(tp);
  1340. }
  1341. static int
  1342. methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg)
  1343. {
  1344. Py_VISIT(mc->name);
  1345. Py_VISIT(mc->args);
  1346. Py_VISIT(mc->kwds);
  1347. Py_VISIT(Py_TYPE(mc));
  1348. return 0;
  1349. }
  1350. static PyObject *
  1351. methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw)
  1352. {
  1353. PyObject *method, *obj, *result;
  1354. if (!_PyArg_NoKeywords("methodcaller", kw))
  1355. return NULL;
  1356. if (!_PyArg_CheckPositional("methodcaller", PyTuple_GET_SIZE(args), 1, 1))
  1357. return NULL;
  1358. obj = PyTuple_GET_ITEM(args, 0);
  1359. method = PyObject_GetAttr(obj, mc->name);
  1360. if (method == NULL)
  1361. return NULL;
  1362. result = PyObject_Call(method, mc->args, mc->kwds);
  1363. Py_DECREF(method);
  1364. return result;
  1365. }
  1366. static PyObject *
  1367. methodcaller_repr(methodcallerobject *mc)
  1368. {
  1369. PyObject *argreprs, *repr = NULL, *sep, *joinedargreprs;
  1370. Py_ssize_t numtotalargs, numposargs, numkwdargs, i;
  1371. int status = Py_ReprEnter((PyObject *)mc);
  1372. if (status != 0) {
  1373. if (status < 0)
  1374. return NULL;
  1375. return PyUnicode_FromFormat("%s(...)", Py_TYPE(mc)->tp_name);
  1376. }
  1377. numkwdargs = mc->kwds != NULL ? PyDict_GET_SIZE(mc->kwds) : 0;
  1378. numposargs = PyTuple_GET_SIZE(mc->args);
  1379. numtotalargs = numposargs + numkwdargs;
  1380. if (numtotalargs == 0) {
  1381. repr = PyUnicode_FromFormat("%s(%R)", Py_TYPE(mc)->tp_name, mc->name);
  1382. Py_ReprLeave((PyObject *)mc);
  1383. return repr;
  1384. }
  1385. argreprs = PyTuple_New(numtotalargs);
  1386. if (argreprs == NULL) {
  1387. Py_ReprLeave((PyObject *)mc);
  1388. return NULL;
  1389. }
  1390. for (i = 0; i < numposargs; ++i) {
  1391. PyObject *onerepr = PyObject_Repr(PyTuple_GET_ITEM(mc->args, i));
  1392. if (onerepr == NULL)
  1393. goto done;
  1394. PyTuple_SET_ITEM(argreprs, i, onerepr);
  1395. }
  1396. if (numkwdargs != 0) {
  1397. PyObject *key, *value;
  1398. Py_ssize_t pos = 0;
  1399. while (PyDict_Next(mc->kwds, &pos, &key, &value)) {
  1400. PyObject *onerepr = PyUnicode_FromFormat("%U=%R", key, value);
  1401. if (onerepr == NULL)
  1402. goto done;
  1403. if (i >= numtotalargs) {
  1404. i = -1;
  1405. Py_DECREF(onerepr);
  1406. break;
  1407. }
  1408. PyTuple_SET_ITEM(argreprs, i, onerepr);
  1409. ++i;
  1410. }
  1411. if (i != numtotalargs) {
  1412. PyErr_SetString(PyExc_RuntimeError,
  1413. "keywords dict changed size during iteration");
  1414. goto done;
  1415. }
  1416. }
  1417. sep = PyUnicode_FromString(", ");
  1418. if (sep == NULL)
  1419. goto done;
  1420. joinedargreprs = PyUnicode_Join(sep, argreprs);
  1421. Py_DECREF(sep);
  1422. if (joinedargreprs == NULL)
  1423. goto done;
  1424. repr = PyUnicode_FromFormat("%s(%R, %U)", Py_TYPE(mc)->tp_name,
  1425. mc->name, joinedargreprs);
  1426. Py_DECREF(joinedargreprs);
  1427. done:
  1428. Py_DECREF(argreprs);
  1429. Py_ReprLeave((PyObject *)mc);
  1430. return repr;
  1431. }
  1432. static PyObject *
  1433. methodcaller_reduce(methodcallerobject *mc, PyObject *Py_UNUSED(ignored))
  1434. {
  1435. PyObject *newargs;
  1436. if (!mc->kwds || PyDict_GET_SIZE(mc->kwds) == 0) {
  1437. Py_ssize_t i;
  1438. Py_ssize_t callargcount = PyTuple_GET_SIZE(mc->args);
  1439. newargs = PyTuple_New(1 + callargcount);
  1440. if (newargs == NULL)
  1441. return NULL;
  1442. PyTuple_SET_ITEM(newargs, 0, Py_NewRef(mc->name));
  1443. for (i = 0; i < callargcount; ++i) {
  1444. PyObject *arg = PyTuple_GET_ITEM(mc->args, i);
  1445. PyTuple_SET_ITEM(newargs, i + 1, Py_NewRef(arg));
  1446. }
  1447. return Py_BuildValue("ON", Py_TYPE(mc), newargs);
  1448. }
  1449. else {
  1450. PyObject *partial;
  1451. PyObject *constructor;
  1452. PyObject *newargs[2];
  1453. partial = _PyImport_GetModuleAttrString("functools", "partial");
  1454. if (!partial)
  1455. return NULL;
  1456. newargs[0] = (PyObject *)Py_TYPE(mc);
  1457. newargs[1] = mc->name;
  1458. constructor = PyObject_VectorcallDict(partial, newargs, 2, mc->kwds);
  1459. Py_DECREF(partial);
  1460. return Py_BuildValue("NO", constructor, mc->args);
  1461. }
  1462. }
  1463. static PyMethodDef methodcaller_methods[] = {
  1464. {"__reduce__", (PyCFunction)methodcaller_reduce, METH_NOARGS,
  1465. reduce_doc},
  1466. {NULL}
  1467. };
  1468. PyDoc_STRVAR(methodcaller_doc,
  1469. "methodcaller(name, /, *args, **kwargs)\n--\n\n\
  1470. Return a callable object that calls the given method on its operand.\n\
  1471. After f = methodcaller('name'), the call f(r) returns r.name().\n\
  1472. After g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\
  1473. r.name('date', foo=1).");
  1474. static PyType_Slot methodcaller_type_slots[] = {
  1475. {Py_tp_doc, (void *)methodcaller_doc},
  1476. {Py_tp_dealloc, methodcaller_dealloc},
  1477. {Py_tp_call, methodcaller_call},
  1478. {Py_tp_traverse, methodcaller_traverse},
  1479. {Py_tp_clear, methodcaller_clear},
  1480. {Py_tp_methods, methodcaller_methods},
  1481. {Py_tp_new, methodcaller_new},
  1482. {Py_tp_getattro, PyObject_GenericGetAttr},
  1483. {Py_tp_repr, methodcaller_repr},
  1484. {0, 0}
  1485. };
  1486. static PyType_Spec methodcaller_type_spec = {
  1487. .name = "operator.methodcaller",
  1488. .basicsize = sizeof(methodcallerobject),
  1489. .itemsize = 0,
  1490. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  1491. Py_TPFLAGS_IMMUTABLETYPE),
  1492. .slots = methodcaller_type_slots,
  1493. };
  1494. static int
  1495. operator_exec(PyObject *module)
  1496. {
  1497. _operator_state *state = get_operator_state(module);
  1498. state->attrgetter_type = PyType_FromModuleAndSpec(module, &attrgetter_type_spec, NULL);
  1499. if (state->attrgetter_type == NULL) {
  1500. return -1;
  1501. }
  1502. if (PyModule_AddType(module, (PyTypeObject *)state->attrgetter_type) < 0) {
  1503. return -1;
  1504. }
  1505. state->itemgetter_type = PyType_FromModuleAndSpec(module, &itemgetter_type_spec, NULL);
  1506. if (state->itemgetter_type == NULL) {
  1507. return -1;
  1508. }
  1509. if (PyModule_AddType(module, (PyTypeObject *)state->itemgetter_type) < 0) {
  1510. return -1;
  1511. }
  1512. state->methodcaller_type = PyType_FromModuleAndSpec(module, &methodcaller_type_spec, NULL);
  1513. if (state->methodcaller_type == NULL) {
  1514. return -1;
  1515. }
  1516. if (PyModule_AddType(module, (PyTypeObject *)state->methodcaller_type) < 0) {
  1517. return -1;
  1518. }
  1519. return 0;
  1520. }
  1521. static struct PyModuleDef_Slot operator_slots[] = {
  1522. {Py_mod_exec, operator_exec},
  1523. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  1524. {0, NULL}
  1525. };
  1526. static int
  1527. operator_traverse(PyObject *module, visitproc visit, void *arg)
  1528. {
  1529. _operator_state *state = get_operator_state(module);
  1530. Py_VISIT(state->attrgetter_type);
  1531. Py_VISIT(state->itemgetter_type);
  1532. Py_VISIT(state->methodcaller_type);
  1533. return 0;
  1534. }
  1535. static int
  1536. operator_clear(PyObject *module)
  1537. {
  1538. _operator_state *state = get_operator_state(module);
  1539. Py_CLEAR(state->attrgetter_type);
  1540. Py_CLEAR(state->itemgetter_type);
  1541. Py_CLEAR(state->methodcaller_type);
  1542. return 0;
  1543. }
  1544. static void
  1545. operator_free(void *module)
  1546. {
  1547. operator_clear((PyObject *)module);
  1548. }
  1549. static struct PyModuleDef operatormodule = {
  1550. PyModuleDef_HEAD_INIT,
  1551. .m_name = "_operator",
  1552. .m_doc = operator_doc,
  1553. .m_size = sizeof(_operator_state),
  1554. .m_methods = operator_methods,
  1555. .m_slots = operator_slots,
  1556. .m_traverse = operator_traverse,
  1557. .m_clear = operator_clear,
  1558. .m_free = operator_free,
  1559. };
  1560. PyMODINIT_FUNC
  1561. PyInit__operator(void)
  1562. {
  1563. return PyModuleDef_Init(&operatormodule);
  1564. }