Skip to content
This repository was archived by the owner on Apr 21, 2023. It is now read-only.

Commit cc45773

Browse files
Expose all operators
1 parent f7b21fd commit cc45773

File tree

2 files changed

+103
-15
lines changed

2 files changed

+103
-15
lines changed

PythonKit/Python.swift

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

12401292
extension PythonObject : SignedNumeric {

PythonKit/PythonLibrary+Symbols.swift

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,45 @@ let PyNumber_Subtract: PyBinaryOperation =
238238
let PyNumber_Multiply: PyBinaryOperation =
239239
PythonLibrary.loadSymbol(name: "PyNumber_Multiply")
240240

241+
let PyNumber_MatrixMultiply: PyBinaryOperation =
242+
PythonLibrary.loadSymbol(name: "PyNumber_MatrixMultiply")
243+
244+
let PyNumber_FloorDivide: PyBinaryOperation =
245+
PythonLibrary.loadSymbol(name: "PyNumber_FloorDivide")
246+
241247
let PyNumber_TrueDivide: PyBinaryOperation =
242248
PythonLibrary.loadSymbol(name: "PyNumber_TrueDivide")
243249

250+
let PyNumber_Remainder: PyBinaryOperation =
251+
PythonLibrary.loadSymbol(name: "PyNumber_Remainder")
252+
244253
let PyNumber_Power: @convention(c) (PyObjectPointer?, PyObjectPointer?, PyObjectPointer?) -> PyObjectPointer? =
245254
PythonLibrary.loadSymbol(name: "PyNumber_Power")
246255

256+
let PyNumber_Negative: PyUnaryOperation =
257+
PythonLibrary.loadSymbol(name: "PyNumber_Negative")
258+
259+
let PyNumber_Positive: PyUnaryOperation =
260+
PythonLibrary.loadSymbol(name: "PyNumber_Positive")
261+
262+
let PyNumber_Invert: PyUnaryOperation =
263+
PythonLibrary.loadSymbol(name: "PyNumber_Invert")
264+
265+
let PyNumber_Lshift: PyBinaryOperation =
266+
PythonLibrary.loadSymbol(name: "PyNumber_Lshift")
267+
268+
let PyNumber_Rshift: PyBinaryOperation =
269+
PythonLibrary.loadSymbol(name: "PyNumber_Rshift")
270+
271+
let PyNumber_And: PyBinaryOperation =
272+
PythonLibrary.loadSymbol(name: "PyNumber_And")
273+
274+
let PyNumber_Xor: PyBinaryOperation =
275+
PythonLibrary.loadSymbol(name: "PyNumber_Xor")
276+
277+
let PyNumber_Or: PyBinaryOperation =
278+
PythonLibrary.loadSymbol(name: "PyNumber_Or")
279+
247280
let PyNumber_InPlaceAdd: PyBinaryOperation =
248281
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceAdd")
249282

@@ -253,20 +286,26 @@ let PyNumber_InPlaceSubtract: PyBinaryOperation =
253286
let PyNumber_InPlaceMultiply: PyBinaryOperation =
254287
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceMultiply")
255288

289+
let PyNumber_InPlaceMatrixMultiply: PyBinaryOperation =
290+
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceMatrixMultiply")
291+
292+
let PyNumber_InPlaceFloorDivide: PyBinaryOperation =
293+
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceFloorDivide")
294+
256295
let PyNumber_InPlaceTrueDivide: PyBinaryOperation =
257296
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceTrueDivide")
258297

259-
let PyNumber_Negative: PyUnaryOperation =
260-
PythonLibrary.loadSymbol(name: "PyNumber_Negative")
298+
let PyNumber_InPlaceRemainder: PyBinaryOperation =
299+
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceRemainder")
261300

262-
let PyNumber_And: PyBinaryOperation =
263-
PythonLibrary.loadSymbol(name: "PyNumber_And")
301+
let PyNumber_InPlacePower: @convention(c) (PyObjectPointer?, PyObjectPointer?, PyObjectPointer?) -> PyObjectPointer? =
302+
PythonLibrary.loadSymbol(name: "PyNumber_InPlacePower")
264303

265-
let PyNumber_Or: PyBinaryOperation =
266-
PythonLibrary.loadSymbol(name: "PyNumber_Or")
304+
let PyNumber_InPlaceLshift: PyBinaryOperation =
305+
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceLshift")
267306

268-
let PyNumber_Xor: PyBinaryOperation =
269-
PythonLibrary.loadSymbol(name: "PyNumber_Xor")
307+
let PyNumber_InPlaceRshift: PyBinaryOperation =
308+
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceRshift")
270309

271310
let PyNumber_InPlaceAnd: PyBinaryOperation =
272311
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceAnd")
@@ -276,6 +315,3 @@ let PyNumber_InPlaceOr: PyBinaryOperation =
276315

277316
let PyNumber_InPlaceXor: PyBinaryOperation =
278317
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceXor")
279-
280-
let PyNumber_Invert: PyUnaryOperation =
281-
PythonLibrary.loadSymbol(name: "PyNumber_Invert")

0 commit comments

Comments
 (0)