Skip to content

Commit b9f8b8c

Browse files
ddsharperakillen
andauthored
Allow PyOrderedDict to work in Jython 2.7 (#594)
* allow PyOrderedDict to work in Jython 2.7 * Construct PyList with array for Jython 2.7 compatibility * Expect qualified class name for Jython 2.7 * Use newer version of wlst-test-maven-plugin, compatible with 14.1.1 * Add PyOrderedDict.getValues() method compatible with both Jython versions Co-authored-by: Richard Killen <[email protected]>
1 parent 702011e commit b9f8b8c

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

core/src/main/java/oracle/weblogic/deploy/util/PyOrderedDict.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public final class PyOrderedDict extends PyDictionary implements Iterable<PyObje
4141
* The no-args constructor.
4242
*/
4343
public PyOrderedDict() {
44+
super(PyType.fromClass(PyOrderedDict.class));
4445
this.linkedHashMap = new LinkedHashMap<>();
4546
}
4647

@@ -346,7 +347,7 @@ public PyList items() {
346347
for (Map.Entry<PyObject, PyObject> entry: entries) {
347348
l.add(new PyTuple(new PyObject[] { entry.getKey(), entry.getValue() }));
348349
}
349-
return new PyList(l);
350+
return new PyList(l.toArray(new PyObject[0]));
350351
}
351352

352353
/**
@@ -389,7 +390,7 @@ public PyList keys() {
389390
Set<PyObject> keys = this.linkedHashMap.keySet();
390391
java.util.Vector<PyObject> v = new java.util.Vector<>(keys.size());
391392
v.addAll(keys);
392-
return new PyList(v);
393+
return new PyList(v.toArray(new PyObject[0]));
393394
}
394395

395396
/**
@@ -454,13 +455,26 @@ public void update(PyObject od) {
454455

455456
/**
456457
* {@inheritDoc}
458+
* This method is not supported, as it is not portable between versions of Jython.
459+
* Jython 2.1 returns PyList, Jython 2.7 returns a Collection.
460+
* WDT code should use getValues() to get ordered values.
457461
*/
458462
@Override
459463
public PyList values() {
464+
String message = ExceptionHelper.getMessage("WLSDPLY-01252");
465+
throw new UnsupportedOperationException(message);
466+
}
467+
468+
/**
469+
* Because return type for values() is different between Jython 2.1 and Jython 2.7,
470+
* WDT code should call this variant to get a list of values.
471+
* @return an ordered list of values
472+
*/
473+
public PyList getValues() {
460474
Collection<PyObject> values = this.linkedHashMap.values();
461475
java.util.Vector<PyObject> v = new java.util.Vector<>(values.size());
462476
v.addAll(values);
463-
return new PyList(v);
477+
return new PyList(v.toArray(new PyObject[0]));
464478
}
465479

466480
// private methods
@@ -518,6 +532,8 @@ private static PyObject doDeepCopy(PyObject orig, PyObject memo) {
518532
break;
519533

520534
case "PyOrderedDict":
535+
case "oracle.weblogic.deploy.util.PyOrderedDict":
536+
// Jython 2.7 uses qualified class name
521537
PyOrderedDict origOrderedDict = PyOrderedDict.class.cast(orig);
522538
result = origOrderedDict.__deepcopy__(memo);
523539
break;

core/src/main/resources/oracle/weblogic/deploy/messages/wlsdeploy_rb.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ WLSDPLY-01208=Process for command {0} drainer thread failed: {1}
150150
# oracle.weblogic.deploy.util.PyOrderedDict.java
151151
WLSDPLY-01250="The memo argument was an instance of class {0} instead of an instance of class {1}"
152152
WLSDPLY-01251=While doing deepcopy of a PyOrderedDict, encountered unexpected type {0} that will not be copied
153+
WLSDPLY-01252=Use PyOrderedDict.getValues() instead of .values() for Jython version portability
153154

154155
# oracle.weblogic.deploy.util.ScriptRunner.java
155156
WLSDPLY-01300=Executing {0}: {1}

core/src/test/python/collections_test.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright (c) 2017, 2019, Oracle Corporation and/or its affiliates. All rights reserved.
2+
Copyright (c) 2017, 2020, Oracle Corporation and/or its affiliates. All rights reserved.
33
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
"""
55
import copy
@@ -42,7 +42,7 @@ def testValues(self):
4242
my_ordered_dict['nba_finals_years'] = ['2015', '2016', '2017']
4343

4444
expected_values = ['Steph Curry', 1, False, ['2015', '2016', '2017']]
45-
values = my_ordered_dict.values()
45+
values = my_ordered_dict.getValues()
4646
self.assertEqual(len(expected_values), len(values),
4747
'expected the ordered dict values to be the same length as the expected values')
4848
for i, value in enumerate(values):
@@ -165,8 +165,6 @@ def testOrderedDictOrderedDictUpdate(self):
165165
dict1['entry1'] = 'you'
166166
dict2 = OrderedDict()
167167
dict2['entry2'] = 'me'
168-
print 'Before: OrderedDict() dict1=', dict1
169-
print ' OrderedDict() dict2=', dict2
170168
dict1.update(dict2)
171169
self.assertEqual('entry1' in dict1 and 'entry2' in dict1, True,
172170
"expected ordereddict1 to contain 'entry1' and 'entry2' keys")
@@ -176,9 +174,6 @@ def testOrderedDictDictUpdate(self):
176174
dict1['entry1'] = 'you'
177175
dict2 = dict()
178176
dict2['entry2'] = 'me'
179-
print 'Before: OrderedDict() dict1=', dict1
180-
print ' dict() dict2=', dict2
181-
print
182177
dict1.update(dict2)
183178
self.assertEqual('entry1' in dict1 and 'entry2' in dict1, True,
184179
"expected ordereddict1 to contain 'entry1' and 'entry2' keys")
@@ -188,9 +183,6 @@ def testDictOrderedDictUpdate(self):
188183
dict1['entry1'] = 'you'
189184
dict2 = OrderedDict()
190185
dict2['entry2'] = 'me'
191-
print 'Before: dict() dict1=', dict1
192-
print ' OrderedDict() dict2=', dict2
193-
print
194186
dict1.update(dict2)
195187
self.assertEqual('entry1' in dict1 and 'entry2' in dict1, True,
196188
"expected ordereddict1 to contain 'entry1' and 'entry2' keys")
@@ -212,7 +204,7 @@ def testOrderedDictSort(self):
212204
for key in sorted_keys:
213205
sorted_dict[key] = dict1[key]
214206

215-
self.assertEqual(sorted_dict.values()[-1], '8100',
207+
self.assertEqual(sorted_dict.getValues()[-1], '8100',
216208
'expected dictionary to be sorted by name')
217209

218210

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
3-
Copyright (c) 2017, 2019, Oracle Corporation and/or its affiliates. All rights reserved.
3+
Copyright (c) 2017, 2020, Oracle Corporation and/or its affiliates. All rights reserved.
44
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
55
-->
66
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
@@ -119,7 +119,7 @@
119119
<plugin>
120120
<groupId>io.rhpatrick.mojo</groupId>
121121
<artifactId>wlst-test-maven-plugin</artifactId>
122-
<version>1.0.3</version>
122+
<version>1.0.4</version>
123123
</plugin>
124124
<plugin>
125125
<groupId>org.antlr</groupId>

0 commit comments

Comments
 (0)