|
1 | | -""" |
2 | | -Python 3 compatibility tools. |
| 1 | +import warnings |
3 | 2 |
|
4 | | -Copied from numpy/compat/py3k. |
| 3 | +warnings.warn("We no longer carry a copy of the 'py3k' module in nibabel; " |
| 4 | + "Please import from the 'numpy.compat.py3k' module directly. " |
| 5 | + "Full removal scheduled for nibabel 4.0.", |
| 6 | + FutureWarning, |
| 7 | + stacklevel=2) |
5 | 8 |
|
6 | | -Please prefer the routines in the six module when possible. |
7 | | -
|
8 | | -BSD license |
9 | | -""" |
10 | | - |
11 | | -__all__ = ['bytes', 'asbytes', 'isfileobj', 'getexception', 'strchar', |
12 | | - 'unicode', 'asunicode', 'asbytes_nested', 'asunicode_nested', |
13 | | - 'asstr', 'open_latin1', 'StringIO', 'BytesIO'] |
14 | | - |
15 | | -import sys |
16 | | - |
17 | | -if sys.version_info[0] >= 3: |
18 | | - import io |
19 | | - StringIO = io.StringIO |
20 | | - BytesIO = io.BytesIO |
21 | | - bytes = bytes |
22 | | - unicode = str |
23 | | - asunicode = str |
24 | | - |
25 | | - def asbytes(s): |
26 | | - if isinstance(s, bytes): |
27 | | - return s |
28 | | - return s.encode('latin1') |
29 | | - |
30 | | - def asstr(s): |
31 | | - if isinstance(s, str): |
32 | | - return s |
33 | | - return s.decode('latin1') |
34 | | - |
35 | | - def isfileobj(f): |
36 | | - return isinstance(f, io.FileIO) |
37 | | - |
38 | | - def open_latin1(filename, mode='r'): |
39 | | - return open(filename, mode=mode, encoding='iso-8859-1') |
40 | | - strchar = 'U' |
41 | | - ints2bytes = lambda seq: bytes(seq) |
42 | | - ZEROB = bytes([0]) |
43 | | - FileNotFoundError = FileNotFoundError |
44 | | - import builtins |
45 | | -else: |
46 | | - import StringIO |
47 | | - StringIO = BytesIO = StringIO.StringIO |
48 | | - bytes = str |
49 | | - unicode = unicode |
50 | | - asbytes = str |
51 | | - asstr = str |
52 | | - strchar = 'S' |
53 | | - |
54 | | - def isfileobj(f): |
55 | | - return isinstance(f, file) |
56 | | - |
57 | | - def asunicode(s): |
58 | | - if isinstance(s, unicode): |
59 | | - return s |
60 | | - return s.decode('ascii') |
61 | | - |
62 | | - def open_latin1(filename, mode='r'): |
63 | | - return open(filename, mode=mode) |
64 | | - ints2bytes = lambda seq: ''.join(chr(i) for i in seq) |
65 | | - ZEROB = chr(0) |
66 | | - |
67 | | - class FileNotFoundError(IOError): |
68 | | - pass |
69 | | - |
70 | | - import __builtin__ as builtins # noqa |
71 | | - |
72 | | - |
73 | | -def getexception(): |
74 | | - return sys.exc_info()[1] |
75 | | - |
76 | | - |
77 | | -def asbytes_nested(x): |
78 | | - if hasattr(x, '__iter__') and not isinstance(x, (bytes, unicode)): |
79 | | - return [asbytes_nested(y) for y in x] |
80 | | - else: |
81 | | - return asbytes(x) |
82 | | - |
83 | | - |
84 | | -def asunicode_nested(x): |
85 | | - if hasattr(x, '__iter__') and not isinstance(x, (bytes, unicode)): |
86 | | - return [asunicode_nested(y) for y in x] |
87 | | - else: |
88 | | - return asunicode(x) |
| 9 | +from numpy.compat.py3k import * # noqa |
0 commit comments