Show More
@@ -1,348 +1,348 | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | Paging capabilities for IPython.core |
|
3 | Paging capabilities for IPython.core | |
4 |
|
4 | |||
5 | Notes |
|
5 | Notes | |
6 | ----- |
|
6 | ----- | |
7 |
|
7 | |||
8 | For now this uses IPython hooks, so it can't be in IPython.utils. If we can get |
|
8 | For now this uses IPython hooks, so it can't be in IPython.utils. If we can get | |
9 | rid of that dependency, we could move it there. |
|
9 | rid of that dependency, we could move it there. | |
10 | ----- |
|
10 | ----- | |
11 | """ |
|
11 | """ | |
12 |
|
12 | |||
13 | # Copyright (c) IPython Development Team. |
|
13 | # Copyright (c) IPython Development Team. | |
14 | # Distributed under the terms of the Modified BSD License. |
|
14 | # Distributed under the terms of the Modified BSD License. | |
15 |
|
15 | |||
16 |
|
16 | |||
17 | import os |
|
17 | import os | |
18 | import io |
|
18 | import io | |
19 | import re |
|
19 | import re | |
20 | import sys |
|
20 | import sys | |
21 | import tempfile |
|
21 | import tempfile | |
22 | import subprocess |
|
22 | import subprocess | |
23 |
|
23 | |||
24 | from io import UnsupportedOperation |
|
24 | from io import UnsupportedOperation | |
25 | from pathlib import Path |
|
25 | from pathlib import Path | |
26 |
|
26 | |||
27 | from IPython import get_ipython |
|
27 | from IPython import get_ipython | |
28 | from IPython.display import display |
|
28 | from IPython.display import display | |
29 | from IPython.core.error import TryNext |
|
29 | from IPython.core.error import TryNext | |
30 | from IPython.utils.data import chop |
|
30 | from IPython.utils.data import chop | |
31 | from IPython.utils.process import system |
|
31 | from IPython.utils.process import system | |
32 | from IPython.utils.terminal import get_terminal_size |
|
32 | from IPython.utils.terminal import get_terminal_size | |
33 | from IPython.utils import py3compat |
|
33 | from IPython.utils import py3compat | |
34 |
|
34 | |||
35 |
|
35 | |||
36 | def display_page(strng, start=0, screen_lines=25): |
|
36 | def display_page(strng, start=0, screen_lines=25): | |
37 | """Just display, no paging. screen_lines is ignored.""" |
|
37 | """Just display, no paging. screen_lines is ignored.""" | |
38 | if isinstance(strng, dict): |
|
38 | if isinstance(strng, dict): | |
39 | data = strng |
|
39 | data = strng | |
40 | else: |
|
40 | else: | |
41 | if start: |
|
41 | if start: | |
42 | strng = u'\n'.join(strng.splitlines()[start:]) |
|
42 | strng = u'\n'.join(strng.splitlines()[start:]) | |
43 | data = { 'text/plain': strng } |
|
43 | data = { 'text/plain': strng } | |
44 | display(data, raw=True) |
|
44 | display(data, raw=True) | |
45 |
|
45 | |||
46 |
|
46 | |||
47 | def as_hook(page_func): |
|
47 | def as_hook(page_func): | |
48 | """Wrap a pager func to strip the `self` arg |
|
48 | """Wrap a pager func to strip the `self` arg | |
49 |
|
49 | |||
50 | so it can be called as a hook. |
|
50 | so it can be called as a hook. | |
51 | """ |
|
51 | """ | |
52 | return lambda self, *args, **kwargs: page_func(*args, **kwargs) |
|
52 | return lambda self, *args, **kwargs: page_func(*args, **kwargs) | |
53 |
|
53 | |||
54 |
|
54 | |||
55 | esc_re = re.compile(r"(\x1b[^m]+m)") |
|
55 | esc_re = re.compile(r"(\x1b[^m]+m)") | |
56 |
|
56 | |||
57 | def page_dumb(strng, start=0, screen_lines=25): |
|
57 | def page_dumb(strng, start=0, screen_lines=25): | |
58 | """Very dumb 'pager' in Python, for when nothing else works. |
|
58 | """Very dumb 'pager' in Python, for when nothing else works. | |
59 |
|
59 | |||
60 | Only moves forward, same interface as page(), except for pager_cmd and |
|
60 | Only moves forward, same interface as page(), except for pager_cmd and | |
61 | mode. |
|
61 | mode. | |
62 | """ |
|
62 | """ | |
63 | if isinstance(strng, dict): |
|
63 | if isinstance(strng, dict): | |
64 | strng = strng.get('text/plain', '') |
|
64 | strng = strng.get('text/plain', '') | |
65 | out_ln = strng.splitlines()[start:] |
|
65 | out_ln = strng.splitlines()[start:] | |
66 | screens = chop(out_ln,screen_lines-1) |
|
66 | screens = chop(out_ln,screen_lines-1) | |
67 | if len(screens) == 1: |
|
67 | if len(screens) == 1: | |
68 | print(os.linesep.join(screens[0])) |
|
68 | print(os.linesep.join(screens[0])) | |
69 | else: |
|
69 | else: | |
70 | last_escape = "" |
|
70 | last_escape = "" | |
71 | for scr in screens[0:-1]: |
|
71 | for scr in screens[0:-1]: | |
72 | hunk = os.linesep.join(scr) |
|
72 | hunk = os.linesep.join(scr) | |
73 | print(last_escape + hunk) |
|
73 | print(last_escape + hunk) | |
74 | if not page_more(): |
|
74 | if not page_more(): | |
75 | return |
|
75 | return | |
76 | esc_list = esc_re.findall(hunk) |
|
76 | esc_list = esc_re.findall(hunk) | |
77 | if len(esc_list) > 0: |
|
77 | if len(esc_list) > 0: | |
78 | last_escape = esc_list[-1] |
|
78 | last_escape = esc_list[-1] | |
79 | print(last_escape + os.linesep.join(screens[-1])) |
|
79 | print(last_escape + os.linesep.join(screens[-1])) | |
80 |
|
80 | |||
81 | def _detect_screen_size(screen_lines_def): |
|
81 | def _detect_screen_size(screen_lines_def): | |
82 | """Attempt to work out the number of lines on the screen. |
|
82 | """Attempt to work out the number of lines on the screen. | |
83 |
|
83 | |||
84 | This is called by page(). It can raise an error (e.g. when run in the |
|
84 | This is called by page(). It can raise an error (e.g. when run in the | |
85 | test suite), so it's separated out so it can easily be called in a try block. |
|
85 | test suite), so it's separated out so it can easily be called in a try block. | |
86 | """ |
|
86 | """ | |
87 | TERM = os.environ.get('TERM',None) |
|
87 | TERM = os.environ.get('TERM',None) | |
88 | if not((TERM=='xterm' or TERM=='xterm-color') and sys.platform != 'sunos5'): |
|
88 | if not((TERM=='xterm' or TERM=='xterm-color') and sys.platform != 'sunos5'): | |
89 | # curses causes problems on many terminals other than xterm, and |
|
89 | # curses causes problems on many terminals other than xterm, and | |
90 | # some termios calls lock up on Sun OS5. |
|
90 | # some termios calls lock up on Sun OS5. | |
91 | return screen_lines_def |
|
91 | return screen_lines_def | |
92 |
|
92 | |||
93 | try: |
|
93 | try: | |
94 | import termios |
|
94 | import termios | |
95 | import curses |
|
95 | import curses | |
96 | except ImportError: |
|
96 | except ImportError: | |
97 | return screen_lines_def |
|
97 | return screen_lines_def | |
98 |
|
98 | |||
99 | # There is a bug in curses, where *sometimes* it fails to properly |
|
99 | # There is a bug in curses, where *sometimes* it fails to properly | |
100 | # initialize, and then after the endwin() call is made, the |
|
100 | # initialize, and then after the endwin() call is made, the | |
101 | # terminal is left in an unusable state. Rather than trying to |
|
101 | # terminal is left in an unusable state. Rather than trying to | |
102 | # check every time for this (by requesting and comparing termios |
|
102 | # check every time for this (by requesting and comparing termios | |
103 | # flags each time), we just save the initial terminal state and |
|
103 | # flags each time), we just save the initial terminal state and | |
104 | # unconditionally reset it every time. It's cheaper than making |
|
104 | # unconditionally reset it every time. It's cheaper than making | |
105 | # the checks. |
|
105 | # the checks. | |
106 | try: |
|
106 | try: | |
107 | term_flags = termios.tcgetattr(sys.stdout) |
|
107 | term_flags = termios.tcgetattr(sys.stdout) | |
108 | except termios.error as err: |
|
108 | except termios.error as err: | |
109 | # can fail on Linux 2.6, pager_page will catch the TypeError |
|
109 | # can fail on Linux 2.6, pager_page will catch the TypeError | |
110 | raise TypeError('termios error: {0}'.format(err)) from err |
|
110 | raise TypeError('termios error: {0}'.format(err)) from err | |
111 |
|
111 | |||
112 | try: |
|
112 | try: | |
113 | scr = curses.initscr() |
|
113 | scr = curses.initscr() | |
114 | except AttributeError: |
|
114 | except AttributeError: | |
115 | # Curses on Solaris may not be complete, so we can't use it there |
|
115 | # Curses on Solaris may not be complete, so we can't use it there | |
116 | return screen_lines_def |
|
116 | return screen_lines_def | |
117 |
|
117 | |||
118 | screen_lines_real,screen_cols = scr.getmaxyx() |
|
118 | screen_lines_real,screen_cols = scr.getmaxyx() | |
119 | curses.endwin() |
|
119 | curses.endwin() | |
120 |
|
120 | |||
121 | # Restore terminal state in case endwin() didn't. |
|
121 | # Restore terminal state in case endwin() didn't. | |
122 | termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags) |
|
122 | termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags) | |
123 | # Now we have what we needed: the screen size in rows/columns |
|
123 | # Now we have what we needed: the screen size in rows/columns | |
124 | return screen_lines_real |
|
124 | return screen_lines_real | |
125 | # print('***Screen size:',screen_lines_real,'lines x', |
|
125 | # print('***Screen size:',screen_lines_real,'lines x', | |
126 | # screen_cols,'columns.') # dbg |
|
126 | # screen_cols,'columns.') # dbg | |
127 |
|
127 | |||
128 | def pager_page(strng, start=0, screen_lines=0, pager_cmd=None): |
|
128 | def pager_page(strng, start=0, screen_lines=0, pager_cmd=None) -> None: | |
129 | """Display a string, piping through a pager after a certain length. |
|
129 | """Display a string, piping through a pager after a certain length. | |
130 |
|
130 | |||
131 | strng can be a mime-bundle dict, supplying multiple representations, |
|
131 | strng can be a mime-bundle dict, supplying multiple representations, | |
132 | keyed by mime-type. |
|
132 | keyed by mime-type. | |
133 |
|
133 | |||
134 | The screen_lines parameter specifies the number of *usable* lines of your |
|
134 | The screen_lines parameter specifies the number of *usable* lines of your | |
135 | terminal screen (total lines minus lines you need to reserve to show other |
|
135 | terminal screen (total lines minus lines you need to reserve to show other | |
136 | information). |
|
136 | information). | |
137 |
|
137 | |||
138 | If you set screen_lines to a number <=0, page() will try to auto-determine |
|
138 | If you set screen_lines to a number <=0, page() will try to auto-determine | |
139 | your screen size and will only use up to (screen_size+screen_lines) for |
|
139 | your screen size and will only use up to (screen_size+screen_lines) for | |
140 | printing, paging after that. That is, if you want auto-detection but need |
|
140 | printing, paging after that. That is, if you want auto-detection but need | |
141 | to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for |
|
141 | to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for | |
142 | auto-detection without any lines reserved simply use screen_lines = 0. |
|
142 | auto-detection without any lines reserved simply use screen_lines = 0. | |
143 |
|
143 | |||
144 | If a string won't fit in the allowed lines, it is sent through the |
|
144 | If a string won't fit in the allowed lines, it is sent through the | |
145 | specified pager command. If none given, look for PAGER in the environment, |
|
145 | specified pager command. If none given, look for PAGER in the environment, | |
146 | and ultimately default to less. |
|
146 | and ultimately default to less. | |
147 |
|
147 | |||
148 | If no system pager works, the string is sent through a 'dumb pager' |
|
148 | If no system pager works, the string is sent through a 'dumb pager' | |
149 | written in python, very simplistic. |
|
149 | written in python, very simplistic. | |
150 | """ |
|
150 | """ | |
151 |
|
151 | |||
152 | # for compatibility with mime-bundle form: |
|
152 | # for compatibility with mime-bundle form: | |
153 | if isinstance(strng, dict): |
|
153 | if isinstance(strng, dict): | |
154 | strng = strng['text/plain'] |
|
154 | strng = strng['text/plain'] | |
155 |
|
155 | |||
156 | # Ugly kludge, but calling curses.initscr() flat out crashes in emacs |
|
156 | # Ugly kludge, but calling curses.initscr() flat out crashes in emacs | |
157 | TERM = os.environ.get('TERM','dumb') |
|
157 | TERM = os.environ.get('TERM','dumb') | |
158 | if TERM in ['dumb','emacs'] and os.name != 'nt': |
|
158 | if TERM in ['dumb','emacs'] and os.name != 'nt': | |
159 | print(strng) |
|
159 | print(strng) | |
160 | return |
|
160 | return | |
161 | # chop off the topmost part of the string we don't want to see |
|
161 | # chop off the topmost part of the string we don't want to see | |
162 | str_lines = strng.splitlines()[start:] |
|
162 | str_lines = strng.splitlines()[start:] | |
163 | str_toprint = os.linesep.join(str_lines) |
|
163 | str_toprint = os.linesep.join(str_lines) | |
164 | num_newlines = len(str_lines) |
|
164 | num_newlines = len(str_lines) | |
165 | len_str = len(str_toprint) |
|
165 | len_str = len(str_toprint) | |
166 |
|
166 | |||
167 | # Dumb heuristics to guesstimate number of on-screen lines the string |
|
167 | # Dumb heuristics to guesstimate number of on-screen lines the string | |
168 | # takes. Very basic, but good enough for docstrings in reasonable |
|
168 | # takes. Very basic, but good enough for docstrings in reasonable | |
169 | # terminals. If someone later feels like refining it, it's not hard. |
|
169 | # terminals. If someone later feels like refining it, it's not hard. | |
170 | numlines = max(num_newlines,int(len_str/80)+1) |
|
170 | numlines = max(num_newlines,int(len_str/80)+1) | |
171 |
|
171 | |||
172 | screen_lines_def = get_terminal_size()[1] |
|
172 | screen_lines_def = get_terminal_size()[1] | |
173 |
|
173 | |||
174 | # auto-determine screen size |
|
174 | # auto-determine screen size | |
175 | if screen_lines <= 0: |
|
175 | if screen_lines <= 0: | |
176 | try: |
|
176 | try: | |
177 | screen_lines += _detect_screen_size(screen_lines_def) |
|
177 | screen_lines += _detect_screen_size(screen_lines_def) | |
178 | except (TypeError, UnsupportedOperation): |
|
178 | except (TypeError, UnsupportedOperation): | |
179 | print(str_toprint) |
|
179 | print(str_toprint) | |
180 | return |
|
180 | return | |
181 |
|
181 | |||
182 | # print('numlines',numlines,'screenlines',screen_lines) # dbg |
|
182 | # print('numlines',numlines,'screenlines',screen_lines) # dbg | |
183 | if numlines <= screen_lines : |
|
183 | if numlines <= screen_lines : | |
184 | # print('*** normal print') # dbg |
|
184 | # print('*** normal print') # dbg | |
185 | print(str_toprint) |
|
185 | print(str_toprint) | |
186 | else: |
|
186 | else: | |
187 | # Try to open pager and default to internal one if that fails. |
|
187 | # Try to open pager and default to internal one if that fails. | |
188 | # All failure modes are tagged as 'retval=1', to match the return |
|
188 | # All failure modes are tagged as 'retval=1', to match the return | |
189 | # value of a failed system command. If any intermediate attempt |
|
189 | # value of a failed system command. If any intermediate attempt | |
190 | # sets retval to 1, at the end we resort to our own page_dumb() pager. |
|
190 | # sets retval to 1, at the end we resort to our own page_dumb() pager. | |
191 | pager_cmd = get_pager_cmd(pager_cmd) |
|
191 | pager_cmd = get_pager_cmd(pager_cmd) | |
192 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) |
|
192 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) | |
193 | if os.name == 'nt': |
|
193 | if os.name == 'nt': | |
194 | if pager_cmd.startswith('type'): |
|
194 | if pager_cmd.startswith('type'): | |
195 | # The default WinXP 'type' command is failing on complex strings. |
|
195 | # The default WinXP 'type' command is failing on complex strings. | |
196 | retval = 1 |
|
196 | retval = 1 | |
197 | else: |
|
197 | else: | |
198 | fd, tmpname = tempfile.mkstemp('.txt') |
|
198 | fd, tmpname = tempfile.mkstemp('.txt') | |
199 | tmppath = Path(tmpname) |
|
199 | tmppath = Path(tmpname) | |
200 | try: |
|
200 | try: | |
201 | os.close(fd) |
|
201 | os.close(fd) | |
202 | with tmppath.open("wt", encoding="utf-8") as tmpfile: |
|
202 | with tmppath.open("wt", encoding="utf-8") as tmpfile: | |
203 | tmpfile.write(strng) |
|
203 | tmpfile.write(strng) | |
204 | cmd = "%s < %s" % (pager_cmd, tmppath) |
|
204 | cmd = "%s < %s" % (pager_cmd, tmppath) | |
205 | # tmpfile needs to be closed for windows |
|
205 | # tmpfile needs to be closed for windows | |
206 | if os.system(cmd): |
|
206 | if os.system(cmd): | |
207 | retval = 1 |
|
207 | retval = 1 | |
208 | else: |
|
208 | else: | |
209 | retval = None |
|
209 | retval = None | |
210 | finally: |
|
210 | finally: | |
211 | Path.unlink(tmppath) |
|
211 | Path.unlink(tmppath) | |
212 | else: |
|
212 | else: | |
213 | try: |
|
213 | try: | |
214 | retval = None |
|
214 | retval = None | |
215 | # Emulate os.popen, but redirect stderr |
|
215 | # Emulate os.popen, but redirect stderr | |
216 | proc = subprocess.Popen( |
|
216 | proc = subprocess.Popen( | |
217 | pager_cmd, |
|
217 | pager_cmd, | |
218 | shell=True, |
|
218 | shell=True, | |
219 | stdin=subprocess.PIPE, |
|
219 | stdin=subprocess.PIPE, | |
220 | stderr=subprocess.DEVNULL, |
|
220 | stderr=subprocess.DEVNULL, | |
221 | ) |
|
221 | ) | |
222 | pager = os._wrap_close( |
|
222 | pager = os._wrap_close( | |
223 | io.TextIOWrapper(proc.stdin, encoding="utf-8"), proc |
|
223 | io.TextIOWrapper(proc.stdin, encoding="utf-8"), proc | |
224 | ) |
|
224 | ) | |
225 | try: |
|
225 | try: | |
226 | pager_encoding = pager.encoding or sys.stdout.encoding |
|
226 | pager_encoding = pager.encoding or sys.stdout.encoding | |
227 | pager.write(strng) |
|
227 | pager.write(strng) | |
228 | finally: |
|
228 | finally: | |
229 | retval = pager.close() |
|
229 | retval = pager.close() | |
230 | except IOError as msg: # broken pipe when user quits |
|
230 | except IOError as msg: # broken pipe when user quits | |
231 | if msg.args == (32, 'Broken pipe'): |
|
231 | if msg.args == (32, 'Broken pipe'): | |
232 | retval = None |
|
232 | retval = None | |
233 | else: |
|
233 | else: | |
234 | retval = 1 |
|
234 | retval = 1 | |
235 | except OSError: |
|
235 | except OSError: | |
236 | # Other strange problems, sometimes seen in Win2k/cygwin |
|
236 | # Other strange problems, sometimes seen in Win2k/cygwin | |
237 | retval = 1 |
|
237 | retval = 1 | |
238 | if retval is not None: |
|
238 | if retval is not None: | |
239 | page_dumb(strng,screen_lines=screen_lines) |
|
239 | page_dumb(strng,screen_lines=screen_lines) | |
240 |
|
240 | |||
241 |
|
241 | |||
242 | def page(data, start=0, screen_lines=0, pager_cmd=None): |
|
242 | def page(data, start: int = 0, screen_lines: int = 0, pager_cmd=None): | |
243 | """Display content in a pager, piping through a pager after a certain length. |
|
243 | """Display content in a pager, piping through a pager after a certain length. | |
244 |
|
244 | |||
245 | data can be a mime-bundle dict, supplying multiple representations, |
|
245 | data can be a mime-bundle dict, supplying multiple representations, | |
246 | keyed by mime-type, or text. |
|
246 | keyed by mime-type, or text. | |
247 |
|
247 | |||
248 | Pager is dispatched via the `show_in_pager` IPython hook. |
|
248 | Pager is dispatched via the `show_in_pager` IPython hook. | |
249 | If no hook is registered, `pager_page` will be used. |
|
249 | If no hook is registered, `pager_page` will be used. | |
250 | """ |
|
250 | """ | |
251 | # Some routines may auto-compute start offsets incorrectly and pass a |
|
251 | # Some routines may auto-compute start offsets incorrectly and pass a | |
252 | # negative value. Offset to 0 for robustness. |
|
252 | # negative value. Offset to 0 for robustness. | |
253 | start = max(0, start) |
|
253 | start = max(0, start) | |
254 |
|
254 | |||
255 | # first, try the hook |
|
255 | # first, try the hook | |
256 | ip = get_ipython() |
|
256 | ip = get_ipython() | |
257 | if ip: |
|
257 | if ip: | |
258 | try: |
|
258 | try: | |
259 | ip.hooks.show_in_pager(data, start=start, screen_lines=screen_lines) |
|
259 | ip.hooks.show_in_pager(data, start=start, screen_lines=screen_lines) | |
260 | return |
|
260 | return | |
261 | except TryNext: |
|
261 | except TryNext: | |
262 | pass |
|
262 | pass | |
263 |
|
263 | |||
264 | # fallback on default pager |
|
264 | # fallback on default pager | |
265 | return pager_page(data, start, screen_lines, pager_cmd) |
|
265 | return pager_page(data, start, screen_lines, pager_cmd) | |
266 |
|
266 | |||
267 |
|
267 | |||
268 | def page_file(fname, start=0, pager_cmd=None): |
|
268 | def page_file(fname, start=0, pager_cmd=None): | |
269 | """Page a file, using an optional pager command and starting line. |
|
269 | """Page a file, using an optional pager command and starting line. | |
270 | """ |
|
270 | """ | |
271 |
|
271 | |||
272 | pager_cmd = get_pager_cmd(pager_cmd) |
|
272 | pager_cmd = get_pager_cmd(pager_cmd) | |
273 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) |
|
273 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) | |
274 |
|
274 | |||
275 | try: |
|
275 | try: | |
276 | if os.environ['TERM'] in ['emacs','dumb']: |
|
276 | if os.environ['TERM'] in ['emacs','dumb']: | |
277 | raise EnvironmentError |
|
277 | raise EnvironmentError | |
278 | system(pager_cmd + ' ' + fname) |
|
278 | system(pager_cmd + ' ' + fname) | |
279 | except: |
|
279 | except: | |
280 | try: |
|
280 | try: | |
281 | if start > 0: |
|
281 | if start > 0: | |
282 | start -= 1 |
|
282 | start -= 1 | |
283 | page(open(fname, encoding="utf-8").read(), start) |
|
283 | page(open(fname, encoding="utf-8").read(), start) | |
284 | except: |
|
284 | except: | |
285 | print('Unable to show file',repr(fname)) |
|
285 | print('Unable to show file',repr(fname)) | |
286 |
|
286 | |||
287 |
|
287 | |||
288 | def get_pager_cmd(pager_cmd=None): |
|
288 | def get_pager_cmd(pager_cmd=None): | |
289 | """Return a pager command. |
|
289 | """Return a pager command. | |
290 |
|
290 | |||
291 | Makes some attempts at finding an OS-correct one. |
|
291 | Makes some attempts at finding an OS-correct one. | |
292 | """ |
|
292 | """ | |
293 | if os.name == 'posix': |
|
293 | if os.name == 'posix': | |
294 | default_pager_cmd = 'less -R' # -R for color control sequences |
|
294 | default_pager_cmd = 'less -R' # -R for color control sequences | |
295 | elif os.name in ['nt','dos']: |
|
295 | elif os.name in ['nt','dos']: | |
296 | default_pager_cmd = 'type' |
|
296 | default_pager_cmd = 'type' | |
297 |
|
297 | |||
298 | if pager_cmd is None: |
|
298 | if pager_cmd is None: | |
299 | try: |
|
299 | try: | |
300 | pager_cmd = os.environ['PAGER'] |
|
300 | pager_cmd = os.environ['PAGER'] | |
301 | except: |
|
301 | except: | |
302 | pager_cmd = default_pager_cmd |
|
302 | pager_cmd = default_pager_cmd | |
303 |
|
303 | |||
304 | if pager_cmd == 'less' and '-r' not in os.environ.get('LESS', '').lower(): |
|
304 | if pager_cmd == 'less' and '-r' not in os.environ.get('LESS', '').lower(): | |
305 | pager_cmd += ' -R' |
|
305 | pager_cmd += ' -R' | |
306 |
|
306 | |||
307 | return pager_cmd |
|
307 | return pager_cmd | |
308 |
|
308 | |||
309 |
|
309 | |||
310 | def get_pager_start(pager, start): |
|
310 | def get_pager_start(pager, start): | |
311 | """Return the string for paging files with an offset. |
|
311 | """Return the string for paging files with an offset. | |
312 |
|
312 | |||
313 | This is the '+N' argument which less and more (under Unix) accept. |
|
313 | This is the '+N' argument which less and more (under Unix) accept. | |
314 | """ |
|
314 | """ | |
315 |
|
315 | |||
316 | if pager in ['less','more']: |
|
316 | if pager in ['less','more']: | |
317 | if start: |
|
317 | if start: | |
318 | start_string = '+' + str(start) |
|
318 | start_string = '+' + str(start) | |
319 | else: |
|
319 | else: | |
320 | start_string = '' |
|
320 | start_string = '' | |
321 | else: |
|
321 | else: | |
322 | start_string = '' |
|
322 | start_string = '' | |
323 | return start_string |
|
323 | return start_string | |
324 |
|
324 | |||
325 |
|
325 | |||
326 | # (X)emacs on win32 doesn't like to be bypassed with msvcrt.getch() |
|
326 | # (X)emacs on win32 doesn't like to be bypassed with msvcrt.getch() | |
327 | if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs': |
|
327 | if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs': | |
328 | import msvcrt |
|
328 | import msvcrt | |
329 | def page_more(): |
|
329 | def page_more(): | |
330 | """ Smart pausing between pages |
|
330 | """ Smart pausing between pages | |
331 |
|
331 | |||
332 | @return: True if need print more lines, False if quit |
|
332 | @return: True if need print more lines, False if quit | |
333 | """ |
|
333 | """ | |
334 | sys.stdout.write('---Return to continue, q to quit--- ') |
|
334 | sys.stdout.write('---Return to continue, q to quit--- ') | |
335 | ans = msvcrt.getwch() |
|
335 | ans = msvcrt.getwch() | |
336 | if ans in ("q", "Q"): |
|
336 | if ans in ("q", "Q"): | |
337 | result = False |
|
337 | result = False | |
338 | else: |
|
338 | else: | |
339 | result = True |
|
339 | result = True | |
340 | sys.stdout.write("\b"*37 + " "*37 + "\b"*37) |
|
340 | sys.stdout.write("\b"*37 + " "*37 + "\b"*37) | |
341 | return result |
|
341 | return result | |
342 | else: |
|
342 | else: | |
343 | def page_more(): |
|
343 | def page_more(): | |
344 | ans = py3compat.input('---Return to continue, q to quit--- ') |
|
344 | ans = py3compat.input('---Return to continue, q to quit--- ') | |
345 | if ans.lower().startswith('q'): |
|
345 | if ans.lower().startswith('q'): | |
346 | return False |
|
346 | return False | |
347 | else: |
|
347 | else: | |
348 | return True |
|
348 | return True |
@@ -1,92 +1,94 | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | Utilities for working with stack frames. |
|
3 | Utilities for working with stack frames. | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 | # Copyright (C) 2008-2011 The IPython Development Team |
|
7 | # Copyright (C) 2008-2011 The IPython Development Team | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | # Distributed under the terms of the BSD License. The full license is in | |
10 | # the file COPYING, distributed as part of this software. |
|
10 | # the file COPYING, distributed as part of this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | import sys |
|
17 | import sys | |
|
18 | from typing import Any | |||
18 |
|
19 | |||
19 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
20 | # Code |
|
21 | # Code | |
21 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
22 |
|
23 | |||
23 | def extract_vars(*names,**kw): |
|
24 | def extract_vars(*names,**kw): | |
24 | """Extract a set of variables by name from another frame. |
|
25 | """Extract a set of variables by name from another frame. | |
25 |
|
26 | |||
26 | Parameters |
|
27 | Parameters | |
27 | ---------- |
|
28 | ---------- | |
28 | *names : str |
|
29 | *names : str | |
29 | One or more variable names which will be extracted from the caller's |
|
30 | One or more variable names which will be extracted from the caller's | |
30 | frame. |
|
31 | frame. | |
31 | **kw : integer, optional |
|
32 | **kw : integer, optional | |
32 | How many frames in the stack to walk when looking for your variables. |
|
33 | How many frames in the stack to walk when looking for your variables. | |
33 | The default is 0, which will use the frame where the call was made. |
|
34 | The default is 0, which will use the frame where the call was made. | |
34 |
|
35 | |||
35 | Examples |
|
36 | Examples | |
36 | -------- |
|
37 | -------- | |
37 | :: |
|
38 | :: | |
38 |
|
39 | |||
39 | In [2]: def func(x): |
|
40 | In [2]: def func(x): | |
40 | ...: y = 1 |
|
41 | ...: y = 1 | |
41 | ...: print(sorted(extract_vars('x','y').items())) |
|
42 | ...: print(sorted(extract_vars('x','y').items())) | |
42 | ...: |
|
43 | ...: | |
43 |
|
44 | |||
44 | In [3]: func('hello') |
|
45 | In [3]: func('hello') | |
45 | [('x', 'hello'), ('y', 1)] |
|
46 | [('x', 'hello'), ('y', 1)] | |
46 | """ |
|
47 | """ | |
47 |
|
48 | |||
48 | depth = kw.get('depth',0) |
|
49 | depth = kw.get('depth',0) | |
49 |
|
50 | |||
50 | callerNS = sys._getframe(depth+1).f_locals |
|
51 | callerNS = sys._getframe(depth+1).f_locals | |
51 | return dict((k,callerNS[k]) for k in names) |
|
52 | return dict((k,callerNS[k]) for k in names) | |
52 |
|
53 | |||
53 |
|
54 | |||
54 | def extract_vars_above(*names): |
|
55 | def extract_vars_above(*names: list[str]): | |
55 | """Extract a set of variables by name from another frame. |
|
56 | """Extract a set of variables by name from another frame. | |
56 |
|
57 | |||
57 | Similar to extractVars(), but with a specified depth of 1, so that names |
|
58 | Similar to extractVars(), but with a specified depth of 1, so that names | |
58 | are extracted exactly from above the caller. |
|
59 | are extracted exactly from above the caller. | |
59 |
|
60 | |||
60 | This is simply a convenience function so that the very common case (for us) |
|
61 | This is simply a convenience function so that the very common case (for us) | |
61 | of skipping exactly 1 frame doesn't have to construct a special dict for |
|
62 | of skipping exactly 1 frame doesn't have to construct a special dict for | |
62 | keyword passing.""" |
|
63 | keyword passing.""" | |
63 |
|
64 | |||
64 | callerNS = sys._getframe(2).f_locals |
|
65 | callerNS = sys._getframe(2).f_locals | |
65 | return dict((k,callerNS[k]) for k in names) |
|
66 | return dict((k,callerNS[k]) for k in names) | |
66 |
|
67 | |||
67 |
|
68 | |||
68 |
def debugx(expr,pre_msg= |
|
69 | def debugx(expr: str, pre_msg: str = ""): | |
69 | """Print the value of an expression from the caller's frame. |
|
70 | """Print the value of an expression from the caller's frame. | |
70 |
|
71 | |||
71 | Takes an expression, evaluates it in the caller's frame and prints both |
|
72 | Takes an expression, evaluates it in the caller's frame and prints both | |
72 | the given expression and the resulting value (as well as a debug mark |
|
73 | the given expression and the resulting value (as well as a debug mark | |
73 | indicating the name of the calling function. The input must be of a form |
|
74 | indicating the name of the calling function. The input must be of a form | |
74 | suitable for eval(). |
|
75 | suitable for eval(). | |
75 |
|
76 | |||
76 | An optional message can be passed, which will be prepended to the printed |
|
77 | An optional message can be passed, which will be prepended to the printed | |
77 | expr->value pair.""" |
|
78 | expr->value pair.""" | |
78 |
|
79 | |||
79 | cf = sys._getframe(1) |
|
80 | cf = sys._getframe(1) | |
80 | print('[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr, |
|
81 | print('[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr, | |
81 | eval(expr,cf.f_globals,cf.f_locals))) |
|
82 | eval(expr,cf.f_globals,cf.f_locals))) | |
82 |
|
83 | |||
83 |
|
84 | |||
84 | # deactivate it by uncommenting the following line, which makes it a no-op |
|
85 | # deactivate it by uncommenting the following line, which makes it a no-op | |
85 | #def debugx(expr,pre_msg=''): pass |
|
86 | #def debugx(expr,pre_msg=''): pass | |
86 |
|
87 | |||
87 | def extract_module_locals(depth=0): |
|
88 | ||
|
89 | def extract_module_locals(depth: int = 0) -> tuple[Any, Any]: | |||
88 | """Returns (module, locals) of the function `depth` frames away from the caller""" |
|
90 | """Returns (module, locals) of the function `depth` frames away from the caller""" | |
89 | f = sys._getframe(depth + 1) |
|
91 | f = sys._getframe(depth + 1) | |
90 | global_ns = f.f_globals |
|
92 | global_ns = f.f_globals | |
91 | module = sys.modules[global_ns['__name__']] |
|
93 | module = sys.modules[global_ns['__name__']] | |
92 | return (module, f.f_locals) |
|
94 | return (module, f.f_locals) |
General Comments 0
You need to be logged in to leave comments.
Login now