[Experimental] Automatic boxing and unboxing for WinRT Object values in Python - #192
Closed
leileizhang (lei9444) wants to merge 3 commits into
Closed
leileizhang (lei9444) wants to merge 3 commits into
leileizhang (lei9444) wants to merge 3 commits into
Conversation
EXPERIMENTAL prototype for design evaluation; not for merge. Core (crates/dynwinrt/src/property_value.rs) now models every Windows.Foundation.PropertyType that has a payload (37 of 41): adds DateTime, TimeSpan, Point, Size, Rect, their arrays and InspectableArray. unbox_property_value classifies payload-less boxes as Unsupported instead of failing, and box_property_value creates system PropertyValue boxes. The JS binding keeps its exact previous behavior through a compile-only adapter. The Python binding gains one conversion layer (src/object_value.rs): to_winrt_object / from_winrt_object, with unbox_object as the same function. A public value model (python/dynwinrt/_values.py) adds PropertyType-tagged scalars (UInt8 ... Char16), typed arrays, immutable Point/Size/Rect and the WinRTObjectValue / WinRTObjectInput aliases, so values read from Object positions write back with their exact WinRT type. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…ooks EXPERIMENTAL prototype for design evaluation; not for merge. python/signature.rs owns the single codegen hook per direction, py_to_winrt_object / py_from_winrt_object. Every TypeMeta::Object arm uses them: parameters and setters, collection and array elements, returns, getters, out params, async results, event arguments, and the inputs and outputs of Python-implemented interfaces. Object no longer shares its return arm with unknown runtime classes and interfaces. type_helpers::py_object_annotation is the single annotation hook; Object positions read WinRTObjectValue | None and accept WinRTObjectInput | None through the shared py_optional_type. The aliases are collision-safe support symbols. Generated enums carry an IReference<Enum> boxing marker and generated Windows.Foundation Point/Size/Rect carry a PropertyType marker. Composable factory outers keep their raw object-reference projection. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
EXPERIMENTAL prototype for design evaluation; not for merge. test_object_values_generated.py generates Windows SDK bindings and runs each scenario in a fresh process: a PropertyValue.create_X round trip for every PropertyType, plain-value writes, PropertySet/ValueSet/StringMap mapping behavior, identity and project_as, generated enums and geometry structs, DeviceInformation and StorageFile property stores, and DeviceWatcher.EnumerationCompleted arguments typed Object. samples/python/object-boxing-demo shows the natural code, times the conversion, and is a mypy --strict / pyright typing consumer. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Mixed-language test coverageWorkflow status: ✅ Passed
|
Contributor
Author
|
Closing this experiment. We will keep
This draft stays as a reference implementation; its core pieces will be reused. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
WinRT
Object(IInspectable) values are awkward in the Python projection:ps["n"] = 5raisesTypeError, so callers have to writePropertyValue.create_int32(5).DynWinRTValuethat has to be passed throughdynwinrt.unbox_object(). DateTime, TimeSpan, Point, Size and Rect cannot be unboxed at all.intand is written back as Int32.This draft tries full automatic boxing and unboxing, so the effect can be judged.
Key changes
crates/dynwinrt/src/property_value.rs. It covers 37 of the 41PropertyTypes, unboxes without raising, and adds a newbox_property_value.bindings/py/src/object_value.rs:to_winrt_object/from_winrt_object.unbox_objectbecomes the same function.dynwinrtso that read-then-write keeps the exact WinRT type:UInt32,Int64,Single, …);Point,SizeandRect.Objectposition through one hook per direction: params, returns, properties, collection elements, async results and event args.create_int32path.How it looks
Open questions
InspectableArray(current behavior), or raiseTypeError?intdefaults to Int32. APIs that expect another numeric type need an explicit tag.Point,SizeandRectalias the runtime value types?PropertyValue.create_*results are now unboxed automatically, soIPropertyValue.from_value(PropertyValue.create_x(...))no longer works.