1515from ._compat import *
1616from . import finalseg
1717
18- from shutil import move as _replace_file
18+ if os .name == 'nt' :
19+ from shutil import move as _replace_file
20+ else :
21+ _replace_file = os .rename
1922
2023_get_module_path = lambda path : os .path .normpath (os .path .join (os .getcwd (),
2124 os .path .dirname (__file__ ), path ))
@@ -107,11 +110,14 @@ def initialize(self, dictionary=None):
107110 # default dictionary
108111 elif abs_path == DEFAULT_DICT :
109112 cache_file = "jieba.cache"
110- else : # custom dictionary
113+ # custom dictionary
114+ else :
111115 cache_file = "jieba.u%s.cache" % md5 (
112116 abs_path .encode ('utf-8' , 'replace' )).hexdigest ()
113117 cache_file = os .path .join (
114118 self .tmp_dir or tempfile .gettempdir (), cache_file )
119+ # prevent absolute path in self.cache_file
120+ tmpdir = os .path .dirname (cache_file )
115121
116122 load_from_cache_fail = True
117123 if os .path .isfile (cache_file ) and os .path .getmtime (cache_file ) > os .path .getmtime (abs_path ):
@@ -132,7 +138,8 @@ def initialize(self, dictionary=None):
132138 default_logger .debug (
133139 "Dumping model to file cache %s" % cache_file )
134140 try :
135- fd , fpath = tempfile .mkstemp ()
141+ # prevent moving across different filesystems
142+ fd , fpath = tempfile .mkstemp (dir = tmpdir )
136143 with os .fdopen (fd , 'wb' ) as temp_cache_file :
137144 marshal .dump (
138145 (self .FREQ , self .total ), temp_cache_file )
0 commit comments