Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/status/PYTHON_CHECKLIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ of a dynamic projection.
- [x] Preserve `Uri` implementation and `.pyi` snapshots.
- [x] Organize output by WinRT namespace instead of one flat short-name
namespace.
- [x] Derive Python symbols, implementation modules, facade modules, and stable
path hashes from a namespace- and kind-preserving semantic type identity,
including recursively closed generic arguments.
- [x] Keep same-short-name types available through their namespace facades,
omit ambiguous root exports, and use deterministic qualified aliases in
consumer modules instead of selecting a short-name ABI implicitly.
- [x] Pass an immutable Python projection context through generation and
rendering instead of installing thread-local module-layout state.
- [x] Detect and reject namespace/type filename collisions.
- [x] Add stage/swap generation and remove stale Python output.
- [x] Emit a consumable Python package manifest with an exact runtime
Expand Down
4 changes: 3 additions & 1 deletion tests/e2e/runners/py_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,9 @@ def block_on_sta():
from typing import get_type_hints

module = importlib.import_module(
implementation_module_name(pkg_name, namespace, cls.__name__)
implementation_module_name(
pkg_name, namespace, 'Direct3DSurfaceDescription'
)
)
descriptor_type = getattr(module, 'Direct3DSurfaceDescription')
nested_type = getattr(module, 'Direct3DMultisampleDescription')
Expand Down
51 changes: 29 additions & 22 deletions tools/dynwinrt-codegen/src/codegen/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,34 +608,32 @@ mod tests {

#[test]
fn py_convert_return_basic() {
let known = HashSet::new();
let context = PythonProjectionContext::default();
assert_eq!(
py_convert_return("r", Some(&TypeMeta::String), false, &known),
py_convert_return("r", Some(&TypeMeta::String), false, &context),
"r.to_string()"
);
assert_eq!(
py_convert_return("r", Some(&TypeMeta::I32), false, &known),
py_convert_return("r", Some(&TypeMeta::I32), false, &context),
"r.to_number()"
);
assert_eq!(
py_convert_return("r", Some(&TypeMeta::U32), false, &known),
py_convert_return("r", Some(&TypeMeta::U32), false, &context),
"r.to_u32()"
);
assert_eq!(
py_convert_return("r", Some(&TypeMeta::U64), false, &known),
py_convert_return("r", Some(&TypeMeta::U64), false, &context),
"r.to_u64()"
);
assert_eq!(
py_convert_return("r", Some(&TypeMeta::Bool), false, &known),
py_convert_return("r", Some(&TypeMeta::Bool), false, &context),
"r.to_bool()"
);
assert_eq!(py_convert_return("r", None, false, &known), "r");
assert_eq!(py_convert_return("r", None, false, &context), "r");
}

#[test]
fn py_convert_return_with_known_class() {
let mut known = HashSet::new();
known.insert("Uri".to_string());
let rt = TypeMeta::RuntimeClass {
namespace: "Windows.Foundation".into(),
name: "Uri".into(),
Expand All @@ -645,12 +643,13 @@ mod tests {
iid: "abc".into(),
})),
};
let context = PythonProjectionContext::packaged([rt.type_identity()]).unwrap();
assert_eq!(
py_convert_return("r", Some(&rt), false, &known),
py_convert_return("r", Some(&rt), false, &context),
"(lambda value: None if value.is_null() else _dynwinrt_symbol('windows__foundation__uri', 'Uri')._from_native(value))(r)"
);
assert_eq!(
py_convert_array_return("r", &rt, &known),
py_convert_array_return("r", &rt, &context),
"_dynwinrt_wrap_values('windows__foundation__uri', 'Uri', r.to_values())"
);
}
Expand All @@ -667,13 +666,13 @@ mod tests {
deprecated: None,
};
assert_eq!(
py_convert_return("r", Some(&en), false, &HashSet::new()),
py_convert_return("r", Some(&en), false, &PythonProjectionContext::default()),
"r.to_number()"
);

let known = HashSet::from(["DayOfWeek".to_string()]);
let context = PythonProjectionContext::packaged([en.type_identity()]).unwrap();
assert_eq!(
py_convert_return("r", Some(&en), false, &known),
py_convert_return("r", Some(&en), false, &context),
"_dynwinrt_enum('windows__globalization__day_of_week', 'DayOfWeek', r.to_number())"
);
}
Expand Down Expand Up @@ -718,16 +717,23 @@ mod tests {

#[test]
fn py_struct_field_getter_expressions() {
let context = PythonProjectionContext::default();
assert_eq!(
py_struct_field_getter(&TypeMeta::Bool, 0),
py_struct_field_getter(&context, &TypeMeta::Bool, 0),
"s.get_u8(0) != 0"
);
assert_eq!(py_struct_field_getter(&TypeMeta::I32, 2), "s.get_i32(2)");
assert_eq!(
py_struct_field_getter(&TypeMeta::String, 1),
py_struct_field_getter(&context, &TypeMeta::I32, 2),
"s.get_i32(2)"
);
assert_eq!(
py_struct_field_getter(&context, &TypeMeta::String, 1),
"s.get_hstring(1)"
);
assert_eq!(py_struct_field_getter(&TypeMeta::F64, 3), "s.get_f64(3)");
assert_eq!(
py_struct_field_getter(&context, &TypeMeta::F64, 3),
"s.get_f64(3)"
);
}

#[test]
Expand All @@ -748,9 +754,10 @@ mod tests {

#[test]
fn py_struct_field_type_mappings() {
assert_eq!(py_struct_field_type(&TypeMeta::Bool), "bool");
assert_eq!(py_struct_field_type(&TypeMeta::String), "str");
assert_eq!(py_struct_field_type(&TypeMeta::I32), "int");
assert_eq!(py_struct_field_type(&TypeMeta::F64), "float");
let context = PythonProjectionContext::default();
assert_eq!(py_struct_field_type(&context, &TypeMeta::Bool), "bool");
assert_eq!(py_struct_field_type(&context, &TypeMeta::String), "str");
assert_eq!(py_struct_field_type(&context, &TypeMeta::I32), "int");
assert_eq!(py_struct_field_type(&context, &TypeMeta::F64), "float");
}
}
31 changes: 24 additions & 7 deletions tools/dynwinrt-codegen/src/codegen/winrt/python/collections.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::meta::{ClassMeta, InterfaceMeta, make_parameterized_name};
use crate::types::TypeMeta;
use crate::meta::{ClassMeta, InterfaceMeta, WINDOWS_FOUNDATION_COLLECTIONS_NAMESPACE};
use crate::types::{TypeIdentity, TypeIdentityKind, TypeMeta};

pub(crate) const IITERABLE_PIID: &str = "faa585ea-6214-4217-afda-7f46de5869b3";
pub(crate) const IITERATOR_PIID: &str = "6a79e863-4300-459a-9966-cbb660963ee1";
Expand Down Expand Up @@ -95,18 +95,35 @@ pub(crate) fn abc_name(kind: CollectionKind) -> Option<&'static str> {
}
}

pub(crate) fn map_iterable_name(args: &[TypeMeta]) -> Option<String> {
pub(crate) fn map_iterable_identity(args: &[TypeMeta]) -> Option<TypeIdentity> {
if args.len() != 2 {
return None;
}
let pair = make_parameterized_name("IKeyValuePair", args);
Some(format!("IIterable_{pair}"))
let pair = TypeIdentity::closed_generic(
TypeIdentityKind::Interface,
WINDOWS_FOUNDATION_COLLECTIONS_NAMESPACE,
"IKeyValuePair",
args.iter().map(TypeMeta::type_identity),
);
Some(TypeIdentity::closed_generic(
TypeIdentityKind::Interface,
WINDOWS_FOUNDATION_COLLECTIONS_NAMESPACE,
"IIterable",
[pair],
))
}

pub(crate) fn observable_vector_name(iface: &InterfaceMeta) -> Option<String> {
pub(crate) fn observable_vector_identity(iface: &InterfaceMeta) -> Option<TypeIdentity> {
(iface.generic_piid.as_deref() == Some(IOBSERVABLE_VECTOR_PIID)
&& iface.generic_args.len() == 1)
.then(|| iface.name.replacen("IObservableVector", "IVector", 1))
.then(|| {
TypeIdentity::closed_generic(
TypeIdentityKind::Interface,
WINDOWS_FOUNDATION_COLLECTIONS_NAMESPACE,
"IVector",
iface.generic_args.iter().map(TypeMeta::type_identity),
)
})
}

pub(crate) fn is_mapping_input(kind: CollectionKind, args: &[TypeMeta]) -> bool {
Expand Down
Loading
Loading