@@ -1231,6 +1231,72 @@ public extension PythonObject {
12311231 }
12321232}
12331233
1234+ extension ThrowingPythonObject {
1235+
1236+ public static func not( _ operand: PythonObject ) throws -> PythonObject {
1237+ return try performUnaryOp ( PyObject_Not, operand: operand)
1238+ }
1239+
1240+ private static func performBinaryOp( _ op: PythonBinaryOp , lhs: PythonObject , rhs: PythonObject ) throws -> PythonObject {
1241+ let result = op ( lhs. borrowedPyObject, rhs. borrowedPyObject)
1242+ try throwPythonErrorIfPresent ( )
1243+ return PythonObject ( consuming: result!)
1244+ }
1245+
1246+ private static func performUnaryOp( _ op: PythonUnaryOp , operand: PythonObject ) throws -> PythonObject {
1247+ let result = op ( operand. borrowedPyObject)
1248+ try throwPythonErrorIfPresent ( )
1249+ return PythonObject ( consuming: result!)
1250+ }
1251+
1252+ public static func add( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_Add, lhs: lhs, rhs: rhs) }
1253+ public static func subtract( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_Subtract, lhs: lhs, rhs: rhs) }
1254+ public static func multiply( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_Multiply, lhs: lhs, rhs: rhs) }
1255+ public static func matrixMultiply( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_MatrixMultiply, lhs: lhs, rhs: rhs) }
1256+ public static func floorDivide( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_FloorDivide, lhs: lhs, rhs: rhs) }
1257+ public static func trueDivide( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_TrueDivide, lhs: lhs, rhs: rhs) }
1258+ public static func remainder( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_Remainder, lhs: lhs, rhs: rhs) }
1259+ public static func negative( _ operand: PythonObject ) throws -> PythonObject { try performUnaryOp ( PyNumber_Negative, operand: operand) }
1260+ public static func positive( _ operand: PythonObject ) throws -> PythonObject { try performUnaryOp ( PyNumber_Positive, operand: operand) }
1261+ public static func invert( _ operand: PythonObject ) throws -> PythonObject { try performUnaryOp ( PyNumber_Invert, operand: operand) }
1262+ public static func lshift( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_Lshift, lhs: lhs, rhs: rhs) }
1263+ public static func rshift( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_Rshift, lhs: lhs, rhs: rhs) }
1264+ public static func and( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_And, lhs: lhs, rhs: rhs) }
1265+ public static func xor( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_Xor, lhs: lhs, rhs: rhs) }
1266+ public static func or( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_Or, lhs: lhs, rhs: rhs) }
1267+
1268+ public static func contains( _ sequence: PythonObject , _ x: PythonObject ) throws -> Bool {
1269+ let result = PySequence_Contains ( sequence. borrowedPyObject, x. borrowedPyObject)
1270+ try throwPythonErrorIfPresent ( )
1271+ return ( result == 1 )
1272+ }
1273+
1274+ public static func power( _ lhs: PythonObject , _ rhs: PythonObject , modulus: PythonObject = Python . None) throws -> PythonObject {
1275+ let result = PyNumber_Power ( lhs. borrowedPyObject, rhs. borrowedPyObject, modulus. borrowedPyObject)
1276+ try throwPythonErrorIfPresent ( )
1277+ return PythonObject ( consuming: result!)
1278+ }
1279+
1280+ public static func inPlaceAdd( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceAdd, lhs: lhs, rhs: rhs) }
1281+ public static func inPlaceSubtract( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceSubtract, lhs: lhs, rhs: rhs) }
1282+ public static func inPlaceMultiply( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceMultiply, lhs: lhs, rhs: rhs) }
1283+ public static func inPlaceMatrixMultiply( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceMatrixMultiply, lhs: lhs, rhs: rhs) }
1284+ public static func inPlaceFloorDivide( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceFloorDivide, lhs: lhs, rhs: rhs) }
1285+ public static func inPlaceTrueDivide( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceTrueDivide, lhs: lhs, rhs: rhs) }
1286+ public static func inPlaceRemainder( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceRemainder, lhs: lhs, rhs: rhs) }
1287+ public static func inPlaceLshift( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceLshift, lhs: lhs, rhs: rhs) }
1288+ public static func inPlaceRshift( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceRshift, lhs: lhs, rhs: rhs) }
1289+ public static func inPlaceAnd( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceAnd, lhs: lhs, rhs: rhs) }
1290+ public static func inPlaceXor( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceOr, lhs: lhs, rhs: rhs) }
1291+ public static func inPlaceOr( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceXor, lhs: lhs, rhs: rhs) }
1292+
1293+ public static func inPlacePower( _ lhs: inout PythonObject , _ rhs: PythonObject , modulus: PythonObject = Python . None) throws {
1294+ let result = PyNumber_InPlacePower ( lhs. borrowedPyObject, rhs. borrowedPyObject, modulus. borrowedPyObject)
1295+ try throwPythonErrorIfPresent ( )
1296+ lhs = PythonObject ( consuming: result!)
1297+ }
1298+ }
1299+
12341300extension PythonObject : SignedNumeric {
12351301 public init < T : BinaryInteger > ( exactly value: T ) {
12361302 self . init ( Int ( value) )
@@ -1305,6 +1371,14 @@ extension PythonObject : Equatable, Comparable {
13051371 public static func >= ( lhs: PythonObject , rhs: PythonObject ) -> Bool {
13061372 return lhs. compared ( to: rhs, byOp: Py_GE)
13071373 }
1374+
1375+ public static func === ( lhs: PythonObject , rhs: PythonObject ) -> Bool {
1376+ return lhs. reference === rhs. reference
1377+ }
1378+
1379+ public static func !== ( lhs: PythonObject , rhs: PythonObject ) -> Bool {
1380+ return lhs. reference !== rhs. reference
1381+ }
13081382}
13091383
13101384public extension PythonObject {
0 commit comments