Skip to content

Cannot convert to managed type #1900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Tiehong opened this issue Aug 8, 2022 · 3 comments
Closed

Cannot convert to managed type #1900

Tiehong opened this issue Aug 8, 2022 · 3 comments

Comments

@Tiehong
Copy link

Tiehong commented Aug 8, 2022

Environment

  • Pythonnet version: 3.0.0-rc4
  • Python version: 3.8
  • Operating System: Windows11
  • .NET Runtime: .NET Framework 4.8

Details

With the following code in C#, when a Python class has getitem() defined, it will be converted to an array (System.Object[]) in managed code:

List lst = new List();
using (Py.GIL())
{
using (var scope = Py.CreateScope())
{
scope.Set("lst", lst);
string text = @"
class DBRow:
def init(self, row, col_names):
self.row = row
self.col_names = col_names

def __getitem__(self, key):   
    if isinstance(key, int):
        return self.row[key]

    if key in self.col_names:
        index = self.col_names.index(key)
        return self.row[index]
    else:
        return None 

col_names=['col1', 'col2', 'col3']
row=[1, '2', 3]

r = DBRow(row, col_names)
lst.Add(r)
";

                    var code = PythonEngine.Compile(text);
                    scope.Execute(code);
                }
            }
            string str = lst[0].GetType().ToString();

@lostmsu
Copy link
Member

lostmsu commented Aug 8, 2022

Can you clarify? The bug is that str variable ends up being System.Object[], and you expect it to be PyObject?

lostmsu added a commit to losttech/pythonnet that referenced this issue Aug 8, 2022
…ng sequence protocol to

.NET arrays when the target .NET type is `System.Object`. The conversion is still attempted when the
target type is a `System.Array`

fixes pythonnet#1900
filmor pushed a commit that referenced this issue Aug 8, 2022
…ng sequence protocol to (#1902)

.NET arrays when the target .NET type is `System.Object`. The conversion is still attempted when the
target type is a `System.Array`

fixes #1900
@Tiehong
Copy link
Author

Tiehong commented Aug 9, 2022

Yes, @lostmsu. I expect it to be PyObject. Then I can view the class members/properties in the C# world. System.Object[] does not allow me to do that.

@lostmsu
Copy link
Member

lostmsu commented Sep 16, 2022

I believe this should be fixed now.

@lostmsu lostmsu closed this as completed Sep 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants