Skip to content

Commit 21577de

Browse files
realdougdan-zheng
authored andcommitted
---
yaml --- r: 312287 b: refs/heads/tensorflow-merge c: 3c27938 h: refs/heads/master i: 312285: cc5b595 312283: b79adfe 312279: c76e7bb 312271: d2e6ebe 312255: 0044509
1 parent 85f1da5 commit 21577de

File tree

7 files changed

+102
-90
lines changed

7 files changed

+102
-90
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ refs/heads/chase-my-tail: 8bb91443a9e81bbfac92a2621a0af887a1da8dbf
13791379
refs/heads/consider-outer-alternatives: 708bac749ec60a22a79e2eefbe734f9488a7370d
13801380
refs/heads/revert-25740-oops-i-linked-it-again: fdd41aeb682fc488572bdc1cf71b2ff6997ba576
13811381
refs/heads/swift-5.1-branch-06-12-2019: e63b7b2d3b93c48232d386099d0ec525d21d8f8d
1382-
refs/heads/tensorflow-merge: b30deb2d3d1720ac4e0e2041fa9f4c63d51fcb3b
1382+
refs/heads/tensorflow-merge: 3c27938dfdb9ea9803a5c372ee61f77020576ba9
13831383
refs/heads/update-checkout-sha-info: 5832743c5c2a842976c42a508a4c6dcceefb0aef
13841384
refs/tags/swift-5.1-DEVELOPMENT-SNAPSHOT-2019-06-12-a: 228f0448d9bb909aacbba4afcb7c600a405d15da
13851385
refs/tags/swift-5.1-DEVELOPMENT-SNAPSHOT-2019-06-14-a: 922861a77b5fc2bf46bc917da70ceb15eef76836

branches/tensorflow-merge/stdlib/public/Python/Python.swift

Lines changed: 82 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,20 @@ extension PythonObject : CustomReflectable {
147147
//===----------------------------------------------------------------------===//
148148

149149
public protocol PythonConvertible {
150+
/// A `PythonObject` instance representing this value.
151+
var pythonObject: PythonObject { get }
152+
}
153+
154+
//===----------------------------------------------------------------------===//
155+
// `PythonConvertible` protocol
156+
//===----------------------------------------------------------------------===//
157+
158+
public protocol ConvertibleFromPython {
150159
/// Creates a new instance from the given `PythonObject`, if possible.
151160
/// - Note: Conversion may fail if the given `PythonObject` instance is
152161
/// incompatible (e.g. a Python `string` object cannot be converted into an
153162
/// `Int`).
154163
init?(_ object: PythonObject)
155-
156-
/// A `PythonObject` instance representing this value.
157-
var pythonObject: PythonObject { get }
158164
}
159165

160166
public extension PythonObject {
@@ -177,7 +183,7 @@ fileprivate extension PythonConvertible {
177183
}
178184

179185
// `PythonObject` is trivially `PythonConvertible`.
180-
extension PythonObject : PythonConvertible {
186+
extension PythonObject : PythonConvertible, ConvertibleFromPython {
181187
public init(_ object: PythonObject) {
182188
self.init(consuming: object.ownedPyObject)
183189
}
@@ -747,7 +753,7 @@ private func isType(_ object: PythonObject,
747753
return pyObject != _Py_ZeroStruct
748754
}
749755

750-
extension Bool : PythonConvertible {
756+
extension Bool : PythonConvertible, ConvertibleFromPython {
751757
public init?(_ pythonObject: PythonObject) {
752758
guard isType(pythonObject, type: PyBool_Type) else { return nil }
753759

@@ -763,7 +769,7 @@ extension Bool : PythonConvertible {
763769
}
764770
}
765771

766-
extension String : PythonConvertible {
772+
extension String : PythonConvertible, ConvertibleFromPython {
767773
public init?(_ pythonObject: PythonObject) {
768774
let pyObject = pythonObject.ownedPyObject
769775
defer { Py_DecRef(pyObject) }
@@ -808,7 +814,7 @@ fileprivate extension PythonObject {
808814
}
809815
}
810816

811-
extension Int : PythonConvertible {
817+
extension Int : PythonConvertible, ConvertibleFromPython {
812818
public init?(_ pythonObject: PythonObject) {
813819
// `PyInt_AsLong` return -1 and sets an error if the Python object is not
814820
// integer compatible.
@@ -825,7 +831,7 @@ extension Int : PythonConvertible {
825831
}
826832
}
827833

828-
extension UInt : PythonConvertible {
834+
extension UInt : PythonConvertible, ConvertibleFromPython {
829835
public init?(_ pythonObject: PythonObject) {
830836
// `PyInt_AsUnsignedLongMask` isn't documented as such, but in fact it does
831837
// return -1 and set an error if the Python object is not integer
@@ -843,7 +849,7 @@ extension UInt : PythonConvertible {
843849
}
844850
}
845851

846-
extension Double : PythonConvertible {
852+
extension Double : PythonConvertible, ConvertibleFromPython {
847853
public init?(_ pythonObject: PythonObject) {
848854
// `PyFloat_AsDouble` return -1 and sets an error if the Python object is
849855
// not float compatible.
@@ -867,7 +873,7 @@ extension Double : PythonConvertible {
867873
// Any `FixedWidthInteger` type is `PythonConvertible` via the `Int`/`UInt`
868874
// implementation.
869875

870-
extension Int8 : PythonConvertible {
876+
extension Int8 : PythonConvertible, ConvertibleFromPython {
871877
public init?(_ pythonObject: PythonObject) {
872878
guard let i = Int(pythonObject) else { return nil }
873879
self.init(i)
@@ -878,7 +884,7 @@ extension Int8 : PythonConvertible {
878884
}
879885
}
880886

881-
extension Int16 : PythonConvertible {
887+
extension Int16 : PythonConvertible, ConvertibleFromPython {
882888
public init?(_ pythonObject: PythonObject) {
883889
guard let i = Int(pythonObject) else { return nil }
884890
self.init(i)
@@ -889,7 +895,7 @@ extension Int16 : PythonConvertible {
889895
}
890896
}
891897

892-
extension Int32 : PythonConvertible {
898+
extension Int32 : PythonConvertible, ConvertibleFromPython {
893899
public init?(_ pythonObject: PythonObject) {
894900
guard let i = Int(pythonObject) else { return nil }
895901
self.init(i)
@@ -900,7 +906,7 @@ extension Int32 : PythonConvertible {
900906
}
901907
}
902908

903-
extension Int64 : PythonConvertible {
909+
extension Int64 : PythonConvertible, ConvertibleFromPython {
904910
public init?(_ pythonObject: PythonObject) {
905911
guard let i = Int(pythonObject) else { return nil }
906912
self.init(i)
@@ -911,7 +917,7 @@ extension Int64 : PythonConvertible {
911917
}
912918
}
913919

914-
extension UInt8 : PythonConvertible {
920+
extension UInt8 : PythonConvertible, ConvertibleFromPython {
915921
public init?(_ pythonObject: PythonObject) {
916922
guard let i = UInt(pythonObject) else { return nil }
917923
self.init(i)
@@ -922,7 +928,7 @@ extension UInt8 : PythonConvertible {
922928
}
923929
}
924930

925-
extension UInt16 : PythonConvertible {
931+
extension UInt16 : PythonConvertible, ConvertibleFromPython {
926932
public init?(_ pythonObject: PythonObject) {
927933
guard let i = UInt(pythonObject) else { return nil }
928934
self.init(i)
@@ -933,7 +939,7 @@ extension UInt16 : PythonConvertible {
933939
}
934940
}
935941

936-
extension UInt32 : PythonConvertible {
942+
extension UInt32 : PythonConvertible, ConvertibleFromPython {
937943
public init?(_ pythonObject: PythonObject) {
938944
guard let i = UInt(pythonObject) else { return nil }
939945
self.init(i)
@@ -944,7 +950,7 @@ extension UInt32 : PythonConvertible {
944950
}
945951
}
946952

947-
extension UInt64 : PythonConvertible {
953+
extension UInt64 : PythonConvertible, ConvertibleFromPython {
948954
public init?(_ pythonObject: PythonObject) {
949955
guard let i = UInt(pythonObject) else { return nil }
950956
self.init(i)
@@ -957,7 +963,7 @@ extension UInt64 : PythonConvertible {
957963

958964
// `Float` is `PythonConvertible` via the `Double` implementation.
959965

960-
extension Float : PythonConvertible {
966+
extension Float : PythonConvertible, ConvertibleFromPython {
961967
public init?(_ pythonObject: PythonObject) {
962968
guard let v = Double(pythonObject) else { return nil }
963969
self.init(v)
@@ -973,6 +979,17 @@ extension Float : PythonConvertible {
973979
//===----------------------------------------------------------------------===//
974980

975981
extension Optional : PythonConvertible where Wrapped : PythonConvertible {
982+
public var pythonObject: PythonObject {
983+
return self?.pythonObject ?? Python.None
984+
}
985+
}
986+
987+
//===----------------------------------------------------------------------===//
988+
// `ConvertibleFromPython` conformance for `Optional`
989+
//===----------------------------------------------------------------------===//
990+
991+
extension Optional : ConvertibleFromPython
992+
where Wrapped : ConvertibleFromPython {
976993
public init?(_ object: PythonObject) {
977994
if object == Python.None {
978995
self = .none
@@ -983,27 +1000,16 @@ extension Optional : PythonConvertible where Wrapped : PythonConvertible {
9831000
self = .some(converted)
9841001
}
9851002
}
986-
987-
public var pythonObject: PythonObject {
988-
return self?.pythonObject ?? Python.None
989-
}
9901003
}
9911004

9921005
//===----------------------------------------------------------------------===//
993-
// `PythonConvertible` conformance for `Array` and `Dictionary`
1006+
// `PythonConvertible` and `ConvertibleFromPython conformance for
1007+
// `Array` and `Dictionary`
9941008
//===----------------------------------------------------------------------===//
9951009

9961010
// `Array` conditionally conforms to `PythonConvertible` if the `Element`
9971011
// associated type does.
9981012
extension Array : PythonConvertible where Element : PythonConvertible {
999-
public init?(_ pythonObject: PythonObject) {
1000-
self = []
1001-
for elementObject in pythonObject {
1002-
guard let element = Element(elementObject) else { return nil }
1003-
append(element)
1004-
}
1005-
}
1006-
10071013
public var pythonObject: PythonObject {
10081014
_ = Python // Ensure Python is initialized.
10091015
let list = PyList_New(count)!
@@ -1015,10 +1021,36 @@ extension Array : PythonConvertible where Element : PythonConvertible {
10151021
}
10161022
}
10171023

1024+
extension Array : ConvertibleFromPython where Element : ConvertibleFromPython {
1025+
public init?(_ pythonObject: PythonObject) {
1026+
self = []
1027+
for elementObject in pythonObject {
1028+
guard let element = Element(elementObject) else { return nil }
1029+
append(element)
1030+
}
1031+
}
1032+
}
1033+
10181034
// `Dictionary` conditionally conforms to `PythonConvertible` if the `Key` and
10191035
// `Value` associated types do.
10201036
extension Dictionary : PythonConvertible
10211037
where Key : PythonConvertible, Value : PythonConvertible {
1038+
public var pythonObject: PythonObject {
1039+
_ = Python // Ensure Python is initialized.
1040+
let dict = PyDict_New()!
1041+
for (key, value) in self {
1042+
let k = key.ownedPyObject
1043+
let v = value.ownedPyObject
1044+
PyDict_SetItem(dict, k, v)
1045+
Py_DecRef(k)
1046+
Py_DecRef(v)
1047+
}
1048+
return PythonObject(consuming: dict)
1049+
}
1050+
}
1051+
1052+
extension Dictionary : ConvertibleFromPython
1053+
where Key : ConvertibleFromPython, Value : ConvertibleFromPython {
10221054
public init?(_ pythonDict: PythonObject) {
10231055
self = [:]
10241056

@@ -1042,26 +1074,21 @@ extension Dictionary : PythonConvertible
10421074
}
10431075
}
10441076
}
1045-
1046-
public var pythonObject: PythonObject {
1047-
_ = Python // Ensure Python is initialized.
1048-
let dict = PyDict_New()!
1049-
for (key, value) in self {
1050-
let k = key.ownedPyObject
1051-
let v = value.ownedPyObject
1052-
PyDict_SetItem(dict, k, v)
1053-
Py_DecRef(k)
1054-
Py_DecRef(v)
1055-
}
1056-
return PythonObject(consuming: dict)
1057-
}
10581077
}
10591078

10601079
//===----------------------------------------------------------------------===//
1061-
// `PythonConvertible` conformance for `Range` types
1080+
// `PythonConvertible` and `ConvertibleFromPython` conformances
1081+
// for `Range` types
10621082
//===----------------------------------------------------------------------===//
10631083

10641084
extension Range : PythonConvertible where Bound : PythonConvertible {
1085+
public var pythonObject: PythonObject {
1086+
_ = Python // Ensure Python is initialized.
1087+
return Python.slice(lowerBound, upperBound, Python.None)
1088+
}
1089+
}
1090+
1091+
extension Range : ConvertibleFromPython where Bound : ConvertibleFromPython {
10651092
public init?(_ pythonObject: PythonObject) {
10661093
guard isType(pythonObject, type: PySlice_Type) else { return nil }
10671094
guard let lowerBound = Bound(pythonObject.start),
@@ -1071,14 +1098,17 @@ extension Range : PythonConvertible where Bound : PythonConvertible {
10711098
guard pythonObject.step == Python.None else { return nil }
10721099
self.init(uncheckedBounds: (lowerBound, upperBound))
10731100
}
1101+
}
10741102

1103+
extension PartialRangeFrom : PythonConvertible where Bound : PythonConvertible {
10751104
public var pythonObject: PythonObject {
10761105
_ = Python // Ensure Python is initialized.
1077-
return Python.slice(lowerBound, upperBound, Python.None)
1106+
return Python.slice(lowerBound, Python.None, Python.None)
10781107
}
10791108
}
10801109

1081-
extension PartialRangeFrom : PythonConvertible where Bound : PythonConvertible {
1110+
extension PartialRangeFrom : ConvertibleFromPython
1111+
where Bound : ConvertibleFromPython {
10821112
public init?(_ pythonObject: PythonObject) {
10831113
guard isType(pythonObject, type: PySlice_Type) else { return nil }
10841114
guard let lowerBound = Bound(pythonObject.start) else { return nil }
@@ -1088,14 +1118,17 @@ extension PartialRangeFrom : PythonConvertible where Bound : PythonConvertible {
10881118
}
10891119
self.init(lowerBound)
10901120
}
1121+
}
10911122

1123+
extension PartialRangeUpTo : PythonConvertible where Bound : PythonConvertible {
10921124
public var pythonObject: PythonObject {
10931125
_ = Python // Ensure Python is initialized.
1094-
return Python.slice(lowerBound, Python.None, Python.None)
1126+
return Python.slice(Python.None, upperBound, Python.None)
10951127
}
10961128
}
10971129

1098-
extension PartialRangeUpTo : PythonConvertible where Bound : PythonConvertible {
1130+
extension PartialRangeUpTo : ConvertibleFromPython
1131+
where Bound : ConvertibleFromPython {
10991132
public init?(_ pythonObject: PythonObject) {
11001133
guard isType(pythonObject, type: PySlice_Type) else { return nil }
11011134
guard let upperBound = Bound(pythonObject.stop) else { return nil }
@@ -1105,11 +1138,6 @@ extension PartialRangeUpTo : PythonConvertible where Bound : PythonConvertible {
11051138
}
11061139
self.init(upperBound)
11071140
}
1108-
1109-
public var pythonObject: PythonObject {
1110-
_ = Python // Ensure Python is initialized.
1111-
return Python.slice(Python.None, upperBound, Python.None)
1112-
}
11131141
}
11141142

11151143
//===----------------------------------------------------------------------===//

branches/tensorflow-merge/stdlib/public/TensorFlow/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ set(SOURCES
5050
Utilities.swift
5151
Threading.swift
5252
# NumPy bridging for `ShapedArray` and `Tensor`.
53-
NumpyConversion.swift)
53+
PythonConversion.swift)
5454

5555
# Copy TensorFlow bindings file, if it exists.
5656
if (TENSORFLOW_SWIFT_BINDINGS)
@@ -65,7 +65,7 @@ if (TENSORFLOW_SWIFT_APIS)
6565
list(APPEND SOURCES "${TENSORFLOW_SWIFT_API_SOURCES}")
6666
endif()
6767

68-
# When Python exists, NumpyConversion.swift imports it, so it must be
68+
# When Python exists, PythonConversion.swift imports it, so it must be
6969
# available at link time.
7070
set(TENSORFLOW_DEPENDS_PYTHON)
7171
if (SWIFT_PYTHON_EXISTS)

branches/tensorflow-merge/stdlib/public/TensorFlow/NumpyConversion.swift renamed to branches/tensorflow-merge/stdlib/public/TensorFlow/PythonConversion.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- NumpyConversion.swift ---------------------------------*- swift -*-===//
1+
//===-- PythonConversion.swift --------------------------------*- swift -*-===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -10,8 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
//
13-
// This file defines conversion initializers from `numpy.ndarray` to
14-
// `ShapedArray` and `Tensor`.
13+
// This file defines conversions between Python types & custom TensorFlow types.
1514
//
1615
//===----------------------------------------------------------------------===//
1716

@@ -164,4 +163,10 @@ extension Tensor where Scalar : NumpyScalarCompatible {
164163
public func makeNumpyArray() -> PythonObject { return array.makeNumpyArray() }
165164
}
166165

166+
extension TensorShape : PythonConvertible {
167+
public var pythonObject: PythonObject {
168+
return dimensions.pythonObject
169+
}
170+
}
171+
167172
#endif // canImport(Python)

0 commit comments

Comments
 (0)