Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EncodingWarning on Python 3.10 #13510

Merged
merged 3 commits into from Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/downstream.yml
Expand Up @@ -41,7 +41,7 @@ jobs:
python -m pip install --upgrade -e file://$PWD#egg=ipython[test]
# we must instal IPython after ipykernel to get the right versions.
python -m pip install --upgrade --upgrade-strategy eager flaky ipyparallel
python -m pip install --upgrade pytest
python -m pip install --upgrade 'pytest<7'
- name: pytest
env:
COLUMNS: 120
Expand Down
2 changes: 1 addition & 1 deletion IPython/core/application.py
Expand Up @@ -470,7 +470,7 @@ def stage_default_config_file(self):
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)
config_file.write_text(s, encoding="utf-8")

@catch_config_error
def initialize(self, argv=None):
Expand Down
2 changes: 1 addition & 1 deletion IPython/core/crashhandler.py
Expand Up @@ -185,7 +185,7 @@ def __call__(self, etype, evalue, etb):

# and generate a complete report on disk
try:
report = open(report_name,'w')
report = open(report_name, "w", encoding="utf-8")
except:
print('Could not create crash report on disk.', file=sys.stderr)
return
Expand Down
9 changes: 7 additions & 2 deletions IPython/core/display.py
Expand Up @@ -349,7 +349,8 @@ def _data_and_metadata(self):
def reload(self):
"""Reload the raw data from file or URL."""
if self.filename is not None:
with open(self.filename, self._read_flags) as f:
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:
# Deferred import
Expand All @@ -369,7 +370,11 @@ def reload(self):
if 'gzip' in response.headers['content-encoding']:
import gzip
from io import BytesIO
with gzip.open(BytesIO(data), 'rt', encoding=encoding) as fp:

# assume utf-8 if encoding is not specified
with gzip.open(
BytesIO(data), "rt", encoding=encoding or "utf-8"
) as fp:
encoding = None
data = fp.read()

Expand Down
8 changes: 4 additions & 4 deletions IPython/core/interactiveshell.py
Expand Up @@ -2626,7 +2626,7 @@ def safe_execfile(self, fname, *where, exit_ignore=False, raise_exceptions=False

# Make sure we can open the file
try:
with fname.open():
with fname.open("rb"):
pass
except:
warn('Could not open file <%s> for safe execution.' % fname)
Expand Down Expand Up @@ -2684,7 +2684,7 @@ def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False):

# Make sure we can open the file
try:
with fname.open():
with fname.open("rb"):
pass
except:
warn('Could not open file <%s> for safe execution.' % fname)
Expand All @@ -2706,7 +2706,7 @@ def get_cells():
if cell.cell_type == 'code':
yield cell.source
else:
yield fname.read_text()
yield fname.read_text(encoding="utf-8")

with prepended_to_syspath(dname):
try:
Expand Down Expand Up @@ -3458,7 +3458,7 @@ def mktempfile(self, data=None, prefix='ipython_edit_'):
self.tempfiles.append(file_path)

if data:
file_path.write_text(data)
file_path.write_text(data, encoding="utf-8")
return filename

def ask_yes_no(self, prompt, default=None, interrupt=None):
Expand Down
12 changes: 6 additions & 6 deletions IPython/core/magics/code.py
Expand Up @@ -538,7 +538,7 @@ def _edit_macro(self,mname,macro):
self.shell.hooks.editor(filename)

# and make a new macro object, to replace the old one
mvalue = Path(filename).read_text()
mvalue = Path(filename).read_text(encoding="utf-8")
self.shell.user_ns[mname] = Macro(mvalue)

@skip_doctest
Expand Down Expand Up @@ -728,25 +728,25 @@ def edit(self, parameter_s='',last_call=['','']):
# 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()
self.shell.user_ns["pasted_block"] = filepath.read_text(encoding="utf-8")

if 'x' in opts: # -x prevents actual execution
print()
else:
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()
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,
self.shell.user_ns)

if is_temp:
try:
return filepath.read_text()
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?')
Expand Down
2 changes: 1 addition & 1 deletion IPython/core/magics/execution.py
Expand Up @@ -360,7 +360,7 @@ def _run_with_profiler(self, code, opts, namespace):
if text_file:
pfile = Path(text_file)
pfile.touch(exist_ok=True)
pfile.write_text(output)
pfile.write_text(output, encoding="utf-8")

print(
f"\n*** Profile printout saved to text file {repr(text_file)}.{sys_exit}"
Expand Down
2 changes: 1 addition & 1 deletion IPython/core/magics/packaging.py
Expand Up @@ -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()
history = Path(sys.prefix, "conda-meta", "history").read_text(encoding="utf-8")
match = re.search(
r"^#\s*cmd:\s*(?P<command>.*conda)\s[create|install]",
history,
Expand Down
19 changes: 11 additions & 8 deletions IPython/core/page.py
Expand Up @@ -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") 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
Expand All @@ -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), 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)
Expand Down Expand Up @@ -277,7 +280,7 @@ def page_file(fname, start=0, pager_cmd=None):
try:
if start > 0:
start -= 1
page(open(fname).read(),start)
page(open(fname, encoding="utf-8").read(), start)
except:
print('Unable to show file',repr(fname))

Expand Down
4 changes: 2 additions & 2 deletions IPython/core/tests/test_application.py
Expand Up @@ -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") 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)
Expand All @@ -59,7 +59,7 @@ class TestApp(BaseIPythonApplication):
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") 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()
Expand Down
6 changes: 3 additions & 3 deletions IPython/core/tests/test_completer.py
Expand Up @@ -346,7 +346,7 @@ def test_abspath_file_completions(self):
suffixes = ["1", "2"]
names = [prefix + s for s in suffixes]
for n in names:
open(n, "w").close()
open(n, "w", encoding="utf-8").close()

# Check simple completion
c = ip.complete(prefix)[1]
Expand All @@ -365,7 +365,7 @@ def test_local_file_completions(self):
suffixes = ["1", "2"]
names = [prefix + s for s in suffixes]
for n in names:
open(n, "w").close()
open(n, "w", encoding="utf-8").close()

# Check simple completion
c = ip.complete(prefix)[1]
Expand All @@ -381,7 +381,7 @@ def test_quoted_file_completions(self):
ip = get_ipython()
with TemporaryWorkingDirectory():
name = "foo'bar"
open(name, "w").close()
open(name, "w", encoding="utf-8").close()

# Don't escape Windows
escaped = name if sys.platform == "win32" else "foo\\'bar"
Expand Down
8 changes: 4 additions & 4 deletions IPython/core/tests/test_completerlib.py
Expand Up @@ -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") 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))
Expand Down Expand Up @@ -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") 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)
Expand Down Expand Up @@ -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').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)
Expand Down
14 changes: 7 additions & 7 deletions IPython/core/tests/test_extension.py
Expand Up @@ -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') 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') 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):
Expand Down Expand Up @@ -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') 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
Expand Down
14 changes: 8 additions & 6 deletions IPython/core/tests/test_interactiveshell.py
Expand Up @@ -485,12 +485,12 @@ def mock_print_func(value, sep=" ", end="\n", file=sys.stdout, flush=False):
def test_mktempfile(self):
filename = ip.mktempfile()
# Check that we can open the file again on Windows
with open(filename, 'w') 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') 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
Expand Down Expand Up @@ -545,7 +545,9 @@ def setUp(self):
self.BASETESTDIR = tempfile.mkdtemp()
self.TESTDIR = join(self.BASETESTDIR, u"åäö")
os.mkdir(self.TESTDIR)
with open(join(self.TESTDIR, u"åäötestscript.py"), "w") 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)
Expand Down