Skip to content

Commit 66fe175

Browse files
committed
prevent moving across different filesystems at tempfile.mkstemp
1 parent be46dde commit 66fe175

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

jieba/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
from ._compat import *
1616
from . 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

Comments
 (0)