提交 27e4c0d2 authored 作者: abalkin's avatar abalkin

Issue #783: python3 compatible - introduced theano.compat.

- Included six.py by Benjamin Peterson. (Commit 62d8be3 2013-01-13.) See <https://bitbucket.org/gutworth/six>. - Moved Python 3 compatibility code to theano.compat.
上级 19ec1bfd
...@@ -181,7 +181,7 @@ def do_setup(): ...@@ -181,7 +181,7 @@ def do_setup():
license=LICENSE, license=LICENSE,
platforms=PLATFORMS, platforms=PLATFORMS,
packages=find_packages(), packages=find_packages(),
install_requires=['numpy>=1.5.0', 'scipy>=0.7.2', 'six>=1.2.0'], install_requires=['numpy>=1.5.0', 'scipy>=0.7.2'],
package_data={ package_data={
'': ['*.txt', '*.rst', '*.cu', '*.cuh', '*.c', '*.sh', '': ['*.txt', '*.rst', '*.cu', '*.cuh', '*.c', '*.sh',
'ChangeLog'], 'ChangeLog'],
......
"""Code supporting compatibility across versions of Python.
"""
# Python 3.x compatibility
from theano.compat.six import PY3, b, BytesIO, next, get_unbound_function
from theano.compat.six.moves import reload_module as reload, configparser
if PY3:
from operator import truediv as operator_div
# In python 3.x, when an exception is reraised it saves original
# exception in its args, therefore in order to find the actual
# message, we need to unpack arguments recurcively.
def exc_message(e):
msg = e.args[0]
if isinstance(msg, Exception):
return exc_message(msg)
return msg
def cmp(a, b):
"""Return -1 if x<y, 0 if x==y, 1 if x > y."""
return (a > b) - (a < b)
else:
from operator import div as operator_div
def exc_message(e):
return e[0]
cmp = cmp
差异被折叠。
...@@ -10,10 +10,10 @@ from itertools import izip ...@@ -10,10 +10,10 @@ from itertools import izip
from StringIO import StringIO from StringIO import StringIO
import numpy import numpy
import six
import theano import theano
from theano import gof from theano import gof
from theano.compat import get_unbound_function
from theano.gof import FunctionGraph,graph, utils, link, ops_with_inner_function from theano.gof import FunctionGraph,graph, utils, link, ops_with_inner_function
from theano.gof.link import raise_with_op from theano.gof.link import raise_with_op
from theano.gof.cc import CLinker from theano.gof.cc import CLinker
...@@ -1564,8 +1564,8 @@ class _VariableEquivalenceTracker(object): ...@@ -1564,8 +1564,8 @@ class _VariableEquivalenceTracker(object):
#List of default version of make thunk. #List of default version of make thunk.
#This is needed to know if the user overrided it. #This is needed to know if the user overrided it.
#The GpuOp will be added here when theano.sandbox.cuda is imported. #The GpuOp will be added here when theano.sandbox.cuda is imported.
default_make_thunk = [six.get_unbound_function(theano.gof.Op.make_thunk), default_make_thunk = [get_unbound_function(theano.gof.Op.make_thunk),
six.get_unbound_function(theano.gof.OpenMPOp.make_thunk)] get_unbound_function(theano.gof.OpenMPOp.make_thunk)]
class _Linker(gof.link.LocalLinker): class _Linker(gof.link.LocalLinker):
......
...@@ -632,7 +632,7 @@ class T_picklefunction(unittest.TestCase): ...@@ -632,7 +632,7 @@ class T_picklefunction(unittest.TestCase):
f = theano.function([x], theano.tensor.dot(x, y)) f = theano.function([x], theano.tensor.dot(x, y))
from six import BytesIO from theano.compat import BytesIO
fp = BytesIO() fp = BytesIO()
p = cPickle.Pickler(fp, 2) p = cPickle.Pickler(fp, 2)
p.persistent_id = pers_save p.persistent_id = pers_save
......
...@@ -4,27 +4,14 @@ ...@@ -4,27 +4,14 @@
__docformat__ = "restructuredtext en" __docformat__ = "restructuredtext en"
import cPickle, numpy, unittest import cPickle, numpy, unittest
from theano import config from theano import config
from theano.compat import exc_message
from theano.compile.module import * from theano.compile.module import *
from theano.compile.function_module import AliasedMemoryError from theano.compile.function_module import AliasedMemoryError
import theano.tensor as T import theano.tensor as T
import sys, copy import sys, copy
import theano import theano
from six import PY3
# Copied from theano/tensor/tests/test_basic.py
if PY3:
# In python 3.x, when an exception is reraised it saves original
# exception in its args, therefore in order to find the actual
# message, we need to unpack arguments recurcively.
def exc_message(e):
msg = e.args[0]
if isinstance(msg, Exception):
return exc_message(msg)
return msg
else:
def exc_message(e):
return e[0]
#TODO: add test for module.make(member=init_value) #TODO: add test for module.make(member=init_value)
class T_module(unittest.TestCase): class T_module(unittest.TestCase):
......
...@@ -7,11 +7,10 @@ import os ...@@ -7,11 +7,10 @@ import os
import sys import sys
import warnings import warnings
from six.moves import configparser as ConfigParser
import StringIO import StringIO
import theano import theano
from theano.compat import configparser as ConfigParser
_logger = logging.getLogger('theano.configparser') _logger = logging.getLogger('theano.configparser')
......
...@@ -9,11 +9,12 @@ import os ...@@ -9,11 +9,12 @@ import os
import StringIO import StringIO
import sys import sys
from itertools import izip from itertools import izip
from six import PY3
import numpy import numpy
if sys.version_info[:2] >= (3, 0): from theano.compat import PY3
if PY3:
import hashlib import hashlib
def hash_from_code(msg): def hash_from_code(msg):
......
...@@ -13,13 +13,13 @@ import subprocess ...@@ -13,13 +13,13 @@ import subprocess
import sys import sys
import tempfile import tempfile
import time import time
from six import PY3, b, next
import distutils.sysconfig import distutils.sysconfig
import numpy.distutils # TODO: TensorType should handle this import numpy.distutils # TODO: TensorType should handle this
import theano import theano
from theano.compat import PY3, b, next
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.cc import hash_from_code
......
import os import os, sys
import sys
from six import PY3 from theano.compat import PY3
from theano.gof.compilelock import get_lock, release_lock from theano.gof.compilelock import get_lock, release_lock
from theano import config from theano import config
......
import errno import errno
import os, logging, sys import os, logging, sys
from six.moves import reload_module as reload
import theano import theano
from theano import config from theano import config
from theano.compat import reload
from theano.gof.compilelock import get_lock, release_lock from theano.gof.compilelock import get_lock, release_lock
from theano.gof import cmodule from theano.gof import cmodule
......
from theano.gof.graph import list_of_nodes from theano.gof.graph import list_of_nodes
from theano.gof.python25 import any, defaultdict from theano.gof.python25 import any, defaultdict
try: from theano.compat import cmp
cmp
except NameError:
def cmp(a, b):
return (a > b) - (a < b)
## {{{ http://code.activestate.com/recipes/578231/ (r1) ## {{{ http://code.activestate.com/recipes/578231/ (r1)
# Copyright (c) Oren Tirosh 2012 # Copyright (c) Oren Tirosh 2012
......
from unittest import TestCase from unittest import TestCase
from six import PY3
if PY3:
# In python 3.x, when an exception is reraised it saves original
# exception in its args, therefore in order to find the actual
# message, we need to unpack arguments recurcively.
def exc_message(e):
msg = e.args[0]
if isinstance(msg, Exception):
return exc_message(msg)
return msg
else:
def exc_message(e):
return e[0]
from theano.compat import exc_message
from theano.gof.optdb import opt, DB from theano.gof.optdb import opt, DB
......
...@@ -5,12 +5,7 @@ import theano ...@@ -5,12 +5,7 @@ import theano
from theano import tensor from theano import tensor
from theano.gof.graph import io_toposort from theano.gof.graph import io_toposort
from theano.gof.python25 import any from theano.gof.python25 import any
try: from theano.compat import cmp
cmp
except NameError:
def cmp(a, b):
return (a > b) - (a < b)
def test_dependence(): def test_dependence():
dependence = make_dependence_cmp() dependence = make_dependence_cmp()
......
...@@ -6,9 +6,8 @@ import shutil ...@@ -6,9 +6,8 @@ import shutil
import stat import stat
import sys import sys
import six
import theano import theano
from theano.compat import get_unbound_function
from theano.compile import optdb from theano.compile import optdb
from theano.gof.cmodule import get_lib_extension from theano.gof.cmodule import get_lib_extension
from theano.gof.compilelock import get_lock, release_lock from theano.gof.compilelock import get_lock, release_lock
...@@ -243,7 +242,7 @@ class GpuOp(theano.gof.Op): ...@@ -243,7 +242,7 @@ class GpuOp(theano.gof.Op):
return super(GpuOp, self).make_thunk(node, storage_map, return super(GpuOp, self).make_thunk(node, storage_map,
compute_map, no_recycling) compute_map, no_recycling)
theano.compile.debugmode.default_make_thunk.append(six.get_unbound_function(GpuOp.make_thunk)) theano.compile.debugmode.default_make_thunk.append(get_unbound_function(GpuOp.make_thunk))
# We must do those import to be able to create the full doc when # We must do those import to be able to create the full doc when
# nvcc is not available # nvcc is not available
......
...@@ -18,10 +18,10 @@ from copy import copy ...@@ -18,10 +18,10 @@ from copy import copy
from itertools import imap from itertools import imap
from textwrap import dedent from textwrap import dedent
from six import PY3
import numpy import numpy
import theano import theano
from theano.compat import PY3
from theano import gof from theano import gof
from theano.gof import Op, utils, Variable, Constant, Type, Apply, FunctionGraph from theano.gof import Op, utils, Variable, Constant, Type, Apply, FunctionGraph
from theano.gof.python25 import partial, all, any from theano.gof.python25 import partial, all, any
......
import os, logging, sys import os, logging, sys
from six.moves import reload_module as reload
import theano import theano
from theano import config from theano import config
from theano.compat import reload
from theano.gof.compilelock import get_lock, release_lock from theano.gof.compilelock import get_lock, release_lock
from theano.gof import cmodule from theano.gof import cmodule
......
...@@ -2,7 +2,6 @@ from nose.plugins.skip import SkipTest ...@@ -2,7 +2,6 @@ from nose.plugins.skip import SkipTest
import sys import sys
import time import time
import unittest import unittest
from six import next
import theano.sparse import theano.sparse
if not theano.sparse.enable_sparse: if not theano.sparse.enable_sparse:
...@@ -15,6 +14,7 @@ import numpy ...@@ -15,6 +14,7 @@ import numpy
from theano import function, tensor from theano import function, tensor
import theano import theano
from theano.compat import next
from theano.sparse.sandbox import sp from theano.sparse.sandbox import sp
from theano.sparse.tests.test_basic import random_lil from theano.sparse.tests.test_basic import random_lil
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
......
...@@ -6,11 +6,11 @@ import sys ...@@ -6,11 +6,11 @@ import sys
import warnings import warnings
from itertools import izip from itertools import izip
from six import PY3
import numpy import numpy
#from copy import copy as python_copy #from copy import copy as python_copy
import theano import theano
from theano.compat import PY3
from theano.configparser import config from theano.configparser import config
from theano import gof from theano import gof
from theano.gof import Apply, Constant, Op, Type, Variable from theano.gof import Apply, Constant, Op, Type, Variable
......
...@@ -13,29 +13,13 @@ from itertools import izip ...@@ -13,29 +13,13 @@ from itertools import izip
import __builtin__ import __builtin__
builtin_min = __builtin__.min builtin_min = __builtin__.min
from six import PY3
if PY3:
operator_div = operator.truediv
# In python 3.x, when an exception is reraised it saves original
# exception in its args, therefore in order to find the actual
# message, we need to unpack arguments recurcively.
def exc_message(e):
msg = e.args[0]
if isinstance(msg, Exception):
return exc_message(msg)
return msg
else:
operator_div = operator.div
def exc_message(e):
return e[0]
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
import numpy import numpy
from numpy.testing import dec, assert_array_equal, assert_allclose from numpy.testing import dec, assert_array_equal, assert_allclose
from numpy.testing.noseclasses import KnownFailureTest from numpy.testing.noseclasses import KnownFailureTest
import theano import theano
from theano.compat import PY3, exc_message, operator_div
from theano import compile, config, function, gof, tensor, shared from theano import compile, config, function, gof, tensor, shared
from theano.compile import DeepCopyOp from theano.compile import DeepCopyOp
from theano.compile.mode import get_default_mode from theano.compile.mode import get_default_mode
......
...@@ -2,23 +2,10 @@ ...@@ -2,23 +2,10 @@
#import traceback #import traceback
import itertools import itertools
import sys import sys
# Copied from tensor/tests/test_basic.py.
from six import PY3
if PY3:
# In python 3.x, when an exception is reraised it saves original
# exception in its args, therefore in order to find the actual
# message, we need to unpack arguments recurcively.
def exc_message(e):
msg = e.args[0]
if isinstance(msg, Exception):
return exc_message(msg)
return msg
else:
def exc_message(e):
return e[0]
import theano.tensor as T import theano.tensor as T
from theano import tensor from theano import tensor
from theano.compat import PY3, exc_message
from theano.gof.python25 import product as itertools_product from theano.gof.python25 import product as itertools_product
from theano.gof.python25 import any from theano.gof.python25 import any
from theano.printing import pp from theano.printing import pp
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论