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

Commit b443414

Browse files
Merge pull request #1 from superluminalengineering/luminal
Luminal
2 parents 8de2a3f + 365177d commit b443414

File tree

2 files changed

+129
-10
lines changed

2 files changed

+129
-10
lines changed

PythonKit/Python.swift

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
12341300
extension 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

13101384
public extension PythonObject {

PythonKit/PythonLibrary+Symbols.swift

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ let PyObject_SetAttrString: @convention(c) (
122122
PyObjectPointer, PyCCharPointer, PyObjectPointer) -> Int32 =
123123
PythonLibrary.loadSymbol(name: "PyObject_SetAttrString")
124124

125+
let PyObject_Not: PyUnaryOperation =
126+
PythonLibrary.loadSymbol(name: "PyObject_Not")
127+
125128
let PySlice_New: @convention(c) (
126129
PyObjectPointer?, PyObjectPointer?,
127130
PyObjectPointer?) -> PyObjectPointer? =
@@ -238,9 +241,45 @@ let PyNumber_Subtract: PyBinaryOperation =
238241
let PyNumber_Multiply: PyBinaryOperation =
239242
PythonLibrary.loadSymbol(name: "PyNumber_Multiply")
240243

244+
let PyNumber_MatrixMultiply: PyBinaryOperation =
245+
PythonLibrary.loadSymbol(name: "PyNumber_MatrixMultiply")
246+
247+
let PyNumber_FloorDivide: PyBinaryOperation =
248+
PythonLibrary.loadSymbol(name: "PyNumber_FloorDivide")
249+
241250
let PyNumber_TrueDivide: PyBinaryOperation =
242251
PythonLibrary.loadSymbol(name: "PyNumber_TrueDivide")
243252

253+
let PyNumber_Remainder: PyBinaryOperation =
254+
PythonLibrary.loadSymbol(name: "PyNumber_Remainder")
255+
256+
let PyNumber_Power: @convention(c) (PyObjectPointer?, PyObjectPointer?, PyObjectPointer?) -> PyObjectPointer? =
257+
PythonLibrary.loadSymbol(name: "PyNumber_Power")
258+
259+
let PyNumber_Negative: PyUnaryOperation =
260+
PythonLibrary.loadSymbol(name: "PyNumber_Negative")
261+
262+
let PyNumber_Positive: PyUnaryOperation =
263+
PythonLibrary.loadSymbol(name: "PyNumber_Positive")
264+
265+
let PyNumber_Invert: PyUnaryOperation =
266+
PythonLibrary.loadSymbol(name: "PyNumber_Invert")
267+
268+
let PyNumber_Lshift: PyBinaryOperation =
269+
PythonLibrary.loadSymbol(name: "PyNumber_Lshift")
270+
271+
let PyNumber_Rshift: PyBinaryOperation =
272+
PythonLibrary.loadSymbol(name: "PyNumber_Rshift")
273+
274+
let PyNumber_And: PyBinaryOperation =
275+
PythonLibrary.loadSymbol(name: "PyNumber_And")
276+
277+
let PyNumber_Xor: PyBinaryOperation =
278+
PythonLibrary.loadSymbol(name: "PyNumber_Xor")
279+
280+
let PyNumber_Or: PyBinaryOperation =
281+
PythonLibrary.loadSymbol(name: "PyNumber_Or")
282+
244283
let PyNumber_InPlaceAdd: PyBinaryOperation =
245284
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceAdd")
246285

@@ -250,20 +289,26 @@ let PyNumber_InPlaceSubtract: PyBinaryOperation =
250289
let PyNumber_InPlaceMultiply: PyBinaryOperation =
251290
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceMultiply")
252291

292+
let PyNumber_InPlaceMatrixMultiply: PyBinaryOperation =
293+
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceMatrixMultiply")
294+
295+
let PyNumber_InPlaceFloorDivide: PyBinaryOperation =
296+
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceFloorDivide")
297+
253298
let PyNumber_InPlaceTrueDivide: PyBinaryOperation =
254299
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceTrueDivide")
255300

256-
let PyNumber_Negative: PyUnaryOperation =
257-
PythonLibrary.loadSymbol(name: "PyNumber_Negative")
301+
let PyNumber_InPlaceRemainder: PyBinaryOperation =
302+
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceRemainder")
258303

259-
let PyNumber_And: PyBinaryOperation =
260-
PythonLibrary.loadSymbol(name: "PyNumber_And")
304+
let PyNumber_InPlacePower: @convention(c) (PyObjectPointer?, PyObjectPointer?, PyObjectPointer?) -> PyObjectPointer? =
305+
PythonLibrary.loadSymbol(name: "PyNumber_InPlacePower")
261306

262-
let PyNumber_Or: PyBinaryOperation =
263-
PythonLibrary.loadSymbol(name: "PyNumber_Or")
307+
let PyNumber_InPlaceLshift: PyBinaryOperation =
308+
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceLshift")
264309

265-
let PyNumber_Xor: PyBinaryOperation =
266-
PythonLibrary.loadSymbol(name: "PyNumber_Xor")
310+
let PyNumber_InPlaceRshift: PyBinaryOperation =
311+
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceRshift")
267312

268313
let PyNumber_InPlaceAnd: PyBinaryOperation =
269314
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceAnd")
@@ -274,5 +319,5 @@ let PyNumber_InPlaceOr: PyBinaryOperation =
274319
let PyNumber_InPlaceXor: PyBinaryOperation =
275320
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceXor")
276321

277-
let PyNumber_Invert: PyUnaryOperation =
278-
PythonLibrary.loadSymbol(name: "PyNumber_Invert")
322+
let PySequence_Contains: @convention(c) (PyObjectPointer?, PyObjectPointer?) -> Int32 =
323+
PythonLibrary.loadSymbol(name: "PySequence_Contains")

0 commit comments

Comments
 (0)