pyodide icon indicating copy to clipboard operation
pyodide copied to clipboard

Changed to sequence check

Open MatthewAndreTaylor opened this issue 2 years ago • 2 comments

Changed _python2js_deep

Sequence types for example numpy arrays of objects are treated as buffers. Sequences in Python are Iterable and have len (sq_length) and get (sq_item). By checking that the PyObject* passed in is not a dictionary first only valid sequences will be converted as expected.

// One might consider trying to convert things that satisfy PyMapping_Check to // maps and things that satisfy PySequence_Check to lists. However // PyMapping_Check "returns 1 for Python classes with a __getitem__() method" // and PySequence_Check returns 1 for classes with a __getitem__ method that // don't subclass dict. For this reason, I think we should stick to subclasses.

However, by only checking PySequence_Check and checking PyDict_Check before ensures this is not the case.

Addresses issue: https://github.com/pyodide/pyodide/issues/4051

MatthewAndreTaylor avatar Nov 01 '23 01:11 MatthewAndreTaylor

This is an interesting suggestion. I wonder why I originally put in the stricter check, I'll try and look at the git blame and see if we discussed this. We'll need a test and a change log entry.

hoodmane avatar Nov 01 '23 03:11 hoodmane

Thanks @MatthewAndreTaylor!

hoodmane avatar Nov 01 '23 03:11 hoodmane