@@ -575,26 +575,15 @@ def _create_tmp_config_dir():
575575
576576 Returns None if a writable temporary directory could not be created.
577577 """
578- import getpass
579- import tempfile
580- from matplotlib .cbook import mkdirs
581-
582578 try :
583579 tempdir = tempfile .gettempdir ()
584580 except NotImplementedError :
585581 # Some restricted platforms (such as Google App Engine) do not provide
586582 # gettempdir.
587583 return None
588- try :
589- username = getpass .getuser ()
590- except KeyError :
591- username = str (os .getuid ())
592-
593- tempdir = tempfile .mkdtemp (prefix = 'matplotlib-%s-' % username , dir = tempdir )
594-
595- os .environ ['MPLCONFIGDIR' ] = tempdir
596-
597- return tempdir
584+ configdir = os .environ ['MPLCONFIGDIR' ] = (
585+ tempfile .mkdtemp (prefix = 'matplotlib-' , dir = tempdir ))
586+ return configdir
598587
599588
600589get_home = verbose .wrap ('$HOME=%s' , _get_home , always = False )
@@ -805,34 +794,24 @@ def matplotlib_fname():
805794 - Lastly, it looks in `$MATPLOTLIBDATA/matplotlibrc` for a
806795 system-defined copy.
807796 """
808- if six .PY2 :
809- cwd = os .getcwdu ()
810- else :
811- cwd = os .getcwd ()
812- fname = os .path .join (cwd , 'matplotlibrc' )
813- if os .path .exists (fname ):
814- return fname
815-
816- if 'MATPLOTLIBRC' in os .environ :
817- path = os .environ ['MATPLOTLIBRC' ]
818- if os .path .exists (path ):
819- if os .path .isfile (path ):
820- return path
821- fname = os .path .join (path , 'matplotlibrc' )
822- if os .path .exists (fname ):
823- return fname
824-
825- configdir = _get_configdir ()
826- if os .path .exists (configdir ):
827- fname = os .path .join (configdir , 'matplotlibrc' )
828- if os .path .exists (fname ):
829- return fname
830-
831- path = get_data_path () # guaranteed to exist or raise
832- fname = os .path .join (path , 'matplotlibrc' )
833- if not os .path .exists (fname ):
834- warnings .warn ('Could not find matplotlibrc; using defaults' )
835797
798+ def gen_candidates ():
799+ yield os .path .join (six .moves .getcwd (), 'matplotlibrc' )
800+ try :
801+ matplotlibrc = os .environ ['MATPLOTLIBRC' ]
802+ except KeyError :
803+ pass
804+ else :
805+ yield matplotlibrc
806+ yield os .path .join (matplotlibrc , 'matplotlibrc' )
807+ yield os .path .join (_get_configdir (), 'matplotlibrc' )
808+ yield os .path .join (get_data_path (), 'matplotlibrc' )
809+
810+ for fname in gen_candidates ():
811+ if os .path .isfile (fname ):
812+ break
813+ # Return first candidate that is a file, or last candidate if none is
814+ # valid (in that case, a warning is raised at startup by `rc_params`).
836815 return fname
837816
838817
0 commit comments