Skip to content

Commit

Permalink
Merge pull request numpy#3191 from charris/2to3-apply-imports-fixer
Browse files Browse the repository at this point in the history
2to3: Apply `imports` fixer.
  • Loading branch information
charris committed Apr 6, 2013
2 parents 3c8fc14 + 4394515 commit 49a8902
Show file tree
Hide file tree
Showing 44 changed files with 514 additions and 452 deletions.
8 changes: 6 additions & 2 deletions doc/cdoc/numpyfilter.py
Expand Up @@ -13,7 +13,11 @@
import os
import textwrap
import optparse
import cPickle as pickle

if sys.version_info[0] >= 3:
import pickle
else:
import cPickle as pickle

CACHE_FILE = 'build/rst-cache.pck'

Expand Down Expand Up @@ -43,7 +47,7 @@ def filter_comment(text):
if text.startswith('UFUNC_API'):
text = text[9:].strip()

html = render_html(text)
html = render_html(text)
return html

def process_match(m, cache=None):
Expand Down
6 changes: 3 additions & 3 deletions doc/numpybook/runcode.py
Expand Up @@ -18,7 +18,7 @@

import sys
import optparse
import cStringIO
import io
import re
import os

Expand All @@ -27,7 +27,7 @@
def getoutput(tstr, dic):
print "\n\nRunning..."
print tstr,
tempstr = cStringIO.StringIO()
tempstr = io.StringIO()
sys.stdout = tempstr
code = compile(tstr, '<input>', 'exec')
try:
Expand Down Expand Up @@ -82,7 +82,7 @@ def getnewcodestr(substr, dic):

def runpycode(lyxstr, name='MyCode'):
schobj = re.compile(r"\\layout %s\s+>>> " % name)
outstr = cStringIO.StringIO()
outstr = io.StringIO()
num = 0
indx = []
for it in schobj.finditer(lyxstr):
Expand Down
2 changes: 1 addition & 1 deletion doc/sphinxext/numpydoc/comment_eater.py
Expand Up @@ -4,7 +4,7 @@
if sys.version_info[0] >= 3:
from io import StringIO
else:
from cStringIO import StringIO
from io import StringIO

import compiler
import inspect
Expand Down
2 changes: 1 addition & 1 deletion doc/sphinxext/numpydoc/compiler_unparse.py
Expand Up @@ -18,7 +18,7 @@
if sys.version_info[0] >= 3:
from io import StringIO
else:
from cStringIO import StringIO
from io import StringIO

def unparse(ast, single_line_functions=False):
s = StringIO()
Expand Down
2 changes: 1 addition & 1 deletion doc/sphinxext/numpydoc/docscrape.py
Expand Up @@ -14,7 +14,7 @@
if sys.version_info[0] >= 3:
from io import StringIO
else:
from cStringIO import StringIO
from io import StringIO

class Reader(object):
"""A line-based string reader.
Expand Down
2 changes: 1 addition & 1 deletion doc/sphinxext/numpydoc/plot_directive.py
Expand Up @@ -82,7 +82,7 @@
if sys.version_info[0] >= 3:
from io import StringIO
else:
from cStringIO import StringIO
from io import StringIO

import warnings
warnings.warn("A plot_directive module is also available under "
Expand Down
9 changes: 7 additions & 2 deletions numpy/__init__.py
Expand Up @@ -106,6 +106,8 @@
"""
from __future__ import division, absolute_import

import sys

# We first need to detect if we're being called as part of the numpy setup
# procedure itself in a reliable manner.
try:
Expand Down Expand Up @@ -160,8 +162,11 @@ def pkgload(*packages, **options):

# Make these accessible from numpy name-space
# but not imported in from numpy import *
from __builtin__ import bool, int, long, float, complex, \
object, unicode, str
if sys.version_info[0] >= 3:
from builtins import bool, int, long, float, complex, object, unicode, str
else:
from __builtin__ import bool, int, long, float, complex, object, unicode, str

from .core import round, abs, max, min

__all__.extend(['__version__', 'pkgload', 'PackageLoader',
Expand Down
6 changes: 3 additions & 3 deletions numpy/core/__init__.py
Expand Up @@ -62,10 +62,10 @@ def _ufunc_reduce(func):


import sys
if sys.version_info[0] < 3:
import copy_reg as copyreg
else:
if sys.version_info[0] >= 3:
import copyreg
else:
import copy_reg as copyreg

copyreg.pickle(ufunc, _ufunc_reduce, _ufunc_reconstruct)
# Unclutter namespace (must keep _ufunc_reconstruct for unpickling)
Expand Down
35 changes: 20 additions & 15 deletions numpy/core/numeric.py
@@ -1,5 +1,22 @@
from __future__ import division, absolute_import

import sys
import warnings
from . import multiarray
from . import umath
from .umath import *
from . import numerictypes
from .numerictypes import *
import collections

if sys.version_info[0] >= 3:
import pickle
else:
import cPickle as pickle

loads = pickle.loads


__all__ = ['newaxis', 'ndarray', 'flatiter', 'nditer', 'nested_iters', 'ufunc',
'arange', 'array', 'zeros', 'count_nonzero',
'empty', 'broadcast', 'dtype', 'fromstring', 'fromfile',
Expand All @@ -24,19 +41,10 @@
'CLIP', 'RAISE', 'WRAP', 'MAXDIMS', 'BUFSIZE', 'ALLOW_THREADS',
'ComplexWarning']

import sys
import warnings
from . import multiarray
from . import umath
from .umath import *
from . import numerictypes
from .numerictypes import *
import collections


if sys.version_info[0] < 3:
__all__.extend(['getbuffer', 'newbuffer'])


class ComplexWarning(RuntimeWarning):
"""
The warning raised when casting a complex dtype to a real dtype.
Expand Down Expand Up @@ -1861,9 +1869,6 @@ def base_repr(number, base=2, padding=0):
res.append('-')
return ''.join(reversed(res or '0'))

from cPickle import load, loads
_cload = load
_file = open

def load(file):
"""
Expand All @@ -1880,8 +1885,8 @@ def load(file):
"""
if isinstance(file, type("")):
file = _file(file,"rb")
return _cload(file)
file = open(file, "rb")
return pickle.load(file)

# These are all essentially abbreviations
# These might wind up in a special abbreviations module
Expand Down
6 changes: 5 additions & 1 deletion numpy/core/numerictypes.py
Expand Up @@ -98,7 +98,11 @@

# we don't export these for import *, but we do want them accessible
# as numerictypes.bool, etc.
from __builtin__ import bool, int, long, float, complex, object, unicode, str
if sys.version_info[0] >= 3:
from builtins import bool, int, long, float, complex, object, unicode, str
else:
from __builtin__ import bool, int, long, float, complex, object, unicode, str

from numpy.compat import bytes

if sys.version_info[0] >= 3:
Expand Down
4 changes: 2 additions & 2 deletions numpy/core/records.py
Expand Up @@ -595,8 +595,8 @@ def fromrecords(recList, dtype=None, shape=None, formats=None, names=None,
>>> r.col2
chararray(['dbe', 'de'],
dtype='|S3')
>>> import cPickle
>>> print cPickle.loads(cPickle.dumps(r))
>>> import pickle
>>> print pickle.loads(pickle.dumps(r))
[(456, 'dbe', 1.2) (2, 'de', 1.3)]
"""

Expand Down
26 changes: 13 additions & 13 deletions numpy/core/setup.py
Expand Up @@ -4,12 +4,14 @@
import os
import sys
import shutil
import pickle
import copy
import warnings
import re
from os.path import join
from numpy.distutils import log
from distutils.dep_util import newer
from distutils.sysconfig import get_config_var
import warnings
import re

from setup_common import *

Expand All @@ -25,11 +27,9 @@
# configuration informations between extensions is not easy.
# Using a pickled-based memoize does not work because config_cmd is an instance
# method, which cPickle does not like.
try:
import cPickle as _pik
except ImportError:
import pickle as _pik
import copy
#
# Use pickle in all cases, as cPickle is gone in python3 and the difference
# in time is only in build. -- Charles Harris, 2013-03-30

class CallOnceOnly(object):
def __init__(self):
Expand All @@ -40,25 +40,25 @@ def __init__(self):
def check_types(self, *a, **kw):
if self._check_types is None:
out = check_types(*a, **kw)
self._check_types = _pik.dumps(out)
self._check_types = pickle.dumps(out)
else:
out = copy.deepcopy(_pik.loads(self._check_types))
out = copy.deepcopy(pickle.loads(self._check_types))
return out

def check_ieee_macros(self, *a, **kw):
if self._check_ieee_macros is None:
out = check_ieee_macros(*a, **kw)
self._check_ieee_macros = _pik.dumps(out)
self._check_ieee_macros = pickle.dumps(out)
else:
out = copy.deepcopy(_pik.loads(self._check_ieee_macros))
out = copy.deepcopy(pickle.loads(self._check_ieee_macros))
return out

def check_complex(self, *a, **kw):
if self._check_complex is None:
out = check_complex(*a, **kw)
self._check_complex = _pik.dumps(out)
self._check_complex = pickle.dumps(out)
else:
out = copy.deepcopy(_pik.loads(self._check_complex))
out = copy.deepcopy(pickle.loads(self._check_complex))
return out

PYTHON_HAS_UNICODE_WIDE = True
Expand Down
6 changes: 5 additions & 1 deletion numpy/core/tests/test_print.py
Expand Up @@ -6,7 +6,11 @@

import locale
import sys
from StringIO import StringIO

if sys.version_info[0] >= 3:
from io import StringIO
else:
from StringIO import StringIO

_REF = {np.inf: 'inf', -np.inf: '-inf', np.nan: 'nan'}

Expand Down
17 changes: 6 additions & 11 deletions numpy/core/tests/test_regression.py
Expand Up @@ -7,8 +7,9 @@
import copy
import warnings
import tempfile
from StringIO import StringIO
from os import path
from io import BytesIO

import numpy as np
from numpy.testing import (
run_module_suite, TestCase, assert_, assert_equal,
Expand All @@ -18,10 +19,6 @@
from numpy.testing.utils import _assert_valid_refcount, WarningManager
from numpy.compat import asbytes, asunicode, asbytes_nested

if sys.version_info[0] >= 3:
import io
StringIO = io.BytesIO

rlevel = 1

class TestRegression(TestCase):
Expand All @@ -37,7 +34,7 @@ def test_mem_empty(self,level=rlevel):
def test_pickle_transposed(self,level=rlevel):
"""Ticket #16"""
a = np.transpose(np.array([[2,9],[7,0],[3,8]]))
f = StringIO()
f = BytesIO()
pickle.dump(a,f)
f.seek(0)
b = pickle.load(f)
Expand Down Expand Up @@ -90,7 +87,7 @@ def test_negative_nd_indexing(self,level=rlevel):

def test_char_dump(self,level=rlevel):
"""Ticket #50"""
f = StringIO()
f = BytesIO()
ca = np.char.array(np.arange(1000,1010),itemsize=4)
ca.dump(f)
f.seek(0)
Expand Down Expand Up @@ -322,7 +319,7 @@ def assign(a, b, c):
def test_unpickle_dtype_with_object(self,level=rlevel):
"""Implemented in r2840"""
dt = np.dtype([('x',int),('y',np.object_),('z','O')])
f = StringIO()
f = BytesIO()
pickle.dump(dt,f)
f.seek(0)
dt_ = pickle.load(f)
Expand Down Expand Up @@ -386,7 +383,6 @@ def test_lexsort(self,level=rlevel):

def test_pickle_dtype(self,level=rlevel):
"""Ticket #251"""
import pickle
pickle.dumps(np.float)

def test_swap_real(self, level=rlevel):
Expand Down Expand Up @@ -725,10 +721,9 @@ def rs():

def test_unicode_scalar(self, level=rlevel):
"""Ticket #600"""
import cPickle
x = np.array(["DROND", "DROND1"], dtype="U6")
el = x[1]
new = cPickle.loads(cPickle.dumps(el))
new = pickle.loads(pickle.dumps(el))
assert_equal(new, el)

def test_arange_non_native_dtype(self, level=rlevel):
Expand Down

0 comments on commit 49a8902

Please sign in to comment.