@@ -1193,6 +1193,36 @@ public extension PythonObject {
1193
1193
}
1194
1194
}
1195
1195
1196
+ public extension PythonObject {
1197
+ static func & ( lhs: PythonObject , rhs: PythonObject ) -> PythonObject {
1198
+ return performBinaryOp ( PyNumber_And, lhs: lhs, rhs: rhs)
1199
+ }
1200
+
1201
+ static func | ( lhs: PythonObject , rhs: PythonObject ) -> PythonObject {
1202
+ return performBinaryOp ( PyNumber_Or, lhs: lhs, rhs: rhs)
1203
+ }
1204
+
1205
+ static func ^ ( lhs: PythonObject , rhs: PythonObject ) -> PythonObject {
1206
+ return performBinaryOp ( PyNumber_Xor, lhs: lhs, rhs: rhs)
1207
+ }
1208
+
1209
+ static func &= ( lhs: inout PythonObject , rhs: PythonObject ) {
1210
+ lhs = performBinaryOp ( PyNumber_InPlaceAnd, lhs: lhs, rhs: rhs)
1211
+ }
1212
+
1213
+ static func |= ( lhs: inout PythonObject , rhs: PythonObject ) {
1214
+ lhs = performBinaryOp ( PyNumber_InPlaceOr, lhs: lhs, rhs: rhs)
1215
+ }
1216
+
1217
+ static func ^= ( lhs: inout PythonObject , rhs: PythonObject ) {
1218
+ lhs = performBinaryOp ( PyNumber_InPlaceXor, lhs: lhs, rhs: rhs)
1219
+ }
1220
+
1221
+ static prefix func ~ ( _ operand: Self ) -> Self {
1222
+ return performUnaryOp ( PyNumber_Invert, operand: operand)
1223
+ }
1224
+ }
1225
+
1196
1226
extension PythonObject : SignedNumeric {
1197
1227
public init < T : BinaryInteger > ( exactly value: T ) {
1198
1228
self . init ( Int ( value) )
@@ -1243,23 +1273,23 @@ extension PythonObject : Equatable, Comparable {
1243
1273
fatalError ( " No result or error returned when comparing \( self ) to \( other) " )
1244
1274
}
1245
1275
}
1246
-
1276
+
1247
1277
public static func == ( lhs: PythonObject , rhs: PythonObject ) -> Bool {
1248
1278
return lhs. compared ( to: rhs, byOp: Py_EQ)
1249
1279
}
1250
-
1280
+
1251
1281
public static func != ( lhs: PythonObject , rhs: PythonObject ) -> Bool {
1252
1282
return lhs. compared ( to: rhs, byOp: Py_NE)
1253
1283
}
1254
-
1284
+
1255
1285
public static func < ( lhs: PythonObject , rhs: PythonObject ) -> Bool {
1256
1286
return lhs. compared ( to: rhs, byOp: Py_LT)
1257
1287
}
1258
-
1288
+
1259
1289
public static func <= ( lhs: PythonObject , rhs: PythonObject ) -> Bool {
1260
1290
return lhs. compared ( to: rhs, byOp: Py_LE)
1261
1291
}
1262
-
1292
+
1263
1293
public static func > ( lhs: PythonObject , rhs: PythonObject ) -> Bool {
1264
1294
return lhs. compared ( to: rhs, byOp: Py_GT)
1265
1295
}
@@ -1269,6 +1299,49 @@ extension PythonObject : Equatable, Comparable {
1269
1299
}
1270
1300
}
1271
1301
1302
+ public extension PythonObject {
1303
+ private func compared( to other: PythonObject , byOp: Int32 ) -> PythonObject {
1304
+ let lhsObject = ownedPyObject
1305
+ let rhsObject = other. ownedPyObject
1306
+ defer {
1307
+ Py_DecRef ( lhsObject)
1308
+ Py_DecRef ( rhsObject)
1309
+ }
1310
+ assert ( PyErr_Occurred ( ) == nil ,
1311
+ " Python error occurred somewhere but wasn't handled " )
1312
+ guard let result = PyObject_RichCompare ( lhsObject, rhsObject, byOp) else {
1313
+ // If a Python exception was thrown, throw a corresponding Swift error.
1314
+ try ! throwPythonErrorIfPresent ( )
1315
+ fatalError ( " No result or error returned when comparing \( self ) to \( other) " )
1316
+ }
1317
+ return PythonObject ( consuming: result)
1318
+ }
1319
+
1320
+ static func == ( lhs: PythonObject , rhs: PythonObject ) -> PythonObject {
1321
+ return lhs. compared ( to: rhs, byOp: Py_EQ)
1322
+ }
1323
+
1324
+ static func != ( lhs: PythonObject , rhs: PythonObject ) -> PythonObject {
1325
+ return lhs. compared ( to: rhs, byOp: Py_NE)
1326
+ }
1327
+
1328
+ static func < ( lhs: PythonObject , rhs: PythonObject ) -> PythonObject {
1329
+ return lhs. compared ( to: rhs, byOp: Py_LT)
1330
+ }
1331
+
1332
+ static func <= ( lhs: PythonObject , rhs: PythonObject ) -> PythonObject {
1333
+ return lhs. compared ( to: rhs, byOp: Py_LE)
1334
+ }
1335
+
1336
+ static func > ( lhs: PythonObject , rhs: PythonObject ) -> PythonObject {
1337
+ return lhs. compared ( to: rhs, byOp: Py_GT)
1338
+ }
1339
+
1340
+ static func >= ( lhs: PythonObject , rhs: PythonObject ) -> PythonObject {
1341
+ return lhs. compared ( to: rhs, byOp: Py_GE)
1342
+ }
1343
+ }
1344
+
1272
1345
extension PythonObject : Hashable {
1273
1346
public func hash( into hasher: inout Hasher ) {
1274
1347
guard let hash = Int ( self . __hash__ ( ) ) else {
0 commit comments