Skip to content

Commit 9f384b5

Browse files
committed
Still more changes
1 parent eb05517 commit 9f384b5

File tree

9 files changed

+21
-114
lines changed

9 files changed

+21
-114
lines changed

native/jpype_module/src/main/java/python/lang/PyExc.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.logging.Logger;
2222
import python.exception.PyException;
2323
import static python.lang.PyExceptionFactory.LOOKUP;
24+
import static python.lang.PyBuiltIn.*;
2425

2526
/**
2627
* Native version of a Python exception.
@@ -38,10 +39,8 @@ public interface PyExc extends PyObject
3839
*/
3940
static Exception of(PyExc base)
4041
{
41-
System.out.println("PyExc.of");
42-
PyType type = base.getType();
42+
PyType type = type(base);
4343
String name = type.getName();
44-
System.out.println("PyExc.of "+name);
4544
Class cls = LOOKUP.get(name);
4645
if (cls == null)
4746
{

native/jpype_module/src/main/java/python/lang/PyJavaObject.java

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@
1616
package python.lang;
1717

1818
import java.util.Objects;
19-
import org.jpype.bridge.Context;
2019
import python.protocol.PyAttributes;
21-
import python.protocol.PyCallable;
22-
import python.protocol.PyMapping;
23-
import python.protocol.PyNumber;
24-
import python.protocol.PySequence;
2520

2621
/**
2722
* Java front end for a Python wrapped Java object.
@@ -37,54 +32,17 @@ public PyJavaObject(Object obj)
3732
}
3833

3934
@Override
40-
public PyAttributes asAttributes()
35+
public PyAttributes getAttributes()
4136
{
4237
// Java objects don't support Python attributes directly.
4338
throw new UnsupportedOperationException();
4439
}
4540

46-
@Override
47-
public PyCallable asCallable()
48-
{
49-
// Java objects don't act as Python functions.
50-
throw new UnsupportedOperationException();
51-
}
52-
53-
@Override
54-
public PyMapping asMapping()
55-
{
56-
throw new UnsupportedOperationException();
57-
}
58-
59-
@Override
60-
public PyNumber asNumber()
61-
{
62-
throw new UnsupportedOperationException();
63-
}
64-
65-
@Override
66-
public PySequence asSequence()
67-
{
68-
// Java objects don't act as Python sequences
69-
throw new UnsupportedOperationException();
70-
}
71-
72-
@Override
73-
public PyObject bytes()
74-
{
75-
throw new UnsupportedOperationException();
76-
}
77-
7841
public Object get()
7942
{
8043
return obj_;
8144
}
8245

83-
@Override
84-
public PyType getType()
85-
{
86-
return Context.type(obj_);
87-
}
8846

8947
@Override
9048
public int hashCode()
@@ -105,10 +63,4 @@ public boolean equals(Object obj)
10563
return Objects.equals(this.obj_, other.obj_);
10664
}
10765

108-
@Override
109-
public boolean isInstance(PyObject cls)
110-
{
111-
throw new UnsupportedOperationException();
112-
}
113-
11466
}

native/jpype_module/src/main/java/python/lang/PyObject.java

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@
1515
*/
1616
package python.lang;
1717

18-
import python.protocol.PyCallable;
19-
import python.protocol.PySequence;
2018
import python.protocol.PyAttributes;
21-
import python.protocol.PyMapping;
22-
import python.protocol.PyNumber;
2319

2420
/**
2521
* PyObject is a representation of a generic object in Python.
2622
*
27-
* PyObjects are very generic and thus must be converted to a protocol using the
28-
* "as" methods. Specific Java like behaviors are implemented on the protocols
29-
* where applicable.
23+
* PyObject when created inherit from multiple Java interfaces called protocols
24+
* based on their duck type behavior. Use `instanceof` and casting to access the
25+
* available behaviors. Specific Java like behaviors are implemented on the
26+
* protocols where applicable.
3027
*
3128
*/
3229
public interface PyObject
@@ -37,61 +34,18 @@ static PyType type()
3734
return (PyType) PyBuiltIn.eval("object", null, null);
3835
}
3936

40-
/**
41-
* Get the type of this object.
42-
*
43-
* Equivalent of type(obj).
44-
*
45-
* @return the object type.
46-
*/
47-
PyType getType();
48-
49-
boolean isInstance(PyObject cls);
50-
5137
/**
5238
* Apply the attributes protocol to this object.
5339
*
5440
* This method never fails.
5541
*
5642
* @return an attribute protocol.
5743
*/
58-
default PyAttributes asAttributes()
44+
default PyAttributes getAttributes()
5945
{
6046
return new PyAttributes(this);
6147
}
6248

63-
/**
64-
* Apply the callable protocol to this object.
65-
*
66-
* The object must be callable for this to succeed.
67-
*
68-
* @return a callable protocol.
69-
*/
70-
PyCallable asCallable();
71-
72-
/**
73-
* Apply the sequence protocol to this object.
74-
*
75-
* @return a sequence protocol.
76-
*/
77-
PySequence asSequence();
78-
79-
/**
80-
* Apply the mapping protocol to this object.
81-
*
82-
* @return a mapping protocol.
83-
*/
84-
PyMapping asMapping();
85-
86-
/**
87-
* Apply the number protocol to this object.
88-
*
89-
* @return a number protocol.
90-
*/
91-
PyNumber asNumber();
92-
93-
PyObject bytes();
94-
9549
@Override
9650
int hashCode();
9751

native/jpype_module/src/main/java/python/lang/PyType.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ static PyType type()
103103
* @param obj The object to check.
104104
* @return True if the object is an instance of this type, false otherwise.
105105
*/
106-
@Override
107106
boolean isInstance(PyObject obj);
108107

109108
/**

native/jpype_module/src/main/java/python/protocol/PyCallable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public CallBuilder arg(Object value)
211211
* @param values is the arguments to add.
212212
* @return this {@link CallBuilder} instance for chaining.
213213
*/
214-
public CallBuilder arg(Object... values)
214+
public CallBuilder args(Object... values)
215215
{
216216
jargs.addAll(Arrays.asList(values));
217217
return this;

native/jpype_module/src/main/java/python/protocol/PyMappingEntrySetIterator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.jpype.bridge.Interpreter;
2424
import python.lang.PyBuiltIn;
2525
import python.lang.PyObject;
26+
import python.lang.PyTuple;
2627

2728
/**
2829
* Iterator implementation for iterating over the entries of a Python mapping.
@@ -163,7 +164,7 @@ public Map.Entry<K, V> next() throws NoSuchElementException
163164
throw new NoSuchElementException();
164165
check = false;
165166
// The yielded object has two members: key and value
166-
PySequence tuple = yield.asSequence();
167+
PyTuple tuple = (PyTuple) yield;
167168
PyObject key = tuple.get(0);
168169
PyObject value = tuple.get(1);
169170
return new Utility.MapEntryWithSet(key, value, setter);

native/jpype_module/src/test/java/python/lang/PyByteArrayNGTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.testng.annotations.BeforeClass;
2222
import org.testng.annotations.Test;
2323
import python.protocol.PyBuffer;
24-
import python.lang.PyBuiltIn;
24+
import static python.lang.PyBuiltIn.*;
2525
/**
2626
*
2727
* @author nelson85
@@ -52,7 +52,7 @@ public void testCreateWithLength()
5252
assertNotNull(instance, "PyByteArray object should not be null");
5353

5454
// Optionally, verify the size of the bytearray (if accessible)
55-
int len2 = PyBuiltIn.len(instance);
55+
int len2 = len(instance);
5656
assertEquals(len2, length, "PyByteArray size should match the specified length");
5757
}
5858

@@ -143,7 +143,7 @@ public void testSize()
143143
PyByteArray instance = PyByteArray.create(10);
144144

145145
// Assert that the size of the PyByteArray matches the specified length
146-
int len2 = PyBuiltIn.len(instance);
146+
int len2 = len(instance);
147147
assertEquals(len2, 10, "PyByteArray size should match the specified length");
148148
}
149149

native/jpype_module/src/test/java/python/lang/PyDictNGTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.testng.annotations.Test;
2626
import static org.testng.Assert.*;
2727
import org.testng.annotations.BeforeClass;
28+
import static python.lang.PyBuiltIn.*;
2829

2930
/**
3031
*
@@ -42,10 +43,10 @@ public static void setUpClass() throws Exception
4243
@Test
4344
public void testPutAndGet()
4445
{
45-
PyDict dict = PyDict.create();
46+
PyDict obj = dict();
4647
PyObject value = PyString.from("value1");
47-
dict.put("key1", value);
48-
assertEquals(dict.get("key1"), value);
48+
obj.put("key1", value);
49+
assertEquals(obj.get("key1"), value);
4950
}
5051

5152
@Test

native/jpype_module/src/test/java/python/lang/PyTypeNGTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.testng.annotations.Test;
2121
import static org.testng.Assert.*;
2222
import org.testng.annotations.BeforeClass;
23+
import static python.lang.PyBuiltIn.*;
2324

2425
/**
2526
*
@@ -70,7 +71,7 @@ public void testIsSubclassOf()
7071
public void testIsInstance()
7172
{
7273
PyObject obj = PyString.from("test");
73-
PyType type = obj.getType();
74+
PyType type = type(obj);
7475
assertTrue(type.isInstance(obj));
7576
assertFalse(type.isInstance(PyDict.type()));
7677
}
@@ -88,7 +89,7 @@ public void testIsAbstract()
8889
Context context = new Context();
8990
context.importModule("collections");
9091
PyObject obj = context.eval("collections.abc.Mapping");
91-
PyType type = obj.getType();
92+
PyType type = type(obj);
9293
assertTrue(type.isAbstract());
9394
PyType concreteType = PyDict.type();
9495
assertFalse(concreteType.isAbstract());

0 commit comments

Comments
 (0)