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

Commit 175e90e

Browse files
author
marcrasi
authored
Merge pull request pvieito#28 from liuliu/master
Implement missing Python calls to make Pandas usage natural
2 parents c1c59bf + 5d1cf32 commit 175e90e

File tree

2 files changed

+103
-5
lines changed

2 files changed

+103
-5
lines changed

PythonKit/Python.swift

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,36 @@ public extension PythonObject {
11931193
}
11941194
}
11951195

1196+
public extension PythonObject {
1197+
static func & (lhs: PythonObject, rhs: PythonObject) -> PythonObject {
1198+
return performBinaryOp(PyNumber_And, lhs: lhs, rhs: rhs)
1199+
}
1200+
1201+
static func | (lhs: PythonObject, rhs: PythonObject) -> PythonObject {
1202+
return performBinaryOp(PyNumber_Or, lhs: lhs, rhs: rhs)
1203+
}
1204+
1205+
static func ^ (lhs: PythonObject, rhs: PythonObject) -> PythonObject {
1206+
return performBinaryOp(PyNumber_Xor, lhs: lhs, rhs: rhs)
1207+
}
1208+
1209+
static func &= (lhs: inout PythonObject, rhs: PythonObject) {
1210+
lhs = performBinaryOp(PyNumber_InPlaceAnd, lhs: lhs, rhs: rhs)
1211+
}
1212+
1213+
static func |= (lhs: inout PythonObject, rhs: PythonObject) {
1214+
lhs = performBinaryOp(PyNumber_InPlaceOr, lhs: lhs, rhs: rhs)
1215+
}
1216+
1217+
static func ^= (lhs: inout PythonObject, rhs: PythonObject) {
1218+
lhs = performBinaryOp(PyNumber_InPlaceXor, lhs: lhs, rhs: rhs)
1219+
}
1220+
1221+
static prefix func ~ (_ operand: Self) -> Self {
1222+
return performUnaryOp(PyNumber_Invert, operand: operand)
1223+
}
1224+
}
1225+
11961226
extension PythonObject : SignedNumeric {
11971227
public init<T : BinaryInteger>(exactly value: T) {
11981228
self.init(Int(value))
@@ -1243,23 +1273,23 @@ extension PythonObject : Equatable, Comparable {
12431273
fatalError("No result or error returned when comparing \(self) to \(other)")
12441274
}
12451275
}
1246-
1276+
12471277
public static func == (lhs: PythonObject, rhs: PythonObject) -> Bool {
12481278
return lhs.compared(to: rhs, byOp: Py_EQ)
12491279
}
1250-
1280+
12511281
public static func != (lhs: PythonObject, rhs: PythonObject) -> Bool {
12521282
return lhs.compared(to: rhs, byOp: Py_NE)
12531283
}
1254-
1284+
12551285
public static func < (lhs: PythonObject, rhs: PythonObject) -> Bool {
12561286
return lhs.compared(to: rhs, byOp: Py_LT)
12571287
}
1258-
1288+
12591289
public static func <= (lhs: PythonObject, rhs: PythonObject) -> Bool {
12601290
return lhs.compared(to: rhs, byOp: Py_LE)
12611291
}
1262-
1292+
12631293
public static func > (lhs: PythonObject, rhs: PythonObject) -> Bool {
12641294
return lhs.compared(to: rhs, byOp: Py_GT)
12651295
}
@@ -1269,6 +1299,49 @@ extension PythonObject : Equatable, Comparable {
12691299
}
12701300
}
12711301

1302+
public extension PythonObject {
1303+
private func compared(to other: PythonObject, byOp: Int32) -> PythonObject {
1304+
let lhsObject = ownedPyObject
1305+
let rhsObject = other.ownedPyObject
1306+
defer {
1307+
Py_DecRef(lhsObject)
1308+
Py_DecRef(rhsObject)
1309+
}
1310+
assert(PyErr_Occurred() == nil,
1311+
"Python error occurred somewhere but wasn't handled")
1312+
guard let result = PyObject_RichCompare(lhsObject, rhsObject, byOp) else {
1313+
// If a Python exception was thrown, throw a corresponding Swift error.
1314+
try! throwPythonErrorIfPresent()
1315+
fatalError("No result or error returned when comparing \(self) to \(other)")
1316+
}
1317+
return PythonObject(consuming: result)
1318+
}
1319+
1320+
static func == (lhs: PythonObject, rhs: PythonObject) -> PythonObject {
1321+
return lhs.compared(to: rhs, byOp: Py_EQ)
1322+
}
1323+
1324+
static func != (lhs: PythonObject, rhs: PythonObject) -> PythonObject {
1325+
return lhs.compared(to: rhs, byOp: Py_NE)
1326+
}
1327+
1328+
static func < (lhs: PythonObject, rhs: PythonObject) -> PythonObject {
1329+
return lhs.compared(to: rhs, byOp: Py_LT)
1330+
}
1331+
1332+
static func <= (lhs: PythonObject, rhs: PythonObject) -> PythonObject {
1333+
return lhs.compared(to: rhs, byOp: Py_LE)
1334+
}
1335+
1336+
static func > (lhs: PythonObject, rhs: PythonObject) -> PythonObject {
1337+
return lhs.compared(to: rhs, byOp: Py_GT)
1338+
}
1339+
1340+
static func >= (lhs: PythonObject, rhs: PythonObject) -> PythonObject {
1341+
return lhs.compared(to: rhs, byOp: Py_GE)
1342+
}
1343+
}
1344+
12721345
extension PythonObject : Hashable {
12731346
public func hash(into hasher: inout Hasher) {
12741347
guard let hash = Int(self.__hash__()) else {

PythonKit/PythonLibrary+Symbols.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ let PyTuple_SetItem: @convention(c) (
116116
PyObjectPointer, Int, PyObjectPointer) -> Void =
117117
PythonLibrary.loadSymbol(name: "PyTuple_SetItem")
118118

119+
let PyObject_RichCompare: @convention(c) (
120+
PyObjectPointer, PyObjectPointer, Int32) -> PyObjectPointer? =
121+
PythonLibrary.loadSymbol(name: "PyObject_RichCompare")
122+
119123
let PyObject_RichCompareBool: @convention(c) (
120124
PyObjectPointer, PyObjectPointer, Int32) -> Int32 =
121125
PythonLibrary.loadSymbol(name: "PyObject_RichCompareBool")
@@ -219,3 +223,24 @@ let PyNumber_InPlaceTrueDivide: PyBinaryOperation =
219223

220224
let PyNumber_Negative: PyUnaryOperation =
221225
PythonLibrary.loadSymbol(name: "PyNumber_Negative")
226+
227+
let PyNumber_And: PyBinaryOperation =
228+
PythonLibrary.loadSymbol(name: "PyNumber_And")
229+
230+
let PyNumber_Or: PyBinaryOperation =
231+
PythonLibrary.loadSymbol(name: "PyNumber_Or")
232+
233+
let PyNumber_Xor: PyBinaryOperation =
234+
PythonLibrary.loadSymbol(name: "PyNumber_Xor")
235+
236+
let PyNumber_InPlaceAnd: PyBinaryOperation =
237+
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceAnd")
238+
239+
let PyNumber_InPlaceOr: PyBinaryOperation =
240+
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceOr")
241+
242+
let PyNumber_InPlaceXor: PyBinaryOperation =
243+
PythonLibrary.loadSymbol(name: "PyNumber_InPlaceXor")
244+
245+
let PyNumber_Invert: PyUnaryOperation =
246+
PythonLibrary.loadSymbol(name: "PyNumber_Invert")

0 commit comments

Comments
 (0)