Tags: Py-Swift/PySwiftKit
Tags
PySwiftKit Major Refactor and Feature Enhancement (#1) * complete rewrite * Add PyDeserialize and PySerialize support for core types Implemented PyDeserialize and PySerialize conformances for Bool, signed/unsigned integers, floating point types, Date, and RawRepresentable. Added error handling using PyStandardException and introduced new files for type checks, Python type definitions, and exception handling. Updated tests to cover deserialization and serialization of these types, and improved Python initialization logic. * Add PyDeserialize conformance for Set type Implements the PyDeserialize protocol for Set where Element conforms to PyDeserialize. Adds methods to cast Python set objects to Swift Set<Element> using both safe and unsafe casting. * Update PyDeserialize+Strings.swift * ... * PySwiftWrapper reimplemented * Update Package.swift * Create PySwiftImports.swift * Create PySerialize+PyPointer.swift * Update Package.swift * Create PyImport.swift * Update PyImport.swift * Update PyDeserialize+FloatingPoint.swift * Update PySerialize+FloatingPoint.swift * Add PyWrap type utilities and enhance type conversion Introduces PyWrap.swift with enums and protocols for type classification and conversion logic. Refactors ConvertArgs.swift to use PyWrap for more robust type handling, including support for function types and improved conversion for optionals, arrays, and force-unwrapped expressions. * Update ConvertArgs.swift * Update ConvertArgs.swift * Add PyCasted macro for Python object casting Introduces the PyCasted macro to cast PyPointer and VectorArgs to Swift types conforming to PyDeserialize using external macros from PySwiftGenerators. * Refactor pyPointer property to pyPointer() method calls Replaces all usages of the pyPointer property with explicit pyPointer() method calls across multiple files for consistency and correctness. This affects attribute setting, function argument conversion, get/set definition generation, and class/closure return handling. * Add PySerialize and PyDeserialize support for Optional Introduces extensions to enable Optional types to conform to PySerialize and PyDeserialize when their Wrapped type conforms. This allows seamless serialization and deserialization of Optional values between Swift and Python, handling None values appropriately. * Add PySerialize and PyDeserialize support for URL Introduces extensions to enable URL serialization and deserialization with PySerialize and PyDeserialize protocols. Also removes unused test function from PySerialize.swift. * Add PyPointer extension for Python constants Introduces a PyPointer extension providing static properties for Python None, True, and False constants, improving code readability and convenience when interacting with CPython objects. * Add PyCastMacro and PyCastMacroVectorArgs support Registered PyCastMacro and PyCastMacroVectorArgs in PySwiftGeneratorsPlugin. Updated PyCasted macro to use PyCastMacroVectorArgs for VectorArgs, improving macro handling for casting operations. * Refactor PyCastMacro to use argument labels Updated PyCastMacro to check for 'index' label in arguments instead of relying on array expression type. This improves flexibility and clarity in macro expansion logic. * Update casted method calls to use 'from:' label Refactored macro implementations to call 'casted(from: ...)' instead of 'casted(...)', improving clarity and consistency in argument labeling. * Update PyCasted.swift * Update PyCasted.swift * Update TypeAliases.swift * Create PyGetSetDef.swift * Add Data serialization and deserialization support Introduces PySerialize and PyDeserialize protocol conformances for Data, enabling conversion between Swift Data and Python objects. Also adds utility for ubyte format handling in PySerializing.swift. * Move ubyte_format and add PyBuffer utilities Relocated the ubyte_format pointer extension from PySerializing.swift to a new PyBuffer.swift file in PySwiftKit. Added buffer info filling utilities for UnsafeMutableRawBufferPointer and UnsafeMutablePointer<UInt8> to facilitate Py_buffer struct population for Python interoperability. * Improve Date deserialization and add PyDateTime type Enhanced Date deserialization to support PyDateTime, PyFloat, and PyUnicode types, allowing conversion from ISO8601 strings and timestamps. Added PyDateTime to PyTypes for better interoperability with Python datetime objects. * Add PyDict and PyTuple utility functions Introduces PyDict_GetItem and PyTuple_GetItem functions in PySwiftKit for interacting with Python dictionaries and tuples from Swift. Also updates Date deserialization to use 'unsafe' casting methods for improved type handling. * Update Swift tools version and improve target config Bumped Swift tools version to 6.0 and updated swift-syntax dependency to 601.0.0 in Package.swift. Refactored target definitions to use a function and added .swiftLanguageMode(.v5) to target swiftSettings. Improved type extraction logic in PyCallbackGenerator.swift and commented out unused PyDict_GetItem function in PyDict.swift. * Add PySerializable typealias for serialization protocols Introduced a public typealias PySerializable combining PySerialize and PyDeserialize for convenience when working with types that support both serialization and deserialization. * Add casted methods for Dictionary with PyPointer values Introduces two static methods, casted(from:) and casted(unsafe:), to convert PyPointer objects to Dictionary<Key, PyPointer> where Key conforms to PyDeserialize. These methods facilitate safe and unsafe deserialization of Python dictionaries into Swift dictionaries. * Add PyObject_GetAttr utility function Introduces PyObject_GetAttr to safely retrieve Python object attributes in Swift, throwing a keyError if the attribute is not found. * Add @preconcurrency attribute to PyDeserialize The PyDeserialize type in the generated extension now includes the @preconcurrency attribute, ensuring proper concurrency semantics for macro expansions. * Add Swift concurrency support and SwiftMode option Introduces a new SwiftMode enum to support Swift 5 and Swift 6 concurrency modes across the codebase. Updates macros, generators, and protocol conformance to handle the new swift_mode parameter, including @mainactor annotations for Swift 6. Refactors PyClass, PyClassByExtension, and PyContainer macros to accept swift_mode. Updates PySwiftObject C interface and related Swift wrappers for improved object creation and casting. Adds PyTypeObjectContainer for thread-safe type handling. Enhances code generation for PyClass and PyGetSetDefs to support concurrency and actor isolation. * Add PyDeserialize extension for AnyObject casting Introduces an extension to PyDeserialize for types conforming to AnyObject, providing safe and unsafe casting from PyPointer to Swift objects using PySwiftObject_Cast. * Remove generic type parameters from PyTuple_GetItem and PyDict_GetItem calls Refactored code to eliminate explicit generic type parameters from PyTuple_GetItem and PyDict_GetItem function calls, simplifying their usage and improving readability. * Update ObjectInitializer.swift * Update ObjectInitializer.swift * Refactor PyTypeObjectStruct to use options array Replaces individual boolean parameters in PyTypeObjectStruct with an options array of enum values. Updates all usages in PySwiftClassGenerator to construct the options array based on class properties, improving extensibility and clarity. * Add public visibility support to class generator Introduces detection of the 'public' attribute for class declarations and propagates it to generated type structures and static properties. This allows generated Swift classes and their pyTypeObject properties to be marked as public when appropriate. * Update PySwiftModuleGenerator.swift * Update PySwiftClassGenerator.swift * Add PyObject_AS_SwiftPtr accessor and fix return type Introduces the PyObject_AS_SwiftPtr function to retrieve the swift_ptr from a PyObject. Also corrects the return type in PySwiftObject_Create to explicitly cast to PyObject*. * Make PyObject_AS_SwiftPtr an inline function Changed the definition and declaration of PyObject_AS_SwiftPtr to use the 'inline' keyword for potential performance improvements. * Add swift_mode and MainActor support to method generators Introduces a swift_mode parameter to PyAsyncMethodsGenerator, PyBufferGenerator, PyMappingMethodsGenerator, PyNumberMethodsGenerator, and PySequenceMethodsGenerator. Applies the MainActor modifier when swift_mode is .v6. Updates PyClass to pass swift_mode to these generators, ensuring correct concurrency annotations based on Swift version. * Add PyStrProtocol and move PyNumberProtocol to Protocols Introduces the PyStrProtocol protocol with a default implementation for types conforming to CustomStringConvertible. Also moves PyNumberProtocol.swift into the Protocols directory for better organization. * Refactor modifier handling for PyClass variable declarations Introduces a base_modifiers property to centralize declaration modifiers, allowing conditional addition of modifiers like .MainActor for Swift 6. Updates all relevant variable declarations to use this new approach, improving maintainability and consistency. * Reorder modifiers in PyGetSetDefs output Moved the 'fileprivate' modifier after the 'MainActor' modifier in the output property to ensure correct modifier order when generating declarations. * Use 'let' instead of 'var' for static function properties Replaces 'var' with 'let' for static properties 'tp_hash', 'tp_str', 'tp_repr', and 'tp_iter' in PyClass extension. This change ensures these static properties are immutable, reflecting their intended usage. * Comment out PyClassProtocol inheritance for v6 main actor The inherited type PyClassProtocol is now commented out for classes marked as main actor in version v6. This may be to prevent protocol conformance issues or to adjust class behavior for main actor classes. * Update PySwiftClassGenerator.swift * Add PyObject_HasAttr function to PyAttr.swift Introduces a new public function, PyObject_HasAttr, which checks if a Python object has a given attribute. This complements the existing attribute access utilities. * Update PyObject_GetAttr usage to use key parameter Replaces direct attribute string argument with the 'key' parameter in PyObject_GetAttr calls in both PyCallbackGenerator and PyContainerMacro. This change likely aligns with an updated function signature or improves clarity and safety. * Refactor Swift pointer access in PyDeserialize Replaced direct access to swift_ptr via PySwiftObject_Cast with the PyObject_AS_SwiftPtr function for improved encapsulation and clarity. Added @inlinable attribute and a new unsafeUnpacked method for convenience. Updated C header and implementation to match function signature style. * Comment out 'inline' for PyObject_AS_SwiftPtr The 'inline' keyword was commented out in both the C and header files for the PyObject_AS_SwiftPtr function. This may address compatibility or linkage issues with the function's usage. * Update PyCallableProtocol.swift * Update Package.resolved and clean up PyCallableProtocol.swift * Add comprehensive development guide for PySwiftKit --------- Co-authored-by: Py-Swift <psychowaspx@gmail.com>
@pyinit fix n @dynamicMemberLookup @pyinit was using wrong function to extract the python values in certain cases and should always just be one fixed set of functions. @dynamicMemberLookup new auto type cast for __getattr__, __setattr__ behavior of the target py_class wrapped on swift side when using the container. Allows to get and set by a dynamic key when using .some_variable, but resulting code is still static compiled for each usage.
PreviousNext