Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Testing (#28)
Browse files Browse the repository at this point in the history
* Delete PyEncodable.swift

* pycall generics update

* PyCall updates

* ...

* Update Package.swift

* pycall fixes

* PyPointer is no longer considered a PyDecodable, since it caused issues with the generics PyCall handlers, and a independent -> PyPointer already existed to handle cases where raw PyPointer is the return value.

* Fix of the issue where "as Void" was needed because swift compiler couldn't tell apart if PyPointer or Void was returned, functions with -> PyPointer have now been marked with "@_disfavoredOverload" which makes swift compiler pick the other 2 options before this, and return type PyPointer must be used to force this version over the others.

* python standard lib added to tests

* Update PythonSwiftCoreTests.swift

---------

Co-authored-by: psychowaspx <[email protected]>
  • Loading branch information
PythonSwiftLink and psychowasp authored Sep 9, 2023
1 parent 43765d7 commit cd041a4
Show file tree
Hide file tree
Showing 645 changed files with 286,155 additions and 387 deletions.
7 changes: 6 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ let package = Package(
// )
.testTarget(
name: "PythonSwiftCoreTests",
dependencies: ["PythonSwiftCore","PythonLib"]),
dependencies: ["PythonSwiftCore","PythonLib"],
resources: [
.copy("python_stdlib")
]
),

]
)
234 changes: 0 additions & 234 deletions PyDecodable.swift

This file was deleted.

20 changes: 20 additions & 0 deletions Sources/PythonSwiftCore/PyCast+PyUnpack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ public func pyCast<R: ConvertibleFromPython>(from o: PyPointer?) throws -> R {
return try R(object: object)
}

@inlinable
public func UnPackPyPointer<T: AnyObject>(from o: PyPointer?) -> T {
guard
let object = o, object != PythonNone,
let pointee = unsafeBitCast(o, to: PySwiftObjectPointer.self)?.pointee
else { fatalError() }

return Unmanaged.fromOpaque(pointee.swift_ptr).takeUnretainedValue()
}

@inlinable
public func UnPackOptionalPyPointer<T: AnyObject>(from o: PyPointer?) -> T? {
guard
let object = o, object != PythonNone,
let pointee = unsafeBitCast(o, to: PySwiftObjectPointer.self)?.pointee
else { return nil }

return Unmanaged.fromOpaque(pointee.swift_ptr).takeUnretainedValue()
}


@inlinable
public func UnPackOptionalPyPointer<T: AnyObject>(with check: PythonType, from self: PyPointer, as: T.Type) -> T? {
Expand Down
21 changes: 19 additions & 2 deletions Sources/PythonSwiftCore/PyDecodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import PythonLib
// }
//
//}

//
extension PyPointer : PyDecodable {

public init(object: PyPointer) throws {
self = object.xINCREF
self = object
}


Expand Down Expand Up @@ -213,6 +213,23 @@ extension Array : PyDecodable where Element : PyDecodable {

}

extension Dictionary: PyDecodable where Key == String, Value == PyPointer {
public init(object: PyPointer) throws {
var d: [Key:Value] = .init()
var pos: Int = 0
var key: PyPointer?
var value: PyPointer?
while PyDict_Next(object, &pos, &key, &value) == 1 {
if let k = key {
d[try String(object: k)] = value
}
}

self = d
}


}



9 changes: 9 additions & 0 deletions Sources/PythonSwiftCore/PyDict.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ public func PyDict_SetItem(_ dict: PyPointer?, _ key: String, _ value: PyPointer
key.withCString { PyDict_SetItemString(dict, $0, value) }
}

public func PyDict_SetItem_Reduced(_ dict: PyPointer,_ next: Dictionary<String, PyPointer>.Element) -> PyPointer {
_ = next.key.withCString { PyDict_SetItemString(dict, $0, next.value) }
return dict
}
public func PyDict_SetItem_ReducedIncRef(_ dict: PyPointer,_ next: Dictionary<String, PyPointer>.Element) -> PyPointer {
_ = next.key.withCString { PyDict_SetItemString(dict, $0, next.value.xINCREF) }
return dict
}

@discardableResult
public func PyDict_SetItem(_ dict: PyPointer?, _ key: String, _ value: PyEncodable) -> Int32 {
key.withCString { PyDict_SetItemString(dict, $0, value.pyPointer) }
Expand Down
4 changes: 3 additions & 1 deletion Sources/PythonSwiftCore/PyEncodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -374,5 +374,7 @@ extension Dictionary: PyEncodable where Key == StringLiteralType, Value == PyEnc

}


public extension Dictionary where Key == String, Value == PyPointer {
var pyDict: PyPointer { self.reduce(PyDict_New()!, PyDict_SetItem_ReducedIncRef) }
}

Loading

0 comments on commit cd041a4

Please sign in to comment.