frameobject.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  1. /* Frame object implementation */
  2. #include "Python.h"
  3. #include "pycore_ceval.h" // _PyEval_BuiltinsFromGlobals()
  4. #include "pycore_code.h" // CO_FAST_LOCAL, etc.
  5. #include "pycore_function.h" // _PyFunction_FromConstructor()
  6. #include "pycore_moduleobject.h" // _PyModule_GetDict()
  7. #include "pycore_object.h" // _PyObject_GC_UNTRACK()
  8. #include "pycore_opcode.h" // _PyOpcode_Caches
  9. #include "frameobject.h" // PyFrameObject
  10. #include "pycore_frame.h"
  11. #include "opcode.h" // EXTENDED_ARG
  12. #include "structmember.h" // PyMemberDef
  13. #define OFF(x) offsetof(PyFrameObject, x)
  14. static PyMemberDef frame_memberlist[] = {
  15. {"f_trace_lines", T_BOOL, OFF(f_trace_lines), 0},
  16. {NULL} /* Sentinel */
  17. };
  18. static PyObject *
  19. frame_getlocals(PyFrameObject *f, void *closure)
  20. {
  21. if (f == NULL) {
  22. PyErr_BadInternalCall();
  23. return NULL;
  24. }
  25. assert(!_PyFrame_IsIncomplete(f->f_frame));
  26. PyObject *locals = _PyFrame_GetLocals(f->f_frame, 1);
  27. if (locals) {
  28. f->f_fast_as_locals = 1;
  29. }
  30. return locals;
  31. }
  32. int
  33. PyFrame_GetLineNumber(PyFrameObject *f)
  34. {
  35. assert(f != NULL);
  36. if (f->f_lineno != 0) {
  37. return f->f_lineno;
  38. }
  39. else {
  40. return PyUnstable_InterpreterFrame_GetLine(f->f_frame);
  41. }
  42. }
  43. static PyObject *
  44. frame_getlineno(PyFrameObject *f, void *closure)
  45. {
  46. int lineno = PyFrame_GetLineNumber(f);
  47. if (lineno < 0) {
  48. Py_RETURN_NONE;
  49. }
  50. else {
  51. return PyLong_FromLong(lineno);
  52. }
  53. }
  54. static PyObject *
  55. frame_getlasti(PyFrameObject *f, void *closure)
  56. {
  57. int lasti = _PyInterpreterFrame_LASTI(f->f_frame);
  58. if (lasti < 0) {
  59. return PyLong_FromLong(-1);
  60. }
  61. return PyLong_FromLong(lasti * sizeof(_Py_CODEUNIT));
  62. }
  63. static PyObject *
  64. frame_getglobals(PyFrameObject *f, void *closure)
  65. {
  66. PyObject *globals = f->f_frame->f_globals;
  67. if (globals == NULL) {
  68. globals = Py_None;
  69. }
  70. return Py_NewRef(globals);
  71. }
  72. static PyObject *
  73. frame_getbuiltins(PyFrameObject *f, void *closure)
  74. {
  75. PyObject *builtins = f->f_frame->f_builtins;
  76. if (builtins == NULL) {
  77. builtins = Py_None;
  78. }
  79. return Py_NewRef(builtins);
  80. }
  81. static PyObject *
  82. frame_getcode(PyFrameObject *f, void *closure)
  83. {
  84. if (PySys_Audit("object.__getattr__", "Os", f, "f_code") < 0) {
  85. return NULL;
  86. }
  87. return (PyObject *)PyFrame_GetCode(f);
  88. }
  89. static PyObject *
  90. frame_getback(PyFrameObject *f, void *closure)
  91. {
  92. PyObject *res = (PyObject *)PyFrame_GetBack(f);
  93. if (res == NULL) {
  94. Py_RETURN_NONE;
  95. }
  96. return res;
  97. }
  98. static PyObject *
  99. frame_gettrace_opcodes(PyFrameObject *f, void *closure)
  100. {
  101. PyObject *result = f->f_trace_opcodes ? Py_True : Py_False;
  102. return Py_NewRef(result);
  103. }
  104. static int
  105. frame_settrace_opcodes(PyFrameObject *f, PyObject* value, void *Py_UNUSED(ignored))
  106. {
  107. if (!PyBool_Check(value)) {
  108. PyErr_SetString(PyExc_TypeError,
  109. "attribute value type must be bool");
  110. return -1;
  111. }
  112. if (value == Py_True) {
  113. f->f_trace_opcodes = 1;
  114. _PyInterpreterState_GET()->f_opcode_trace_set = true;
  115. }
  116. else {
  117. f->f_trace_opcodes = 0;
  118. }
  119. return 0;
  120. }
  121. /* Model the evaluation stack, to determine which jumps
  122. * are safe and how many values needs to be popped.
  123. * The stack is modelled by a 64 integer, treating any
  124. * stack that can't fit into 64 bits as "overflowed".
  125. */
  126. typedef enum kind {
  127. Iterator = 1,
  128. Except = 2,
  129. Object = 3,
  130. Null = 4,
  131. Lasti = 5,
  132. } Kind;
  133. static int
  134. compatible_kind(Kind from, Kind to) {
  135. if (to == 0) {
  136. return 0;
  137. }
  138. if (to == Object) {
  139. return from != Null;
  140. }
  141. if (to == Null) {
  142. return 1;
  143. }
  144. return from == to;
  145. }
  146. #define BITS_PER_BLOCK 3
  147. #define UNINITIALIZED -2
  148. #define OVERFLOWED -1
  149. #define MAX_STACK_ENTRIES (63/BITS_PER_BLOCK)
  150. #define WILL_OVERFLOW (1ULL<<((MAX_STACK_ENTRIES-1)*BITS_PER_BLOCK))
  151. #define EMPTY_STACK 0
  152. static inline int64_t
  153. push_value(int64_t stack, Kind kind)
  154. {
  155. if (((uint64_t)stack) >= WILL_OVERFLOW) {
  156. return OVERFLOWED;
  157. }
  158. else {
  159. return (stack << BITS_PER_BLOCK) | kind;
  160. }
  161. }
  162. static inline int64_t
  163. pop_value(int64_t stack)
  164. {
  165. return Py_ARITHMETIC_RIGHT_SHIFT(int64_t, stack, BITS_PER_BLOCK);
  166. }
  167. #define MASK ((1<<BITS_PER_BLOCK)-1)
  168. static inline Kind
  169. top_of_stack(int64_t stack)
  170. {
  171. return stack & MASK;
  172. }
  173. static inline Kind
  174. peek(int64_t stack, int n)
  175. {
  176. assert(n >= 1);
  177. return (stack>>(BITS_PER_BLOCK*(n-1))) & MASK;
  178. }
  179. static Kind
  180. stack_swap(int64_t stack, int n)
  181. {
  182. assert(n >= 1);
  183. Kind to_swap = peek(stack, n);
  184. Kind top = top_of_stack(stack);
  185. int shift = BITS_PER_BLOCK*(n-1);
  186. int64_t replaced_low = (stack & ~(MASK << shift)) | (top << shift);
  187. int64_t replaced_top = (replaced_low & ~MASK) | to_swap;
  188. return replaced_top;
  189. }
  190. static int64_t
  191. pop_to_level(int64_t stack, int level) {
  192. if (level == 0) {
  193. return EMPTY_STACK;
  194. }
  195. int64_t max_item = (1<<BITS_PER_BLOCK) - 1;
  196. int64_t level_max_stack = max_item << ((level-1) * BITS_PER_BLOCK);
  197. while (stack > level_max_stack) {
  198. stack = pop_value(stack);
  199. }
  200. return stack;
  201. }
  202. #if 0
  203. /* These functions are useful for debugging the stack marking code */
  204. static char
  205. tos_char(int64_t stack) {
  206. switch(top_of_stack(stack)) {
  207. case Iterator:
  208. return 'I';
  209. case Except:
  210. return 'E';
  211. case Object:
  212. return 'O';
  213. case Lasti:
  214. return 'L';
  215. case Null:
  216. return 'N';
  217. }
  218. return '?';
  219. }
  220. static void
  221. print_stack(int64_t stack) {
  222. if (stack < 0) {
  223. if (stack == UNINITIALIZED) {
  224. printf("---");
  225. }
  226. else if (stack == OVERFLOWED) {
  227. printf("OVERFLOWED");
  228. }
  229. else {
  230. printf("??");
  231. }
  232. return;
  233. }
  234. while (stack) {
  235. printf("%c", tos_char(stack));
  236. stack = pop_value(stack);
  237. }
  238. }
  239. static void
  240. print_stacks(int64_t *stacks, int n) {
  241. for (int i = 0; i < n; i++) {
  242. printf("%d: ", i);
  243. print_stack(stacks[i]);
  244. printf("\n");
  245. }
  246. }
  247. #endif
  248. static int64_t *
  249. mark_stacks(PyCodeObject *code_obj, int len)
  250. {
  251. PyObject *co_code = _PyCode_GetCode(code_obj);
  252. if (co_code == NULL) {
  253. return NULL;
  254. }
  255. _Py_CODEUNIT *code = (_Py_CODEUNIT *)PyBytes_AS_STRING(co_code);
  256. int64_t *stacks = PyMem_New(int64_t, len+1);
  257. int i, j, opcode;
  258. if (stacks == NULL) {
  259. PyErr_NoMemory();
  260. Py_DECREF(co_code);
  261. return NULL;
  262. }
  263. for (int i = 1; i <= len; i++) {
  264. stacks[i] = UNINITIALIZED;
  265. }
  266. stacks[0] = EMPTY_STACK;
  267. if (code_obj->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR))
  268. {
  269. // Generators get sent None while starting:
  270. stacks[0] = push_value(stacks[0], Object);
  271. }
  272. int todo = 1;
  273. while (todo) {
  274. todo = 0;
  275. /* Scan instructions */
  276. for (i = 0; i < len;) {
  277. int64_t next_stack = stacks[i];
  278. opcode = _Py_GetBaseOpcode(code_obj, i);
  279. int oparg = 0;
  280. while (opcode == EXTENDED_ARG) {
  281. oparg = (oparg << 8) | code[i].op.arg;
  282. i++;
  283. opcode = _Py_GetBaseOpcode(code_obj, i);
  284. stacks[i] = next_stack;
  285. }
  286. int next_i = i + _PyOpcode_Caches[opcode] + 1;
  287. if (next_stack == UNINITIALIZED) {
  288. i = next_i;
  289. continue;
  290. }
  291. oparg = (oparg << 8) | code[i].op.arg;
  292. switch (opcode) {
  293. case POP_JUMP_IF_FALSE:
  294. case POP_JUMP_IF_TRUE:
  295. case POP_JUMP_IF_NONE:
  296. case POP_JUMP_IF_NOT_NONE:
  297. {
  298. int64_t target_stack;
  299. int j = next_i + oparg;
  300. assert(j < len);
  301. next_stack = pop_value(next_stack);
  302. target_stack = next_stack;
  303. assert(stacks[j] == UNINITIALIZED || stacks[j] == target_stack);
  304. stacks[j] = target_stack;
  305. stacks[next_i] = next_stack;
  306. break;
  307. }
  308. case SEND:
  309. j = oparg + i + INLINE_CACHE_ENTRIES_SEND + 1;
  310. assert(j < len);
  311. assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
  312. stacks[j] = next_stack;
  313. stacks[next_i] = next_stack;
  314. break;
  315. case JUMP_FORWARD:
  316. j = oparg + i + 1;
  317. assert(j < len);
  318. assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
  319. stacks[j] = next_stack;
  320. break;
  321. case JUMP_BACKWARD:
  322. case JUMP_BACKWARD_NO_INTERRUPT:
  323. j = i + 1 - oparg;
  324. assert(j >= 0);
  325. assert(j < len);
  326. if (stacks[j] == UNINITIALIZED && j < i) {
  327. todo = 1;
  328. }
  329. assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
  330. stacks[j] = next_stack;
  331. break;
  332. case GET_ITER:
  333. case GET_AITER:
  334. next_stack = push_value(pop_value(next_stack), Iterator);
  335. stacks[next_i] = next_stack;
  336. break;
  337. case FOR_ITER:
  338. {
  339. int64_t target_stack = push_value(next_stack, Object);
  340. stacks[next_i] = target_stack;
  341. j = oparg + 1 + INLINE_CACHE_ENTRIES_FOR_ITER + i;
  342. assert(j < len);
  343. assert(stacks[j] == UNINITIALIZED || stacks[j] == target_stack);
  344. stacks[j] = target_stack;
  345. break;
  346. }
  347. case END_ASYNC_FOR:
  348. next_stack = pop_value(pop_value(next_stack));
  349. stacks[next_i] = next_stack;
  350. break;
  351. case PUSH_EXC_INFO:
  352. next_stack = push_value(next_stack, Except);
  353. stacks[next_i] = next_stack;
  354. break;
  355. case POP_EXCEPT:
  356. assert(top_of_stack(next_stack) == Except);
  357. next_stack = pop_value(next_stack);
  358. stacks[next_i] = next_stack;
  359. break;
  360. case RETURN_VALUE:
  361. assert(pop_value(next_stack) == EMPTY_STACK);
  362. assert(top_of_stack(next_stack) == Object);
  363. break;
  364. case RETURN_CONST:
  365. break;
  366. case RAISE_VARARGS:
  367. break;
  368. case RERAISE:
  369. assert(top_of_stack(next_stack) == Except);
  370. /* End of block */
  371. break;
  372. case PUSH_NULL:
  373. next_stack = push_value(next_stack, Null);
  374. stacks[next_i] = next_stack;
  375. break;
  376. case LOAD_GLOBAL:
  377. {
  378. int j = oparg;
  379. if (j & 1) {
  380. next_stack = push_value(next_stack, Null);
  381. }
  382. next_stack = push_value(next_stack, Object);
  383. stacks[next_i] = next_stack;
  384. break;
  385. }
  386. case LOAD_ATTR:
  387. {
  388. assert(top_of_stack(next_stack) == Object);
  389. int j = oparg;
  390. if (j & 1) {
  391. next_stack = pop_value(next_stack);
  392. next_stack = push_value(next_stack, Null);
  393. next_stack = push_value(next_stack, Object);
  394. }
  395. stacks[next_i] = next_stack;
  396. break;
  397. }
  398. case CALL:
  399. {
  400. int args = oparg;
  401. for (int j = 0; j < args+2; j++) {
  402. next_stack = pop_value(next_stack);
  403. }
  404. next_stack = push_value(next_stack, Object);
  405. stacks[next_i] = next_stack;
  406. break;
  407. }
  408. case SWAP:
  409. {
  410. int n = oparg;
  411. next_stack = stack_swap(next_stack, n);
  412. stacks[next_i] = next_stack;
  413. break;
  414. }
  415. case COPY:
  416. {
  417. int n = oparg;
  418. next_stack = push_value(next_stack, peek(next_stack, n));
  419. stacks[next_i] = next_stack;
  420. break;
  421. }
  422. case CACHE:
  423. case RESERVED:
  424. {
  425. assert(0);
  426. }
  427. default:
  428. {
  429. int delta = PyCompile_OpcodeStackEffect(opcode, oparg);
  430. assert(delta != PY_INVALID_STACK_EFFECT);
  431. while (delta < 0) {
  432. next_stack = pop_value(next_stack);
  433. delta++;
  434. }
  435. while (delta > 0) {
  436. next_stack = push_value(next_stack, Object);
  437. delta--;
  438. }
  439. stacks[next_i] = next_stack;
  440. }
  441. }
  442. i = next_i;
  443. }
  444. /* Scan exception table */
  445. unsigned char *start = (unsigned char *)PyBytes_AS_STRING(code_obj->co_exceptiontable);
  446. unsigned char *end = start + PyBytes_GET_SIZE(code_obj->co_exceptiontable);
  447. unsigned char *scan = start;
  448. while (scan < end) {
  449. int start_offset, size, handler;
  450. scan = parse_varint(scan, &start_offset);
  451. assert(start_offset >= 0 && start_offset < len);
  452. scan = parse_varint(scan, &size);
  453. assert(size >= 0 && start_offset+size <= len);
  454. scan = parse_varint(scan, &handler);
  455. assert(handler >= 0 && handler < len);
  456. int depth_and_lasti;
  457. scan = parse_varint(scan, &depth_and_lasti);
  458. int level = depth_and_lasti >> 1;
  459. int lasti = depth_and_lasti & 1;
  460. if (stacks[start_offset] != UNINITIALIZED) {
  461. if (stacks[handler] == UNINITIALIZED) {
  462. todo = 1;
  463. uint64_t target_stack = pop_to_level(stacks[start_offset], level);
  464. if (lasti) {
  465. target_stack = push_value(target_stack, Lasti);
  466. }
  467. target_stack = push_value(target_stack, Except);
  468. stacks[handler] = target_stack;
  469. }
  470. }
  471. }
  472. }
  473. Py_DECREF(co_code);
  474. return stacks;
  475. }
  476. static int
  477. compatible_stack(int64_t from_stack, int64_t to_stack)
  478. {
  479. if (from_stack < 0 || to_stack < 0) {
  480. return 0;
  481. }
  482. while(from_stack > to_stack) {
  483. from_stack = pop_value(from_stack);
  484. }
  485. while(from_stack) {
  486. Kind from_top = top_of_stack(from_stack);
  487. Kind to_top = top_of_stack(to_stack);
  488. if (!compatible_kind(from_top, to_top)) {
  489. return 0;
  490. }
  491. from_stack = pop_value(from_stack);
  492. to_stack = pop_value(to_stack);
  493. }
  494. return to_stack == 0;
  495. }
  496. static const char *
  497. explain_incompatible_stack(int64_t to_stack)
  498. {
  499. assert(to_stack != 0);
  500. if (to_stack == OVERFLOWED) {
  501. return "stack is too deep to analyze";
  502. }
  503. if (to_stack == UNINITIALIZED) {
  504. return "can't jump into an exception handler, or code may be unreachable";
  505. }
  506. Kind target_kind = top_of_stack(to_stack);
  507. switch(target_kind) {
  508. case Except:
  509. return "can't jump into an 'except' block as there's no exception";
  510. case Lasti:
  511. return "can't jump into a re-raising block as there's no location";
  512. case Object:
  513. case Null:
  514. return "incompatible stacks";
  515. case Iterator:
  516. return "can't jump into the body of a for loop";
  517. default:
  518. Py_UNREACHABLE();
  519. }
  520. }
  521. static int *
  522. marklines(PyCodeObject *code, int len)
  523. {
  524. PyCodeAddressRange bounds;
  525. _PyCode_InitAddressRange(code, &bounds);
  526. assert (bounds.ar_end == 0);
  527. int last_line = -1;
  528. int *linestarts = PyMem_New(int, len);
  529. if (linestarts == NULL) {
  530. return NULL;
  531. }
  532. for (int i = 0; i < len; i++) {
  533. linestarts[i] = -1;
  534. }
  535. while (_PyLineTable_NextAddressRange(&bounds)) {
  536. assert(bounds.ar_start / (int)sizeof(_Py_CODEUNIT) < len);
  537. if (bounds.ar_line != last_line && bounds.ar_line != -1) {
  538. linestarts[bounds.ar_start / sizeof(_Py_CODEUNIT)] = bounds.ar_line;
  539. last_line = bounds.ar_line;
  540. }
  541. }
  542. return linestarts;
  543. }
  544. static int
  545. first_line_not_before(int *lines, int len, int line)
  546. {
  547. int result = INT_MAX;
  548. for (int i = 0; i < len; i++) {
  549. if (lines[i] < result && lines[i] >= line) {
  550. result = lines[i];
  551. }
  552. }
  553. if (result == INT_MAX) {
  554. return -1;
  555. }
  556. return result;
  557. }
  558. static PyFrameState
  559. _PyFrame_GetState(PyFrameObject *frame)
  560. {
  561. assert(!_PyFrame_IsIncomplete(frame->f_frame));
  562. if (frame->f_frame->stacktop == 0) {
  563. return FRAME_CLEARED;
  564. }
  565. switch(frame->f_frame->owner) {
  566. case FRAME_OWNED_BY_GENERATOR:
  567. {
  568. PyGenObject *gen = _PyFrame_GetGenerator(frame->f_frame);
  569. return gen->gi_frame_state;
  570. }
  571. case FRAME_OWNED_BY_THREAD:
  572. {
  573. if (_PyInterpreterFrame_LASTI(frame->f_frame) < 0) {
  574. return FRAME_CREATED;
  575. }
  576. switch (frame->f_frame->prev_instr->op.code)
  577. {
  578. case COPY_FREE_VARS:
  579. case MAKE_CELL:
  580. case RETURN_GENERATOR:
  581. /* Frame not fully initialized */
  582. return FRAME_CREATED;
  583. default:
  584. return FRAME_EXECUTING;
  585. }
  586. }
  587. case FRAME_OWNED_BY_FRAME_OBJECT:
  588. return FRAME_COMPLETED;
  589. }
  590. Py_UNREACHABLE();
  591. }
  592. /* Setter for f_lineno - you can set f_lineno from within a trace function in
  593. * order to jump to a given line of code, subject to some restrictions. Most
  594. * lines are OK to jump to because they don't make any assumptions about the
  595. * state of the stack (obvious because you could remove the line and the code
  596. * would still work without any stack errors), but there are some constructs
  597. * that limit jumping:
  598. *
  599. * o Any exception handlers.
  600. * o 'for' and 'async for' loops can't be jumped into because the
  601. * iterator needs to be on the stack.
  602. * o Jumps cannot be made from within a trace function invoked with a
  603. * 'return' or 'exception' event since the eval loop has been exited at
  604. * that time.
  605. */
  606. static int
  607. frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignored))
  608. {
  609. if (p_new_lineno == NULL) {
  610. PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
  611. return -1;
  612. }
  613. /* f_lineno must be an integer. */
  614. if (!PyLong_CheckExact(p_new_lineno)) {
  615. PyErr_SetString(PyExc_ValueError,
  616. "lineno must be an integer");
  617. return -1;
  618. }
  619. PyFrameState state = _PyFrame_GetState(f);
  620. /*
  621. * This code preserves the historical restrictions on
  622. * setting the line number of a frame.
  623. * Jumps are forbidden on a 'return' trace event (except after a yield).
  624. * Jumps from 'call' trace events are also forbidden.
  625. * In addition, jumps are forbidden when not tracing,
  626. * as this is a debugging feature.
  627. */
  628. int what_event = PyThreadState_GET()->what_event;
  629. if (what_event < 0) {
  630. PyErr_Format(PyExc_ValueError,
  631. "f_lineno can only be set in a trace function");
  632. return -1;
  633. }
  634. switch (what_event) {
  635. case PY_MONITORING_EVENT_PY_RESUME:
  636. case PY_MONITORING_EVENT_JUMP:
  637. case PY_MONITORING_EVENT_BRANCH:
  638. case PY_MONITORING_EVENT_LINE:
  639. case PY_MONITORING_EVENT_PY_YIELD:
  640. /* Setting f_lineno is allowed for the above events */
  641. break;
  642. case PY_MONITORING_EVENT_PY_START:
  643. PyErr_Format(PyExc_ValueError,
  644. "can't jump from the 'call' trace event of a new frame");
  645. return -1;
  646. case PY_MONITORING_EVENT_CALL:
  647. case PY_MONITORING_EVENT_C_RETURN:
  648. PyErr_SetString(PyExc_ValueError,
  649. "can't jump during a call");
  650. return -1;
  651. case PY_MONITORING_EVENT_PY_RETURN:
  652. case PY_MONITORING_EVENT_PY_UNWIND:
  653. case PY_MONITORING_EVENT_PY_THROW:
  654. case PY_MONITORING_EVENT_RAISE:
  655. case PY_MONITORING_EVENT_C_RAISE:
  656. case PY_MONITORING_EVENT_INSTRUCTION:
  657. case PY_MONITORING_EVENT_EXCEPTION_HANDLED:
  658. PyErr_Format(PyExc_ValueError,
  659. "can only jump from a 'line' trace event");
  660. return -1;
  661. default:
  662. PyErr_SetString(PyExc_SystemError,
  663. "unexpected event type");
  664. return -1;
  665. }
  666. int new_lineno;
  667. /* Fail if the line falls outside the code block and
  668. select first line with actual code. */
  669. int overflow;
  670. long l_new_lineno = PyLong_AsLongAndOverflow(p_new_lineno, &overflow);
  671. if (overflow
  672. #if SIZEOF_LONG > SIZEOF_INT
  673. || l_new_lineno > INT_MAX
  674. || l_new_lineno < INT_MIN
  675. #endif
  676. ) {
  677. PyErr_SetString(PyExc_ValueError,
  678. "lineno out of range");
  679. return -1;
  680. }
  681. new_lineno = (int)l_new_lineno;
  682. if (new_lineno < f->f_frame->f_code->co_firstlineno) {
  683. PyErr_Format(PyExc_ValueError,
  684. "line %d comes before the current code block",
  685. new_lineno);
  686. return -1;
  687. }
  688. /* PyCode_NewWithPosOnlyArgs limits co_code to be under INT_MAX so this
  689. * should never overflow. */
  690. int len = (int)Py_SIZE(f->f_frame->f_code);
  691. int *lines = marklines(f->f_frame->f_code, len);
  692. if (lines == NULL) {
  693. return -1;
  694. }
  695. new_lineno = first_line_not_before(lines, len, new_lineno);
  696. if (new_lineno < 0) {
  697. PyErr_Format(PyExc_ValueError,
  698. "line %d comes after the current code block",
  699. (int)l_new_lineno);
  700. PyMem_Free(lines);
  701. return -1;
  702. }
  703. int64_t *stacks = mark_stacks(f->f_frame->f_code, len);
  704. if (stacks == NULL) {
  705. PyMem_Free(lines);
  706. return -1;
  707. }
  708. int64_t best_stack = OVERFLOWED;
  709. int best_addr = -1;
  710. int64_t start_stack = stacks[_PyInterpreterFrame_LASTI(f->f_frame)];
  711. int err = -1;
  712. const char *msg = "cannot find bytecode for specified line";
  713. for (int i = 0; i < len; i++) {
  714. if (lines[i] == new_lineno) {
  715. int64_t target_stack = stacks[i];
  716. if (compatible_stack(start_stack, target_stack)) {
  717. err = 0;
  718. if (target_stack > best_stack) {
  719. best_stack = target_stack;
  720. best_addr = i;
  721. }
  722. }
  723. else if (err < 0) {
  724. if (start_stack == OVERFLOWED) {
  725. msg = "stack to deep to analyze";
  726. }
  727. else if (start_stack == UNINITIALIZED) {
  728. msg = "can't jump from unreachable code";
  729. }
  730. else {
  731. msg = explain_incompatible_stack(target_stack);
  732. err = 1;
  733. }
  734. }
  735. }
  736. }
  737. PyMem_Free(stacks);
  738. PyMem_Free(lines);
  739. if (err) {
  740. PyErr_SetString(PyExc_ValueError, msg);
  741. return -1;
  742. }
  743. // Populate any NULL locals that the compiler might have "proven" to exist
  744. // in the new location. Rather than crashing or changing co_code, just bind
  745. // None instead:
  746. int unbound = 0;
  747. for (int i = 0; i < f->f_frame->f_code->co_nlocalsplus; i++) {
  748. // Counting every unbound local is overly-cautious, but a full flow
  749. // analysis (like we do in the compiler) is probably too expensive:
  750. unbound += f->f_frame->localsplus[i] == NULL;
  751. }
  752. if (unbound) {
  753. const char *e = "assigning None to %d unbound local%s";
  754. const char *s = (unbound == 1) ? "" : "s";
  755. if (PyErr_WarnFormat(PyExc_RuntimeWarning, 0, e, unbound, s)) {
  756. return -1;
  757. }
  758. // Do this in a second pass to avoid writing a bunch of Nones when
  759. // warnings are being treated as errors and the previous bit raises:
  760. for (int i = 0; i < f->f_frame->f_code->co_nlocalsplus; i++) {
  761. if (f->f_frame->localsplus[i] == NULL) {
  762. f->f_frame->localsplus[i] = Py_NewRef(Py_None);
  763. unbound--;
  764. }
  765. }
  766. assert(unbound == 0);
  767. }
  768. if (state == FRAME_SUSPENDED) {
  769. /* Account for value popped by yield */
  770. start_stack = pop_value(start_stack);
  771. }
  772. while (start_stack > best_stack) {
  773. if (top_of_stack(start_stack) == Except) {
  774. /* Pop exception stack as well as the evaluation stack */
  775. PyThreadState *tstate = _PyThreadState_GET();
  776. _PyErr_StackItem *exc_info = tstate->exc_info;
  777. PyObject *value = exc_info->exc_value;
  778. PyObject *exc = _PyFrame_StackPop(f->f_frame);
  779. assert(PyExceptionInstance_Check(exc) || exc == Py_None);
  780. exc_info->exc_value = exc;
  781. Py_XDECREF(value);
  782. }
  783. else {
  784. PyObject *v = _PyFrame_StackPop(f->f_frame);
  785. Py_XDECREF(v);
  786. }
  787. start_stack = pop_value(start_stack);
  788. }
  789. /* Finally set the new lasti and return OK. */
  790. f->f_lineno = 0;
  791. f->f_frame->prev_instr = _PyCode_CODE(f->f_frame->f_code) + best_addr;
  792. return 0;
  793. }
  794. static PyObject *
  795. frame_gettrace(PyFrameObject *f, void *closure)
  796. {
  797. PyObject* trace = f->f_trace;
  798. if (trace == NULL)
  799. trace = Py_None;
  800. return Py_NewRef(trace);
  801. }
  802. static int
  803. frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
  804. {
  805. if (v == Py_None) {
  806. v = NULL;
  807. }
  808. if (v != f->f_trace) {
  809. Py_XSETREF(f->f_trace, Py_XNewRef(v));
  810. }
  811. return 0;
  812. }
  813. static PyGetSetDef frame_getsetlist[] = {
  814. {"f_back", (getter)frame_getback, NULL, NULL},
  815. {"f_locals", (getter)frame_getlocals, NULL, NULL},
  816. {"f_lineno", (getter)frame_getlineno,
  817. (setter)frame_setlineno, NULL},
  818. {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL},
  819. {"f_lasti", (getter)frame_getlasti, NULL, NULL},
  820. {"f_globals", (getter)frame_getglobals, NULL, NULL},
  821. {"f_builtins", (getter)frame_getbuiltins, NULL, NULL},
  822. {"f_code", (getter)frame_getcode, NULL, NULL},
  823. {"f_trace_opcodes", (getter)frame_gettrace_opcodes, (setter)frame_settrace_opcodes, NULL},
  824. {0}
  825. };
  826. static void
  827. frame_dealloc(PyFrameObject *f)
  828. {
  829. /* It is the responsibility of the owning generator/coroutine
  830. * to have cleared the generator pointer */
  831. if (_PyObject_GC_IS_TRACKED(f)) {
  832. _PyObject_GC_UNTRACK(f);
  833. }
  834. Py_TRASHCAN_BEGIN(f, frame_dealloc);
  835. PyCodeObject *co = NULL;
  836. /* GH-106092: If f->f_frame was on the stack and we reached the maximum
  837. * nesting depth for deallocations, the trashcan may have delayed this
  838. * deallocation until after f->f_frame is freed. Avoid dereferencing
  839. * f->f_frame unless we know it still points to valid memory. */
  840. _PyInterpreterFrame *frame = (_PyInterpreterFrame *)f->_f_frame_data;
  841. /* Kill all local variables including specials, if we own them */
  842. if (f->f_frame == frame && frame->owner == FRAME_OWNED_BY_FRAME_OBJECT) {
  843. /* Don't clear code object until the end */
  844. co = frame->f_code;
  845. frame->f_code = NULL;
  846. Py_CLEAR(frame->f_funcobj);
  847. Py_CLEAR(frame->f_locals);
  848. PyObject **locals = _PyFrame_GetLocalsArray(frame);
  849. for (int i = 0; i < frame->stacktop; i++) {
  850. Py_CLEAR(locals[i]);
  851. }
  852. }
  853. Py_CLEAR(f->f_back);
  854. Py_CLEAR(f->f_trace);
  855. PyObject_GC_Del(f);
  856. Py_XDECREF(co);
  857. Py_TRASHCAN_END;
  858. }
  859. static int
  860. frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
  861. {
  862. Py_VISIT(f->f_back);
  863. Py_VISIT(f->f_trace);
  864. if (f->f_frame->owner != FRAME_OWNED_BY_FRAME_OBJECT) {
  865. return 0;
  866. }
  867. assert(f->f_frame->frame_obj == NULL);
  868. return _PyFrame_Traverse(f->f_frame, visit, arg);
  869. }
  870. static int
  871. frame_tp_clear(PyFrameObject *f)
  872. {
  873. Py_CLEAR(f->f_trace);
  874. /* locals and stack */
  875. PyObject **locals = _PyFrame_GetLocalsArray(f->f_frame);
  876. assert(f->f_frame->stacktop >= 0);
  877. for (int i = 0; i < f->f_frame->stacktop; i++) {
  878. Py_CLEAR(locals[i]);
  879. }
  880. f->f_frame->stacktop = 0;
  881. return 0;
  882. }
  883. static PyObject *
  884. frame_clear(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
  885. {
  886. if (f->f_frame->owner == FRAME_OWNED_BY_GENERATOR) {
  887. PyGenObject *gen = _PyFrame_GetGenerator(f->f_frame);
  888. if (gen->gi_frame_state == FRAME_EXECUTING) {
  889. goto running;
  890. }
  891. _PyGen_Finalize((PyObject *)gen);
  892. }
  893. else if (f->f_frame->owner == FRAME_OWNED_BY_THREAD) {
  894. goto running;
  895. }
  896. else {
  897. assert(f->f_frame->owner == FRAME_OWNED_BY_FRAME_OBJECT);
  898. (void)frame_tp_clear(f);
  899. }
  900. Py_RETURN_NONE;
  901. running:
  902. PyErr_SetString(PyExc_RuntimeError,
  903. "cannot clear an executing frame");
  904. return NULL;
  905. }
  906. PyDoc_STRVAR(clear__doc__,
  907. "F.clear(): clear most references held by the frame");
  908. static PyObject *
  909. frame_sizeof(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
  910. {
  911. Py_ssize_t res;
  912. res = offsetof(PyFrameObject, _f_frame_data) + offsetof(_PyInterpreterFrame, localsplus);
  913. PyCodeObject *code = f->f_frame->f_code;
  914. res += _PyFrame_NumSlotsForCodeObject(code) * sizeof(PyObject *);
  915. return PyLong_FromSsize_t(res);
  916. }
  917. PyDoc_STRVAR(sizeof__doc__,
  918. "F.__sizeof__() -> size of F in memory, in bytes");
  919. static PyObject *
  920. frame_repr(PyFrameObject *f)
  921. {
  922. int lineno = PyFrame_GetLineNumber(f);
  923. PyCodeObject *code = f->f_frame->f_code;
  924. return PyUnicode_FromFormat(
  925. "<frame at %p, file %R, line %d, code %S>",
  926. f, code->co_filename, lineno, code->co_name);
  927. }
  928. static PyMethodDef frame_methods[] = {
  929. {"clear", (PyCFunction)frame_clear, METH_NOARGS,
  930. clear__doc__},
  931. {"__sizeof__", (PyCFunction)frame_sizeof, METH_NOARGS,
  932. sizeof__doc__},
  933. {NULL, NULL} /* sentinel */
  934. };
  935. PyTypeObject PyFrame_Type = {
  936. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  937. "frame",
  938. offsetof(PyFrameObject, _f_frame_data) +
  939. offsetof(_PyInterpreterFrame, localsplus),
  940. sizeof(PyObject *),
  941. (destructor)frame_dealloc, /* tp_dealloc */
  942. 0, /* tp_vectorcall_offset */
  943. 0, /* tp_getattr */
  944. 0, /* tp_setattr */
  945. 0, /* tp_as_async */
  946. (reprfunc)frame_repr, /* tp_repr */
  947. 0, /* tp_as_number */
  948. 0, /* tp_as_sequence */
  949. 0, /* tp_as_mapping */
  950. 0, /* tp_hash */
  951. 0, /* tp_call */
  952. 0, /* tp_str */
  953. PyObject_GenericGetAttr, /* tp_getattro */
  954. PyObject_GenericSetAttr, /* tp_setattro */
  955. 0, /* tp_as_buffer */
  956. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
  957. 0, /* tp_doc */
  958. (traverseproc)frame_traverse, /* tp_traverse */
  959. (inquiry)frame_tp_clear, /* tp_clear */
  960. 0, /* tp_richcompare */
  961. 0, /* tp_weaklistoffset */
  962. 0, /* tp_iter */
  963. 0, /* tp_iternext */
  964. frame_methods, /* tp_methods */
  965. frame_memberlist, /* tp_members */
  966. frame_getsetlist, /* tp_getset */
  967. 0, /* tp_base */
  968. 0, /* tp_dict */
  969. };
  970. static void
  971. init_frame(_PyInterpreterFrame *frame, PyFunctionObject *func, PyObject *locals)
  972. {
  973. PyCodeObject *code = (PyCodeObject *)func->func_code;
  974. _PyFrame_Initialize(frame, (PyFunctionObject*)Py_NewRef(func),
  975. Py_XNewRef(locals), code, 0);
  976. frame->previous = NULL;
  977. }
  978. PyFrameObject*
  979. _PyFrame_New_NoTrack(PyCodeObject *code)
  980. {
  981. CALL_STAT_INC(frame_objects_created);
  982. int slots = code->co_nlocalsplus + code->co_stacksize;
  983. PyFrameObject *f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type, slots);
  984. if (f == NULL) {
  985. return NULL;
  986. }
  987. f->f_back = NULL;
  988. f->f_trace = NULL;
  989. f->f_trace_lines = 1;
  990. f->f_trace_opcodes = 0;
  991. f->f_fast_as_locals = 0;
  992. f->f_lineno = 0;
  993. return f;
  994. }
  995. /* Legacy API */
  996. PyFrameObject*
  997. PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
  998. PyObject *globals, PyObject *locals)
  999. {
  1000. PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
  1001. if (builtins == NULL) {
  1002. return NULL;
  1003. }
  1004. PyFrameConstructor desc = {
  1005. .fc_globals = globals,
  1006. .fc_builtins = builtins,
  1007. .fc_name = code->co_name,
  1008. .fc_qualname = code->co_name,
  1009. .fc_code = (PyObject *)code,
  1010. .fc_defaults = NULL,
  1011. .fc_kwdefaults = NULL,
  1012. .fc_closure = NULL
  1013. };
  1014. PyFunctionObject *func = _PyFunction_FromConstructor(&desc);
  1015. if (func == NULL) {
  1016. return NULL;
  1017. }
  1018. PyFrameObject *f = _PyFrame_New_NoTrack(code);
  1019. if (f == NULL) {
  1020. Py_DECREF(func);
  1021. return NULL;
  1022. }
  1023. init_frame((_PyInterpreterFrame *)f->_f_frame_data, func, locals);
  1024. f->f_frame = (_PyInterpreterFrame *)f->_f_frame_data;
  1025. f->f_frame->owner = FRAME_OWNED_BY_FRAME_OBJECT;
  1026. // This frame needs to be "complete", so pretend that the first RESUME ran:
  1027. f->f_frame->prev_instr = _PyCode_CODE(code) + code->_co_firsttraceable;
  1028. assert(!_PyFrame_IsIncomplete(f->f_frame));
  1029. Py_DECREF(func);
  1030. _PyObject_GC_TRACK(f);
  1031. return f;
  1032. }
  1033. static int
  1034. _PyFrame_OpAlreadyRan(_PyInterpreterFrame *frame, int opcode, int oparg)
  1035. {
  1036. // This only works when opcode is a non-quickened form:
  1037. assert(_PyOpcode_Deopt[opcode] == opcode);
  1038. int check_oparg = 0;
  1039. for (_Py_CODEUNIT *instruction = _PyCode_CODE(frame->f_code);
  1040. instruction < frame->prev_instr; instruction++)
  1041. {
  1042. int check_opcode = _PyOpcode_Deopt[instruction->op.code];
  1043. check_oparg |= instruction->op.arg;
  1044. if (check_opcode == opcode && check_oparg == oparg) {
  1045. return 1;
  1046. }
  1047. if (check_opcode == EXTENDED_ARG) {
  1048. check_oparg <<= 8;
  1049. }
  1050. else {
  1051. check_oparg = 0;
  1052. }
  1053. instruction += _PyOpcode_Caches[check_opcode];
  1054. }
  1055. return 0;
  1056. }
  1057. // Initialize frame free variables if needed
  1058. static void
  1059. frame_init_get_vars(_PyInterpreterFrame *frame)
  1060. {
  1061. // COPY_FREE_VARS has no quickened forms, so no need to use _PyOpcode_Deopt
  1062. // here:
  1063. PyCodeObject *co = frame->f_code;
  1064. int lasti = _PyInterpreterFrame_LASTI(frame);
  1065. if (!(lasti < 0 && _PyCode_CODE(co)->op.code == COPY_FREE_VARS
  1066. && PyFunction_Check(frame->f_funcobj)))
  1067. {
  1068. /* Free vars are initialized */
  1069. return;
  1070. }
  1071. /* Free vars have not been initialized -- Do that */
  1072. PyObject *closure = ((PyFunctionObject *)frame->f_funcobj)->func_closure;
  1073. int offset = PyCode_GetFirstFree(co);
  1074. for (int i = 0; i < co->co_nfreevars; ++i) {
  1075. PyObject *o = PyTuple_GET_ITEM(closure, i);
  1076. frame->localsplus[offset + i] = Py_NewRef(o);
  1077. }
  1078. // COPY_FREE_VARS doesn't have inline CACHEs, either:
  1079. frame->prev_instr = _PyCode_CODE(frame->f_code);
  1080. }
  1081. static int
  1082. frame_get_var(_PyInterpreterFrame *frame, PyCodeObject *co, int i,
  1083. PyObject **pvalue)
  1084. {
  1085. _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
  1086. /* If the namespace is unoptimized, then one of the
  1087. following cases applies:
  1088. 1. It does not contain free variables, because it
  1089. uses import * or is a top-level namespace.
  1090. 2. It is a class namespace.
  1091. We don't want to accidentally copy free variables
  1092. into the locals dict used by the class.
  1093. */
  1094. if (kind & CO_FAST_FREE && !(co->co_flags & CO_OPTIMIZED)) {
  1095. return 0;
  1096. }
  1097. PyObject *value = frame->localsplus[i];
  1098. if (frame->stacktop) {
  1099. if (kind & CO_FAST_FREE) {
  1100. // The cell was set by COPY_FREE_VARS.
  1101. assert(value != NULL && PyCell_Check(value));
  1102. value = PyCell_GET(value);
  1103. }
  1104. else if (kind & CO_FAST_CELL) {
  1105. // Note that no *_DEREF ops can happen before MAKE_CELL
  1106. // executes. So there's no need to duplicate the work
  1107. // that MAKE_CELL would otherwise do later, if it hasn't
  1108. // run yet.
  1109. if (value != NULL) {
  1110. if (PyCell_Check(value) &&
  1111. _PyFrame_OpAlreadyRan(frame, MAKE_CELL, i)) {
  1112. // (likely) MAKE_CELL must have executed already.
  1113. value = PyCell_GET(value);
  1114. }
  1115. // (likely) Otherwise it it is an arg (kind & CO_FAST_LOCAL),
  1116. // with the initial value set when the frame was created...
  1117. // (unlikely) ...or it was set to some initial value by
  1118. // an earlier call to PyFrame_LocalsToFast().
  1119. }
  1120. }
  1121. }
  1122. else {
  1123. assert(value == NULL);
  1124. }
  1125. *pvalue = value;
  1126. return 1;
  1127. }
  1128. PyObject *
  1129. _PyFrame_GetLocals(_PyInterpreterFrame *frame, int include_hidden)
  1130. {
  1131. /* Merge fast locals into f->f_locals */
  1132. PyObject *locals = frame->f_locals;
  1133. if (locals == NULL) {
  1134. locals = frame->f_locals = PyDict_New();
  1135. if (locals == NULL) {
  1136. return NULL;
  1137. }
  1138. }
  1139. PyObject *hidden = NULL;
  1140. /* If include_hidden, "hidden" fast locals (from inlined comprehensions in
  1141. module/class scopes) will be included in the returned dict, but not in
  1142. frame->f_locals; the returned dict will be a modified copy. Non-hidden
  1143. locals will still be updated in frame->f_locals. */
  1144. if (include_hidden) {
  1145. hidden = PyDict_New();
  1146. if (hidden == NULL) {
  1147. return NULL;
  1148. }
  1149. }
  1150. frame_init_get_vars(frame);
  1151. PyCodeObject *co = frame->f_code;
  1152. for (int i = 0; i < co->co_nlocalsplus; i++) {
  1153. PyObject *value; // borrowed reference
  1154. if (!frame_get_var(frame, co, i, &value)) {
  1155. continue;
  1156. }
  1157. PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
  1158. _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
  1159. if (kind & CO_FAST_HIDDEN) {
  1160. if (include_hidden && value != NULL) {
  1161. if (PyObject_SetItem(hidden, name, value) != 0) {
  1162. goto error;
  1163. }
  1164. }
  1165. continue;
  1166. }
  1167. if (value == NULL) {
  1168. if (PyObject_DelItem(locals, name) != 0) {
  1169. if (PyErr_ExceptionMatches(PyExc_KeyError)) {
  1170. PyErr_Clear();
  1171. }
  1172. else {
  1173. goto error;
  1174. }
  1175. }
  1176. }
  1177. else {
  1178. if (PyObject_SetItem(locals, name, value) != 0) {
  1179. goto error;
  1180. }
  1181. }
  1182. }
  1183. if (include_hidden && PyDict_Size(hidden)) {
  1184. PyObject *innerlocals = PyDict_New();
  1185. if (innerlocals == NULL) {
  1186. goto error;
  1187. }
  1188. if (PyDict_Merge(innerlocals, locals, 1) != 0) {
  1189. Py_DECREF(innerlocals);
  1190. goto error;
  1191. }
  1192. if (PyDict_Merge(innerlocals, hidden, 1) != 0) {
  1193. Py_DECREF(innerlocals);
  1194. goto error;
  1195. }
  1196. locals = innerlocals;
  1197. }
  1198. else {
  1199. Py_INCREF(locals);
  1200. }
  1201. Py_CLEAR(hidden);
  1202. return locals;
  1203. error:
  1204. Py_XDECREF(hidden);
  1205. return NULL;
  1206. }
  1207. int
  1208. _PyFrame_FastToLocalsWithError(_PyInterpreterFrame *frame)
  1209. {
  1210. PyObject *locals = _PyFrame_GetLocals(frame, 0);
  1211. if (locals == NULL) {
  1212. return -1;
  1213. }
  1214. Py_DECREF(locals);
  1215. return 0;
  1216. }
  1217. PyObject *
  1218. PyFrame_GetVar(PyFrameObject *frame_obj, PyObject *name)
  1219. {
  1220. if (!PyUnicode_Check(name)) {
  1221. PyErr_Format(PyExc_TypeError, "name must be str, not %s",
  1222. Py_TYPE(name)->tp_name);
  1223. return NULL;
  1224. }
  1225. _PyInterpreterFrame *frame = frame_obj->f_frame;
  1226. frame_init_get_vars(frame);
  1227. PyCodeObject *co = frame->f_code;
  1228. for (int i = 0; i < co->co_nlocalsplus; i++) {
  1229. PyObject *var_name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
  1230. if (!_PyUnicode_Equal(var_name, name)) {
  1231. continue;
  1232. }
  1233. PyObject *value; // borrowed reference
  1234. if (!frame_get_var(frame, co, i, &value)) {
  1235. break;
  1236. }
  1237. if (value == NULL) {
  1238. break;
  1239. }
  1240. return Py_NewRef(value);
  1241. }
  1242. PyErr_Format(PyExc_NameError, "variable %R does not exist", name);
  1243. return NULL;
  1244. }
  1245. PyObject *
  1246. PyFrame_GetVarString(PyFrameObject *frame, const char *name)
  1247. {
  1248. PyObject *name_obj = PyUnicode_FromString(name);
  1249. if (name_obj == NULL) {
  1250. return NULL;
  1251. }
  1252. PyObject *value = PyFrame_GetVar(frame, name_obj);
  1253. Py_DECREF(name_obj);
  1254. return value;
  1255. }
  1256. int
  1257. PyFrame_FastToLocalsWithError(PyFrameObject *f)
  1258. {
  1259. if (f == NULL) {
  1260. PyErr_BadInternalCall();
  1261. return -1;
  1262. }
  1263. assert(!_PyFrame_IsIncomplete(f->f_frame));
  1264. int err = _PyFrame_FastToLocalsWithError(f->f_frame);
  1265. if (err == 0) {
  1266. f->f_fast_as_locals = 1;
  1267. }
  1268. return err;
  1269. }
  1270. void
  1271. PyFrame_FastToLocals(PyFrameObject *f)
  1272. {
  1273. int res;
  1274. assert(!_PyFrame_IsIncomplete(f->f_frame));
  1275. assert(!PyErr_Occurred());
  1276. res = PyFrame_FastToLocalsWithError(f);
  1277. if (res < 0)
  1278. PyErr_Clear();
  1279. }
  1280. void
  1281. _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear)
  1282. {
  1283. /* Merge locals into fast locals */
  1284. PyObject *locals;
  1285. PyObject **fast;
  1286. PyCodeObject *co;
  1287. locals = frame->f_locals;
  1288. if (locals == NULL) {
  1289. return;
  1290. }
  1291. fast = _PyFrame_GetLocalsArray(frame);
  1292. co = frame->f_code;
  1293. PyObject *exc = PyErr_GetRaisedException();
  1294. for (int i = 0; i < co->co_nlocalsplus; i++) {
  1295. _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
  1296. /* Same test as in PyFrame_FastToLocals() above. */
  1297. if (kind & CO_FAST_FREE && !(co->co_flags & CO_OPTIMIZED)) {
  1298. continue;
  1299. }
  1300. PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
  1301. PyObject *value = PyObject_GetItem(locals, name);
  1302. /* We only care about NULLs if clear is true. */
  1303. if (value == NULL) {
  1304. PyErr_Clear();
  1305. if (!clear) {
  1306. continue;
  1307. }
  1308. }
  1309. PyObject *oldvalue = fast[i];
  1310. PyObject *cell = NULL;
  1311. if (kind == CO_FAST_FREE) {
  1312. // The cell was set when the frame was created from
  1313. // the function's closure.
  1314. assert(oldvalue != NULL && PyCell_Check(oldvalue));
  1315. cell = oldvalue;
  1316. }
  1317. else if (kind & CO_FAST_CELL && oldvalue != NULL) {
  1318. /* Same test as in PyFrame_FastToLocals() above. */
  1319. if (PyCell_Check(oldvalue) &&
  1320. _PyFrame_OpAlreadyRan(frame, MAKE_CELL, i)) {
  1321. // (likely) MAKE_CELL must have executed already.
  1322. cell = oldvalue;
  1323. }
  1324. // (unlikely) Otherwise, it must have been set to some
  1325. // initial value by an earlier call to PyFrame_LocalsToFast().
  1326. }
  1327. if (cell != NULL) {
  1328. oldvalue = PyCell_GET(cell);
  1329. if (value != oldvalue) {
  1330. PyCell_SET(cell, Py_XNewRef(value));
  1331. Py_XDECREF(oldvalue);
  1332. }
  1333. }
  1334. else if (value != oldvalue) {
  1335. if (value == NULL) {
  1336. // Probably can't delete this, since the compiler's flow
  1337. // analysis may have already "proven" that it exists here:
  1338. const char *e = "assigning None to unbound local %R";
  1339. if (PyErr_WarnFormat(PyExc_RuntimeWarning, 0, e, name)) {
  1340. // It's okay if frame_obj is NULL, just try anyways:
  1341. PyErr_WriteUnraisable((PyObject *)frame->frame_obj);
  1342. }
  1343. value = Py_NewRef(Py_None);
  1344. }
  1345. Py_XSETREF(fast[i], Py_NewRef(value));
  1346. }
  1347. Py_XDECREF(value);
  1348. }
  1349. PyErr_SetRaisedException(exc);
  1350. }
  1351. void
  1352. PyFrame_LocalsToFast(PyFrameObject *f, int clear)
  1353. {
  1354. assert(!_PyFrame_IsIncomplete(f->f_frame));
  1355. if (f && f->f_fast_as_locals && _PyFrame_GetState(f) != FRAME_CLEARED) {
  1356. _PyFrame_LocalsToFast(f->f_frame, clear);
  1357. f->f_fast_as_locals = 0;
  1358. }
  1359. }
  1360. int
  1361. _PyFrame_IsEntryFrame(PyFrameObject *frame)
  1362. {
  1363. assert(frame != NULL);
  1364. _PyInterpreterFrame *f = frame->f_frame;
  1365. assert(!_PyFrame_IsIncomplete(f));
  1366. return f->previous && f->previous->owner == FRAME_OWNED_BY_CSTACK;
  1367. }
  1368. PyCodeObject *
  1369. PyFrame_GetCode(PyFrameObject *frame)
  1370. {
  1371. assert(frame != NULL);
  1372. assert(!_PyFrame_IsIncomplete(frame->f_frame));
  1373. PyCodeObject *code = frame->f_frame->f_code;
  1374. assert(code != NULL);
  1375. return (PyCodeObject*)Py_NewRef(code);
  1376. }
  1377. PyFrameObject*
  1378. PyFrame_GetBack(PyFrameObject *frame)
  1379. {
  1380. assert(frame != NULL);
  1381. assert(!_PyFrame_IsIncomplete(frame->f_frame));
  1382. PyFrameObject *back = frame->f_back;
  1383. if (back == NULL) {
  1384. _PyInterpreterFrame *prev = frame->f_frame->previous;
  1385. prev = _PyFrame_GetFirstComplete(prev);
  1386. if (prev) {
  1387. back = _PyFrame_GetFrameObject(prev);
  1388. }
  1389. }
  1390. return (PyFrameObject*)Py_XNewRef(back);
  1391. }
  1392. PyObject*
  1393. PyFrame_GetLocals(PyFrameObject *frame)
  1394. {
  1395. assert(!_PyFrame_IsIncomplete(frame->f_frame));
  1396. return frame_getlocals(frame, NULL);
  1397. }
  1398. PyObject*
  1399. PyFrame_GetGlobals(PyFrameObject *frame)
  1400. {
  1401. assert(!_PyFrame_IsIncomplete(frame->f_frame));
  1402. return frame_getglobals(frame, NULL);
  1403. }
  1404. PyObject*
  1405. PyFrame_GetBuiltins(PyFrameObject *frame)
  1406. {
  1407. assert(!_PyFrame_IsIncomplete(frame->f_frame));
  1408. return frame_getbuiltins(frame, NULL);
  1409. }
  1410. int
  1411. PyFrame_GetLasti(PyFrameObject *frame)
  1412. {
  1413. assert(!_PyFrame_IsIncomplete(frame->f_frame));
  1414. int lasti = _PyInterpreterFrame_LASTI(frame->f_frame);
  1415. if (lasti < 0) {
  1416. return -1;
  1417. }
  1418. return lasti * sizeof(_Py_CODEUNIT);
  1419. }
  1420. PyObject *
  1421. PyFrame_GetGenerator(PyFrameObject *frame)
  1422. {
  1423. assert(!_PyFrame_IsIncomplete(frame->f_frame));
  1424. if (frame->f_frame->owner != FRAME_OWNED_BY_GENERATOR) {
  1425. return NULL;
  1426. }
  1427. PyGenObject *gen = _PyFrame_GetGenerator(frame->f_frame);
  1428. return Py_NewRef(gen);
  1429. }
  1430. PyObject*
  1431. _PyEval_BuiltinsFromGlobals(PyThreadState *tstate, PyObject *globals)
  1432. {
  1433. PyObject *builtins = PyDict_GetItemWithError(globals, &_Py_ID(__builtins__));
  1434. if (builtins) {
  1435. if (PyModule_Check(builtins)) {
  1436. builtins = _PyModule_GetDict(builtins);
  1437. assert(builtins != NULL);
  1438. }
  1439. return builtins;
  1440. }
  1441. if (PyErr_Occurred()) {
  1442. return NULL;
  1443. }
  1444. return _PyEval_GetBuiltins(tstate);
  1445. }