From 1a9d9554bcee466394990535e190d55008904df8 2022-02-05 06:52:25 From: gousaiyang Date: 2022-02-05 06:52:25 Subject: [PATCH] Format code --- diff --git a/IPython/core/application.py b/IPython/core/application.py index 90ecf13..0cdea5c 100644 --- a/IPython/core/application.py +++ b/IPython/core/application.py @@ -470,7 +470,7 @@ class BaseIPythonApplication(Application): config_file = Path(self.profile_dir.location) / self.config_file_name if self.overwrite or not config_file.exists(): self.log.warning("Generating default config file: %r" % (config_file)) - config_file.write_text(s, encoding='utf-8') + config_file.write_text(s, encoding="utf-8") @catch_config_error def initialize(self, argv=None): diff --git a/IPython/core/crashhandler.py b/IPython/core/crashhandler.py index 3ee4879..4af3936 100644 --- a/IPython/core/crashhandler.py +++ b/IPython/core/crashhandler.py @@ -185,7 +185,7 @@ class CrashHandler(object): # and generate a complete report on disk try: - report = open(report_name, 'w', encoding='utf-8') + report = open(report_name, "w", encoding="utf-8") except: print('Could not create crash report on disk.', file=sys.stderr) return diff --git a/IPython/core/display.py b/IPython/core/display.py index 608188c..d36a176 100644 --- a/IPython/core/display.py +++ b/IPython/core/display.py @@ -349,7 +349,7 @@ class DisplayObject(object): def reload(self): """Reload the raw data from file or URL.""" if self.filename is not None: - encoding = None if 'b' in self._read_flags else 'utf-8' + encoding = None if "b" in self._read_flags else "utf-8" with open(self.filename, self._read_flags, encoding=encoding) as f: self.data = f.read() elif self.url is not None: @@ -370,8 +370,11 @@ class DisplayObject(object): if 'gzip' in response.headers['content-encoding']: import gzip from io import BytesIO + # assume utf-8 if encoding is not specified - with gzip.open(BytesIO(data), 'rt', encoding=encoding or 'utf-8') as fp: + with gzip.open( + BytesIO(data), "rt", encoding=encoding or "utf-8" + ) as fp: encoding = None data = fp.read() diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 65c3635..4f3105a 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2626,7 +2626,7 @@ class InteractiveShell(SingletonConfigurable): # Make sure we can open the file try: - with fname.open('rb'): + with fname.open("rb"): pass except: warn('Could not open file <%s> for safe execution.' % fname) @@ -2684,7 +2684,7 @@ class InteractiveShell(SingletonConfigurable): # Make sure we can open the file try: - with fname.open('rb'): + with fname.open("rb"): pass except: warn('Could not open file <%s> for safe execution.' % fname) @@ -2706,7 +2706,7 @@ class InteractiveShell(SingletonConfigurable): if cell.cell_type == 'code': yield cell.source else: - yield fname.read_text(encoding='utf-8') + yield fname.read_text(encoding="utf-8") with prepended_to_syspath(dname): try: @@ -3458,7 +3458,7 @@ class InteractiveShell(SingletonConfigurable): self.tempfiles.append(file_path) if data: - file_path.write_text(data, encoding='utf-8') + file_path.write_text(data, encoding="utf-8") return filename def ask_yes_no(self, prompt, default=None, interrupt=None): diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index 6f44018..65ba52b 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -538,7 +538,7 @@ class CodeMagics(Magics): self.shell.hooks.editor(filename) # and make a new macro object, to replace the old one - mvalue = Path(filename).read_text(encoding='utf-8') + mvalue = Path(filename).read_text(encoding="utf-8") self.shell.user_ns[mname] = Macro(mvalue) @skip_doctest @@ -728,7 +728,7 @@ class CodeMagics(Magics): # XXX TODO: should this be generalized for all string vars? # For now, this is special-cased to blocks created by cpaste if args.strip() == "pasted_block": - self.shell.user_ns["pasted_block"] = filepath.read_text(encoding='utf-8') + self.shell.user_ns["pasted_block"] = filepath.read_text(encoding="utf-8") if 'x' in opts: # -x prevents actual execution print() @@ -736,9 +736,9 @@ class CodeMagics(Magics): print('done. Executing edited code...') with preserve_keys(self.shell.user_ns, '__file__'): if not is_temp: - self.shell.user_ns['__file__'] = filename - if 'r' in opts: # Untranslated IPython code - source = filepath.read_text(encoding='utf-8') + self.shell.user_ns["__file__"] = filename + if "r" in opts: # Untranslated IPython code + source = filepath.read_text(encoding="utf-8") self.shell.run_cell(source, store_history=False) else: self.shell.safe_execfile(filename, self.shell.user_ns, @@ -746,7 +746,7 @@ class CodeMagics(Magics): if is_temp: try: - return filepath.read_text(encoding='utf-8') + return filepath.read_text(encoding="utf-8") except IOError as msg: if Path(msg.filename) == filepath: warn('File not found. Did you forget to save?') diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index a90bd4e..371da5b 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -360,7 +360,7 @@ class ExecutionMagics(Magics): if text_file: pfile = Path(text_file) pfile.touch(exist_ok=True) - pfile.write_text(output, encoding='utf-8') + pfile.write_text(output, encoding="utf-8") print( f"\n*** Profile printout saved to text file {repr(text_file)}.{sys_exit}" diff --git a/IPython/core/magics/packaging.py b/IPython/core/magics/packaging.py index 1859a94..2f7652c 100644 --- a/IPython/core/magics/packaging.py +++ b/IPython/core/magics/packaging.py @@ -32,7 +32,7 @@ def _get_conda_executable(): # Otherwise, attempt to extract the executable from conda history. # This applies in any conda environment. - history = Path(sys.prefix, "conda-meta", "history").read_text(encoding='utf-8') + history = Path(sys.prefix, "conda-meta", "history").read_text(encoding="utf-8") match = re.search( r"^#\s*cmd:\s*(?P.*conda)\s[create|install]", history, diff --git a/IPython/core/page.py b/IPython/core/page.py index 7ebb6ec..d3e6a9e 100644 --- a/IPython/core/page.py +++ b/IPython/core/page.py @@ -199,7 +199,7 @@ def pager_page(strng, start=0, screen_lines=0, pager_cmd=None): tmppath = Path(tmpname) try: os.close(fd) - with tmppath.open("wt", encoding='utf-8') as tmpfile: + with tmppath.open("wt", encoding="utf-8") as tmpfile: tmpfile.write(strng) cmd = "%s < %s" % (pager_cmd, tmppath) # tmpfile needs to be closed for windows @@ -213,12 +213,15 @@ def pager_page(strng, start=0, screen_lines=0, pager_cmd=None): try: retval = None # Emulate os.popen, but redirect stderr - proc = subprocess.Popen(pager_cmd, - shell=True, - stdin=subprocess.PIPE, - stderr=subprocess.DEVNULL - ) - pager = os._wrap_close(io.TextIOWrapper(proc.stdin, encoding='utf-8'), proc) + proc = subprocess.Popen( + pager_cmd, + shell=True, + stdin=subprocess.PIPE, + stderr=subprocess.DEVNULL, + ) + pager = os._wrap_close( + io.TextIOWrapper(proc.stdin, encoding="utf-8"), proc + ) try: pager_encoding = pager.encoding or sys.stdout.encoding pager.write(strng) @@ -277,7 +280,7 @@ def page_file(fname, start=0, pager_cmd=None): try: if start > 0: start -= 1 - page(open(fname, encoding='utf-8').read(),start) + page(open(fname, encoding="utf-8").read(), start) except: print('Unable to show file',repr(fname)) diff --git a/IPython/core/tests/test_application.py b/IPython/core/tests/test_application.py index f4b11b5..74ea0f7 100644 --- a/IPython/core/tests/test_application.py +++ b/IPython/core/tests/test_application.py @@ -34,7 +34,7 @@ def test_unicode_ipdir(): ipdir = tempfile.mkdtemp(suffix=u"€") # Create the config file, so it tries to load it. - with open(os.path.join(ipdir, 'ipython_config.py'), "w", encoding='utf-8') as f: + with open(os.path.join(ipdir, "ipython_config.py"), "w", encoding="utf-8") as f: pass old_ipdir1 = os.environ.pop("IPYTHONDIR", None) @@ -59,7 +59,7 @@ def test_cli_priority(): test = Unicode().tag(config=True) # Create the config file, so it tries to load it. - with open(os.path.join(td, 'ipython_config.py'), "w", encoding='utf-8') as f: + with open(os.path.join(td, "ipython_config.py"), "w", encoding="utf-8") as f: f.write("c.TestApp.test = 'config file'") app = TestApp() diff --git a/IPython/core/tests/test_completer.py b/IPython/core/tests/test_completer.py index 7ce160a..5f791e8 100644 --- a/IPython/core/tests/test_completer.py +++ b/IPython/core/tests/test_completer.py @@ -346,7 +346,7 @@ class TestCompleter(unittest.TestCase): suffixes = ["1", "2"] names = [prefix + s for s in suffixes] for n in names: - open(n, "w", encoding='utf-8').close() + open(n, "w", encoding="utf-8").close() # Check simple completion c = ip.complete(prefix)[1] @@ -365,7 +365,7 @@ class TestCompleter(unittest.TestCase): suffixes = ["1", "2"] names = [prefix + s for s in suffixes] for n in names: - open(n, "w", encoding='utf-8').close() + open(n, "w", encoding="utf-8").close() # Check simple completion c = ip.complete(prefix)[1] @@ -381,7 +381,7 @@ class TestCompleter(unittest.TestCase): ip = get_ipython() with TemporaryWorkingDirectory(): name = "foo'bar" - open(name, "w", encoding='utf-8').close() + open(name, "w", encoding="utf-8").close() # Don't escape Windows escaped = name if sys.platform == "win32" else "foo\\'bar" diff --git a/IPython/core/tests/test_completerlib.py b/IPython/core/tests/test_completerlib.py index 23e24e6..fbbc258 100644 --- a/IPython/core/tests/test_completerlib.py +++ b/IPython/core/tests/test_completerlib.py @@ -33,7 +33,7 @@ class Test_magic_run_completer(unittest.TestCase): def setUp(self): self.BASETESTDIR = tempfile.mkdtemp() for fil in self.files: - with open(join(self.BASETESTDIR, fil), "w", encoding='utf-8') as sfile: + with open(join(self.BASETESTDIR, fil), "w", encoding="utf-8") as sfile: sfile.write("pass\n") for d in self.dirs: os.mkdir(join(self.BASETESTDIR, d)) @@ -89,7 +89,7 @@ class Test_magic_run_completer_nonascii(unittest.TestCase): def setUp(self): self.BASETESTDIR = tempfile.mkdtemp() for fil in [u"aaø.py", u"a.py", u"b.py"]: - with open(join(self.BASETESTDIR, fil), "w", encoding='utf-8') as sfile: + with open(join(self.BASETESTDIR, fil), "w", encoding="utf-8") as sfile: sfile.write("pass\n") self.oldpath = os.getcwd() os.chdir(self.BASETESTDIR) @@ -133,8 +133,8 @@ def test_import_invalid_module(): with TemporaryDirectory() as tmpdir: sys.path.insert( 0, tmpdir ) for name in invalid_module_names | valid_module_names: - filename = os.path.join(tmpdir, name + '.py') - open(filename, 'w', encoding='utf-8').close() + filename = os.path.join(tmpdir, name + ".py") + open(filename, "w", encoding="utf-8").close() s = set( module_completion('import foo') ) intersection = s.intersection(invalid_module_names) diff --git a/IPython/core/tests/test_extension.py b/IPython/core/tests/test_extension.py index 958fd2b..59e21dc 100644 --- a/IPython/core/tests/test_extension.py +++ b/IPython/core/tests/test_extension.py @@ -26,12 +26,12 @@ def load_ipython_extension(ip): def test_extension_loading(): em = get_ipython().extension_manager with TemporaryDirectory() as td: - ext1 = os.path.join(td, 'ext1.py') - with open(ext1, 'w', encoding='utf-8') as f: + ext1 = os.path.join(td, "ext1.py") + with open(ext1, "w", encoding="utf-8") as f: f.write(ext1_content) - - ext2 = os.path.join(td, 'ext2.py') - with open(ext2, 'w', encoding='utf-8') as f: + + ext2 = os.path.join(td, "ext2.py") + with open(ext2, "w", encoding="utf-8") as f: f.write(ext2_content) with prepended_to_syspath(td): @@ -76,8 +76,8 @@ def test_extension_loading(): def test_extension_builtins(): em = get_ipython().extension_manager with TemporaryDirectory() as td: - ext3 = os.path.join(td, 'ext3.py') - with open(ext3, 'w', encoding='utf-8') as f: + ext3 = os.path.join(td, "ext3.py") + with open(ext3, "w", encoding="utf-8") as f: f.write(ext3_content) assert 'ext3' not in em.loaded diff --git a/IPython/core/tests/test_interactiveshell.py b/IPython/core/tests/test_interactiveshell.py index e86c4fc..09dbd96 100644 --- a/IPython/core/tests/test_interactiveshell.py +++ b/IPython/core/tests/test_interactiveshell.py @@ -485,12 +485,12 @@ class InteractiveShellTestCase(unittest.TestCase): def test_mktempfile(self): filename = ip.mktempfile() # Check that we can open the file again on Windows - with open(filename, 'w', encoding='utf-8') as f: - f.write('abc') + with open(filename, "w", encoding="utf-8") as f: + f.write("abc") - filename = ip.mktempfile(data='blah') - with open(filename, 'r', encoding='utf-8') as f: - self.assertEqual(f.read(), 'blah') + filename = ip.mktempfile(data="blah") + with open(filename, "r", encoding="utf-8") as f: + self.assertEqual(f.read(), "blah") def test_new_main_mod(self): # Smoketest to check that this accepts a unicode module name @@ -545,7 +545,9 @@ class TestSafeExecfileNonAsciiPath(unittest.TestCase): self.BASETESTDIR = tempfile.mkdtemp() self.TESTDIR = join(self.BASETESTDIR, u"åäö") os.mkdir(self.TESTDIR) - with open(join(self.TESTDIR, u"åäötestscript.py"), "w", encoding='utf-8') as sfile: + with open( + join(self.TESTDIR, u"åäötestscript.py"), "w", encoding="utf-8" + ) as sfile: sfile.write("pass\n") self.oldpath = os.getcwd() os.chdir(self.TESTDIR) diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index f645daf..0f6d4aa 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -861,12 +861,18 @@ def test_file(): """Basic %%writefile""" ip = get_ipython() with TemporaryDirectory() as td: - fname = os.path.join(td, 'file1') - ip.run_cell_magic("writefile", fname, u'\n'.join([ - 'line1', - 'line2', - ])) - s = Path(fname).read_text(encoding='utf-8') + fname = os.path.join(td, "file1") + ip.run_cell_magic( + "writefile", + fname, + "\n".join( + [ + "line1", + "line2", + ] + ), + ) + s = Path(fname).read_text(encoding="utf-8") assert "line1\n" in s assert "line2" in s @@ -876,12 +882,18 @@ def test_file_single_quote(): """Basic %%writefile with embedded single quotes""" ip = get_ipython() with TemporaryDirectory() as td: - fname = os.path.join(td, '\'file1\'') - ip.run_cell_magic("writefile", fname, u'\n'.join([ - 'line1', - 'line2', - ])) - s = Path(fname).read_text(encoding='utf-8') + fname = os.path.join(td, "'file1'") + ip.run_cell_magic( + "writefile", + fname, + "\n".join( + [ + "line1", + "line2", + ] + ), + ) + s = Path(fname).read_text(encoding="utf-8") assert "line1\n" in s assert "line2" in s @@ -892,11 +904,17 @@ def test_file_double_quote(): ip = get_ipython() with TemporaryDirectory() as td: fname = os.path.join(td, '"file1"') - ip.run_cell_magic("writefile", fname, u'\n'.join([ - 'line1', - 'line2', - ])) - s = Path(fname).read_text(encoding='utf-8') + ip.run_cell_magic( + "writefile", + fname, + "\n".join( + [ + "line1", + "line2", + ] + ), + ) + s = Path(fname).read_text(encoding="utf-8") assert "line1\n" in s assert "line2" in s @@ -905,13 +923,19 @@ def test_file_var_expand(): """%%writefile $filename""" ip = get_ipython() with TemporaryDirectory() as td: - fname = os.path.join(td, 'file1') - ip.user_ns['filename'] = fname - ip.run_cell_magic("writefile", '$filename', u'\n'.join([ - 'line1', - 'line2', - ])) - s = Path(fname).read_text(encoding='utf-8') + fname = os.path.join(td, "file1") + ip.user_ns["filename"] = fname + ip.run_cell_magic( + "writefile", + "$filename", + "\n".join( + [ + "line1", + "line2", + ] + ), + ) + s = Path(fname).read_text(encoding="utf-8") assert "line1\n" in s assert "line2" in s @@ -935,16 +959,28 @@ def test_file_amend(): """%%writefile -a amends files""" ip = get_ipython() with TemporaryDirectory() as td: - fname = os.path.join(td, 'file2') - ip.run_cell_magic("writefile", fname, u'\n'.join([ - 'line1', - 'line2', - ])) - ip.run_cell_magic("writefile", "-a %s" % fname, u'\n'.join([ - 'line3', - 'line4', - ])) - s = Path(fname).read_text(encoding='utf-8') + fname = os.path.join(td, "file2") + ip.run_cell_magic( + "writefile", + fname, + "\n".join( + [ + "line1", + "line2", + ] + ), + ) + ip.run_cell_magic( + "writefile", + "-a %s" % fname, + "\n".join( + [ + "line3", + "line4", + ] + ), + ) + s = Path(fname).read_text(encoding="utf-8") assert "line1\n" in s assert "line3\n" in s @@ -954,11 +990,17 @@ def test_file_spaces(): ip = get_ipython() with TemporaryWorkingDirectory() as td: fname = "file name" - ip.run_cell_magic("file", '"%s"'%fname, u'\n'.join([ - 'line1', - 'line2', - ])) - s = Path(fname).read_text(encoding='utf-8') + ip.run_cell_magic( + "file", + '"%s"' % fname, + "\n".join( + [ + "line1", + "line2", + ] + ), + ) + s = Path(fname).read_text(encoding="utf-8") assert "line1\n" in s assert "line2" in s @@ -1154,11 +1196,11 @@ def test_save(): with TemporaryDirectory() as tmpdir: file = os.path.join(tmpdir, "testsave.py") ip.run_line_magic("save", "%s 1-10" % file) - content = Path(file).read_text(encoding='utf-8') + content = Path(file).read_text(encoding="utf-8") assert content.count(cmds[0]) == 1 assert "coding: utf-8" in content ip.run_line_magic("save", "-a %s 1-10" % file) - content = Path(file).read_text(encoding='utf-8') + content = Path(file).read_text(encoding="utf-8") assert content.count(cmds[0]) == 2 assert "coding: utf-8" in content @@ -1173,7 +1215,7 @@ def test_save_with_no_args(): with TemporaryDirectory() as tmpdir: path = os.path.join(tmpdir, "testsave.py") ip.run_line_magic("save", path) - content = Path(path).read_text(encoding='utf-8') + content = Path(path).read_text(encoding="utf-8") expected_content = dedent( """\ # coding: utf-8 @@ -1335,8 +1377,8 @@ if __name__ == "__main__": def test_run_module_from_import_hook(): "Test that a module can be loaded via an import hook" with TemporaryDirectory() as tmpdir: - fullpath = os.path.join(tmpdir, 'my_tmp.py') - Path(fullpath).write_text(TEST_MODULE, encoding='utf-8') + fullpath = os.path.join(tmpdir, "my_tmp.py") + Path(fullpath).write_text(TEST_MODULE, encoding="utf-8") import importlib.abc import importlib.util @@ -1352,7 +1394,7 @@ def test_run_module_from_import_hook(): def get_data(self, path): assert Path(path).samefile(fullpath) - return Path(fullpath).read_text(encoding='utf-8') + return Path(fullpath).read_text(encoding="utf-8") sys.meta_path.insert(0, MyTempImporter()) diff --git a/IPython/core/tests/test_profile.py b/IPython/core/tests/test_profile.py index 22966e4..d034b50 100644 --- a/IPython/core/tests/test_profile.py +++ b/IPython/core/tests/test_profile.py @@ -84,10 +84,10 @@ class ProfileStartupTest(TestCase): def init(self, startup_file, startup, test): # write startup python file - with open(Path(self.pd.startup_dir) / startup_file, "w", encoding='utf-8') as f: + with open(Path(self.pd.startup_dir) / startup_file, "w", encoding="utf-8") as f: f.write(startup) # write simple test file, to check that the startup file was run - with open(self.fname, 'w', encoding='utf-8') as f: + with open(self.fname, "w", encoding="utf-8") as f: f.write(test) def validate(self, output): @@ -111,7 +111,7 @@ def test_list_profiles_in(): if dec.unicode_paths: Path(td / u"profile_ünicode").mkdir(parents=True) - with open(td / "profile_file", "w", encoding='utf-8') as f: + with open(td / "profile_file", "w", encoding="utf-8") as f: f.write("I am not a profile directory") profiles = list_profiles_in(td) diff --git a/IPython/core/tests/test_run.py b/IPython/core/tests/test_run.py index 6957657..9204e81 100644 --- a/IPython/core/tests/test_run.py +++ b/IPython/core/tests/test_run.py @@ -443,7 +443,7 @@ class TestMagicRunWithPackage(unittest.TestCase): d = os.path.dirname(path) if not os.path.isdir(d): os.makedirs(d) - with open(path, 'w', encoding='utf-8') as f: + with open(path, "w", encoding="utf-8") as f: f.write(textwrap.dedent(content)) def setUp(self): @@ -526,8 +526,8 @@ class TestMagicRunWithPackage(unittest.TestCase): def test_run__name__(): with TemporaryDirectory() as td: - path = pjoin(td, 'foo.py') - with open(path, 'w', encoding='utf-8') as f: + path = pjoin(td, "foo.py") + with open(path, "w", encoding="utf-8") as f: f.write("q = __name__") _ip.user_ns.pop("q", None) @@ -547,15 +547,19 @@ def test_run__name__(): def test_run_tb(): """Test traceback offset in %run""" with TemporaryDirectory() as td: - path = pjoin(td, 'foo.py') - with open(path, 'w', encoding='utf-8') as f: - f.write('\n'.join([ - "def foo():", - " return bar()", - "def bar():", - " raise RuntimeError('hello!')", - "foo()", - ])) + path = pjoin(td, "foo.py") + with open(path, "w", encoding="utf-8") as f: + f.write( + "\n".join( + [ + "def foo():", + " return bar()", + "def bar():", + " raise RuntimeError('hello!')", + "foo()", + ] + ) + ) with capture_output() as io: _ip.magic('run {}'.format(path)) out = io.stdout @@ -577,8 +581,8 @@ def test_multiprocessing_run(): mpm = sys.modules.get('__mp_main__') sys.modules['__mp_main__'] = None try: - path = pjoin(td, 'test.py') - with open(path, 'w', encoding='utf-8') as f: + path = pjoin(td, "test.py") + with open(path, "w", encoding="utf-8") as f: f.write("import multiprocessing\nprint('hoy')") with capture_output() as io: _ip.run_line_magic('run', path) @@ -597,15 +601,19 @@ def test_multiprocessing_run(): def test_script_tb(): """Test traceback offset in `ipython script.py`""" with TemporaryDirectory() as td: - path = pjoin(td, 'foo.py') - with open(path, 'w', encoding='utf-8') as f: - f.write('\n'.join([ - "def foo():", - " return bar()", - "def bar():", - " raise RuntimeError('hello!')", - "foo()", - ])) + path = pjoin(td, "foo.py") + with open(path, "w", encoding="utf-8") as f: + f.write( + "\n".join( + [ + "def foo():", + " return bar()", + "def bar():", + " raise RuntimeError('hello!')", + "foo()", + ] + ) + ) out, err = tt.ipexec(path) assert "execfile" not in out assert "RuntimeError" in out diff --git a/IPython/core/tests/test_ultratb.py b/IPython/core/tests/test_ultratb.py index 6e855d1..e10abb8 100644 --- a/IPython/core/tests/test_ultratb.py +++ b/IPython/core/tests/test_ultratb.py @@ -58,7 +58,7 @@ class ChangedPyFileTest(unittest.TestCase): """ with TemporaryDirectory() as td: fname = os.path.join(td, "foo.py") - with open(fname, "w", encoding='utf-8') as f: + with open(fname, "w", encoding="utf-8") as f: f.write(file_1) with prepended_to_syspath(td): @@ -68,7 +68,7 @@ class ChangedPyFileTest(unittest.TestCase): ip.run_cell("foo.f()") # Make the file shorter, so the line of the error is missing. - with open(fname, "w", encoding='utf-8') as f: + with open(fname, "w", encoding="utf-8") as f: f.write(file_2) # For some reason, this was failing on the *second* call after @@ -92,7 +92,7 @@ class NonAsciiTest(unittest.TestCase): # Non-ascii directory name as well. with TemporaryDirectory(suffix=u'é') as td: fname = os.path.join(td, u"fooé.py") - with open(fname, "w", encoding='utf-8') as f: + with open(fname, "w", encoding="utf-8") as f: f.write(file_1) with prepended_to_syspath(td): @@ -172,7 +172,7 @@ class IndentationErrorTest(unittest.TestCase): with TemporaryDirectory() as td: fname = os.path.join(td, "foo.py") - with open(fname, "w", encoding='utf-8') as f: + with open(fname, "w", encoding="utf-8") as f: f.write(indentationerror_file) with tt.AssertPrints("IndentationError"): @@ -221,14 +221,14 @@ bar() def test_changing_py_file(self): with TemporaryDirectory() as td: fname = os.path.join(td, "foo.py") - with open(fname, 'w', encoding='utf-8') as f: + with open(fname, "w", encoding="utf-8") as f: f.write(se_file_1) with tt.AssertPrints(["7/", "SyntaxError"]): ip.magic("run " + fname) # Modify the file - with open(fname, 'w', encoding='utf-8') as f: + with open(fname, "w", encoding="utf-8") as f: f.write(se_file_2) # The SyntaxError should point to the correct line diff --git a/IPython/extensions/storemagic.py b/IPython/extensions/storemagic.py index ff124ee..d9d00f1 100644 --- a/IPython/extensions/storemagic.py +++ b/IPython/extensions/storemagic.py @@ -176,12 +176,12 @@ class StoreMagics(Magics): # default action - store the variable else: # %store foo >file.txt or >>file.txt - if len(args) > 1 and args[1].startswith('>'): - fnam = os.path.expanduser(args[1].lstrip('>').lstrip()) - if args[1].startswith('>>'): - fil = open(fnam, 'a', encoding='utf-8') + if len(args) > 1 and args[1].startswith(">"): + fnam = os.path.expanduser(args[1].lstrip(">").lstrip()) + if args[1].startswith(">>"): + fil = open(fnam, "a", encoding="utf-8") else: - fil = open(fnam, 'w', encoding='utf-8') + fil = open(fnam, "w", encoding="utf-8") with fil: obj = ip.ev(args[0]) print("Writing '%s' (%s) to file '%s'." % (args[0], diff --git a/IPython/extensions/tests/test_autoreload.py b/IPython/extensions/tests/test_autoreload.py index 63bae80..a0fe725 100644 --- a/IPython/extensions/tests/test_autoreload.py +++ b/IPython/extensions/tests/test_autoreload.py @@ -119,13 +119,13 @@ class Fixture(TestCase): time.sleep(1.05) # Write - with open(filename, "w", encoding='utf-8') as f: + with open(filename, "w", encoding="utf-8") as f: f.write(content) def new_module(self, code): code = textwrap.dedent(code) mod_name, mod_fn = self.get_module() - with open(mod_fn, "w", encoding='utf-8') as f: + with open(mod_fn, "w", encoding="utf-8") as f: f.write(code) return mod_name, mod_fn diff --git a/IPython/lib/demo.py b/IPython/lib/demo.py index 0c96de6..8c9ae90 100644 --- a/IPython/lib/demo.py +++ b/IPython/lib/demo.py @@ -405,7 +405,7 @@ class Demo(object): filename = self.shell.mktempfile(self.src_blocks[index]) self.shell.hooks.editor(filename, 1) - with open(Path(filename), "r", encoding='utf-8') as f: + with open(Path(filename), "r", encoding="utf-8") as f: new_block = f.read() # update the source and colored block self.src_blocks[index] = new_block diff --git a/IPython/lib/tests/test_deepreload.py b/IPython/lib/tests/test_deepreload.py index f86e102..827249c 100644 --- a/IPython/lib/tests/test_deepreload.py +++ b/IPython/lib/tests/test_deepreload.py @@ -19,9 +19,9 @@ def test_deepreload(): with TemporaryDirectory() as tmpdir: with prepended_to_syspath(tmpdir): tmpdirpath = Path(tmpdir) - with open(tmpdirpath / "A.py", "w", encoding='utf-8') as f: + with open(tmpdirpath / "A.py", "w", encoding="utf-8") as f: f.write("class Object:\n pass\nok = True\n") - with open(tmpdirpath / "B.py", "w", encoding='utf-8') as f: + with open(tmpdirpath / "B.py", "w", encoding="utf-8") as f: f.write("import A\nassert A.ok, 'we are fine'\n") import A import B diff --git a/IPython/testing/tests/test_tools.py b/IPython/testing/tests/test_tools.py index 0e66ca3..178863c 100644 --- a/IPython/testing/tests/test_tools.py +++ b/IPython/testing/tests/test_tools.py @@ -62,7 +62,7 @@ def test_temp_pyfile(): src = 'pass\n' fname = tt.temp_pyfile(src) assert os.path.isfile(fname) - with open(fname, encoding='utf-8') as fh2: + with open(fname, encoding="utf-8") as fh2: src2 = fh2.read() assert src2 == src diff --git a/IPython/testing/tools.py b/IPython/testing/tools.py index fafb73f..2ff63a6 100644 --- a/IPython/testing/tools.py +++ b/IPython/testing/tools.py @@ -426,9 +426,8 @@ def mute_warn(): @contextmanager def make_tempfile(name): - """ Create an empty, named, temporary file for the duration of the context. - """ - open(name, 'w', encoding='utf-8').close() + """Create an empty, named, temporary file for the duration of the context.""" + open(name, "w", encoding="utf-8").close() try: yield finally: diff --git a/IPython/tests/cve.py b/IPython/tests/cve.py index aba6e20..0a9dec4 100644 --- a/IPython/tests/cve.py +++ b/IPython/tests/cve.py @@ -33,7 +33,9 @@ def test_cve_2022_21699(): with TemporaryWorkingDirectory() as t: dangerous_startup_dir.mkdir(parents=True) - (dangerous_startup_dir / "foo.py").write_text(f'print("{dangerous_expected}")', encoding='utf-8') + (dangerous_startup_dir / "foo.py").write_text( + f'print("{dangerous_expected}")', encoding="utf-8" + ) # 1 sec to make sure FS is flushed. # time.sleep(1) cmd = [sys.executable, "-m", "IPython"] diff --git a/IPython/utils/io.py b/IPython/utils/io.py index ef1be80..170bc62 100644 --- a/IPython/utils/io.py +++ b/IPython/utils/io.py @@ -20,7 +20,7 @@ from IPython.utils.decorators import undoc from .capture import CapturedIO, capture_output # setup stdin/stdout/stderr to sys.stdin/sys.stdout/sys.stderr -devnull = open(os.devnull, 'w', encoding='utf-8') +devnull = open(os.devnull, "w", encoding="utf-8") atexit.register(devnull.close) @@ -52,7 +52,7 @@ class Tee(object): if hasattr(file_or_name, 'write') and hasattr(file_or_name, 'seek'): self.file = file_or_name else: - encoding = None if 'b' in mode else 'utf-8' + encoding = None if "b" in mode else "utf-8" self.file = open(file_or_name, mode, encoding=encoding) self.channel = channel self.ostream = getattr(sys, channel) @@ -132,7 +132,7 @@ def temp_pyfile(src, ext='.py'): It is the caller's responsibility to close the open file and unlink it. """ fname = tempfile.mkstemp(ext)[1] - with open(Path(fname), "w", encoding='utf-8') as f: + with open(Path(fname), "w", encoding="utf-8") as f: f.write(src) f.flush() return fname diff --git a/IPython/utils/tempdir.py b/IPython/utils/tempdir.py index 0729a84..c3918d0 100644 --- a/IPython/utils/tempdir.py +++ b/IPython/utils/tempdir.py @@ -24,7 +24,7 @@ class NamedFileInTemporaryDirectory(object): """ self._tmpdir = TemporaryDirectory(**kwds) path = Path(self._tmpdir.name) / filename - encoding = None if 'b' in mode else 'utf-8' + encoding = None if "b" in mode else "utf-8" self.file = open(path, mode, bufsize, encoding=encoding) def cleanup(self): diff --git a/IPython/utils/tests/test_module_paths.py b/IPython/utils/tests/test_module_paths.py index 2abf889..8438a1e 100644 --- a/IPython/utils/tests/test_module_paths.py +++ b/IPython/utils/tests/test_module_paths.py @@ -32,7 +32,7 @@ TMP_TEST_DIR = Path(tempfile.mkdtemp(suffix="with.dot")) old_syspath = sys.path def make_empty_file(fname): - open(fname, 'w', encoding='utf-8').close() + open(fname, "w", encoding="utf-8").close() def setup_module(): diff --git a/IPython/utils/tests/test_path.py b/IPython/utils/tests/test_path.py index f92e6ae..13e3223 100644 --- a/IPython/utils/tests/test_path.py +++ b/IPython/utils/tests/test_path.py @@ -295,7 +295,7 @@ class TestRaiseDeprecation(unittest.TestCase): ipdir = os.path.join(tmpdir, '.ipython') os.mkdir(ipdir, 0o555) try: - open(os.path.join(ipdir, "_foo_"), 'w', encoding='utf-8').close() + open(os.path.join(ipdir, "_foo_"), "w", encoding="utf-8").close() except IOError: pass else: @@ -352,7 +352,7 @@ class TestShellGlob(unittest.TestCase): with cls.in_tempdir(): # Create empty files for fname in cls.filenames: - open(os.path.join(td, fname), 'w', encoding='utf-8').close() + open(os.path.join(td, fname), "w", encoding="utf-8").close() @classmethod def tearDownClass(cls): @@ -426,9 +426,9 @@ def test_ensure_dir_exists(): d = os.path.join(td, '∂ir') path.ensure_dir_exists(d) # create it assert os.path.isdir(d) - path.ensure_dir_exists(d) # no-op - f = os.path.join(td, 'ƒile') - open(f, 'w', encoding='utf-8').close() # touch + path.ensure_dir_exists(d) # no-op + f = os.path.join(td, "ƒile") + open(f, "w", encoding="utf-8").close() # touch with pytest.raises(IOError): path.ensure_dir_exists(f) @@ -436,7 +436,7 @@ class TestLinkOrCopy(unittest.TestCase): def setUp(self): self.tempdir = TemporaryDirectory() self.src = self.dst("src") - with open(self.src, "w", encoding='utf-8') as f: + with open(self.src, "w", encoding="utf-8") as f: f.write("Hello, world!") def tearDown(self): @@ -456,8 +456,8 @@ class TestLinkOrCopy(unittest.TestCase): ), "%r and %r do not reference the same indoes" % (a, b) def assert_content_equal(self, a, b): - with open(a, 'rb') as a_f: - with open(b, 'rb') as b_f: + with open(a, "rb") as a_f: + with open(b, "rb") as b_f: assert a_f.read() == b_f.read() @skip_win32 @@ -477,7 +477,7 @@ class TestLinkOrCopy(unittest.TestCase): @skip_win32 def test_target_exists(self): dst = self.dst("target") - open(dst, "w", encoding='utf-8').close() + open(dst, "w", encoding="utf-8").close() path.link_or_copy(self.src, dst) self.assert_inode_equal(self.src, dst) diff --git a/docs/autogen_config.py b/docs/autogen_config.py index 56733f3..43c38dd 100755 --- a/docs/autogen_config.py +++ b/docs/autogen_config.py @@ -102,7 +102,7 @@ def reverse_aliases(app): def write_doc(name, title, app, preamble=None): trait_aliases = reverse_aliases(app) filename = options / (name + ".rst") - with open(filename, "w", encoding='utf-8') as f: + with open(filename, "w", encoding="utf-8") as f: f.write(title + "\n") f.write(("=" * len(title)) + "\n") f.write("\n") @@ -117,7 +117,7 @@ def write_doc(name, title, app, preamble=None): if __name__ == '__main__': # Touch this file for the make target - Path(generated).write_text("", encoding='utf-8') + Path(generated).write_text("", encoding="utf-8") write_doc('terminal', 'Terminal IPython options', TerminalIPythonApp()) write_doc('kernel', 'IPython kernel options', IPKernelApp(), diff --git a/docs/autogen_magics.py b/docs/autogen_magics.py index bfeb6aa..6102d09 100644 --- a/docs/autogen_magics.py +++ b/docs/autogen_magics.py @@ -63,4 +63,4 @@ for name, func in sorted(magics["cell"].items(), key=sortkey): src_path = Path(__file__).parent dest = src_path.joinpath("source", "interactive", "magics-generated.txt") -dest.write_text("\n".join(output), encoding='utf-8') +dest.write_text("\n".join(output), encoding="utf-8") diff --git a/docs/autogen_shortcuts.py b/docs/autogen_shortcuts.py index 7ee0f91..db7fe8d 100755 --- a/docs/autogen_shortcuts.py +++ b/docs/autogen_shortcuts.py @@ -89,6 +89,8 @@ if __name__ == '__main__': (single_filter, "single_filtered"), (multi_filter, "multi_filtered"), ]: - with (dest / "{}.csv".format(output_filename)).open("w", encoding='utf-8') as csv: + with (dest / "{}.csv".format(output_filename)).open( + "w", encoding="utf-8" + ) as csv: for (shortcut, flt), v in sorted(filters.items(), key=sort_key): csv.write(":kbd:`{}`\t{}\t{}\n".format(shortcut, flt, v)) diff --git a/docs/source/conf.py b/docs/source/conf.py index ba9aa1d..29212af 100755 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -28,11 +28,14 @@ if ON_RTD: for name in ("config", "api", "magics", "shortcuts"): fname = Path("autogen_{}.py".format(name)) fpath = (Path(__file__).parent).joinpath("..", fname) - with open(fpath, encoding='utf-8') as f: - exec(compile(f.read(), fname, 'exec'), { - '__file__': fpath, - '__name__': '__main__', - }) + with open(fpath, encoding="utf-8") as f: + exec( + compile(f.read(), fname, "exec"), + { + "__file__": fpath, + "__name__": "__main__", + }, + ) else: import sphinx_rtd_theme html_theme = "sphinx_rtd_theme" @@ -45,7 +48,14 @@ sys.path.insert(0, os.path.abspath('../sphinxext')) # We load the ipython release info into a dict by explicit execution iprelease = {} -exec(compile(open('../../IPython/core/release.py', encoding='utf-8').read(), '../../IPython/core/release.py', 'exec'),iprelease) +exec( + compile( + open("../../IPython/core/release.py", encoding="utf-8").read(), + "../../IPython/core/release.py", + "exec", + ), + iprelease, +) # General configuration # --------------------- diff --git a/docs/sphinxext/apigen.py b/docs/sphinxext/apigen.py index 71a384b..e58493b 100644 --- a/docs/sphinxext/apigen.py +++ b/docs/sphinxext/apigen.py @@ -390,9 +390,8 @@ class ApiDocWriter(object): if not api_str: continue # write out to file - outfile = os.path.join(outdir, - m + self.rst_extension) - with open(outfile, 'wt', encoding='utf-8') as fileobj: + outfile = os.path.join(outdir, m + self.rst_extension) + with open(outfile, "wt", encoding="utf-8") as fileobj: fileobj.write(api_str) written_modules.append(m) self.written_modules = written_modules @@ -444,7 +443,7 @@ class ApiDocWriter(object): relpath = outdir.replace(relative_to + os.path.sep, '') else: relpath = outdir - with open(path,'wt', encoding='utf-8') as idx: + with open(path, "wt", encoding="utf-8") as idx: w = idx.write w('.. AUTO-GENERATED FILE -- DO NOT EDIT!\n\n') w('.. autosummary::\n' diff --git a/examples/IPython Kernel/ipython-get-history.py b/examples/IPython Kernel/ipython-get-history.py index fe38081..116b4b7 100755 --- a/examples/IPython Kernel/ipython-get-history.py +++ b/examples/IPython Kernel/ipython-get-history.py @@ -24,7 +24,7 @@ from IPython.core.history import HistoryAccessor session_number = int(sys.argv[1]) if len(sys.argv) > 2: filepath = Path(sys.argv[2]) - dest = open(filepath, "w", encoding='utf-8') + dest = open(filepath, "w", encoding="utf-8") raw = not filepath.name.endswith(".py") else: dest = sys.stdout diff --git a/setupbase.py b/setupbase.py index 788ce24..b57dcc1 100644 --- a/setupbase.py +++ b/setupbase.py @@ -36,7 +36,7 @@ repo_root = os.path.dirname(os.path.abspath(__file__)) def execfile(fname, globs, locs=None): locs = locs or globs - with open(fname, encoding='utf-8') as f: + with open(fname, encoding="utf-8") as f: exec(compile(f.read(), fname, "exec"), globs, locs) # A little utility we'll need below, since glob() does NOT allow you to do @@ -336,10 +336,13 @@ def git_prebuild(pkg_dir, build_cmd=build_py): os.remove(out_pth) except (IOError, OSError): pass - with open(out_pth, 'w', encoding='utf-8') as out_file: - out_file.writelines([ - '# GENERATED BY setup.py\n', - 'commit = u"%s"\n' % repo_commit, - ]) + with open(out_pth, "w", encoding="utf-8") as out_file: + out_file.writelines( + [ + "# GENERATED BY setup.py\n", + 'commit = u"%s"\n' % repo_commit, + ] + ) + return MyBuildPy diff --git a/tools/fixup_whats_new_pr.py b/tools/fixup_whats_new_pr.py index 397c0bf..9b74da8 100644 --- a/tools/fixup_whats_new_pr.py +++ b/tools/fixup_whats_new_pr.py @@ -22,14 +22,14 @@ def main(): print("Adding pseudo-title to:", filepath.name) title = filepath.name[:-4].split("/")[-1].replace("-", " ").capitalize() - data = filepath.read_text(encoding='utf-8') + data = filepath.read_text(encoding="utf-8") try: if data and data.splitlines()[1].startswith('='): continue except IndexError: pass - with filepath.open("w", encoding='utf-8') as f: + with filepath.open("w", encoding="utf-8") as f: f.write(title + "\n") f.write("=" * len(title) + "\n\n") f.write(data) diff --git a/tools/toollib.py b/tools/toollib.py index c77bbbc..f32e06a 100644 --- a/tools/toollib.py +++ b/tools/toollib.py @@ -45,4 +45,4 @@ def get_ipdir(): def execfile(fname, globs, locs=None): locs = locs or globs - exec(compile(open(fname, encoding='utf-8').read(), fname, "exec"), globs, locs) + exec(compile(open(fname, encoding="utf-8").read(), fname, "exec"), globs, locs)