3333import textwrap
3434import time
3535import traceback
36+ from enum import Enum
3637from itertools import takewhile
38+ from pathlib import Path
3739from types import ModuleType
38- from enum import Enum
3940
4041from pygments .token import Token
4142from pygments .lexers import Python3Lexer
@@ -435,11 +436,10 @@ def __init__(self, interp, config):
435436 self .closed = False
436437 self .clipboard = get_clipboard ()
437438
438- pythonhist = os .path .expanduser (self .config .hist_file )
439- if os .path .exists (pythonhist ):
439+ if self .config .hist_file .exists ():
440440 try :
441441 self .rl_history .load (
442- pythonhist , getpreferredencoding () or "ascii"
442+ self . config . hist_file , getpreferredencoding () or "ascii"
443443 )
444444 except OSError :
445445 pass
@@ -829,13 +829,13 @@ def write2file(self):
829829 self .interact .notify (_ ("Save cancelled." ))
830830 return
831831
832- if fn . startswith ( "~" ):
833- fn = os . path . expanduser ( fn )
834- if not fn .endswith (".py" ) and self . config . save_append_py :
835- fn = fn + " .py"
832+ fn = Path ( fn ). expanduser ()
833+ if fn . suffix != ".py" and self . config . save_append_py :
834+ # fn.with_suffix (".py") does not append if fn has a non-empty suffix
835+ fn = Path ( f" { fn } .py")
836836
837837 mode = "w"
838- if os . path . exists (fn ):
838+ if fn . exists ():
839839 mode = self .interact .file_prompt (
840840 _ (
841841 "%s already exists. Do you "
@@ -941,10 +941,9 @@ def push(self, s, insert_into_history=True):
941941 return more
942942
943943 def insert_into_history (self , s ):
944- pythonhist = os .path .expanduser (self .config .hist_file )
945944 try :
946945 self .rl_history .append_reload_and_write (
947- s , pythonhist , getpreferredencoding ()
946+ s , self . config . hist_file , getpreferredencoding ()
948947 )
949948 except RuntimeError as e :
950949 self .interact .notify (f"{ e } " )
@@ -1147,7 +1146,7 @@ def open_in_external_editor(self, filename):
11471146 return subprocess .call (args ) == 0
11481147
11491148 def edit_config (self ):
1150- if not os . path . isfile ( self .config .config_path ):
1149+ if not self .config .config_path . is_file ( ):
11511150 if self .interact .confirm (
11521151 _ (
11531152 "Config file does not exist - create "
@@ -1160,17 +1159,15 @@ def edit_config(self):
11601159 )
11611160 # Py3 files need unicode
11621161 default_config = default_config .decode ("ascii" )
1163- containing_dir = os .path .dirname (
1164- os .path .abspath (self .config .config_path )
1165- )
1166- if not os .path .exists (containing_dir ):
1167- os .makedirs (containing_dir )
1162+ containing_dir = self .config .config_path .parent
1163+ if not containing_dir .exists ():
1164+ containing_dir .mkdir (parents = True )
11681165 with open (self .config .config_path , "w" ) as f :
11691166 f .write (default_config )
11701167 except OSError as e :
11711168 self .interact .notify (
11721169 _ ("Error writing file '%s': %s" )
1173- % (self .config .config . path , e )
1170+ % (self .config .config_path , e )
11741171 )
11751172 return False
11761173 else :
0 commit comments