irbuild-basic.test 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707
  1. [case testTrivialFunction]
  2. def f() -> int:
  3. return 1
  4. [out]
  5. def f():
  6. L0:
  7. return 2
  8. [case testFunctionArgument]
  9. def f(x: int) -> int:
  10. return x
  11. [out]
  12. def f(x):
  13. x :: int
  14. L0:
  15. return x
  16. [case testExplicitNoneReturn]
  17. def f() -> None:
  18. return
  19. [out]
  20. def f():
  21. L0:
  22. return 1
  23. [case testExplicitNoneReturn2]
  24. def f() -> None:
  25. return None
  26. [out]
  27. def f():
  28. L0:
  29. return 1
  30. [case testAssignment]
  31. def f() -> int:
  32. x = 1
  33. y = x
  34. return y
  35. [out]
  36. def f():
  37. x, y :: int
  38. L0:
  39. x = 2
  40. y = x
  41. return y
  42. [case testAssignmentTwice]
  43. def f(x: int) -> None:
  44. y = 1
  45. y = x
  46. return
  47. [out]
  48. def f(x):
  49. x, y :: int
  50. L0:
  51. y = 2
  52. y = x
  53. return 1
  54. [case testIntArithmetic]
  55. def f(x: int, y: int) -> int:
  56. return x * (y + 1)
  57. [out]
  58. def f(x, y):
  59. x, y, r0, r1 :: int
  60. L0:
  61. r0 = CPyTagged_Add(y, 2)
  62. r1 = CPyTagged_Multiply(x, r0)
  63. return r1
  64. [case testIf]
  65. def f(x: int, y: int) -> int:
  66. if x < y:
  67. x = 1
  68. return x
  69. [out]
  70. def f(x, y):
  71. x, y :: int
  72. r0 :: native_int
  73. r1 :: bit
  74. r2 :: native_int
  75. r3, r4, r5 :: bit
  76. L0:
  77. r0 = x & 1
  78. r1 = r0 != 0
  79. if r1 goto L2 else goto L1 :: bool
  80. L1:
  81. r2 = y & 1
  82. r3 = r2 != 0
  83. if r3 goto L2 else goto L3 :: bool
  84. L2:
  85. r4 = CPyTagged_IsLt_(x, y)
  86. if r4 goto L4 else goto L5 :: bool
  87. L3:
  88. r5 = x < y :: signed
  89. if r5 goto L4 else goto L5 :: bool
  90. L4:
  91. x = 2
  92. L5:
  93. return x
  94. [case testIfElse]
  95. def f(x: int, y: int) -> int:
  96. if x < y:
  97. x = 1
  98. else:
  99. x = 2
  100. return x
  101. [out]
  102. def f(x, y):
  103. x, y :: int
  104. r0 :: native_int
  105. r1 :: bit
  106. r2 :: native_int
  107. r3, r4, r5 :: bit
  108. L0:
  109. r0 = x & 1
  110. r1 = r0 != 0
  111. if r1 goto L2 else goto L1 :: bool
  112. L1:
  113. r2 = y & 1
  114. r3 = r2 != 0
  115. if r3 goto L2 else goto L3 :: bool
  116. L2:
  117. r4 = CPyTagged_IsLt_(x, y)
  118. if r4 goto L4 else goto L5 :: bool
  119. L3:
  120. r5 = x < y :: signed
  121. if r5 goto L4 else goto L5 :: bool
  122. L4:
  123. x = 2
  124. goto L6
  125. L5:
  126. x = 4
  127. L6:
  128. return x
  129. [case testAnd1]
  130. def f(x: int, y: int) -> int:
  131. if x < y and x > y:
  132. x = 1
  133. else:
  134. x = 2
  135. return x
  136. [out]
  137. def f(x, y):
  138. x, y :: int
  139. r0 :: native_int
  140. r1 :: bit
  141. r2 :: native_int
  142. r3, r4, r5 :: bit
  143. r6 :: native_int
  144. r7 :: bit
  145. r8 :: native_int
  146. r9, r10, r11 :: bit
  147. L0:
  148. r0 = x & 1
  149. r1 = r0 != 0
  150. if r1 goto L2 else goto L1 :: bool
  151. L1:
  152. r2 = y & 1
  153. r3 = r2 != 0
  154. if r3 goto L2 else goto L3 :: bool
  155. L2:
  156. r4 = CPyTagged_IsLt_(x, y)
  157. if r4 goto L4 else goto L9 :: bool
  158. L3:
  159. r5 = x < y :: signed
  160. if r5 goto L4 else goto L9 :: bool
  161. L4:
  162. r6 = x & 1
  163. r7 = r6 != 0
  164. if r7 goto L6 else goto L5 :: bool
  165. L5:
  166. r8 = y & 1
  167. r9 = r8 != 0
  168. if r9 goto L6 else goto L7 :: bool
  169. L6:
  170. r10 = CPyTagged_IsLt_(y, x)
  171. if r10 goto L8 else goto L9 :: bool
  172. L7:
  173. r11 = x > y :: signed
  174. if r11 goto L8 else goto L9 :: bool
  175. L8:
  176. x = 2
  177. goto L10
  178. L9:
  179. x = 4
  180. L10:
  181. return x
  182. [case testAnd2]
  183. def f(x: object, y: object) -> str:
  184. return str(x) or str(y)
  185. [out]
  186. def f(x, y):
  187. x, y :: object
  188. r0 :: str
  189. r1 :: bit
  190. r2, r3 :: str
  191. L0:
  192. r0 = PyObject_Str(x)
  193. r1 = CPyStr_IsTrue(r0)
  194. if r1 goto L1 else goto L2 :: bool
  195. L1:
  196. r2 = r0
  197. goto L3
  198. L2:
  199. r3 = PyObject_Str(y)
  200. r2 = r3
  201. L3:
  202. return r2
  203. [case testOr]
  204. def f(x: int, y: int) -> int:
  205. if x < y or x > y:
  206. x = 1
  207. else:
  208. x = 2
  209. return x
  210. [out]
  211. def f(x, y):
  212. x, y :: int
  213. r0 :: native_int
  214. r1 :: bit
  215. r2 :: native_int
  216. r3, r4, r5 :: bit
  217. r6 :: native_int
  218. r7 :: bit
  219. r8 :: native_int
  220. r9, r10, r11 :: bit
  221. L0:
  222. r0 = x & 1
  223. r1 = r0 != 0
  224. if r1 goto L2 else goto L1 :: bool
  225. L1:
  226. r2 = y & 1
  227. r3 = r2 != 0
  228. if r3 goto L2 else goto L3 :: bool
  229. L2:
  230. r4 = CPyTagged_IsLt_(x, y)
  231. if r4 goto L8 else goto L4 :: bool
  232. L3:
  233. r5 = x < y :: signed
  234. if r5 goto L8 else goto L4 :: bool
  235. L4:
  236. r6 = x & 1
  237. r7 = r6 != 0
  238. if r7 goto L6 else goto L5 :: bool
  239. L5:
  240. r8 = y & 1
  241. r9 = r8 != 0
  242. if r9 goto L6 else goto L7 :: bool
  243. L6:
  244. r10 = CPyTagged_IsLt_(y, x)
  245. if r10 goto L8 else goto L9 :: bool
  246. L7:
  247. r11 = x > y :: signed
  248. if r11 goto L8 else goto L9 :: bool
  249. L8:
  250. x = 2
  251. goto L10
  252. L9:
  253. x = 4
  254. L10:
  255. return x
  256. [case testOr2]
  257. def f(x: object, y: object) -> str:
  258. return str(x) and str(y)
  259. [out]
  260. def f(x, y):
  261. x, y :: object
  262. r0 :: str
  263. r1 :: bit
  264. r2, r3 :: str
  265. L0:
  266. r0 = PyObject_Str(x)
  267. r1 = CPyStr_IsTrue(r0)
  268. if r1 goto L2 else goto L1 :: bool
  269. L1:
  270. r2 = r0
  271. goto L3
  272. L2:
  273. r3 = PyObject_Str(y)
  274. r2 = r3
  275. L3:
  276. return r2
  277. [case testSimpleNot]
  278. def f(x: int, y: int) -> int:
  279. if not (x < y):
  280. x = 1
  281. return x
  282. [out]
  283. def f(x, y):
  284. x, y :: int
  285. r0 :: native_int
  286. r1 :: bit
  287. r2 :: native_int
  288. r3, r4, r5 :: bit
  289. L0:
  290. r0 = x & 1
  291. r1 = r0 != 0
  292. if r1 goto L2 else goto L1 :: bool
  293. L1:
  294. r2 = y & 1
  295. r3 = r2 != 0
  296. if r3 goto L2 else goto L3 :: bool
  297. L2:
  298. r4 = CPyTagged_IsLt_(x, y)
  299. if r4 goto L5 else goto L4 :: bool
  300. L3:
  301. r5 = x < y :: signed
  302. if r5 goto L5 else goto L4 :: bool
  303. L4:
  304. x = 2
  305. L5:
  306. return x
  307. [case testNotAnd]
  308. def f(x: int, y: int) -> int:
  309. if not (x < y and x > y):
  310. x = 1
  311. return x
  312. [out]
  313. def f(x, y):
  314. x, y :: int
  315. r0 :: native_int
  316. r1 :: bit
  317. r2 :: native_int
  318. r3, r4, r5 :: bit
  319. r6 :: native_int
  320. r7 :: bit
  321. r8 :: native_int
  322. r9, r10, r11 :: bit
  323. L0:
  324. r0 = x & 1
  325. r1 = r0 != 0
  326. if r1 goto L2 else goto L1 :: bool
  327. L1:
  328. r2 = y & 1
  329. r3 = r2 != 0
  330. if r3 goto L2 else goto L3 :: bool
  331. L2:
  332. r4 = CPyTagged_IsLt_(x, y)
  333. if r4 goto L4 else goto L8 :: bool
  334. L3:
  335. r5 = x < y :: signed
  336. if r5 goto L4 else goto L8 :: bool
  337. L4:
  338. r6 = x & 1
  339. r7 = r6 != 0
  340. if r7 goto L6 else goto L5 :: bool
  341. L5:
  342. r8 = y & 1
  343. r9 = r8 != 0
  344. if r9 goto L6 else goto L7 :: bool
  345. L6:
  346. r10 = CPyTagged_IsLt_(y, x)
  347. if r10 goto L9 else goto L8 :: bool
  348. L7:
  349. r11 = x > y :: signed
  350. if r11 goto L9 else goto L8 :: bool
  351. L8:
  352. x = 2
  353. L9:
  354. return x
  355. [case testWhile]
  356. def f(x: int, y: int) -> int:
  357. while x > y:
  358. x = x - y
  359. return x
  360. [out]
  361. def f(x, y):
  362. x, y :: int
  363. r0 :: native_int
  364. r1 :: bit
  365. r2 :: native_int
  366. r3, r4, r5 :: bit
  367. r6 :: int
  368. L0:
  369. L1:
  370. r0 = x & 1
  371. r1 = r0 != 0
  372. if r1 goto L3 else goto L2 :: bool
  373. L2:
  374. r2 = y & 1
  375. r3 = r2 != 0
  376. if r3 goto L3 else goto L4 :: bool
  377. L3:
  378. r4 = CPyTagged_IsLt_(y, x)
  379. if r4 goto L5 else goto L6 :: bool
  380. L4:
  381. r5 = x > y :: signed
  382. if r5 goto L5 else goto L6 :: bool
  383. L5:
  384. r6 = CPyTagged_Subtract(x, y)
  385. x = r6
  386. goto L1
  387. L6:
  388. return x
  389. [case testWhile2]
  390. def f(x: int, y: int) -> int:
  391. x = 1
  392. while x > y:
  393. x = x - y
  394. return x
  395. [out]
  396. def f(x, y):
  397. x, y :: int
  398. r0 :: native_int
  399. r1 :: bit
  400. r2 :: native_int
  401. r3, r4, r5 :: bit
  402. r6 :: int
  403. L0:
  404. x = 2
  405. L1:
  406. r0 = x & 1
  407. r1 = r0 != 0
  408. if r1 goto L3 else goto L2 :: bool
  409. L2:
  410. r2 = y & 1
  411. r3 = r2 != 0
  412. if r3 goto L3 else goto L4 :: bool
  413. L3:
  414. r4 = CPyTagged_IsLt_(y, x)
  415. if r4 goto L5 else goto L6 :: bool
  416. L4:
  417. r5 = x > y :: signed
  418. if r5 goto L5 else goto L6 :: bool
  419. L5:
  420. r6 = CPyTagged_Subtract(x, y)
  421. x = r6
  422. goto L1
  423. L6:
  424. return x
  425. [case testImplicitNoneReturn]
  426. def f() -> None:
  427. pass
  428. [out]
  429. def f():
  430. L0:
  431. return 1
  432. [case testImplicitNoneReturn2]
  433. def f() -> None:
  434. x = 1
  435. [out]
  436. def f():
  437. x :: int
  438. L0:
  439. x = 2
  440. return 1
  441. [case testImplicitNoneReturnAndIf]
  442. def f(x: int, y: int) -> None:
  443. if x < y:
  444. x = 1
  445. else:
  446. y = 2
  447. [out]
  448. def f(x, y):
  449. x, y :: int
  450. r0 :: native_int
  451. r1 :: bit
  452. r2 :: native_int
  453. r3, r4, r5 :: bit
  454. L0:
  455. r0 = x & 1
  456. r1 = r0 != 0
  457. if r1 goto L2 else goto L1 :: bool
  458. L1:
  459. r2 = y & 1
  460. r3 = r2 != 0
  461. if r3 goto L2 else goto L3 :: bool
  462. L2:
  463. r4 = CPyTagged_IsLt_(x, y)
  464. if r4 goto L4 else goto L5 :: bool
  465. L3:
  466. r5 = x < y :: signed
  467. if r5 goto L4 else goto L5 :: bool
  468. L4:
  469. x = 2
  470. goto L6
  471. L5:
  472. y = 4
  473. L6:
  474. return 1
  475. [case testRecursion]
  476. def f(n: int) -> int:
  477. if n <= 1:
  478. return 1
  479. else:
  480. return f(n - 1) + f(n - 2)
  481. [out]
  482. def f(n):
  483. n :: int
  484. r0 :: native_int
  485. r1, r2, r3 :: bit
  486. r4, r5, r6, r7, r8 :: int
  487. L0:
  488. r0 = n & 1
  489. r1 = r0 != 0
  490. if r1 goto L1 else goto L2 :: bool
  491. L1:
  492. r2 = CPyTagged_IsLt_(2, n)
  493. if r2 goto L4 else goto L3 :: bool
  494. L2:
  495. r3 = n <= 2 :: signed
  496. if r3 goto L3 else goto L4 :: bool
  497. L3:
  498. return 2
  499. L4:
  500. r4 = CPyTagged_Subtract(n, 2)
  501. r5 = f(r4)
  502. r6 = CPyTagged_Subtract(n, 4)
  503. r7 = f(r6)
  504. r8 = CPyTagged_Add(r5, r7)
  505. return r8
  506. L5:
  507. unreachable
  508. [case testReportTypeCheckError]
  509. def f() -> None:
  510. return 1 # E: No return value expected
  511. [case testReportSemanticaAnalysisError1]
  512. def f(x: List[int]) -> None: pass # E: Name "List" is not defined \
  513. # N: Did you forget to import it from "typing"? (Suggestion: "from typing import List")
  514. [case testReportSemanticaAnalysisError2]
  515. def f() -> None:
  516. x # E: Name "x" is not defined
  517. [case testElif]
  518. def f(n: int) -> int:
  519. if n < 0:
  520. x = 1
  521. elif n == 0:
  522. x = 1
  523. else:
  524. x = 2
  525. return x
  526. [out]
  527. def f(n):
  528. n :: int
  529. r0 :: native_int
  530. r1, r2, r3 :: bit
  531. x :: int
  532. r4 :: bit
  533. L0:
  534. r0 = n & 1
  535. r1 = r0 != 0
  536. if r1 goto L1 else goto L2 :: bool
  537. L1:
  538. r2 = CPyTagged_IsLt_(n, 0)
  539. if r2 goto L3 else goto L4 :: bool
  540. L2:
  541. r3 = n < 0 :: signed
  542. if r3 goto L3 else goto L4 :: bool
  543. L3:
  544. x = 2
  545. goto L8
  546. L4:
  547. r4 = n == 0
  548. if r4 goto L5 else goto L6 :: bool
  549. L5:
  550. x = 2
  551. goto L7
  552. L6:
  553. x = 4
  554. L7:
  555. L8:
  556. return x
  557. [case testUnaryMinus]
  558. def f(n: int) -> int:
  559. return -n
  560. [out]
  561. def f(n):
  562. n, r0 :: int
  563. L0:
  564. r0 = CPyTagged_Negate(n)
  565. return r0
  566. [case testConditionalExpr]
  567. def f(n: int) -> int:
  568. return 0 if n == 0 else 1
  569. [out]
  570. def f(n):
  571. n :: int
  572. r0 :: bit
  573. r1 :: int
  574. L0:
  575. r0 = n == 0
  576. if r0 goto L1 else goto L2 :: bool
  577. L1:
  578. r1 = 0
  579. goto L3
  580. L2:
  581. r1 = 2
  582. L3:
  583. return r1
  584. [case testOperatorAssignment]
  585. def f() -> int:
  586. x = 0
  587. x += 1
  588. return x
  589. [out]
  590. def f():
  591. x, r0 :: int
  592. L0:
  593. x = 0
  594. r0 = CPyTagged_Add(x, 2)
  595. x = r0
  596. return x
  597. [case testTrue]
  598. def f() -> bool:
  599. return True
  600. [out]
  601. def f():
  602. L0:
  603. return 1
  604. [case testFalse]
  605. def f() -> bool:
  606. return False
  607. [out]
  608. def f():
  609. L0:
  610. return 0
  611. [case testBoolCond]
  612. def f(x: bool) -> bool:
  613. if x:
  614. return False
  615. else:
  616. return True
  617. [out]
  618. def f(x):
  619. x :: bool
  620. L0:
  621. if x goto L1 else goto L2 :: bool
  622. L1:
  623. return 0
  624. L2:
  625. return 1
  626. L3:
  627. unreachable
  628. [case testPycall]
  629. import testmodule
  630. def f(x: int) -> int:
  631. return testmodule.factorial(x)
  632. [file testmodule.py]
  633. def factorial(x: int) -> int:
  634. if x == 0:
  635. return 1
  636. else:
  637. return x * factorial(x-1)
  638. [out]
  639. def f(x):
  640. x :: int
  641. r0 :: object
  642. r1 :: str
  643. r2, r3, r4 :: object
  644. r5 :: int
  645. L0:
  646. r0 = testmodule :: module
  647. r1 = 'factorial'
  648. r2 = CPyObject_GetAttr(r0, r1)
  649. r3 = box(int, x)
  650. r4 = PyObject_CallFunctionObjArgs(r2, r3, 0)
  651. r5 = unbox(int, r4)
  652. return r5
  653. [case testImport_toplevel]
  654. import sys
  655. import enum as enum2
  656. import collections.abc
  657. import collections.abc as abc2
  658. _ = "filler"
  659. import single
  660. single.hello()
  661. [file single.py]
  662. def hello() -> None:
  663. print("hello, world")
  664. [out]
  665. def __top_level__():
  666. r0, r1 :: object
  667. r2 :: bit
  668. r3 :: str
  669. r4 :: object
  670. r5, r6, r7, r8 :: object_ptr
  671. r9 :: object_ptr[4]
  672. r10 :: c_ptr
  673. r11 :: native_int[4]
  674. r12 :: c_ptr
  675. r13 :: object
  676. r14 :: dict
  677. r15, r16 :: str
  678. r17 :: bit
  679. r18 :: str
  680. r19 :: dict
  681. r20 :: str
  682. r21 :: i32
  683. r22 :: bit
  684. r23 :: object_ptr
  685. r24 :: object_ptr[1]
  686. r25 :: c_ptr
  687. r26 :: native_int[1]
  688. r27 :: c_ptr
  689. r28 :: object
  690. r29 :: dict
  691. r30, r31 :: str
  692. r32 :: bit
  693. r33 :: object
  694. r34 :: str
  695. r35, r36 :: object
  696. L0:
  697. r0 = builtins :: module
  698. r1 = load_address _Py_NoneStruct
  699. r2 = r0 != r1
  700. if r2 goto L2 else goto L1 :: bool
  701. L1:
  702. r3 = 'builtins'
  703. r4 = PyImport_Import(r3)
  704. builtins = r4 :: module
  705. L2:
  706. r5 = load_address sys :: module
  707. r6 = load_address enum :: module
  708. r7 = load_address collections.abc :: module
  709. r8 = load_address collections.abc :: module
  710. r9 = [r5, r6, r7, r8]
  711. r10 = load_address r9
  712. r11 = [1, 2, 3, 4]
  713. r12 = load_address r11
  714. r13 = (('sys', 'sys', 'sys'), ('enum', 'enum', 'enum2'), ('collections.abc', 'collections', 'collections'), ('collections.abc', 'collections.abc', 'abc2'))
  715. r14 = __main__.globals :: static
  716. r15 = 'main'
  717. r16 = '<module>'
  718. r17 = CPyImport_ImportMany(r13, r10, r14, r15, r16, r12)
  719. r18 = 'filler'
  720. r19 = __main__.globals :: static
  721. r20 = '_'
  722. r21 = CPyDict_SetItem(r19, r20, r18)
  723. r22 = r21 >= 0 :: signed
  724. r23 = load_address single :: module
  725. r24 = [r23]
  726. r25 = load_address r24
  727. r26 = [6]
  728. r27 = load_address r26
  729. r28 = (('single', 'single', 'single'),)
  730. r29 = __main__.globals :: static
  731. r30 = 'main'
  732. r31 = '<module>'
  733. r32 = CPyImport_ImportMany(r28, r25, r29, r30, r31, r27)
  734. r33 = single :: module
  735. r34 = 'hello'
  736. r35 = CPyObject_GetAttr(r33, r34)
  737. r36 = PyObject_CallFunctionObjArgs(r35, 0)
  738. return 1
  739. [case testFromImport_toplevel]
  740. from testmodule import g, h
  741. from testmodule import h as two
  742. def f(x: int) -> int:
  743. return g(x) + h() + two()
  744. [file testmodule.py]
  745. def g(x: int) -> int:
  746. return x + 1
  747. def h() -> int:
  748. return 2
  749. [out]
  750. def f(x):
  751. x :: int
  752. r0 :: dict
  753. r1 :: str
  754. r2, r3, r4 :: object
  755. r5 :: int
  756. r6 :: dict
  757. r7 :: str
  758. r8, r9 :: object
  759. r10, r11 :: int
  760. r12 :: dict
  761. r13 :: str
  762. r14, r15 :: object
  763. r16, r17 :: int
  764. L0:
  765. r0 = __main__.globals :: static
  766. r1 = 'g'
  767. r2 = CPyDict_GetItem(r0, r1)
  768. r3 = box(int, x)
  769. r4 = PyObject_CallFunctionObjArgs(r2, r3, 0)
  770. r5 = unbox(int, r4)
  771. r6 = __main__.globals :: static
  772. r7 = 'h'
  773. r8 = CPyDict_GetItem(r6, r7)
  774. r9 = PyObject_CallFunctionObjArgs(r8, 0)
  775. r10 = unbox(int, r9)
  776. r11 = CPyTagged_Add(r5, r10)
  777. r12 = __main__.globals :: static
  778. r13 = 'two'
  779. r14 = CPyDict_GetItem(r12, r13)
  780. r15 = PyObject_CallFunctionObjArgs(r14, 0)
  781. r16 = unbox(int, r15)
  782. r17 = CPyTagged_Add(r11, r16)
  783. return r17
  784. def __top_level__():
  785. r0, r1 :: object
  786. r2 :: bit
  787. r3 :: str
  788. r4, r5 :: object
  789. r6 :: str
  790. r7 :: dict
  791. r8, r9, r10 :: object
  792. r11 :: str
  793. r12 :: dict
  794. r13 :: object
  795. L0:
  796. r0 = builtins :: module
  797. r1 = load_address _Py_NoneStruct
  798. r2 = r0 != r1
  799. if r2 goto L2 else goto L1 :: bool
  800. L1:
  801. r3 = 'builtins'
  802. r4 = PyImport_Import(r3)
  803. builtins = r4 :: module
  804. L2:
  805. r5 = ('g', 'h')
  806. r6 = 'testmodule'
  807. r7 = __main__.globals :: static
  808. r8 = CPyImport_ImportFromMany(r6, r5, r5, r7)
  809. testmodule = r8 :: module
  810. r9 = ('h',)
  811. r10 = ('two',)
  812. r11 = 'testmodule'
  813. r12 = __main__.globals :: static
  814. r13 = CPyImport_ImportFromMany(r11, r9, r10, r12)
  815. testmodule = r13 :: module
  816. return 1
  817. [case testPrintFullname]
  818. import builtins
  819. def f(x: int) -> None:
  820. builtins.print(5)
  821. [out]
  822. def f(x):
  823. x :: int
  824. r0 :: object
  825. r1 :: str
  826. r2, r3, r4 :: object
  827. L0:
  828. r0 = builtins :: module
  829. r1 = 'print'
  830. r2 = CPyObject_GetAttr(r0, r1)
  831. r3 = object 5
  832. r4 = PyObject_CallFunctionObjArgs(r2, r3, 0)
  833. return 1
  834. [case testPrint]
  835. import builtins
  836. def f(x: int) -> None:
  837. print(5)
  838. [out]
  839. def f(x):
  840. x :: int
  841. r0 :: object
  842. r1 :: str
  843. r2, r3, r4 :: object
  844. L0:
  845. r0 = builtins :: module
  846. r1 = 'print'
  847. r2 = CPyObject_GetAttr(r0, r1)
  848. r3 = object 5
  849. r4 = PyObject_CallFunctionObjArgs(r2, r3, 0)
  850. return 1
  851. [case testUnicodeLiteral]
  852. def f() -> str:
  853. x = "some string"
  854. return "some other string"
  855. [out]
  856. def f():
  857. r0, x, r1 :: str
  858. L0:
  859. r0 = 'some string'
  860. x = r0
  861. r1 = 'some other string'
  862. return r1
  863. [case testBytesLiteral]
  864. def f() -> bytes:
  865. x = b'\xf0'
  866. return b'1234'
  867. [out]
  868. def f():
  869. r0, x, r1 :: bytes
  870. L0:
  871. r0 = b'\xf0'
  872. x = r0
  873. r1 = b'1234'
  874. return r1
  875. [case testPyMethodCall1]
  876. from typing import Any
  877. def f(x: Any) -> int:
  878. y: int = x.pop()
  879. return x.pop()
  880. [out]
  881. def f(x):
  882. x :: object
  883. r0 :: str
  884. r1 :: object
  885. r2, y :: int
  886. r3 :: str
  887. r4 :: object
  888. r5 :: int
  889. L0:
  890. r0 = 'pop'
  891. r1 = CPyObject_CallMethodObjArgs(x, r0, 0)
  892. r2 = unbox(int, r1)
  893. y = r2
  894. r3 = 'pop'
  895. r4 = CPyObject_CallMethodObjArgs(x, r3, 0)
  896. r5 = unbox(int, r4)
  897. return r5
  898. [case testObjectType]
  899. def g(y: object) -> None:
  900. g(y)
  901. g([1])
  902. g(None)
  903. [out]
  904. def g(y):
  905. y :: object
  906. r0 :: None
  907. r1 :: list
  908. r2 :: object
  909. r3, r4 :: ptr
  910. r5 :: None
  911. r6 :: object
  912. r7 :: None
  913. L0:
  914. r0 = g(y)
  915. r1 = PyList_New(1)
  916. r2 = object 1
  917. r3 = get_element_ptr r1 ob_item :: PyListObject
  918. r4 = load_mem r3 :: ptr*
  919. set_mem r4, r2 :: builtins.object*
  920. keep_alive r1
  921. r5 = g(r1)
  922. r6 = box(None, 1)
  923. r7 = g(r6)
  924. return 1
  925. [case testCoerceToObject1]
  926. def g(y: object) -> object:
  927. g(1)
  928. a = [y]
  929. a[0] = (1, 2)
  930. y = True
  931. return 3
  932. [out]
  933. def g(y):
  934. y, r0, r1 :: object
  935. r2 :: list
  936. r3, r4 :: ptr
  937. a :: list
  938. r5 :: tuple[int, int]
  939. r6 :: object
  940. r7 :: bit
  941. r8, r9 :: object
  942. L0:
  943. r0 = object 1
  944. r1 = g(r0)
  945. r2 = PyList_New(1)
  946. r3 = get_element_ptr r2 ob_item :: PyListObject
  947. r4 = load_mem r3 :: ptr*
  948. set_mem r4, y :: builtins.object*
  949. keep_alive r2
  950. a = r2
  951. r5 = (2, 4)
  952. r6 = box(tuple[int, int], r5)
  953. r7 = CPyList_SetItem(a, 0, r6)
  954. r8 = box(bool, 1)
  955. y = r8
  956. r9 = object 3
  957. return r9
  958. [case testCoerceToObject2]
  959. class A:
  960. x: object
  961. n: int
  962. def f(a: A, o: object) -> None:
  963. a.x = 1
  964. o = a.n
  965. [out]
  966. def f(a, o):
  967. a :: __main__.A
  968. o, r0 :: object
  969. r1 :: bool
  970. r2 :: int
  971. r3 :: object
  972. L0:
  973. r0 = object 1
  974. a.x = r0; r1 = is_error
  975. r2 = a.n
  976. r3 = box(int, r2)
  977. o = r3
  978. return 1
  979. [case testAssertType]
  980. from typing import assert_type
  981. def f(x: int) -> None:
  982. y = assert_type(x, int)
  983. [out]
  984. def f(x):
  985. x, y :: int
  986. L0:
  987. y = x
  988. return 1
  989. [case testDownCast]
  990. from typing import cast, List, Tuple
  991. class A: pass
  992. def f(x: object) -> None:
  993. n = cast(int, x)
  994. b = cast(bool, x)
  995. a = cast(A, x)
  996. l = cast(List[int], x)
  997. t = cast(Tuple[int, A], x)
  998. [out]
  999. def f(x):
  1000. x :: object
  1001. r0, n :: int
  1002. r1, b :: bool
  1003. r2, a :: __main__.A
  1004. r3, l :: list
  1005. r4, t :: tuple[int, __main__.A]
  1006. L0:
  1007. r0 = unbox(int, x)
  1008. n = r0
  1009. r1 = unbox(bool, x)
  1010. b = r1
  1011. r2 = cast(__main__.A, x)
  1012. a = r2
  1013. r3 = cast(list, x)
  1014. l = r3
  1015. r4 = unbox(tuple[int, __main__.A], x)
  1016. t = r4
  1017. return 1
  1018. [case testDownCastSpecialCases]
  1019. from typing import cast, Optional, Tuple
  1020. class A: pass
  1021. def f(o: Optional[A], n: int, t: Tuple[int, ...], tt: Tuple[int, int]) -> None:
  1022. a = cast(A, o)
  1023. m = cast(bool, n)
  1024. t = tt
  1025. [out]
  1026. def f(o, n, t, tt):
  1027. o :: union[__main__.A, None]
  1028. n :: int
  1029. t :: tuple
  1030. tt :: tuple[int, int]
  1031. r0, a :: __main__.A
  1032. r1 :: object
  1033. r2, m :: bool
  1034. r3 :: object
  1035. L0:
  1036. r0 = cast(__main__.A, o)
  1037. a = r0
  1038. r1 = box(int, n)
  1039. r2 = unbox(bool, r1)
  1040. m = r2
  1041. r3 = box(tuple[int, int], tt)
  1042. t = r3
  1043. return 1
  1044. [case testSuccessfulCast]
  1045. from typing import cast, Optional, Tuple, List, Dict
  1046. class A: pass
  1047. def f(o: object,
  1048. p: Optional[A],
  1049. n: int,
  1050. b: bool,
  1051. t: Tuple[int, ...],
  1052. s: Tuple[int, int],
  1053. a: A,
  1054. l: List[A],
  1055. d: Dict[int, str]) -> None:
  1056. o = cast(object, o)
  1057. p = cast(Optional[A], p)
  1058. n = cast(int, n)
  1059. b = cast(bool, b)
  1060. t = cast(Tuple[int, ...], t)
  1061. s = cast(Tuple[int, int], s)
  1062. o = cast(object, n)
  1063. a = cast(A, a)
  1064. l2 = cast(List[object], l)
  1065. d2 = cast(Dict[object, str], d)
  1066. [out]
  1067. def f(o, p, n, b, t, s, a, l, d):
  1068. o :: object
  1069. p :: union[__main__.A, None]
  1070. n :: int
  1071. b :: bool
  1072. t :: tuple
  1073. s :: tuple[int, int]
  1074. a :: __main__.A
  1075. l :: list
  1076. d :: dict
  1077. r0 :: object
  1078. l2 :: list
  1079. d2 :: dict
  1080. L0:
  1081. o = o
  1082. p = p
  1083. n = n
  1084. b = b
  1085. t = t
  1086. s = s
  1087. r0 = box(int, n)
  1088. o = r0
  1089. a = a
  1090. l2 = l
  1091. d2 = d
  1092. return 1
  1093. [case testGenericSetItem]
  1094. from typing import Any
  1095. def f(x: Any, y: Any, z: Any) -> None:
  1096. x[y] = z
  1097. [out]
  1098. def f(x, y, z):
  1099. x, y, z :: object
  1100. r0 :: i32
  1101. r1 :: bit
  1102. L0:
  1103. r0 = PyObject_SetItem(x, y, z)
  1104. r1 = r0 >= 0 :: signed
  1105. return 1
  1106. [case testLoadFloatSum]
  1107. def assign_and_return_float_sum() -> float:
  1108. f1 = 1.0
  1109. f2 = 2.0
  1110. f3 = 3.0
  1111. return f1 * f2 + f3
  1112. [out]
  1113. def assign_and_return_float_sum():
  1114. f1, f2, f3, r0, r1 :: float
  1115. L0:
  1116. f1 = 1.0
  1117. f2 = 2.0
  1118. f3 = 3.0
  1119. r0 = f1 * f2
  1120. r1 = r0 + f3
  1121. return r1
  1122. [case testLoadComplex]
  1123. def load() -> complex:
  1124. real = 1
  1125. return 5j+real
  1126. [out]
  1127. def load():
  1128. real :: int
  1129. r0, r1, r2 :: object
  1130. L0:
  1131. real = 2
  1132. r0 = 5j
  1133. r1 = box(int, real)
  1134. r2 = PyNumber_Add(r0, r1)
  1135. return r2
  1136. [case testBigIntLiteral_64bit]
  1137. def big_int() -> None:
  1138. a_62_bit = 4611686018427387902
  1139. max_62_bit = 4611686018427387903
  1140. b_63_bit = 4611686018427387904
  1141. c_63_bit = 9223372036854775806
  1142. max_63_bit = 9223372036854775807
  1143. d_64_bit = 9223372036854775808
  1144. max_32_bit = 2147483647
  1145. max_31_bit = 1073741823
  1146. [out]
  1147. def big_int():
  1148. a_62_bit, max_62_bit, r0, b_63_bit, r1, c_63_bit, r2, max_63_bit, r3, d_64_bit, max_32_bit, max_31_bit :: int
  1149. L0:
  1150. a_62_bit = 9223372036854775804
  1151. max_62_bit = 9223372036854775806
  1152. r0 = object 4611686018427387904
  1153. b_63_bit = r0
  1154. r1 = object 9223372036854775806
  1155. c_63_bit = r1
  1156. r2 = object 9223372036854775807
  1157. max_63_bit = r2
  1158. r3 = object 9223372036854775808
  1159. d_64_bit = r3
  1160. max_32_bit = 4294967294
  1161. max_31_bit = 2147483646
  1162. return 1
  1163. [case testBigIntLiteral_32bit]
  1164. def big_int() -> None:
  1165. a_62_bit = 4611686018427387902
  1166. max_62_bit = 4611686018427387903
  1167. b_63_bit = 4611686018427387904
  1168. c_63_bit = 9223372036854775806
  1169. max_63_bit = 9223372036854775807
  1170. d_64_bit = 9223372036854775808
  1171. max_32_bit = 2147483647
  1172. max_31_bit = 1073741823
  1173. [out]
  1174. def big_int():
  1175. r0, a_62_bit, r1, max_62_bit, r2, b_63_bit, r3, c_63_bit, r4, max_63_bit, r5, d_64_bit, r6, max_32_bit, max_31_bit :: int
  1176. L0:
  1177. r0 = object 4611686018427387902
  1178. a_62_bit = r0
  1179. r1 = object 4611686018427387903
  1180. max_62_bit = r1
  1181. r2 = object 4611686018427387904
  1182. b_63_bit = r2
  1183. r3 = object 9223372036854775806
  1184. c_63_bit = r3
  1185. r4 = object 9223372036854775807
  1186. max_63_bit = r4
  1187. r5 = object 9223372036854775808
  1188. d_64_bit = r5
  1189. r6 = object 2147483647
  1190. max_32_bit = r6
  1191. max_31_bit = 2147483646
  1192. return 1
  1193. [case testCallableTypes]
  1194. from typing import Callable, Any
  1195. from m import f
  1196. def absolute_value(x: int) -> int:
  1197. return x if x > 0 else -x
  1198. def call_native_function(x: int) -> int:
  1199. return absolute_value(x)
  1200. def call_python_function(x: int) -> int:
  1201. return f(x)
  1202. def return_float() -> float:
  1203. return 5.0
  1204. def return_callable_type() -> Callable[[], float]:
  1205. return return_float
  1206. def call_callable_type() -> float:
  1207. f = return_callable_type()
  1208. return f()
  1209. [file m.py]
  1210. def f(x: int) -> int:
  1211. return x
  1212. [out]
  1213. def absolute_value(x):
  1214. x :: int
  1215. r0 :: native_int
  1216. r1, r2, r3 :: bit
  1217. r4, r5 :: int
  1218. L0:
  1219. r0 = x & 1
  1220. r1 = r0 != 0
  1221. if r1 goto L1 else goto L2 :: bool
  1222. L1:
  1223. r2 = CPyTagged_IsLt_(0, x)
  1224. if r2 goto L3 else goto L4 :: bool
  1225. L2:
  1226. r3 = x > 0 :: signed
  1227. if r3 goto L3 else goto L4 :: bool
  1228. L3:
  1229. r4 = x
  1230. goto L5
  1231. L4:
  1232. r5 = CPyTagged_Negate(x)
  1233. r4 = r5
  1234. L5:
  1235. return r4
  1236. def call_native_function(x):
  1237. x, r0 :: int
  1238. L0:
  1239. r0 = absolute_value(x)
  1240. return r0
  1241. def call_python_function(x):
  1242. x :: int
  1243. r0 :: dict
  1244. r1 :: str
  1245. r2, r3, r4 :: object
  1246. r5 :: int
  1247. L0:
  1248. r0 = __main__.globals :: static
  1249. r1 = 'f'
  1250. r2 = CPyDict_GetItem(r0, r1)
  1251. r3 = box(int, x)
  1252. r4 = PyObject_CallFunctionObjArgs(r2, r3, 0)
  1253. r5 = unbox(int, r4)
  1254. return r5
  1255. def return_float():
  1256. L0:
  1257. return 5.0
  1258. def return_callable_type():
  1259. r0 :: dict
  1260. r1 :: str
  1261. r2 :: object
  1262. L0:
  1263. r0 = __main__.globals :: static
  1264. r1 = 'return_float'
  1265. r2 = CPyDict_GetItem(r0, r1)
  1266. return r2
  1267. def call_callable_type():
  1268. r0, f, r1 :: object
  1269. r2 :: float
  1270. L0:
  1271. r0 = return_callable_type()
  1272. f = r0
  1273. r1 = PyObject_CallFunctionObjArgs(f, 0)
  1274. r2 = unbox(float, r1)
  1275. return r2
  1276. [case testCallableTypesWithKeywordArgs]
  1277. from typing import List
  1278. def call_python_function_with_keyword_arg(x: str) -> int:
  1279. return int(x, base=2)
  1280. def call_python_method_with_keyword_args(xs: List[int], first: int, second: int) -> List[int]:
  1281. xs.insert(0, x=first)
  1282. xs.insert(x=second, i=1)
  1283. return xs
  1284. [out]
  1285. def call_python_function_with_keyword_arg(x):
  1286. x :: str
  1287. r0 :: object
  1288. r1 :: str
  1289. r2 :: tuple
  1290. r3 :: object
  1291. r4 :: dict
  1292. r5 :: object
  1293. r6 :: int
  1294. L0:
  1295. r0 = load_address PyLong_Type
  1296. r1 = 'base'
  1297. r2 = PyTuple_Pack(1, x)
  1298. r3 = object 2
  1299. r4 = CPyDict_Build(1, r1, r3)
  1300. r5 = PyObject_Call(r0, r2, r4)
  1301. r6 = unbox(int, r5)
  1302. return r6
  1303. def call_python_method_with_keyword_args(xs, first, second):
  1304. xs :: list
  1305. first, second :: int
  1306. r0 :: str
  1307. r1 :: object
  1308. r2 :: str
  1309. r3 :: object
  1310. r4 :: tuple
  1311. r5 :: object
  1312. r6 :: dict
  1313. r7 :: object
  1314. r8 :: str
  1315. r9 :: object
  1316. r10, r11 :: str
  1317. r12 :: tuple
  1318. r13, r14 :: object
  1319. r15 :: dict
  1320. r16 :: object
  1321. L0:
  1322. r0 = 'insert'
  1323. r1 = CPyObject_GetAttr(xs, r0)
  1324. r2 = 'x'
  1325. r3 = object 0
  1326. r4 = PyTuple_Pack(1, r3)
  1327. r5 = box(int, first)
  1328. r6 = CPyDict_Build(1, r2, r5)
  1329. r7 = PyObject_Call(r1, r4, r6)
  1330. r8 = 'insert'
  1331. r9 = CPyObject_GetAttr(xs, r8)
  1332. r10 = 'x'
  1333. r11 = 'i'
  1334. r12 = PyTuple_Pack(0)
  1335. r13 = box(int, second)
  1336. r14 = object 1
  1337. r15 = CPyDict_Build(2, r10, r13, r11, r14)
  1338. r16 = PyObject_Call(r9, r12, r15)
  1339. return xs
  1340. [case testObjectAsBoolean]
  1341. from typing import List
  1342. def obj(x: object) -> int:
  1343. if x:
  1344. return 1
  1345. else:
  1346. return 0
  1347. def num(x: int) -> int:
  1348. if x:
  1349. return 1
  1350. else:
  1351. return 0
  1352. def lst(x: List[int]) -> int:
  1353. if x:
  1354. return 1
  1355. else:
  1356. return 0
  1357. [out]
  1358. def obj(x):
  1359. x :: object
  1360. r0 :: i32
  1361. r1 :: bit
  1362. r2 :: bool
  1363. L0:
  1364. r0 = PyObject_IsTrue(x)
  1365. r1 = r0 >= 0 :: signed
  1366. r2 = truncate r0: i32 to builtins.bool
  1367. if r2 goto L1 else goto L2 :: bool
  1368. L1:
  1369. return 2
  1370. L2:
  1371. return 0
  1372. L3:
  1373. unreachable
  1374. def num(x):
  1375. x :: int
  1376. r0 :: bit
  1377. L0:
  1378. r0 = x != 0
  1379. if r0 goto L1 else goto L2 :: bool
  1380. L1:
  1381. return 2
  1382. L2:
  1383. return 0
  1384. L3:
  1385. unreachable
  1386. def lst(x):
  1387. x :: list
  1388. r0 :: ptr
  1389. r1 :: native_int
  1390. r2 :: short_int
  1391. r3 :: bit
  1392. L0:
  1393. r0 = get_element_ptr x ob_size :: PyVarObject
  1394. r1 = load_mem r0 :: native_int*
  1395. keep_alive x
  1396. r2 = r1 << 1
  1397. r3 = r2 != 0
  1398. if r3 goto L1 else goto L2 :: bool
  1399. L1:
  1400. return 2
  1401. L2:
  1402. return 0
  1403. L3:
  1404. unreachable
  1405. [case testOptionalAsBoolean]
  1406. from typing import Optional
  1407. class A: pass
  1408. def opt_int(x: Optional[int]) -> int:
  1409. if x:
  1410. return 1
  1411. else:
  1412. return 0
  1413. def opt_a(x: Optional[A]) -> int:
  1414. if x:
  1415. return 1
  1416. else:
  1417. return 0
  1418. def opt_o(x: Optional[object]) -> int:
  1419. if x:
  1420. return 1
  1421. else:
  1422. return 0
  1423. [out]
  1424. def opt_int(x):
  1425. x :: union[int, None]
  1426. r0 :: object
  1427. r1 :: bit
  1428. r2 :: int
  1429. r3 :: bit
  1430. L0:
  1431. r0 = load_address _Py_NoneStruct
  1432. r1 = x != r0
  1433. if r1 goto L1 else goto L3 :: bool
  1434. L1:
  1435. r2 = unbox(int, x)
  1436. r3 = r2 != 0
  1437. if r3 goto L2 else goto L3 :: bool
  1438. L2:
  1439. return 2
  1440. L3:
  1441. return 0
  1442. L4:
  1443. unreachable
  1444. def opt_a(x):
  1445. x :: union[__main__.A, None]
  1446. r0 :: object
  1447. r1 :: bit
  1448. L0:
  1449. r0 = load_address _Py_NoneStruct
  1450. r1 = x != r0
  1451. if r1 goto L1 else goto L2 :: bool
  1452. L1:
  1453. return 2
  1454. L2:
  1455. return 0
  1456. L3:
  1457. unreachable
  1458. def opt_o(x):
  1459. x :: union[object, None]
  1460. r0 :: object
  1461. r1 :: bit
  1462. r2 :: object
  1463. r3 :: i32
  1464. r4 :: bit
  1465. r5 :: bool
  1466. L0:
  1467. r0 = load_address _Py_NoneStruct
  1468. r1 = x != r0
  1469. if r1 goto L1 else goto L3 :: bool
  1470. L1:
  1471. r2 = cast(object, x)
  1472. r3 = PyObject_IsTrue(r2)
  1473. r4 = r3 >= 0 :: signed
  1474. r5 = truncate r3: i32 to builtins.bool
  1475. if r5 goto L2 else goto L3 :: bool
  1476. L2:
  1477. return 2
  1478. L3:
  1479. return 0
  1480. L4:
  1481. unreachable
  1482. [case testRaise]
  1483. def foo() -> None:
  1484. raise Exception()
  1485. def bar() -> None:
  1486. raise Exception
  1487. [out]
  1488. def foo():
  1489. r0 :: object
  1490. r1 :: str
  1491. r2, r3 :: object
  1492. L0:
  1493. r0 = builtins :: module
  1494. r1 = 'Exception'
  1495. r2 = CPyObject_GetAttr(r0, r1)
  1496. r3 = PyObject_CallFunctionObjArgs(r2, 0)
  1497. CPy_Raise(r3)
  1498. unreachable
  1499. def bar():
  1500. r0 :: object
  1501. r1 :: str
  1502. r2 :: object
  1503. L0:
  1504. r0 = builtins :: module
  1505. r1 = 'Exception'
  1506. r2 = CPyObject_GetAttr(r0, r1)
  1507. CPy_Raise(r2)
  1508. unreachable
  1509. [case testModuleTopLevel_toplevel]
  1510. x = 1
  1511. print(x)
  1512. def f() -> None:
  1513. print(x)
  1514. [out]
  1515. def f():
  1516. r0 :: dict
  1517. r1 :: str
  1518. r2 :: object
  1519. r3 :: int
  1520. r4 :: object
  1521. r5 :: str
  1522. r6, r7, r8 :: object
  1523. L0:
  1524. r0 = __main__.globals :: static
  1525. r1 = 'x'
  1526. r2 = CPyDict_GetItem(r0, r1)
  1527. r3 = unbox(int, r2)
  1528. r4 = builtins :: module
  1529. r5 = 'print'
  1530. r6 = CPyObject_GetAttr(r4, r5)
  1531. r7 = box(int, r3)
  1532. r8 = PyObject_CallFunctionObjArgs(r6, r7, 0)
  1533. return 1
  1534. def __top_level__():
  1535. r0, r1 :: object
  1536. r2 :: bit
  1537. r3 :: str
  1538. r4 :: object
  1539. r5 :: dict
  1540. r6 :: str
  1541. r7 :: object
  1542. r8 :: i32
  1543. r9 :: bit
  1544. r10 :: dict
  1545. r11 :: str
  1546. r12 :: object
  1547. r13 :: int
  1548. r14 :: object
  1549. r15 :: str
  1550. r16, r17, r18 :: object
  1551. L0:
  1552. r0 = builtins :: module
  1553. r1 = load_address _Py_NoneStruct
  1554. r2 = r0 != r1
  1555. if r2 goto L2 else goto L1 :: bool
  1556. L1:
  1557. r3 = 'builtins'
  1558. r4 = PyImport_Import(r3)
  1559. builtins = r4 :: module
  1560. L2:
  1561. r5 = __main__.globals :: static
  1562. r6 = 'x'
  1563. r7 = object 1
  1564. r8 = CPyDict_SetItem(r5, r6, r7)
  1565. r9 = r8 >= 0 :: signed
  1566. r10 = __main__.globals :: static
  1567. r11 = 'x'
  1568. r12 = CPyDict_GetItem(r10, r11)
  1569. r13 = unbox(int, r12)
  1570. r14 = builtins :: module
  1571. r15 = 'print'
  1572. r16 = CPyObject_GetAttr(r14, r15)
  1573. r17 = box(int, r13)
  1574. r18 = PyObject_CallFunctionObjArgs(r16, r17, 0)
  1575. return 1
  1576. [case testCallOverloaded]
  1577. import m
  1578. def f() -> str:
  1579. return m.f(1)
  1580. [file m.pyi]
  1581. from typing import overload
  1582. @overload
  1583. def f(x: int) -> str: ...
  1584. @overload
  1585. def f(x: str) -> int: ...
  1586. [out]
  1587. def f():
  1588. r0 :: object
  1589. r1 :: str
  1590. r2, r3, r4 :: object
  1591. r5 :: str
  1592. L0:
  1593. r0 = m :: module
  1594. r1 = 'f'
  1595. r2 = CPyObject_GetAttr(r0, r1)
  1596. r3 = object 1
  1597. r4 = PyObject_CallFunctionObjArgs(r2, r3, 0)
  1598. r5 = cast(str, r4)
  1599. return r5
  1600. [case testCallOverloadedNative]
  1601. from typing import overload, Union
  1602. @overload
  1603. def foo(x: int) -> int: ...
  1604. @overload
  1605. def foo(x: str) -> str: ...
  1606. def foo(x: Union[int, str]) -> Union[int, str]:
  1607. return x
  1608. def main() -> None:
  1609. x = foo(0)
  1610. [out]
  1611. def foo(x):
  1612. x :: union[int, str]
  1613. L0:
  1614. return x
  1615. def main():
  1616. r0 :: object
  1617. r1 :: union[int, str]
  1618. r2, x :: int
  1619. L0:
  1620. r0 = object 0
  1621. r1 = foo(r0)
  1622. r2 = unbox(int, r1)
  1623. x = r2
  1624. return 1
  1625. [case testCallOverloadedNativeSubclass]
  1626. from typing import overload, Union
  1627. class A:
  1628. x: int
  1629. class B(A):
  1630. y: int
  1631. @overload
  1632. def foo(x: int) -> B: ...
  1633. @overload
  1634. def foo(x: Union[int, str]) -> A: ...
  1635. def foo(x: Union[int, str]) -> A:
  1636. if isinstance(x, int):
  1637. return B()
  1638. return A()
  1639. def main() -> None:
  1640. x = foo(0)
  1641. [out]
  1642. def foo(x):
  1643. x :: union[int, str]
  1644. r0 :: object
  1645. r1 :: i32
  1646. r2 :: bit
  1647. r3 :: bool
  1648. r4 :: __main__.B
  1649. r5 :: __main__.A
  1650. L0:
  1651. r0 = load_address PyLong_Type
  1652. r1 = PyObject_IsInstance(x, r0)
  1653. r2 = r1 >= 0 :: signed
  1654. r3 = truncate r1: i32 to builtins.bool
  1655. if r3 goto L1 else goto L2 :: bool
  1656. L1:
  1657. r4 = B()
  1658. return r4
  1659. L2:
  1660. r5 = A()
  1661. return r5
  1662. def main():
  1663. r0 :: object
  1664. r1 :: __main__.A
  1665. r2, x :: __main__.B
  1666. L0:
  1667. r0 = object 0
  1668. r1 = foo(r0)
  1669. r2 = cast(__main__.B, r1)
  1670. x = r2
  1671. return 1
  1672. [case testFunctionCallWithKeywordArgs]
  1673. def f(x: int, y: str) -> None: pass
  1674. def g() -> None:
  1675. f(y='a', x=0)
  1676. f(1, y='b')
  1677. [out]
  1678. def f(x, y):
  1679. x :: int
  1680. y :: str
  1681. L0:
  1682. return 1
  1683. def g():
  1684. r0 :: str
  1685. r1 :: None
  1686. r2 :: str
  1687. r3 :: None
  1688. L0:
  1689. r0 = 'a'
  1690. r1 = f(0, r0)
  1691. r2 = 'b'
  1692. r3 = f(2, r2)
  1693. return 1
  1694. [case testMethodCallWithKeywordArgs]
  1695. class A:
  1696. def f(self, x: int, y: str) -> None: pass
  1697. def g(a: A) -> None:
  1698. a.f(y='a', x=0)
  1699. a.f(1, y='b')
  1700. [out]
  1701. def A.f(self, x, y):
  1702. self :: __main__.A
  1703. x :: int
  1704. y :: str
  1705. L0:
  1706. return 1
  1707. def g(a):
  1708. a :: __main__.A
  1709. r0 :: str
  1710. r1 :: None
  1711. r2 :: str
  1712. r3 :: None
  1713. L0:
  1714. r0 = 'a'
  1715. r1 = a.f(0, r0)
  1716. r2 = 'b'
  1717. r3 = a.f(2, r2)
  1718. return 1
  1719. [case testStarArgs]
  1720. from typing import Tuple
  1721. def f(a: int, b: int, c: int) -> Tuple[int, int, int]:
  1722. return a, b, c
  1723. def g() -> Tuple[int, int, int]:
  1724. return f(*(1, 2, 3))
  1725. def h() -> Tuple[int, int, int]:
  1726. return f(1, *(2, 3))
  1727. [out]
  1728. def f(a, b, c):
  1729. a, b, c :: int
  1730. r0 :: tuple[int, int, int]
  1731. L0:
  1732. r0 = (a, b, c)
  1733. return r0
  1734. def g():
  1735. r0 :: tuple[int, int, int]
  1736. r1 :: dict
  1737. r2 :: str
  1738. r3 :: object
  1739. r4 :: list
  1740. r5, r6 :: object
  1741. r7 :: tuple
  1742. r8 :: dict
  1743. r9 :: object
  1744. r10 :: tuple[int, int, int]
  1745. L0:
  1746. r0 = (2, 4, 6)
  1747. r1 = __main__.globals :: static
  1748. r2 = 'f'
  1749. r3 = CPyDict_GetItem(r1, r2)
  1750. r4 = PyList_New(0)
  1751. r5 = box(tuple[int, int, int], r0)
  1752. r6 = CPyList_Extend(r4, r5)
  1753. r7 = PyList_AsTuple(r4)
  1754. r8 = PyDict_New()
  1755. r9 = PyObject_Call(r3, r7, r8)
  1756. r10 = unbox(tuple[int, int, int], r9)
  1757. return r10
  1758. def h():
  1759. r0 :: tuple[int, int]
  1760. r1 :: dict
  1761. r2 :: str
  1762. r3 :: object
  1763. r4 :: list
  1764. r5 :: object
  1765. r6, r7 :: ptr
  1766. r8, r9 :: object
  1767. r10 :: tuple
  1768. r11 :: dict
  1769. r12 :: object
  1770. r13 :: tuple[int, int, int]
  1771. L0:
  1772. r0 = (4, 6)
  1773. r1 = __main__.globals :: static
  1774. r2 = 'f'
  1775. r3 = CPyDict_GetItem(r1, r2)
  1776. r4 = PyList_New(1)
  1777. r5 = object 1
  1778. r6 = get_element_ptr r4 ob_item :: PyListObject
  1779. r7 = load_mem r6 :: ptr*
  1780. set_mem r7, r5 :: builtins.object*
  1781. keep_alive r4
  1782. r8 = box(tuple[int, int], r0)
  1783. r9 = CPyList_Extend(r4, r8)
  1784. r10 = PyList_AsTuple(r4)
  1785. r11 = PyDict_New()
  1786. r12 = PyObject_Call(r3, r10, r11)
  1787. r13 = unbox(tuple[int, int, int], r12)
  1788. return r13
  1789. [case testStar2Args]
  1790. from typing import Tuple
  1791. def f(a: int, b: int, c: int) -> Tuple[int, int, int]:
  1792. return a, b, c
  1793. def g() -> Tuple[int, int, int]:
  1794. return f(**{'a': 1, 'b': 2, 'c': 3})
  1795. def h() -> Tuple[int, int, int]:
  1796. return f(1, **{'b': 2, 'c': 3})
  1797. [out]
  1798. def f(a, b, c):
  1799. a, b, c :: int
  1800. r0 :: tuple[int, int, int]
  1801. L0:
  1802. r0 = (a, b, c)
  1803. return r0
  1804. def g():
  1805. r0, r1, r2 :: str
  1806. r3, r4, r5 :: object
  1807. r6, r7 :: dict
  1808. r8 :: str
  1809. r9 :: object
  1810. r10 :: dict
  1811. r11 :: i32
  1812. r12 :: bit
  1813. r13 :: tuple
  1814. r14 :: object
  1815. r15 :: tuple[int, int, int]
  1816. L0:
  1817. r0 = 'a'
  1818. r1 = 'b'
  1819. r2 = 'c'
  1820. r3 = object 1
  1821. r4 = object 2
  1822. r5 = object 3
  1823. r6 = CPyDict_Build(3, r0, r3, r1, r4, r2, r5)
  1824. r7 = __main__.globals :: static
  1825. r8 = 'f'
  1826. r9 = CPyDict_GetItem(r7, r8)
  1827. r10 = PyDict_New()
  1828. r11 = CPyDict_UpdateInDisplay(r10, r6)
  1829. r12 = r11 >= 0 :: signed
  1830. r13 = PyTuple_Pack(0)
  1831. r14 = PyObject_Call(r9, r13, r10)
  1832. r15 = unbox(tuple[int, int, int], r14)
  1833. return r15
  1834. def h():
  1835. r0, r1 :: str
  1836. r2, r3 :: object
  1837. r4, r5 :: dict
  1838. r6 :: str
  1839. r7 :: object
  1840. r8 :: dict
  1841. r9 :: i32
  1842. r10 :: bit
  1843. r11 :: object
  1844. r12 :: tuple
  1845. r13 :: object
  1846. r14 :: tuple[int, int, int]
  1847. L0:
  1848. r0 = 'b'
  1849. r1 = 'c'
  1850. r2 = object 2
  1851. r3 = object 3
  1852. r4 = CPyDict_Build(2, r0, r2, r1, r3)
  1853. r5 = __main__.globals :: static
  1854. r6 = 'f'
  1855. r7 = CPyDict_GetItem(r5, r6)
  1856. r8 = PyDict_New()
  1857. r9 = CPyDict_UpdateInDisplay(r8, r4)
  1858. r10 = r9 >= 0 :: signed
  1859. r11 = object 1
  1860. r12 = PyTuple_Pack(1, r11)
  1861. r13 = PyObject_Call(r7, r12, r8)
  1862. r14 = unbox(tuple[int, int, int], r13)
  1863. return r14
  1864. [case testFunctionCallWithDefaultArgs]
  1865. def f(x: int, y: int = 3, z: str = "test") -> None:
  1866. return None
  1867. def g() -> None:
  1868. f(2)
  1869. f(y = 3, x = 6)
  1870. [out]
  1871. def f(x, y, z):
  1872. x, y :: int
  1873. z, r0 :: str
  1874. L0:
  1875. if is_error(y) goto L1 else goto L2
  1876. L1:
  1877. y = 6
  1878. L2:
  1879. if is_error(z) goto L3 else goto L4
  1880. L3:
  1881. r0 = 'test'
  1882. z = r0
  1883. L4:
  1884. return 1
  1885. def g():
  1886. r0 :: int
  1887. r1 :: str
  1888. r2 :: None
  1889. r3 :: str
  1890. r4 :: None
  1891. L0:
  1892. r0 = <error> :: int
  1893. r1 = <error> :: str
  1894. r2 = f(4, r0, r1)
  1895. r3 = <error> :: str
  1896. r4 = f(12, 6, r3)
  1897. return 1
  1898. [case testMethodCallWithDefaultArgs]
  1899. class A:
  1900. def f(self, x: int, y: int = 3, z: str = "test") -> None:
  1901. return None
  1902. def g() -> None:
  1903. a = A()
  1904. a.f(2)
  1905. a.f(y = 3, x = 6)
  1906. [out]
  1907. def A.f(self, x, y, z):
  1908. self :: __main__.A
  1909. x, y :: int
  1910. z, r0 :: str
  1911. L0:
  1912. if is_error(y) goto L1 else goto L2
  1913. L1:
  1914. y = 6
  1915. L2:
  1916. if is_error(z) goto L3 else goto L4
  1917. L3:
  1918. r0 = 'test'
  1919. z = r0
  1920. L4:
  1921. return 1
  1922. def g():
  1923. r0, a :: __main__.A
  1924. r1 :: int
  1925. r2 :: str
  1926. r3 :: None
  1927. r4 :: str
  1928. r5 :: None
  1929. L0:
  1930. r0 = A()
  1931. a = r0
  1932. r1 = <error> :: int
  1933. r2 = <error> :: str
  1934. r3 = a.f(4, r1, r2)
  1935. r4 = <error> :: str
  1936. r5 = a.f(12, 6, r4)
  1937. return 1
  1938. [case testListComprehension]
  1939. from typing import List
  1940. def f() -> List[int]:
  1941. return [x*x for x in [1,2,3] if x != 2 if x != 3]
  1942. [out]
  1943. def f():
  1944. r0, r1 :: list
  1945. r2, r3, r4 :: object
  1946. r5, r6, r7, r8 :: ptr
  1947. r9 :: short_int
  1948. r10 :: ptr
  1949. r11 :: native_int
  1950. r12 :: short_int
  1951. r13 :: bit
  1952. r14 :: object
  1953. r15, x :: int
  1954. r16 :: native_int
  1955. r17, r18 :: bit
  1956. r19 :: bool
  1957. r20, r21 :: bit
  1958. r22 :: native_int
  1959. r23, r24 :: bit
  1960. r25 :: bool
  1961. r26, r27 :: bit
  1962. r28 :: int
  1963. r29 :: object
  1964. r30 :: i32
  1965. r31 :: bit
  1966. r32 :: short_int
  1967. L0:
  1968. r0 = PyList_New(0)
  1969. r1 = PyList_New(3)
  1970. r2 = object 1
  1971. r3 = object 2
  1972. r4 = object 3
  1973. r5 = get_element_ptr r1 ob_item :: PyListObject
  1974. r6 = load_mem r5 :: ptr*
  1975. set_mem r6, r2 :: builtins.object*
  1976. r7 = r6 + WORD_SIZE*1
  1977. set_mem r7, r3 :: builtins.object*
  1978. r8 = r6 + WORD_SIZE*2
  1979. set_mem r8, r4 :: builtins.object*
  1980. keep_alive r1
  1981. r9 = 0
  1982. L1:
  1983. r10 = get_element_ptr r1 ob_size :: PyVarObject
  1984. r11 = load_mem r10 :: native_int*
  1985. keep_alive r1
  1986. r12 = r11 << 1
  1987. r13 = r9 < r12 :: signed
  1988. if r13 goto L2 else goto L14 :: bool
  1989. L2:
  1990. r14 = CPyList_GetItemUnsafe(r1, r9)
  1991. r15 = unbox(int, r14)
  1992. x = r15
  1993. r16 = x & 1
  1994. r17 = r16 == 0
  1995. if r17 goto L3 else goto L4 :: bool
  1996. L3:
  1997. r18 = x != 4
  1998. r19 = r18
  1999. goto L5
  2000. L4:
  2001. r20 = CPyTagged_IsEq_(x, 4)
  2002. r21 = r20 ^ 1
  2003. r19 = r21
  2004. L5:
  2005. if r19 goto L7 else goto L6 :: bool
  2006. L6:
  2007. goto L13
  2008. L7:
  2009. r22 = x & 1
  2010. r23 = r22 == 0
  2011. if r23 goto L8 else goto L9 :: bool
  2012. L8:
  2013. r24 = x != 6
  2014. r25 = r24
  2015. goto L10
  2016. L9:
  2017. r26 = CPyTagged_IsEq_(x, 6)
  2018. r27 = r26 ^ 1
  2019. r25 = r27
  2020. L10:
  2021. if r25 goto L12 else goto L11 :: bool
  2022. L11:
  2023. goto L13
  2024. L12:
  2025. r28 = CPyTagged_Multiply(x, x)
  2026. r29 = box(int, r28)
  2027. r30 = PyList_Append(r0, r29)
  2028. r31 = r30 >= 0 :: signed
  2029. L13:
  2030. r32 = r9 + 2
  2031. r9 = r32
  2032. goto L1
  2033. L14:
  2034. return r0
  2035. [case testDictComprehension]
  2036. from typing import Dict
  2037. def f() -> Dict[int, int]:
  2038. return {x: x*x for x in [1,2,3] if x != 2 if x != 3}
  2039. [out]
  2040. def f():
  2041. r0 :: dict
  2042. r1 :: list
  2043. r2, r3, r4 :: object
  2044. r5, r6, r7, r8 :: ptr
  2045. r9 :: short_int
  2046. r10 :: ptr
  2047. r11 :: native_int
  2048. r12 :: short_int
  2049. r13 :: bit
  2050. r14 :: object
  2051. r15, x :: int
  2052. r16 :: native_int
  2053. r17, r18 :: bit
  2054. r19 :: bool
  2055. r20, r21 :: bit
  2056. r22 :: native_int
  2057. r23, r24 :: bit
  2058. r25 :: bool
  2059. r26, r27 :: bit
  2060. r28 :: int
  2061. r29, r30 :: object
  2062. r31 :: i32
  2063. r32 :: bit
  2064. r33 :: short_int
  2065. L0:
  2066. r0 = PyDict_New()
  2067. r1 = PyList_New(3)
  2068. r2 = object 1
  2069. r3 = object 2
  2070. r4 = object 3
  2071. r5 = get_element_ptr r1 ob_item :: PyListObject
  2072. r6 = load_mem r5 :: ptr*
  2073. set_mem r6, r2 :: builtins.object*
  2074. r7 = r6 + WORD_SIZE*1
  2075. set_mem r7, r3 :: builtins.object*
  2076. r8 = r6 + WORD_SIZE*2
  2077. set_mem r8, r4 :: builtins.object*
  2078. keep_alive r1
  2079. r9 = 0
  2080. L1:
  2081. r10 = get_element_ptr r1 ob_size :: PyVarObject
  2082. r11 = load_mem r10 :: native_int*
  2083. keep_alive r1
  2084. r12 = r11 << 1
  2085. r13 = r9 < r12 :: signed
  2086. if r13 goto L2 else goto L14 :: bool
  2087. L2:
  2088. r14 = CPyList_GetItemUnsafe(r1, r9)
  2089. r15 = unbox(int, r14)
  2090. x = r15
  2091. r16 = x & 1
  2092. r17 = r16 == 0
  2093. if r17 goto L3 else goto L4 :: bool
  2094. L3:
  2095. r18 = x != 4
  2096. r19 = r18
  2097. goto L5
  2098. L4:
  2099. r20 = CPyTagged_IsEq_(x, 4)
  2100. r21 = r20 ^ 1
  2101. r19 = r21
  2102. L5:
  2103. if r19 goto L7 else goto L6 :: bool
  2104. L6:
  2105. goto L13
  2106. L7:
  2107. r22 = x & 1
  2108. r23 = r22 == 0
  2109. if r23 goto L8 else goto L9 :: bool
  2110. L8:
  2111. r24 = x != 6
  2112. r25 = r24
  2113. goto L10
  2114. L9:
  2115. r26 = CPyTagged_IsEq_(x, 6)
  2116. r27 = r26 ^ 1
  2117. r25 = r27
  2118. L10:
  2119. if r25 goto L12 else goto L11 :: bool
  2120. L11:
  2121. goto L13
  2122. L12:
  2123. r28 = CPyTagged_Multiply(x, x)
  2124. r29 = box(int, x)
  2125. r30 = box(int, r28)
  2126. r31 = CPyDict_SetItem(r0, r29, r30)
  2127. r32 = r31 >= 0 :: signed
  2128. L13:
  2129. r33 = r9 + 2
  2130. r9 = r33
  2131. goto L1
  2132. L14:
  2133. return r0
  2134. [case testLoopsMultipleAssign]
  2135. from typing import List, Tuple
  2136. def f(l: List[Tuple[int, int, int]]) -> List[int]:
  2137. for x, y, z in l:
  2138. pass
  2139. return [x+y+z for x, y, z in l]
  2140. [out]
  2141. def f(l):
  2142. l :: list
  2143. r0 :: short_int
  2144. r1 :: ptr
  2145. r2 :: native_int
  2146. r3 :: short_int
  2147. r4 :: bit
  2148. r5 :: object
  2149. r6 :: tuple[int, int, int]
  2150. r7, x, r8, y, r9, z :: int
  2151. r10 :: short_int
  2152. r11 :: ptr
  2153. r12 :: native_int
  2154. r13 :: list
  2155. r14 :: short_int
  2156. r15 :: ptr
  2157. r16 :: native_int
  2158. r17 :: short_int
  2159. r18 :: bit
  2160. r19 :: object
  2161. r20 :: tuple[int, int, int]
  2162. r21, x_2, r22, y_2, r23, z_2, r24, r25 :: int
  2163. r26 :: object
  2164. r27 :: bit
  2165. r28 :: short_int
  2166. L0:
  2167. r0 = 0
  2168. L1:
  2169. r1 = get_element_ptr l ob_size :: PyVarObject
  2170. r2 = load_mem r1 :: native_int*
  2171. keep_alive l
  2172. r3 = r2 << 1
  2173. r4 = r0 < r3 :: signed
  2174. if r4 goto L2 else goto L4 :: bool
  2175. L2:
  2176. r5 = CPyList_GetItemUnsafe(l, r0)
  2177. r6 = unbox(tuple[int, int, int], r5)
  2178. r7 = r6[0]
  2179. x = r7
  2180. r8 = r6[1]
  2181. y = r8
  2182. r9 = r6[2]
  2183. z = r9
  2184. L3:
  2185. r10 = r0 + 2
  2186. r0 = r10
  2187. goto L1
  2188. L4:
  2189. r11 = get_element_ptr l ob_size :: PyVarObject
  2190. r12 = load_mem r11 :: native_int*
  2191. keep_alive l
  2192. r13 = PyList_New(r12)
  2193. r14 = 0
  2194. L5:
  2195. r15 = get_element_ptr l ob_size :: PyVarObject
  2196. r16 = load_mem r15 :: native_int*
  2197. keep_alive l
  2198. r17 = r16 << 1
  2199. r18 = r14 < r17 :: signed
  2200. if r18 goto L6 else goto L8 :: bool
  2201. L6:
  2202. r19 = CPyList_GetItemUnsafe(l, r14)
  2203. r20 = unbox(tuple[int, int, int], r19)
  2204. r21 = r20[0]
  2205. x_2 = r21
  2206. r22 = r20[1]
  2207. y_2 = r22
  2208. r23 = r20[2]
  2209. z_2 = r23
  2210. r24 = CPyTagged_Add(x_2, y_2)
  2211. r25 = CPyTagged_Add(r24, z_2)
  2212. r26 = box(int, r25)
  2213. r27 = CPyList_SetItemUnsafe(r13, r14, r26)
  2214. L7:
  2215. r28 = r14 + 2
  2216. r14 = r28
  2217. goto L5
  2218. L8:
  2219. return r13
  2220. [case testProperty]
  2221. class PropertyHolder:
  2222. @property
  2223. def value(self) -> int:
  2224. return self.left + self.right if self.is_add else self.left - self.right
  2225. def __init__(self, left: int, right: int, is_add: bool) -> None:
  2226. self.left = left
  2227. self.right = right
  2228. self.is_add = is_add
  2229. def twice_value(self) -> int:
  2230. return 2 * self.value
  2231. [out]
  2232. def PropertyHolder.value(self):
  2233. self :: __main__.PropertyHolder
  2234. r0 :: bool
  2235. r1, r2, r3, r4, r5, r6, r7 :: int
  2236. L0:
  2237. r0 = self.is_add
  2238. if r0 goto L1 else goto L2 :: bool
  2239. L1:
  2240. r1 = borrow self.left
  2241. r2 = borrow self.right
  2242. r3 = CPyTagged_Add(r1, r2)
  2243. keep_alive self, self
  2244. r4 = r3
  2245. goto L3
  2246. L2:
  2247. r5 = borrow self.left
  2248. r6 = borrow self.right
  2249. r7 = CPyTagged_Subtract(r5, r6)
  2250. keep_alive self, self
  2251. r4 = r7
  2252. L3:
  2253. return r4
  2254. def PropertyHolder.__init__(self, left, right, is_add):
  2255. self :: __main__.PropertyHolder
  2256. left, right :: int
  2257. is_add :: bool
  2258. L0:
  2259. self.left = left
  2260. self.right = right
  2261. self.is_add = is_add
  2262. return 1
  2263. def PropertyHolder.twice_value(self):
  2264. self :: __main__.PropertyHolder
  2265. r0, r1 :: int
  2266. L0:
  2267. r0 = self.value
  2268. r1 = CPyTagged_Multiply(4, r0)
  2269. return r1
  2270. [case testNativeIndex]
  2271. from typing import List
  2272. class A:
  2273. def __getitem__(self, index: int) -> int: pass
  2274. def g(a: A, b: List[int], c: int) -> int:
  2275. return a[c] + b[c]
  2276. [out]
  2277. def A.__getitem__(self, index):
  2278. self :: __main__.A
  2279. index :: int
  2280. L0:
  2281. unreachable
  2282. def g(a, b, c):
  2283. a :: __main__.A
  2284. b :: list
  2285. c, r0 :: int
  2286. r1 :: object
  2287. r2, r3 :: int
  2288. L0:
  2289. r0 = a.__getitem__(c)
  2290. r1 = CPyList_GetItemBorrow(b, c)
  2291. r2 = unbox(int, r1)
  2292. r3 = CPyTagged_Add(r0, r2)
  2293. keep_alive b, c
  2294. return r3
  2295. [case testTypeAlias_toplevel]
  2296. from typing import List, NewType, NamedTuple
  2297. Lol = NamedTuple('Lol', (('a', int), ('b', str)))
  2298. x = Lol(1, '')
  2299. Foo = List[int]
  2300. Bar = NewType('Bar', Foo)
  2301. y = Bar([1,2,3])
  2302. [out]
  2303. def __top_level__():
  2304. r0, r1 :: object
  2305. r2 :: bit
  2306. r3 :: str
  2307. r4, r5 :: object
  2308. r6 :: str
  2309. r7 :: dict
  2310. r8 :: object
  2311. r9, r10 :: str
  2312. r11 :: object
  2313. r12 :: tuple[str, object]
  2314. r13 :: object
  2315. r14 :: str
  2316. r15 :: object
  2317. r16 :: tuple[str, object]
  2318. r17 :: object
  2319. r18 :: tuple[object, object]
  2320. r19 :: object
  2321. r20 :: dict
  2322. r21 :: str
  2323. r22, r23 :: object
  2324. r24 :: dict
  2325. r25 :: str
  2326. r26 :: i32
  2327. r27 :: bit
  2328. r28 :: str
  2329. r29 :: dict
  2330. r30 :: str
  2331. r31, r32, r33 :: object
  2332. r34 :: tuple
  2333. r35 :: dict
  2334. r36 :: str
  2335. r37 :: i32
  2336. r38 :: bit
  2337. r39 :: dict
  2338. r40 :: str
  2339. r41, r42, r43 :: object
  2340. r44 :: dict
  2341. r45 :: str
  2342. r46 :: i32
  2343. r47 :: bit
  2344. r48 :: str
  2345. r49 :: dict
  2346. r50 :: str
  2347. r51 :: object
  2348. r52 :: dict
  2349. r53 :: str
  2350. r54, r55 :: object
  2351. r56 :: dict
  2352. r57 :: str
  2353. r58 :: i32
  2354. r59 :: bit
  2355. r60 :: list
  2356. r61, r62, r63 :: object
  2357. r64, r65, r66, r67 :: ptr
  2358. r68 :: dict
  2359. r69 :: str
  2360. r70 :: i32
  2361. r71 :: bit
  2362. L0:
  2363. r0 = builtins :: module
  2364. r1 = load_address _Py_NoneStruct
  2365. r2 = r0 != r1
  2366. if r2 goto L2 else goto L1 :: bool
  2367. L1:
  2368. r3 = 'builtins'
  2369. r4 = PyImport_Import(r3)
  2370. builtins = r4 :: module
  2371. L2:
  2372. r5 = ('List', 'NewType', 'NamedTuple')
  2373. r6 = 'typing'
  2374. r7 = __main__.globals :: static
  2375. r8 = CPyImport_ImportFromMany(r6, r5, r5, r7)
  2376. typing = r8 :: module
  2377. r9 = 'Lol'
  2378. r10 = 'a'
  2379. r11 = load_address PyLong_Type
  2380. r12 = (r10, r11)
  2381. r13 = box(tuple[str, object], r12)
  2382. r14 = 'b'
  2383. r15 = load_address PyUnicode_Type
  2384. r16 = (r14, r15)
  2385. r17 = box(tuple[str, object], r16)
  2386. r18 = (r13, r17)
  2387. r19 = box(tuple[object, object], r18)
  2388. r20 = __main__.globals :: static
  2389. r21 = 'NamedTuple'
  2390. r22 = CPyDict_GetItem(r20, r21)
  2391. r23 = PyObject_CallFunctionObjArgs(r22, r9, r19, 0)
  2392. r24 = __main__.globals :: static
  2393. r25 = 'Lol'
  2394. r26 = CPyDict_SetItem(r24, r25, r23)
  2395. r27 = r26 >= 0 :: signed
  2396. r28 = ''
  2397. r29 = __main__.globals :: static
  2398. r30 = 'Lol'
  2399. r31 = CPyDict_GetItem(r29, r30)
  2400. r32 = object 1
  2401. r33 = PyObject_CallFunctionObjArgs(r31, r32, r28, 0)
  2402. r34 = cast(tuple, r33)
  2403. r35 = __main__.globals :: static
  2404. r36 = 'x'
  2405. r37 = CPyDict_SetItem(r35, r36, r34)
  2406. r38 = r37 >= 0 :: signed
  2407. r39 = __main__.globals :: static
  2408. r40 = 'List'
  2409. r41 = CPyDict_GetItem(r39, r40)
  2410. r42 = load_address PyLong_Type
  2411. r43 = PyObject_GetItem(r41, r42)
  2412. r44 = __main__.globals :: static
  2413. r45 = 'Foo'
  2414. r46 = CPyDict_SetItem(r44, r45, r43)
  2415. r47 = r46 >= 0 :: signed
  2416. r48 = 'Bar'
  2417. r49 = __main__.globals :: static
  2418. r50 = 'Foo'
  2419. r51 = CPyDict_GetItem(r49, r50)
  2420. r52 = __main__.globals :: static
  2421. r53 = 'NewType'
  2422. r54 = CPyDict_GetItem(r52, r53)
  2423. r55 = PyObject_CallFunctionObjArgs(r54, r48, r51, 0)
  2424. r56 = __main__.globals :: static
  2425. r57 = 'Bar'
  2426. r58 = CPyDict_SetItem(r56, r57, r55)
  2427. r59 = r58 >= 0 :: signed
  2428. r60 = PyList_New(3)
  2429. r61 = object 1
  2430. r62 = object 2
  2431. r63 = object 3
  2432. r64 = get_element_ptr r60 ob_item :: PyListObject
  2433. r65 = load_mem r64 :: ptr*
  2434. set_mem r65, r61 :: builtins.object*
  2435. r66 = r65 + WORD_SIZE*1
  2436. set_mem r66, r62 :: builtins.object*
  2437. r67 = r65 + WORD_SIZE*2
  2438. set_mem r67, r63 :: builtins.object*
  2439. keep_alive r60
  2440. r68 = __main__.globals :: static
  2441. r69 = 'y'
  2442. r70 = CPyDict_SetItem(r68, r69, r60)
  2443. r71 = r70 >= 0 :: signed
  2444. return 1
  2445. [case testChainedConditional]
  2446. def g(x: int) -> int:
  2447. return x
  2448. def f(x: int, y: int, z: int) -> bool:
  2449. return g(x) < g(y) > g(z)
  2450. [out]
  2451. def g(x):
  2452. x :: int
  2453. L0:
  2454. return x
  2455. def f(x, y, z):
  2456. x, y, z, r0, r1 :: int
  2457. r2 :: native_int
  2458. r3 :: bit
  2459. r4 :: native_int
  2460. r5, r6, r7 :: bit
  2461. r8 :: bool
  2462. r9 :: bit
  2463. r10 :: bool
  2464. r11 :: int
  2465. r12 :: native_int
  2466. r13 :: bit
  2467. r14 :: native_int
  2468. r15, r16, r17 :: bit
  2469. r18 :: bool
  2470. r19 :: bit
  2471. L0:
  2472. r0 = g(x)
  2473. r1 = g(y)
  2474. r2 = r0 & 1
  2475. r3 = r2 == 0
  2476. r4 = r1 & 1
  2477. r5 = r4 == 0
  2478. r6 = r3 & r5
  2479. if r6 goto L1 else goto L2 :: bool
  2480. L1:
  2481. r7 = r0 < r1 :: signed
  2482. r8 = r7
  2483. goto L3
  2484. L2:
  2485. r9 = CPyTagged_IsLt_(r0, r1)
  2486. r8 = r9
  2487. L3:
  2488. if r8 goto L5 else goto L4 :: bool
  2489. L4:
  2490. r10 = r8
  2491. goto L9
  2492. L5:
  2493. r11 = g(z)
  2494. r12 = r1 & 1
  2495. r13 = r12 == 0
  2496. r14 = r11 & 1
  2497. r15 = r14 == 0
  2498. r16 = r13 & r15
  2499. if r16 goto L6 else goto L7 :: bool
  2500. L6:
  2501. r17 = r1 > r11 :: signed
  2502. r18 = r17
  2503. goto L8
  2504. L7:
  2505. r19 = CPyTagged_IsLt_(r11, r1)
  2506. r18 = r19
  2507. L8:
  2508. r10 = r18
  2509. L9:
  2510. return r10
  2511. [case testEq]
  2512. class A:
  2513. def __eq__(self, x: object) -> bool:
  2514. return NotImplemented
  2515. [out]
  2516. def A.__eq__(self, x):
  2517. self :: __main__.A
  2518. x, r0 :: object
  2519. L0:
  2520. r0 = load_address _Py_NotImplementedStruct
  2521. return r0
  2522. def A.__ne__(__mypyc_self__, rhs):
  2523. __mypyc_self__ :: __main__.A
  2524. rhs, r0, r1 :: object
  2525. r2 :: bit
  2526. r3 :: i32
  2527. r4 :: bit
  2528. r5 :: bool
  2529. r6 :: object
  2530. L0:
  2531. r0 = __mypyc_self__.__eq__(rhs)
  2532. r1 = load_address _Py_NotImplementedStruct
  2533. r2 = r0 == r1
  2534. if r2 goto L2 else goto L1 :: bool
  2535. L1:
  2536. r3 = PyObject_Not(r0)
  2537. r4 = r3 >= 0 :: signed
  2538. r5 = truncate r3: i32 to builtins.bool
  2539. r6 = box(bool, r5)
  2540. return r6
  2541. L2:
  2542. return r1
  2543. [case testDecorators_toplevel]
  2544. from typing import Callable
  2545. def a(f: Callable[[], None]) -> Callable[[], None]:
  2546. def g() -> None:
  2547. print('Entering')
  2548. f()
  2549. print('Exited')
  2550. return g
  2551. def b(f: Callable[[], None]) -> Callable[[], None]:
  2552. def g() -> None:
  2553. print('---')
  2554. f()
  2555. print('---')
  2556. return g
  2557. @a
  2558. @b
  2559. def c() -> None:
  2560. @a
  2561. @b
  2562. def d() -> None:
  2563. print('d')
  2564. print('c')
  2565. d()
  2566. [out]
  2567. def g_a_obj.__get__(__mypyc_self__, instance, owner):
  2568. __mypyc_self__, instance, owner, r0 :: object
  2569. r1 :: bit
  2570. r2 :: object
  2571. L0:
  2572. r0 = load_address _Py_NoneStruct
  2573. r1 = instance == r0
  2574. if r1 goto L1 else goto L2 :: bool
  2575. L1:
  2576. return __mypyc_self__
  2577. L2:
  2578. r2 = PyMethod_New(__mypyc_self__, instance)
  2579. return r2
  2580. def g_a_obj.__call__(__mypyc_self__):
  2581. __mypyc_self__ :: __main__.g_a_obj
  2582. r0 :: __main__.a_env
  2583. r1, g :: object
  2584. r2 :: str
  2585. r3 :: object
  2586. r4 :: str
  2587. r5, r6, r7, r8 :: object
  2588. r9 :: str
  2589. r10 :: object
  2590. r11 :: str
  2591. r12, r13 :: object
  2592. L0:
  2593. r0 = __mypyc_self__.__mypyc_env__
  2594. r1 = r0.g
  2595. g = r1
  2596. r2 = 'Entering'
  2597. r3 = builtins :: module
  2598. r4 = 'print'
  2599. r5 = CPyObject_GetAttr(r3, r4)
  2600. r6 = PyObject_CallFunctionObjArgs(r5, r2, 0)
  2601. r7 = r0.f
  2602. r8 = PyObject_CallFunctionObjArgs(r7, 0)
  2603. r9 = 'Exited'
  2604. r10 = builtins :: module
  2605. r11 = 'print'
  2606. r12 = CPyObject_GetAttr(r10, r11)
  2607. r13 = PyObject_CallFunctionObjArgs(r12, r9, 0)
  2608. return 1
  2609. def a(f):
  2610. f :: object
  2611. r0 :: __main__.a_env
  2612. r1 :: bool
  2613. r2 :: __main__.g_a_obj
  2614. r3, r4 :: bool
  2615. r5 :: object
  2616. L0:
  2617. r0 = a_env()
  2618. r0.f = f; r1 = is_error
  2619. r2 = g_a_obj()
  2620. r2.__mypyc_env__ = r0; r3 = is_error
  2621. r0.g = r2; r4 = is_error
  2622. r5 = r0.g
  2623. return r5
  2624. def g_b_obj.__get__(__mypyc_self__, instance, owner):
  2625. __mypyc_self__, instance, owner, r0 :: object
  2626. r1 :: bit
  2627. r2 :: object
  2628. L0:
  2629. r0 = load_address _Py_NoneStruct
  2630. r1 = instance == r0
  2631. if r1 goto L1 else goto L2 :: bool
  2632. L1:
  2633. return __mypyc_self__
  2634. L2:
  2635. r2 = PyMethod_New(__mypyc_self__, instance)
  2636. return r2
  2637. def g_b_obj.__call__(__mypyc_self__):
  2638. __mypyc_self__ :: __main__.g_b_obj
  2639. r0 :: __main__.b_env
  2640. r1, g :: object
  2641. r2 :: str
  2642. r3 :: object
  2643. r4 :: str
  2644. r5, r6, r7, r8 :: object
  2645. r9 :: str
  2646. r10 :: object
  2647. r11 :: str
  2648. r12, r13 :: object
  2649. L0:
  2650. r0 = __mypyc_self__.__mypyc_env__
  2651. r1 = r0.g
  2652. g = r1
  2653. r2 = '---'
  2654. r3 = builtins :: module
  2655. r4 = 'print'
  2656. r5 = CPyObject_GetAttr(r3, r4)
  2657. r6 = PyObject_CallFunctionObjArgs(r5, r2, 0)
  2658. r7 = r0.f
  2659. r8 = PyObject_CallFunctionObjArgs(r7, 0)
  2660. r9 = '---'
  2661. r10 = builtins :: module
  2662. r11 = 'print'
  2663. r12 = CPyObject_GetAttr(r10, r11)
  2664. r13 = PyObject_CallFunctionObjArgs(r12, r9, 0)
  2665. return 1
  2666. def b(f):
  2667. f :: object
  2668. r0 :: __main__.b_env
  2669. r1 :: bool
  2670. r2 :: __main__.g_b_obj
  2671. r3, r4 :: bool
  2672. r5 :: object
  2673. L0:
  2674. r0 = b_env()
  2675. r0.f = f; r1 = is_error
  2676. r2 = g_b_obj()
  2677. r2.__mypyc_env__ = r0; r3 = is_error
  2678. r0.g = r2; r4 = is_error
  2679. r5 = r0.g
  2680. return r5
  2681. def d_c_obj.__get__(__mypyc_self__, instance, owner):
  2682. __mypyc_self__, instance, owner, r0 :: object
  2683. r1 :: bit
  2684. r2 :: object
  2685. L0:
  2686. r0 = load_address _Py_NoneStruct
  2687. r1 = instance == r0
  2688. if r1 goto L1 else goto L2 :: bool
  2689. L1:
  2690. return __mypyc_self__
  2691. L2:
  2692. r2 = PyMethod_New(__mypyc_self__, instance)
  2693. return r2
  2694. def d_c_obj.__call__(__mypyc_self__):
  2695. __mypyc_self__ :: __main__.d_c_obj
  2696. r0 :: __main__.c_env
  2697. r1, d :: object
  2698. r2 :: str
  2699. r3 :: object
  2700. r4 :: str
  2701. r5, r6 :: object
  2702. L0:
  2703. r0 = __mypyc_self__.__mypyc_env__
  2704. r1 = r0.d
  2705. d = r1
  2706. r2 = 'd'
  2707. r3 = builtins :: module
  2708. r4 = 'print'
  2709. r5 = CPyObject_GetAttr(r3, r4)
  2710. r6 = PyObject_CallFunctionObjArgs(r5, r2, 0)
  2711. return 1
  2712. def c():
  2713. r0 :: __main__.c_env
  2714. r1 :: __main__.d_c_obj
  2715. r2 :: bool
  2716. r3 :: dict
  2717. r4 :: str
  2718. r5, r6 :: object
  2719. r7 :: dict
  2720. r8 :: str
  2721. r9, r10 :: object
  2722. r11 :: bool
  2723. r12 :: dict
  2724. r13 :: str
  2725. r14 :: i32
  2726. r15 :: bit
  2727. r16 :: str
  2728. r17 :: object
  2729. r18 :: str
  2730. r19, r20, r21, r22 :: object
  2731. L0:
  2732. r0 = c_env()
  2733. r1 = d_c_obj()
  2734. r1.__mypyc_env__ = r0; r2 = is_error
  2735. r3 = __main__.globals :: static
  2736. r4 = 'b'
  2737. r5 = CPyDict_GetItem(r3, r4)
  2738. r6 = PyObject_CallFunctionObjArgs(r5, r1, 0)
  2739. r7 = __main__.globals :: static
  2740. r8 = 'a'
  2741. r9 = CPyDict_GetItem(r7, r8)
  2742. r10 = PyObject_CallFunctionObjArgs(r9, r6, 0)
  2743. r0.d = r10; r11 = is_error
  2744. r12 = __main__.globals :: static
  2745. r13 = 'd'
  2746. r14 = CPyDict_SetItem(r12, r13, r10)
  2747. r15 = r14 >= 0 :: signed
  2748. r16 = 'c'
  2749. r17 = builtins :: module
  2750. r18 = 'print'
  2751. r19 = CPyObject_GetAttr(r17, r18)
  2752. r20 = PyObject_CallFunctionObjArgs(r19, r16, 0)
  2753. r21 = r0.d
  2754. r22 = PyObject_CallFunctionObjArgs(r21, 0)
  2755. return 1
  2756. def __top_level__():
  2757. r0, r1 :: object
  2758. r2 :: bit
  2759. r3 :: str
  2760. r4, r5 :: object
  2761. r6 :: str
  2762. r7 :: dict
  2763. r8 :: object
  2764. r9 :: dict
  2765. r10 :: str
  2766. r11 :: object
  2767. r12 :: dict
  2768. r13 :: str
  2769. r14, r15 :: object
  2770. r16 :: dict
  2771. r17 :: str
  2772. r18, r19 :: object
  2773. r20 :: dict
  2774. r21 :: str
  2775. r22 :: i32
  2776. r23 :: bit
  2777. L0:
  2778. r0 = builtins :: module
  2779. r1 = load_address _Py_NoneStruct
  2780. r2 = r0 != r1
  2781. if r2 goto L2 else goto L1 :: bool
  2782. L1:
  2783. r3 = 'builtins'
  2784. r4 = PyImport_Import(r3)
  2785. builtins = r4 :: module
  2786. L2:
  2787. r5 = ('Callable',)
  2788. r6 = 'typing'
  2789. r7 = __main__.globals :: static
  2790. r8 = CPyImport_ImportFromMany(r6, r5, r5, r7)
  2791. typing = r8 :: module
  2792. r9 = __main__.globals :: static
  2793. r10 = 'c'
  2794. r11 = CPyDict_GetItem(r9, r10)
  2795. r12 = __main__.globals :: static
  2796. r13 = 'b'
  2797. r14 = CPyDict_GetItem(r12, r13)
  2798. r15 = PyObject_CallFunctionObjArgs(r14, r11, 0)
  2799. r16 = __main__.globals :: static
  2800. r17 = 'a'
  2801. r18 = CPyDict_GetItem(r16, r17)
  2802. r19 = PyObject_CallFunctionObjArgs(r18, r15, 0)
  2803. r20 = __main__.globals :: static
  2804. r21 = 'c'
  2805. r22 = CPyDict_SetItem(r20, r21, r19)
  2806. r23 = r22 >= 0 :: signed
  2807. return 1
  2808. [case testDecoratorsSimple_toplevel]
  2809. from typing import Callable
  2810. def a(f: Callable[[], None]) -> Callable[[], None]:
  2811. def g() -> None:
  2812. print('Entering')
  2813. f()
  2814. print('Exited')
  2815. return g
  2816. [out]
  2817. def g_a_obj.__get__(__mypyc_self__, instance, owner):
  2818. __mypyc_self__, instance, owner, r0 :: object
  2819. r1 :: bit
  2820. r2 :: object
  2821. L0:
  2822. r0 = load_address _Py_NoneStruct
  2823. r1 = instance == r0
  2824. if r1 goto L1 else goto L2 :: bool
  2825. L1:
  2826. return __mypyc_self__
  2827. L2:
  2828. r2 = PyMethod_New(__mypyc_self__, instance)
  2829. return r2
  2830. def g_a_obj.__call__(__mypyc_self__):
  2831. __mypyc_self__ :: __main__.g_a_obj
  2832. r0 :: __main__.a_env
  2833. r1, g :: object
  2834. r2 :: str
  2835. r3 :: object
  2836. r4 :: str
  2837. r5, r6, r7, r8 :: object
  2838. r9 :: str
  2839. r10 :: object
  2840. r11 :: str
  2841. r12, r13 :: object
  2842. L0:
  2843. r0 = __mypyc_self__.__mypyc_env__
  2844. r1 = r0.g
  2845. g = r1
  2846. r2 = 'Entering'
  2847. r3 = builtins :: module
  2848. r4 = 'print'
  2849. r5 = CPyObject_GetAttr(r3, r4)
  2850. r6 = PyObject_CallFunctionObjArgs(r5, r2, 0)
  2851. r7 = r0.f
  2852. r8 = PyObject_CallFunctionObjArgs(r7, 0)
  2853. r9 = 'Exited'
  2854. r10 = builtins :: module
  2855. r11 = 'print'
  2856. r12 = CPyObject_GetAttr(r10, r11)
  2857. r13 = PyObject_CallFunctionObjArgs(r12, r9, 0)
  2858. return 1
  2859. def a(f):
  2860. f :: object
  2861. r0 :: __main__.a_env
  2862. r1 :: bool
  2863. r2 :: __main__.g_a_obj
  2864. r3, r4 :: bool
  2865. r5 :: object
  2866. L0:
  2867. r0 = a_env()
  2868. r0.f = f; r1 = is_error
  2869. r2 = g_a_obj()
  2870. r2.__mypyc_env__ = r0; r3 = is_error
  2871. r0.g = r2; r4 = is_error
  2872. r5 = r0.g
  2873. return r5
  2874. def __top_level__():
  2875. r0, r1 :: object
  2876. r2 :: bit
  2877. r3 :: str
  2878. r4, r5 :: object
  2879. r6 :: str
  2880. r7 :: dict
  2881. r8 :: object
  2882. L0:
  2883. r0 = builtins :: module
  2884. r1 = load_address _Py_NoneStruct
  2885. r2 = r0 != r1
  2886. if r2 goto L2 else goto L1 :: bool
  2887. L1:
  2888. r3 = 'builtins'
  2889. r4 = PyImport_Import(r3)
  2890. builtins = r4 :: module
  2891. L2:
  2892. r5 = ('Callable',)
  2893. r6 = 'typing'
  2894. r7 = __main__.globals :: static
  2895. r8 = CPyImport_ImportFromMany(r6, r5, r5, r7)
  2896. typing = r8 :: module
  2897. return 1
  2898. [case testAnyAllG]
  2899. from typing import Iterable
  2900. def call_any(l: Iterable[int]) -> bool:
  2901. return any(i == 0 for i in l)
  2902. def call_all(l: Iterable[int]) -> bool:
  2903. return all(i == 0 for i in l)
  2904. [out]
  2905. def call_any(l):
  2906. l :: object
  2907. r0 :: bool
  2908. r1, r2 :: object
  2909. r3, i :: int
  2910. r4 :: native_int
  2911. r5, r6 :: bit
  2912. r7 :: bool
  2913. r8, r9 :: bit
  2914. L0:
  2915. r0 = 0
  2916. r1 = PyObject_GetIter(l)
  2917. L1:
  2918. r2 = PyIter_Next(r1)
  2919. if is_error(r2) goto L9 else goto L2
  2920. L2:
  2921. r3 = unbox(int, r2)
  2922. i = r3
  2923. r4 = i & 1
  2924. r5 = r4 == 0
  2925. if r5 goto L3 else goto L4 :: bool
  2926. L3:
  2927. r6 = i == 0
  2928. r7 = r6
  2929. goto L5
  2930. L4:
  2931. r8 = CPyTagged_IsEq_(i, 0)
  2932. r7 = r8
  2933. L5:
  2934. if r7 goto L6 else goto L7 :: bool
  2935. L6:
  2936. r0 = 1
  2937. goto L11
  2938. L7:
  2939. L8:
  2940. goto L1
  2941. L9:
  2942. r9 = CPy_NoErrOccured()
  2943. L10:
  2944. L11:
  2945. return r0
  2946. def call_all(l):
  2947. l :: object
  2948. r0 :: bool
  2949. r1, r2 :: object
  2950. r3, i :: int
  2951. r4 :: native_int
  2952. r5, r6 :: bit
  2953. r7 :: bool
  2954. r8 :: bit
  2955. r9 :: bool
  2956. r10 :: bit
  2957. L0:
  2958. r0 = 1
  2959. r1 = PyObject_GetIter(l)
  2960. L1:
  2961. r2 = PyIter_Next(r1)
  2962. if is_error(r2) goto L9 else goto L2
  2963. L2:
  2964. r3 = unbox(int, r2)
  2965. i = r3
  2966. r4 = i & 1
  2967. r5 = r4 == 0
  2968. if r5 goto L3 else goto L4 :: bool
  2969. L3:
  2970. r6 = i == 0
  2971. r7 = r6
  2972. goto L5
  2973. L4:
  2974. r8 = CPyTagged_IsEq_(i, 0)
  2975. r7 = r8
  2976. L5:
  2977. r9 = r7 ^ 1
  2978. if r9 goto L6 else goto L7 :: bool
  2979. L6:
  2980. r0 = 0
  2981. goto L11
  2982. L7:
  2983. L8:
  2984. goto L1
  2985. L9:
  2986. r10 = CPy_NoErrOccured()
  2987. L10:
  2988. L11:
  2989. return r0
  2990. [case testSum]
  2991. from typing import Callable, Iterable
  2992. def call_sum(l: Iterable[int], comparison: Callable[[int], bool]) -> int:
  2993. return sum(comparison(x) for x in l)
  2994. [out]
  2995. def call_sum(l, comparison):
  2996. l, comparison :: object
  2997. r0 :: int
  2998. r1, r2 :: object
  2999. r3, x :: int
  3000. r4, r5 :: object
  3001. r6, r7 :: bool
  3002. r8, r9 :: int
  3003. r10 :: bit
  3004. L0:
  3005. r0 = 0
  3006. r1 = PyObject_GetIter(l)
  3007. L1:
  3008. r2 = PyIter_Next(r1)
  3009. if is_error(r2) goto L4 else goto L2
  3010. L2:
  3011. r3 = unbox(int, r2)
  3012. x = r3
  3013. r4 = box(int, x)
  3014. r5 = PyObject_CallFunctionObjArgs(comparison, r4, 0)
  3015. r6 = unbox(bool, r5)
  3016. r7 = r6 << 1
  3017. r8 = extend r7: builtins.bool to builtins.int
  3018. r9 = CPyTagged_Add(r0, r8)
  3019. r0 = r9
  3020. L3:
  3021. goto L1
  3022. L4:
  3023. r10 = CPy_NoErrOccured()
  3024. L5:
  3025. return r0
  3026. [case testSetAttr1]
  3027. from typing import Any, Dict, List
  3028. def lol(x: Any):
  3029. setattr(x, 'x', '5')
  3030. [out]
  3031. def lol(x):
  3032. x :: object
  3033. r0, r1 :: str
  3034. r2 :: i32
  3035. r3 :: bit
  3036. r4 :: object
  3037. L0:
  3038. r0 = 'x'
  3039. r1 = '5'
  3040. r2 = PyObject_SetAttr(x, r0, r1)
  3041. r3 = r2 >= 0 :: signed
  3042. r4 = box(None, 1)
  3043. return r4
  3044. [case testFinalModuleInt]
  3045. from typing import Final
  3046. x: Final = 1
  3047. y: Final = 2
  3048. def f(a: bool) -> int:
  3049. if a:
  3050. return x
  3051. else:
  3052. return y
  3053. [out]
  3054. def f(a):
  3055. a :: bool
  3056. L0:
  3057. if a goto L1 else goto L2 :: bool
  3058. L1:
  3059. return 2
  3060. L2:
  3061. return 4
  3062. L3:
  3063. unreachable
  3064. [case testFinalModuleStr]
  3065. from typing import Final
  3066. x: Final = 'x'
  3067. y: Final = 'y'
  3068. def f(a: bool) -> str:
  3069. if a:
  3070. return x
  3071. else:
  3072. return y
  3073. [out]
  3074. def f(a):
  3075. a :: bool
  3076. r0, r1 :: str
  3077. L0:
  3078. if a goto L1 else goto L2 :: bool
  3079. L1:
  3080. r0 = 'x'
  3081. return r0
  3082. L2:
  3083. r1 = 'y'
  3084. return r1
  3085. L3:
  3086. unreachable
  3087. [case testFinalModuleBool]
  3088. from typing import Final
  3089. x: Final = True
  3090. y: Final = False
  3091. def f(a: bool) -> bool:
  3092. if a:
  3093. return x
  3094. else:
  3095. return y
  3096. [out]
  3097. def f(a):
  3098. a :: bool
  3099. L0:
  3100. if a goto L1 else goto L2 :: bool
  3101. L1:
  3102. return 1
  3103. L2:
  3104. return 0
  3105. L3:
  3106. unreachable
  3107. [case testFinalClass]
  3108. from typing import Final
  3109. class C:
  3110. x: Final = 1
  3111. y: Final = 2
  3112. def f(a: bool) -> int:
  3113. if a:
  3114. return C.x
  3115. else:
  3116. return C.y
  3117. [out]
  3118. def C.__mypyc_defaults_setup(__mypyc_self__):
  3119. __mypyc_self__ :: __main__.C
  3120. L0:
  3121. __mypyc_self__.x = 2
  3122. __mypyc_self__.y = 4
  3123. return 1
  3124. def f(a):
  3125. a :: bool
  3126. L0:
  3127. if a goto L1 else goto L2 :: bool
  3128. L1:
  3129. return 2
  3130. L2:
  3131. return 4
  3132. L3:
  3133. unreachable
  3134. [case testFinalStaticList]
  3135. from typing import Final
  3136. x: Final = [1]
  3137. def f() -> int:
  3138. return x[0]
  3139. [out]
  3140. def f():
  3141. r0 :: list
  3142. r1 :: bool
  3143. r2 :: object
  3144. r3 :: int
  3145. L0:
  3146. r0 = __main__.x :: static
  3147. if is_error(r0) goto L1 else goto L2
  3148. L1:
  3149. r1 = raise NameError('value for final name "x" was not set')
  3150. unreachable
  3151. L2:
  3152. r2 = CPyList_GetItemShort(r0, 0)
  3153. r3 = unbox(int, r2)
  3154. return r3
  3155. [case testFinalStaticTuple]
  3156. from typing import Final
  3157. x: Final = (1, 2)
  3158. def f() -> int:
  3159. return x[0]
  3160. [out]
  3161. def f():
  3162. r0 :: tuple[int, int]
  3163. r1 :: bool
  3164. r2 :: int
  3165. L0:
  3166. r0 = __main__.x :: static
  3167. if is_error(r0) goto L1 else goto L2
  3168. L1:
  3169. r1 = raise NameError('value for final name "x" was not set')
  3170. unreachable
  3171. L2:
  3172. r2 = r0[0]
  3173. return r2
  3174. [case testFinalStaticInt]
  3175. from typing import Final
  3176. x: Final = 1 + int()
  3177. def f() -> int:
  3178. return x - 1
  3179. [out]
  3180. def f():
  3181. r0 :: int
  3182. r1 :: bool
  3183. r2 :: int
  3184. L0:
  3185. r0 = __main__.x :: static
  3186. if is_error(r0) goto L1 else goto L2
  3187. L1:
  3188. r1 = raise NameError('value for final name "x" was not set')
  3189. unreachable
  3190. L2:
  3191. r2 = CPyTagged_Subtract(r0, 2)
  3192. return r2
  3193. [case testFinalRestrictedTypeVar]
  3194. from typing import TypeVar
  3195. if False:
  3196. from typing import Final
  3197. FOO = 10 # type: Final
  3198. Targ = TypeVar('Targ', int, str)
  3199. def foo(z: Targ) -> None:
  3200. FOO
  3201. [out]
  3202. def foo(z):
  3203. z :: object
  3204. L0:
  3205. return 1
  3206. [case testDirectlyCall__bool__]
  3207. class A:
  3208. def __bool__(self) -> bool:
  3209. return True
  3210. class B(A):
  3211. def __bool__(self) -> bool:
  3212. return False
  3213. def lol(x: A) -> int:
  3214. if x:
  3215. return 1
  3216. else:
  3217. return 0
  3218. [out]
  3219. def A.__bool__(self):
  3220. self :: __main__.A
  3221. L0:
  3222. return 1
  3223. def B.__bool__(self):
  3224. self :: __main__.B
  3225. L0:
  3226. return 0
  3227. def lol(x):
  3228. x :: __main__.A
  3229. r0 :: bool
  3230. L0:
  3231. r0 = x.__bool__()
  3232. if r0 goto L1 else goto L2 :: bool
  3233. L1:
  3234. return 2
  3235. L2:
  3236. return 0
  3237. L3:
  3238. unreachable
  3239. [case testRevealType]
  3240. def f(x: int) -> None:
  3241. reveal_type(x) # type: ignore
  3242. [out]
  3243. def f(x):
  3244. x :: int
  3245. r0 :: object
  3246. r1 :: str
  3247. r2, r3, r4 :: object
  3248. L0:
  3249. r0 = builtins :: module
  3250. r1 = 'reveal_type'
  3251. r2 = CPyObject_GetAttr(r0, r1)
  3252. r3 = box(int, x)
  3253. r4 = PyObject_CallFunctionObjArgs(r2, r3, 0)
  3254. return 1
  3255. [case testCallCWithStrJoinMethod]
  3256. from typing import List
  3257. def f(x: str, y: List[str]) -> str:
  3258. return x.join(y)
  3259. [out]
  3260. def f(x, y):
  3261. x :: str
  3262. y :: list
  3263. r0 :: str
  3264. L0:
  3265. r0 = PyUnicode_Join(x, y)
  3266. return r0
  3267. [case testCallCWithToListFunction]
  3268. from typing import List, Iterable, Tuple, Dict
  3269. # generic object
  3270. def f(x: Iterable[int]) -> List[int]:
  3271. return list(x)
  3272. # need coercing
  3273. def g(x: Tuple[int, int, int]) -> List[int]:
  3274. return list(x)
  3275. # non-list object
  3276. def h(x: Dict[int, str]) -> List[int]:
  3277. return list(x)
  3278. [out]
  3279. def f(x):
  3280. x :: object
  3281. r0 :: list
  3282. L0:
  3283. r0 = PySequence_List(x)
  3284. return r0
  3285. def g(x):
  3286. x :: tuple[int, int, int]
  3287. r0 :: object
  3288. r1 :: list
  3289. L0:
  3290. r0 = box(tuple[int, int, int], x)
  3291. r1 = PySequence_List(r0)
  3292. return r1
  3293. def h(x):
  3294. x :: dict
  3295. r0 :: list
  3296. L0:
  3297. r0 = PySequence_List(x)
  3298. return r0
  3299. [case testBoolFunction]
  3300. def f(x: object) -> bool:
  3301. return bool(x)
  3302. [out]
  3303. def f(x):
  3304. x :: object
  3305. r0 :: i32
  3306. r1 :: bit
  3307. r2 :: bool
  3308. L0:
  3309. r0 = PyObject_IsTrue(x)
  3310. r1 = r0 >= 0 :: signed
  3311. r2 = truncate r0: i32 to builtins.bool
  3312. return r2
  3313. [case testLocalImports]
  3314. def root() -> None:
  3315. import dataclasses
  3316. import enum
  3317. def submodule() -> int:
  3318. import p.m
  3319. return p.x
  3320. [file p/__init__.py]
  3321. x = 1
  3322. [file p/m.py]
  3323. [out]
  3324. def root():
  3325. r0 :: dict
  3326. r1, r2 :: object
  3327. r3 :: bit
  3328. r4 :: str
  3329. r5 :: object
  3330. r6 :: str
  3331. r7 :: dict
  3332. r8 :: str
  3333. r9 :: object
  3334. r10 :: i32
  3335. r11 :: bit
  3336. r12 :: dict
  3337. r13, r14 :: object
  3338. r15 :: bit
  3339. r16 :: str
  3340. r17 :: object
  3341. r18 :: str
  3342. r19 :: dict
  3343. r20 :: str
  3344. r21 :: object
  3345. r22 :: i32
  3346. r23 :: bit
  3347. L0:
  3348. r0 = __main__.globals :: static
  3349. r1 = dataclasses :: module
  3350. r2 = load_address _Py_NoneStruct
  3351. r3 = r1 != r2
  3352. if r3 goto L2 else goto L1 :: bool
  3353. L1:
  3354. r4 = 'dataclasses'
  3355. r5 = PyImport_Import(r4)
  3356. dataclasses = r5 :: module
  3357. L2:
  3358. r6 = 'dataclasses'
  3359. r7 = PyImport_GetModuleDict()
  3360. r8 = 'dataclasses'
  3361. r9 = CPyDict_GetItem(r7, r8)
  3362. r10 = CPyDict_SetItem(r0, r6, r9)
  3363. r11 = r10 >= 0 :: signed
  3364. r12 = __main__.globals :: static
  3365. r13 = enum :: module
  3366. r14 = load_address _Py_NoneStruct
  3367. r15 = r13 != r14
  3368. if r15 goto L4 else goto L3 :: bool
  3369. L3:
  3370. r16 = 'enum'
  3371. r17 = PyImport_Import(r16)
  3372. enum = r17 :: module
  3373. L4:
  3374. r18 = 'enum'
  3375. r19 = PyImport_GetModuleDict()
  3376. r20 = 'enum'
  3377. r21 = CPyDict_GetItem(r19, r20)
  3378. r22 = CPyDict_SetItem(r12, r18, r21)
  3379. r23 = r22 >= 0 :: signed
  3380. return 1
  3381. def submodule():
  3382. r0 :: dict
  3383. r1, r2 :: object
  3384. r3 :: bit
  3385. r4 :: str
  3386. r5 :: object
  3387. r6 :: str
  3388. r7 :: dict
  3389. r8 :: str
  3390. r9 :: object
  3391. r10 :: i32
  3392. r11 :: bit
  3393. r12 :: dict
  3394. r13 :: str
  3395. r14 :: object
  3396. r15 :: str
  3397. r16 :: object
  3398. r17 :: int
  3399. L0:
  3400. r0 = __main__.globals :: static
  3401. r1 = p.m :: module
  3402. r2 = load_address _Py_NoneStruct
  3403. r3 = r1 != r2
  3404. if r3 goto L2 else goto L1 :: bool
  3405. L1:
  3406. r4 = 'p.m'
  3407. r5 = PyImport_Import(r4)
  3408. p.m = r5 :: module
  3409. L2:
  3410. r6 = 'p'
  3411. r7 = PyImport_GetModuleDict()
  3412. r8 = 'p'
  3413. r9 = CPyDict_GetItem(r7, r8)
  3414. r10 = CPyDict_SetItem(r0, r6, r9)
  3415. r11 = r10 >= 0 :: signed
  3416. r12 = PyImport_GetModuleDict()
  3417. r13 = 'p'
  3418. r14 = CPyDict_GetItem(r12, r13)
  3419. r15 = 'x'
  3420. r16 = CPyObject_GetAttr(r14, r15)
  3421. r17 = unbox(int, r16)
  3422. return r17
  3423. [case testIsinstanceBool]
  3424. def f(x: object) -> bool:
  3425. return isinstance(x, bool)
  3426. [out]
  3427. def f(x):
  3428. x, r0 :: object
  3429. r1 :: i32
  3430. r2 :: bit
  3431. r3 :: bool
  3432. L0:
  3433. r0 = load_address PyBool_Type
  3434. r1 = PyObject_IsInstance(x, r0)
  3435. r2 = r1 >= 0 :: signed
  3436. r3 = truncate r1: i32 to builtins.bool
  3437. return r3
  3438. [case testRangeObject]
  3439. def range_object() -> None:
  3440. r = range(4, 12, 2)
  3441. sum = 0
  3442. for i in r:
  3443. sum += i
  3444. def range_in_loop() -> None:
  3445. sum = 0
  3446. for i in range(4, 12, 2):
  3447. sum += i
  3448. [out]
  3449. def range_object():
  3450. r0, r1, r2, r3, r4 :: object
  3451. r5, r :: range
  3452. sum :: int
  3453. r6, r7 :: object
  3454. r8, i, r9 :: int
  3455. r10 :: bit
  3456. L0:
  3457. r0 = load_address PyRange_Type
  3458. r1 = object 4
  3459. r2 = object 12
  3460. r3 = object 2
  3461. r4 = PyObject_CallFunctionObjArgs(r0, r1, r2, r3, 0)
  3462. r5 = cast(range, r4)
  3463. r = r5
  3464. sum = 0
  3465. r6 = PyObject_GetIter(r)
  3466. L1:
  3467. r7 = PyIter_Next(r6)
  3468. if is_error(r7) goto L4 else goto L2
  3469. L2:
  3470. r8 = unbox(int, r7)
  3471. i = r8
  3472. r9 = CPyTagged_Add(sum, i)
  3473. sum = r9
  3474. L3:
  3475. goto L1
  3476. L4:
  3477. r10 = CPy_NoErrOccured()
  3478. L5:
  3479. return 1
  3480. def range_in_loop():
  3481. sum :: int
  3482. r0 :: short_int
  3483. i :: int
  3484. r1 :: bit
  3485. r2 :: int
  3486. r3 :: short_int
  3487. L0:
  3488. sum = 0
  3489. r0 = 8
  3490. i = r0
  3491. L1:
  3492. r1 = r0 < 24 :: signed
  3493. if r1 goto L2 else goto L4 :: bool
  3494. L2:
  3495. r2 = CPyTagged_Add(sum, i)
  3496. sum = r2
  3497. L3:
  3498. r3 = r0 + 4
  3499. r0 = r3
  3500. i = r3
  3501. goto L1
  3502. L4:
  3503. return 1
  3504. [case testLocalRedefinition]
  3505. # mypy: allow-redefinition
  3506. def f() -> None:
  3507. i = 0
  3508. i += 1
  3509. i = "foo"
  3510. i += i
  3511. i = 0.0
  3512. [out]
  3513. def f():
  3514. i, r0 :: int
  3515. r1, i__redef__, r2 :: str
  3516. i__redef____redef__ :: float
  3517. L0:
  3518. i = 0
  3519. r0 = CPyTagged_Add(i, 2)
  3520. i = r0
  3521. r1 = 'foo'
  3522. i__redef__ = r1
  3523. r2 = CPyStr_Append(i__redef__, i__redef__)
  3524. i__redef__ = r2
  3525. i__redef____redef__ = 0.0
  3526. return 1
  3527. [case testNewType]
  3528. from typing import NewType
  3529. class A: pass
  3530. N = NewType("N", A)
  3531. def f(arg: A) -> N:
  3532. return N(arg)
  3533. [out]
  3534. def f(arg):
  3535. arg :: __main__.A
  3536. L0:
  3537. return arg