Skip to content
This repository was archived by the owner on Apr 21, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions PythonKit/Python.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,72 @@ public extension PythonObject {
}
}

extension ThrowingPythonObject {

public static func not(_ operand: PythonObject) throws -> PythonObject {
return try performUnaryOp(PyObject_Not, operand: operand)
}

private static func performBinaryOp(_ op: PythonBinaryOp, lhs: PythonObject, rhs: PythonObject) throws -> PythonObject {
let result = op(lhs.borrowedPyObject, rhs.borrowedPyObject)
try throwPythonErrorIfPresent()
return PythonObject(consuming: result!)
}

private static func performUnaryOp(_ op: PythonUnaryOp, operand: PythonObject) throws -> PythonObject {
let result = op(operand.borrowedPyObject)
try throwPythonErrorIfPresent()
return PythonObject(consuming: result!)
}

public static func add(_ lhs: PythonObject, _ rhs: PythonObject) throws -> PythonObject { try performBinaryOp(PyNumber_Add, lhs: lhs, rhs: rhs) }
public static func subtract(_ lhs: PythonObject, _ rhs: PythonObject) throws -> PythonObject { try performBinaryOp(PyNumber_Subtract, lhs: lhs, rhs: rhs) }
public static func multiply(_ lhs: PythonObject, _ rhs: PythonObject) throws -> PythonObject { try performBinaryOp(PyNumber_Multiply, lhs: lhs, rhs: rhs) }
public static func matrixMultiply(_ lhs: PythonObject, _ rhs: PythonObject) throws -> PythonObject { try performBinaryOp(PyNumber_MatrixMultiply, lhs: lhs, rhs: rhs) }
public static func floorDivide(_ lhs: PythonObject, _ rhs: PythonObject) throws -> PythonObject { try performBinaryOp(PyNumber_FloorDivide, lhs: lhs, rhs: rhs) }
public static func trueDivide(_ lhs: PythonObject, _ rhs: PythonObject) throws -> PythonObject { try performBinaryOp(PyNumber_TrueDivide, lhs: lhs, rhs: rhs) }
public static func remainder(_ lhs: PythonObject, _ rhs: PythonObject) throws -> PythonObject { try performBinaryOp(PyNumber_Remainder, lhs: lhs, rhs: rhs) }
public static func negative(_ operand: PythonObject) throws -> PythonObject { try performUnaryOp(PyNumber_Negative, operand: operand) }
public static func positive(_ operand: PythonObject) throws -> PythonObject { try performUnaryOp(PyNumber_Positive, operand: operand) }
public static func invert(_ operand: PythonObject) throws -> PythonObject { try performUnaryOp(PyNumber_Invert, operand: operand) }
public static func lshift(_ lhs: PythonObject, _ rhs: PythonObject) throws -> PythonObject { try performBinaryOp(PyNumber_Lshift, lhs: lhs, rhs: rhs) }
public static func rshift(_ lhs: PythonObject, _ rhs: PythonObject) throws -> PythonObject { try performBinaryOp(PyNumber_Rshift, lhs: lhs, rhs: rhs) }
public static func and(_ lhs: PythonObject, _ rhs: PythonObject) throws -> PythonObject { try performBinaryOp(PyNumber_And, lhs: lhs, rhs: rhs) }
public static func xor(_ lhs: PythonObject, _ rhs: PythonObject) throws -> PythonObject { try performBinaryOp(PyNumber_Xor, lhs: lhs, rhs: rhs) }
public static func or(_ lhs: PythonObject, _ rhs: PythonObject) throws -> PythonObject { try performBinaryOp(PyNumber_Or, lhs: lhs, rhs: rhs) }

public static func contains(_ sequence: PythonObject, _ x: PythonObject) throws -> Bool {
let result = PySequence_Contains(sequence.borrowedPyObject, x.borrowedPyObject)
try throwPythonErrorIfPresent()
return (result == 1)
}

public static func power(_ lhs: PythonObject, _ rhs: PythonObject, modulus: PythonObject = Python.None) throws -> PythonObject {
let result = PyNumber_Power(lhs.borrowedPyObject, rhs.borrowedPyObject, modulus.borrowedPyObject)
try throwPythonErrorIfPresent()
return PythonObject(consuming: result!)
}

public static func inPlaceAdd(_ lhs: inout PythonObject, _ rhs: PythonObject) throws { lhs = try performBinaryOp(PyNumber_InPlaceAdd, lhs: lhs, rhs: rhs) }
public static func inPlaceSubtract(_ lhs: inout PythonObject, _ rhs: PythonObject) throws { lhs = try performBinaryOp(PyNumber_InPlaceSubtract, lhs: lhs, rhs: rhs) }
public static func inPlaceMultiply(_ lhs: inout PythonObject, _ rhs: PythonObject) throws { lhs = try performBinaryOp(PyNumber_InPlaceMultiply, lhs: lhs, rhs: rhs) }
public static func inPlaceMatrixMultiply(_ lhs: inout PythonObject, _ rhs: PythonObject) throws { lhs = try performBinaryOp(PyNumber_InPlaceMatrixMultiply, lhs: lhs, rhs: rhs) }
public static func inPlaceFloorDivide(_ lhs: inout PythonObject, _ rhs: PythonObject) throws { lhs = try performBinaryOp(PyNumber_InPlaceFloorDivide, lhs: lhs, rhs: rhs) }
public static func inPlaceTrueDivide(_ lhs: inout PythonObject, _ rhs: PythonObject) throws { lhs = try performBinaryOp(PyNumber_InPlaceTrueDivide, lhs: lhs, rhs: rhs) }
public static func inPlaceRemainder(_ lhs: inout PythonObject, _ rhs: PythonObject) throws { lhs = try performBinaryOp(PyNumber_InPlaceRemainder, lhs: lhs, rhs: rhs) }
public static func inPlaceLshift(_ lhs: inout PythonObject, _ rhs: PythonObject) throws { lhs = try performBinaryOp(PyNumber_InPlaceLshift, lhs: lhs, rhs: rhs) }
public static func inPlaceRshift(_ lhs: inout PythonObject, _ rhs: PythonObject) throws { lhs = try performBinaryOp(PyNumber_InPlaceRshift, lhs: lhs, rhs: rhs) }
public static func inPlaceAnd(_ lhs: inout PythonObject, _ rhs: PythonObject) throws { lhs = try performBinaryOp(PyNumber_InPlaceAnd, lhs: lhs, rhs: rhs) }
public static func inPlaceXor(_ lhs: inout PythonObject, _ rhs: PythonObject) throws { lhs = try performBinaryOp(PyNumber_InPlaceOr, lhs: lhs, rhs: rhs) }
public static func inPlaceOr(_ lhs: inout PythonObject, _ rhs: PythonObject) throws { lhs = try performBinaryOp(PyNumber_InPlaceXor, lhs: lhs, rhs: rhs) }

public static func inPlacePower(_ lhs: inout PythonObject, _ rhs: PythonObject, modulus: PythonObject = Python.None) throws {
let result = PyNumber_InPlacePower(lhs.borrowedPyObject, rhs.borrowedPyObject, modulus.borrowedPyObject)
try throwPythonErrorIfPresent()
lhs = PythonObject(consuming: result!)
}
}

extension PythonObject : SignedNumeric {
public init<T : BinaryInteger>(exactly value: T) {
self.init(Int(value))
Expand Down Expand Up @@ -1305,6 +1371,14 @@ extension PythonObject : Equatable, Comparable {
public static func >= (lhs: PythonObject, rhs: PythonObject) -> Bool {
return lhs.compared(to: rhs, byOp: Py_GE)
}

public static func === (lhs: PythonObject, rhs: PythonObject) -> Bool {
return lhs.reference === rhs.reference
}

public static func !== (lhs: PythonObject, rhs: PythonObject) -> Bool {
return lhs.reference !== rhs.reference
}
}

public extension PythonObject {
Expand Down
65 changes: 55 additions & 10 deletions PythonKit/PythonLibrary+Symbols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ let PyObject_SetAttrString: @convention(c) (
PyObjectPointer, PyCCharPointer, PyObjectPointer) -> Int32 =
PythonLibrary.loadSymbol(name: "PyObject_SetAttrString")

let PyObject_Not: PyUnaryOperation =
PythonLibrary.loadSymbol(name: "PyObject_Not")

let PySlice_New: @convention(c) (
PyObjectPointer?, PyObjectPointer?,
PyObjectPointer?) -> PyObjectPointer? =
Expand Down Expand Up @@ -238,9 +241,45 @@ let PyNumber_Subtract: PyBinaryOperation =
let PyNumber_Multiply: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_Multiply")

let PyNumber_MatrixMultiply: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_MatrixMultiply")

let PyNumber_FloorDivide: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_FloorDivide")

let PyNumber_TrueDivide: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_TrueDivide")

let PyNumber_Remainder: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_Remainder")

let PyNumber_Power: @convention(c) (PyObjectPointer?, PyObjectPointer?, PyObjectPointer?) -> PyObjectPointer? =
PythonLibrary.loadSymbol(name: "PyNumber_Power")

let PyNumber_Negative: PyUnaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_Negative")

let PyNumber_Positive: PyUnaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_Positive")

let PyNumber_Invert: PyUnaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_Invert")

let PyNumber_Lshift: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_Lshift")

let PyNumber_Rshift: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_Rshift")

let PyNumber_And: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_And")

let PyNumber_Xor: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_Xor")

let PyNumber_Or: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_Or")

let PyNumber_InPlaceAdd: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceAdd")

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

let PyNumber_InPlaceMatrixMultiply: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceMatrixMultiply")

let PyNumber_InPlaceFloorDivide: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceFloorDivide")

let PyNumber_InPlaceTrueDivide: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceTrueDivide")

let PyNumber_Negative: PyUnaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_Negative")
let PyNumber_InPlaceRemainder: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceRemainder")

let PyNumber_And: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_And")
let PyNumber_InPlacePower: @convention(c) (PyObjectPointer?, PyObjectPointer?, PyObjectPointer?) -> PyObjectPointer? =
PythonLibrary.loadSymbol(name: "PyNumber_InPlacePower")

let PyNumber_Or: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_Or")
let PyNumber_InPlaceLshift: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceLshift")

let PyNumber_Xor: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_Xor")
let PyNumber_InPlaceRshift: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceRshift")

let PyNumber_InPlaceAnd: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceAnd")
Expand All @@ -274,5 +319,5 @@ let PyNumber_InPlaceOr: PyBinaryOperation =
let PyNumber_InPlaceXor: PyBinaryOperation =
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceXor")

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