Hi,
for info, (in addition to overloading resolution issues) methods with template Parameters doesn’t work with PythonNet 2.5.x (seem fixed in PythonNet 3.x.x)
Hi folks,
I’m having trouble using method overloading when using the CPython engine in Dynamo 2.10
Please check the following code to create a schema and place an instance of it in an element in the model.
It works just fine in IronPython but crashes in CPython (I skip the import block for brevity):
component = UnwrapElement(IN[0])
TransactionManager.Instance.EnsureInTransaction(doc)
def create_schema():
schema_guid = System.Guid("DF3BBCC1-4D4D-4A01-B444-F9722814F9CE")
schema_build…
opened 11:57PM - 19 Aug 21 UTC
closed 06:57PM - 05 Jan 22 UTC
bug
### Environment
- Pythonnet version: 3.0.0.dev1
- Python version: 3.9.6 … (also tried on 3.7)
- Operating System: Ubuntu 20.04
- .NET Runtime: mono-devel (Mono JIT compiler version 6.8.0.105)
- Open XML SDK 2.13.0 (from https://www.nuget.org/packages/DocumentFormat.OpenXml/)
### Details
Trying to re-implement example from [here](https://docs.microsoft.com/en-us/office/open-xml/how-to-create-a-presentation-document-by-providing-a-file-name) using Python.
Everything works fine except this line of code:
```python
slideMasterPart1.AddPart(slideLayoutPart1, "rId1");
````
There are two definitions of `AddPart`:
```csharp
public virtual T AddPart<T>(T part) where T : OpenXmlPart
public virtual T AddPart<T>(T part, string id) where T : OpenXmlPart
```
` slideMasterPart1.AddPart.Overloads` output:
```
T AddPart[T](T)
T AddPart[T](T, System.String)
```
What works:
```python
slideMasterPart1.AddPart(slideLayoutPart1)
slideMasterPart1.AddPart[SlideLayoutPart](slideLayoutPart1)
````
What doesn't work:
```python
slideMasterPart1.AddPart(slideLayoutPart1, "rId1")
slideMasterPart1.AddPart(slideLayoutPart1, System.String("rId1"))
slideMasterPart1.AddPart[SlideLayoutPart](slideLayoutPart1, "rId1")
````
Error:
```
Python.Runtime.PythonException: DocumentFormat.OpenXml.Packaging.SlideLayoutPart value cannot be converted to T
The above exception was the direct cause of the following exception:
System.ArgumentException: DocumentFormat.OpenXml.Packaging.SlideLayoutPart value cannot be converted to T in method T AddPart[T](T, System.String) ---> Python.Runtime.PythonException: DocumentFormat.OpenXml.Packaging.SlideLayoutPart value cannot be converted to T
--- End of inner exception stack trace ---
The above exception was the direct cause of the following exception:
System.AggregateException: One or more errors occurred. (DocumentFormat.OpenXml.Packaging.SlideLayoutPart value cannot be converted to T in method T AddPart[T](T, System.String)) ---> System.ArgumentException: DocumentFormat.OpenXml.Packaging.SlideLayoutPart value cannot be converted to T in method T AddPart[T](T, System.String) ---> Python.Runtime.PythonException: DocumentFormat.OpenXml.Packaging.SlideLayoutPart value cannot be converted to T
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.ArgumentException: DocumentFormat.OpenXml.Packaging.SlideLayoutPart value cannot be converted to T in method T AddPart[T](T, System.String) ---> Python.Runtime.PythonException: DocumentFormat.OpenXml.Packaging.SlideLayoutPart value cannot be converted to T
--- End of inner exception stack trace ---<---
The above exception was the direct cause of the following exception:
...
slideMasterPart1.AddPart(slideLayoutPart1, "rId1");
TypeError: No method matches given arguments for AddPart: (<class 'DocumentFormat.OpenXml.Packaging.SlideLayoutPart'>, <class 'str'>)
```
mcve:
```python
import clr
clr.AddReference('DocumentFormat.OpenXml')
from DocumentFormat.OpenXml.Packaging import (
SlideLayoutPart,
SlideMasterPart,
)
slideLayoutPart1: SlideLayoutPart = SlideLayoutPart()
slideMasterPart1: SlideMasterPart = SlideMasterPart()
slideMasterPart1.AddPart(slideLayoutPart1) # this works OK
slideMasterPart1.AddPart(slideLayoutPart1, "rId1") # this raises a runtime exception
```
So it's only when the second (string) parameter is provided, the first can't be converted. Looks like a pythonnet issue.
you can use IronPython (2 or 3) instead
1 Like