提交 5e516c42 authored 作者: abalkin's avatar abalkin

Porting to py3k: use 'six'; import theano now works.

上级 e2749272
...@@ -177,7 +177,7 @@ def do_setup(): ...@@ -177,7 +177,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'], install_requires=['numpy>=1.5.0', 'scipy>=0.7.2', 'six>=1.2.0'],
package_data={ package_data={
'': ['*.txt', '*.rst', '*.cu', '*.cuh', '*.c', '*.sh', '': ['*.txt', '*.rst', '*.cu', '*.cuh', '*.c', '*.sh',
'ChangeLog'], 'ChangeLog'],
......
...@@ -10,6 +10,7 @@ from itertools import izip ...@@ -10,6 +10,7 @@ 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
...@@ -1563,8 +1564,8 @@ class _VariableEquivalenceTracker(object): ...@@ -1563,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 = [theano.gof.Op.make_thunk.im_func, default_make_thunk = [six.get_unbound_function(theano.gof.Op.make_thunk),
theano.gof.OpenMPOp.make_thunk.im_func] six.get_unbound_function(theano.gof.OpenMPOp.make_thunk)]
class _Linker(gof.link.LocalLinker): class _Linker(gof.link.LocalLinker):
......
...@@ -6,7 +6,9 @@ import logging ...@@ -6,7 +6,9 @@ import logging
import os import os
import sys import sys
import warnings import warnings
import ConfigParser
from six.moves import configparser as ConfigParser
import StringIO import StringIO
import theano import theano
......
...@@ -13,6 +13,7 @@ import subprocess ...@@ -13,6 +13,7 @@ import subprocess
import sys import sys
import tempfile import tempfile
import time import time
from six import b
import distutils.sysconfig import distutils.sysconfig
...@@ -1572,7 +1573,7 @@ class GCC_compiler(object): ...@@ -1572,7 +1573,7 @@ class GCC_compiler(object):
lib_dirs.append(python_lib) lib_dirs.append(python_lib)
cppfilename = os.path.join(location, 'mod.cpp') cppfilename = os.path.join(location, 'mod.cpp')
cppfile = file(cppfilename, 'w') cppfile = open(cppfilename, 'w')
_logger.debug('Writing module C++ code to %s', cppfilename) _logger.debug('Writing module C++ code to %s', cppfilename)
...@@ -1630,14 +1631,14 @@ class GCC_compiler(object): ...@@ -1630,14 +1631,14 @@ class GCC_compiler(object):
# prints the exception, having '\n' in the text makes it more # prints the exception, having '\n' in the text makes it more
# difficult to read. # difficult to read.
raise Exception('Compilation failed (return status=%s): %s' % raise Exception('Compilation failed (return status=%s): %s' %
(status, compile_stderr.replace('\n', '. '))) (status, compile_stderr.replace(b('\n'), b('. '))))
elif config.cmodule.compilation_warning and compile_stderr: elif config.cmodule.compilation_warning and compile_stderr:
# Print errors just below the command line. # Print errors just below the command line.
print compile_stderr print compile_stderr
if py_module: if py_module:
#touch the __init__ file #touch the __init__ file
file(os.path.join(location, "__init__.py"), 'w').close() open(os.path.join(location, "__init__.py"), 'w').close()
return dlimport(lib_filename) return dlimport(lib_filename)
......
...@@ -27,7 +27,7 @@ try: ...@@ -27,7 +27,7 @@ try:
stdin=dummy_in.fileno(), stdin=dummy_in.fileno(),
stderr=dummy_err.fileno()) stderr=dummy_err.fileno())
p.wait() p.wait()
gcc_version_str = p.stdout.readline().strip() gcc_version_str = p.stdout.readline().strip().decode()
except OSError: except OSError:
# Typically means gcc cannot be found. # Typically means gcc cannot be found.
gcc_version_str = 'GCC_NOT_FOUND' gcc_version_str = 'GCC_NOT_FOUND'
......
...@@ -13,6 +13,17 @@ typedef int Py_ssize_t; ...@@ -13,6 +13,17 @@ typedef int Py_ssize_t;
#define PyNumber_AsSsize_t(ob, exc) PyInt_AsLong(ob) #define PyNumber_AsSsize_t(ob, exc) PyInt_AsLong(ob)
#endif #endif
#if PY_VERSION_HEX >= 0x03000000
#include "numpy/npy_3kcompat.h"
#define PyCObject_AsVoidPtr NpyCapsule_AsVoidPtr
#define PyCObject_GetDesc NpyCapsule_GetDesc
#define PyCObject_Check NpyCapsule_Check
#endif
#ifndef Py_TYPE
#define Py_TYPE(obj) obj->ob_type
#endif
/** /**
TODO: TODO:
...@@ -172,7 +183,7 @@ CLazyLinker_dealloc(PyObject* _self) ...@@ -172,7 +183,7 @@ CLazyLinker_dealloc(PyObject* _self)
Py_XDECREF(self->call_times); Py_XDECREF(self->call_times);
Py_XDECREF(self->call_counts); Py_XDECREF(self->call_counts);
Py_XDECREF(self->pre_call_clear); Py_XDECREF(self->pre_call_clear);
self->ob_type->tp_free((PyObject*)self); Py_TYPE(self)->tp_free((PyObject*)self);
} }
static PyObject * static PyObject *
CLazyLinker_new(PyTypeObject *type, PyObject *args, PyObject *kwds) CLazyLinker_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
...@@ -937,8 +948,12 @@ static PyMemberDef CLazyLinker_members[] = { ...@@ -937,8 +948,12 @@ static PyMemberDef CLazyLinker_members[] = {
}; };
static PyTypeObject lazylinker_ext_CLazyLinkerType = { static PyTypeObject lazylinker_ext_CLazyLinkerType = {
#if defined(NPY_PY3K)
PyVarObject_HEAD_INIT(NULL, 0)
#else
PyObject_HEAD_INIT(NULL) PyObject_HEAD_INIT(NULL)
0, /*ob_size*/ 0, /*ob_size*/
#endif
"lazylinker_ext.CLazyLinker", /*tp_name*/ "lazylinker_ext.CLazyLinker", /*tp_name*/
sizeof(CLazyLinker), /*tp_basicsize*/ sizeof(CLazyLinker), /*tp_basicsize*/
0, /*tp_itemsize*/ 0, /*tp_itemsize*/
...@@ -992,19 +1007,42 @@ static PyMethodDef lazylinker_ext_methods[] = { ...@@ -992,19 +1007,42 @@ static PyMethodDef lazylinker_ext_methods[] = {
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
#define PyMODINIT_FUNC void #define PyMODINIT_FUNC void
#endif #endif
#if defined(NPY_PY3K)
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"lazylinker_ext",
NULL,
-1,
lazylinker_ext_methods,
NULL,
NULL,
NULL,
NULL
};
#endif
#if defined(NPY_PY3K)
#define RETVAL m
PyObject *PyInit_lazylinker_ext(void) {
#else
PyMODINIT_FUNC PyMODINIT_FUNC
initlazylinker_ext(void) initlazylinker_ext(void)
{ {
#endif
PyObject* m; PyObject* m;
lazylinker_ext_CLazyLinkerType.tp_new = PyType_GenericNew; lazylinker_ext_CLazyLinkerType.tp_new = PyType_GenericNew;
if (PyType_Ready(&lazylinker_ext_CLazyLinkerType) < 0) if (PyType_Ready(&lazylinker_ext_CLazyLinkerType) < 0)
return; return RETVAL;
#if defined(NPY_PY3K)
m = PyModule_Create(&moduledef);
#else
m = Py_InitModule3("lazylinker_ext", lazylinker_ext_methods, m = Py_InitModule3("lazylinker_ext", lazylinker_ext_methods,
"Example module that creates an extension type."); "Example module that creates an extension type.");
#endif
Py_INCREF(&lazylinker_ext_CLazyLinkerType); Py_INCREF(&lazylinker_ext_CLazyLinkerType);
PyModule_AddObject(m, "CLazyLinker", (PyObject *)&lazylinker_ext_CLazyLinkerType); PyModule_AddObject(m, "CLazyLinker", (PyObject *)&lazylinker_ext_CLazyLinkerType);
return RETVAL;
} }
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
...@@ -45,7 +46,7 @@ try: ...@@ -45,7 +46,7 @@ try:
assert os.path.isdir(location) assert os.path.isdir(location)
if not os.path.exists(os.path.join(location, '__init__.py')): if not os.path.exists(os.path.join(location, '__init__.py')):
file(os.path.join(location, '__init__.py'), 'w').close() open(os.path.join(location, '__init__.py'), 'w').close()
_need_reload = False _need_reload = False
if force_compile: if force_compile:
......
...@@ -13,7 +13,9 @@ imported_scipy_special = False ...@@ -13,7 +13,9 @@ imported_scipy_special = False
try: try:
import scipy.special import scipy.special
imported_scipy_special = True imported_scipy_special = True
except ImportError: # Importing scipy.special may raise ValueError.
# See http://projects.scipy.org/scipy/ticket/1739
except (ImportError, ValueError):
pass pass
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论