Skip to content

Commit 4d65400

Browse files
committed
Matplotlib now works when the user has no home directory.
matplotlib.get_home now returns None if there is no home directory, instead of raising RuntimeError. Updated code in several places to handle this gracefully. matplotlib.get_configdir now returns a temporary directory if there is no home directory, instead of raising RuntimeError.
1 parent a697a26 commit 4d65400

File tree

1 file changed

+45
-39
lines changed

1 file changed

+45
-39
lines changed

lib/matplotlib/__init__.py

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -470,26 +470,25 @@ def checkdep_usetex(s):
470470

471471
def _get_home():
472472
"""Find user's home directory if possible.
473-
Otherwise raise error.
473+
Otherwise, returns None.
474474
475-
:see: http://mail.python.org/pipermail/python-list/2005-February/263921.html
475+
:see: http://mail.python.org/pipermail/python-list/2005-February/325395.html
476476
"""
477-
path=''
478477
try:
479-
path=os.path.expanduser("~")
478+
path = os.path.expanduser("~")
480479
except:
481480
pass
482-
if not os.path.isdir(path):
483-
for evar in ('HOME', 'USERPROFILE', 'TMP'):
484-
try:
485-
path = os.environ[evar]
486-
if os.path.isdir(path):
487-
break
488-
except: pass
489-
if path:
490-
return path
491481
else:
492-
raise RuntimeError('please define environment variable $HOME')
482+
if os.path.isdir(path):
483+
return path
484+
for evar in ('HOME', 'USERPROFILE', 'TMP'):
485+
try:
486+
path = os.environ[evar]
487+
if os.path.isdir(path):
488+
return path
489+
except:
490+
pass
491+
return None
493492

494493

495494
def _create_tmp_config_dir():
@@ -513,12 +512,14 @@ def _get_configdir():
513512
"""
514513
Return the string representing the configuration directory.
515514
516-
Default is HOME/.matplotlib. You can override this with the
517-
MPLCONFIGDIR environment variable. If the default is not
518-
writable, and MPLCONFIGDIR is not set, then
519-
tempfile.gettempdir() is used to provide a directory in
520-
which a matplotlib subdirectory is created as the configuration
521-
directory.
515+
The directory is chosen as follows:
516+
517+
1. If the MPLCONFIGDIR environment variable is supplied, choose that. Else,
518+
choose the '.matplotlib' subdirectory of the user's home directory (and
519+
create it if necessary).
520+
2. If the chosen directory exists and is writable, use that as the
521+
configuration directory.
522+
3. Create a temporary directory, and use it as the configuration directory.
522523
"""
523524

524525
configdir = os.environ.get('MPLCONFIGDIR')
@@ -530,18 +531,21 @@ def _get_configdir():
530531
return configdir
531532

532533
h = get_home()
533-
p = os.path.join(get_home(), '.matplotlib')
534+
if h is not None:
535+
p = os.path.join(h, '.matplotlib')
534536

535-
if os.path.exists(p):
536-
if not _is_writable_dir(p):
537-
return _create_tmp_config_dir()
538-
else:
539-
if not _is_writable_dir(h):
540-
return _create_tmp_config_dir()
541-
from matplotlib.cbook import mkdirs
542-
mkdirs(p)
537+
if os.path.exists(p):
538+
if not _is_writable_dir(p):
539+
return _create_tmp_config_dir()
540+
else:
541+
if not _is_writable_dir(h):
542+
return _create_tmp_config_dir()
543+
from matplotlib.cbook import mkdirs
544+
mkdirs(p)
545+
546+
return p
543547

544-
return p
548+
return _create_tmp_config_dir()
545549
get_configdir = verbose.wrap('CONFIGDIR=%s', _get_configdir, always=False)
546550

547551

@@ -643,18 +647,20 @@ def matplotlib_fname():
643647
print("WARNING: File could not be renamed: %s" % e, file=sys.stderr)
644648

645649
home = get_home()
646-
oldname = os.path.join( home, '.matplotlibrc')
647-
if os.path.exists(oldname):
648-
configdir = get_configdir()
649-
newname = os.path.join(configdir, 'matplotlibrc')
650-
print("""\
650+
if home:
651+
oldname = os.path.join( home, '.matplotlibrc')
652+
if os.path.exists(oldname):
653+
configdir = get_configdir()
654+
newname = os.path.join(configdir, 'matplotlibrc')
655+
print("""\
651656
WARNING: Old rc filename "%s" found and renamed to
652657
new default rc file name "%s"."""%(oldname, newname), file=sys.stderr)
653658

654-
try:
655-
shutil.move(oldname, newname)
656-
except IOError as e:
657-
print("WARNING: File could not be renamed: %s" % e, file=sys.stderr)
659+
try:
660+
shutil.move(oldname, newname)
661+
except IOError as e:
662+
print("WARNING: File could not be renamed: %s" % e,
663+
file=sys.stderr)
658664

659665

660666
fname = os.path.join( os.getcwd(), 'matplotlibrc')

0 commit comments

Comments
 (0)