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

Commit cb1a05e

Browse files
authored
Update Python.swift
1 parent b9aa387 commit cb1a05e

File tree

1 file changed

+70
-16
lines changed

1 file changed

+70
-16
lines changed

PythonKit/Python.swift

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,27 +1550,13 @@ public struct PythonFunction {
15501550
return try fn(argumentsAsTuple[0])
15511551
}
15521552
}
1553-
1554-
public init(_ fn: @escaping (PythonObject) throws -> Void) {
1555-
function = PyFunction { argumentsAsTuple in
1556-
try fn(argumentsAsTuple[0])
1557-
return Python.None
1558-
}
1559-
}
15601553

15611554
/// For cases where the Swift function should accept more (or less) than one parameter, accept an ordered array of all arguments instead
15621555
public init(_ fn: @escaping ([PythonObject]) throws -> PythonConvertible) {
15631556
function = PyFunction { argumentsAsTuple in
15641557
return try fn(argumentsAsTuple.map { $0 })
15651558
}
15661559
}
1567-
1568-
public init(_ fn: @escaping ([PythonObject]) throws -> Void) {
1569-
function = PyFunction { argumentsAsTuple in
1570-
try fn(argumentsAsTuple.map { $0 })
1571-
return Python.None
1572-
}
1573-
}
15741560
}
15751561

15761562
extension PythonFunction : PythonConvertible {
@@ -1588,9 +1574,10 @@ extension PythonFunction : PythonConvertible {
15881574
Unmanaged<PyFunction>.fromOpaque(funcPointer).release()
15891575
})
15901576

1591-
let pyFuncPointer = PyCFunction_New(
1577+
let pyFuncPointer = PyCFunction_NewEx(
15921578
PythonFunction.sharedMethodDefinition,
1593-
capsulePointer
1579+
capsulePointer,
1580+
nil
15941581
)
15951582

15961583
return PythonObject(consuming: pyFuncPointer)
@@ -1673,3 +1660,70 @@ struct PyMethodDef {
16731660
/// The __doc__ attribute, or NULL
16741661
var ml_doc: UnsafePointer<Int8>?
16751662
}
1663+
1664+
//===----------------------------------------------------------------------===//
1665+
// PythonInstanceMethod - create functions that can be bound to a Python object
1666+
//===----------------------------------------------------------------------===//
1667+
1668+
public struct PythonInstanceMethod {
1669+
private var function: PythonFunction
1670+
1671+
public init(_ fn: @escaping (PythonObject) throws -> PythonConvertible) {
1672+
function = PythonFunction(fn)
1673+
}
1674+
1675+
public init(_ fn: @escaping ([PythonObject]) throws -> PythonConvertible) {
1676+
function = PythonFunction(fn)
1677+
}
1678+
}
1679+
1680+
extension PythonInstanceMethod : PythonConvertible {
1681+
public var pythonObject: PythonObject {
1682+
let pyFuncPointer = function.pythonObject.ownedPyObject
1683+
let methodPointer = PyInstanceMethod_New(pyFuncPointer)
1684+
return PythonObject(consuming: methodPointer)
1685+
}
1686+
}
1687+
1688+
//===----------------------------------------------------------------------===//
1689+
// PythonClass - construct subclasses of a Python class
1690+
//===----------------------------------------------------------------------===//
1691+
1692+
public struct PythonClass {
1693+
private var typeObject: PythonObject
1694+
1695+
public struct Members: ExpressibleByDictionaryLiteral {
1696+
public typealias Key = String
1697+
public typealias Value = PythonConvertible
1698+
1699+
var dictionary: [String: PythonObject]
1700+
1701+
public init(dictionaryLiteral elements: (Key, Value)...) {
1702+
let castedElements = elements.map { (key, value) in
1703+
(key, value.pythonObject)
1704+
}
1705+
1706+
dictionary = Dictionary(castedElements, uniquingKeysWith: { lhs, _ in lhs })
1707+
}
1708+
}
1709+
1710+
public init(_ name: String, superclasses: [PythonObject] = [], members: Members = [:]) {
1711+
self.init(name, superclasses: superclasses, members: members.dictionary)
1712+
}
1713+
1714+
public init(_ name: String, superclasses: [PythonObject] = [], members: [String: PythonObject] = [:]) {
1715+
var trueSuperclasses = superclasses
1716+
if !trueSuperclasses.contains(Python.object) {
1717+
trueSuperclasses.append(Python.object)
1718+
}
1719+
1720+
let superclassesTuple = PythonObject(tupleContentsOf: trueSuperclasses)
1721+
typeObject = Python.type(name, superclassesTuple, members.pythonObject)
1722+
}
1723+
}
1724+
1725+
extension PythonClass : PythonConvertible {
1726+
public var pythonObject: PythonObject {
1727+
typeObject
1728+
}
1729+
}

0 commit comments

Comments
 (0)