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

Use pathlib instead of os in IPython/{lib/demo,core/hooks} #13785

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions IPython/core/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def load_ipython_extension(ip):
# the file COPYING, distributed as part of this software.
#*****************************************************************************

import os
import subprocess
import sys
from pathlib import Path

from .error import TryNext

Expand Down Expand Up @@ -75,7 +75,7 @@ def editor(self, filename, linenum=None, wait=True):
linemark = '+%d' % int(linenum)

# Enclose in quotes if necessary and legal
if ' ' in editor and os.path.isfile(editor) and editor[0] != '"':
if " " in editor and Path(editor).is_file() and editor[0] != '"':
editor = '"%s"' % editor

# Call the actual editor
Expand Down
8 changes: 3 additions & 5 deletions IPython/lib/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,11 @@
#
#*****************************************************************************

import os
import re
import shlex
import sys
import pygments
from pathlib import Path
from pathlib import Path, PurePath

from IPython.utils.text import marquee
from IPython.utils import openpy
Expand Down Expand Up @@ -251,8 +250,7 @@ def __init__(self,src,title='',arg_str='',auto_all=None, format_rst=False,
# Assume it's a string or something that can be converted to one
self.fname = src
if title == '':
(filepath, filename) = os.path.split(src)
self.title = filename
self.title = PurePath(src).name
else:
self.title = title
self.sys_argv = [src] + shlex.split(arg_str)
Expand Down Expand Up @@ -405,7 +403,7 @@ def edit(self,index=None):

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 Path(filename).open("r", encoding="utf-8") as f:
new_block = f.read()
# update the source and colored block
self.src_blocks[index] = new_block
Expand Down
26 changes: 11 additions & 15 deletions IPython/utils/tests/test_openpy.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
import io
import os.path
from pathlib import Path

from IPython.utils import openpy
from IPython.utils.openpy import detect_encoding, read_py_file, source_to_unicode

mydir = os.path.dirname(__file__)
nonascii_path = os.path.join(mydir, "../../core/tests/nonascii.py")
nonascii_path = Path(__file__).parent / "../../core/tests/nonascii.py"


def test_detect_encoding():
with open(nonascii_path, "rb") as f:
enc, lines = openpy.detect_encoding(f.readline)
with nonascii_path.open("rb") as f:
enc, lines = detect_encoding(f.readline)
assert enc == "iso-8859-5"


def test_read_file():
with io.open(nonascii_path, encoding="iso-8859-5") as f:
with nonascii_path.open(encoding="iso-8859-5") as f:
read_specified_enc = f.read()
read_detected_enc = openpy.read_py_file(nonascii_path, skip_encoding_cookie=False)
read_detected_enc = read_py_file(nonascii_path, skip_encoding_cookie=False)
assert read_detected_enc == read_specified_enc
assert "coding: iso-8859-5" in read_detected_enc

read_strip_enc_cookie = openpy.read_py_file(
nonascii_path, skip_encoding_cookie=True
)
read_strip_enc_cookie = read_py_file(nonascii_path, skip_encoding_cookie=True)
assert "coding: iso-8859-5" not in read_strip_enc_cookie


def test_source_to_unicode():
with io.open(nonascii_path, "rb") as f:
with nonascii_path.open("rb") as f:
source_bytes = f.read()
assert (
openpy.source_to_unicode(source_bytes, skip_encoding_cookie=False).splitlines()
source_to_unicode(source_bytes, skip_encoding_cookie=False).splitlines()
== source_bytes.decode("iso-8859-5").splitlines()
)

source_no_cookie = openpy.source_to_unicode(source_bytes, skip_encoding_cookie=True)
source_no_cookie = source_to_unicode(source_bytes, skip_encoding_cookie=True)
assert "coding: iso-8859-5" not in source_no_cookie