Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b2face8

Browse files
committed
Python 3 compatibility for os.getcwdu()
1 parent 6238fc3 commit b2face8

26 files changed

Lines changed: 70 additions & 54 deletions

IPython/core/application.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from IPython.core import release, crashhandler
4141
from IPython.core.profiledir import ProfileDir, ProfileDirError
4242
from IPython.utils.path import get_ipython_dir, get_ipython_package_dir
43+
from IPython.utils import py3compat
4344
from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, Set, Instance
4445

4546
#-----------------------------------------------------------------------------
@@ -103,7 +104,7 @@ def _config_file_name_changed(self, name, old, new):
103104

104105
config_file_paths = List(Unicode)
105106
def _config_file_paths_default(self):
106-
return [os.getcwdu()]
107+
return [py3compat.getcwd()]
107108

108109
extra_config_file = Unicode(config=True,
109110
help="""Path to an extra config file to load.
@@ -179,7 +180,7 @@ def __init__(self, **kwargs):
179180
super(BaseIPythonApplication, self).__init__(**kwargs)
180181
# ensure current working directory exists
181182
try:
182-
directory = os.getcwdu()
183+
directory = py3compat.getcwd()
183184
except:
184185
# raise exception
185186
self.log.error("Current working directory doesn't exist.")

IPython/core/crashhandler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from IPython.core import ultratb
2929
from IPython.core.release import author_email
3030
from IPython.utils.sysinfo import sys_info
31-
from IPython.utils.py3compat import input
31+
from IPython.utils.py3compat import input, getcwd
3232

3333
#-----------------------------------------------------------------------------
3434
# Code
@@ -140,9 +140,9 @@ def __call__(self, etype, evalue, etb):
140140
try:
141141
rptdir = self.app.ipython_dir
142142
except:
143-
rptdir = os.getcwdu()
143+
rptdir = getcwd()
144144
if rptdir is None or not os.path.isdir(rptdir):
145-
rptdir = os.getcwdu()
145+
rptdir = getcwd()
146146
report_name = os.path.join(rptdir,self.crash_report_fname)
147147
# write the report filename into the instance dict so it can get
148148
# properly expanded out in the user message template

IPython/core/history.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from IPython.config.configurable import Configurable
3131
from IPython.external.decorator import decorator
3232
from IPython.utils.path import locate_profile
33+
from IPython.utils import py3compat
3334
from IPython.utils.traitlets import (
3435
Any, Bool, Dict, Instance, Integer, List, Unicode, TraitError,
3536
)
@@ -423,7 +424,7 @@ class HistoryManager(HistoryAccessor):
423424
dir_hist = List()
424425
def _dir_hist_default(self):
425426
try:
426-
return [os.getcwdu()]
427+
return [py3compat.getcwd()]
427428
except OSError:
428429
return []
429430

@@ -519,7 +520,7 @@ def reset(self, new_session=True):
519520
optionally open a new session."""
520521
self.output_hist.clear()
521522
# The directory history can't be completely empty
522-
self.dir_hist[:] = [os.getcwdu()]
523+
self.dir_hist[:] = [py3compat.getcwd()]
523524

524525
if new_session:
525526
if self.session_number:

IPython/core/interactiveshell.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def init_instance_attrs(self):
578578

579579
# keep track of where we started running (mainly for crash post-mortem)
580580
# This is not being used anywhere currently.
581-
self.starting_dir = os.getcwdu()
581+
self.starting_dir = py3compat.getcwd()
582582

583583
# Indentation management
584584
self.indent_current_nsp = 0

IPython/core/magics/osm.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from IPython.utils.openpy import source_to_unicode
3737
from IPython.utils.path import unquote_filename
3838
from IPython.utils.process import abbrev_cwd
39+
from IPython.utils import py3compat
3940
from IPython.utils.py3compat import unicode_type
4041
from IPython.utils.terminal import set_term_title
4142

@@ -180,7 +181,7 @@ def rehashx(self, parameter_s=''):
180181
winext += '|py'
181182
execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
182183
isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
183-
savedir = os.getcwdu()
184+
savedir = py3compat.getcwd()
184185

185186
# Now walk the paths looking for executables to alias.
186187
try:
@@ -234,7 +235,7 @@ def pwd(self, parameter_s=''):
234235
In [9]: pwd
235236
Out[9]: '/home/tsuser/sprint/ipython'
236237
"""
237-
return os.getcwdu()
238+
return py3compat.getcwd()
238239

239240
@skip_doctest
240241
@line_magic
@@ -278,7 +279,7 @@ def cd(self, parameter_s=''):
278279
/home/tsuser/parent/child
279280
"""
280281

281-
oldcwd = os.getcwdu()
282+
oldcwd = py3compat.getcwd()
282283
numcd = re.match(r'(-)(\d+)$',parameter_s)
283284
# jump in directory history by number
284285
if numcd:
@@ -351,7 +352,7 @@ def cd(self, parameter_s=''):
351352
except OSError:
352353
print(sys.exc_info()[1])
353354
else:
354-
cwd = os.getcwdu()
355+
cwd = py3compat.getcwd()
355356
dhist = self.shell.user_ns['_dh']
356357
if oldcwd != cwd:
357358
dhist.append(cwd)
@@ -361,7 +362,7 @@ def cd(self, parameter_s=''):
361362
os.chdir(self.shell.home_dir)
362363
if hasattr(self.shell, 'term_title') and self.shell.term_title:
363364
set_term_title('IPython: ' + '~')
364-
cwd = os.getcwdu()
365+
cwd = py3compat.getcwd()
365366
dhist = self.shell.user_ns['_dh']
366367

367368
if oldcwd != cwd:
@@ -387,7 +388,7 @@ def pushd(self, parameter_s=''):
387388

388389
dir_s = self.shell.dir_stack
389390
tgt = os.path.expanduser(unquote_filename(parameter_s))
390-
cwd = os.getcwdu().replace(self.shell.home_dir,'~')
391+
cwd = py3compat.getcwd().replace(self.shell.home_dir,'~')
391392
if tgt:
392393
self.cd(parameter_s)
393394
dir_s.insert(0,cwd)
@@ -676,7 +677,7 @@ def bookmark(self, parameter_s=''):
676677
if not args:
677678
raise UsageError("%bookmark: You must specify the bookmark name")
678679
elif len(args)==1:
679-
bkms[args[0]] = os.getcwdu()
680+
bkms[args[0]] = py3compat.getcwd()
680681
elif len(args)==2:
681682
bkms[args[0]] = args[1]
682683
self.shell.db['bookmarks'] = bkms

IPython/core/profileapp.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from IPython.core.profiledir import ProfileDir
3232
from IPython.utils.importstring import import_item
3333
from IPython.utils.path import get_ipython_dir, get_ipython_package_dir
34+
from IPython.utils import py3compat
3435
from IPython.utils.traitlets import Unicode, Bool, Dict
3536

3637
#-----------------------------------------------------------------------------
@@ -180,10 +181,10 @@ def list_profile_dirs(self):
180181
print("Available profiles in %s:" % self.ipython_dir)
181182
self._print_profiles(profiles)
182183

183-
profiles = list_profiles_in(os.getcwdu())
184+
profiles = list_profiles_in(py3compat.getcwd())
184185
if profiles:
185186
print()
186-
print("Available profiles in current directory (%s):" % os.getcwdu())
187+
print("Available profiles in current directory (%s):" % py3compat.getcwd())
187188
self._print_profiles(profiles)
188189

189190
print()
@@ -248,7 +249,7 @@ def _import_app(self, app_path):
248249
name = app_path.rsplit('.', 1)[-1]
249250
try:
250251
app = import_item(app_path)
251-
except ImportError as e:
252+
except ImportError:
252253
self.log.info("Couldn't import %s, config file will be excluded", name)
253254
except Exception:
254255
self.log.warn('Unexpected error importing %s', name, exc_info=True)

IPython/core/profiledir.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
from IPython.config.configurable import LoggingConfigurable
2929
from IPython.utils.path import get_ipython_package_dir, expand_path
30+
from IPython.utils import py3compat
3031
from IPython.utils.traitlets import Unicode, Bool
3132

3233
#-----------------------------------------------------------------------------
@@ -233,7 +234,7 @@ def find_profile_dir_by_name(cls, ipython_dir, name=u'default', config=None):
233234
is not found, a :class:`ProfileDirError` exception will be raised.
234235
235236
The search path algorithm is:
236-
1. ``os.getcwdu()``
237+
1. ``py3compat.getcwd()``
237238
2. ``ipython_dir``
238239
239240
Parameters
@@ -245,7 +246,7 @@ def find_profile_dir_by_name(cls, ipython_dir, name=u'default', config=None):
245246
will be "profile_<profile>".
246247
"""
247248
dirname = u'profile_' + name
248-
paths = [os.getcwdu(), ipython_dir]
249+
paths = [py3compat.getcwd(), ipython_dir]
249250
for p in paths:
250251
profile_dir = os.path.join(p, dirname)
251252
if os.path.isdir(profile_dir):

IPython/core/prompts.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def cwd_filt(depth):
212212
$HOME is always replaced with '~'.
213213
If depth==0, the full path is returned."""
214214

215-
cwd = os.getcwdu().replace(HOME,"~")
215+
cwd = py3compat.getcwd().replace(HOME,"~")
216216
out = os.sep.join(cwd.split(os.sep)[-depth:])
217217
return out or os.sep
218218

@@ -222,7 +222,7 @@ def cwd_filt2(depth):
222222
$HOME is always replaced with '~'.
223223
If depth==0, the full path is returned."""
224224

225-
full_cwd = os.getcwdu()
225+
full_cwd = py3compat.getcwd()
226226
cwd = full_cwd.replace(HOME,"~").split(os.sep)
227227
if '~' in cwd and len(cwd) == depth+1:
228228
depth += 1
@@ -238,9 +238,9 @@ def cwd_filt2(depth):
238238
#-----------------------------------------------------------------------------
239239

240240
lazily_evaluate = {'time': LazyEvaluate(time.strftime, "%H:%M:%S"),
241-
'cwd': LazyEvaluate(os.getcwdu),
242-
'cwd_last': LazyEvaluate(lambda: os.getcwdu().split(os.sep)[-1]),
243-
'cwd_x': [LazyEvaluate(lambda: os.getcwdu().replace(HOME,"~"))] +\
241+
'cwd': LazyEvaluate(py3compat.getcwd),
242+
'cwd_last': LazyEvaluate(lambda: py3compat.getcwd().split(os.sep)[-1]),
243+
'cwd_x': [LazyEvaluate(lambda: py3compat.getcwd().replace(HOME,"~"))] +\
244244
[LazyEvaluate(cwd_filt, x) for x in range(1,6)],
245245
'cwd_y': [LazyEvaluate(cwd_filt2, x) for x in range(6)]
246246
}

IPython/core/tests/test_application.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ def test_unicode_cwd():
1313
"""Check that IPython starts with non-ascii characters in the path."""
1414
wd = tempfile.mkdtemp(suffix=u"€")
1515

16-
old_wd = os.getcwdu()
16+
old_wd = py3compat.getcwd()
1717
os.chdir(wd)
18-
#raise Exception(repr(os.getcwdu()))
18+
#raise Exception(repr(py3compat.getcwd()))
1919
try:
2020
app = BaseIPythonApplication()
2121
# The lines below are copied from Application.initialize()

IPython/core/tests/test_completer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from IPython.external.decorators import knownfailureif
1919
from IPython.utils.tempdir import TemporaryDirectory
2020
from IPython.utils.generics import complete_object
21+
from IPython.utils import py3compat
2122
from IPython.utils.py3compat import string_types, unicode_type
2223

2324
#-----------------------------------------------------------------------------
@@ -177,7 +178,7 @@ def test_abspath_file_completions():
177178

178179
def test_local_file_completions():
179180
ip = get_ipython()
180-
cwd = os.getcwdu()
181+
cwd = py3compat.getcwd()
181182
try:
182183
with TemporaryDirectory() as tmpdir:
183184
os.chdir(tmpdir)

0 commit comments

Comments
 (0)