Mercurial > cpython-lru_cache-weakref
annotate Mac/Tools/IDE/PyInteractive.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 95ea66122325 |
| children | c3a096bfbd6b |
| rev | line source |
|---|---|
| 10047 | 1 import string |
| 2 import sys | |
| 3 import traceback | |
| 4 | |
| 5 | |
| 6 try: | |
| 7 sys.ps1 | |
| 8 except AttributeError: | |
| 9 sys.ps1 = ">>> " | |
| 10 try: | |
| 11 sys.ps2 | |
| 12 except AttributeError: | |
| 13 sys.ps2 = "... " | |
| 14 | |
| 15 | |
| 16 def print_exc(limit=None, file=None): | |
| 17 if not file: | |
| 18 file = sys.stderr | |
| 19 # we're going to skip the outermost traceback object, we don't | |
| 20 # want people to see the line which excecuted their code. | |
| 21 tb = sys.exc_traceback | |
| 22 if tb: | |
| 23 tb = tb.tb_next | |
| 24 try: | |
| 25 sys.last_type = sys.exc_type | |
| 26 sys.last_value = sys.exc_value | |
| 27 sys.last_traceback = tb | |
| 28 traceback.print_exception(sys.last_type, sys.last_value, | |
| 29 sys.last_traceback, limit, file) | |
| 30 except: | |
| 31 print '--- hola! ---' | |
| 32 traceback.print_exception(sys.exc_type, sys.exc_value, | |
| 33 sys.exc_traceback, limit, file) | |
| 34 | |
| 35 | |
| 36 class PyInteractive: | |
| 37 | |
| 38 def __init__(self): | |
|
19794
95ea66122325
Patch by Mark Day to allow from __future__ imports. Looks harmless
Jack Jansen <jack.jansen@cwi.nl>
parents:
10047
diff
changeset
|
39 import codeop |
| 10047 | 40 self._pybuf = "" |
|
19794
95ea66122325
Patch by Mark Day to allow from __future__ imports. Looks harmless
Jack Jansen <jack.jansen@cwi.nl>
parents:
10047
diff
changeset
|
41 self._compile = codeop.Compile() |
| 10047 | 42 |
| 43 def executeline(self, stuff, out = None, env = None): | |
| 44 if env is None: | |
| 45 import __main__ | |
| 46 env = __main__.__dict__ | |
| 47 if out: | |
| 48 saveerr, saveout = sys.stderr, sys.stdout | |
| 49 sys.stderr = sys.stdout = out | |
| 50 try: | |
| 51 if self._pybuf: | |
| 52 self._pybuf = self._pybuf + '\n' + stuff | |
| 53 else: | |
| 54 self._pybuf = stuff | |
| 55 | |
| 56 # Compile three times: as is, with \n, and with \n\n appended. | |
| 57 # If it compiles as is, it's complete. If it compiles with | |
| 58 # one \n appended, we expect more. If it doesn't compile | |
| 59 # either way, we compare the error we get when compiling with | |
| 60 # \n or \n\n appended. If the errors are the same, the code | |
| 61 # is broken. But if the errors are different, we expect more. | |
| 62 # Not intuitive; not even guaranteed to hold in future | |
| 63 # releases; but this matches the compiler's behavior in Python | |
| 64 # 1.4 and 1.5. | |
| 65 err = err1 = err2 = None | |
| 66 code = code1 = code2 = None | |
| 67 | |
| 68 # quickly get out of here when the line is 'empty' or is a comment | |
| 69 stripped = string.strip(self._pybuf) | |
| 70 if not stripped or stripped[0] == '#': | |
| 71 self._pybuf = '' | |
| 72 sys.stdout.write(sys.ps1) | |
| 73 sys.stdout.flush() | |
| 74 return | |
| 75 | |
| 76 try: | |
|
19794
95ea66122325
Patch by Mark Day to allow from __future__ imports. Looks harmless
Jack Jansen <jack.jansen@cwi.nl>
parents:
10047
diff
changeset
|
77 code = self._compile(self._pybuf, "<input>", "single") |
| 10047 | 78 except SyntaxError, err: |
| 79 pass | |
| 80 except: | |
| 81 # OverflowError. More? | |
| 82 print_exc() | |
| 83 self._pybuf = "" | |
| 84 sys.stdout.write(sys.ps1) | |
| 85 sys.stdout.flush() | |
| 86 return | |
| 87 | |
| 88 try: | |
|
19794
95ea66122325
Patch by Mark Day to allow from __future__ imports. Looks harmless
Jack Jansen <jack.jansen@cwi.nl>
parents:
10047
diff
changeset
|
89 code1 = self._compile(self._pybuf + "\n", "<input>", "single") |
| 10047 | 90 except SyntaxError, err1: |
| 91 pass | |
| 92 | |
| 93 try: | |
|
19794
95ea66122325
Patch by Mark Day to allow from __future__ imports. Looks harmless
Jack Jansen <jack.jansen@cwi.nl>
parents:
10047
diff
changeset
|
94 code2 = self._compile(self._pybuf + "\n\n", "<input>", "single") |
| 10047 | 95 except SyntaxError, err2: |
| 96 pass | |
| 97 | |
| 98 if code: | |
| 99 try: | |
| 100 exec code in env | |
| 101 except: | |
| 102 print_exc() | |
| 103 self._pybuf = "" | |
| 104 elif code1: | |
| 105 pass | |
| 106 elif err1 == err2 or (not stuff and self._pybuf): | |
| 107 print_exc() | |
| 108 self._pybuf = "" | |
| 109 if self._pybuf: | |
| 110 sys.stdout.write(sys.ps2) | |
| 111 sys.stdout.flush() | |
| 112 else: | |
| 113 sys.stdout.write(sys.ps1) | |
| 114 sys.stdout.flush() | |
| 115 finally: | |
| 116 if out: | |
| 117 sys.stderr, sys.stdout = saveerr, saveout |
