提交 a793ab8d authored 作者: Frederic's avatar Frederic

Fix circular import by refactoring

上级 700d883b
...@@ -177,7 +177,7 @@ def get_config_md5(): ...@@ -177,7 +177,7 @@ def get_config_md5():
""" """
all_opts = sorted([c for c in _config_var_list if c.in_c_key], all_opts = sorted([c for c in _config_var_list if c.in_c_key],
key=lambda cv: cv.fullname) key=lambda cv: cv.fullname)
return theano.gof.cc.hash_from_code('\n'.join( return theano.gof.utils.hash_from_code('\n'.join(
['%s = %s' % (cv.fullname, cv.__get__()) for cv in all_opts])) ['%s = %s' % (cv.fullname, cv.__get__()) for cv in all_opts]))
......
...@@ -36,36 +36,10 @@ AddConfigVar('gcc.cxxflags', ...@@ -36,36 +36,10 @@ AddConfigVar('gcc.cxxflags',
"Extra compiler flags for gcc", "Extra compiler flags for gcc",
StrParam("")) StrParam(""))
if PY3:
import hashlib
def hash_from_code(msg):
# hashlib.md5() requires an object that supports buffer interface,
# but Python 3 (unicode) strings don't.
if isinstance(msg, str):
msg = msg.encode()
# Python 3 does not like module names that start with
# a digit.
return 'm' + hashlib.md5(msg).hexdigest()
else:
import hashlib
def hash_from_code(msg):
try:
return hashlib.md5(msg).hexdigest()
except TypeError:
assert isinstance(msg, numpy.ndarray)
return hashlib.md5(numpy.getbuffer(msg)).hexdigest()
_logger = logging.getLogger("theano.gof.cc") _logger = logging.getLogger("theano.gof.cc")
def hash_from_file(file_path):
"""Return the MD5 hash of a file."""
return hash_from_code(open(file_path, 'rb').read())
run_cthunk = None # Will be imported only when needed. run_cthunk = None # Will be imported only when needed.
......
...@@ -24,7 +24,7 @@ from theano.compat import PY3, decode, decode_iter ...@@ -24,7 +24,7 @@ from theano.compat import PY3, decode, decode_iter
from six import b, BytesIO, StringIO, string_types, iteritems from six import b, BytesIO, StringIO, string_types, iteritems
from theano.gof.utils import flatten from theano.gof.utils import flatten
from theano.configparser import config from theano.configparser import config
from theano.gof.cc import hash_from_code from theano.gof.utils import hash_from_code
from theano.misc.windows import (subprocess_Popen, from theano.misc.windows import (subprocess_Popen,
output_subprocess_Popen) output_subprocess_Popen)
......
...@@ -3,9 +3,11 @@ import linecache ...@@ -3,9 +3,11 @@ import linecache
import traceback import traceback
import sys import sys
import numpy
from six import iteritems from six import iteritems
from theano import config from theano import config
from theano.compat import PY3
def simple_extract_stack(f=None, limit=None): def simple_extract_stack(f=None, limit=None):
...@@ -435,3 +437,31 @@ def remove(predicate, coll): ...@@ -435,3 +437,31 @@ def remove(predicate, coll):
[1, 3] [1, 3]
""" """
return [x for x in coll if not predicate(x)] return [x for x in coll if not predicate(x)]
if PY3:
import hashlib
def hash_from_code(msg):
# hashlib.md5() requires an object that supports buffer interface,
# but Python 3 (unicode) strings don't.
if isinstance(msg, str):
msg = msg.encode()
# Python 3 does not like module names that start with
# a digit.
return 'm' + hashlib.md5(msg).hexdigest()
else:
import hashlib
def hash_from_code(msg):
try:
return hashlib.md5(msg).hexdigest()
except TypeError:
assert isinstance(msg, numpy.ndarray)
return hashlib.md5(numpy.getbuffer(msg)).hexdigest()
def hash_from_file(file_path):
"""Return the MD5 hash of a file."""
return hash_from_code(open(file_path, 'rb').read())
...@@ -10,7 +10,7 @@ import numpy ...@@ -10,7 +10,7 @@ import numpy
from theano.compat import decode, decode_iter from theano.compat import decode, decode_iter
from theano.gof import local_bitwidth from theano.gof import local_bitwidth
from theano.gof.cc import hash_from_file from theano.gof.utils import hash_from_file
from theano.gof.cmodule import (std_libs, std_lib_dirs, from theano.gof.cmodule import (std_libs, std_lib_dirs,
std_include_dirs, dlimport, std_include_dirs, dlimport,
Compiler, Compiler,
......
from theano.gof.cc import hash_from_code from theano.gof.utils import hash_from_code
def hash_from_sparse(data): def hash_from_sparse(data):
......
...@@ -2,7 +2,7 @@ import numpy ...@@ -2,7 +2,7 @@ import numpy
import theano import theano
from theano.compat import izip from theano.compat import izip
from theano.gof.cc import hash_from_code from theano.gof.utils import hash_from_code
def hash_from_ndarray(data): def hash_from_ndarray(data):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论