Skip to content

Commit 14ec799

Browse files
committed
[GR-10393] make test weakset run
PullRequest: graalpython/83
2 parents bc10b90 + d8e1ab7 commit 14ec799

File tree

12 files changed

+249
-51
lines changed

12 files changed

+249
-51
lines changed

graalpython/com.oracle.graal.python.test/src/python_unittests.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@
5454
# constants
5555
PATH_UNITTESTS = "graalpython/lib-python/3/test/"
5656

57-
CSV_RESULTS_NAME = "unittests.csv"
58-
HTML_RESULTS_NAME = "unittests.html"
57+
_BASE_NAME = "unittests"
58+
TXT_RESULTS_NAME = "{}.txt".format(_BASE_NAME)
59+
CSV_RESULTS_NAME = "{}.csv".format(_BASE_NAME)
60+
HTML_RESULTS_NAME = "{}.html".format(_BASE_NAME)
61+
5962

6063
PTRN_ERROR = re.compile(r'^(?P<error>[A-Z][a-z][a-zA-Z]+):(?P<message>.*)$')
6164
PTRN_UNITTEST = re.compile(r'^#### running: graalpython/lib-python/3/test/(?P<unittest>.*)$')
@@ -117,7 +120,7 @@ def scp(results_file_path, destination_path, destination_name=None):
117120

118121

119122
def _run_unittest(test_path):
120-
cmd = ["mx", "python3", "--python.CatchAllExceptions=true", test_path]
123+
cmd = ["mx", "python3", "--python.CatchAllExceptions=true", test_path, "-v"]
121124
success, output = _run_cmd(cmd)
122125
output = '''
123126
##############################################################
@@ -278,6 +281,13 @@ class Stat(object):
278281
TEST_PERCENT_PASS = "test_percent_pass" # percentage of tests which pass from all running tests (all unittests)
279282

280283

284+
def save_as_txt(report_path, results):
285+
with open(report_path, 'w') as TXT:
286+
output = '\n'.join(results)
287+
TXT.write(output)
288+
return output
289+
290+
281291
def save_as_csv(report_path, unittests, error_messages, stats, current_date):
282292
rows = []
283293
with open(report_path, 'w') as CSV:
@@ -595,7 +605,10 @@ def _fmt(t):
595605
unittests = get_unittests(flags.tests_path, limit=flags.limit)
596606

597607
results = run_unittests(unittests)
598-
unittests, error_messages, stats = process_output('\n'.join(results))
608+
txt_report_path = file_name(TXT_RESULTS_NAME, current_date)
609+
output = save_as_txt(txt_report_path, results)
610+
611+
unittests, error_messages, stats = process_output(output)
599612

600613
csv_report_path = file_name(CSV_RESULTS_NAME, current_date)
601614
rows, totals = save_as_csv(csv_report_path, unittests, error_messages, stats, current_date)
@@ -608,6 +621,7 @@ def _fmt(t):
608621

609622
if flags.path:
610623
log("[SAVE] saving results to {} ... ", flags.path)
624+
scp(txt_report_path, flags.path)
611625
scp(csv_report_path, flags.path)
612626
scp(html_report_path, flags.path)
613627

graalpython/com.oracle.graal.python.test/src/tests/test_pyio.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,7 @@ def test_builtin_open():
170170
except Exception as e:
171171
print(e)
172172
success = False
173+
finally:
174+
unlink(file_name)
173175

174176
assert success

graalpython/com.oracle.graal.python.test/src/tests/test_set.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@
3939
# Iterating by Sequence Index
4040

4141

42+
def assert_raises(err, fn, *args, **kwargs):
43+
raised = False
44+
try:
45+
fn(*args, **kwargs)
46+
except err:
47+
raised = True
48+
assert raised
49+
50+
51+
class PassThru(Exception):
52+
pass
53+
54+
55+
def check_pass_thru():
56+
raise PassThru
57+
yield 1
58+
59+
4260
def test_set_or():
4361
s1 = {1, 2, 3}
4462
s2 = {4, 5, 6}
@@ -65,3 +83,64 @@ def test_set_remove():
6583
def test_set_le():
6684
assert set("a") <= set("abc")
6785

86+
87+
def test_difference():
88+
word = 'simsalabim'
89+
otherword = 'madagascar'
90+
letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
91+
s = set(word)
92+
d = dict.fromkeys(word)
93+
94+
i = s.difference(otherword)
95+
for c in letters:
96+
assert (c in i) == (c in d and c not in otherword)
97+
98+
assert s == set(word)
99+
assert type(i) == set
100+
assert_raises(PassThru, s.difference, check_pass_thru())
101+
assert_raises(TypeError, s.difference, [[]])
102+
103+
for C in set, frozenset, dict.fromkeys, str, list, tuple:
104+
assert set('abcba').difference(C('cdc')) == set('ab')
105+
assert set('abcba').difference(C('efgfe')) == set('abc')
106+
assert set('abcba').difference(C('ccb')) == set('a')
107+
assert set('abcba').difference(C('ef')) == set('abc')
108+
assert set('abcba').difference() == set('abc')
109+
assert set('abcba').difference(C('a'), C('b')) == set('c')
110+
111+
112+
def test_difference_update():
113+
word = 'simsalabim'
114+
otherword = 'madagascar'
115+
s = set(word)
116+
117+
retval = s.difference_update(otherword)
118+
assert retval == None
119+
120+
for c in (word + otherword):
121+
if c in word and c not in otherword:
122+
assert c in s
123+
else:
124+
assert c not in s
125+
126+
assert_raises(PassThru, s.difference_update, check_pass_thru())
127+
assert_raises(TypeError, s.difference_update, [[]])
128+
# assert_raises(TypeError, s.symmetric_difference_update, [[]])
129+
130+
for p, q in (('cdc', 'ab'), ('efgfe', 'abc'), ('ccb', 'a'), ('ef', 'abc')):
131+
for C in set, frozenset, dict.fromkeys, str, list, tuple:
132+
s = set('abcba')
133+
assert s.difference_update(C(p)) == None
134+
assert s == set(q)
135+
136+
s = set('abcdefghih')
137+
s.difference_update()
138+
assert s == set('abcdefghih')
139+
140+
s = set('abcdefghih')
141+
s.difference_update(C('aba'))
142+
assert s == set('cdefghih')
143+
144+
s = set('abcdefghih')
145+
s.difference_update(C('cdc'), C('aba'))
146+
assert s == set('efghih')

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/WeakRefModuleBuiltins.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@
4848
import com.oracle.graal.python.builtins.objects.function.PFunction;
4949
import com.oracle.graal.python.builtins.objects.object.PythonObject;
5050
import com.oracle.graal.python.builtins.objects.referencetype.PReferenceType;
51+
import com.oracle.graal.python.builtins.objects.type.PythonClass;
5152
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
53+
import com.oracle.graal.python.runtime.exception.PythonErrorType;
54+
import com.oracle.truffle.api.dsl.Fallback;
5255
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
5356
import com.oracle.truffle.api.dsl.NodeFactory;
5457
import com.oracle.truffle.api.dsl.Specialization;
@@ -65,15 +68,19 @@ protected List<? extends NodeFactory<? extends PythonBuiltinNode>> getNodeFactor
6568
@Builtin(name = "ReferenceType", minNumOfArguments = 2, maxNumOfArguments = 3, constructsClass = PReferenceType.class)
6669
@GenerateNodeFactory
6770
public abstract static class ReferenceTypeNode extends PythonBuiltinNode {
68-
6971
@Specialization
70-
public PReferenceType refType(Object cls, PythonObject pythonObject, PNone none) {
71-
return factory().createReferenceType(pythonObject, null);
72+
public PReferenceType refType(PythonClass cls, PythonObject pythonObject, PNone none) {
73+
return factory().createReferenceType(cls, pythonObject, null);
7274
}
7375

7476
@Specialization
75-
public PReferenceType refType(Object cls, PythonObject pythonObject, PFunction callback) {
76-
return factory().createReferenceType(pythonObject, callback);
77+
public PReferenceType refType(PythonClass cls, PythonObject pythonObject, PFunction callback) {
78+
return factory().createReferenceType(cls, pythonObject, callback);
79+
}
80+
81+
@Fallback
82+
public PReferenceType refType(Object cls, Object object, Object callback) {
83+
throw raise(PythonErrorType.TypeError, "cannot create weak reference to '%p' object", object);
7784
}
7885
}
7986

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/referencetype/PReferenceType.java

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,10 @@
3838
*/
3939
package com.oracle.graal.python.builtins.objects.referencetype;
4040

41-
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
42-
4341
import java.lang.ref.WeakReference;
4442

45-
import com.oracle.graal.python.PythonLanguage;
4643
import com.oracle.graal.python.builtins.objects.PNone;
44+
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
4745
import com.oracle.graal.python.builtins.objects.function.PArguments;
4846
import com.oracle.graal.python.builtins.objects.function.PFunction;
4947
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
@@ -89,54 +87,44 @@ protected void finalize() throws Throwable {
8987
}
9088

9189
private final WeakRefStorage store;
90+
private int hash = -1;
9291

9392
@TruffleBoundary
9493
public PReferenceType(PythonClass cls, PythonObject pythonObject, PFunction callback) {
9594
super(cls);
9695
this.store = new WeakRefStorage(this, pythonObject, callback);
9796
}
9897

99-
public Object __callback__() {
98+
public Object getCallback() {
10099
if (this.store.callback == null) {
101100
return PNone.NONE;
102101
}
103102
return this.store.callback;
104103
}
105104

106105
@TruffleBoundary
107-
public Object __call__() {
108-
Object referent = this.store.get();
109-
return (referent == null) ? PNone.NONE : referent;
106+
public PythonObject getObject() {
107+
return this.store.get();
110108
}
111109

112-
@TruffleBoundary
113-
public int getWeakRefCount() {
114-
return (this.store.get() == null) ? 0 : 1;
110+
public PythonAbstractObject getPyObject() {
111+
PythonObject object = getObject();
112+
return (object == null) ? PNone.NONE : object;
115113
}
116114

117-
@Override
118-
public String toString() {
119-
return "<" + pythonClass.getName() + " object at " + hashCode() + ">";
115+
public int getWeakRefCount() {
116+
return (this.getObject() == null) ? 0 : 1;
120117
}
121118

122-
@Override
123-
@TruffleBoundary
124-
public int hashCode() {
125-
PythonObject referent = store.get();
126-
if (referent != null) {
127-
return referent.hashCode();
119+
public int getHash() {
120+
if (this.hash != -1) {
121+
return this.hash;
128122
}
129-
throw PythonLanguage.getCore().raise(TypeError);
130-
}
131123

132-
@Override
133-
@TruffleBoundary
134-
public boolean equals(Object obj) {
135-
PythonObject referent = store.get();
136-
if (referent != null) {
137-
return referent.equals(obj);
124+
PythonObject object = getObject();
125+
if (object != null) {
126+
this.hash = object.hashCode();
138127
}
139-
throw PythonLanguage.getCore().raise(TypeError);
128+
return this.hash;
140129
}
141-
142130
}

0 commit comments

Comments
 (0)