Skip to content

Commit 405c2f1

Browse files
committed
Add byte and short converters utilizing hints
And test them, to improve confidence that conversion hints work.
1 parent 07d13bd commit 405c2f1

2 files changed

Lines changed: 40 additions & 13 deletions

File tree

src/scyjava/_convert.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,33 @@ def _stock_java_converters() -> List[Converter]:
163163
predicate=lambda obj: isinstance(obj, bool),
164164
converter=_jc.Boolean,
165165
),
166+
# int -> java.lang.Byte
167+
Converter(
168+
predicate=lambda obj, **hints: isinstance(obj, int)
169+
and ("type" in hints and hints["type"] in ("b", "byte", "Byte"))
170+
and _jc.Byte.MIN_VALUE <= obj <= _jc.Byte.MAX_VALUE,
171+
converter=_jc.Byte,
172+
priority=Priority.HIGH,
173+
),
174+
# int -> java.lang.Short
175+
Converter(
176+
predicate=lambda obj, **hints: isinstance(obj, int)
177+
and ("type" in hints and hints["type"] in ("s", "short", "Short"))
178+
and _jc.Short.MIN_VALUE <= obj <= _jc.Short.MAX_VALUE,
179+
converter=_jc.Short,
180+
priority=Priority.HIGH,
181+
),
166182
# int -> java.lang.Integer
167183
Converter(
168-
predicate=lambda obj: isinstance(obj, int)
184+
predicate=lambda obj, **hints: isinstance(obj, int)
185+
and ("type" not in hints or hints["type"] in ("i", "int", "Integer"))
169186
and _jc.Integer.MIN_VALUE <= obj <= _jc.Integer.MAX_VALUE,
170187
converter=_jc.Integer,
171188
),
172189
# int -> java.lang.Long
173190
Converter(
174-
predicate=lambda obj: isinstance(obj, int)
191+
predicate=lambda obj, **hints: isinstance(obj, int)
192+
and ("type" not in hints or hints["type"] in ("j", "l", "long", "Long"))
175193
and _jc.Long.MIN_VALUE <= obj <= _jc.Long.MAX_VALUE,
176194
converter=_jc.Long,
177195
priority=Priority.NORMAL - 1,
@@ -184,7 +202,8 @@ def _stock_java_converters() -> List[Converter]:
184202
),
185203
# float -> java.lang.Float
186204
Converter(
187-
predicate=lambda obj: isinstance(obj, float)
205+
predicate=lambda obj, **hints: isinstance(obj, float)
206+
and ("type" not in hints or hints["type"] in ("f", "float", "Float"))
188207
and (
189208
math.isinf(obj)
190209
or math.isnan(obj)
@@ -194,7 +213,8 @@ def _stock_java_converters() -> List[Converter]:
194213
),
195214
# float -> java.lang.Double
196215
Converter(
197-
predicate=lambda obj: isinstance(obj, float)
216+
predicate=lambda obj, **hints: isinstance(obj, float)
217+
and ("type" not in hints or hints["type"] in ("d", "double", "Double"))
198218
and (
199219
math.isinf(obj)
200220
or math.isnan(obj)

tests/test_convert.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
from os import getcwd
33
from pathlib import Path
44

5-
from jpype import JByte
6-
75
from scyjava import (
86
Converter,
97
config,
@@ -60,13 +58,22 @@ def testBoolean(self):
6058
assert pfalse is False
6159

6260
def testByte(self):
63-
# NB we can't (yet) convert TO Bytes, since there is not (yet)
64-
# a great type to convert FROM. We convert python ints to Integers
65-
i = 5
66-
ji = JByte(i)
67-
pi = to_python(ji)
68-
assert i == pi
69-
assert str(i) == str(pi)
61+
obyte = 5
62+
jbyte = to_java(obyte, type="b")
63+
assert jinstance(jbyte, "java.lang.Byte")
64+
assert obyte == jbyte.byteValue()
65+
pbyte = to_python(jbyte)
66+
assert isinstance(pbyte, int)
67+
assert obyte == pbyte
68+
69+
def testShort(self):
70+
oshort = 5
71+
jshort = to_java(oshort, type="s")
72+
assert jinstance(jshort, "java.lang.Short")
73+
assert oshort == jshort.shortValue()
74+
pshort = to_python(jshort)
75+
assert isinstance(pshort, int)
76+
assert oshort == pshort
7077

7178
def testInteger(self):
7279
oint = 5

0 commit comments

Comments
 (0)