@@ -28,6 +28,14 @@ def _supports_buffer_protocol(obj):
28
28
return False
29
29
return True
30
30
31
+ def _check_device (device ):
32
+ # _array_object imports in this file are inside the functions to avoid
33
+ # circular imports
34
+ from ._array_object import CPU_DEVICE
35
+
36
+ if device not in [CPU_DEVICE , None ]:
37
+ raise ValueError (f"Unsupported device { device !r} " )
38
+
31
39
def asarray (
32
40
obj : Union [
33
41
Array ,
@@ -48,16 +56,13 @@ def asarray(
48
56
49
57
See its docstring for more information.
50
58
"""
51
- # _array_object imports in this file are inside the functions to avoid
52
- # circular imports
53
- from ._array_object import Array , CPU_DEVICE
59
+ from ._array_object import Array
54
60
55
61
_check_valid_dtype (dtype )
56
62
_np_dtype = None
57
63
if dtype is not None :
58
64
_np_dtype = dtype ._np_dtype
59
- if device not in [CPU_DEVICE , None ]:
60
- raise ValueError (f"Unsupported device { device !r} " )
65
+ _check_device (device )
61
66
62
67
if np .__version__ [0 ] < '2' :
63
68
if copy is False :
@@ -106,11 +111,11 @@ def arange(
106
111
107
112
See its docstring for more information.
108
113
"""
109
- from ._array_object import Array , CPU_DEVICE
114
+ from ._array_object import Array
110
115
111
116
_check_valid_dtype (dtype )
112
- if device not in [ CPU_DEVICE , None ]:
113
- raise ValueError ( f"Unsupported device { device !r } " )
117
+ _check_device ( device )
118
+
114
119
if dtype is not None :
115
120
dtype = dtype ._np_dtype
116
121
return Array ._new (np .arange (start , stop = stop , step = step , dtype = dtype ))
@@ -127,11 +132,11 @@ def empty(
127
132
128
133
See its docstring for more information.
129
134
"""
130
- from ._array_object import Array , CPU_DEVICE
135
+ from ._array_object import Array
131
136
132
137
_check_valid_dtype (dtype )
133
- if device not in [ CPU_DEVICE , None ]:
134
- raise ValueError ( f"Unsupported device { device !r } " )
138
+ _check_device ( device )
139
+
135
140
if dtype is not None :
136
141
dtype = dtype ._np_dtype
137
142
return Array ._new (np .empty (shape , dtype = dtype ))
@@ -145,11 +150,11 @@ def empty_like(
145
150
146
151
See its docstring for more information.
147
152
"""
148
- from ._array_object import Array , CPU_DEVICE
153
+ from ._array_object import Array
149
154
150
155
_check_valid_dtype (dtype )
151
- if device not in [ CPU_DEVICE , None ]:
152
- raise ValueError ( f"Unsupported device { device !r } " )
156
+ _check_device ( device )
157
+
153
158
if dtype is not None :
154
159
dtype = dtype ._np_dtype
155
160
return Array ._new (np .empty_like (x ._array , dtype = dtype ))
@@ -197,11 +202,11 @@ def full(
197
202
198
203
See its docstring for more information.
199
204
"""
200
- from ._array_object import Array , CPU_DEVICE
205
+ from ._array_object import Array
201
206
202
207
_check_valid_dtype (dtype )
203
- if device not in [ CPU_DEVICE , None ]:
204
- raise ValueError ( f"Unsupported device { device !r } " )
208
+ _check_device ( device )
209
+
205
210
if isinstance (fill_value , Array ) and fill_value .ndim == 0 :
206
211
fill_value = fill_value ._array
207
212
if dtype is not None :
@@ -227,11 +232,11 @@ def full_like(
227
232
228
233
See its docstring for more information.
229
234
"""
230
- from ._array_object import Array , CPU_DEVICE
235
+ from ._array_object import Array
231
236
232
237
_check_valid_dtype (dtype )
233
- if device not in [ CPU_DEVICE , None ]:
234
- raise ValueError ( f"Unsupported device { device !r } " )
238
+ _check_device ( device )
239
+
235
240
if dtype is not None :
236
241
dtype = dtype ._np_dtype
237
242
res = np .full_like (x ._array , fill_value , dtype = dtype )
@@ -257,11 +262,11 @@ def linspace(
257
262
258
263
See its docstring for more information.
259
264
"""
260
- from ._array_object import Array , CPU_DEVICE
265
+ from ._array_object import Array
261
266
262
267
_check_valid_dtype (dtype )
263
- if device not in [ CPU_DEVICE , None ]:
264
- raise ValueError ( f"Unsupported device { device !r } " )
268
+ _check_device ( device )
269
+
265
270
if dtype is not None :
266
271
dtype = dtype ._np_dtype
267
272
return Array ._new (np .linspace (start , stop , num , dtype = dtype , endpoint = endpoint ))
@@ -298,11 +303,11 @@ def ones(
298
303
299
304
See its docstring for more information.
300
305
"""
301
- from ._array_object import Array , CPU_DEVICE
306
+ from ._array_object import Array
302
307
303
308
_check_valid_dtype (dtype )
304
- if device not in [ CPU_DEVICE , None ]:
305
- raise ValueError ( f"Unsupported device { device !r } " )
309
+ _check_device ( device )
310
+
306
311
if dtype is not None :
307
312
dtype = dtype ._np_dtype
308
313
return Array ._new (np .ones (shape , dtype = dtype ))
@@ -316,11 +321,11 @@ def ones_like(
316
321
317
322
See its docstring for more information.
318
323
"""
319
- from ._array_object import Array , CPU_DEVICE
324
+ from ._array_object import Array
320
325
321
326
_check_valid_dtype (dtype )
322
- if device not in [ CPU_DEVICE , None ]:
323
- raise ValueError ( f"Unsupported device { device !r } " )
327
+ _check_device ( device )
328
+
324
329
if dtype is not None :
325
330
dtype = dtype ._np_dtype
326
331
return Array ._new (np .ones_like (x ._array , dtype = dtype ))
@@ -365,11 +370,11 @@ def zeros(
365
370
366
371
See its docstring for more information.
367
372
"""
368
- from ._array_object import Array , CPU_DEVICE
373
+ from ._array_object import Array
369
374
370
375
_check_valid_dtype (dtype )
371
- if device not in [ CPU_DEVICE , None ]:
372
- raise ValueError ( f"Unsupported device { device !r } " )
376
+ _check_device ( device )
377
+
373
378
if dtype is not None :
374
379
dtype = dtype ._np_dtype
375
380
return Array ._new (np .zeros (shape , dtype = dtype ))
@@ -383,11 +388,11 @@ def zeros_like(
383
388
384
389
See its docstring for more information.
385
390
"""
386
- from ._array_object import Array , CPU_DEVICE
391
+ from ._array_object import Array
387
392
388
393
_check_valid_dtype (dtype )
389
- if device not in [ CPU_DEVICE , None ]:
390
- raise ValueError ( f"Unsupported device { device !r } " )
394
+ _check_device ( device )
395
+
391
396
if dtype is not None :
392
397
dtype = dtype ._np_dtype
393
398
return Array ._new (np .zeros_like (x ._array , dtype = dtype ))
0 commit comments