-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConvertibleToPython.swift
More file actions
81 lines (67 loc) · 2.63 KB
/
Copy pathConvertibleToPython.swift
File metadata and controls
81 lines (67 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import PythonWrapper
public protocol ConvertibleToPython {
var pythonObject: PythonObject { get }
}
extension PythonObject : ConvertibleToPython {
public var pythonObject: PythonObject { return self }
}
extension Bool : ConvertibleToPython {
public var pythonObject: PythonObject {
return PythonObject(consuming: PyBool_FromLong(self ? 1 : 0))
}
}
extension String : ConvertibleToPython {
public var pythonObject: PythonObject {
let v = self.cString(using:.utf8)!
let c = v.count
let r = UnsafeMutablePointer<CChar>.allocate(capacity: c)
for i in 0..<c { r[i]=v[i] }
let j = PyUnicode_FromStringAndSize(r, c-1)
r.deallocate()
return PythonObject(consuming: j!)
}
}
extension Int : ConvertibleToPython {
public var pythonObject: PythonObject { return PythonObject(consuming: PyLong_FromLong(self)) }
}
extension UInt : ConvertibleToPython {
public var pythonObject: PythonObject { return PythonObject(consuming: PyLong_FromSize_t(Int(self))) }
}
extension Double : ConvertibleToPython {
public var pythonObject: PythonObject { return PythonObject(consuming: PyFloat_FromDouble(self)) }
}
extension Optional : ConvertibleToPython where Wrapped : ConvertibleToPython {
public var pythonObject: PythonObject { return self?.pythonObject ?? Python.None }
}
extension Array : ConvertibleToPython where Element : ConvertibleToPython {
public var pythonObject: PythonObject {
let list = PyList_New(count)!
for (index, element) in enumerated() {
// `PyList_SetItem` steals the reference of the object stored.
_ = PyList_SetItem(list, index, element.pythonObject.retained() )
}
return PythonObject(consuming: list)
}
}
extension Dictionary : ConvertibleToPython where Key : ConvertibleToPython, Value : ConvertibleToPython {
public var pythonObject: PythonObject {
let dict = PyDict_New()!
for (key, value) in self {
let k = key.pythonObject.retained()
let v = value.pythonObject.retained()
PyDict_SetItem(dict, k, v)
Py_DecRef(k)
Py_DecRef(v)
}
return PythonObject(consuming: dict)
}
}
extension Range : ConvertibleToPython where Bound : ConvertibleToPython {
public var pythonObject: PythonObject { return try! Python.slice(lowerBound, upperBound, Python.None) }
}
extension PartialRangeFrom : ConvertibleToPython where Bound : ConvertibleToPython {
public var pythonObject: PythonObject { return try! Python.slice(lowerBound, Python.None, Python.None) }
}
extension PartialRangeUpTo : ConvertibleToPython where Bound : ConvertibleToPython {
public var pythonObject: PythonObject { return try! Python.slice(Python.None, upperBound, Python.None) }
}