@@ -34,17 +34,42 @@ def ensure_text(l, encoding='utf-8'):
34
34
35
35
36
36
def ensure_ndarray (buf , dtype = None ):
37
- """TODO"""
37
+ """Convenience function to coerce `buf` to a numpy array, if it is not already a
38
+ numpy array.
38
39
39
- # make that we create an array from a memory buffer with no copy
40
+ Parameters
41
+ ----------
42
+ buf : array-like or bytes-like
43
+ A numpy array or any object exporting a buffer interface.
44
+ dtype : dtype, optional
45
+ Request that the data be viewed as the given dtype.
40
46
41
- if isinstance (buf , np .ndarray ):
47
+ Returns
48
+ -------
49
+ arr : ndarray
50
+ A numpy array, sharing memory with `buf`.
51
+
52
+ Notes
53
+ -----
54
+ This function will not create a copy under any circumstances, it is guaranteed to
55
+ return a view on memory exported by `buf`.
42
56
57
+ """
58
+
59
+ if isinstance (buf , np .ndarray ):
43
60
# already a numpy array
44
61
arr = buf
45
62
63
+ elif isinstance (buf , array .array ) and buf .typecode == 'u' :
64
+ # guard condition, do not support array.array with unicode type, this is
65
+ # problematic because numpy does not support it on all platforms
66
+ raise TypeError ('array.array with unicode type is not supported' )
67
+
46
68
else :
47
69
70
+ # N.B., first take a memoryview to make sure that we subsequently create a
71
+ # numpy array from a memory buffer with no copy
72
+
48
73
if PY2 : # pragma: py3 no cover
49
74
try :
50
75
mem = memoryview (buf )
@@ -59,30 +84,45 @@ def ensure_ndarray(buf, dtype=None):
59
84
arr = np .array (mem , copy = False )
60
85
61
86
if PY2 and isinstance (buf , array .array ): # pragma: py3 no cover
62
-
63
87
# type information will not have been propagated via the old-style buffer
64
- # interface, so we have to manually hack it after the fact
65
- if buf .typecode == 'u' :
66
- t = 'U1'
67
- else :
68
- t = buf .typecode
69
- arr = arr .view (t )
88
+ # interface, so we have to manually hack it back in after the fact
89
+ arr = arr .view (buf .typecode )
70
90
71
91
if dtype is not None :
72
-
73
92
# view as requested dtype
74
93
arr = arr .view (dtype )
75
94
76
95
return arr
77
96
78
97
79
98
def ensure_contiguous_ndarray (buf , dtype = None ):
80
- """TODO"""
99
+ """Convenience function to coerce `buf` to a numpy array, if it is not already a
100
+ numpy array. Also ensures that the returned value exports fully contiguous memory,
101
+ and supports the new-style buffer interface.
102
+
103
+ Parameters
104
+ ----------
105
+ buf : array-like or bytes-like
106
+ A numpy array or any object exporting a buffer interface.
107
+ dtype : dtype, optional
108
+ Request that the data be viewed as the given dtype.
109
+
110
+ Returns
111
+ -------
112
+ arr : ndarray
113
+ A numpy array, sharing memory with `buf`.
114
+
115
+ Notes
116
+ -----
117
+ This function will not create a copy under any circumstances, it is guaranteed to
118
+ return a view on memory exported by `buf`.
119
+
120
+ """
81
121
82
122
# ensure input is a numpy array
83
123
arr = ensure_ndarray (buf , dtype = dtype )
84
124
85
- # check for datetime or timedelta ndarray, cannot take a memoryview of those
125
+ # check for datetime or timedelta ndarray, the buffer interface doesn't support those
86
126
if isinstance (buf , np .ndarray ) and buf .dtype .kind in 'Mm' :
87
127
arr = arr .view (np .int64 )
88
128
@@ -93,7 +133,6 @@ def ensure_contiguous_ndarray(buf, dtype=None):
93
133
94
134
# check memory is contiguous, if so flatten
95
135
if arr .flags .c_contiguous or arr .flags .f_contiguous :
96
-
97
136
# can flatten without copy
98
137
arr = arr .reshape (- 1 , order = 'A' )
99
138
@@ -103,18 +142,18 @@ def ensure_contiguous_ndarray(buf, dtype=None):
103
142
return arr
104
143
105
144
106
- def ensure_bytes (o ):
107
- """Obtain a bytes object from memory exposed by `o `."""
145
+ def ensure_bytes (buf ):
146
+ """Obtain a bytes object from memory exposed by `buf `."""
108
147
109
- if not isinstance (o , binary_type ):
148
+ if not isinstance (buf , binary_type ):
110
149
111
150
# go via numpy, for convenience
112
- a = ensure_contiguous_ndarray ( o )
151
+ arr = ensure_ndarray ( buf )
113
152
114
153
# create bytes
115
- o = a .tobytes ()
154
+ buf = arr .tobytes (order = 'A' )
116
155
117
- return o
156
+ return buf
118
157
119
158
120
159
def ndarray_copy (src , dst ):
0 commit comments