Show More
@@ -1,3795 +1,3804 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 |
|
|
3 | 3 | |
|
4 | 4 |
|
|
5 | 5 |
|
|
6 | 6 |
|
|
7 | 7 |
|
|
8 | 8 |
|
|
9 | 9 |
|
|
10 | 10 |
|
|
11 | 11 |
|
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 |
|
|
15 | 15 |
|
|
16 | 16 |
|
|
17 | 17 |
|
|
18 | 18 |
|
|
19 | 19 |
|
|
20 | 20 |
|
|
21 | 21 |
|
|
22 | 22 |
|
|
23 | 23 |
|
|
24 | 24 |
|
|
25 | 25 |
|
|
26 | 26 |
|
|
27 | 27 |
|
|
28 | 28 |
|
|
29 | 29 |
|
|
30 | 30 | |
|
31 | 31 |
|
|
32 | 32 |
|
|
33 | 33 | |
|
34 | 34 |
|
|
35 | 35 |
|
|
36 | 36 |
|
|
37 | 37 |
|
|
38 | 38 |
|
|
39 | 39 |
|
|
40 | 40 |
|
|
41 | 41 |
|
|
42 | 42 |
|
|
43 | 43 |
|
|
44 | 44 |
|
|
45 | 45 |
|
|
46 | 46 |
|
|
47 | 47 |
|
|
48 | 48 |
|
|
49 | 49 |
|
|
50 | 50 |
|
|
51 | 51 |
|
|
52 | 52 |
|
|
53 | 53 |
|
|
54 | 54 |
|
|
55 | 55 |
|
|
56 | 56 |
|
|
57 | 57 |
|
|
58 | 58 |
|
|
59 | 59 |
|
|
60 | 60 |
|
|
61 | 61 |
|
|
62 | 62 |
|
|
63 | 63 |
|
|
64 | 64 |
|
|
65 | 65 |
|
|
66 | 66 |
|
|
67 | 67 |
|
|
68 | 68 |
|
|
69 | 69 |
|
|
70 | 70 |
|
|
71 | 71 |
|
|
72 | 72 |
|
|
73 | 73 |
|
|
74 | 74 |
|
|
75 | 75 |
|
|
76 | 76 |
|
|
77 | 77 |
|
|
78 | 78 |
|
|
79 | 79 |
|
|
80 | 80 |
|
|
81 | 81 |
|
|
82 | 82 |
|
|
83 | 83 |
|
|
84 | 84 | |
|
85 | 85 |
|
|
86 | 86 |
|
|
87 | 87 | |
|
88 | 88 |
|
|
89 | 89 |
|
|
90 | 90 |
|
|
91 | 91 |
|
|
92 | 92 | |
|
93 | 93 |
|
|
94 | 94 |
|
|
95 | 95 | |
|
96 | 96 |
|
|
97 | 97 |
|
|
98 | 98 |
|
|
99 | 99 |
|
|
100 | 100 |
|
|
101 | 101 |
|
|
102 | 102 |
|
|
103 | 103 |
|
|
104 | 104 | |
|
105 | 105 | |
|
106 | 106 |
|
|
107 | 107 |
|
|
108 | 108 | Warning class for unstable features |
|
109 | 109 | """ |
|
110 | 110 |
|
|
111 | 111 | |
|
112 | 112 |
|
|
113 | 113 |
|
|
114 | 114 |
|
|
115 | 115 |
|
|
116 | 116 |
|
|
117 | 117 |
|
|
118 | 118 |
|
|
119 | 119 | |
|
120 | 120 |
|
|
121 | 121 |
|
|
122 | 122 |
|
|
123 | 123 |
|
|
124 | 124 |
|
|
125 | 125 |
|
|
126 | 126 | |
|
127 | 127 |
|
|
128 | 128 |
|
|
129 | 129 |
|
|
130 | 130 | |
|
131 | 131 |
|
|
132 | 132 |
|
|
133 | 133 | |
|
134 | 134 | Given a function, create a clone of this function where the co_newlocal flag |
|
135 | 135 | has been removed, making this function code actually run in the sourounding |
|
136 | 136 | scope. |
|
137 | 137 | |
|
138 | 138 | We need this in order to run asynchronous code in user level namespace. |
|
139 | 139 | """ |
|
140 | 140 |
|
|
141 | 141 |
|
|
142 | 142 |
|
|
143 | 143 |
|
|
144 | 144 |
|
|
145 | 145 |
|
|
146 | 146 |
|
|
147 | 147 |
|
|
148 | 148 |
|
|
149 | 149 |
|
|
150 | 150 |
|
|
151 | 151 |
|
|
152 | 152 |
|
|
153 | 153 |
|
|
154 | 154 |
|
|
155 | 155 |
|
|
156 | 156 |
|
|
157 | 157 |
|
|
158 | 158 |
|
|
159 | 159 |
|
|
160 | 160 |
|
|
161 | 161 |
|
|
162 | 162 |
|
|
163 | 163 |
|
|
164 | 164 |
|
|
165 | 165 | |
|
166 | 166 | |
|
167 | 167 |
|
|
168 | 168 |
|
|
169 | 169 |
|
|
170 | 170 |
|
|
171 | 171 | |
|
172 | 172 | |
|
173 | 173 |
|
|
174 | 174 |
|
|
175 | 175 | Parse a cell with top-level await and modify the AST to be able to run it later. |
|
176 | 176 | |
|
177 | 177 | Parameters |
|
178 | 178 | ---------- |
|
179 | 179 | cell: str |
|
180 | 180 | The code cell to asyncronify |
|
181 | 181 | wrapper_name: str |
|
182 | 182 | The name of the function to be used to wrap the passed `cell`. It is |
|
183 | 183 | advised to **not** use a python identifier in order to not pollute the |
|
184 | 184 | global namespace in which the function will be ran. |
|
185 | 185 | |
|
186 | 186 | Returns |
|
187 | 187 | ------- |
|
188 | 188 | ModuleType: |
|
189 | 189 | A module object AST containing **one** function named `wrapper_name`. |
|
190 | 190 | |
|
191 | 191 | The given code is wrapped in a async-def function, parsed into an AST, and |
|
192 | 192 | the resulting function definition AST is modified to return the last |
|
193 | 193 | expression. |
|
194 | 194 | |
|
195 | 195 | The last expression or await node is moved into a return statement at the |
|
196 | 196 | end of the function, and removed from its original location. If the last |
|
197 | 197 | node is not Expr or Await nothing is done. |
|
198 | 198 | |
|
199 | 199 | The function `__code__` will need to be later modified (by |
|
200 | 200 | ``removed_co_newlocals``) in a subsequent step to not create new `locals()` |
|
201 | 201 | meaning that the local and global scope are the same, ie as if the body of |
|
202 | 202 | the function was at module level. |
|
203 | 203 | |
|
204 | 204 | Lastly a call to `locals()` is made just before the last expression of the |
|
205 | 205 | function, or just after the last assignment or statement to make sure the |
|
206 | 206 | global dict is updated as python function work with a local fast cache which |
|
207 | 207 | is updated only on `local()` calls. |
|
208 | 208 | """ |
|
209 | 209 | |
|
210 | 210 |
|
|
211 | 211 |
|
|
212 | 212 |
|
|
213 | 213 |
|
|
214 | 214 | |
|
215 | 215 |
|
|
216 | 216 |
|
|
217 | 217 |
|
|
218 | 218 |
|
|
219 | 219 |
|
|
220 | 220 |
|
|
221 | 221 |
|
|
222 | 222 |
|
|
223 | 223 |
|
|
224 | 224 |
|
|
225 | 225 |
|
|
226 | 226 | |
|
227 | 227 |
|
|
228 | 228 |
|
|
229 | 229 | |
|
230 | 230 |
|
|
231 | 231 |
|
|
232 | 232 |
|
|
233 | 233 | |
|
234 | 234 |
|
|
235 | 235 |
|
|
236 | 236 |
|
|
237 | 237 | |
|
238 | 238 |
|
|
239 | 239 |
|
|
240 | 240 |
|
|
241 | 241 |
|
|
242 | 242 |
|
|
243 | 243 |
|
|
244 | 244 |
|
|
245 | 245 |
|
|
246 | 246 |
|
|
247 | 247 |
|
|
248 | 248 |
|
|
249 | 249 | |
|
250 | 250 |
|
|
251 | 251 |
|
|
252 | 252 |
|
|
253 | 253 | |
|
254 | 254 | |
|
255 | 255 |
|
|
256 | 256 | |
|
257 | 257 | |
|
258 | 258 |
|
|
259 | 259 |
|
|
260 | 260 |
|
|
261 | 261 |
|
|
262 | 262 |
|
|
263 | 263 | |
|
264 | 264 | |
|
265 | 265 |
|
|
266 | 266 |
|
|
267 | 267 | |
|
268 | 268 | This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``. |
|
269 | 269 | """ |
|
270 | 270 | |
|
271 | 271 |
|
|
272 | 272 |
|
|
273 | 273 |
|
|
274 | 274 |
|
|
275 | 275 | |
|
276 | 276 | |
|
277 | 277 |
|
|
278 | 278 |
|
|
279 | 279 |
|
|
280 | 280 | a namespace must be assigned to the module's __dict__.""" |
|
281 | 281 |
|
|
282 | 282 | |
|
283 | 283 | |
|
284 | 284 |
|
|
285 | 285 |
|
|
286 | 286 | |
|
287 | 287 | Stores information about what is going to happen. |
|
288 | 288 | """ |
|
289 | 289 |
|
|
290 | 290 |
|
|
291 | 291 |
|
|
292 | 292 |
|
|
293 | 293 | |
|
294 | 294 |
|
|
295 | 295 |
|
|
296 | 296 |
|
|
297 | 297 |
|
|
298 | 298 |
|
|
299 | 299 | |
|
300 | 300 |
|
|
301 | 301 |
|
|
302 | 302 |
|
|
303 | 303 |
|
|
304 | 304 |
|
|
305 | 305 |
|
|
306 | 306 | |
|
307 | 307 | |
|
308 | 308 |
|
|
309 | 309 |
|
|
310 | 310 | |
|
311 | 311 | Stores information about what took place. |
|
312 | 312 | """ |
|
313 | 313 |
|
|
314 | 314 |
|
|
315 | 315 |
|
|
316 | 316 |
|
|
317 | 317 |
|
|
318 | 318 | |
|
319 | 319 |
|
|
320 | 320 |
|
|
321 | 321 | |
|
322 | 322 |
|
|
323 | 323 |
|
|
324 | 324 |
|
|
325 | 325 | |
|
326 | 326 |
|
|
327 | 327 |
|
|
328 | 328 |
|
|
329 | 329 |
|
|
330 | 330 |
|
|
331 | 331 |
|
|
332 | 332 | |
|
333 | 333 |
|
|
334 | 334 |
|
|
335 | 335 |
|
|
336 | 336 |
|
|
337 | 337 | |
|
338 | 338 | |
|
339 | 339 |
|
|
340 | 340 |
|
|
341 | 341 | |
|
342 | 342 |
|
|
343 | 343 |
|
|
344 | 344 |
|
|
345 | 345 |
|
|
346 | 346 | A list of ast.NodeTransformer subclass instances, which will be applied |
|
347 | 347 | to user input before code is run. |
|
348 | 348 | """ |
|
349 | 349 |
|
|
350 | 350 | |
|
351 | 351 |
|
|
352 | 352 |
|
|
353 | 353 | Make IPython automatically call any callable object even if you didn't |
|
354 | 354 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' |
|
355 | 355 | automatically. The value can be '0' to disable the feature, '1' for |
|
356 | 356 | 'smart' autocall, where it is not applied if there are no more |
|
357 | 357 | arguments on the line, and '2' for 'full' autocall, where all callable |
|
358 | 358 | objects are automatically called (even if no arguments are present). |
|
359 | 359 | """ |
|
360 | 360 |
|
|
361 | 361 | |
|
362 | 362 |
|
|
363 | 363 |
|
|
364 | 364 | Autoindent IPython code entered interactively. |
|
365 | 365 | """ |
|
366 | 366 |
|
|
367 | 367 | |
|
368 | 368 |
|
|
369 | 369 |
|
|
370 | 370 | Automatically run await statement in the top level repl. |
|
371 | 371 | """ |
|
372 | 372 |
|
|
373 | 373 | |
|
374 | 374 |
|
|
375 | 375 |
|
|
376 | 376 |
|
|
377 | 377 |
|
|
378 | 378 |
|
|
379 | 379 |
|
|
380 | 380 | |
|
381 | 381 |
|
|
382 | 382 |
|
|
383 | 383 |
|
|
384 | 384 |
|
|
385 | 385 | |
|
386 | 386 |
|
|
387 | 387 |
|
|
388 | 388 |
|
|
389 | 389 | |
|
390 | 390 |
|
|
391 | 391 |
|
|
392 | 392 |
|
|
393 | 393 |
|
|
394 | 394 |
|
|
395 | 395 |
|
|
396 | 396 |
|
|
397 | 397 |
|
|
398 | 398 |
|
|
399 | 399 |
|
|
400 | 400 |
|
|
401 | 401 |
|
|
402 | 402 |
|
|
403 | 403 |
|
|
404 | 404 | |
|
405 | 405 |
|
|
406 | 406 |
|
|
407 | 407 | Enable magic commands to be called without the leading %. |
|
408 | 408 | """ |
|
409 | 409 |
|
|
410 | 410 |
|
|
411 | 411 |
|
|
412 | 412 |
|
|
413 | 413 |
|
|
414 | 414 |
|
|
415 | 415 |
|
|
416 | 416 |
|
|
417 | 417 | |
|
418 | 418 |
|
|
419 | 419 |
|
|
420 | 420 | Set the size of the output cache. The default is 1000, you can |
|
421 | 421 | change it permanently in your config file. Setting it to 0 completely |
|
422 | 422 | disables the caching system, and the minimum value accepted is 3 (if |
|
423 | 423 | you provide a value less than 3, it is reset to 0 and a warning is |
|
424 | 424 | issued). This limit is defined because otherwise you'll spend more |
|
425 | 425 | time re-flushing a too small cache than working |
|
426 | 426 | """ |
|
427 | 427 |
|
|
428 | 428 |
|
|
429 | 429 |
|
|
430 | 430 | Use colors for displaying information about objects. Because this |
|
431 | 431 | information is passed through a pager (like 'less'), and some pagers |
|
432 | 432 | get confused with color codes, this capability can be turned off. |
|
433 | 433 | """ |
|
434 | 434 |
|
|
435 | 435 |
|
|
436 | 436 |
|
|
437 | 437 |
|
|
438 | 438 |
|
|
439 | 439 |
|
|
440 | 440 |
|
|
441 | 441 |
|
|
442 | 442 |
|
|
443 | 443 |
|
|
444 | 444 |
|
|
445 | 445 |
|
|
446 | 446 |
|
|
447 | 447 |
|
|
448 | 448 |
|
|
449 | 449 | Enables rich html representation of docstrings. (This requires the |
|
450 | 450 | docrepr module). |
|
451 | 451 | """).tag(config=True) |
|
452 | 452 | |
|
453 | 453 |
|
|
454 | 454 |
|
|
455 | 455 |
|
|
456 | 456 |
|
|
457 | 457 | |
|
458 | 458 |
|
|
459 | 459 |
|
|
460 | 460 | (Provisional API) enables html representation in mime bundles sent |
|
461 | 461 | to pagers. |
|
462 | 462 | """).tag(config=True) |
|
463 | 463 | |
|
464 | 464 |
|
|
465 | 465 |
|
|
466 | 466 |
|
|
467 | 467 |
|
|
468 | 468 | |
|
469 | 469 |
|
|
470 | 470 | |
|
471 | 471 |
|
|
472 | 472 |
|
|
473 | 473 |
|
|
474 | 474 |
|
|
475 | 475 |
|
|
476 | 476 |
|
|
477 | 477 |
|
|
478 | 478 |
|
|
479 | 479 |
|
|
480 | 480 | |
|
481 | 481 |
|
|
482 | 482 |
|
|
483 | 483 |
|
|
484 | 484 | |
|
485 | 485 |
|
|
486 | 486 |
|
|
487 | 487 |
|
|
488 | 488 | |
|
489 | 489 |
|
|
490 | 490 |
|
|
491 | 491 |
|
|
492 | 492 |
|
|
493 | 493 | |
|
494 | 494 |
|
|
495 | 495 |
|
|
496 | 496 |
|
|
497 | 497 | |
|
498 | 498 | For example, ipykernel ipykernel currently uses |
|
499 | 499 | `shell.input_splitter.check_complete` |
|
500 | 500 | """ |
|
501 | 501 |
|
|
502 | 502 |
|
|
503 | 503 |
|
|
504 | 504 |
|
|
505 | 505 |
|
|
506 | 506 | |
|
507 | 507 |
|
|
508 | 508 |
|
|
509 | 509 | Start logging to the default log file in overwrite mode. |
|
510 | 510 | Use `logappend` to specify a log file to **append** logs to. |
|
511 | 511 | """ |
|
512 | 512 |
|
|
513 | 513 |
|
|
514 | 514 |
|
|
515 | 515 | The name of the logfile to use. |
|
516 | 516 | """ |
|
517 | 517 |
|
|
518 | 518 |
|
|
519 | 519 |
|
|
520 | 520 | Start logging to the given file in append mode. |
|
521 | 521 | Use `logfile` to specify a log file to **overwrite** logs to. |
|
522 | 522 | """ |
|
523 | 523 |
|
|
524 | 524 |
|
|
525 | 525 |
|
|
526 | 526 |
|
|
527 | 527 |
|
|
528 | 528 | Automatically call the pdb debugger after every exception. |
|
529 | 529 | """ |
|
530 | 530 |
|
|
531 | 531 |
|
|
532 | 532 |
|
|
533 | 533 | will be displayed as regular output instead.""" |
|
534 | 534 |
|
|
535 | 535 | |
|
536 | 536 |
|
|
537 | 537 |
|
|
538 | 538 |
|
|
539 | 539 |
|
|
540 | 540 |
|
|
541 | 541 |
|
|
542 | 542 |
|
|
543 | 543 |
|
|
544 | 544 |
|
|
545 | 545 |
|
|
546 | 546 |
|
|
547 | 547 |
|
|
548 | 548 |
|
|
549 | 549 |
|
|
550 | 550 |
|
|
551 | 551 |
|
|
552 | 552 |
|
|
553 | 553 |
|
|
554 | 554 |
|
|
555 | 555 |
|
|
556 | 556 |
|
|
557 | 557 |
|
|
558 | 558 |
|
|
559 | 559 | |
|
560 | 560 |
|
|
561 | 561 |
|
|
562 | 562 |
|
|
563 | 563 | |
|
564 | 564 |
|
|
565 | 565 | |
|
566 | 566 |
|
|
567 | 567 |
|
|
568 | 568 |
|
|
569 | 569 | |
|
570 | 570 |
|
|
571 | 571 |
|
|
572 | 572 | The number of saved history entries to be loaded |
|
573 | 573 | into the history buffer at startup. |
|
574 | 574 | """ |
|
575 | 575 |
|
|
576 | 576 | |
|
577 | 577 |
|
|
578 | 578 |
|
|
579 | 579 |
|
|
580 | 580 |
|
|
581 | 581 | which nodes should be run interactively (displaying output from expressions). |
|
582 | 582 |
|
|
583 | 583 |
|
|
584 | 584 | |
|
585 | 585 |
|
|
586 | 586 |
|
|
587 | 587 |
|
|
588 | 588 |
|
|
589 | 589 |
|
|
590 | 590 |
|
|
591 | 591 |
|
|
592 | 592 |
|
|
593 | 593 |
|
|
594 | 594 |
|
|
595 | 595 | |
|
596 | 596 |
|
|
597 | 597 |
|
|
598 | 598 |
|
|
599 | 599 |
|
|
600 | 600 |
|
|
601 | 601 |
|
|
602 | 602 |
|
|
603 | 603 |
|
|
604 | 604 |
|
|
605 | 605 | |
|
606 | 606 |
|
|
607 | 607 |
|
|
608 | 608 |
|
|
609 | 609 |
|
|
610 | 610 |
|
|
611 | 611 |
|
|
612 | 612 | |
|
613 | 613 | |
|
614 | 614 |
|
|
615 | 615 |
|
|
616 | 616 | |
|
617 | 617 |
|
|
618 | 618 |
|
|
619 | 619 | |
|
620 | 620 |
|
|
621 | 621 | |
|
622 | 622 |
|
|
623 | 623 | |
|
624 | 624 |
|
|
625 | 625 |
|
|
626 | 626 |
|
|
627 | 627 | |
|
628 | 628 |
|
|
629 | 629 |
|
|
630 | 630 |
|
|
631 | 631 |
|
|
632 | 632 |
|
|
633 | 633 |
|
|
634 | 634 |
|
|
635 | 635 | |
|
636 | 636 |
|
|
637 | 637 |
|
|
638 | 638 |
|
|
639 | 639 |
|
|
640 | 640 |
|
|
641 | 641 |
|
|
642 | 642 |
|
|
643 | 643 |
|
|
644 | 644 | |
|
645 | 645 |
|
|
646 | 646 |
|
|
647 | 647 |
|
|
648 | 648 |
|
|
649 | 649 |
|
|
650 | 650 |
|
|
651 | 651 |
|
|
652 | 652 |
|
|
653 | 653 |
|
|
654 | 654 |
|
|
655 | 655 | |
|
656 | 656 |
|
|
657 | 657 |
|
|
658 | 658 |
|
|
659 | 659 |
|
|
660 | 660 | |
|
661 | 661 |
|
|
662 | 662 |
|
|
663 | 663 |
|
|
664 | 664 | |
|
665 | 665 |
|
|
666 | 666 |
|
|
667 | 667 |
|
|
668 | 668 |
|
|
669 | 669 |
|
|
670 | 670 |
|
|
671 | 671 |
|
|
672 | 672 | |
|
673 | 673 |
|
|
674 | 674 |
|
|
675 | 675 |
|
|
676 | 676 |
|
|
677 | 677 |
|
|
678 | 678 |
|
|
679 | 679 |
|
|
680 | 680 |
|
|
681 | 681 |
|
|
682 | 682 |
|
|
683 | 683 |
|
|
684 | 684 |
|
|
685 | 685 |
|
|
686 | 686 |
|
|
687 | 687 |
|
|
688 | 688 |
|
|
689 | 689 |
|
|
690 | 690 |
|
|
691 | 691 |
|
|
692 | 692 |
|
|
693 | 693 |
|
|
694 | 694 |
|
|
695 | 695 |
|
|
696 | 696 |
|
|
697 | 697 | |
|
698 | 698 |
|
|
699 | 699 |
|
|
700 | 700 |
|
|
701 | 701 |
|
|
702 | 702 |
|
|
703 | 703 |
|
|
704 | 704 | |
|
705 | 705 |
|
|
706 | 706 |
|
|
707 | 707 |
|
|
708 | 708 | |
|
709 | 709 |
|
|
710 | 710 |
|
|
711 | 711 |
|
|
712 | 712 |
|
|
713 | 713 |
|
|
714 | 714 |
|
|
715 | 715 | |
|
716 | 716 |
|
|
717 | 717 |
|
|
718 | 718 | |
|
719 | 719 | If called with no arguments, it acts as a toggle.""" |
|
720 | 720 |
|
|
721 | 721 |
|
|
722 | 722 |
|
|
723 | 723 |
|
|
724 | 724 | |
|
725 | 725 |
|
|
726 | 726 |
|
|
727 | 727 | |
|
728 | 728 |
|
|
729 | 729 |
|
|
730 | 730 |
|
|
731 | 731 | |
|
732 | 732 |
|
|
733 | 733 |
|
|
734 | 734 |
|
|
735 | 735 |
|
|
736 | 736 | |
|
737 | 737 |
|
|
738 | 738 | |
|
739 | 739 |
|
|
740 | 740 |
|
|
741 | 741 |
|
|
742 | 742 |
|
|
743 | 743 |
|
|
744 | 744 |
|
|
745 | 745 |
|
|
746 | 746 | |
|
747 | 747 |
|
|
748 | 748 |
|
|
749 | 749 | |
|
750 | 750 |
|
|
751 | 751 |
|
|
752 | 752 | |
|
753 | 753 |
|
|
754 | 754 |
|
|
755 | 755 |
|
|
756 | 756 |
|
|
757 | 757 |
|
|
758 | 758 |
|
|
759 | 759 | |
|
760 | 760 |
|
|
761 | 761 |
|
|
762 | 762 |
|
|
763 | 763 |
|
|
764 | 764 | |
|
765 | 765 |
|
|
766 | 766 |
|
|
767 | 767 |
|
|
768 | 768 | |
|
769 | 769 |
|
|
770 | 770 |
|
|
771 | 771 | |
|
772 | 772 |
|
|
773 | 773 |
|
|
774 | 774 | |
|
775 | 775 |
|
|
776 | 776 |
|
|
777 | 777 |
|
|
778 | 778 | |
|
779 | 779 |
|
|
780 | 780 |
|
|
781 | 781 |
|
|
782 | 782 |
|
|
783 | 783 |
|
|
784 | 784 |
|
|
785 | 785 |
|
|
786 | 786 |
|
|
787 | 787 | |
|
788 | 788 | |
|
789 | 789 |
|
|
790 | 790 |
|
|
791 | 791 |
|
|
792 | 792 |
|
|
793 | 793 |
|
|
794 | 794 | |
|
795 | 795 |
|
|
796 | 796 |
|
|
797 | 797 |
|
|
798 | 798 | |
|
799 | 799 |
|
|
800 | 800 |
|
|
801 | 801 |
|
|
802 | 802 | |
|
803 | 803 |
|
|
804 | 804 | |
|
805 | 805 |
|
|
806 | 806 |
|
|
807 | 807 |
|
|
808 | 808 | |
|
809 | 809 |
|
|
810 | 810 |
|
|
811 | 811 | """ |
|
812 | 812 |
|
|
813 | 813 |
|
|
814 | 814 |
|
|
815 | 815 |
|
|
816 | 816 |
|
|
817 | 817 |
|
|
818 | 818 | |
|
819 | 819 |
|
|
820 | 820 |
|
|
821 | 821 | register default filter for deprecation warning. |
|
822 | 822 | |
|
823 | 823 | This will allow deprecation warning of function used interactively to show |
|
824 | 824 | warning to users, and still hide deprecation warning from libraries import. |
|
825 | 825 | """ |
|
826 | 826 |
|
|
827 | 827 |
|
|
828 | 828 | |
|
829 | 829 | |
|
830 | 830 |
|
|
831 | 831 |
|
|
832 | 832 |
|
|
833 | 833 |
|
|
834 | 834 |
|
|
835 | 835 |
|
|
836 | 836 |
|
|
837 | 837 | |
|
838 | 838 |
|
|
839 | 839 | |
|
840 | 840 |
|
|
841 | 841 |
|
|
842 | 842 |
|
|
843 | 843 |
|
|
844 | 844 |
|
|
845 | 845 |
|
|
846 | 846 |
|
|
847 | 847 | |
|
848 | 848 |
|
|
849 | 849 |
|
|
850 | 850 |
|
|
851 | 851 |
|
|
852 | 852 |
|
|
853 | 853 |
|
|
854 | 854 |
|
|
855 | 855 |
|
|
856 | 856 |
|
|
857 | 857 |
|
|
858 | 858 |
|
|
859 | 859 | |
|
860 | 860 |
|
|
861 | 861 |
|
|
862 | 862 |
|
|
863 | 863 |
|
|
864 | 864 |
|
|
865 | 865 |
|
|
866 | 866 | |
|
867 | 867 |
|
|
868 | 868 |
|
|
869 | 869 |
|
|
870 | 870 | |
|
871 | 871 |
|
|
872 | 872 |
|
|
873 | 873 |
|
|
874 | 874 | |
|
875 | 875 |
|
|
876 | 876 |
|
|
877 | 877 |
|
|
878 | 878 |
|
|
879 | 879 |
|
|
880 | 880 |
|
|
881 | 881 | |
|
882 | 882 |
|
|
883 | 883 |
|
|
884 | 884 |
|
|
885 | 885 |
|
|
886 | 886 |
|
|
887 | 887 |
|
|
888 | 888 |
|
|
889 | 889 |
|
|
890 | 890 |
|
|
891 | 891 |
|
|
892 | 892 |
|
|
893 | 893 | |
|
894 | 894 |
|
|
895 | 895 |
|
|
896 | 896 | This isn't perfect: it doesn't use the Python interpreter with which the |
|
897 | 897 | virtualenv was built, and it ignores the --no-site-packages option. A |
|
898 | 898 | warning will appear suggesting the user installs IPython in the |
|
899 | 899 | virtualenv, but for many cases, it probably works well enough. |
|
900 | 900 | |
|
901 | 901 | Adapted from code snippets online. |
|
902 | 902 | |
|
903 | 903 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv |
|
904 | 904 | """ |
|
905 | 905 |
|
|
906 | 906 |
|
|
907 | 907 |
|
|
908 | 908 |
|
|
909 | 909 |
|
|
910 | 910 |
|
|
911 | 911 | |
|
912 | 912 |
|
|
913 | 913 |
|
|
914 | 914 | |
|
915 | 915 |
|
|
916 | 916 |
|
|
917 | 917 |
|
|
918 | 918 |
|
|
919 | 919 |
|
|
920 | 920 |
|
|
921 | 921 |
|
|
922 | 922 |
|
|
923 | 923 | |
|
924 | 924 |
|
|
925 | 925 |
|
|
926 | 926 |
|
|
927 | 927 |
|
|
928 | 928 |
|
|
929 | 929 | |
|
930 | 930 |
|
|
931 | 931 |
|
|
932 | 932 |
|
|
933 | 933 | |
|
934 | 934 |
|
|
935 | 935 |
|
|
936 | 936 |
|
|
937 | 937 |
|
|
938 | 938 |
|
|
939 | 939 |
|
|
940 | 940 |
|
|
941 | 941 |
|
|
942 | 942 |
|
|
943 | 943 |
|
|
944 | 944 |
|
|
945 | 945 |
|
|
946 | 946 |
|
|
947 | 947 |
|
|
948 | 948 | |
|
949 | 949 |
|
|
950 | 950 |
|
|
951 | 951 |
|
|
952 | 952 | |
|
953 | 953 |
|
|
954 | 954 |
|
|
955 | 955 | |
|
956 | 956 | This has to be called after self.user_module is created. |
|
957 | 957 | """ |
|
958 | 958 |
|
|
959 | 959 |
|
|
960 | 960 |
|
|
961 | 961 |
|
|
962 | 962 |
|
|
963 | 963 |
|
|
964 | 964 | |
|
965 | 965 |
|
|
966 | 966 |
|
|
967 | 967 |
|
|
968 | 968 |
|
|
969 | 969 |
|
|
970 | 970 |
|
|
971 | 971 |
|
|
972 | 972 |
|
|
973 | 973 |
|
|
974 | 974 |
|
|
975 | 975 | |
|
976 | 976 |
|
|
977 | 977 |
|
|
978 | 978 |
|
|
979 | 979 |
|
|
980 | 980 |
|
|
981 | 981 |
|
|
982 | 982 |
|
|
983 | 983 |
|
|
984 | 984 |
|
|
985 | 985 |
|
|
986 | 986 |
|
|
987 | 987 |
|
|
988 | 988 | |
|
989 | 989 |
|
|
990 | 990 |
|
|
991 | 991 |
|
|
992 | 992 |
|
|
993 | 993 |
|
|
994 | 994 |
|
|
995 | 995 |
|
|
996 | 996 |
|
|
997 | 997 | |
|
998 | 998 |
|
|
999 | 999 |
|
|
1000 | 1000 |
|
|
1001 | 1001 | |
|
1002 | 1002 |
|
|
1003 | 1003 | |
|
1004 | 1004 |
|
|
1005 | 1005 |
|
|
1006 | 1006 |
|
|
1007 | 1007 |
|
|
1008 | 1008 |
|
|
1009 | 1009 |
|
|
1010 | 1010 |
|
|
1011 | 1011 |
|
|
1012 | 1012 |
|
|
1013 | 1013 |
|
|
1014 | 1014 |
|
|
1015 | 1015 |
|
|
1016 | 1016 |
|
|
1017 | 1017 | |
|
1018 | 1018 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
1019 | 1019 | adding your function to one of these hooks, you can modify IPython's |
|
1020 | 1020 | behavior to call at runtime your own routines.""" |
|
1021 | 1021 | |
|
1022 | 1022 |
|
|
1023 | 1023 |
|
|
1024 | 1024 |
|
|
1025 | 1025 | |
|
1026 | 1026 |
|
|
1027 | 1027 | |
|
1028 | 1028 |
|
|
1029 | 1029 |
|
|
1030 | 1030 |
|
|
1031 | 1031 |
|
|
1032 | 1032 |
|
|
1033 | 1033 |
|
|
1034 | 1034 |
|
|
1035 | 1035 |
|
|
1036 | 1036 |
|
|
1037 | 1037 |
|
|
1038 | 1038 |
|
|
1039 | 1039 | |
|
1040 | 1040 |
|
|
1041 | 1041 |
|
|
1042 | 1042 |
|
|
1043 | 1043 |
|
|
1044 | 1044 | |
|
1045 | 1045 |
|
|
1046 | 1046 |
|
|
1047 | 1047 |
|
|
1048 | 1048 | |
|
1049 | 1049 |
|
|
1050 | 1050 |
|
|
1051 | 1051 | |
|
1052 | 1052 |
|
|
1053 | 1053 |
|
|
1054 | 1054 |
|
|
1055 | 1055 |
|
|
1056 | 1056 |
|
|
1057 | 1057 | |
|
1058 | 1058 |
|
|
1059 | 1059 | |
|
1060 | 1060 |
|
|
1061 | 1061 |
|
|
1062 | 1062 |
|
|
1063 | 1063 | |
|
1064 | 1064 |
|
|
1065 | 1065 |
|
|
1066 | 1066 | |
|
1067 | 1067 |
|
|
1068 | 1068 | |
|
1069 | 1069 |
|
|
1070 | 1070 |
|
|
1071 | 1071 | |
|
1072 | 1072 | Register a function for calling after code execution. |
|
1073 | 1073 | """ |
|
1074 | 1074 |
|
|
1075 | 1075 |
|
|
1076 | 1076 |
|
|
1077 | 1077 |
|
|
1078 | 1078 |
|
|
1079 | 1079 |
|
|
1080 | 1080 |
|
|
1081 | 1081 |
|
|
1082 | 1082 |
|
|
1083 | 1083 |
|
|
1084 | 1084 | |
|
1085 | 1085 |
|
|
1086 | 1086 |
|
|
1087 | 1087 |
|
|
1088 | 1088 | |
|
1089 | 1089 |
|
|
1090 | 1090 |
|
|
1091 | 1091 | |
|
1092 | 1092 | ``filename`` should be the path of the script which will be run in the |
|
1093 | 1093 | module. Requests with the same filename will get the same module, with |
|
1094 | 1094 | its namespace cleared. |
|
1095 | 1095 | |
|
1096 | 1096 | ``modname`` should be the module name - normally either '__main__' or |
|
1097 | 1097 | the basename of the file without the extension. |
|
1098 | 1098 | |
|
1099 | 1099 | When scripts are executed via %run, we must keep a reference to their |
|
1100 | 1100 | __main__ module around so that Python doesn't |
|
1101 | 1101 | clear it, rendering references to module globals useless. |
|
1102 | 1102 | |
|
1103 | 1103 | This method keeps said reference in a private dict, keyed by the |
|
1104 | 1104 | absolute path of the script. This way, for multiple executions of the |
|
1105 | 1105 | same script we only keep one copy of the namespace (the last one), |
|
1106 | 1106 | thus preventing memory leaks from old references while allowing the |
|
1107 | 1107 | objects from the last execution to be accessible. |
|
1108 | 1108 | """ |
|
1109 | 1109 |
|
|
1110 | 1110 |
|
|
1111 | 1111 |
|
|
1112 | 1112 |
|
|
1113 | 1113 |
|
|
1114 | 1114 |
|
|
1115 | 1115 |
|
|
1116 | 1116 |
|
|
1117 | 1117 |
|
|
1118 | 1118 |
|
|
1119 | 1119 |
|
|
1120 | 1120 |
|
|
1121 | 1121 |
|
|
1122 | 1122 |
|
|
1123 | 1123 |
|
|
1124 | 1124 |
|
|
1125 | 1125 |
|
|
1126 | 1126 | |
|
1127 | 1127 |
|
|
1128 | 1128 |
|
|
1129 | 1129 | |
|
1130 | 1130 | Mainly for use by utilities like %reset. |
|
1131 | 1131 | |
|
1132 | 1132 | Examples |
|
1133 | 1133 | -------- |
|
1134 | 1134 | In [15]: import IPython |
|
1135 | 1135 | |
|
1136 | 1136 | In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython') |
|
1137 | 1137 | |
|
1138 | 1138 | In [17]: len(_ip._main_mod_cache) > 0 |
|
1139 | 1139 | Out[17]: True |
|
1140 | 1140 | |
|
1141 | 1141 | In [18]: _ip.clear_main_mod_cache() |
|
1142 | 1142 | |
|
1143 | 1143 | In [19]: len(_ip._main_mod_cache) == 0 |
|
1144 | 1144 | Out[19]: True |
|
1145 | 1145 | """ |
|
1146 | 1146 |
|
|
1147 | 1147 | |
|
1148 | 1148 |
|
|
1149 | 1149 |
|
|
1150 | 1150 |
|
|
1151 | 1151 | |
|
1152 | 1152 |
|
|
1153 | 1153 |
|
|
1154 | 1154 |
|
|
1155 | 1155 |
|
|
1156 | 1156 | |
|
1157 | 1157 |
|
|
1158 | 1158 |
|
|
1159 | 1159 | |
|
1160 | 1160 |
|
|
1161 | 1161 | |
|
1162 | 1162 |
|
|
1163 | 1163 |
|
|
1164 | 1164 | |
|
1165 | 1165 |
|
|
1166 | 1166 |
|
|
1167 | 1167 | |
|
1168 | 1168 |
|
|
1169 | 1169 |
|
|
1170 | 1170 | |
|
1171 | 1171 |
|
|
1172 | 1172 |
|
|
1173 | 1173 | |
|
1174 | 1174 |
|
|
1175 | 1175 |
|
|
1176 | 1176 | |
|
1177 | 1177 | Keywords: |
|
1178 | 1178 | |
|
1179 | 1179 | - force(False): by default, this routine checks the instance call_pdb |
|
1180 | 1180 | flag and does not actually invoke the debugger if the flag is false. |
|
1181 | 1181 | The 'force' option forces the debugger to activate even if the flag |
|
1182 | 1182 | is false. |
|
1183 | 1183 | """ |
|
1184 | 1184 | |
|
1185 | 1185 |
|
|
1186 | 1186 |
|
|
1187 | 1187 | |
|
1188 | 1188 |
|
|
1189 | 1189 |
|
|
1190 | 1190 |
|
|
1191 | 1191 | |
|
1192 | 1192 |
|
|
1193 | 1193 | |
|
1194 | 1194 |
|
|
1195 | 1195 |
|
|
1196 | 1196 |
|
|
1197 | 1197 |
|
|
1198 | 1198 | |
|
1199 | 1199 |
|
|
1200 | 1200 |
|
|
1201 | 1201 |
|
|
1202 | 1202 |
|
|
1203 | 1203 |
|
|
1204 | 1204 |
|
|
1205 | 1205 |
|
|
1206 | 1206 |
|
|
1207 | 1207 | |
|
1208 | 1208 |
|
|
1209 | 1209 |
|
|
1210 | 1210 |
|
|
1211 | 1211 |
|
|
1212 | 1212 | |
|
1213 | 1213 |
|
|
1214 | 1214 |
|
|
1215 | 1215 |
|
|
1216 | 1216 |
|
|
1217 | 1217 |
|
|
1218 | 1218 | |
|
1219 | 1219 |
|
|
1220 | 1220 |
|
|
1221 | 1221 |
|
|
1222 | 1222 |
|
|
1223 | 1223 |
|
|
1224 | 1224 |
|
|
1225 | 1225 | |
|
1226 | 1226 |
|
|
1227 | 1227 |
|
|
1228 | 1228 |
|
|
1229 | 1229 |
|
|
1230 | 1230 |
|
|
1231 | 1231 |
|
|
1232 | 1232 | |
|
1233 | 1233 |
|
|
1234 | 1234 |
|
|
1235 | 1235 |
|
|
1236 | 1236 |
|
|
1237 | 1237 |
|
|
1238 | 1238 |
|
|
1239 | 1239 | |
|
1240 | 1240 |
|
|
1241 | 1241 |
|
|
1242 | 1242 |
|
|
1243 | 1243 | |
|
1244 | 1244 |
|
|
1245 | 1245 |
|
|
1246 | 1246 |
|
|
1247 | 1247 |
|
|
1248 | 1248 |
|
|
1249 | 1249 |
|
|
1250 | 1250 |
|
|
1251 | 1251 |
|
|
1252 | 1252 |
|
|
1253 | 1253 |
|
|
1254 | 1254 |
|
|
1255 | 1255 |
|
|
1256 | 1256 |
|
|
1257 | 1257 |
|
|
1258 | 1258 |
|
|
1259 | 1259 |
|
|
1260 | 1260 |
|
|
1261 | 1261 |
|
|
1262 | 1262 |
|
|
1263 | 1263 |
|
|
1264 | 1264 |
|
|
1265 | 1265 |
|
|
1266 | 1266 | |
|
1267 | 1267 |
|
|
1268 | 1268 |
|
|
1269 | 1269 | |
|
1270 | 1270 |
|
|
1271 | 1271 |
|
|
1272 | 1272 |
|
|
1273 | 1273 |
|
|
1274 | 1274 |
|
|
1275 | 1275 |
|
|
1276 | 1276 |
|
|
1277 | 1277 |
|
|
1278 | 1278 |
|
|
1279 | 1279 |
|
|
1280 | 1280 | |
|
1281 | 1281 |
|
|
1282 | 1282 |
|
|
1283 | 1283 | |
|
1284 | 1284 | When IPython is started normally, both parameters are None: a new module |
|
1285 | 1285 | is created automatically, and its __dict__ used as the namespace. |
|
1286 | 1286 | |
|
1287 | 1287 | If only user_module is provided, its __dict__ is used as the namespace. |
|
1288 | 1288 | If only user_ns is provided, a dummy module is created, and user_ns |
|
1289 | 1289 | becomes the global namespace. If both are provided (as they may be |
|
1290 | 1290 | when embedding), user_ns is the local namespace, and user_module |
|
1291 | 1291 | provides the global namespace. |
|
1292 | 1292 | |
|
1293 | 1293 | Parameters |
|
1294 | 1294 | ---------- |
|
1295 | 1295 | user_module : module, optional |
|
1296 | 1296 | The current user module in which IPython is being run. If None, |
|
1297 | 1297 | a clean module will be created. |
|
1298 | 1298 | user_ns : dict, optional |
|
1299 | 1299 | A namespace in which to run interactive commands. |
|
1300 | 1300 | |
|
1301 | 1301 | Returns |
|
1302 | 1302 | ------- |
|
1303 | 1303 | A tuple of user_module and user_ns, each properly initialised. |
|
1304 | 1304 | """ |
|
1305 | 1305 |
|
|
1306 | 1306 |
|
|
1307 | 1307 |
|
|
1308 | 1308 |
|
|
1309 | 1309 |
|
|
1310 | 1310 |
|
|
1311 | 1311 |
|
|
1312 | 1312 |
|
|
1313 | 1313 |
|
|
1314 | 1314 |
|
|
1315 | 1315 |
|
|
1316 | 1316 |
|
|
1317 | 1317 |
|
|
1318 | 1318 |
|
|
1319 | 1319 |
|
|
1320 | 1320 |
|
|
1321 | 1321 |
|
|
1322 | 1322 | |
|
1323 | 1323 |
|
|
1324 | 1324 | |
|
1325 | 1325 |
|
|
1326 | 1326 |
|
|
1327 | 1327 |
|
|
1328 | 1328 |
|
|
1329 | 1329 |
|
|
1330 | 1330 |
|
|
1331 | 1331 |
|
|
1332 | 1332 | |
|
1333 | 1333 |
|
|
1334 | 1334 |
|
|
1335 | 1335 |
|
|
1336 | 1336 |
|
|
1337 | 1337 |
|
|
1338 | 1338 |
|
|
1339 | 1339 |
|
|
1340 | 1340 | |
|
1341 | 1341 |
|
|
1342 | 1342 |
|
|
1343 | 1343 |
|
|
1344 | 1344 | |
|
1345 | 1345 |
|
|
1346 | 1346 |
|
|
1347 | 1347 | |
|
1348 | 1348 | Certain history lists are also initialized here, as they effectively |
|
1349 | 1349 | act as user namespaces. |
|
1350 | 1350 | |
|
1351 | 1351 | Notes |
|
1352 | 1352 | ----- |
|
1353 | 1353 | All data structures here are only filled in, they are NOT reset by this |
|
1354 | 1354 | method. If they were not empty before, data will simply be added to |
|
1355 | 1355 | them. |
|
1356 | 1356 | """ |
|
1357 | 1357 |
|
|
1358 | 1358 |
|
|
1359 | 1359 |
|
|
1360 | 1360 |
|
|
1361 | 1361 |
|
|
1362 | 1362 | |
|
1363 | 1363 |
|
|
1364 | 1364 |
|
|
1365 | 1365 |
|
|
1366 | 1366 |
|
|
1367 | 1367 |
|
|
1368 | 1368 |
|
|
1369 | 1369 |
|
|
1370 | 1370 |
|
|
1371 | 1371 | |
|
1372 | 1372 |
|
|
1373 | 1373 |
|
|
1374 | 1374 |
|
|
1375 | 1375 |
|
|
1376 | 1376 |
|
|
1377 | 1377 |
|
|
1378 | 1378 |
|
|
1379 | 1379 |
|
|
1380 | 1380 | |
|
1381 | 1381 |
|
|
1382 | 1382 |
|
|
1383 | 1383 |
|
|
1384 | 1384 |
|
|
1385 | 1385 | |
|
1386 | 1386 |
|
|
1387 | 1387 |
|
|
1388 | 1388 |
|
|
1389 | 1389 |
|
|
1390 | 1390 |
|
|
1391 | 1391 | |
|
1392 | 1392 |
|
|
1393 | 1393 |
|
|
1394 | 1394 |
|
|
1395 | 1395 | |
|
1396 | 1396 |
|
|
1397 | 1397 |
|
|
1398 | 1398 |
|
|
1399 | 1399 | |
|
1400 | 1400 |
|
|
1401 | 1401 |
|
|
1402 | 1402 |
|
|
1403 | 1403 |
|
|
1404 | 1404 |
|
|
1405 | 1405 |
|
|
1406 | 1406 | IPython might store a user-created object. |
|
1407 | 1407 | |
|
1408 | 1408 | Note that this does not include the displayhook, which also caches |
|
1409 | 1409 | objects from the output.""" |
|
1410 | 1410 |
|
|
1411 | 1411 |
|
|
1412 | 1412 | |
|
1413 |
|
|
|
1413 | def reset(self, new_session=True, aggressive=False): | |
|
1414 | 1414 |
|
|
1415 | 1415 | user objects. |
|
1416 | 1416 | |
|
1417 | 1417 | If new_session is True, a new history session will be opened. |
|
1418 | 1418 | """ |
|
1419 | 1419 |
|
|
1420 | 1420 |
|
|
1421 | 1421 |
|
|
1422 | 1422 |
|
|
1423 | 1423 |
|
|
1424 | 1424 | |
|
1425 | 1425 |
|
|
1426 | 1426 |
|
|
1427 | 1427 |
|
|
1428 | 1428 |
|
|
1429 | 1429 |
|
|
1430 | 1430 |
|
|
1431 | 1431 |
|
|
1432 | 1432 | |
|
1433 | 1433 |
|
|
1434 | 1434 |
|
|
1435 | 1435 |
|
|
1436 | 1436 |
|
|
1437 | 1437 |
|
|
1438 | 1438 |
|
|
1439 | 1439 |
|
|
1440 | 1440 |
|
|
1441 | 1441 |
|
|
1442 | 1442 |
|
|
1443 | 1443 |
|
|
1444 | 1444 |
|
|
1445 | 1445 | |
|
1446 | 1446 |
|
|
1447 | 1447 | |
|
1448 | 1448 |
|
|
1449 | 1449 |
|
|
1450 | if aggressive and not hasattr(self, "_sys_modules_keys"): | |
|
1451 | print("Cannot restore sys.module, no snapshot") | |
|
1452 | elif aggressive: | |
|
1453 | print("culling sys module...") | |
|
1454 | current_keys = set(sys.modules.keys()) | |
|
1455 | for k in current_keys - self._sys_modules_keys: | |
|
1456 | if k.startswith("multiprocessing"): | |
|
1457 | continue | |
|
1458 | del sys.modules[k] | |
|
1450 | 1459 | |
|
1451 | 1460 |
|
|
1452 | 1461 |
|
|
1453 | 1462 |
|
|
1454 | 1463 | |
|
1455 | 1464 |
|
|
1456 | 1465 |
|
|
1457 | 1466 |
|
|
1458 | 1467 |
|
|
1459 | 1468 |
|
|
1460 | 1469 |
|
|
1461 | 1470 |
|
|
1462 | 1471 | |
|
1463 | 1472 |
|
|
1464 | 1473 |
|
|
1465 | 1474 |
|
|
1466 | 1475 | |
|
1467 | 1476 |
|
|
1468 | 1477 |
|
|
1469 | 1478 | far as possible, we're not keeping any hidden references to it. |
|
1470 | 1479 | |
|
1471 | 1480 | Parameters |
|
1472 | 1481 | ---------- |
|
1473 | 1482 | varname : str |
|
1474 | 1483 | The name of the variable to delete. |
|
1475 | 1484 | by_name : bool |
|
1476 | 1485 | If True, delete variables with the given name in each |
|
1477 | 1486 | namespace. If False (default), find the variable in the user |
|
1478 | 1487 | namespace, and delete references to it. |
|
1479 | 1488 | """ |
|
1480 | 1489 |
|
|
1481 | 1490 |
|
|
1482 | 1491 | |
|
1483 | 1492 |
|
|
1484 | 1493 |
|
|
1485 | 1494 |
|
|
1486 | 1495 |
|
|
1487 | 1496 |
|
|
1488 | 1497 |
|
|
1489 | 1498 |
|
|
1490 | 1499 |
|
|
1491 | 1500 |
|
|
1492 | 1501 |
|
|
1493 | 1502 |
|
|
1494 | 1503 |
|
|
1495 | 1504 |
|
|
1496 | 1505 |
|
|
1497 | 1506 |
|
|
1498 | 1507 |
|
|
1499 | 1508 |
|
|
1500 | 1509 |
|
|
1501 | 1510 |
|
|
1502 | 1511 | |
|
1503 | 1512 |
|
|
1504 | 1513 |
|
|
1505 | 1514 |
|
|
1506 | 1515 | |
|
1507 | 1516 |
|
|
1508 | 1517 |
|
|
1509 | 1518 |
|
|
1510 | 1519 |
|
|
1511 | 1520 | |
|
1512 | 1521 |
|
|
1513 | 1522 |
|
|
1514 | 1523 | specified regular expression. |
|
1515 | 1524 | |
|
1516 | 1525 | Parameters |
|
1517 | 1526 | ---------- |
|
1518 | 1527 | regex : string or compiled pattern, optional |
|
1519 | 1528 | A regular expression pattern that will be used in searching |
|
1520 | 1529 | variable names in the users namespaces. |
|
1521 | 1530 | """ |
|
1522 | 1531 |
|
|
1523 | 1532 |
|
|
1524 | 1533 |
|
|
1525 | 1534 |
|
|
1526 | 1535 |
|
|
1527 | 1536 |
|
|
1528 | 1537 |
|
|
1529 | 1538 |
|
|
1530 | 1539 |
|
|
1531 | 1540 |
|
|
1532 | 1541 |
|
|
1533 | 1542 | |
|
1534 | 1543 |
|
|
1535 | 1544 |
|
|
1536 | 1545 | |
|
1537 | 1546 | Parameters |
|
1538 | 1547 | ---------- |
|
1539 | 1548 | variables : dict, str or list/tuple of str |
|
1540 | 1549 | The variables to inject into the user's namespace. If a dict, a |
|
1541 | 1550 | simple update is done. If a str, the string is assumed to have |
|
1542 | 1551 | variable names separated by spaces. A list/tuple of str can also |
|
1543 | 1552 | be used to give the variable names. If just the variable names are |
|
1544 | 1553 | give (list/tuple/str) then the variable values looked up in the |
|
1545 | 1554 | callers frame. |
|
1546 | 1555 | interactive : bool |
|
1547 | 1556 | If True (default), the variables will be listed with the ``who`` |
|
1548 | 1557 | magic. |
|
1549 | 1558 | """ |
|
1550 | 1559 |
|
|
1551 | 1560 | |
|
1552 | 1561 |
|
|
1553 | 1562 |
|
|
1554 | 1563 |
|
|
1555 | 1564 |
|
|
1556 | 1565 |
|
|
1557 | 1566 |
|
|
1558 | 1567 |
|
|
1559 | 1568 |
|
|
1560 | 1569 |
|
|
1561 | 1570 |
|
|
1562 | 1571 |
|
|
1563 | 1572 |
|
|
1564 | 1573 |
|
|
1565 | 1574 |
|
|
1566 | 1575 |
|
|
1567 | 1576 |
|
|
1568 | 1577 |
|
|
1569 | 1578 |
|
|
1570 | 1579 | |
|
1571 | 1580 |
|
|
1572 | 1581 |
|
|
1573 | 1582 | |
|
1574 | 1583 |
|
|
1575 | 1584 |
|
|
1576 | 1585 |
|
|
1577 | 1586 |
|
|
1578 | 1587 |
|
|
1579 | 1588 |
|
|
1580 | 1589 |
|
|
1581 | 1590 | |
|
1582 | 1591 |
|
|
1583 | 1592 |
|
|
1584 | 1593 | same as the values in the dictionary. |
|
1585 | 1594 | |
|
1586 | 1595 | This is intended for use by extensions: variables that they've added can |
|
1587 | 1596 | be taken back out if they are unloaded, without removing any that the |
|
1588 | 1597 | user has overwritten. |
|
1589 | 1598 | |
|
1590 | 1599 | Parameters |
|
1591 | 1600 | ---------- |
|
1592 | 1601 | variables : dict |
|
1593 | 1602 | A dictionary mapping object names (as strings) to the objects. |
|
1594 | 1603 | """ |
|
1595 | 1604 |
|
|
1596 | 1605 |
|
|
1597 | 1606 |
|
|
1598 | 1607 |
|
|
1599 | 1608 | |
|
1600 | 1609 |
|
|
1601 | 1610 |
|
|
1602 | 1611 |
|
|
1603 | 1612 | |
|
1604 | 1613 |
|
|
1605 | 1614 |
|
|
1606 | 1615 | |
|
1607 | 1616 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
1608 | 1617 | |
|
1609 | 1618 | Has special code to detect magic functions. |
|
1610 | 1619 | """ |
|
1611 | 1620 |
|
|
1612 | 1621 |
|
|
1613 | 1622 |
|
|
1614 | 1623 |
|
|
1615 | 1624 |
|
|
1616 | 1625 | |
|
1617 | 1626 |
|
|
1618 | 1627 |
|
|
1619 | 1628 |
|
|
1620 | 1629 |
|
|
1621 | 1630 |
|
|
1622 | 1631 |
|
|
1623 | 1632 |
|
|
1624 | 1633 |
|
|
1625 | 1634 | |
|
1626 | 1635 |
|
|
1627 | 1636 |
|
|
1628 | 1637 |
|
|
1629 | 1638 |
|
|
1630 | 1639 |
|
|
1631 | 1640 |
|
|
1632 | 1641 | |
|
1633 | 1642 | |
|
1634 | 1643 |
|
|
1635 | 1644 |
|
|
1636 | 1645 |
|
|
1637 | 1646 |
|
|
1638 | 1647 |
|
|
1639 | 1648 |
|
|
1640 | 1649 |
|
|
1641 | 1650 |
|
|
1642 | 1651 |
|
|
1643 | 1652 |
|
|
1644 | 1653 |
|
|
1645 | 1654 |
|
|
1646 | 1655 |
|
|
1647 | 1656 |
|
|
1648 | 1657 |
|
|
1649 | 1658 |
|
|
1650 | 1659 |
|
|
1651 | 1660 |
|
|
1652 | 1661 |
|
|
1653 | 1662 |
|
|
1654 | 1663 |
|
|
1655 | 1664 |
|
|
1656 | 1665 |
|
|
1657 | 1666 |
|
|
1658 | 1667 |
|
|
1659 | 1668 |
|
|
1660 | 1669 |
|
|
1661 | 1670 |
|
|
1662 | 1671 |
|
|
1663 | 1672 |
|
|
1664 | 1673 |
|
|
1665 | 1674 | |
|
1666 | 1675 |
|
|
1667 | 1676 |
|
|
1668 | 1677 |
|
|
1669 | 1678 |
|
|
1670 | 1679 |
|
|
1671 | 1680 |
|
|
1672 | 1681 |
|
|
1673 | 1682 |
|
|
1674 | 1683 |
|
|
1675 | 1684 |
|
|
1676 | 1685 |
|
|
1677 | 1686 |
|
|
1678 | 1687 |
|
|
1679 | 1688 |
|
|
1680 | 1689 |
|
|
1681 | 1690 |
|
|
1682 | 1691 |
|
|
1683 | 1692 |
|
|
1684 | 1693 |
|
|
1685 | 1694 | |
|
1686 | 1695 |
|
|
1687 | 1696 |
|
|
1688 | 1697 |
|
|
1689 | 1698 |
|
|
1690 | 1699 |
|
|
1691 | 1700 | |
|
1692 | 1701 |
|
|
1693 | 1702 |
|
|
1694 | 1703 |
|
|
1695 | 1704 |
|
|
1696 | 1705 |
|
|
1697 | 1706 |
|
|
1698 | 1707 |
|
|
1699 | 1708 |
|
|
1700 | 1709 | |
|
1701 | 1710 |
|
|
1702 | 1711 |
|
|
1703 | 1712 |
|
|
1704 | 1713 | |
|
1705 | 1714 | If attrname represents a property, return it unevaluated (in case it has |
|
1706 | 1715 | side effects or raises an error. |
|
1707 | 1716 | |
|
1708 | 1717 | """ |
|
1709 | 1718 |
|
|
1710 | 1719 |
|
|
1711 | 1720 |
|
|
1712 | 1721 |
|
|
1713 | 1722 |
|
|
1714 | 1723 |
|
|
1715 | 1724 |
|
|
1716 | 1725 |
|
|
1717 | 1726 |
|
|
1718 | 1727 |
|
|
1719 | 1728 |
|
|
1720 | 1729 |
|
|
1721 | 1730 |
|
|
1722 | 1731 |
|
|
1723 | 1732 |
|
|
1724 | 1733 |
|
|
1725 | 1734 |
|
|
1726 | 1735 |
|
|
1727 | 1736 |
|
|
1728 | 1737 |
|
|
1729 | 1738 |
|
|
1730 | 1739 |
|
|
1731 | 1740 |
|
|
1732 | 1741 |
|
|
1733 | 1742 |
|
|
1734 | 1743 |
|
|
1735 | 1744 |
|
|
1736 | 1745 | |
|
1737 | 1746 |
|
|
1738 | 1747 |
|
|
1739 | 1748 | |
|
1740 | 1749 |
|
|
1741 | 1750 |
|
|
1742 | 1751 |
|
|
1743 | 1752 | |
|
1744 | 1753 |
|
|
1745 | 1754 |
|
|
1746 | 1755 | |
|
1747 | 1756 | This function is meant to be called by pdef, pdoc & friends. |
|
1748 | 1757 | """ |
|
1749 | 1758 |
|
|
1750 | 1759 |
|
|
1751 | 1760 |
|
|
1752 | 1761 |
|
|
1753 | 1762 |
|
|
1754 | 1763 |
|
|
1755 | 1764 |
|
|
1756 | 1765 |
|
|
1757 | 1766 |
|
|
1758 | 1767 |
|
|
1759 | 1768 |
|
|
1760 | 1769 |
|
|
1761 | 1770 |
|
|
1762 | 1771 |
|
|
1763 | 1772 |
|
|
1764 | 1773 |
|
|
1765 | 1774 |
|
|
1766 | 1775 | |
|
1767 | 1776 |
|
|
1768 | 1777 |
|
|
1769 | 1778 |
|
|
1770 | 1779 |
|
|
1771 | 1780 |
|
|
1772 | 1781 |
|
|
1773 | 1782 |
|
|
1774 | 1783 |
|
|
1775 | 1784 |
|
|
1776 | 1785 |
|
|
1777 | 1786 | |
|
1778 | 1787 |
|
|
1779 | 1788 |
|
|
1780 | 1789 |
|
|
1781 | 1790 | |
|
1782 | 1791 |
|
|
1783 | 1792 |
|
|
1784 | 1793 | |
|
1785 | 1794 | A mimebundle is a dictionary, keyed by mime-type. |
|
1786 | 1795 | It must always have the key `'text/plain'`. |
|
1787 | 1796 | """ |
|
1788 | 1797 |
|
|
1789 | 1798 |
|
|
1790 | 1799 |
|
|
1791 | 1800 |
|
|
1792 | 1801 |
|
|
1793 | 1802 |
|
|
1794 | 1803 |
|
|
1795 | 1804 |
|
|
1796 | 1805 | |
|
1797 | 1806 |
|
|
1798 | 1807 |
|
|
1799 | 1808 |
|
|
1800 | 1809 | |
|
1801 | 1810 |
|
|
1802 | 1811 |
|
|
1803 | 1812 |
|
|
1804 | 1813 |
|
|
1805 | 1814 | |
|
1806 | 1815 |
|
|
1807 | 1816 |
|
|
1808 | 1817 |
|
|
1809 | 1818 | |
|
1810 | 1819 |
|
|
1811 | 1820 | |
|
1812 | 1821 |
|
|
1813 | 1822 |
|
|
1814 | 1823 |
|
|
1815 | 1824 | |
|
1816 | 1825 |
|
|
1817 | 1826 |
|
|
1818 | 1827 |
|
|
1819 | 1828 |
|
|
1820 | 1829 |
|
|
1821 | 1830 |
|
|
1822 | 1831 |
|
|
1823 | 1832 |
|
|
1824 | 1833 | |
|
1825 | 1834 |
|
|
1826 | 1835 |
|
|
1827 | 1836 |
|
|
1828 | 1837 |
|
|
1829 | 1838 | |
|
1830 | 1839 |
|
|
1831 | 1840 |
|
|
1832 | 1841 | |
|
1833 | 1842 |
|
|
1834 | 1843 |
|
|
1835 | 1844 | |
|
1836 | 1845 |
|
|
1837 | 1846 |
|
|
1838 | 1847 | |
|
1839 | 1848 | Set a custom exception handler, which will be called if any of the |
|
1840 | 1849 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1841 | 1850 | run_code() method). |
|
1842 | 1851 | |
|
1843 | 1852 | Parameters |
|
1844 | 1853 | ---------- |
|
1845 | 1854 | |
|
1846 | 1855 | exc_tuple : tuple of exception classes |
|
1847 | 1856 | A *tuple* of exception classes, for which to call the defined |
|
1848 | 1857 | handler. It is very important that you use a tuple, and NOT A |
|
1849 | 1858 | LIST here, because of the way Python's except statement works. If |
|
1850 | 1859 | you only want to trap a single exception, use a singleton tuple:: |
|
1851 | 1860 | |
|
1852 | 1861 | exc_tuple == (MyCustomException,) |
|
1853 | 1862 | |
|
1854 | 1863 | handler : callable |
|
1855 | 1864 | handler must have the following signature:: |
|
1856 | 1865 | |
|
1857 | 1866 | def my_handler(self, etype, value, tb, tb_offset=None): |
|
1858 | 1867 | ... |
|
1859 | 1868 | return structured_traceback |
|
1860 | 1869 | |
|
1861 | 1870 | Your handler must return a structured traceback (a list of strings), |
|
1862 | 1871 | or None. |
|
1863 | 1872 | |
|
1864 | 1873 | This will be made into an instance method (via types.MethodType) |
|
1865 | 1874 | of IPython itself, and it will be called if any of the exceptions |
|
1866 | 1875 | listed in the exc_tuple are caught. If the handler is None, an |
|
1867 | 1876 | internal basic one is used, which just prints basic info. |
|
1868 | 1877 | |
|
1869 | 1878 | To protect IPython from crashes, if your handler ever raises an |
|
1870 | 1879 | exception or returns an invalid result, it will be immediately |
|
1871 | 1880 | disabled. |
|
1872 | 1881 | |
|
1873 | 1882 | WARNING: by putting in your own exception handler into IPython's main |
|
1874 | 1883 | execution loop, you run a very good chance of nasty crashes. This |
|
1875 | 1884 | facility should only be used if you really know what you are doing.""" |
|
1876 | 1885 |
|
|
1877 | 1886 |
|
|
1878 | 1887 | |
|
1879 | 1888 |
|
|
1880 | 1889 |
|
|
1881 | 1890 |
|
|
1882 | 1891 |
|
|
1883 | 1892 |
|
|
1884 | 1893 |
|
|
1885 | 1894 |
|
|
1886 | 1895 |
|
|
1887 | 1896 | |
|
1888 | 1897 | return type of CustomTB *should* be a list of strings, but allow |
|
1889 | 1898 | single strings or None, which are harmless. |
|
1890 | 1899 | |
|
1891 | 1900 | This function will *always* return a list of strings, |
|
1892 | 1901 | and will raise a TypeError if stb is inappropriate. |
|
1893 | 1902 | """ |
|
1894 | 1903 |
|
|
1895 | 1904 |
|
|
1896 | 1905 |
|
|
1897 | 1906 |
|
|
1898 | 1907 |
|
|
1899 | 1908 |
|
|
1900 | 1909 |
|
|
1901 | 1910 |
|
|
1902 | 1911 |
|
|
1903 | 1912 |
|
|
1904 | 1913 |
|
|
1905 | 1914 |
|
|
1906 | 1915 |
|
|
1907 | 1916 | |
|
1908 | 1917 |
|
|
1909 | 1918 |
|
|
1910 | 1919 |
|
|
1911 | 1920 |
|
|
1912 | 1921 |
|
|
1913 | 1922 | |
|
1914 | 1923 | This makes it harder (but not impossible) for custom exception |
|
1915 | 1924 | handlers to crash IPython. |
|
1916 | 1925 | """ |
|
1917 | 1926 |
|
|
1918 | 1927 |
|
|
1919 | 1928 |
|
|
1920 | 1929 |
|
|
1921 | 1930 |
|
|
1922 | 1931 |
|
|
1923 | 1932 |
|
|
1924 | 1933 |
|
|
1925 | 1934 |
|
|
1926 | 1935 |
|
|
1927 | 1936 |
|
|
1928 | 1937 |
|
|
1929 | 1938 |
|
|
1930 | 1939 |
|
|
1931 | 1940 |
|
|
1932 | 1941 | |
|
1933 | 1942 |
|
|
1934 | 1943 |
|
|
1935 | 1944 | |
|
1936 | 1945 |
|
|
1937 | 1946 |
|
|
1938 | 1947 | |
|
1939 | 1948 | GUI frameworks like wxPython trap exceptions and call |
|
1940 | 1949 | sys.excepthook themselves. I guess this is a feature that |
|
1941 | 1950 | enables them to keep running after exceptions that would |
|
1942 | 1951 | otherwise kill their mainloop. This is a bother for IPython |
|
1943 | 1952 | which excepts to catch all of the program exceptions with a try: |
|
1944 | 1953 | except: statement. |
|
1945 | 1954 | |
|
1946 | 1955 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1947 | 1956 | any app directly invokes sys.excepthook, it will look to the user like |
|
1948 | 1957 | IPython crashed. In order to work around this, we can disable the |
|
1949 | 1958 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1950 | 1959 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1951 | 1960 | call sys.excepthook will generate a regular-looking exception from |
|
1952 | 1961 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1953 | 1962 | crashes. |
|
1954 | 1963 | |
|
1955 | 1964 | This hook should be used sparingly, only in places which are not likely |
|
1956 | 1965 | to be true IPython errors. |
|
1957 | 1966 | """ |
|
1958 | 1967 |
|
|
1959 | 1968 | |
|
1960 | 1969 |
|
|
1961 | 1970 |
|
|
1962 | 1971 | |
|
1963 | 1972 | Ensures sys.last_type,value,traceback hold the exc_info we found, |
|
1964 | 1973 | from whichever source. |
|
1965 | 1974 | |
|
1966 | 1975 | raises ValueError if none of these contain any information |
|
1967 | 1976 | """ |
|
1968 | 1977 |
|
|
1969 | 1978 |
|
|
1970 | 1979 |
|
|
1971 | 1980 |
|
|
1972 | 1981 | |
|
1973 | 1982 |
|
|
1974 | 1983 |
|
|
1975 | 1984 |
|
|
1976 | 1985 |
|
|
1977 | 1986 |
|
|
1978 | 1987 |
|
|
1979 | 1988 |
|
|
1980 | 1989 |
|
|
1981 | 1990 |
|
|
1982 | 1991 |
|
|
1983 | 1992 |
|
|
1984 | 1993 |
|
|
1985 | 1994 |
|
|
1986 | 1995 |
|
|
1987 | 1996 |
|
|
1988 | 1997 |
|
|
1989 | 1998 |
|
|
1990 | 1999 |
|
|
1991 | 2000 |
|
|
1992 | 2001 |
|
|
1993 | 2002 |
|
|
1994 | 2003 | |
|
1995 | 2004 | These are special exceptions that shouldn't show a traceback. |
|
1996 | 2005 | """ |
|
1997 | 2006 |
|
|
1998 | 2007 |
|
|
1999 | 2008 |
|
|
2000 | 2009 |
|
|
2001 | 2010 | Return as a string (ending with a newline) the exception that |
|
2002 | 2011 | just occurred, without any traceback. |
|
2003 | 2012 | """ |
|
2004 | 2013 |
|
|
2005 | 2014 |
|
|
2006 | 2015 |
|
|
2007 | 2016 | |
|
2008 | 2017 |
|
|
2009 | 2018 |
|
|
2010 | 2019 |
|
|
2011 | 2020 | |
|
2012 | 2021 | If nothing is known about the exception, this is the method which |
|
2013 | 2022 | should be used throughout the code for presenting user tracebacks, |
|
2014 | 2023 | rather than directly invoking the InteractiveTB object. |
|
2015 | 2024 | |
|
2016 | 2025 | A specific showsyntaxerror() also exists, but this method can take |
|
2017 | 2026 | care of calling it if needed, so unless you are explicitly catching a |
|
2018 | 2027 | SyntaxError exception, don't try to analyze the stack manually and |
|
2019 | 2028 | simply call this method.""" |
|
2020 | 2029 | |
|
2021 | 2030 |
|
|
2022 | 2031 |
|
|
2023 | 2032 |
|
|
2024 | 2033 |
|
|
2025 | 2034 |
|
|
2026 | 2035 |
|
|
2027 | 2036 | |
|
2028 | 2037 |
|
|
2029 | 2038 |
|
|
2030 | 2039 |
|
|
2031 | 2040 |
|
|
2032 | 2041 |
|
|
2033 | 2042 |
|
|
2034 | 2043 |
|
|
2035 | 2044 |
|
|
2036 | 2045 |
|
|
2037 | 2046 |
|
|
2038 | 2047 |
|
|
2039 | 2048 |
|
|
2040 | 2049 |
|
|
2041 | 2050 |
|
|
2042 | 2051 |
|
|
2043 | 2052 |
|
|
2044 | 2053 |
|
|
2045 | 2054 |
|
|
2046 | 2055 |
|
|
2047 | 2056 |
|
|
2048 | 2057 |
|
|
2049 | 2058 | |
|
2050 | 2059 |
|
|
2051 | 2060 |
|
|
2052 | 2061 |
|
|
2053 | 2062 |
|
|
2054 | 2063 |
|
|
2055 | 2064 | |
|
2056 | 2065 |
|
|
2057 | 2066 |
|
|
2058 | 2067 | |
|
2059 | 2068 |
|
|
2060 | 2069 |
|
|
2061 | 2070 | |
|
2062 | 2071 |
|
|
2063 | 2072 |
|
|
2064 | 2073 | |
|
2065 | 2074 | Subclasses may override this method to put the traceback on a different |
|
2066 | 2075 | place, like a side channel. |
|
2067 | 2076 | """ |
|
2068 | 2077 |
|
|
2069 | 2078 | |
|
2070 | 2079 |
|
|
2071 | 2080 |
|
|
2072 | 2081 | |
|
2073 | 2082 | This doesn't display a stack trace because there isn't one. |
|
2074 | 2083 | |
|
2075 | 2084 | If a filename is given, it is stuffed in the exception instead |
|
2076 | 2085 | of what was there before (because Python's parser always uses |
|
2077 | 2086 | "<string>" when reading from a string). |
|
2078 | 2087 | |
|
2079 | 2088 | If the syntax error occurred when running a compiled code (i.e. running_compile_code=True), |
|
2080 | 2089 | longer stack trace will be displayed. |
|
2081 | 2090 | """ |
|
2082 | 2091 |
|
|
2083 | 2092 | |
|
2084 | 2093 |
|
|
2085 | 2094 |
|
|
2086 | 2095 |
|
|
2087 | 2096 |
|
|
2088 | 2097 |
|
|
2089 | 2098 |
|
|
2090 | 2099 | |
|
2091 | 2100 |
|
|
2092 | 2101 |
|
|
2093 | 2102 |
|
|
2094 | 2103 |
|
|
2095 | 2104 | |
|
2096 | 2105 |
|
|
2097 | 2106 |
|
|
2098 | 2107 |
|
|
2099 | 2108 |
|
|
2100 | 2109 | at the prompt. |
|
2101 | 2110 | |
|
2102 | 2111 | This is overridden in TerminalInteractiveShell to show a message about |
|
2103 | 2112 | the %paste magic.""" |
|
2104 | 2113 |
|
|
2105 | 2114 | |
|
2106 | 2115 |
|
|
2107 | 2116 |
|
|
2108 | 2117 |
|
|
2109 | 2118 | |
|
2110 | 2119 |
|
|
2111 | 2120 |
|
|
2112 | 2121 | |
|
2113 | 2122 | Moved to terminal subclass, here only to simplify the init logic.""" |
|
2114 | 2123 |
|
|
2115 | 2124 |
|
|
2116 | 2125 |
|
|
2117 | 2126 |
|
|
2118 | 2127 | |
|
2119 | 2128 |
|
|
2120 | 2129 |
|
|
2121 | 2130 |
|
|
2122 | 2131 | |
|
2123 | 2132 | Example:: |
|
2124 | 2133 | |
|
2125 | 2134 | In [1]: _ip.set_next_input("Hello Word") |
|
2126 | 2135 | In [2]: Hello Word_ # cursor is here |
|
2127 | 2136 | """ |
|
2128 | 2137 |
|
|
2129 | 2138 | |
|
2130 | 2139 |
|
|
2131 | 2140 |
|
|
2132 | 2141 |
|
|
2133 | 2142 | |
|
2134 | 2143 |
|
|
2135 | 2144 |
|
|
2136 | 2145 |
|
|
2137 | 2146 | |
|
2138 | 2147 |
|
|
2139 | 2148 |
|
|
2140 | 2149 | |
|
2141 | 2150 | This creates completion machinery that can be used by client code, |
|
2142 | 2151 | either interactively in-process (typically triggered by the readline |
|
2143 | 2152 | library), programmatically (such as in test suites) or out-of-process |
|
2144 | 2153 | (typically over the network by remote frontends). |
|
2145 | 2154 | """ |
|
2146 | 2155 |
|
|
2147 | 2156 |
|
|
2148 | 2157 |
|
|
2149 | 2158 | |
|
2150 | 2159 |
|
|
2151 | 2160 |
|
|
2152 | 2161 |
|
|
2153 | 2162 |
|
|
2154 | 2163 |
|
|
2155 | 2164 |
|
|
2156 | 2165 | |
|
2157 | 2166 |
|
|
2158 | 2167 |
|
|
2159 | 2168 |
|
|
2160 | 2169 |
|
|
2161 | 2170 | |
|
2162 | 2171 |
|
|
2163 | 2172 |
|
|
2164 | 2173 |
|
|
2165 | 2174 |
|
|
2166 | 2175 |
|
|
2167 | 2176 |
|
|
2168 | 2177 | |
|
2169 | 2178 |
|
|
2170 | 2179 |
|
|
2171 | 2180 |
|
|
2172 | 2181 | |
|
2173 | 2182 | Parameters |
|
2174 | 2183 | ---------- |
|
2175 | 2184 | |
|
2176 | 2185 | text : string |
|
2177 | 2186 | A string of text to be completed on. It can be given as empty and |
|
2178 | 2187 | instead a line/position pair are given. In this case, the |
|
2179 | 2188 | completer itself will split the line like readline does. |
|
2180 | 2189 | |
|
2181 | 2190 | line : string, optional |
|
2182 | 2191 | The complete line that text is part of. |
|
2183 | 2192 | |
|
2184 | 2193 | cursor_pos : int, optional |
|
2185 | 2194 | The position of the cursor on the input line. |
|
2186 | 2195 | |
|
2187 | 2196 | Returns |
|
2188 | 2197 | ------- |
|
2189 | 2198 | text : string |
|
2190 | 2199 | The actual text that was completed. |
|
2191 | 2200 | |
|
2192 | 2201 | matches : list |
|
2193 | 2202 | A sorted list with all possible completions. |
|
2194 | 2203 | |
|
2195 | 2204 | The optional arguments allow the completion to take more context into |
|
2196 | 2205 | account, and are part of the low-level completion API. |
|
2197 | 2206 | |
|
2198 | 2207 | This is a wrapper around the completion mechanism, similar to what |
|
2199 | 2208 | readline does at the command line when the TAB key is hit. By |
|
2200 | 2209 | exposing it as a method, it can be used by other non-readline |
|
2201 | 2210 | environments (such as GUIs) for text completion. |
|
2202 | 2211 | |
|
2203 | 2212 | Simple usage example: |
|
2204 | 2213 | |
|
2205 | 2214 | In [1]: x = 'hello' |
|
2206 | 2215 | |
|
2207 | 2216 | In [2]: _ip.complete('x.l') |
|
2208 | 2217 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) |
|
2209 | 2218 | """ |
|
2210 | 2219 | |
|
2211 | 2220 |
|
|
2212 | 2221 |
|
|
2213 | 2222 |
|
|
2214 | 2223 | |
|
2215 | 2224 |
|
|
2216 | 2225 |
|
|
2217 | 2226 | |
|
2218 | 2227 | The position argument (defaults to 0) is the index in the completers |
|
2219 | 2228 | list where you want the completer to be inserted. |
|
2220 | 2229 | |
|
2221 | 2230 | `completer` should have the following signature:: |
|
2222 | 2231 | |
|
2223 | 2232 | def completion(self: Completer, text: string) -> List[str]: |
|
2224 | 2233 | raise NotImplementedError |
|
2225 | 2234 | |
|
2226 | 2235 | It will be bound to the current Completer instance and pass some text |
|
2227 | 2236 | and return a list with current completions to suggest to the user. |
|
2228 | 2237 | """ |
|
2229 | 2238 | |
|
2230 | 2239 |
|
|
2231 | 2240 |
|
|
2232 | 2241 | |
|
2233 | 2242 |
|
|
2234 | 2243 |
|
|
2235 | 2244 |
|
|
2236 | 2245 |
|
|
2237 | 2246 |
|
|
2238 | 2247 |
|
|
2239 | 2248 |
|
|
2240 | 2249 |
|
|
2241 | 2250 | |
|
2242 | 2251 |
|
|
2243 | 2252 |
|
|
2244 | 2253 |
|
|
2245 | 2254 | |
|
2246 | 2255 |
|
|
2247 | 2256 |
|
|
2248 | 2257 |
|
|
2249 | 2258 |
|
|
2250 | 2259 |
|
|
2251 | 2260 |
|
|
2252 | 2261 | |
|
2253 | 2262 |
|
|
2254 | 2263 |
|
|
2255 | 2264 | |
|
2256 | 2265 |
|
|
2257 | 2266 |
|
|
2258 | 2267 |
|
|
2259 | 2268 |
|
|
2260 | 2269 |
|
|
2261 | 2270 |
|
|
2262 | 2271 |
|
|
2263 | 2272 | |
|
2264 | 2273 |
|
|
2265 | 2274 |
|
|
2266 | 2275 |
|
|
2267 | 2276 |
|
|
2268 | 2277 |
|
|
2269 | 2278 |
|
|
2270 | 2279 |
|
|
2271 | 2280 |
|
|
2272 | 2281 |
|
|
2273 | 2282 |
|
|
2274 | 2283 | |
|
2275 | 2284 |
|
|
2276 | 2285 |
|
|
2277 | 2286 |
|
|
2278 | 2287 |
|
|
2279 | 2288 |
|
|
2280 | 2289 |
|
|
2281 | 2290 |
|
|
2282 | 2291 |
|
|
2283 | 2292 |
|
|
2284 | 2293 |
|
|
2285 | 2294 | |
|
2286 | 2295 |
|
|
2287 | 2296 |
|
|
2288 | 2297 | |
|
2289 | 2298 | Parameters |
|
2290 | 2299 | ---------- |
|
2291 | 2300 | magic_name : str |
|
2292 | 2301 | Name of the desired magic function, without '%' prefix. |
|
2293 | 2302 | line : str |
|
2294 | 2303 | The rest of the input line as a single string. |
|
2295 | 2304 | _stack_depth : int |
|
2296 | 2305 | If run_line_magic() is called from magic() then _stack_depth=2. |
|
2297 | 2306 | This is added to ensure backward compatibility for use of 'get_ipython().magic()' |
|
2298 | 2307 | """ |
|
2299 | 2308 |
|
|
2300 | 2309 |
|
|
2301 | 2310 |
|
|
2302 | 2311 |
|
|
2303 | 2312 |
|
|
2304 | 2313 |
|
|
2305 | 2314 |
|
|
2306 | 2315 |
|
|
2307 | 2316 |
|
|
2308 | 2317 |
|
|
2309 | 2318 |
|
|
2310 | 2319 | |
|
2311 | 2320 |
|
|
2312 | 2321 |
|
|
2313 | 2322 |
|
|
2314 | 2323 |
|
|
2315 | 2324 |
|
|
2316 | 2325 |
|
|
2317 | 2326 |
|
|
2318 | 2327 |
|
|
2319 | 2328 |
|
|
2320 | 2329 |
|
|
2321 | 2330 |
|
|
2322 | 2331 |
|
|
2323 | 2332 |
|
|
2324 | 2333 |
|
|
2325 | 2334 |
|
|
2326 | 2335 |
|
|
2327 | 2336 | |
|
2328 | 2337 |
|
|
2329 | 2338 |
|
|
2330 | 2339 | |
|
2331 | 2340 | Parameters |
|
2332 | 2341 | ---------- |
|
2333 | 2342 | stack_depth : int |
|
2334 | 2343 | Depth relative to calling frame |
|
2335 | 2344 | """ |
|
2336 | 2345 |
|
|
2337 | 2346 | |
|
2338 | 2347 |
|
|
2339 | 2348 |
|
|
2340 | 2349 | |
|
2341 | 2350 | Parameters |
|
2342 | 2351 | ---------- |
|
2343 | 2352 | magic_name : str |
|
2344 | 2353 | Name of the desired magic function, without '%' prefix. |
|
2345 | 2354 | line : str |
|
2346 | 2355 | The rest of the first input line as a single string. |
|
2347 | 2356 | cell : str |
|
2348 | 2357 | The body of the cell as a (possibly multiline) string. |
|
2349 | 2358 | """ |
|
2350 | 2359 |
|
|
2351 | 2360 |
|
|
2352 | 2361 |
|
|
2353 | 2362 |
|
|
2354 | 2363 |
|
|
2355 | 2364 |
|
|
2356 | 2365 |
|
|
2357 | 2366 |
|
|
2358 | 2367 |
|
|
2359 | 2368 |
|
|
2360 | 2369 |
|
|
2361 | 2370 |
|
|
2362 | 2371 |
|
|
2363 | 2372 |
|
|
2364 | 2373 |
|
|
2365 | 2374 |
|
|
2366 | 2375 |
|
|
2367 | 2376 |
|
|
2368 | 2377 |
|
|
2369 | 2378 |
|
|
2370 | 2379 |
|
|
2371 | 2380 |
|
|
2372 | 2381 |
|
|
2373 | 2382 |
|
|
2374 | 2383 |
|
|
2375 | 2384 | |
|
2376 | 2385 |
|
|
2377 | 2386 |
|
|
2378 | 2387 |
|
|
2379 | 2388 |
|
|
2380 | 2389 | |
|
2381 | 2390 |
|
|
2382 | 2391 |
|
|
2383 | 2392 | |
|
2384 | 2393 | Returns None if the magic isn't found.""" |
|
2385 | 2394 |
|
|
2386 | 2395 | |
|
2387 | 2396 |
|
|
2388 | 2397 |
|
|
2389 | 2398 | |
|
2390 | 2399 | Returns None if the magic isn't found.""" |
|
2391 | 2400 |
|
|
2392 | 2401 | |
|
2393 | 2402 |
|
|
2394 | 2403 |
|
|
2395 | 2404 | |
|
2396 | 2405 | Returns None if the magic isn't found.""" |
|
2397 | 2406 |
|
|
2398 | 2407 | |
|
2399 | 2408 |
|
|
2400 | 2409 |
|
|
2401 | 2410 | |
|
2402 | 2411 | Call a magic function by name. |
|
2403 | 2412 | |
|
2404 | 2413 | Input: a string containing the name of the magic function to call and |
|
2405 | 2414 | any additional arguments to be passed to the magic. |
|
2406 | 2415 | |
|
2407 | 2416 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
2408 | 2417 | prompt: |
|
2409 | 2418 | |
|
2410 | 2419 | In[1]: %name -opt foo bar |
|
2411 | 2420 | |
|
2412 | 2421 | To call a magic without arguments, simply use magic('name'). |
|
2413 | 2422 | |
|
2414 | 2423 | This provides a proper Python function to call IPython's magics in any |
|
2415 | 2424 | valid Python code you can type at the interpreter, including loops and |
|
2416 | 2425 | compound statements. |
|
2417 | 2426 | """ |
|
2418 | 2427 |
|
|
2419 | 2428 |
|
|
2420 | 2429 |
|
|
2421 | 2430 |
|
|
2422 | 2431 | |
|
2423 | 2432 |
|
|
2424 | 2433 |
|
|
2425 | 2434 |
|
|
2426 | 2435 | |
|
2427 | 2436 |
|
|
2428 | 2437 |
|
|
2429 | 2438 | |
|
2430 | 2439 | Parameters |
|
2431 | 2440 | ---------- |
|
2432 | 2441 | name : str |
|
2433 | 2442 | The name of the macro. |
|
2434 | 2443 | themacro : str or Macro |
|
2435 | 2444 | The action to do upon invoking the macro. If a string, a new |
|
2436 | 2445 | Macro object is created by passing the string to it. |
|
2437 | 2446 | """ |
|
2438 | 2447 | |
|
2439 | 2448 |
|
|
2440 | 2449 | |
|
2441 | 2450 |
|
|
2442 | 2451 |
|
|
2443 | 2452 |
|
|
2444 | 2453 |
|
|
2445 | 2454 |
|
|
2446 | 2455 | |
|
2447 | 2456 |
|
|
2448 | 2457 |
|
|
2449 | 2458 |
|
|
2450 | 2459 | |
|
2451 | 2460 |
|
|
2452 | 2461 |
|
|
2453 | 2462 | |
|
2454 | 2463 | Parameters |
|
2455 | 2464 | ---------- |
|
2456 | 2465 | cmd : str |
|
2457 | 2466 | Command to execute (can not end in '&', as background processes are |
|
2458 | 2467 | not supported. Should not be a command that expects input |
|
2459 | 2468 | other than simple text. |
|
2460 | 2469 | """ |
|
2461 | 2470 |
|
|
2462 | 2471 |
|
|
2463 | 2472 |
|
|
2464 | 2473 |
|
|
2465 | 2474 |
|
|
2466 | 2475 |
|
|
2467 | 2476 |
|
|
2468 | 2477 | |
|
2469 | 2478 |
|
|
2470 | 2479 |
|
|
2471 | 2480 |
|
|
2472 | 2481 |
|
|
2473 | 2482 | |
|
2474 | 2483 |
|
|
2475 | 2484 |
|
|
2476 | 2485 | subprocess.call using the system shell on other platforms. |
|
2477 | 2486 | |
|
2478 | 2487 | Parameters |
|
2479 | 2488 | ---------- |
|
2480 | 2489 | cmd : str |
|
2481 | 2490 | Command to execute. |
|
2482 | 2491 | """ |
|
2483 | 2492 |
|
|
2484 | 2493 |
|
|
2485 | 2494 |
|
|
2486 | 2495 |
|
|
2487 | 2496 |
|
|
2488 | 2497 |
|
|
2489 | 2498 |
|
|
2490 | 2499 |
|
|
2491 | 2500 |
|
|
2492 | 2501 |
|
|
2493 | 2502 |
|
|
2494 | 2503 |
|
|
2495 | 2504 |
|
|
2496 | 2505 |
|
|
2497 | 2506 |
|
|
2498 | 2507 |
|
|
2499 | 2508 |
|
|
2500 | 2509 |
|
|
2501 | 2510 |
|
|
2502 | 2511 |
|
|
2503 | 2512 |
|
|
2504 | 2513 |
|
|
2505 | 2514 |
|
|
2506 | 2515 |
|
|
2507 | 2516 |
|
|
2508 | 2517 |
|
|
2509 | 2518 |
|
|
2510 | 2519 |
|
|
2511 | 2520 |
|
|
2512 | 2521 |
|
|
2513 | 2522 |
|
|
2514 | 2523 |
|
|
2515 | 2524 |
|
|
2516 | 2525 |
|
|
2517 | 2526 |
|
|
2518 | 2527 |
|
|
2519 | 2528 |
|
|
2520 | 2529 |
|
|
2521 | 2530 |
|
|
2522 | 2531 | |
|
2523 | 2532 |
|
|
2524 | 2533 |
|
|
2525 | 2534 | |
|
2526 | 2535 |
|
|
2527 | 2536 |
|
|
2528 | 2537 | |
|
2529 | 2538 | Parameters |
|
2530 | 2539 | ---------- |
|
2531 | 2540 | cmd : str |
|
2532 | 2541 | Command to execute (can not end in '&', as background processes are |
|
2533 | 2542 | not supported. |
|
2534 | 2543 | split : bool, optional |
|
2535 | 2544 | If True, split the output into an IPython SList. Otherwise, an |
|
2536 | 2545 | IPython LSString is returned. These are objects similar to normal |
|
2537 | 2546 | lists and strings, with a few convenience attributes for easier |
|
2538 | 2547 | manipulation of line-based output. You can use '?' on them for |
|
2539 | 2548 | details. |
|
2540 | 2549 | depth : int, optional |
|
2541 | 2550 | How many frames above the caller are the local variables which should |
|
2542 | 2551 | be expanded in the command string? The default (0) assumes that the |
|
2543 | 2552 | expansion variables are in the stack frame calling this function. |
|
2544 | 2553 | """ |
|
2545 | 2554 |
|
|
2546 | 2555 |
|
|
2547 | 2556 |
|
|
2548 | 2557 |
|
|
2549 | 2558 |
|
|
2550 | 2559 |
|
|
2551 | 2560 |
|
|
2552 | 2561 |
|
|
2553 | 2562 |
|
|
2554 | 2563 | |
|
2555 | 2564 |
|
|
2556 | 2565 |
|
|
2557 | 2566 |
|
|
2558 | 2567 | |
|
2559 | 2568 |
|
|
2560 | 2569 |
|
|
2561 | 2570 |
|
|
2562 | 2571 | |
|
2563 | 2572 |
|
|
2564 | 2573 |
|
|
2565 | 2574 |
|
|
2566 | 2575 | |
|
2567 | 2576 |
|
|
2568 | 2577 |
|
|
2569 | 2578 |
|
|
2570 | 2579 | |
|
2571 | 2580 |
|
|
2572 | 2581 |
|
|
2573 | 2582 |
|
|
2574 | 2583 | |
|
2575 | 2584 |
|
|
2576 | 2585 |
|
|
2577 | 2586 |
|
|
2578 | 2587 |
|
|
2579 | 2588 |
|
|
2580 | 2589 |
|
|
2581 | 2590 |
|
|
2582 | 2591 | |
|
2583 | 2592 |
|
|
2584 | 2593 |
|
|
2585 | 2594 |
|
|
2586 | 2595 |
|
|
2587 | 2596 |
|
|
2588 | 2597 |
|
|
2589 | 2598 |
|
|
2590 | 2599 | |
|
2591 | 2600 |
|
|
2592 | 2601 |
|
|
2593 | 2602 | |
|
2594 | 2603 | This shows visual feedback by rewriting input lines that cause |
|
2595 | 2604 | automatic calling to kick in, like:: |
|
2596 | 2605 | |
|
2597 | 2606 | /f x |
|
2598 | 2607 | |
|
2599 | 2608 | into:: |
|
2600 | 2609 | |
|
2601 | 2610 | ------> f(x) |
|
2602 | 2611 | |
|
2603 | 2612 | after the user's input prompt. This helps the user understand that the |
|
2604 | 2613 | input line was transformed automatically by IPython. |
|
2605 | 2614 | """ |
|
2606 | 2615 |
|
|
2607 | 2616 |
|
|
2608 | 2617 | |
|
2609 | 2618 |
|
|
2610 | 2619 |
|
|
2611 | 2620 | |
|
2612 | 2621 |
|
|
2613 | 2622 |
|
|
2614 | 2623 |
|
|
2615 | 2624 | |
|
2616 | 2625 |
|
|
2617 | 2626 |
|
|
2618 | 2627 | |
|
2619 | 2628 | for use in user_expressions |
|
2620 | 2629 | """ |
|
2621 | 2630 |
|
|
2622 | 2631 |
|
|
2623 | 2632 |
|
|
2624 | 2633 |
|
|
2625 | 2634 |
|
|
2626 | 2635 |
|
|
2627 | 2636 |
|
|
2628 | 2637 |
|
|
2629 | 2638 |
|
|
2630 | 2639 |
|
|
2631 | 2640 | |
|
2632 | 2641 |
|
|
2633 | 2642 |
|
|
2634 | 2643 |
|
|
2635 | 2644 |
|
|
2636 | 2645 | |
|
2637 | 2646 | for use in user_expressions |
|
2638 | 2647 | """ |
|
2639 | 2648 |
|
|
2640 | 2649 |
|
|
2641 | 2650 |
|
|
2642 | 2651 |
|
|
2643 | 2652 |
|
|
2644 | 2653 |
|
|
2645 | 2654 |
|
|
2646 | 2655 |
|
|
2647 | 2656 |
|
|
2648 | 2657 |
|
|
2649 | 2658 |
|
|
2650 | 2659 | |
|
2651 | 2660 | Parameters |
|
2652 | 2661 | ---------- |
|
2653 | 2662 | expressions : dict |
|
2654 | 2663 | A dict with string keys and string values. The expression values |
|
2655 | 2664 | should be valid Python expressions, each of which will be evaluated |
|
2656 | 2665 | in the user namespace. |
|
2657 | 2666 | |
|
2658 | 2667 | Returns |
|
2659 | 2668 | ------- |
|
2660 | 2669 | A dict, keyed like the input expressions dict, with the rich mime-typed |
|
2661 | 2670 | display_data of each value. |
|
2662 | 2671 | """ |
|
2663 | 2672 |
|
|
2664 | 2673 |
|
|
2665 | 2674 |
|
|
2666 | 2675 |
|
|
2667 | 2676 |
|
|
2668 | 2677 |
|
|
2669 | 2678 |
|
|
2670 | 2679 |
|
|
2671 | 2680 |
|
|
2672 | 2681 |
|
|
2673 | 2682 |
|
|
2674 | 2683 | |
|
2675 | 2684 |
|
|
2676 | 2685 |
|
|
2677 | 2686 |
|
|
2678 | 2687 | |
|
2679 | 2688 |
|
|
2680 | 2689 |
|
|
2681 | 2690 |
|
|
2682 | 2691 |
|
|
2683 | 2692 | |
|
2684 | 2693 |
|
|
2685 | 2694 |
|
|
2686 | 2695 | |
|
2687 | 2696 | Returns the result of evaluation |
|
2688 | 2697 | """ |
|
2689 | 2698 |
|
|
2690 | 2699 |
|
|
2691 | 2700 | |
|
2692 | 2701 |
|
|
2693 | 2702 |
|
|
2694 | 2703 | |
|
2695 | 2704 | This version will never throw an exception, but instead print |
|
2696 | 2705 | helpful error messages to the screen. This only works on pure |
|
2697 | 2706 | Python files with the .py extension. |
|
2698 | 2707 | |
|
2699 | 2708 | Parameters |
|
2700 | 2709 | ---------- |
|
2701 | 2710 | fname : string |
|
2702 | 2711 | The name of the file to be executed. |
|
2703 | 2712 | where : tuple |
|
2704 | 2713 | One or two namespaces, passed to execfile() as (globals,locals). |
|
2705 | 2714 | If only one is given, it is passed as both. |
|
2706 | 2715 | exit_ignore : bool (False) |
|
2707 | 2716 | If True, then silence SystemExit for non-zero status (it is always |
|
2708 | 2717 | silenced for zero status, as it is so common). |
|
2709 | 2718 | raise_exceptions : bool (False) |
|
2710 | 2719 | If True raise exceptions everywhere. Meant for testing. |
|
2711 | 2720 | shell_futures : bool (False) |
|
2712 | 2721 | If True, the code will share future statements with the interactive |
|
2713 | 2722 | shell. It will both be affected by previous __future__ imports, and |
|
2714 | 2723 | any __future__ imports in the code will affect the shell. If False, |
|
2715 | 2724 | __future__ imports are not shared in either direction. |
|
2716 | 2725 | |
|
2717 | 2726 | """ |
|
2718 | 2727 |
|
|
2719 | 2728 | |
|
2720 | 2729 |
|
|
2721 | 2730 |
|
|
2722 | 2731 |
|
|
2723 | 2732 |
|
|
2724 | 2733 |
|
|
2725 | 2734 |
|
|
2726 | 2735 |
|
|
2727 | 2736 | |
|
2728 | 2737 |
|
|
2729 | 2738 |
|
|
2730 | 2739 |
|
|
2731 | 2740 |
|
|
2732 | 2741 | |
|
2733 | 2742 |
|
|
2734 | 2743 |
|
|
2735 | 2744 |
|
|
2736 | 2745 |
|
|
2737 | 2746 |
|
|
2738 | 2747 |
|
|
2739 | 2748 |
|
|
2740 | 2749 |
|
|
2741 | 2750 |
|
|
2742 | 2751 |
|
|
2743 | 2752 |
|
|
2744 | 2753 |
|
|
2745 | 2754 |
|
|
2746 | 2755 |
|
|
2747 | 2756 |
|
|
2748 | 2757 |
|
|
2749 | 2758 |
|
|
2750 | 2759 |
|
|
2751 | 2760 |
|
|
2752 | 2761 |
|
|
2753 | 2762 |
|
|
2754 | 2763 |
|
|
2755 | 2764 |
|
|
2756 | 2765 |
|
|
2757 | 2766 |
|
|
2758 | 2767 |
|
|
2759 | 2768 | |
|
2760 | 2769 |
|
|
2761 | 2770 |
|
|
2762 | 2771 | |
|
2763 | 2772 | Parameters |
|
2764 | 2773 | ---------- |
|
2765 | 2774 | fname : str |
|
2766 | 2775 | The name of the file to execute. The filename must have a |
|
2767 | 2776 | .ipy or .ipynb extension. |
|
2768 | 2777 | shell_futures : bool (False) |
|
2769 | 2778 | If True, the code will share future statements with the interactive |
|
2770 | 2779 | shell. It will both be affected by previous __future__ imports, and |
|
2771 | 2780 | any __future__ imports in the code will affect the shell. If False, |
|
2772 | 2781 | __future__ imports are not shared in either direction. |
|
2773 | 2782 | raise_exceptions : bool (False) |
|
2774 | 2783 | If True raise exceptions everywhere. Meant for testing. |
|
2775 | 2784 | """ |
|
2776 | 2785 |
|
|
2777 | 2786 | |
|
2778 | 2787 |
|
|
2779 | 2788 |
|
|
2780 | 2789 |
|
|
2781 | 2790 |
|
|
2782 | 2791 |
|
|
2783 | 2792 |
|
|
2784 | 2793 |
|
|
2785 | 2794 | |
|
2786 | 2795 |
|
|
2787 | 2796 |
|
|
2788 | 2797 |
|
|
2789 | 2798 |
|
|
2790 | 2799 | |
|
2791 | 2800 |
|
|
2792 | 2801 |
|
|
2793 | 2802 |
|
|
2794 | 2803 |
|
|
2795 | 2804 |
|
|
2796 | 2805 |
|
|
2797 | 2806 |
|
|
2798 | 2807 |
|
|
2799 | 2808 |
|
|
2800 | 2809 |
|
|
2801 | 2810 |
|
|
2802 | 2811 |
|
|
2803 | 2812 | |
|
2804 | 2813 |
|
|
2805 | 2814 |
|
|
2806 | 2815 |
|
|
2807 | 2816 |
|
|
2808 | 2817 |
|
|
2809 | 2818 |
|
|
2810 | 2819 |
|
|
2811 | 2820 |
|
|
2812 | 2821 |
|
|
2813 | 2822 |
|
|
2814 | 2823 |
|
|
2815 | 2824 |
|
|
2816 | 2825 |
|
|
2817 | 2826 | |
|
2818 | 2827 |
|
|
2819 | 2828 |
|
|
2820 | 2829 | |
|
2821 | 2830 | This version will never throw an exception, but instead print |
|
2822 | 2831 | helpful error messages to the screen. |
|
2823 | 2832 | |
|
2824 | 2833 | `SystemExit` exceptions with status code 0 or None are ignored. |
|
2825 | 2834 | |
|
2826 | 2835 | Parameters |
|
2827 | 2836 | ---------- |
|
2828 | 2837 | mod_name : string |
|
2829 | 2838 | The name of the module to be executed. |
|
2830 | 2839 | where : dict |
|
2831 | 2840 | The globals namespace. |
|
2832 | 2841 | """ |
|
2833 | 2842 |
|
|
2834 | 2843 |
|
|
2835 | 2844 |
|
|
2836 | 2845 |
|
|
2837 | 2846 |
|
|
2838 | 2847 |
|
|
2839 | 2848 |
|
|
2840 | 2849 |
|
|
2841 | 2850 |
|
|
2842 | 2851 |
|
|
2843 | 2852 |
|
|
2844 | 2853 |
|
|
2845 | 2854 | |
|
2846 | 2855 |
|
|
2847 | 2856 |
|
|
2848 | 2857 | |
|
2849 | 2858 | Parameters |
|
2850 | 2859 | ---------- |
|
2851 | 2860 | raw_cell : str |
|
2852 | 2861 | The code (including IPython code such as %magic functions) to run. |
|
2853 | 2862 | store_history : bool |
|
2854 | 2863 | If True, the raw and translated cell will be stored in IPython's |
|
2855 | 2864 | history. For user code calling back into IPython's machinery, this |
|
2856 | 2865 | should be set to False. |
|
2857 | 2866 | silent : bool |
|
2858 | 2867 | If True, avoid side-effects, such as implicit displayhooks and |
|
2859 | 2868 | and logging. silent=True forces store_history=False. |
|
2860 | 2869 | shell_futures : bool |
|
2861 | 2870 | If True, the code will share future statements with the interactive |
|
2862 | 2871 | shell. It will both be affected by previous __future__ imports, and |
|
2863 | 2872 | any __future__ imports in the code will affect the shell. If False, |
|
2864 | 2873 | __future__ imports are not shared in either direction. |
|
2865 | 2874 | |
|
2866 | 2875 | Returns |
|
2867 | 2876 | ------- |
|
2868 | 2877 | result : :class:`ExecutionResult` |
|
2869 | 2878 | """ |
|
2870 | 2879 |
|
|
2871 | 2880 |
|
|
2872 | 2881 |
|
|
2873 | 2882 |
|
|
2874 | 2883 |
|
|
2875 | 2884 |
|
|
2876 | 2885 |
|
|
2877 | 2886 |
|
|
2878 | 2887 |
|
|
2879 | 2888 | |
|
2880 | 2889 |
|
|
2881 | 2890 |
|
|
2882 | 2891 | |
|
2883 | 2892 |
|
|
2884 | 2893 |
|
|
2885 | 2894 |
|
|
2886 | 2895 |
|
|
2887 | 2896 |
|
|
2888 | 2897 |
|
|
2889 | 2898 |
|
|
2890 | 2899 |
|
|
2891 | 2900 | |
|
2892 | 2901 |
|
|
2893 | 2902 |
|
|
2894 | 2903 |
|
|
2895 | 2904 |
|
|
2896 | 2905 |
|
|
2897 | 2906 |
|
|
2898 | 2907 |
|
|
2899 | 2908 |
|
|
2900 | 2909 |
|
|
2901 | 2910 | |
|
2902 | 2911 |
|
|
2903 | 2912 |
|
|
2904 | 2913 |
|
|
2905 | 2914 |
|
|
2906 | 2915 |
|
|
2907 | 2916 |
|
|
2908 | 2917 |
|
|
2909 | 2918 |
|
|
2910 | 2919 |
|
|
2911 | 2920 |
|
|
2912 | 2921 |
|
|
2913 | 2922 |
|
|
2914 | 2923 |
|
|
2915 | 2924 |
|
|
2916 | 2925 | |
|
2917 | 2926 |
|
|
2918 | 2927 |
|
|
2919 | 2928 |
|
|
2920 | 2929 |
|
|
2921 | 2930 |
|
|
2922 | 2931 |
|
|
2923 | 2932 |
|
|
2924 | 2933 |
|
|
2925 | 2934 | |
|
2926 | 2935 |
|
|
2927 | 2936 |
|
|
2928 | 2937 |
|
|
2929 | 2938 |
|
|
2930 | 2939 | |
|
2931 | 2940 | Parameters |
|
2932 | 2941 | ---------- |
|
2933 | 2942 | raw_cell: str |
|
2934 | 2943 | The code to be executed |
|
2935 | 2944 | |
|
2936 | 2945 | Returns |
|
2937 | 2946 | ------- |
|
2938 | 2947 | result: bool |
|
2939 | 2948 | Whether the code needs to be run with a coroutine runner or not |
|
2940 | 2949 | |
|
2941 | 2950 | .. versionadded: 7.0 |
|
2942 | 2951 | """ |
|
2943 | 2952 |
|
|
2944 | 2953 |
|
|
2945 | 2954 |
|
|
2946 | 2955 |
|
|
2947 | 2956 |
|
|
2948 | 2957 |
|
|
2949 | 2958 |
|
|
2950 | 2959 |
|
|
2951 | 2960 |
|
|
2952 | 2961 |
|
|
2953 | 2962 |
|
|
2954 | 2963 |
|
|
2955 | 2964 |
|
|
2956 | 2965 |
|
|
2957 | 2966 |
|
|
2958 | 2967 |
|
|
2959 | 2968 |
|
|
2960 | 2969 |
|
|
2961 | 2970 |
|
|
2962 | 2971 |
|
|
2963 | 2972 |
|
|
2964 | 2973 |
|
|
2965 | 2974 |
|
|
2966 | 2975 |
|
|
2967 | 2976 |
|
|
2968 | 2977 | |
|
2969 | 2978 |
|
|
2970 | 2979 |
|
|
2971 | 2980 |
|
|
2972 | 2981 |
|
|
2973 | 2982 |
|
|
2974 | 2983 |
|
|
2975 | 2984 |
|
|
2976 | 2985 |
|
|
2977 | 2986 |
|
|
2978 | 2987 |
|
|
2979 | 2988 |
|
|
2980 | 2989 | |
|
2981 | 2990 | Parameters |
|
2982 | 2991 | ---------- |
|
2983 | 2992 | raw_cell : str |
|
2984 | 2993 | The code (including IPython code such as %magic functions) to run. |
|
2985 | 2994 | store_history : bool |
|
2986 | 2995 | If True, the raw and translated cell will be stored in IPython's |
|
2987 | 2996 | history. For user code calling back into IPython's machinery, this |
|
2988 | 2997 | should be set to False. |
|
2989 | 2998 | silent : bool |
|
2990 | 2999 | If True, avoid side-effects, such as implicit displayhooks and |
|
2991 | 3000 | and logging. silent=True forces store_history=False. |
|
2992 | 3001 | shell_futures : bool |
|
2993 | 3002 | If True, the code will share future statements with the interactive |
|
2994 | 3003 | shell. It will both be affected by previous __future__ imports, and |
|
2995 | 3004 | any __future__ imports in the code will affect the shell. If False, |
|
2996 | 3005 | __future__ imports are not shared in either direction. |
|
2997 | 3006 | transformed_cell: str |
|
2998 | 3007 | cell that was passed through transformers |
|
2999 | 3008 | preprocessing_exc_tuple: |
|
3000 | 3009 | trace if the transformation failed. |
|
3001 | 3010 | |
|
3002 | 3011 | Returns |
|
3003 | 3012 | ------- |
|
3004 | 3013 | result : :class:`ExecutionResult` |
|
3005 | 3014 | |
|
3006 | 3015 | .. versionadded: 7.0 |
|
3007 | 3016 | """ |
|
3008 | 3017 |
|
|
3009 | 3018 |
|
|
3010 | 3019 |
|
|
3011 | 3020 | |
|
3012 | 3021 |
|
|
3013 | 3022 |
|
|
3014 | 3023 |
|
|
3015 | 3024 |
|
|
3016 | 3025 | |
|
3017 | 3026 |
|
|
3018 | 3027 |
|
|
3019 | 3028 | |
|
3020 | 3029 |
|
|
3021 | 3030 |
|
|
3022 | 3031 | |
|
3023 | 3032 |
|
|
3024 | 3033 |
|
|
3025 | 3034 |
|
|
3026 | 3035 |
|
|
3027 | 3036 |
|
|
3028 | 3037 |
|
|
3029 | 3038 |
|
|
3030 | 3039 | |
|
3031 | 3040 |
|
|
3032 | 3041 |
|
|
3033 | 3042 |
|
|
3034 | 3043 | |
|
3035 | 3044 |
|
|
3036 | 3045 |
|
|
3037 | 3046 |
|
|
3038 | 3047 |
|
|
3039 | 3048 |
|
|
3040 | 3049 |
|
|
3041 | 3050 |
|
|
3042 | 3051 |
|
|
3043 | 3052 |
|
|
3044 | 3053 |
|
|
3045 | 3054 |
|
|
3046 | 3055 |
|
|
3047 | 3056 |
|
|
3048 | 3057 |
|
|
3049 | 3058 |
|
|
3050 | 3059 |
|
|
3051 | 3060 |
|
|
3052 | 3061 |
|
|
3053 | 3062 |
|
|
3054 | 3063 |
|
|
3055 | 3064 |
|
|
3056 | 3065 |
|
|
3057 | 3066 |
|
|
3058 | 3067 |
|
|
3059 | 3068 |
|
|
3060 | 3069 |
|
|
3061 | 3070 |
|
|
3062 | 3071 | |
|
3063 | 3072 |
|
|
3064 | 3073 |
|
|
3065 | 3074 |
|
|
3066 | 3075 |
|
|
3067 | 3076 |
|
|
3068 | 3077 |
|
|
3069 | 3078 | |
|
3070 | 3079 |
|
|
3071 | 3080 |
|
|
3072 | 3081 |
|
|
3073 | 3082 |
|
|
3074 | 3083 |
|
|
3075 | 3084 |
|
|
3076 | 3085 | |
|
3077 | 3086 |
|
|
3078 | 3087 |
|
|
3079 | 3088 |
|
|
3080 | 3089 |
|
|
3081 | 3090 | |
|
3082 | 3091 |
|
|
3083 | 3092 | |
|
3084 | 3093 |
|
|
3085 | 3094 |
|
|
3086 | 3095 | |
|
3087 | 3096 |
|
|
3088 | 3097 |
|
|
3089 | 3098 |
|
|
3090 | 3099 |
|
|
3091 | 3100 |
|
|
3092 | 3101 |
|
|
3093 | 3102 |
|
|
3094 | 3103 |
|
|
3095 | 3104 |
|
|
3096 | 3105 |
|
|
3097 | 3106 |
|
|
3098 | 3107 |
|
|
3099 | 3108 |
|
|
3100 | 3109 |
|
|
3101 | 3110 |
|
|
3102 | 3111 |
|
|
3103 | 3112 |
|
|
3104 | 3113 |
|
|
3105 | 3114 |
|
|
3106 | 3115 |
|
|
3107 | 3116 |
|
|
3108 | 3117 |
|
|
3109 | 3118 |
|
|
3110 | 3119 |
|
|
3111 | 3120 |
|
|
3112 | 3121 |
|
|
3113 | 3122 |
|
|
3114 | 3123 |
|
|
3115 | 3124 |
|
|
3116 | 3125 |
|
|
3117 | 3126 |
|
|
3118 | 3127 |
|
|
3119 | 3128 |
|
|
3120 | 3129 |
|
|
3121 | 3130 |
|
|
3122 | 3131 |
|
|
3123 | 3132 | |
|
3124 | 3133 |
|
|
3125 | 3134 |
|
|
3126 | 3135 |
|
|
3127 | 3136 |
|
|
3128 | 3137 |
|
|
3129 | 3138 |
|
|
3130 | 3139 | |
|
3131 | 3140 |
|
|
3132 | 3141 |
|
|
3133 | 3142 |
|
|
3134 | 3143 | |
|
3135 | 3144 |
|
|
3136 | 3145 |
|
|
3137 | 3146 |
|
|
3138 | 3147 |
|
|
3139 | 3148 | |
|
3140 | 3149 |
|
|
3141 | 3150 |
|
|
3142 | 3151 | |
|
3143 | 3152 |
|
|
3144 | 3153 |
|
|
3145 | 3154 | |
|
3146 | 3155 |
|
|
3147 | 3156 |
|
|
3148 | 3157 |
|
|
3149 | 3158 | |
|
3150 | 3159 |
|
|
3151 | 3160 |
|
|
3152 | 3161 |
|
|
3153 | 3162 |
|
|
3154 | 3163 |
|
|
3155 | 3164 |
|
|
3156 | 3165 | |
|
3157 | 3166 |
|
|
3158 | 3167 | |
|
3159 | 3168 |
|
|
3160 | 3169 |
|
|
3161 | 3170 | |
|
3162 | 3171 | Static transformations, implemented in IPython.core.inputtransformer2, |
|
3163 | 3172 | deal with things like ``%magic`` and ``!system`` commands. |
|
3164 | 3173 | These run on all input. |
|
3165 | 3174 | Dynamic transformations, for things like unescaped magics and the exit |
|
3166 | 3175 | autocall, depend on the state of the interpreter. |
|
3167 | 3176 | These only apply to single line inputs. |
|
3168 | 3177 | |
|
3169 | 3178 | These string-based transformations are followed by AST transformations; |
|
3170 | 3179 | see :meth:`transform_ast`. |
|
3171 | 3180 | """ |
|
3172 | 3181 |
|
|
3173 | 3182 |
|
|
3174 | 3183 | |
|
3175 | 3184 |
|
|
3176 | 3185 |
|
|
3177 | 3186 |
|
|
3178 | 3187 |
|
|
3179 | 3188 |
|
|
3180 | 3189 |
|
|
3181 | 3190 | |
|
3182 | 3191 |
|
|
3183 | 3192 |
|
|
3184 | 3193 |
|
|
3185 | 3194 |
|
|
3186 | 3195 | |
|
3187 | 3196 |
|
|
3188 | 3197 | |
|
3189 | 3198 |
|
|
3190 | 3199 |
|
|
3191 | 3200 | |
|
3192 | 3201 | Parameters |
|
3193 | 3202 | ---------- |
|
3194 | 3203 | node : ast.Node |
|
3195 | 3204 | The root node to be transformed. Typically called with the ast.Module |
|
3196 | 3205 | produced by parsing user input. |
|
3197 | 3206 | |
|
3198 | 3207 | Returns |
|
3199 | 3208 | ------- |
|
3200 | 3209 | An ast.Node corresponding to the node it was called with. Note that it |
|
3201 | 3210 | may also modify the passed object, so don't rely on references to the |
|
3202 | 3211 | original AST. |
|
3203 | 3212 | """ |
|
3204 | 3213 |
|
|
3205 | 3214 |
|
|
3206 | 3215 |
|
|
3207 | 3216 |
|
|
3208 | 3217 |
|
|
3209 | 3218 |
|
|
3210 | 3219 |
|
|
3211 | 3220 |
|
|
3212 | 3221 |
|
|
3213 | 3222 |
|
|
3214 | 3223 |
|
|
3215 | 3224 | |
|
3216 | 3225 |
|
|
3217 | 3226 |
|
|
3218 | 3227 |
|
|
3219 | 3228 | |
|
3220 | 3229 |
|
|
3221 | 3230 |
|
|
3222 | 3231 |
|
|
3223 | 3232 | interactivity parameter. |
|
3224 | 3233 | |
|
3225 | 3234 | Parameters |
|
3226 | 3235 | ---------- |
|
3227 | 3236 | nodelist : list |
|
3228 | 3237 | A sequence of AST nodes to run. |
|
3229 | 3238 | cell_name : str |
|
3230 | 3239 | Will be passed to the compiler as the filename of the cell. Typically |
|
3231 | 3240 | the value returned by ip.compile.cache(cell). |
|
3232 | 3241 | interactivity : str |
|
3233 | 3242 | 'all', 'last', 'last_expr' , 'last_expr_or_assign' or 'none', |
|
3234 | 3243 | specifying which nodes should be run interactively (displaying output |
|
3235 | 3244 | from expressions). 'last_expr' will run the last node interactively |
|
3236 | 3245 | only if it is an expression (i.e. expressions in loops or other blocks |
|
3237 | 3246 | are not displayed) 'last_expr_or_assign' will run the last expression |
|
3238 | 3247 | or the last assignment. Other values for this parameter will raise a |
|
3239 | 3248 | ValueError. |
|
3240 | 3249 | |
|
3241 | 3250 | Experimental value: 'async' Will try to run top level interactive |
|
3242 | 3251 | async/await code in default runner, this will not respect the |
|
3243 | 3252 | interactivity setting and will only run the last node if it is an |
|
3244 | 3253 | expression. |
|
3245 | 3254 | |
|
3246 | 3255 | compiler : callable |
|
3247 | 3256 | A function with the same interface as the built-in compile(), to turn |
|
3248 | 3257 | the AST nodes into code objects. Default is the built-in compile(). |
|
3249 | 3258 | result : ExecutionResult, optional |
|
3250 | 3259 | An object to store exceptions that occur during execution. |
|
3251 | 3260 | |
|
3252 | 3261 | Returns |
|
3253 | 3262 | ------- |
|
3254 | 3263 | True if an exception occurred while running code, False if it finished |
|
3255 | 3264 | running. |
|
3256 | 3265 | """ |
|
3257 | 3266 |
|
|
3258 | 3267 |
|
|
3259 | 3268 | |
|
3260 | 3269 |
|
|
3261 | 3270 |
|
|
3262 | 3271 |
|
|
3263 | 3272 |
|
|
3264 | 3273 |
|
|
3265 | 3274 |
|
|
3266 | 3275 |
|
|
3267 | 3276 |
|
|
3268 | 3277 |
|
|
3269 | 3278 |
|
|
3270 | 3279 |
|
|
3271 | 3280 |
|
|
3272 | 3281 |
|
|
3273 | 3282 |
|
|
3274 | 3283 | |
|
3275 | 3284 |
|
|
3276 | 3285 |
|
|
3277 | 3286 |
|
|
3278 | 3287 |
|
|
3279 | 3288 |
|
|
3280 | 3289 |
|
|
3281 | 3290 | |
|
3282 | 3291 |
|
|
3283 | 3292 |
|
|
3284 | 3293 |
|
|
3285 | 3294 |
|
|
3286 | 3295 |
|
|
3287 | 3296 |
|
|
3288 | 3297 |
|
|
3289 | 3298 |
|
|
3290 | 3299 |
|
|
3291 | 3300 |
|
|
3292 | 3301 |
|
|
3293 | 3302 | |
|
3294 | 3303 |
|
|
3295 | 3304 |
|
|
3296 | 3305 |
|
|
3297 | 3306 |
|
|
3298 | 3307 |
|
|
3299 | 3308 |
|
|
3300 | 3309 |
|
|
3301 | 3310 |
|
|
3302 | 3311 |
|
|
3303 | 3312 |
|
|
3304 | 3313 |
|
|
3305 | 3314 |
|
|
3306 | 3315 |
|
|
3307 | 3316 |
|
|
3308 | 3317 |
|
|
3309 | 3318 |
|
|
3310 | 3319 |
|
|
3311 | 3320 |
|
|
3312 | 3321 |
|
|
3313 | 3322 |
|
|
3314 | 3323 |
|
|
3315 | 3324 | |
|
3316 | 3325 |
|
|
3317 | 3326 |
|
|
3318 | 3327 |
|
|
3319 | 3328 |
|
|
3320 | 3329 | |
|
3321 | 3330 |
|
|
3322 | 3331 |
|
|
3323 | 3332 | |
|
3324 | 3333 |
|
|
3325 | 3334 |
|
|
3326 | 3335 |
|
|
3327 | 3336 |
|
|
3328 | 3337 |
|
|
3329 | 3338 |
|
|
3330 | 3339 |
|
|
3331 | 3340 |
|
|
3332 | 3341 |
|
|
3333 | 3342 |
|
|
3334 | 3343 | |
|
3335 | 3344 |
|
|
3336 | 3345 |
|
|
3337 | 3346 |
|
|
3338 | 3347 | |
|
3339 | 3348 |
|
|
3340 | 3349 |
|
|
3341 | 3350 |
|
|
3342 | 3351 |
|
|
3343 | 3352 |
|
|
3344 | 3353 |
|
|
3345 | 3354 | |
|
3346 | 3355 |
|
|
3347 | 3356 |
|
|
3348 | 3357 |
|
|
3349 | 3358 |
|
|
3350 | 3359 |
|
|
3351 | 3360 |
|
|
3352 | 3361 |
|
|
3353 | 3362 | |
|
3354 | 3363 |
|
|
3355 | 3364 | |
|
3356 | 3365 |
|
|
3357 | 3366 |
|
|
3358 | 3367 | Evaluate an asynchronous code object using a code runner |
|
3359 | 3368 | |
|
3360 | 3369 | Fake asynchronous execution of code_object in a namespace via a proxy namespace. |
|
3361 | 3370 | |
|
3362 | 3371 | Returns coroutine object, which can be executed via async loop runner |
|
3363 | 3372 | |
|
3364 | 3373 | WARNING: The semantics of `async_exec` are quite different from `exec`, |
|
3365 | 3374 | in particular you can only pass a single namespace. It also return a |
|
3366 | 3375 | handle to the value of the last things returned by code_object. |
|
3367 | 3376 | """ |
|
3368 | 3377 | |
|
3369 | 3378 |
|
|
3370 | 3379 | |
|
3371 | 3380 |
|
|
3372 | 3381 |
|
|
3373 | 3382 | |
|
3374 | 3383 | When an exception occurs, self.showtraceback() is called to display a |
|
3375 | 3384 | traceback. |
|
3376 | 3385 | |
|
3377 | 3386 | Parameters |
|
3378 | 3387 | ---------- |
|
3379 | 3388 | code_obj : code object |
|
3380 | 3389 | A compiled code object, to be executed |
|
3381 | 3390 | result : ExecutionResult, optional |
|
3382 | 3391 | An object to store exceptions that occur during execution. |
|
3383 | 3392 | async_ : Bool (Experimental) |
|
3384 | 3393 | Attempt to run top-level asynchronous code in a default loop. |
|
3385 | 3394 | |
|
3386 | 3395 | Returns |
|
3387 | 3396 | ------- |
|
3388 | 3397 | False : successful execution. |
|
3389 | 3398 | True : an error occurred. |
|
3390 | 3399 | """ |
|
3391 | 3400 |
|
|
3392 | 3401 |
|
|
3393 | 3402 |
|
|
3394 | 3403 |
|
|
3395 | 3404 |
|
|
3396 | 3405 |
|
|
3397 | 3406 | |
|
3398 | 3407 |
|
|
3399 | 3408 |
|
|
3400 | 3409 |
|
|
3401 | 3410 |
|
|
3402 | 3411 |
|
|
3403 | 3412 |
|
|
3404 | 3413 |
|
|
3405 | 3414 |
|
|
3406 | 3415 |
|
|
3407 | 3416 |
|
|
3408 | 3417 |
|
|
3409 | 3418 |
|
|
3410 | 3419 |
|
|
3411 | 3420 |
|
|
3412 | 3421 |
|
|
3413 | 3422 |
|
|
3414 | 3423 |
|
|
3415 | 3424 |
|
|
3416 | 3425 |
|
|
3417 | 3426 |
|
|
3418 | 3427 |
|
|
3419 | 3428 |
|
|
3420 | 3429 |
|
|
3421 | 3430 |
|
|
3422 | 3431 |
|
|
3423 | 3432 |
|
|
3424 | 3433 |
|
|
3425 | 3434 |
|
|
3426 | 3435 |
|
|
3427 | 3436 |
|
|
3428 | 3437 |
|
|
3429 | 3438 |
|
|
3430 | 3439 |
|
|
3431 | 3440 |
|
|
3432 | 3441 |
|
|
3433 | 3442 | |
|
3434 | 3443 |
|
|
3435 | 3444 |
|
|
3436 | 3445 | |
|
3437 | 3446 |
|
|
3438 | 3447 |
|
|
3439 | 3448 | |
|
3440 | 3449 | Parameters |
|
3441 | 3450 | ---------- |
|
3442 | 3451 | source : string |
|
3443 | 3452 | Python input code, which can be multiline. |
|
3444 | 3453 | |
|
3445 | 3454 | Returns |
|
3446 | 3455 | ------- |
|
3447 | 3456 | status : str |
|
3448 | 3457 | One of 'complete', 'incomplete', or 'invalid' if source is not a |
|
3449 | 3458 | prefix of valid code. |
|
3450 | 3459 | indent : str |
|
3451 | 3460 | When status is 'incomplete', this is some whitespace to insert on |
|
3452 | 3461 | the next line of the prompt. |
|
3453 | 3462 | """ |
|
3454 | 3463 |
|
|
3455 | 3464 |
|
|
3456 | 3465 | |
|
3457 | 3466 |
|
|
3458 | 3467 |
|
|
3459 | 3468 |
|
|
3460 | 3469 | |
|
3461 | 3470 |
|
|
3462 | 3471 | |
|
3463 | 3472 |
|
|
3464 | 3473 |
|
|
3465 | 3474 |
|
|
3466 | 3475 |
|
|
3467 | 3476 |
|
|
3468 | 3477 | |
|
3469 | 3478 | This takes the following steps: |
|
3470 | 3479 | |
|
3471 | 3480 | 1. select the appropriate eventloop and matplotlib backend |
|
3472 | 3481 | 2. set up matplotlib for interactive use with that backend |
|
3473 | 3482 | 3. configure formatters for inline figure display |
|
3474 | 3483 | 4. enable the selected gui eventloop |
|
3475 | 3484 | |
|
3476 | 3485 | Parameters |
|
3477 | 3486 | ---------- |
|
3478 | 3487 | gui : optional, string |
|
3479 | 3488 | If given, dictates the choice of matplotlib GUI backend to use |
|
3480 | 3489 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
3481 | 3490 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
3482 | 3491 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
3483 | 3492 | user's matplotlibrc configuration file). Note that not all backends |
|
3484 | 3493 | make sense in all contexts, for example a terminal ipython can't |
|
3485 | 3494 | display figures inline. |
|
3486 | 3495 | """ |
|
3487 | 3496 |
|
|
3488 | 3497 |
|
|
3489 | 3498 |
|
|
3490 | 3499 |
|
|
3491 | 3500 |
|
|
3492 | 3501 |
|
|
3493 | 3502 |
|
|
3494 | 3503 |
|
|
3495 | 3504 |
|
|
3496 | 3505 |
|
|
3497 | 3506 |
|
|
3498 | 3507 |
|
|
3499 | 3508 |
|
|
3500 | 3509 |
|
|
3501 | 3510 |
|
|
3502 | 3511 |
|
|
3503 | 3512 |
|
|
3504 | 3513 |
|
|
3505 | 3514 |
|
|
3506 | 3515 |
|
|
3507 | 3516 |
|
|
3508 | 3517 |
|
|
3509 | 3518 |
|
|
3510 | 3519 | |
|
3511 | 3520 |
|
|
3512 | 3521 |
|
|
3513 | 3522 | |
|
3514 | 3523 | This turns on support for matplotlib, preloads into the interactive |
|
3515 | 3524 | namespace all of numpy and pylab, and configures IPython to correctly |
|
3516 | 3525 | interact with the GUI event loop. The GUI backend to be used can be |
|
3517 | 3526 | optionally selected with the optional ``gui`` argument. |
|
3518 | 3527 | |
|
3519 | 3528 | This method only adds preloading the namespace to InteractiveShell.enable_matplotlib. |
|
3520 | 3529 | |
|
3521 | 3530 | Parameters |
|
3522 | 3531 | ---------- |
|
3523 | 3532 | gui : optional, string |
|
3524 | 3533 | If given, dictates the choice of matplotlib GUI backend to use |
|
3525 | 3534 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
3526 | 3535 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
3527 | 3536 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
3528 | 3537 | user's matplotlibrc configuration file). Note that not all backends |
|
3529 | 3538 | make sense in all contexts, for example a terminal ipython can't |
|
3530 | 3539 | display figures inline. |
|
3531 | 3540 | import_all : optional, bool, default: True |
|
3532 | 3541 | Whether to do `from numpy import *` and `from pylab import *` |
|
3533 | 3542 | in addition to module imports. |
|
3534 | 3543 | welcome_message : deprecated |
|
3535 | 3544 | This argument is ignored, no welcome message will be displayed. |
|
3536 | 3545 | """ |
|
3537 | 3546 |
|
|
3538 | 3547 |
|
|
3539 | 3548 |
|
|
3540 | 3549 |
|
|
3541 | 3550 |
|
|
3542 | 3551 |
|
|
3543 | 3552 |
|
|
3544 | 3553 |
|
|
3545 | 3554 |
|
|
3546 | 3555 |
|
|
3547 | 3556 |
|
|
3548 | 3557 |
|
|
3549 | 3558 |
|
|
3550 | 3559 |
|
|
3551 | 3560 |
|
|
3552 | 3561 |
|
|
3553 | 3562 |
|
|
3554 | 3563 | |
|
3555 | 3564 |
|
|
3556 | 3565 |
|
|
3557 | 3566 |
|
|
3558 | 3567 | |
|
3559 | 3568 |
|
|
3560 | 3569 |
|
|
3561 | 3570 | |
|
3562 | 3571 | The depth argument indicates how many frames above the caller should |
|
3563 | 3572 | be walked to look for the local namespace where to expand variables. |
|
3564 | 3573 | |
|
3565 | 3574 | The global namespace for expansion is always the user's interactive |
|
3566 | 3575 | namespace. |
|
3567 | 3576 | """ |
|
3568 | 3577 |
|
|
3569 | 3578 |
|
|
3570 | 3579 |
|
|
3571 | 3580 |
|
|
3572 | 3581 |
|
|
3573 | 3582 |
|
|
3574 | 3583 |
|
|
3575 | 3584 |
|
|
3576 | 3585 |
|
|
3577 | 3586 | |
|
3578 | 3587 |
|
|
3579 | 3588 |
|
|
3580 | 3589 |
|
|
3581 | 3590 |
|
|
3582 | 3591 |
|
|
3583 | 3592 |
|
|
3584 | 3593 |
|
|
3585 | 3594 |
|
|
3586 | 3595 |
|
|
3587 | 3596 | |
|
3588 | 3597 |
|
|
3589 | 3598 |
|
|
3590 | 3599 | |
|
3591 | 3600 | This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp), |
|
3592 | 3601 | but it registers the created filename internally so ipython cleans it up |
|
3593 | 3602 | at exit time. |
|
3594 | 3603 | |
|
3595 | 3604 | Optional inputs: |
|
3596 | 3605 | |
|
3597 | 3606 | - data(None): if data is given, it gets written out to the temp file |
|
3598 | 3607 | immediately, and the file is closed again.""" |
|
3599 | 3608 | |
|
3600 | 3609 |
|
|
3601 | 3610 |
|
|
3602 | 3611 | |
|
3603 | 3612 |
|
|
3604 | 3613 |
|
|
3605 | 3614 | |
|
3606 | 3615 |
|
|
3607 | 3616 |
|
|
3608 | 3617 | |
|
3609 | 3618 |
|
|
3610 | 3619 |
|
|
3611 | 3620 |
|
|
3612 | 3621 | |
|
3613 | 3622 |
|
|
3614 | 3623 |
|
|
3615 | 3624 |
|
|
3616 | 3625 |
|
|
3617 | 3626 |
|
|
3618 | 3627 |
|
|
3619 | 3628 | |
|
3620 | 3629 |
|
|
3621 | 3630 |
|
|
3622 | 3631 |
|
|
3623 | 3632 |
|
|
3624 | 3633 |
|
|
3625 | 3634 |
|
|
3626 | 3635 | |
|
3627 | 3636 |
|
|
3628 | 3637 |
|
|
3629 | 3638 |
|
|
3630 | 3639 |
|
|
3631 | 3640 | |
|
3632 | 3641 |
|
|
3633 | 3642 |
|
|
3634 | 3643 |
|
|
3635 | 3644 | |
|
3636 | 3645 |
|
|
3637 | 3646 |
|
|
3638 | 3647 | |
|
3639 | 3648 | Parameters |
|
3640 | 3649 | ---------- |
|
3641 | 3650 | range_str : string |
|
3642 | 3651 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", |
|
3643 | 3652 | since this function is for use by magic functions which get their |
|
3644 | 3653 | arguments as strings. The number before the / is the session |
|
3645 | 3654 | number: ~n goes n back from the current session. |
|
3646 | 3655 | |
|
3647 | 3656 | raw : bool, optional |
|
3648 | 3657 | By default, the processed input is used. If this is true, the raw |
|
3649 | 3658 | input history is used instead. |
|
3650 | 3659 | |
|
3651 | 3660 | Notes |
|
3652 | 3661 | ----- |
|
3653 | 3662 | |
|
3654 | 3663 | Slices can be described with two notations: |
|
3655 | 3664 | |
|
3656 | 3665 | * ``N:M`` -> standard python form, means including items N...(M-1). |
|
3657 | 3666 | * ``N-M`` -> include items N..M (closed endpoint). |
|
3658 | 3667 | """ |
|
3659 | 3668 |
|
|
3660 | 3669 |
|
|
3661 | 3670 | |
|
3662 | 3671 |
|
|
3663 | 3672 |
|
|
3664 | 3673 | |
|
3665 | 3674 | This is mainly used by magic functions. |
|
3666 | 3675 | |
|
3667 | 3676 | Parameters |
|
3668 | 3677 | ---------- |
|
3669 | 3678 | |
|
3670 | 3679 | target : str |
|
3671 | 3680 | |
|
3672 | 3681 | A string specifying code to retrieve. This will be tried respectively |
|
3673 | 3682 | as: ranges of input history (see %history for syntax), url, |
|
3674 | 3683 | corresponding .py file, filename, or an expression evaluating to a |
|
3675 | 3684 | string or Macro in the user namespace. |
|
3676 | 3685 | |
|
3677 | 3686 | raw : bool |
|
3678 | 3687 | If true (default), retrieve raw history. Has no effect on the other |
|
3679 | 3688 | retrieval mechanisms. |
|
3680 | 3689 | |
|
3681 | 3690 | py_only : bool (default False) |
|
3682 | 3691 | Only try to fetch python code, do not try alternative methods to decode file |
|
3683 | 3692 | if unicode fails. |
|
3684 | 3693 | |
|
3685 | 3694 | Returns |
|
3686 | 3695 | ------- |
|
3687 | 3696 | A string of code. |
|
3688 | 3697 | |
|
3689 | 3698 | ValueError is raised if nothing is found, and TypeError if it evaluates |
|
3690 | 3699 | to an object of another type. In each case, .args[0] is a printable |
|
3691 | 3700 | message. |
|
3692 | 3701 | """ |
|
3693 | 3702 |
|
|
3694 | 3703 |
|
|
3695 | 3704 |
|
|
3696 | 3705 |
|
|
3697 | 3706 |
|
|
3698 | 3707 |
|
|
3699 | 3708 |
|
|
3700 | 3709 |
|
|
3701 | 3710 |
|
|
3702 | 3711 |
|
|
3703 | 3712 |
|
|
3704 | 3713 |
|
|
3705 | 3714 |
|
|
3706 | 3715 | |
|
3707 | 3716 |
|
|
3708 | 3717 |
|
|
3709 | 3718 |
|
|
3710 | 3719 |
|
|
3711 | 3720 |
|
|
3712 | 3721 | |
|
3713 | 3722 |
|
|
3714 | 3723 |
|
|
3715 | 3724 |
|
|
3716 | 3725 |
|
|
3717 | 3726 |
|
|
3718 | 3727 |
|
|
3719 | 3728 |
|
|
3720 | 3729 |
|
|
3721 | 3730 |
|
|
3722 | 3731 |
|
|
3723 | 3732 |
|
|
3724 | 3733 | |
|
3725 | 3734 |
|
|
3726 | 3735 |
|
|
3727 | 3736 |
|
|
3728 | 3737 |
|
|
3729 | 3738 |
|
|
3730 | 3739 | |
|
3731 | 3740 |
|
|
3732 | 3741 |
|
|
3733 | 3742 |
|
|
3734 | 3743 |
|
|
3735 | 3744 |
|
|
3736 | 3745 | |
|
3737 | 3746 |
|
|
3738 | 3747 |
|
|
3739 | 3748 |
|
|
3740 | 3749 |
|
|
3741 | 3750 | |
|
3742 | 3751 |
|
|
3743 | 3752 |
|
|
3744 | 3753 | |
|
3745 | 3754 |
|
|
3746 | 3755 |
|
|
3747 | 3756 |
|
|
3748 | 3757 |
|
|
3749 | 3758 |
|
|
3750 | 3759 | |
|
3751 | 3760 | Cleanup operations and saving of persistent data that is done |
|
3752 | 3761 | unconditionally by IPython should be performed here. |
|
3753 | 3762 | |
|
3754 | 3763 | For things that may depend on startup flags or platform specifics (such |
|
3755 | 3764 | as having readline or not), register a separate atexit function in the |
|
3756 | 3765 | code that has the appropriate information, rather than trying to |
|
3757 | 3766 | clutter |
|
3758 | 3767 | """ |
|
3759 | 3768 |
|
|
3760 | 3769 |
|
|
3761 | 3770 |
|
|
3762 | 3771 |
|
|
3763 | 3772 | |
|
3764 | 3773 |
|
|
3765 | 3774 |
|
|
3766 | 3775 |
|
|
3767 | 3776 |
|
|
3768 | 3777 |
|
|
3769 | 3778 |
|
|
3770 | 3779 | |
|
3771 | 3780 |
|
|
3772 | 3781 |
|
|
3773 | 3782 |
|
|
3774 | 3783 |
|
|
3775 | 3784 |
|
|
3776 | 3785 | |
|
3777 | 3786 |
|
|
3778 | 3787 |
|
|
3779 | 3788 | |
|
3780 | 3789 |
|
|
3781 | 3790 |
|
|
3782 | 3791 | |
|
3783 | 3792 |
|
|
3784 | 3793 |
|
|
3785 | 3794 | |
|
3786 | 3795 | |
|
3787 | 3796 |
|
|
3788 | 3797 |
|
|
3789 | 3798 |
|
|
3790 | 3799 | |
|
3791 | 3800 | |
|
3792 | 3801 |
|
|
3793 | 3802 |
|
|
3794 | 3803 | |
|
3795 | 3804 |
|
@@ -1,708 +1,712 b'' | |||
|
1 | 1 | """Implementation of namespace-related magic functions. |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (c) 2012 The IPython Development Team. |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | # |
|
8 | 8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | # Stdlib |
|
16 | 16 | import gc |
|
17 | 17 | import re |
|
18 | 18 | import sys |
|
19 | 19 | |
|
20 | 20 | # Our own packages |
|
21 | 21 | from IPython.core import page |
|
22 | 22 | from IPython.core.error import StdinNotImplementedError, UsageError |
|
23 | 23 | from IPython.core.magic import Magics, magics_class, line_magic |
|
24 | 24 | from IPython.testing.skipdoctest import skip_doctest |
|
25 | 25 | from IPython.utils.encoding import DEFAULT_ENCODING |
|
26 | 26 | from IPython.utils.openpy import read_py_file |
|
27 | 27 | from IPython.utils.path import get_py_filename |
|
28 | 28 | |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | # Magic implementation classes |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | |
|
33 | 33 | @magics_class |
|
34 | 34 | class NamespaceMagics(Magics): |
|
35 | 35 | """Magics to manage various aspects of the user's namespace. |
|
36 | 36 | |
|
37 | 37 | These include listing variables, introspecting into them, etc. |
|
38 | 38 | """ |
|
39 | 39 | |
|
40 | 40 | @line_magic |
|
41 | 41 | def pinfo(self, parameter_s='', namespaces=None): |
|
42 | 42 | """Provide detailed information about an object. |
|
43 | 43 | |
|
44 | 44 | '%pinfo object' is just a synonym for object? or ?object.""" |
|
45 | 45 | |
|
46 | 46 | #print 'pinfo par: <%s>' % parameter_s # dbg |
|
47 | 47 | # detail_level: 0 -> obj? , 1 -> obj?? |
|
48 | 48 | detail_level = 0 |
|
49 | 49 | # We need to detect if we got called as 'pinfo pinfo foo', which can |
|
50 | 50 | # happen if the user types 'pinfo foo?' at the cmd line. |
|
51 | 51 | pinfo,qmark1,oname,qmark2 = \ |
|
52 | 52 | re.match(r'(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups() |
|
53 | 53 | if pinfo or qmark1 or qmark2: |
|
54 | 54 | detail_level = 1 |
|
55 | 55 | if "*" in oname: |
|
56 | 56 | self.psearch(oname) |
|
57 | 57 | else: |
|
58 | 58 | self.shell._inspect('pinfo', oname, detail_level=detail_level, |
|
59 | 59 | namespaces=namespaces) |
|
60 | 60 | |
|
61 | 61 | @line_magic |
|
62 | 62 | def pinfo2(self, parameter_s='', namespaces=None): |
|
63 | 63 | """Provide extra detailed information about an object. |
|
64 | 64 | |
|
65 | 65 | '%pinfo2 object' is just a synonym for object?? or ??object.""" |
|
66 | 66 | self.shell._inspect('pinfo', parameter_s, detail_level=1, |
|
67 | 67 | namespaces=namespaces) |
|
68 | 68 | |
|
69 | 69 | @skip_doctest |
|
70 | 70 | @line_magic |
|
71 | 71 | def pdef(self, parameter_s='', namespaces=None): |
|
72 | 72 | """Print the call signature for any callable object. |
|
73 | 73 | |
|
74 | 74 | If the object is a class, print the constructor information. |
|
75 | 75 | |
|
76 | 76 | Examples |
|
77 | 77 | -------- |
|
78 | 78 | :: |
|
79 | 79 | |
|
80 | 80 | In [3]: %pdef urllib.urlopen |
|
81 | 81 | urllib.urlopen(url, data=None, proxies=None) |
|
82 | 82 | """ |
|
83 | 83 | self.shell._inspect('pdef',parameter_s, namespaces) |
|
84 | 84 | |
|
85 | 85 | @line_magic |
|
86 | 86 | def pdoc(self, parameter_s='', namespaces=None): |
|
87 | 87 | """Print the docstring for an object. |
|
88 | 88 | |
|
89 | 89 | If the given object is a class, it will print both the class and the |
|
90 | 90 | constructor docstrings.""" |
|
91 | 91 | self.shell._inspect('pdoc',parameter_s, namespaces) |
|
92 | 92 | |
|
93 | 93 | @line_magic |
|
94 | 94 | def psource(self, parameter_s='', namespaces=None): |
|
95 | 95 | """Print (or run through pager) the source code for an object.""" |
|
96 | 96 | if not parameter_s: |
|
97 | 97 | raise UsageError('Missing object name.') |
|
98 | 98 | self.shell._inspect('psource',parameter_s, namespaces) |
|
99 | 99 | |
|
100 | 100 | @line_magic |
|
101 | 101 | def pfile(self, parameter_s='', namespaces=None): |
|
102 | 102 | """Print (or run through pager) the file where an object is defined. |
|
103 | 103 | |
|
104 | 104 | The file opens at the line where the object definition begins. IPython |
|
105 | 105 | will honor the environment variable PAGER if set, and otherwise will |
|
106 | 106 | do its best to print the file in a convenient form. |
|
107 | 107 | |
|
108 | 108 | If the given argument is not an object currently defined, IPython will |
|
109 | 109 | try to interpret it as a filename (automatically adding a .py extension |
|
110 | 110 | if needed). You can thus use %pfile as a syntax highlighting code |
|
111 | 111 | viewer.""" |
|
112 | 112 | |
|
113 | 113 | # first interpret argument as an object name |
|
114 | 114 | out = self.shell._inspect('pfile',parameter_s, namespaces) |
|
115 | 115 | # if not, try the input as a filename |
|
116 | 116 | if out == 'not found': |
|
117 | 117 | try: |
|
118 | 118 | filename = get_py_filename(parameter_s) |
|
119 | 119 | except IOError as msg: |
|
120 | 120 | print(msg) |
|
121 | 121 | return |
|
122 | 122 | page.page(self.shell.pycolorize(read_py_file(filename, skip_encoding_cookie=False))) |
|
123 | 123 | |
|
124 | 124 | @line_magic |
|
125 | 125 | def psearch(self, parameter_s=''): |
|
126 | 126 | """Search for object in namespaces by wildcard. |
|
127 | 127 | |
|
128 | 128 | %psearch [options] PATTERN [OBJECT TYPE] |
|
129 | 129 | |
|
130 | 130 | Note: ? can be used as a synonym for %psearch, at the beginning or at |
|
131 | 131 | the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the |
|
132 | 132 | rest of the command line must be unchanged (options come first), so |
|
133 | 133 | for example the following forms are equivalent |
|
134 | 134 | |
|
135 | 135 | %psearch -i a* function |
|
136 | 136 | -i a* function? |
|
137 | 137 | ?-i a* function |
|
138 | 138 | |
|
139 | 139 | Arguments: |
|
140 | 140 | |
|
141 | 141 | PATTERN |
|
142 | 142 | |
|
143 | 143 | where PATTERN is a string containing * as a wildcard similar to its |
|
144 | 144 | use in a shell. The pattern is matched in all namespaces on the |
|
145 | 145 | search path. By default objects starting with a single _ are not |
|
146 | 146 | matched, many IPython generated objects have a single |
|
147 | 147 | underscore. The default is case insensitive matching. Matching is |
|
148 | 148 | also done on the attributes of objects and not only on the objects |
|
149 | 149 | in a module. |
|
150 | 150 | |
|
151 | 151 | [OBJECT TYPE] |
|
152 | 152 | |
|
153 | 153 | Is the name of a python type from the types module. The name is |
|
154 | 154 | given in lowercase without the ending type, ex. StringType is |
|
155 | 155 | written string. By adding a type here only objects matching the |
|
156 | 156 | given type are matched. Using all here makes the pattern match all |
|
157 | 157 | types (this is the default). |
|
158 | 158 | |
|
159 | 159 | Options: |
|
160 | 160 | |
|
161 | 161 | -a: makes the pattern match even objects whose names start with a |
|
162 | 162 | single underscore. These names are normally omitted from the |
|
163 | 163 | search. |
|
164 | 164 | |
|
165 | 165 | -i/-c: make the pattern case insensitive/sensitive. If neither of |
|
166 | 166 | these options are given, the default is read from your configuration |
|
167 | 167 | file, with the option ``InteractiveShell.wildcards_case_sensitive``. |
|
168 | 168 | If this option is not specified in your configuration file, IPython's |
|
169 | 169 | internal default is to do a case sensitive search. |
|
170 | 170 | |
|
171 | 171 | -e/-s NAMESPACE: exclude/search a given namespace. The pattern you |
|
172 | 172 | specify can be searched in any of the following namespaces: |
|
173 | 173 | 'builtin', 'user', 'user_global','internal', 'alias', where |
|
174 | 174 | 'builtin' and 'user' are the search defaults. Note that you should |
|
175 | 175 | not use quotes when specifying namespaces. |
|
176 | 176 | |
|
177 | 177 | -l: List all available object types for object matching. This function |
|
178 | 178 | can be used without arguments. |
|
179 | 179 | |
|
180 | 180 | 'Builtin' contains the python module builtin, 'user' contains all |
|
181 | 181 | user data, 'alias' only contain the shell aliases and no python |
|
182 | 182 | objects, 'internal' contains objects used by IPython. The |
|
183 | 183 | 'user_global' namespace is only used by embedded IPython instances, |
|
184 | 184 | and it contains module-level globals. You can add namespaces to the |
|
185 | 185 | search with -s or exclude them with -e (these options can be given |
|
186 | 186 | more than once). |
|
187 | 187 | |
|
188 | 188 | Examples |
|
189 | 189 | -------- |
|
190 | 190 | :: |
|
191 | 191 | |
|
192 | 192 | %psearch a* -> objects beginning with an a |
|
193 | 193 | %psearch -e builtin a* -> objects NOT in the builtin space starting in a |
|
194 | 194 | %psearch a* function -> all functions beginning with an a |
|
195 | 195 | %psearch re.e* -> objects beginning with an e in module re |
|
196 | 196 | %psearch r*.e* -> objects that start with e in modules starting in r |
|
197 | 197 | %psearch r*.* string -> all strings in modules beginning with r |
|
198 | 198 | |
|
199 | 199 | Case sensitive search:: |
|
200 | 200 | |
|
201 | 201 | %psearch -c a* list all object beginning with lower case a |
|
202 | 202 | |
|
203 | 203 | Show objects beginning with a single _:: |
|
204 | 204 | |
|
205 | 205 | %psearch -a _* list objects beginning with a single underscore |
|
206 | 206 | |
|
207 | 207 | List available objects:: |
|
208 | 208 | |
|
209 | 209 | %psearch -l list all available object types |
|
210 | 210 | """ |
|
211 | 211 | # default namespaces to be searched |
|
212 | 212 | def_search = ['user_local', 'user_global', 'builtin'] |
|
213 | 213 | |
|
214 | 214 | # Process options/args |
|
215 | 215 | opts,args = self.parse_options(parameter_s,'cias:e:l',list_all=True) |
|
216 | 216 | opt = opts.get |
|
217 | 217 | shell = self.shell |
|
218 | 218 | psearch = shell.inspector.psearch |
|
219 | 219 | |
|
220 | 220 | # select list object types |
|
221 | 221 | list_types = False |
|
222 | 222 | if 'l' in opts: |
|
223 | 223 | list_types = True |
|
224 | 224 | |
|
225 | 225 | # select case options |
|
226 | 226 | if 'i' in opts: |
|
227 | 227 | ignore_case = True |
|
228 | 228 | elif 'c' in opts: |
|
229 | 229 | ignore_case = False |
|
230 | 230 | else: |
|
231 | 231 | ignore_case = not shell.wildcards_case_sensitive |
|
232 | 232 | |
|
233 | 233 | # Build list of namespaces to search from user options |
|
234 | 234 | def_search.extend(opt('s',[])) |
|
235 | 235 | ns_exclude = ns_exclude=opt('e',[]) |
|
236 | 236 | ns_search = [nm for nm in def_search if nm not in ns_exclude] |
|
237 | 237 | |
|
238 | 238 | # Call the actual search |
|
239 | 239 | try: |
|
240 | 240 | psearch(args,shell.ns_table,ns_search, |
|
241 | 241 | show_all=opt('a'),ignore_case=ignore_case, list_types=list_types) |
|
242 | 242 | except: |
|
243 | 243 | shell.showtraceback() |
|
244 | 244 | |
|
245 | 245 | @skip_doctest |
|
246 | 246 | @line_magic |
|
247 | 247 | def who_ls(self, parameter_s=''): |
|
248 | 248 | """Return a sorted list of all interactive variables. |
|
249 | 249 | |
|
250 | 250 | If arguments are given, only variables of types matching these |
|
251 | 251 | arguments are returned. |
|
252 | 252 | |
|
253 | 253 | Examples |
|
254 | 254 | -------- |
|
255 | 255 | |
|
256 | 256 | Define two variables and list them with who_ls:: |
|
257 | 257 | |
|
258 | 258 | In [1]: alpha = 123 |
|
259 | 259 | |
|
260 | 260 | In [2]: beta = 'test' |
|
261 | 261 | |
|
262 | 262 | In [3]: %who_ls |
|
263 | 263 | Out[3]: ['alpha', 'beta'] |
|
264 | 264 | |
|
265 | 265 | In [4]: %who_ls int |
|
266 | 266 | Out[4]: ['alpha'] |
|
267 | 267 | |
|
268 | 268 | In [5]: %who_ls str |
|
269 | 269 | Out[5]: ['beta'] |
|
270 | 270 | """ |
|
271 | 271 | |
|
272 | 272 | user_ns = self.shell.user_ns |
|
273 | 273 | user_ns_hidden = self.shell.user_ns_hidden |
|
274 | 274 | nonmatching = object() # This can never be in user_ns |
|
275 | 275 | out = [ i for i in user_ns |
|
276 | 276 | if not i.startswith('_') \ |
|
277 | 277 | and (user_ns[i] is not user_ns_hidden.get(i, nonmatching)) ] |
|
278 | 278 | |
|
279 | 279 | typelist = parameter_s.split() |
|
280 | 280 | if typelist: |
|
281 | 281 | typeset = set(typelist) |
|
282 | 282 | out = [i for i in out if type(user_ns[i]).__name__ in typeset] |
|
283 | 283 | |
|
284 | 284 | out.sort() |
|
285 | 285 | return out |
|
286 | 286 | |
|
287 | 287 | @skip_doctest |
|
288 | 288 | @line_magic |
|
289 | 289 | def who(self, parameter_s=''): |
|
290 | 290 | """Print all interactive variables, with some minimal formatting. |
|
291 | 291 | |
|
292 | 292 | If any arguments are given, only variables whose type matches one of |
|
293 | 293 | these are printed. For example:: |
|
294 | 294 | |
|
295 | 295 | %who function str |
|
296 | 296 | |
|
297 | 297 | will only list functions and strings, excluding all other types of |
|
298 | 298 | variables. To find the proper type names, simply use type(var) at a |
|
299 | 299 | command line to see how python prints type names. For example: |
|
300 | 300 | |
|
301 | 301 | :: |
|
302 | 302 | |
|
303 | 303 | In [1]: type('hello')\\ |
|
304 | 304 | Out[1]: <type 'str'> |
|
305 | 305 | |
|
306 | 306 | indicates that the type name for strings is 'str'. |
|
307 | 307 | |
|
308 | 308 | ``%who`` always excludes executed names loaded through your configuration |
|
309 | 309 | file and things which are internal to IPython. |
|
310 | 310 | |
|
311 | 311 | This is deliberate, as typically you may load many modules and the |
|
312 | 312 | purpose of %who is to show you only what you've manually defined. |
|
313 | 313 | |
|
314 | 314 | Examples |
|
315 | 315 | -------- |
|
316 | 316 | |
|
317 | 317 | Define two variables and list them with who:: |
|
318 | 318 | |
|
319 | 319 | In [1]: alpha = 123 |
|
320 | 320 | |
|
321 | 321 | In [2]: beta = 'test' |
|
322 | 322 | |
|
323 | 323 | In [3]: %who |
|
324 | 324 | alpha beta |
|
325 | 325 | |
|
326 | 326 | In [4]: %who int |
|
327 | 327 | alpha |
|
328 | 328 | |
|
329 | 329 | In [5]: %who str |
|
330 | 330 | beta |
|
331 | 331 | """ |
|
332 | 332 | |
|
333 | 333 | varlist = self.who_ls(parameter_s) |
|
334 | 334 | if not varlist: |
|
335 | 335 | if parameter_s: |
|
336 | 336 | print('No variables match your requested type.') |
|
337 | 337 | else: |
|
338 | 338 | print('Interactive namespace is empty.') |
|
339 | 339 | return |
|
340 | 340 | |
|
341 | 341 | # if we have variables, move on... |
|
342 | 342 | count = 0 |
|
343 | 343 | for i in varlist: |
|
344 | 344 | print(i+'\t', end=' ') |
|
345 | 345 | count += 1 |
|
346 | 346 | if count > 8: |
|
347 | 347 | count = 0 |
|
348 | 348 | print() |
|
349 | 349 | print() |
|
350 | 350 | |
|
351 | 351 | @skip_doctest |
|
352 | 352 | @line_magic |
|
353 | 353 | def whos(self, parameter_s=''): |
|
354 | 354 | """Like %who, but gives some extra information about each variable. |
|
355 | 355 | |
|
356 | 356 | The same type filtering of %who can be applied here. |
|
357 | 357 | |
|
358 | 358 | For all variables, the type is printed. Additionally it prints: |
|
359 | 359 | |
|
360 | 360 | - For {},[],(): their length. |
|
361 | 361 | |
|
362 | 362 | - For numpy arrays, a summary with shape, number of |
|
363 | 363 | elements, typecode and size in memory. |
|
364 | 364 | |
|
365 | 365 | - Everything else: a string representation, snipping their middle if |
|
366 | 366 | too long. |
|
367 | 367 | |
|
368 | 368 | Examples |
|
369 | 369 | -------- |
|
370 | 370 | |
|
371 | 371 | Define two variables and list them with whos:: |
|
372 | 372 | |
|
373 | 373 | In [1]: alpha = 123 |
|
374 | 374 | |
|
375 | 375 | In [2]: beta = 'test' |
|
376 | 376 | |
|
377 | 377 | In [3]: %whos |
|
378 | 378 | Variable Type Data/Info |
|
379 | 379 | -------------------------------- |
|
380 | 380 | alpha int 123 |
|
381 | 381 | beta str test |
|
382 | 382 | """ |
|
383 | 383 | |
|
384 | 384 | varnames = self.who_ls(parameter_s) |
|
385 | 385 | if not varnames: |
|
386 | 386 | if parameter_s: |
|
387 | 387 | print('No variables match your requested type.') |
|
388 | 388 | else: |
|
389 | 389 | print('Interactive namespace is empty.') |
|
390 | 390 | return |
|
391 | 391 | |
|
392 | 392 | # if we have variables, move on... |
|
393 | 393 | |
|
394 | 394 | # for these types, show len() instead of data: |
|
395 | 395 | seq_types = ['dict', 'list', 'tuple'] |
|
396 | 396 | |
|
397 | 397 | # for numpy arrays, display summary info |
|
398 | 398 | ndarray_type = None |
|
399 | 399 | if 'numpy' in sys.modules: |
|
400 | 400 | try: |
|
401 | 401 | from numpy import ndarray |
|
402 | 402 | except ImportError: |
|
403 | 403 | pass |
|
404 | 404 | else: |
|
405 | 405 | ndarray_type = ndarray.__name__ |
|
406 | 406 | |
|
407 | 407 | # Find all variable names and types so we can figure out column sizes |
|
408 | 408 | |
|
409 | 409 | # some types are well known and can be shorter |
|
410 | 410 | abbrevs = {'IPython.core.macro.Macro' : 'Macro'} |
|
411 | 411 | def type_name(v): |
|
412 | 412 | tn = type(v).__name__ |
|
413 | 413 | return abbrevs.get(tn,tn) |
|
414 | 414 | |
|
415 | 415 | varlist = [self.shell.user_ns[n] for n in varnames] |
|
416 | 416 | |
|
417 | 417 | typelist = [] |
|
418 | 418 | for vv in varlist: |
|
419 | 419 | tt = type_name(vv) |
|
420 | 420 | |
|
421 | 421 | if tt=='instance': |
|
422 | 422 | typelist.append( abbrevs.get(str(vv.__class__), |
|
423 | 423 | str(vv.__class__))) |
|
424 | 424 | else: |
|
425 | 425 | typelist.append(tt) |
|
426 | 426 | |
|
427 | 427 | # column labels and # of spaces as separator |
|
428 | 428 | varlabel = 'Variable' |
|
429 | 429 | typelabel = 'Type' |
|
430 | 430 | datalabel = 'Data/Info' |
|
431 | 431 | colsep = 3 |
|
432 | 432 | # variable format strings |
|
433 | 433 | vformat = "{0:<{varwidth}}{1:<{typewidth}}" |
|
434 | 434 | aformat = "%s: %s elems, type `%s`, %s bytes" |
|
435 | 435 | # find the size of the columns to format the output nicely |
|
436 | 436 | varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep |
|
437 | 437 | typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep |
|
438 | 438 | # table header |
|
439 | 439 | print(varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \ |
|
440 | 440 | ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)) |
|
441 | 441 | # and the table itself |
|
442 | 442 | kb = 1024 |
|
443 | 443 | Mb = 1048576 # kb**2 |
|
444 | 444 | for vname,var,vtype in zip(varnames,varlist,typelist): |
|
445 | 445 | print(vformat.format(vname, vtype, varwidth=varwidth, typewidth=typewidth), end=' ') |
|
446 | 446 | if vtype in seq_types: |
|
447 | 447 | print("n="+str(len(var))) |
|
448 | 448 | elif vtype == ndarray_type: |
|
449 | 449 | vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1] |
|
450 | 450 | if vtype==ndarray_type: |
|
451 | 451 | # numpy |
|
452 | 452 | vsize = var.size |
|
453 | 453 | vbytes = vsize*var.itemsize |
|
454 | 454 | vdtype = var.dtype |
|
455 | 455 | |
|
456 | 456 | if vbytes < 100000: |
|
457 | 457 | print(aformat % (vshape, vsize, vdtype, vbytes)) |
|
458 | 458 | else: |
|
459 | 459 | print(aformat % (vshape, vsize, vdtype, vbytes), end=' ') |
|
460 | 460 | if vbytes < Mb: |
|
461 | 461 | print('(%s kb)' % (vbytes/kb,)) |
|
462 | 462 | else: |
|
463 | 463 | print('(%s Mb)' % (vbytes/Mb,)) |
|
464 | 464 | else: |
|
465 | 465 | try: |
|
466 | 466 | vstr = str(var) |
|
467 | 467 | except UnicodeEncodeError: |
|
468 | 468 | vstr = var.encode(DEFAULT_ENCODING, |
|
469 | 469 | 'backslashreplace') |
|
470 | 470 | except: |
|
471 | 471 | vstr = "<object with id %d (str() failed)>" % id(var) |
|
472 | 472 | vstr = vstr.replace('\n', '\\n') |
|
473 | 473 | if len(vstr) < 50: |
|
474 | 474 | print(vstr) |
|
475 | 475 | else: |
|
476 | 476 | print(vstr[:25] + "<...>" + vstr[-25:]) |
|
477 | 477 | |
|
478 | 478 | @line_magic |
|
479 | 479 | def reset(self, parameter_s=''): |
|
480 | 480 | """Resets the namespace by removing all names defined by the user, if |
|
481 | 481 | called without arguments, or by removing some types of objects, such |
|
482 | 482 | as everything currently in IPython's In[] and Out[] containers (see |
|
483 | 483 | the parameters for details). |
|
484 | 484 | |
|
485 | 485 | Parameters |
|
486 | 486 | ---------- |
|
487 | 487 | -f : force reset without asking for confirmation. |
|
488 | 488 | |
|
489 | 489 | -s : 'Soft' reset: Only clears your namespace, leaving history intact. |
|
490 | 490 | References to objects may be kept. By default (without this option), |
|
491 | 491 | we do a 'hard' reset, giving you a new session and removing all |
|
492 | 492 | references to objects from the current session. |
|
493 | 493 | |
|
494 | --aggressive: Try to aggressively remove modules from sys.modules ; this | |
|
495 | may allow you to reimport Python modules that have been updated and | |
|
496 | pick up changes, but can have unattended consequences. | |
|
497 | ||
|
494 | 498 | in : reset input history |
|
495 | 499 | |
|
496 | 500 | out : reset output history |
|
497 | 501 | |
|
498 | 502 | dhist : reset directory history |
|
499 | 503 | |
|
500 | 504 | array : reset only variables that are NumPy arrays |
|
501 | 505 | |
|
502 | 506 | See Also |
|
503 | 507 | -------- |
|
504 | 508 | reset_selective : invoked as ``%reset_selective`` |
|
505 | 509 | |
|
506 | 510 | Examples |
|
507 | 511 | -------- |
|
508 | 512 | :: |
|
509 | 513 | |
|
510 | 514 | In [6]: a = 1 |
|
511 | 515 | |
|
512 | 516 | In [7]: a |
|
513 | 517 | Out[7]: 1 |
|
514 | 518 | |
|
515 | 519 | In [8]: 'a' in get_ipython().user_ns |
|
516 | 520 | Out[8]: True |
|
517 | 521 | |
|
518 | 522 | In [9]: %reset -f |
|
519 | 523 | |
|
520 | 524 | In [1]: 'a' in get_ipython().user_ns |
|
521 | 525 | Out[1]: False |
|
522 | 526 | |
|
523 | 527 | In [2]: %reset -f in |
|
524 | 528 | Flushing input history |
|
525 | 529 | |
|
526 | 530 | In [3]: %reset -f dhist in |
|
527 | 531 | Flushing directory history |
|
528 | 532 | Flushing input history |
|
529 | 533 | |
|
530 | 534 | Notes |
|
531 | 535 | ----- |
|
532 | 536 | Calling this magic from clients that do not implement standard input, |
|
533 | 537 | such as the ipython notebook interface, will reset the namespace |
|
534 | 538 | without confirmation. |
|
535 | 539 | """ |
|
536 |
opts, args = self.parse_options(parameter_s, |
|
|
537 |
if |
|
|
540 | opts, args = self.parse_options(parameter_s, "sf", "aggressive", mode="list") | |
|
541 | if "f" in opts: | |
|
538 | 542 | ans = True |
|
539 | 543 | else: |
|
540 | 544 | try: |
|
541 | 545 | ans = self.shell.ask_yes_no( |
|
542 | 546 | "Once deleted, variables cannot be recovered. Proceed (y/[n])?", |
|
543 | 547 | default='n') |
|
544 | 548 | except StdinNotImplementedError: |
|
545 | 549 | ans = True |
|
546 | 550 | if not ans: |
|
547 | 551 | print('Nothing done.') |
|
548 | 552 | return |
|
549 | 553 | |
|
550 | 554 | if 's' in opts: # Soft reset |
|
551 | 555 | user_ns = self.shell.user_ns |
|
552 | 556 | for i in self.who_ls(): |
|
553 | 557 | del(user_ns[i]) |
|
554 | 558 | elif len(args) == 0: # Hard reset |
|
555 |
self.shell.reset(new_session = |
|
|
559 | self.shell.reset(new_session=False, aggressive=("aggressive" in opts)) | |
|
556 | 560 | |
|
557 | 561 | # reset in/out/dhist/array: previously extensinions/clearcmd.py |
|
558 | 562 | ip = self.shell |
|
559 | 563 | user_ns = self.shell.user_ns # local lookup, heavily used |
|
560 | 564 | |
|
561 | 565 | for target in args: |
|
562 | 566 | target = target.lower() # make matches case insensitive |
|
563 | 567 | if target == 'out': |
|
564 | 568 | print("Flushing output cache (%d entries)" % len(user_ns['_oh'])) |
|
565 | 569 | self.shell.displayhook.flush() |
|
566 | 570 | |
|
567 | 571 | elif target == 'in': |
|
568 | 572 | print("Flushing input history") |
|
569 | 573 | pc = self.shell.displayhook.prompt_count + 1 |
|
570 | 574 | for n in range(1, pc): |
|
571 | 575 | key = '_i'+repr(n) |
|
572 | 576 | user_ns.pop(key,None) |
|
573 | 577 | user_ns.update(dict(_i=u'',_ii=u'',_iii=u'')) |
|
574 | 578 | hm = ip.history_manager |
|
575 | 579 | # don't delete these, as %save and %macro depending on the |
|
576 | 580 | # length of these lists to be preserved |
|
577 | 581 | hm.input_hist_parsed[:] = [''] * pc |
|
578 | 582 | hm.input_hist_raw[:] = [''] * pc |
|
579 | 583 | # hm has internal machinery for _i,_ii,_iii, clear it out |
|
580 | 584 | hm._i = hm._ii = hm._iii = hm._i00 = u'' |
|
581 | 585 | |
|
582 | 586 | elif target == 'array': |
|
583 | 587 | # Support cleaning up numpy arrays |
|
584 | 588 | try: |
|
585 | 589 | from numpy import ndarray |
|
586 | 590 | # This must be done with items and not iteritems because |
|
587 | 591 | # we're going to modify the dict in-place. |
|
588 | 592 | for x,val in list(user_ns.items()): |
|
589 | 593 | if isinstance(val,ndarray): |
|
590 | 594 | del user_ns[x] |
|
591 | 595 | except ImportError: |
|
592 | 596 | print("reset array only works if Numpy is available.") |
|
593 | 597 | |
|
594 | 598 | elif target == 'dhist': |
|
595 | 599 | print("Flushing directory history") |
|
596 | 600 | del user_ns['_dh'][:] |
|
597 | 601 | |
|
598 | 602 | else: |
|
599 | 603 | print("Don't know how to reset ", end=' ') |
|
600 | 604 | print(target + ", please run `%reset?` for details") |
|
601 | 605 | |
|
602 | 606 | gc.collect() |
|
603 | 607 | |
|
604 | 608 | @line_magic |
|
605 | 609 | def reset_selective(self, parameter_s=''): |
|
606 | 610 | """Resets the namespace by removing names defined by the user. |
|
607 | 611 | |
|
608 | 612 | Input/Output history are left around in case you need them. |
|
609 | 613 | |
|
610 | 614 | %reset_selective [-f] regex |
|
611 | 615 | |
|
612 | 616 | No action is taken if regex is not included |
|
613 | 617 | |
|
614 | 618 | Options |
|
615 | 619 | -f : force reset without asking for confirmation. |
|
616 | 620 | |
|
617 | 621 | See Also |
|
618 | 622 | -------- |
|
619 | 623 | reset : invoked as ``%reset`` |
|
620 | 624 | |
|
621 | 625 | Examples |
|
622 | 626 | -------- |
|
623 | 627 | |
|
624 | 628 | We first fully reset the namespace so your output looks identical to |
|
625 | 629 | this example for pedagogical reasons; in practice you do not need a |
|
626 | 630 | full reset:: |
|
627 | 631 | |
|
628 | 632 | In [1]: %reset -f |
|
629 | 633 | |
|
630 | 634 | Now, with a clean namespace we can make a few variables and use |
|
631 | 635 | ``%reset_selective`` to only delete names that match our regexp:: |
|
632 | 636 | |
|
633 | 637 | In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8 |
|
634 | 638 | |
|
635 | 639 | In [3]: who_ls |
|
636 | 640 | Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c'] |
|
637 | 641 | |
|
638 | 642 | In [4]: %reset_selective -f b[2-3]m |
|
639 | 643 | |
|
640 | 644 | In [5]: who_ls |
|
641 | 645 | Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c'] |
|
642 | 646 | |
|
643 | 647 | In [6]: %reset_selective -f d |
|
644 | 648 | |
|
645 | 649 | In [7]: who_ls |
|
646 | 650 | Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c'] |
|
647 | 651 | |
|
648 | 652 | In [8]: %reset_selective -f c |
|
649 | 653 | |
|
650 | 654 | In [9]: who_ls |
|
651 | 655 | Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m'] |
|
652 | 656 | |
|
653 | 657 | In [10]: %reset_selective -f b |
|
654 | 658 | |
|
655 | 659 | In [11]: who_ls |
|
656 | 660 | Out[11]: ['a'] |
|
657 | 661 | |
|
658 | 662 | Notes |
|
659 | 663 | ----- |
|
660 | 664 | Calling this magic from clients that do not implement standard input, |
|
661 | 665 | such as the ipython notebook interface, will reset the namespace |
|
662 | 666 | without confirmation. |
|
663 | 667 | """ |
|
664 | 668 | |
|
665 | 669 | opts, regex = self.parse_options(parameter_s,'f') |
|
666 | 670 | |
|
667 | 671 | if 'f' in opts: |
|
668 | 672 | ans = True |
|
669 | 673 | else: |
|
670 | 674 | try: |
|
671 | 675 | ans = self.shell.ask_yes_no( |
|
672 | 676 | "Once deleted, variables cannot be recovered. Proceed (y/[n])? ", |
|
673 | 677 | default='n') |
|
674 | 678 | except StdinNotImplementedError: |
|
675 | 679 | ans = True |
|
676 | 680 | if not ans: |
|
677 | 681 | print('Nothing done.') |
|
678 | 682 | return |
|
679 | 683 | user_ns = self.shell.user_ns |
|
680 | 684 | if not regex: |
|
681 | 685 | print('No regex pattern specified. Nothing done.') |
|
682 | 686 | return |
|
683 | 687 | else: |
|
684 | 688 | try: |
|
685 | 689 | m = re.compile(regex) |
|
686 | 690 | except TypeError as e: |
|
687 | 691 | raise TypeError('regex must be a string or compiled pattern') from e |
|
688 | 692 | for i in self.who_ls(): |
|
689 | 693 | if m.search(i): |
|
690 | 694 | del(user_ns[i]) |
|
691 | 695 | |
|
692 | 696 | @line_magic |
|
693 | 697 | def xdel(self, parameter_s=''): |
|
694 | 698 | """Delete a variable, trying to clear it from anywhere that |
|
695 | 699 | IPython's machinery has references to it. By default, this uses |
|
696 | 700 | the identity of the named object in the user namespace to remove |
|
697 | 701 | references held under other names. The object is also removed |
|
698 | 702 | from the output history. |
|
699 | 703 | |
|
700 | 704 | Options |
|
701 | 705 | -n : Delete the specified name from all namespaces, without |
|
702 | 706 | checking their identity. |
|
703 | 707 | """ |
|
704 | 708 | opts, varname = self.parse_options(parameter_s,'n') |
|
705 | 709 | try: |
|
706 | 710 | self.shell.del_var(varname, ('n' in opts)) |
|
707 | 711 | except (NameError, ValueError) as e: |
|
708 | 712 | print(type(e).__name__ +": "+ str(e)) |
@@ -1,469 +1,470 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | A mixin for :class:`~IPython.core.application.Application` classes that |
|
4 | 4 | launch InteractiveShell instances, load extensions, etc. |
|
5 | 5 | """ |
|
6 | 6 | |
|
7 | 7 | # Copyright (c) IPython Development Team. |
|
8 | 8 | # Distributed under the terms of the Modified BSD License. |
|
9 | 9 | |
|
10 | 10 | import glob |
|
11 | 11 | from itertools import chain |
|
12 | 12 | import os |
|
13 | 13 | import sys |
|
14 | 14 | |
|
15 | 15 | from traitlets.config.application import boolean_flag |
|
16 | 16 | from traitlets.config.configurable import Configurable |
|
17 | 17 | from traitlets.config.loader import Config |
|
18 | 18 | from IPython.core.application import SYSTEM_CONFIG_DIRS, ENV_CONFIG_DIRS |
|
19 | 19 | from IPython.core import pylabtools |
|
20 | 20 | from IPython.utils.contexts import preserve_keys |
|
21 | 21 | from IPython.utils.path import filefind |
|
22 | 22 | import traitlets |
|
23 | 23 | from traitlets import ( |
|
24 | 24 | Unicode, Instance, List, Bool, CaselessStrEnum, observe, |
|
25 | 25 | DottedObjectName, |
|
26 | 26 | ) |
|
27 | 27 | from IPython.terminal import pt_inputhooks |
|
28 | 28 | |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | # Aliases and Flags |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | |
|
33 | 33 | gui_keys = tuple(sorted(pt_inputhooks.backends) + sorted(pt_inputhooks.aliases)) |
|
34 | 34 | |
|
35 | 35 | backend_keys = sorted(pylabtools.backends.keys()) |
|
36 | 36 | backend_keys.insert(0, 'auto') |
|
37 | 37 | |
|
38 | 38 | shell_flags = {} |
|
39 | 39 | |
|
40 | 40 | addflag = lambda *args: shell_flags.update(boolean_flag(*args)) |
|
41 | 41 | addflag('autoindent', 'InteractiveShell.autoindent', |
|
42 | 42 | 'Turn on autoindenting.', 'Turn off autoindenting.' |
|
43 | 43 | ) |
|
44 | 44 | addflag('automagic', 'InteractiveShell.automagic', |
|
45 | 45 | """Turn on the auto calling of magic commands. Type %%magic at the |
|
46 | 46 | IPython prompt for more information.""", |
|
47 | 47 | 'Turn off the auto calling of magic commands.' |
|
48 | 48 | ) |
|
49 | 49 | addflag('pdb', 'InteractiveShell.pdb', |
|
50 | 50 | "Enable auto calling the pdb debugger after every exception.", |
|
51 | 51 | "Disable auto calling the pdb debugger after every exception." |
|
52 | 52 | ) |
|
53 | 53 | addflag('pprint', 'PlainTextFormatter.pprint', |
|
54 | 54 | "Enable auto pretty printing of results.", |
|
55 | 55 | "Disable auto pretty printing of results." |
|
56 | 56 | ) |
|
57 | 57 | addflag('color-info', 'InteractiveShell.color_info', |
|
58 | 58 | """IPython can display information about objects via a set of functions, |
|
59 | 59 | and optionally can use colors for this, syntax highlighting |
|
60 | 60 | source code and various other elements. This is on by default, but can cause |
|
61 | 61 | problems with some pagers. If you see such problems, you can disable the |
|
62 | 62 | colours.""", |
|
63 | 63 | "Disable using colors for info related things." |
|
64 | 64 | ) |
|
65 | 65 | addflag('ignore-cwd', 'InteractiveShellApp.ignore_cwd', |
|
66 | 66 | "Exclude the current working directory from sys.path", |
|
67 | 67 | "Include the current working directory in sys.path", |
|
68 | 68 | ) |
|
69 | 69 | nosep_config = Config() |
|
70 | 70 | nosep_config.InteractiveShell.separate_in = '' |
|
71 | 71 | nosep_config.InteractiveShell.separate_out = '' |
|
72 | 72 | nosep_config.InteractiveShell.separate_out2 = '' |
|
73 | 73 | |
|
74 | 74 | shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.") |
|
75 | 75 | shell_flags['pylab'] = ( |
|
76 | 76 | {'InteractiveShellApp' : {'pylab' : 'auto'}}, |
|
77 | 77 | """Pre-load matplotlib and numpy for interactive use with |
|
78 | 78 | the default matplotlib backend.""" |
|
79 | 79 | ) |
|
80 | 80 | shell_flags['matplotlib'] = ( |
|
81 | 81 | {'InteractiveShellApp' : {'matplotlib' : 'auto'}}, |
|
82 | 82 | """Configure matplotlib for interactive use with |
|
83 | 83 | the default matplotlib backend.""" |
|
84 | 84 | ) |
|
85 | 85 | |
|
86 | 86 | # it's possible we don't want short aliases for *all* of these: |
|
87 | 87 | shell_aliases = dict( |
|
88 | 88 | autocall='InteractiveShell.autocall', |
|
89 | 89 | colors='InteractiveShell.colors', |
|
90 | 90 | logfile='InteractiveShell.logfile', |
|
91 | 91 | logappend='InteractiveShell.logappend', |
|
92 | 92 | c='InteractiveShellApp.code_to_run', |
|
93 | 93 | m='InteractiveShellApp.module_to_run', |
|
94 | 94 | ext="InteractiveShellApp.extra_extensions", |
|
95 | 95 | gui='InteractiveShellApp.gui', |
|
96 | 96 | pylab='InteractiveShellApp.pylab', |
|
97 | 97 | matplotlib='InteractiveShellApp.matplotlib', |
|
98 | 98 | ) |
|
99 | 99 | shell_aliases['cache-size'] = 'InteractiveShell.cache_size' |
|
100 | 100 | |
|
101 | 101 | if traitlets.version_info < (5, 0): |
|
102 | 102 | # traitlets 4 doesn't handle lists on CLI |
|
103 | 103 | shell_aliases["ext"] = "InteractiveShellApp.extra_extension" |
|
104 | 104 | |
|
105 | 105 | |
|
106 | 106 | #----------------------------------------------------------------------------- |
|
107 | 107 | # Main classes and functions |
|
108 | 108 | #----------------------------------------------------------------------------- |
|
109 | 109 | |
|
110 | 110 | class InteractiveShellApp(Configurable): |
|
111 | 111 | """A Mixin for applications that start InteractiveShell instances. |
|
112 | 112 | |
|
113 | 113 | Provides configurables for loading extensions and executing files |
|
114 | 114 | as part of configuring a Shell environment. |
|
115 | 115 | |
|
116 | 116 | The following methods should be called by the :meth:`initialize` method |
|
117 | 117 | of the subclass: |
|
118 | 118 | |
|
119 | 119 | - :meth:`init_path` |
|
120 | 120 | - :meth:`init_shell` (to be implemented by the subclass) |
|
121 | 121 | - :meth:`init_gui_pylab` |
|
122 | 122 | - :meth:`init_extensions` |
|
123 | 123 | - :meth:`init_code` |
|
124 | 124 | """ |
|
125 | 125 | extensions = List(Unicode(), |
|
126 | 126 | help="A list of dotted module names of IPython extensions to load." |
|
127 | 127 | ).tag(config=True) |
|
128 | 128 | |
|
129 | 129 | extra_extension = Unicode( |
|
130 | 130 | "", |
|
131 | 131 | help=""" |
|
132 | 132 | DEPRECATED. Dotted module name of a single extra IPython extension to load. |
|
133 | 133 | |
|
134 | 134 | Only one extension can be added this way. |
|
135 | 135 | |
|
136 | 136 | Only used with traitlets < 5.0, plural extra_extensions list is used in traitlets 5. |
|
137 | 137 | """, |
|
138 | 138 | ).tag(config=True) |
|
139 | 139 | |
|
140 | 140 | extra_extensions = List( |
|
141 | 141 | DottedObjectName(), |
|
142 | 142 | help=""" |
|
143 | 143 | Dotted module name(s) of one or more IPython extensions to load. |
|
144 | 144 | |
|
145 | 145 | For specifying extra extensions to load on the command-line. |
|
146 | 146 | |
|
147 | 147 | .. versionadded:: 7.10 |
|
148 | 148 | """, |
|
149 | 149 | ).tag(config=True) |
|
150 | 150 | |
|
151 | 151 | reraise_ipython_extension_failures = Bool(False, |
|
152 | 152 | help="Reraise exceptions encountered loading IPython extensions?", |
|
153 | 153 | ).tag(config=True) |
|
154 | 154 | |
|
155 | 155 | # Extensions that are always loaded (not configurable) |
|
156 | 156 | default_extensions = List(Unicode(), [u'storemagic']).tag(config=False) |
|
157 | 157 | |
|
158 | 158 | hide_initial_ns = Bool(True, |
|
159 | 159 | help="""Should variables loaded at startup (by startup files, exec_lines, etc.) |
|
160 | 160 | be hidden from tools like %who?""" |
|
161 | 161 | ).tag(config=True) |
|
162 | 162 | |
|
163 | 163 | exec_files = List(Unicode(), |
|
164 | 164 | help="""List of files to run at IPython startup.""" |
|
165 | 165 | ).tag(config=True) |
|
166 | 166 | exec_PYTHONSTARTUP = Bool(True, |
|
167 | 167 | help="""Run the file referenced by the PYTHONSTARTUP environment |
|
168 | 168 | variable at IPython startup.""" |
|
169 | 169 | ).tag(config=True) |
|
170 | 170 | file_to_run = Unicode('', |
|
171 | 171 | help="""A file to be run""").tag(config=True) |
|
172 | 172 | |
|
173 | 173 | exec_lines = List(Unicode(), |
|
174 | 174 | help="""lines of code to run at IPython startup.""" |
|
175 | 175 | ).tag(config=True) |
|
176 | 176 | code_to_run = Unicode('', |
|
177 | 177 | help="Execute the given command string." |
|
178 | 178 | ).tag(config=True) |
|
179 | 179 | module_to_run = Unicode('', |
|
180 | 180 | help="Run the module as a script." |
|
181 | 181 | ).tag(config=True) |
|
182 | 182 | gui = CaselessStrEnum(gui_keys, allow_none=True, |
|
183 | 183 | help="Enable GUI event loop integration with any of {0}.".format(gui_keys) |
|
184 | 184 | ).tag(config=True) |
|
185 | 185 | matplotlib = CaselessStrEnum(backend_keys, allow_none=True, |
|
186 | 186 | help="""Configure matplotlib for interactive use with |
|
187 | 187 | the default matplotlib backend.""" |
|
188 | 188 | ).tag(config=True) |
|
189 | 189 | pylab = CaselessStrEnum(backend_keys, allow_none=True, |
|
190 | 190 | help="""Pre-load matplotlib and numpy for interactive use, |
|
191 | 191 | selecting a particular matplotlib backend and loop integration. |
|
192 | 192 | """ |
|
193 | 193 | ).tag(config=True) |
|
194 | 194 | pylab_import_all = Bool(True, |
|
195 | 195 | help="""If true, IPython will populate the user namespace with numpy, pylab, etc. |
|
196 | 196 | and an ``import *`` is done from numpy and pylab, when using pylab mode. |
|
197 | 197 | |
|
198 | 198 | When False, pylab mode should not import any names into the user namespace. |
|
199 | 199 | """ |
|
200 | 200 | ).tag(config=True) |
|
201 | 201 | ignore_cwd = Bool( |
|
202 | 202 | False, |
|
203 | 203 | help="""If True, IPython will not add the current working directory to sys.path. |
|
204 | 204 | When False, the current working directory is added to sys.path, allowing imports |
|
205 | 205 | of modules defined in the current directory.""" |
|
206 | 206 | ).tag(config=True) |
|
207 | 207 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', |
|
208 | 208 | allow_none=True) |
|
209 | 209 | # whether interact-loop should start |
|
210 | 210 | interact = Bool(True) |
|
211 | 211 | |
|
212 | 212 | user_ns = Instance(dict, args=None, allow_none=True) |
|
213 | 213 | @observe('user_ns') |
|
214 | 214 | def _user_ns_changed(self, change): |
|
215 | 215 | if self.shell is not None: |
|
216 | 216 | self.shell.user_ns = change['new'] |
|
217 | 217 | self.shell.init_user_ns() |
|
218 | 218 | |
|
219 | 219 | def init_path(self): |
|
220 | 220 | """Add current working directory, '', to sys.path |
|
221 | 221 | |
|
222 | 222 | Unlike Python's default, we insert before the first `site-packages` |
|
223 | 223 | or `dist-packages` directory, |
|
224 | 224 | so that it is after the standard library. |
|
225 | 225 | |
|
226 | 226 | .. versionchanged:: 7.2 |
|
227 | 227 | Try to insert after the standard library, instead of first. |
|
228 | 228 | .. versionchanged:: 8.0 |
|
229 | 229 | Allow optionally not including the current directory in sys.path |
|
230 | 230 | """ |
|
231 | 231 | if '' in sys.path or self.ignore_cwd: |
|
232 | 232 | return |
|
233 | 233 | for idx, path in enumerate(sys.path): |
|
234 | 234 | parent, last_part = os.path.split(path) |
|
235 | 235 | if last_part in {'site-packages', 'dist-packages'}: |
|
236 | 236 | break |
|
237 | 237 | else: |
|
238 | 238 | # no site-packages or dist-packages found (?!) |
|
239 | 239 | # back to original behavior of inserting at the front |
|
240 | 240 | idx = 0 |
|
241 | 241 | sys.path.insert(idx, '') |
|
242 | 242 | |
|
243 | 243 | def init_shell(self): |
|
244 | 244 | raise NotImplementedError("Override in subclasses") |
|
245 | 245 | |
|
246 | 246 | def init_gui_pylab(self): |
|
247 | 247 | """Enable GUI event loop integration, taking pylab into account.""" |
|
248 | 248 | enable = False |
|
249 | 249 | shell = self.shell |
|
250 | 250 | if self.pylab: |
|
251 | 251 | enable = lambda key: shell.enable_pylab(key, import_all=self.pylab_import_all) |
|
252 | 252 | key = self.pylab |
|
253 | 253 | elif self.matplotlib: |
|
254 | 254 | enable = shell.enable_matplotlib |
|
255 | 255 | key = self.matplotlib |
|
256 | 256 | elif self.gui: |
|
257 | 257 | enable = shell.enable_gui |
|
258 | 258 | key = self.gui |
|
259 | 259 | |
|
260 | 260 | if not enable: |
|
261 | 261 | return |
|
262 | 262 | |
|
263 | 263 | try: |
|
264 | 264 | r = enable(key) |
|
265 | 265 | except ImportError: |
|
266 | 266 | self.log.warning("Eventloop or matplotlib integration failed. Is matplotlib installed?") |
|
267 | 267 | self.shell.showtraceback() |
|
268 | 268 | return |
|
269 | 269 | except Exception: |
|
270 | 270 | self.log.warning("GUI event loop or pylab initialization failed") |
|
271 | 271 | self.shell.showtraceback() |
|
272 | 272 | return |
|
273 | 273 | |
|
274 | 274 | if isinstance(r, tuple): |
|
275 | 275 | gui, backend = r[:2] |
|
276 | 276 | self.log.info("Enabling GUI event loop integration, " |
|
277 | 277 | "eventloop=%s, matplotlib=%s", gui, backend) |
|
278 | 278 | if key == "auto": |
|
279 | 279 | print("Using matplotlib backend: %s" % backend) |
|
280 | 280 | else: |
|
281 | 281 | gui = r |
|
282 | 282 | self.log.info("Enabling GUI event loop integration, " |
|
283 | 283 | "eventloop=%s", gui) |
|
284 | 284 | |
|
285 | 285 | def init_extensions(self): |
|
286 | 286 | """Load all IPython extensions in IPythonApp.extensions. |
|
287 | 287 | |
|
288 | 288 | This uses the :meth:`ExtensionManager.load_extensions` to load all |
|
289 | 289 | the extensions listed in ``self.extensions``. |
|
290 | 290 | """ |
|
291 | 291 | try: |
|
292 | 292 | self.log.debug("Loading IPython extensions...") |
|
293 | 293 | extensions = ( |
|
294 | 294 | self.default_extensions + self.extensions + self.extra_extensions |
|
295 | 295 | ) |
|
296 | 296 | if self.extra_extension: |
|
297 | 297 | extensions.append(self.extra_extension) |
|
298 | 298 | for ext in extensions: |
|
299 | 299 | try: |
|
300 | 300 | self.log.info("Loading IPython extension: %s" % ext) |
|
301 | 301 | self.shell.extension_manager.load_extension(ext) |
|
302 | 302 | except: |
|
303 | 303 | if self.reraise_ipython_extension_failures: |
|
304 | 304 | raise |
|
305 | 305 | msg = ("Error in loading extension: {ext}\n" |
|
306 | 306 | "Check your config files in {location}".format( |
|
307 | 307 | ext=ext, |
|
308 | 308 | location=self.profile_dir.location |
|
309 | 309 | )) |
|
310 | 310 | self.log.warning(msg, exc_info=True) |
|
311 | 311 | except: |
|
312 | 312 | if self.reraise_ipython_extension_failures: |
|
313 | 313 | raise |
|
314 | 314 | self.log.warning("Unknown error in loading extensions:", exc_info=True) |
|
315 | 315 | |
|
316 | 316 | def init_code(self): |
|
317 | 317 | """run the pre-flight code, specified via exec_lines""" |
|
318 | 318 | self._run_startup_files() |
|
319 | 319 | self._run_exec_lines() |
|
320 | 320 | self._run_exec_files() |
|
321 | 321 | |
|
322 | 322 | # Hide variables defined here from %who etc. |
|
323 | 323 | if self.hide_initial_ns: |
|
324 | 324 | self.shell.user_ns_hidden.update(self.shell.user_ns) |
|
325 | 325 | |
|
326 | 326 | # command-line execution (ipython -i script.py, ipython -m module) |
|
327 | 327 | # should *not* be excluded from %whos |
|
328 | 328 | self._run_cmd_line_code() |
|
329 | 329 | self._run_module() |
|
330 | 330 | |
|
331 | 331 | # flush output, so itwon't be attached to the first cell |
|
332 | 332 | sys.stdout.flush() |
|
333 | 333 | sys.stderr.flush() |
|
334 | self.shell._sys_modules_keys = set(sys.modules.keys()) | |
|
334 | 335 | |
|
335 | 336 | def _run_exec_lines(self): |
|
336 | 337 | """Run lines of code in IPythonApp.exec_lines in the user's namespace.""" |
|
337 | 338 | if not self.exec_lines: |
|
338 | 339 | return |
|
339 | 340 | try: |
|
340 | 341 | self.log.debug("Running code from IPythonApp.exec_lines...") |
|
341 | 342 | for line in self.exec_lines: |
|
342 | 343 | try: |
|
343 | 344 | self.log.info("Running code in user namespace: %s" % |
|
344 | 345 | line) |
|
345 | 346 | self.shell.run_cell(line, store_history=False) |
|
346 | 347 | except: |
|
347 | 348 | self.log.warning("Error in executing line in user " |
|
348 | 349 | "namespace: %s" % line) |
|
349 | 350 | self.shell.showtraceback() |
|
350 | 351 | except: |
|
351 | 352 | self.log.warning("Unknown error in handling IPythonApp.exec_lines:") |
|
352 | 353 | self.shell.showtraceback() |
|
353 | 354 | |
|
354 | 355 | def _exec_file(self, fname, shell_futures=False): |
|
355 | 356 | try: |
|
356 | 357 | full_filename = filefind(fname, [u'.', self.ipython_dir]) |
|
357 | 358 | except IOError: |
|
358 | 359 | self.log.warning("File not found: %r"%fname) |
|
359 | 360 | return |
|
360 | 361 | # Make sure that the running script gets a proper sys.argv as if it |
|
361 | 362 | # were run from a system shell. |
|
362 | 363 | save_argv = sys.argv |
|
363 | 364 | sys.argv = [full_filename] + self.extra_args[1:] |
|
364 | 365 | try: |
|
365 | 366 | if os.path.isfile(full_filename): |
|
366 | 367 | self.log.info("Running file in user namespace: %s" % |
|
367 | 368 | full_filename) |
|
368 | 369 | # Ensure that __file__ is always defined to match Python |
|
369 | 370 | # behavior. |
|
370 | 371 | with preserve_keys(self.shell.user_ns, '__file__'): |
|
371 | 372 | self.shell.user_ns['__file__'] = fname |
|
372 | 373 | if full_filename.endswith('.ipy') or full_filename.endswith('.ipynb'): |
|
373 | 374 | self.shell.safe_execfile_ipy(full_filename, |
|
374 | 375 | shell_futures=shell_futures) |
|
375 | 376 | else: |
|
376 | 377 | # default to python, even without extension |
|
377 | 378 | self.shell.safe_execfile(full_filename, |
|
378 | 379 | self.shell.user_ns, |
|
379 | 380 | shell_futures=shell_futures, |
|
380 | 381 | raise_exceptions=True) |
|
381 | 382 | finally: |
|
382 | 383 | sys.argv = save_argv |
|
383 | 384 | |
|
384 | 385 | def _run_startup_files(self): |
|
385 | 386 | """Run files from profile startup directory""" |
|
386 | 387 | startup_dirs = [self.profile_dir.startup_dir] + [ |
|
387 | 388 | os.path.join(p, 'startup') for p in chain(ENV_CONFIG_DIRS, SYSTEM_CONFIG_DIRS) |
|
388 | 389 | ] |
|
389 | 390 | startup_files = [] |
|
390 | 391 | |
|
391 | 392 | if self.exec_PYTHONSTARTUP and os.environ.get('PYTHONSTARTUP', False) and \ |
|
392 | 393 | not (self.file_to_run or self.code_to_run or self.module_to_run): |
|
393 | 394 | python_startup = os.environ['PYTHONSTARTUP'] |
|
394 | 395 | self.log.debug("Running PYTHONSTARTUP file %s...", python_startup) |
|
395 | 396 | try: |
|
396 | 397 | self._exec_file(python_startup) |
|
397 | 398 | except: |
|
398 | 399 | self.log.warning("Unknown error in handling PYTHONSTARTUP file %s:", python_startup) |
|
399 | 400 | self.shell.showtraceback() |
|
400 | 401 | for startup_dir in startup_dirs[::-1]: |
|
401 | 402 | startup_files += glob.glob(os.path.join(startup_dir, '*.py')) |
|
402 | 403 | startup_files += glob.glob(os.path.join(startup_dir, '*.ipy')) |
|
403 | 404 | if not startup_files: |
|
404 | 405 | return |
|
405 | 406 | |
|
406 | 407 | self.log.debug("Running startup files from %s...", startup_dir) |
|
407 | 408 | try: |
|
408 | 409 | for fname in sorted(startup_files): |
|
409 | 410 | self._exec_file(fname) |
|
410 | 411 | except: |
|
411 | 412 | self.log.warning("Unknown error in handling startup files:") |
|
412 | 413 | self.shell.showtraceback() |
|
413 | 414 | |
|
414 | 415 | def _run_exec_files(self): |
|
415 | 416 | """Run files from IPythonApp.exec_files""" |
|
416 | 417 | if not self.exec_files: |
|
417 | 418 | return |
|
418 | 419 | |
|
419 | 420 | self.log.debug("Running files in IPythonApp.exec_files...") |
|
420 | 421 | try: |
|
421 | 422 | for fname in self.exec_files: |
|
422 | 423 | self._exec_file(fname) |
|
423 | 424 | except: |
|
424 | 425 | self.log.warning("Unknown error in handling IPythonApp.exec_files:") |
|
425 | 426 | self.shell.showtraceback() |
|
426 | 427 | |
|
427 | 428 | def _run_cmd_line_code(self): |
|
428 | 429 | """Run code or file specified at the command-line""" |
|
429 | 430 | if self.code_to_run: |
|
430 | 431 | line = self.code_to_run |
|
431 | 432 | try: |
|
432 | 433 | self.log.info("Running code given at command line (c=): %s" % |
|
433 | 434 | line) |
|
434 | 435 | self.shell.run_cell(line, store_history=False) |
|
435 | 436 | except: |
|
436 | 437 | self.log.warning("Error in executing line in user namespace: %s" % |
|
437 | 438 | line) |
|
438 | 439 | self.shell.showtraceback() |
|
439 | 440 | if not self.interact: |
|
440 | 441 | self.exit(1) |
|
441 | 442 | |
|
442 | 443 | # Like Python itself, ignore the second if the first of these is present |
|
443 | 444 | elif self.file_to_run: |
|
444 | 445 | fname = self.file_to_run |
|
445 | 446 | if os.path.isdir(fname): |
|
446 | 447 | fname = os.path.join(fname, "__main__.py") |
|
447 | 448 | if not os.path.exists(fname): |
|
448 | 449 | self.log.warning("File '%s' doesn't exist", fname) |
|
449 | 450 | if not self.interact: |
|
450 | 451 | self.exit(2) |
|
451 | 452 | try: |
|
452 | 453 | self._exec_file(fname, shell_futures=True) |
|
453 | 454 | except: |
|
454 | 455 | self.shell.showtraceback(tb_offset=4) |
|
455 | 456 | if not self.interact: |
|
456 | 457 | self.exit(1) |
|
457 | 458 | |
|
458 | 459 | def _run_module(self): |
|
459 | 460 | """Run module specified at the command-line.""" |
|
460 | 461 | if self.module_to_run: |
|
461 | 462 | # Make sure that the module gets a proper sys.argv as if it were |
|
462 | 463 | # run using `python -m`. |
|
463 | 464 | save_argv = sys.argv |
|
464 | 465 | sys.argv = [sys.executable] + self.extra_args |
|
465 | 466 | try: |
|
466 | 467 | self.shell.safe_run_module(self.module_to_run, |
|
467 | 468 | self.shell.user_ns) |
|
468 | 469 | finally: |
|
469 | 470 | sys.argv = save_argv |
General Comments 0
You need to be logged in to leave comments.
Login now