diff --git a/stdlib/public/Python/NumpyConversion.swift b/stdlib/public/Python/NumpyConversion.swift index 009c2012e7edc..bbfc44280c7a2 100644 --- a/stdlib/public/Python/NumpyConversion.swift +++ b/stdlib/public/Python/NumpyConversion.swift @@ -24,7 +24,7 @@ private let ctypes = Python.import("ctypes") /// A type that can be initialized from a `numpy.ndarray` instance represented /// as a `PythonObject`. public protocol ConvertibleFromNumpyArray { - init?(numpyArray: PythonObject) + init?(numpy: PythonObject) } /// A type that is bitwise compatible with one or more NumPy scalar types. @@ -93,7 +93,7 @@ extension Double : NumpyScalarCompatible { extension Array : ConvertibleFromNumpyArray where Element : NumpyScalarCompatible { - public init?(numpyArray: PythonObject) { + public init?(numpy numpyArray: PythonObject) { // Check if input is a `numpy.ndarray` instance. guard Python.isinstance(numpyArray, np.ndarray) == true else { return nil diff --git a/stdlib/public/Python/Python.swift b/stdlib/public/Python/Python.swift index 98fbf0ebe7b9e..897fe95ff67f4 100644 --- a/stdlib/public/Python/Python.swift +++ b/stdlib/public/Python/Python.swift @@ -684,6 +684,18 @@ public struct PythonInterface { public subscript(dynamicMember name: String) -> PythonObject { return builtins[name] } + + // The Python runtime version. + // Equivalent to `sys.version` in Python. + public var version: PythonObject { + return self.import("sys").version + } + + // The Python runtime version information. + // Equivalent to `sys.version_info` in Python. + public var versionInfo: PythonObject { + return self.import("sys").version_info + } } //===----------------------------------------------------------------------===// diff --git a/stdlib/public/TensorFlow/NumpyConversion.swift b/stdlib/public/TensorFlow/NumpyConversion.swift index dbc19d5e06ad3..eed4228713ffc 100644 --- a/stdlib/public/TensorFlow/NumpyConversion.swift +++ b/stdlib/public/TensorFlow/NumpyConversion.swift @@ -25,7 +25,7 @@ private let np = Python.import("numpy") extension ShapedArray : ConvertibleFromNumpyArray where Scalar : NumpyScalarCompatible { - public init?(numpyArray: PythonObject) { + public init?(numpy numpyArray: PythonObject) { // Check if input is a `numpy.ndarray` instance. guard Python.isinstance(numpyArray, np.ndarray) == true else { return nil @@ -73,7 +73,7 @@ extension ShapedArray : ConvertibleFromNumpyArray extension Tensor : ConvertibleFromNumpyArray where Scalar : NumpyScalarCompatible { - public init?(numpyArray: PythonObject) { + public init?(numpy numpyArray: PythonObject) { // Check if input is a `numpy.ndarray` instance. guard Python.isinstance(numpyArray, np.ndarray) == true else { return nil diff --git a/test/Python/numpy_conversion.swift b/test/Python/numpy_conversion.swift index a1a2fd98b0be8..266f39aab00cc 100644 --- a/test/Python/numpy_conversion.swift +++ b/test/Python/numpy_conversion.swift @@ -13,34 +13,34 @@ NumpyConversionTests.test("array-conversion") { guard let np = numpyModule else { return } let numpyArrayEmpty = np.array([] as [Float], dtype: np.float32) - if let array = expectNotNil(Array(numpyArray: numpyArrayEmpty)) { + if let array = expectNotNil(Array(numpy: numpyArrayEmpty)) { expectEqual([], array) } let numpyArrayBool = np.array([true, false, false, true]) - if let array = expectNotNil(Array(numpyArray: numpyArrayBool)) { + if let array = expectNotNil(Array(numpy: numpyArrayBool)) { expectEqual([true, false, false, true], array) } let numpyArrayFloat = np.ones([6], dtype: np.float32) - if let array = expectNotNil(Array(numpyArray: numpyArrayFloat)) { + if let array = expectNotNil(Array(numpy: numpyArrayFloat)) { expectEqual(Array(repeating: 1, count: 6), array) } let numpyArrayInt32 = np.array([-1, 4, 25, 2018], dtype: np.int32) - if let array = expectNotNil(Array(numpyArray: numpyArrayInt32)) { + if let array = expectNotNil(Array(numpy: numpyArrayInt32)) { expectEqual([-1, 4, 25, 2018], array) } let numpyArray2D = np.ones([2, 3]) - expectNil(Array(numpyArray: numpyArray2D)) + expectNil(Array(numpy: numpyArray2D)) let numpyArrayStrided = np.array([[1, 2], [1, 2]], dtype: np.int32)[ Python.slice(Python.None), 1] // Assert that the array has a stride, so that we're certainly testing a // strided array. expectNotEqual(numpyArrayStrided.__array_interface__["strides"], Python.None) - if let array = expectNotNil(Array(numpyArray: numpyArrayStrided)) { + if let array = expectNotNil(Array(numpy: numpyArrayStrided)) { expectEqual([2, 2], array) } } diff --git a/test/Python/python_runtime.swift b/test/Python/python_runtime.swift index 26073df7d59fb..228ce01f64aaa 100644 --- a/test/Python/python_runtime.swift +++ b/test/Python/python_runtime.swift @@ -10,9 +10,10 @@ var PythonRuntimeTestSuite = TestSuite("PythonRuntime") PythonLibrary.useVersion(2, 7) PythonRuntimeTestSuite.test("CheckVersion") { - let sysModule = Python.import("sys") - let version = String(sysModule.version)! - expectEqual("2.7.", version.prefix(4)) + expectEqual("2.7.", String(Python.version)!.prefix(4)) + let versionInfo = Python.versionInfo + expectEqual(2, versionInfo.major) + expectEqual(7, versionInfo.minor) } PythonRuntimeTestSuite.test("PythonList") { diff --git a/test/TensorFlowRuntime/numpy_conversion.swift b/test/TensorFlowRuntime/numpy_conversion.swift index 9337d03fd3912..9f3010bc7ceae 100644 --- a/test/TensorFlowRuntime/numpy_conversion.swift +++ b/test/TensorFlowRuntime/numpy_conversion.swift @@ -24,30 +24,30 @@ NumpyConversionTests.test("shaped-array-conversion") { guard let np = numpyModule else { return } let numpyArrayEmpty = np.array([[]] as [[Float]], dtype: np.float32) - if let array = expectNotNil(ShapedArray(numpyArray: numpyArrayEmpty)) { + if let array = expectNotNil(ShapedArray(numpy: numpyArrayEmpty)) { expectEqual(ShapedArray(shape: [1, 0], scalars: []), array) } let numpyArrayBool = np.array([[true, false], [false, true]]) - expectNil(ShapedArray(numpyArray: numpyArrayBool)) - expectNil(ShapedArray(numpyArray: numpyArrayBool)) - if let array = expectNotNil(ShapedArray(numpyArray: numpyArrayBool)) { + expectNil(ShapedArray(numpy: numpyArrayBool)) + expectNil(ShapedArray(numpy: numpyArrayBool)) + if let array = expectNotNil(ShapedArray(numpy: numpyArrayBool)) { expectEqual(ShapedArray(shape: [2, 2], scalars: [true, false, false, true]), array) } let numpyArrayFloat = np.ones([2, 3], dtype: np.float32) - expectNil(ShapedArray(numpyArray: numpyArrayFloat)) - expectNil(ShapedArray(numpyArray: numpyArrayFloat)) - if let array = expectNotNil(ShapedArray(numpyArray: numpyArrayFloat)) { + expectNil(ShapedArray(numpy: numpyArrayFloat)) + expectNil(ShapedArray(numpy: numpyArrayFloat)) + if let array = expectNotNil(ShapedArray(numpy: numpyArrayFloat)) { expectEqual(ShapedArray(shape: [2, 3], repeating: 1), array) } let numpyArrayInt32 = np.array([[[1, 2, 3], [4, 5, 6]]], dtype: np.int32) - expectNil(ShapedArray(numpyArray: numpyArrayInt32)) - expectNil(ShapedArray(numpyArray: numpyArrayInt32)) - if let array = expectNotNil(ShapedArray(numpyArray: numpyArrayInt32)) { + expectNil(ShapedArray(numpy: numpyArrayInt32)) + expectNil(ShapedArray(numpy: numpyArrayInt32)) + if let array = expectNotNil(ShapedArray(numpy: numpyArrayInt32)) { expectEqual(ShapedArray(shape: [1, 2, 3], scalars: [1, 2, 3, 4, 5, 6]), array) } @@ -58,7 +58,7 @@ NumpyConversionTests.test("shaped-array-conversion") { // strided array. expectNotEqual(numpyArrayStrided.__array_interface__["strides"], Python.None) if let array = expectNotNil( - ShapedArray(numpyArray: numpyArrayStrided)) { + ShapedArray(numpy: numpyArrayStrided)) { expectEqual(ShapedArray(shape: [2], scalars: [2, 2]), array) } } @@ -67,30 +67,30 @@ NumpyConversionTests.test("tensor-conversion") { guard let np = numpyModule else { return } let numpyArrayEmpty = np.array([[]] as [[Float]], dtype: np.float32) - if let tensor = expectNotNil(Tensor(numpyArray: numpyArrayEmpty)) { + if let tensor = expectNotNil(Tensor(numpy: numpyArrayEmpty)) { expectEqual(ShapedArray(shape: [1, 0], scalars: []), tensor.array) } let numpyArrayBool = np.array([[true, false], [false, true]]) - expectNil(Tensor(numpyArray: numpyArrayBool)) - expectNil(Tensor(numpyArray: numpyArrayBool)) - if let tensor = expectNotNil(Tensor(numpyArray: numpyArrayBool)) { + expectNil(Tensor(numpy: numpyArrayBool)) + expectNil(Tensor(numpy: numpyArrayBool)) + if let tensor = expectNotNil(Tensor(numpy: numpyArrayBool)) { expectEqual(ShapedArray(shape: [2, 2], scalars: [true, false, false, true]), tensor.array) } let numpyArrayFloat = np.ones([2, 3], dtype: np.float32) - expectNil(Tensor(numpyArray: numpyArrayFloat)) - expectNil(Tensor(numpyArray: numpyArrayFloat)) - if let tensor = expectNotNil(Tensor(numpyArray: numpyArrayFloat)) { + expectNil(Tensor(numpy: numpyArrayFloat)) + expectNil(Tensor(numpy: numpyArrayFloat)) + if let tensor = expectNotNil(Tensor(numpy: numpyArrayFloat)) { expectEqual(ShapedArray(shape: [2, 3], repeating: 1), tensor.array) } let numpyArrayInt32 = np.array([[[1, 2, 3], [4, 5, 6]]], dtype: np.int32) - expectNil(Tensor(numpyArray: numpyArrayInt32)) - expectNil(Tensor(numpyArray: numpyArrayInt32)) - if let tensor = expectNotNil(Tensor(numpyArray: numpyArrayInt32)) { + expectNil(Tensor(numpy: numpyArrayInt32)) + expectNil(Tensor(numpy: numpyArrayInt32)) + if let tensor = expectNotNil(Tensor(numpy: numpyArrayInt32)) { expectEqual(ShapedArray(shape: [1, 2, 3], scalars: [1, 2, 3, 4, 5, 6]), tensor.array) } @@ -100,7 +100,7 @@ NumpyConversionTests.test("tensor-conversion") { // Assert that the array has a stride, so that we're certainly testing a // strided array. expectNotEqual(numpyArrayStrided.__array_interface__["strides"], Python.None) - if let tensor = expectNotNil(Tensor(numpyArray: numpyArrayStrided)) { + if let tensor = expectNotNil(Tensor(numpy: numpyArrayStrided)) { expectEqual(ShapedArray(shape: [2], scalars: [2, 2]), tensor.array) } } @@ -110,13 +110,13 @@ NumpyConversionTests.test("tensor-round-trip") { guard ctypesModule != nil else { return } let t1 = Tensor(shape: [1,2,3,4], repeating: 3.0) - expectEqual(t1, Tensor(numpyArray: t1.makeNumpyArray())!) + expectEqual(t1, Tensor(numpy: t1.makeNumpyArray())!) let t2 = Tensor(shape: [2,3], scalars: [1, 2, 3, 4, 5, 6]) - expectEqual(t2, Tensor(numpyArray: t2.makeNumpyArray())!) + expectEqual(t2, Tensor(numpy: t2.makeNumpyArray())!) let t3 = Tensor(shape: [8,5,4], repeating: 30) - expectEqual(t3, Tensor(numpyArray: t3.makeNumpyArray())!) + expectEqual(t3, Tensor(numpy: t3.makeNumpyArray())!) } #endif