winrt-runtime 2.3.0__tar.gz → 3.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of winrt-runtime might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: winrt-runtime
3
- Version: 2.3.0
3
+ Version: 3.0.0
4
4
  Summary: Python projection of Windows Runtime (WinRT) APIs
5
5
  License: MIT
6
6
  Project-URL: Documentation, https://pywinrt.readthedocs.io
@@ -10,8 +10,9 @@ Classifier: Operating System :: Microsoft :: Windows
10
10
  Classifier: Programming Language :: Python :: 3
11
11
  Classifier: Programming Language :: Python :: Implementation :: CPython
12
12
  Classifier: Intended Audience :: Developers
13
- Requires-Python: <3.14,>=3.9
13
+ Requires-Python: >=3.9
14
14
  Description-Content-Type: text/markdown
15
+ Requires-Dist: typing_extensions>=4.12.2
15
16
 
16
17
  # winrt-runtime
17
18
 
@@ -5,10 +5,81 @@
5
5
  #define PYWINRT_RUNTIME_MODULE
6
6
  #include "pybase.h"
7
7
  #include "pyruntime.h"
8
+ #include "_winrt_box.h"
8
9
  #include <winrt/base.h>
9
10
 
10
11
  namespace py::cpp::_winrt
11
12
  {
13
+ // BEGIN: class _winrt.IInspectable_Static:
14
+
15
+ static PyObject* IInspectable_Static_instancecheck(
16
+ PyObject* cls, PyObject* obj) noexcept
17
+ {
18
+ try
19
+ {
20
+ py::pyobj_handle guid_method{};
21
+
22
+ auto ret = PyObject_GetOptionalAttrString(cls, "_guid_", guid_method.put());
23
+ if (ret == -1)
24
+ {
25
+ return nullptr;
26
+ }
27
+
28
+ if (ret == 0)
29
+ {
30
+ // target class doesn't have a _guid_ attribute, so fall back to
31
+ // base class implementation
32
+ return PyObject_CallMethod(
33
+ reinterpret_cast<PyObject*>(&PyType_Type),
34
+ "__instancecheck__",
35
+ "OO",
36
+ cls,
37
+ obj);
38
+ }
39
+
40
+ py::pyobj_handle guid_obj{PyObject_CallNoArgs(guid_method.get())};
41
+ if (!guid_obj)
42
+ {
43
+ return nullptr;
44
+ }
45
+
46
+ auto guid = py::convert_to<winrt::guid>(guid_obj.get());
47
+ auto instance
48
+ = py::convert_to<winrt::Windows::Foundation::IInspectable>(obj);
49
+ auto interfaces = winrt::get_interfaces(instance);
50
+
51
+ return PyBool_FromLong(
52
+ std::find(interfaces.begin(), interfaces.end(), guid)
53
+ != interfaces.end());
54
+ }
55
+ catch (...)
56
+ {
57
+ py::to_PyErr();
58
+ return nullptr;
59
+ }
60
+ }
61
+
62
+ static PyMethodDef IInspectable_Static_methods[]
63
+ = {{"__instancecheck__",
64
+ reinterpret_cast<PyCFunction>(IInspectable_Static_instancecheck),
65
+ METH_O,
66
+ nullptr},
67
+ {}};
68
+
69
+ static PyType_Slot IInspectable_Static_type_slots[]
70
+ = {{Py_tp_base, reinterpret_cast<void*>(&PyType_Type)},
71
+ {Py_tp_methods, reinterpret_cast<void*>(IInspectable_Static_methods)},
72
+ {}};
73
+
74
+ static PyType_Spec IInspectable_Static_type_spec
75
+ = {"winrt._winrt.IInspectable_Static",
76
+ 0,
77
+ 0,
78
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
79
+ IInspectable_Static_type_slots};
80
+
81
+ // END: class _winrt.IInspectable_Static:
82
+
12
83
  // BEGIN: class _winrt.Object:
13
84
 
14
85
  static constexpr const char* const type_name_Object = "Object";
@@ -72,6 +143,25 @@ namespace py::cpp::_winrt
72
143
  }
73
144
  }
74
145
 
146
+ static PyObject* Object_as(PyObject* self, PyObject* arg) noexcept
147
+ {
148
+ if (reinterpret_cast<PyObject*>(Py_TYPE(self)) == arg)
149
+ {
150
+ return Py_NewRef(self);
151
+ }
152
+
153
+ pyobj_handle from{PyUnicode_InternFromString("_from")};
154
+ if (!from)
155
+ {
156
+ return nullptr;
157
+ }
158
+
159
+ return PyObject_CallMethodOneArg(arg, from.get(), self);
160
+ }
161
+
162
+ static PyMethodDef Object_methods[]
163
+ = {{"as_", reinterpret_cast<PyCFunction>(Object_as), METH_O, nullptr}, {}};
164
+
75
165
  static PyGetSetDef Object_getset[]
76
166
  = {{"_iids_",
77
167
  reinterpret_cast<getter>(Object_iids_get),
@@ -85,11 +175,69 @@ namespace py::cpp::_winrt
85
175
  nullptr},
86
176
  {}};
87
177
 
178
+ static PyObject* Object_richcompare(
179
+ py::winrt_wrapper<winrt::Windows::Foundation::IInspectable>* self,
180
+ PyObject* other,
181
+ int op) noexcept
182
+ {
183
+ if (op == Py_EQ)
184
+ {
185
+ try
186
+ {
187
+ auto other_inspectable
188
+ = py::convert_to<winrt::Windows::Foundation::IInspectable>(other);
189
+
190
+ if (other_inspectable)
191
+ {
192
+ return PyBool_FromLong(self->obj == other_inspectable);
193
+ }
194
+ }
195
+ catch (python_exception)
196
+ {
197
+ PyErr_Clear();
198
+ }
199
+
200
+ Py_RETURN_FALSE;
201
+ }
202
+
203
+ if (op == Py_NE)
204
+ {
205
+ try
206
+ {
207
+ auto other_inspectable
208
+ = py::convert_to<winrt::Windows::Foundation::IInspectable>(other);
209
+
210
+ if (other_inspectable)
211
+ {
212
+ return PyBool_FromLong(self->obj != other_inspectable);
213
+ }
214
+ }
215
+ catch (python_exception)
216
+ {
217
+ PyErr_Clear();
218
+ }
219
+
220
+ Py_RETURN_TRUE;
221
+ }
222
+
223
+ Py_RETURN_NOTIMPLEMENTED;
224
+ }
225
+
226
+ static Py_hash_t Object_hash(
227
+ py::winrt_wrapper<winrt::Windows::Foundation::IInspectable>* self) noexcept
228
+ {
229
+ return static_cast<Py_hash_t>(
230
+ std::hash<winrt::Windows::Foundation::IInspectable>{}(self->obj));
231
+ }
232
+
88
233
  static PyType_Slot Object_type_slots[]
89
234
  = {{Py_tp_doc, const_cast<char*>(Object_doc)},
90
235
  {Py_tp_new, reinterpret_cast<void*>(Object_new)},
91
236
  {Py_tp_dealloc, reinterpret_cast<void*>(Object_dealloc)},
237
+ {Py_tp_methods, reinterpret_cast<void*>(Object_methods)},
92
238
  {Py_tp_getset, reinterpret_cast<void*>(Object_getset)},
239
+ {Py_tp_richcompare, reinterpret_cast<void*>(Object_richcompare)},
240
+ {Py_tp_hash, reinterpret_cast<void*>(Object_hash)},
93
241
  {}};
94
242
 
95
243
  static PyType_Spec Object_type_spec
@@ -150,14 +298,16 @@ namespace py::cpp::_winrt
150
298
  return -1;
151
299
  }
152
300
 
153
- // borrowed ref
154
- auto base_iter = PyTuple_GetItem(args, 0);
155
-
156
- if (!base_iter)
301
+ auto arg_count = PyTuple_GET_SIZE(args);
302
+ if (arg_count != 1)
157
303
  {
304
+ PyErr_SetString(PyExc_TypeError, "requires a single argument");
158
305
  return -1;
159
306
  }
160
307
 
308
+ // borrowed ref
309
+ auto base_iter = PyTuple_GET_ITEM(args, 0);
310
+
161
311
  if (!PyIter_Check(base_iter))
162
312
  {
163
313
  PyErr_SetString(PyExc_TypeError, "expecting an iterator");
@@ -238,12 +388,14 @@ namespace py::cpp::_winrt
238
388
  .abi_version_minor = py::runtime_abi_version_minor,
239
389
  .register_python_type = py::register_python_type,
240
390
  .get_python_type = py::get_python_type,
391
+ .get_struct_from_tuple_func = py::get_struct_from_tuple_func,
241
392
  .wrap_mapping_iter = py::wrap_mapping_iter,
242
393
  .is_buffer_compatible = py::is_buffer_compatible,
243
394
  .convert_datetime = py::convert_datetime,
244
395
  .convert_to_datetime = py::convert_to_datetime,
245
396
  .convert_guid = py::convert_guid,
246
397
  .convert_to_guid = py::convert_to_guid,
398
+ .get_inspectable_meta_type = py::get_inspectable_meta_type,
247
399
  .get_object_type = py::get_object_type,
248
400
  .array_new = py::cpp::_winrt::Array_New,
249
401
  .array_assign = &py::cpp::_winrt::Array_Assign,
@@ -402,6 +554,7 @@ namespace py::cpp::_winrt
402
554
  {
403
555
  auto state = reinterpret_cast<module_state*>(PyModule_GetState(module));
404
556
 
557
+ Py_VISIT(state->inspectable_meta_type);
405
558
  Py_VISIT(state->object_type);
406
559
  Py_VISIT(state->array_type);
407
560
  Py_VISIT(state->mapping_iter_type);
@@ -419,6 +572,7 @@ namespace py::cpp::_winrt
419
572
  {
420
573
  auto state = reinterpret_cast<module_state*>(PyModule_GetState(module));
421
574
 
575
+ Py_CLEAR(state->inspectable_meta_type);
422
576
  Py_CLEAR(state->object_type);
423
577
  Py_CLEAR(state->array_type);
424
578
  Py_CLEAR(state->mapping_iter_type);
@@ -438,6 +592,7 @@ namespace py::cpp::_winrt
438
592
  {
439
593
  auto state = reinterpret_cast<module_state*>(PyModule_GetState(module));
440
594
 
595
+ Py_XDECREF(state->inspectable_meta_type);
441
596
  Py_XDECREF(state->object_type);
442
597
  Py_XDECREF(state->array_type);
443
598
  Py_XDECREF(state->mapping_iter_type);
@@ -449,6 +604,8 @@ namespace py::cpp::_winrt
449
604
  }
450
605
 
451
606
  std::destroy_at(&state->type_cache);
607
+
608
+ std::destroy_at(&state->struct_from_tuple_cache);
452
609
  }
453
610
 
454
611
  PyDoc_STRVAR(module_doc, "_winrt");
@@ -473,6 +630,50 @@ namespace py::cpp::_winrt
473
630
  METH_O,
474
631
  PyDoc_STR(
475
632
  "Removes a directory that was added to the process DLL search path by using _add_dll_directory.")},
633
+ {"box_boolean", box_boolean, METH_O, PyDoc_STR("Box a Boolean value")},
634
+ {"box_char16", box_char16, METH_O, PyDoc_STR("Box a Char16 value")},
635
+ {"box_string", box_string, METH_O, PyDoc_STR("Box a string value")},
636
+ {"box_int8", box_int8, METH_O, PyDoc_STR("Box a Int8 value")},
637
+ {"box_uint8", box_uint8, METH_O, PyDoc_STR("Box a UInt8 value")},
638
+ {"box_int16", box_int16, METH_O, PyDoc_STR("Box a Int16 value")},
639
+ {"box_uint16", box_uint16, METH_O, PyDoc_STR("Box a UInt16 value")},
640
+ {"box_int32", box_int32, METH_O, PyDoc_STR("Box a Int32 value")},
641
+ {"box_uint32", box_uint32, METH_O, PyDoc_STR("Box a UInt32 value")},
642
+ {"box_int64", box_int64, METH_O, PyDoc_STR("Box a Int64 value")},
643
+ {"box_uint64", box_uint64, METH_O, PyDoc_STR("Box a UInt64 value")},
644
+ {"box_single", box_single, METH_O, PyDoc_STR("Box a Single value")},
645
+ {"box_double", box_double, METH_O, PyDoc_STR("Box a Double value")},
646
+ {"box_guid", box_guid, METH_O, PyDoc_STR("Box a GUID value")},
647
+ {"box_date_time",
648
+ box_date_time,
649
+ METH_O,
650
+ PyDoc_STR("Box a Windows.Foundation.DateTime value")},
651
+ {"box_time_span",
652
+ box_time_span,
653
+ METH_O,
654
+ PyDoc_STR("Box a Windows.Foundation.TimeSpan value")},
655
+ {"unbox_boolean", unbox_boolean, METH_O, PyDoc_STR("Unbox a Boolean value")},
656
+ {"unbox_char16", unbox_char16, METH_O, PyDoc_STR("Unbox a Char16 value")},
657
+ {"unbox_string", unbox_string, METH_O, PyDoc_STR("Unbox a string value")},
658
+ {"unbox_int8", unbox_int8, METH_O, PyDoc_STR("Unbox a Int8 value")},
659
+ {"unbox_uint8", unbox_uint8, METH_O, PyDoc_STR("Unbox a UInt8 value")},
660
+ {"unbox_int16", unbox_int16, METH_O, PyDoc_STR("Unbox a Int16 value")},
661
+ {"unbox_uint16", unbox_uint16, METH_O, PyDoc_STR("Unbox a UInt16 value")},
662
+ {"unbox_int32", unbox_int32, METH_O, PyDoc_STR("Unbox a Int32 value")},
663
+ {"unbox_uint32", unbox_uint32, METH_O, PyDoc_STR("Unbox a UInt32 value")},
664
+ {"unbox_int64", unbox_int64, METH_O, PyDoc_STR("Unbox a Int64 value")},
665
+ {"unbox_uint64", unbox_uint64, METH_O, PyDoc_STR("Unbox a UInt64 value")},
666
+ {"unbox_single", unbox_single, METH_O, PyDoc_STR("Unbox a Single value")},
667
+ {"unbox_double", unbox_double, METH_O, PyDoc_STR("Unbox a Double value")},
668
+ {"unbox_guid", unbox_guid, METH_O, PyDoc_STR("Unbox a GUID value")},
669
+ {"unbox_date_time",
670
+ unbox_date_time,
671
+ METH_O,
672
+ PyDoc_STR("Unbox a Windows.Foundation.DateTime value")},
673
+ {"unbox_time_span",
674
+ unbox_time_span,
675
+ METH_O,
676
+ PyDoc_STR("Unbox a Windows.Foundation.TimeSpan value")},
476
677
  {}};
477
678
 
478
679
  static PyModuleDef module_def
@@ -501,6 +702,14 @@ namespace py::cpp::_winrt
501
702
 
502
703
  auto state = reinterpret_cast<module_state*>(PyModule_GetState(module.get()));
503
704
  std::construct_at(&state->type_cache);
705
+ std::construct_at(&state->struct_from_tuple_cache);
706
+
707
+ py::pytype_handle inspectable_meta_type{py::register_python_type(
708
+ module.get(), &IInspectable_Static_type_spec, nullptr, nullptr)};
709
+ if (!inspectable_meta_type)
710
+ {
711
+ return nullptr;
712
+ }
504
713
 
505
714
  py::pytype_handle object_type{py::register_python_type(
506
715
  module.get(), &Object_type_spec, nullptr, nullptr)};
@@ -555,6 +764,7 @@ namespace py::cpp::_winrt
555
764
  return nullptr;
556
765
  }
557
766
 
767
+ state->inspectable_meta_type = inspectable_meta_type.detach();
558
768
  state->object_type = object_type.detach();
559
769
  state->array_type = array_type.detach();
560
770
  state->mapping_iter_type = mapping_iter_type.detach();
@@ -580,6 +790,17 @@ py::cpp::_winrt::module_state* py::cpp::_winrt::get_module_state() noexcept
580
790
  return reinterpret_cast<py::cpp::_winrt::module_state*>(PyModule_GetState(module));
581
791
  }
582
792
 
793
+ PyTypeObject* py::get_inspectable_meta_type() noexcept
794
+ {
795
+ auto state = py::cpp::_winrt::get_module_state();
796
+ if (!state)
797
+ {
798
+ return nullptr;
799
+ }
800
+
801
+ return state->inspectable_meta_type;
802
+ }
803
+
583
804
  PyTypeObject* py::get_object_type() noexcept
584
805
  {
585
806
  auto state = py::cpp::_winrt::get_module_state();