|
1 | | -# encoding: utf-8 |
2 | | - |
3 | | -# The MIT License |
4 | | -# |
5 | | -# Copyright (c) 2009-2011 the bpython authors. |
6 | | -# Copyright (c) 2012-2013,2015 Sebastian Ramacher |
7 | | -# |
8 | | -# Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | | -# of this software and associated documentation files (the "Software"), to deal |
10 | | -# in the Software without restriction, including without limitation the rights |
11 | | -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12 | | -# copies of the Software, and to permit persons to whom the Software is |
13 | | -# furnished to do so, subject to the following conditions: |
14 | | -# |
15 | | -# The above copyright notice and this permission notice shall be included in |
16 | | -# all copies or substantial portions of the Software. |
17 | | -# |
18 | | -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | | -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20 | | -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21 | | -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22 | | -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23 | | -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24 | | -# THE SOFTWARE. |
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +The MIT License |
| 5 | +
|
| 6 | +Copyright (c) 2009-2011 the bpython authors. |
| 7 | +Copyright (c) 2012-2013,2015 Sebastian Ramacher |
| 8 | +
|
| 9 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +of this software and associated documentation files (the "Software"), to deal |
| 11 | +in the Software without restriction, including without limitation the rights |
| 12 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +copies of the Software, and to permit persons to whom the Software is |
| 14 | +furnished to do so, subject to the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included in |
| 17 | +all copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +THE SOFTWARE. |
| 26 | +""" |
25 | 27 |
|
26 | 28 | import code |
27 | 29 | import inspect |
|
56 | 58 |
|
57 | 59 |
|
58 | 60 | class RuntimeTimer(object): |
| 61 | + """Calculate running time""" |
59 | 62 | def __init__(self): |
60 | 63 | self.reset_timer() |
61 | 64 | self.time = time.monotonic if hasattr(time, 'monotonic') else time.time |
@@ -207,7 +210,7 @@ def showtraceback(self): |
207 | 210 | tblist[i] = (fname, lineno, module, something) |
208 | 211 | # Set the right lineno (encoding header adds an extra line) |
209 | 212 | if fname == '<input>' and not py3: |
210 | | - tblist[i] = (fname, lineno - 2, module, something) |
| 213 | + tblist[i] = (fname, lineno - 2, module, something) |
211 | 214 |
|
212 | 215 | l = traceback.format_list(tblist) |
213 | 216 | if l: |
@@ -601,7 +604,7 @@ def get_args(self): |
601 | 604 | try: |
602 | 605 | fake_cursor = self.current_line.index(func) + len(func) |
603 | 606 | f = simpleeval.evaluate_current_attribute( |
604 | | - fake_cursor, self.current_line, self.interp.locals) |
| 607 | + fake_cursor, self.current_line, self.interp.locals) |
605 | 608 | except simpleeval.EvaluationError: |
606 | 609 | return False |
607 | 610 | except Exception: |
@@ -770,13 +773,13 @@ def line_is_empty(line): |
770 | 773 | indentation = 0 |
771 | 774 | return indentation |
772 | 775 |
|
773 | | - def formatforfile(self, s): |
| 776 | + def formatforfile(self, stdout): |
774 | 777 | """Format the stdout buffer to something suitable for writing to disk, |
775 | 778 | i.e. without >>> and ... at input lines and with "# OUT: " prepended to |
776 | 779 | output lines.""" |
777 | 780 |
|
778 | 781 | def process(): |
779 | | - for line in s.split('\n'): |
| 782 | + for line in stdout.split('\n'): |
780 | 783 | if line.startswith(self.ps1): |
781 | 784 | yield line[len(self.ps1):] |
782 | 785 | elif line.startswith(self.ps2): |
@@ -817,11 +820,11 @@ def write2file(self): |
817 | 820 | self.interact.notify(_('Save cancelled.')) |
818 | 821 | return |
819 | 822 |
|
820 | | - s = self.formatforfile(self.getstdout()) |
| 823 | + stdout_text = self.formatforfile(self.getstdout()) |
821 | 824 |
|
822 | 825 | try: |
823 | 826 | with open(fn, mode) as f: |
824 | | - f.write(s) |
| 827 | + f.write(stdout_text) |
825 | 828 | except IOError as e: |
826 | 829 | self.interact.notify(_("Error writing file '%s': %s") % (fn, e)) |
827 | 830 | else: |
@@ -859,8 +862,8 @@ def do_pastebin(self, s): |
859 | 862 | if s == self.prev_pastebin_content: |
860 | 863 | self.interact.notify(_('Duplicate pastebin. Previous URL: %s. ' |
861 | 864 | 'Removal URL: %s') % |
862 | | - (self.prev_pastebin_url, |
863 | | - self.prev_removal_url), 10) |
| 865 | + (self.prev_pastebin_url, |
| 866 | + self.prev_removal_url), 10) |
864 | 867 | return self.prev_pastebin_url |
865 | 868 |
|
866 | 869 | self.interact.notify(_('Posting data to pastebin...')) |
@@ -1069,8 +1072,9 @@ def tokenize(self, s, newline=False): |
1069 | 1072 | def clear_current_line(self): |
1070 | 1073 | """This is used as the exception callback for the Interpreter instance. |
1071 | 1074 | It prevents autoindentation from occurring after a traceback.""" |
| 1075 | + # XXX: Empty function |
1072 | 1076 |
|
1073 | | - def send_to_external_editor(self, text, filename=None): |
| 1077 | + def send_to_external_editor(self, text): |
1074 | 1078 | """Returns modified text from an editor, or the original text if editor |
1075 | 1079 | exited with non-zero""" |
1076 | 1080 |
|
@@ -1099,15 +1103,15 @@ def open_in_external_editor(self, filename): |
1099 | 1103 | return subprocess.call(args) == 0 |
1100 | 1104 |
|
1101 | 1105 | def edit_config(self): |
1102 | | - if not (os.path.isfile(self.config.config_path)): |
| 1106 | + if not os.path.isfile(self.config.config_path): |
1103 | 1107 | if self.interact.confirm(_("Config file does not exist - create " |
1104 | 1108 | "new from default? (y/N)")): |
1105 | 1109 | try: |
1106 | 1110 | default_config = pkgutil.get_data('bpython', |
1107 | 1111 | 'sample-config') |
1108 | 1112 | if py3: # py3 files need unicode |
1109 | 1113 | default_config = default_config.decode('ascii') |
1110 | | - bpython_dir, script_name = os.path.split(__file__) |
| 1114 | + #bpython_dir, script_name = os.path.split(__file__) |
1111 | 1115 | containing_dir = os.path.dirname( |
1112 | 1116 | os.path.abspath(self.config.config_path)) |
1113 | 1117 | if not os.path.exists(containing_dir): |
@@ -1141,10 +1145,10 @@ def next_indentation(line, tab_length): |
1141 | 1145 | return indentation |
1142 | 1146 |
|
1143 | 1147 |
|
1144 | | -def next_token_inside_string(s, inside_string): |
| 1148 | +def next_token_inside_string(code_string, inside_string): |
1145 | 1149 | """Given a code string s and an initial state inside_string, return |
1146 | 1150 | whether the next token will be inside a string or not.""" |
1147 | | - for token, value in PythonLexer().get_tokens(s): |
| 1151 | + for token, value in PythonLexer().get_tokens(code_string): |
1148 | 1152 | if token is Token.String: |
1149 | 1153 | value = value.lstrip('bBrRuU') |
1150 | 1154 | if value in ['"""', "'''", '"', "'"]: |
|
0 commit comments