@@ -1231,10 +1231,62 @@ public extension PythonObject {
12311231 }
12321232}
12331233
1234- public func pow( _ lhs: PythonObject , _ rhs: PythonObject ) -> PythonObject {
1235- let result = PyNumber_Power ( lhs. borrowedPyObject, rhs. borrowedPyObject, Python . None. borrowedPyObject)
1236- try ! throwPythonErrorIfPresent ( )
1237- return PythonObject ( consuming: result!)
1234+ public enum PythonNumber {
1235+ private static func performBinaryOp( _ op: PythonBinaryOp , lhs: PythonObject , rhs: PythonObject ) throws -> PythonObject {
1236+ let result = op ( lhs. borrowedPyObject, rhs. borrowedPyObject)
1237+ // If binary operation fails (e.g. due to `TypeError`), throw an exception.
1238+ try throwPythonErrorIfPresent ( )
1239+ return PythonObject ( consuming: result!)
1240+ }
1241+
1242+ private static func performUnaryOp( _ op: PythonUnaryOp , operand: PythonObject ) throws -> PythonObject {
1243+ let result = op ( operand. borrowedPyObject)
1244+ // If unary operation fails (e.g. due to `TypeError`), throw an exception.
1245+ try throwPythonErrorIfPresent ( )
1246+ return PythonObject ( consuming: result!)
1247+ }
1248+
1249+ public static func add( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_Add, lhs: lhs, rhs: rhs) }
1250+ public static func subtract( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_Subtract, lhs: lhs, rhs: rhs) }
1251+ public static func multiply( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_Multiply, lhs: lhs, rhs: rhs) }
1252+ public static func matrixMultiply( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_MatrixMultiply, lhs: lhs, rhs: rhs) }
1253+ public static func floorDivide( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_FloorDivide, lhs: lhs, rhs: rhs) }
1254+ public static func trueDivide( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_TrueDivide, lhs: lhs, rhs: rhs) }
1255+ public static func remainder( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_Remainder, lhs: lhs, rhs: rhs) }
1256+ public static func negative( _ operand: PythonObject ) throws -> PythonObject { try performUnaryOp ( PyNumber_Negative, operand: operand) }
1257+ public static func positive( _ operand: PythonObject ) throws -> PythonObject { try performUnaryOp ( PyNumber_Positive, operand: operand) }
1258+ public static func invert( _ operand: PythonObject ) throws -> PythonObject { try performUnaryOp ( PyNumber_Invert, operand: operand) }
1259+ public static func lshift( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_Lshift, lhs: lhs, rhs: rhs) }
1260+ public static func rshift( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_Rshift, lhs: lhs, rhs: rhs) }
1261+ public static func and( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_And, lhs: lhs, rhs: rhs) }
1262+ public static func xor( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_Xor, lhs: lhs, rhs: rhs) }
1263+ public static func or( _ lhs: PythonObject , _ rhs: PythonObject ) throws -> PythonObject { try performBinaryOp ( PyNumber_Or, lhs: lhs, rhs: rhs) }
1264+
1265+ public static func power( _ lhs: PythonObject , _ rhs: PythonObject , modulus: PythonObject = Python . None) throws -> PythonObject {
1266+ let result = PyNumber_Power ( lhs. borrowedPyObject, rhs. borrowedPyObject, modulus. borrowedPyObject)
1267+ try throwPythonErrorIfPresent ( )
1268+ return PythonObject ( consuming: result!)
1269+ }
1270+
1271+ public static func inPlaceAdd( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceAdd, lhs: lhs, rhs: rhs) }
1272+ public static func inPlaceSubtract( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceSubtract, lhs: lhs, rhs: rhs) }
1273+ public static func inPlaceMultiply( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceMultiply, lhs: lhs, rhs: rhs) }
1274+ public static func inPlaceMatrixMultiply( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceMatrixMultiply, lhs: lhs, rhs: rhs) }
1275+ public static func inPlaceFloorDivide( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceFloorDivide, lhs: lhs, rhs: rhs) }
1276+ public static func inPlaceTrueDivide( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceTrueDivide, lhs: lhs, rhs: rhs) }
1277+ public static func inPlaceRemainder( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceRemainder, lhs: lhs, rhs: rhs) }
1278+ public static func inPlaceLshift( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceLshift, lhs: lhs, rhs: rhs) }
1279+ public static func inPlaceRshift( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceRshift, lhs: lhs, rhs: rhs) }
1280+ public static func inPlaceAnd( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceAnd, lhs: lhs, rhs: rhs) }
1281+ public static func inPlaceXor( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceOr, lhs: lhs, rhs: rhs) }
1282+ public static func inPlaceOr( _ lhs: inout PythonObject , _ rhs: PythonObject ) throws { lhs = try performBinaryOp ( PyNumber_InPlaceXor, lhs: lhs, rhs: rhs) }
1283+
1284+ public static func inPlacePower( _ lhs: inout PythonObject , _ rhs: PythonObject , modulus: PythonObject = Python . None) throws {
1285+ let result = PyNumber_InPlacePower ( lhs. borrowedPyObject, rhs. borrowedPyObject, modulus. borrowedPyObject)
1286+ try throwPythonErrorIfPresent ( )
1287+ lhs = PythonObject ( consuming: result!)
1288+ }
1289+
12381290}
12391291
12401292extension PythonObject : SignedNumeric {
0 commit comments