Skip to content

Commit

Permalink
MAINT: weave: fix Python 3 syntax errors
Browse files Browse the repository at this point in the history
The weave module doesn't work on Python 3, but these changes at least
make its syntax compatible, so that no SyntaxErrors arise on build.

2to3 fixers used: print, import, repr, exec, except
Added from __future__ import absolute_import, print_function
on top.
  • Loading branch information
pv committed Mar 12, 2013
1 parent c98070a commit aad4b5e
Show file tree
Hide file tree
Showing 65 changed files with 418 additions and 318 deletions.
13 changes: 7 additions & 6 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,26 @@
scipy.
"""
from __future__ import absolute_import, print_function

import sys

if sys.version_info[0] >= 3:
raise ImportError("scipy.weave only supports Python 2.x")


from weave_version import weave_version as __version__
from .weave_version import weave_version as __version__

try:
from blitz_tools import blitz
from .blitz_tools import blitz
except ImportError:
pass # scipy (core) wasn't available

from inline_tools import inline
import ext_tools
from ext_tools import ext_module, ext_function
from .inline_tools import inline
from . import ext_tools
from .ext_tools import ext_module, ext_function
try:
from accelerate_tools import accelerate
from .accelerate_tools import accelerate
except:
pass

Expand Down
7 changes: 4 additions & 3 deletions accelerate_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
C++ equivalents to Python functions.
"""
#**************************************************************************#
from __future__ import absolute_import, print_function

from types import InstanceType, XRangeType
import inspect
import scipy.weave.md5_load as md5
import scipy.weave as weave
from numpy.testing import assert_

from bytecodecompiler import CXXCoder,Type_Descriptor,Function_Descriptor
from .bytecodecompiler import CXXCoder,Type_Descriptor,Function_Descriptor

def CStr(s):
"Hacky way to get legal C string from Python string"
Expand Down Expand Up @@ -315,9 +316,9 @@ def singleton(self,signature):

# See if we have an accelerated version of module
try:
print 'lookup',self.module.__name__+'_weave'
print('lookup',self.module.__name__+'_weave')
accelerated_module = __import__(self.module.__name__+'_weave')
print 'have accelerated',self.module.__name__+'_weave'
print('have accelerated',self.module.__name__+'_weave')
fast = getattr(accelerated_module,identifier)
return fast
except ImportError:
Expand Down
4 changes: 3 additions & 1 deletion ast_tools.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import absolute_import, print_function

import token
import symbol
import parser
Expand Down Expand Up @@ -63,7 +65,7 @@ def build_atom(expr_string):
if isinstance(expr_string, str):
ast = parser.expr(expr_string).totuple()[1][1]
else:
ast = parser.expr(`expr_string`).totuple()[1][1]
ast = parser.expr(repr(expr_string)).totuple()[1][1]
return ast

def atom_tuple(expr_string):
Expand Down
2 changes: 2 additions & 0 deletions base_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
info_list -- a handy list class for working with multiple
info classes at the same time.
"""
from __future__ import absolute_import, print_function

import UserList

class base_info(object):
Expand Down
4 changes: 3 additions & 1 deletion base_spec.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import absolute_import, print_function

class base_converter(object):
"""
Properties:
Expand Down Expand Up @@ -69,7 +71,7 @@ def variable_as_string(self):
return '"' + self.name + '"'

import UserList
import base_info
from . import base_info

class arg_spec_list(UserList.UserList):
def build_information(self):
Expand Down
7 changes: 4 additions & 3 deletions blitz_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
array_info -- for building functions that use scipy arrays
"""
from __future__ import absolute_import, print_function

import base_info
import standard_array_spec
from . import base_info
from . import standard_array_spec
import os

blitz_support_code = \
Expand Down Expand Up @@ -56,7 +57,7 @@
}
"""

import blitz_spec
from . import blitz_spec
local_dir,junk = os.path.split(os.path.abspath(blitz_spec.__file__))
blitz_dir = os.path.join(local_dir,'blitz')

Expand Down
18 changes: 10 additions & 8 deletions blitz_tools.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from __future__ import absolute_import, print_function

import parser
import sys
import ast_tools
import slice_handler
import size_check
import converters
from . import ast_tools
from . import slice_handler
from . import size_check
from . import converters

import numpy
import copy

import inline_tools
from inline_tools import attempt_function_call
from . import inline_tools
from .inline_tools import attempt_function_call
function_catalog = inline_tools.function_catalog
function_cache = inline_tools.function_cache

Expand Down Expand Up @@ -64,8 +66,8 @@ def blitz(expr,local_dict=None, global_dict=None,check_size=1,verbose=0,**kw):
try:
results = attempt_function_call(expr,local_dict,global_dict)
except ValueError:
print 'warning: compilation failed. Executing as python code'
exec expr in global_dict, local_dict
print('warning: compilation failed. Executing as python code')
exec(expr, global_dict, local_dict)

def ast_to_blitz_expr(ast_seq):
""" Convert an ast_sequence to a blitz expression.
Expand Down
17 changes: 9 additions & 8 deletions build_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
does this by converting a pythonxx.lib file to a libpythonxx.a file.
Note that you need write access to the pythonxx/lib directory to do this.
"""
from __future__ import absolute_import, print_function

import sys
import os
Expand All @@ -25,7 +26,7 @@
import subprocess
import warnings

import platform_info
from . import platform_info

# If linker is 'gcc', this will convert it to 'g++'
# necessary to make sure stdc++ is linked in cross-platform way.
Expand Down Expand Up @@ -248,7 +249,7 @@ def build_extension(module_path,compiler_name = '',build_dir = None,
# the business end of the function
try:
if verbose == 1:
print 'Compiling code...'
print('Compiling code...')

# set compiler verboseness 2 or more makes it output results
if verbose > 1:
Expand Down Expand Up @@ -278,7 +279,7 @@ def build_extension(module_path,compiler_name = '',build_dir = None,
t2 = time.time()

if verbose == 1:
print 'finished compiling (sec): ', t2 - t1
print('finished compiling (sec): ', t2 - t1)
success = 1
configure_python_path(build_dir)
except SyntaxError: #TypeError:
Expand Down Expand Up @@ -398,9 +399,9 @@ def configure_temp_dir(temp_dir=None):
if temp_dir is None:
temp_dir = tempfile.gettempdir()
elif not os.path.exists(temp_dir) or not os.access(temp_dir,os.W_OK):
print "warning: specified temp_dir '%s' does not exist " \
print("warning: specified temp_dir '%s' does not exist " \
"or is not writable. Using the default temp directory" % \
temp_dir
temp_dir)
temp_dir = tempfile.gettempdir()

# final check that that directories are writable.
Expand All @@ -425,9 +426,9 @@ def configure_build_dir(build_dir=None):
# if it doesn't work use the current directory. This should always
# be writable.
if not os.access(build_dir,os.W_OK):
print "warning:, neither the module's directory nor the "\
print("warning:, neither the module's directory nor the "\
"current directory are writable. Using the temporary"\
"directory."
"directory.")
build_dir = tempfile.gettempdir()

# final check that that directories are writable.
Expand Down Expand Up @@ -610,7 +611,7 @@ def build_import_library():
success = not os.system(cmd)
# for now, fail silently
if not success:
print 'WARNING: failed to build import library for gcc. Linking will fail.'
print('WARNING: failed to build import library for gcc. Linking will fail.')
#if not success:
# msg = "Couldn't find import library, and failed to build it."
# raise DistutilsPlatformError, msg
36 changes: 19 additions & 17 deletions bytecodecompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
#**************************************************************************#
#* *#
#**************************************************************************#
from __future__ import absolute_import, print_function

import sys
import inspect
import accelerate_tools
from . import accelerate_tools

from numpy.testing import assert_

Expand Down Expand Up @@ -1001,7 +1003,7 @@ def codeup(self, rhs, rhs_type):
rhs_type.cxxtype,
lhs,
rhs))
print self.__body
print(self.__body)
self.push(lhs,rhs_type)
return

Expand Down Expand Up @@ -1029,7 +1031,7 @@ def BINARY_ADD(self,pc):
def BINARY_SUBTRACT(self,pc):
return self.binop(pc,'-')
def BINARY_MULTIPLY(self,pc):
print 'MULTIPLY',self.stack[-2],self.types[-2],'*',self.stack[-1],self.types[-1]
print('MULTIPLY',self.stack[-2],self.types[-2],'*',self.stack[-1],self.types[-1])
return self.binop(pc,'*')
def BINARY_DIVIDE(self,pc):
return self.binop(pc,'/')
Expand Down Expand Up @@ -1121,7 +1123,7 @@ def LOAD_CONST(self,pc,consti):
# Fetch the constant
k = self.consts[consti]
t = type(k)
print 'LOAD_CONST',repr(k),t
print('LOAD_CONST',repr(k),t)

# Fetch a None is just skipped
if t is None:
Expand Down Expand Up @@ -1154,13 +1156,13 @@ def BUILD_TUPLE(self,pc,count):
def LOAD_FAST(self,pc,var_num):
v = self.stack[var_num]
t = self.types[var_num]
print 'LOADFAST',var_num,v,t
print('LOADFAST',var_num,v,t)
for VV, TT in zip(self.stack, self.types):
print VV,':',TT
print(VV,':',TT)
if t is None:
raise TypeError('%s used before set?' % v)
print self.__body
print 'PC',pc
print(self.__body)
print('PC',pc)
self.push(v,t)
return

Expand All @@ -1171,10 +1173,10 @@ def LOAD_FAST(self,pc,var_num):
def LOAD_ATTR(self,pc,namei):
v,t = self.pop()
attr_name = self.codeobject.co_names[namei]
print 'LOAD_ATTR',namei,v,t,attr_name
print('LOAD_ATTR',namei,v,t,attr_name)
aType,aCode = t.get_attribute(attr_name)
print 'ATTR',aType
print aCode
print('ATTR',aType)
print(aCode)
lhs = self.unique()
rhs = v
lhsType = aType.cxxtype
Expand All @@ -1189,12 +1191,12 @@ def LOAD_ATTR(self,pc,namei):
def STORE_ATTR(self,pc,namei):
v,t = self.pop()
attr_name = self.codeobject.co_names[namei]
print 'STORE_ATTR',namei,v,t,attr_name
print('STORE_ATTR',namei,v,t,attr_name)
v2,t2 = self.pop()
print 'SA value',v2,t2
print('SA value',v2,t2)
aType,aCode = t.set_attribute(attr_name)
print 'ATTR',aType
print aCode
print('ATTR',aType)
print(aCode)
assert_(t2 is aType)
rhs = v2
lhs = v
Expand Down Expand Up @@ -1274,7 +1276,7 @@ def POP_BLOCK(self,pc):
def STORE_FAST(self,pc,var_num):

v,t = self.pop()
print 'STORE FAST',var_num,v,t
print('STORE FAST',var_num,v,t)

save = self.stack[var_num]
saveT = self.types[var_num]
Expand Down Expand Up @@ -1393,4 +1395,4 @@ def RETURN_VALUE(self,pc):
self.emit('return;')
else:
self.emit('return %s;'%v)
print 'return with',v
print('return with',v)
22 changes: 12 additions & 10 deletions c_spec.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import absolute_import, print_function

import types
from base_spec import base_converter
import base_info
from .base_spec import base_converter
from . import base_info

#----------------------------------------------------------------------------
# C++ code template for converting code from python objects to C++ objects
Expand Down Expand Up @@ -294,7 +296,7 @@ def c_to_py_code(self):
num_to_c_types[type(1.)] = 'double'
num_to_c_types[type(1.+1.j)] = 'std::complex<double> '
# !! hmmm. The following is likely unsafe...
num_to_c_types[type(1L)] = 'npy_longlong'
num_to_c_types[long] = 'npy_longlong'

#----------------------------------------------------------------------------
# Numeric array Python numeric --> C type maps
Expand Down Expand Up @@ -460,10 +462,10 @@ def type_match(self,value):

if __name__ == "__main__":
x = list_converter().type_spec("x",1)
print x.py_to_c_code()
print
print x.c_to_py_code()
print
print x.declaration_code(inline=1)
print
print x.cleanup_code()
print(x.py_to_c_code())
print()
print(x.c_to_py_code())
print()
print(x.declaration_code(inline=1))
print()
print(x.cleanup_code())
Loading

0 comments on commit aad4b5e

Please sign in to comment.