mirror of
https://github.com/fxsjy/jieba.git
synced 2025-07-10 00:01:33 +08:00
prevent moving across different filesystems at tempfile.mkstemp
This commit is contained in:
parent
be46ddef9a
commit
66fe17517d
@ -15,7 +15,10 @@ from hashlib import md5
|
|||||||
from ._compat import *
|
from ._compat import *
|
||||||
from . import finalseg
|
from . import finalseg
|
||||||
|
|
||||||
from shutil import move as _replace_file
|
if os.name == 'nt':
|
||||||
|
from shutil import move as _replace_file
|
||||||
|
else:
|
||||||
|
_replace_file = os.rename
|
||||||
|
|
||||||
_get_module_path = lambda path: os.path.normpath(os.path.join(os.getcwd(),
|
_get_module_path = lambda path: os.path.normpath(os.path.join(os.getcwd(),
|
||||||
os.path.dirname(__file__), path))
|
os.path.dirname(__file__), path))
|
||||||
@ -107,11 +110,14 @@ class Tokenizer(object):
|
|||||||
# default dictionary
|
# default dictionary
|
||||||
elif abs_path == DEFAULT_DICT:
|
elif abs_path == DEFAULT_DICT:
|
||||||
cache_file = "jieba.cache"
|
cache_file = "jieba.cache"
|
||||||
else: # custom dictionary
|
# custom dictionary
|
||||||
|
else:
|
||||||
cache_file = "jieba.u%s.cache" % md5(
|
cache_file = "jieba.u%s.cache" % md5(
|
||||||
abs_path.encode('utf-8', 'replace')).hexdigest()
|
abs_path.encode('utf-8', 'replace')).hexdigest()
|
||||||
cache_file = os.path.join(
|
cache_file = os.path.join(
|
||||||
self.tmp_dir or tempfile.gettempdir(), cache_file)
|
self.tmp_dir or tempfile.gettempdir(), cache_file)
|
||||||
|
# prevent absolute path in self.cache_file
|
||||||
|
tmpdir = os.path.dirname(cache_file)
|
||||||
|
|
||||||
load_from_cache_fail = True
|
load_from_cache_fail = True
|
||||||
if os.path.isfile(cache_file) and os.path.getmtime(cache_file) > os.path.getmtime(abs_path):
|
if os.path.isfile(cache_file) and os.path.getmtime(cache_file) > os.path.getmtime(abs_path):
|
||||||
@ -132,7 +138,8 @@ class Tokenizer(object):
|
|||||||
default_logger.debug(
|
default_logger.debug(
|
||||||
"Dumping model to file cache %s" % cache_file)
|
"Dumping model to file cache %s" % cache_file)
|
||||||
try:
|
try:
|
||||||
fd, fpath = tempfile.mkstemp()
|
# prevent moving across different filesystems
|
||||||
|
fd, fpath = tempfile.mkstemp(dir=tmpdir)
|
||||||
with os.fdopen(fd, 'wb') as temp_cache_file:
|
with os.fdopen(fd, 'wb') as temp_cache_file:
|
||||||
marshal.dump(
|
marshal.dump(
|
||||||
(self.FREQ, self.total), temp_cache_file)
|
(self.FREQ, self.total), temp_cache_file)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user