Skip to content

Commit e8179b4

Browse files
committed
HPy tests for JNI fast paths in Get/SetItem_x
1 parent 6f4235c commit e8179b4

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/GraalHPyContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1761,8 +1761,9 @@ public final int ctxSetItems(long receiverHandle, String name, long valueHandle)
17611761
HPyRaiseNode.raiseIntUncached(this, -1, SystemError, ErrorMessages.HPY_UNEXPECTED_HPY_NULL);
17621762
return -1;
17631763
}
1764+
TruffleString tsName = toTruffleStringUncached(name);
17641765
try (UncachedAcquire gil = GilNode.uncachedAcquire()) {
1765-
PyObjectSetItem.getUncached().execute(null, receiver, name, value);
1766+
PyObjectSetItem.getUncached().execute(null, receiver, tsName, value);
17661767
return 0;
17671768
} catch (PException e) {
17681769
HPyTransformExceptionToNativeNode.executeUncached(this, e);

graalpython/lib-graalpython/modules/hpy/test/test_object.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
to be able to use e.g. pytest.raises (which on PyPy will be implemented by a
3030
"fake pytest module")
3131
"""
32+
import math
33+
3234
from .support import HPyTest
3335

3436

@@ -339,6 +341,12 @@ def foo(self, value):
339341
mod.f(b)
340342
assert b.foo is True
341343

344+
def check_subscript_type_error(self, fun):
345+
import pytest
346+
for obj in [42, 3.14, None]:
347+
with pytest.raises(TypeError):
348+
fun(obj)
349+
342350
def test_getitem(self):
343351
import pytest
344352
mod = self.make_module("""
@@ -359,6 +367,9 @@ def test_getitem(self):
359367
@INIT
360368
""")
361369
assert mod.f({3: "hello"}) == "hello"
370+
assert mod.f({3: 42}) == 42
371+
assert mod.f({3: 0.5}) == 0.5
372+
assert math.isnan(mod.f({3: math.nan}))
362373
with pytest.raises(KeyError) as exc:
363374
mod.f({1: "bad"})
364375
assert exc.value.args == (3,)
@@ -367,6 +378,8 @@ def test_getitem(self):
367378
with pytest.raises(IndexError):
368379
mod.f([])
369380

381+
self.check_subscript_type_error(mod.f)
382+
370383
def test_getitem_i(self):
371384
import pytest
372385
mod = self.make_module("""
@@ -383,6 +396,9 @@ def test_getitem_i(self):
383396
@INIT
384397
""")
385398
assert mod.f({3: "hello"}) == "hello"
399+
assert mod.f({3: 42}) == 42
400+
assert mod.f({3: 0.5}) == 0.5
401+
assert math.isnan(mod.f({3: math.nan}))
386402
with pytest.raises(KeyError) as exc:
387403
mod.f({1: "bad"})
388404
assert exc.value.args == (3,)
@@ -391,6 +407,8 @@ def test_getitem_i(self):
391407
with pytest.raises(IndexError):
392408
mod.f([])
393409

410+
self.check_subscript_type_error(mod.f)
411+
394412
def test_getitem_s(self):
395413
import pytest
396414
mod = self.make_module("""
@@ -407,13 +425,18 @@ def test_getitem_s(self):
407425
@INIT
408426
""")
409427
assert mod.f({"limes": "hello"}) == "hello"
428+
assert mod.f({"limes": 42}) == 42
429+
assert mod.f({"limes": 0.5}) == 0.5
430+
assert math.isnan(mod.f({"limes": math.nan}))
410431
with pytest.raises(KeyError) as exc:
411432
mod.f({"oranges": "bad"})
412433
assert exc.value.args == ("limes",)
413434

414435
with pytest.raises(TypeError):
415436
mod.f([])
416437

438+
self.check_subscript_type_error(mod.f)
439+
417440
def test_setitem(self):
418441
import pytest
419442
mod = self.make_module("""
@@ -442,6 +465,9 @@ def test_setitem(self):
442465
with pytest.raises(IndexError):
443466
mod.f([])
444467

468+
self.check_subscript_type_error(mod.f)
469+
470+
445471
def test_setitem_i(self):
446472
import pytest
447473
mod = self.make_module("""
@@ -465,6 +491,8 @@ def test_setitem_i(self):
465491
with pytest.raises(IndexError):
466492
mod.f([])
467493

494+
self.check_subscript_type_error(mod.f)
495+
468496
def test_setitem_s(self):
469497
import pytest
470498
mod = self.make_module("""
@@ -487,6 +515,8 @@ def test_setitem_s(self):
487515
with pytest.raises(TypeError):
488516
mod.f([])
489517

518+
self.check_subscript_type_error(mod.f)
519+
490520
def test_length(self):
491521
mod = self.make_module("""
492522
HPyDef_METH(f, "f", f_impl, HPyFunc_O)

0 commit comments

Comments
 (0)