Skip to content
This repository was archived by the owner on Aug 5, 2021. It is now read-only.
This repository was archived by the owner on Aug 5, 2021. It is now read-only.

Cannot iterate through a dict in Python 3.x #136

@Dr-Irv

Description

@Dr-Irv

I'm using Python 3.6. The following code fails in Java:

import org.jpy.PyInputMode;
import org.jpy.PyLib;
import org.jpy.PyObject;

import java.util.HashMap;
import java.util.Map;

public class MyTest
{
	public static void main(String[] args) {
		PyLib.startPython("");
		HashMap<String, Object> localMap = new HashMap<>();
		PyObject.executeCode("d = { 10 : 1 , 20 : 2}", PyInputMode.STATEMENT, null, localMap);
		Map<PyObject, PyObject> dict = ((PyObject)localMap.get("d")).asDict();
		
		for (Map.Entry<PyObject, PyObject> entry : dict.entrySet()) {
			int key = entry.getKey().getIntValue();
			int val = entry.getValue().getIntValue();
			System.out.println("key "+key+" val "+val);
		}
	}
}

The error is:

Exception in thread "main" java.lang.RuntimeException: Error in Python interpreter:
Type: <class 'AttributeError'>
Value: 'dict_keyiterator' object has no attribute 'next'
Line: <not available>
Namespace: <not available>
File: <not available>
	at org.jpy.PyLib.callAndReturnObject(Native Method)
	at org.jpy.PyObject.callMethod(PyObject.java:374)
	at org.jpy.PyDictWrapper$EntrySet$1.prepareNext(PyDictWrapper.java:176)
	at org.jpy.PyDictWrapper$EntrySet$1.<init>(PyDictWrapper.java:172)
	at org.jpy.PyDictWrapper$EntrySet.iterator(PyDictWrapper.java:170)
	at Globals.main(Globals.java:16)

Note that the Maven tests do pick this error up. I have a fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions