Skip to content

Commit 84f6136

Browse files
author
LeeKamentsky
committed
Merge pull request #76 from CellProfiler/issues/75
Fixes #75 + test. Need explicit handling for utf-encoded Python 2.7 strings
2 parents 276de5e + 01d5719 commit 84f6136

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

javabridge/jutil.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,9 @@ def get_nice_arg(arg, sig):
11711171
if arg is None:
11721172
return None
11731173
else:
1174-
arg = unicode(arg)
1174+
if sys.version_info.major == 2:
1175+
if isinstance(arg, str):
1176+
arg = arg.decode("utf-8")
11751177
return env.new_string_utf(arg)
11761178
if sig == 'Ljava/lang/Integer;' and type(arg) in [int, long, bool]:
11771179
return make_instance('java/lang/Integer', '(I)V', int(arg))

javabridge/tests/test_jutil.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,5 +662,17 @@ def test_12_03_jref_create_and_lock(self):
662662
javabridge.unlock_jref(ref_self)
663663
self.assertRaises(KeyError, javabridge.redeem_jref, ref_self)
664664

665+
def test_13_01_unicode_arg(self):
666+
# On 2.x, check that a unicode argument is properly prepared
667+
s = u"Hola ni\u00F1os"
668+
s1, s2 = s.split(" ")
669+
if sys.version_info.major == 2:
670+
s2 = s2.encode("utf-8")
671+
env = javabridge.get_env()
672+
js1 = env.new_string(s1+" ")
673+
result = javabridge.call(
674+
js1, "concat", "(Ljava/lang/String;)Ljava/lang/String;", s2)
675+
self.assertEqual(s, result)
676+
665677
if __name__=="__main__":
666678
unittest.main()

0 commit comments

Comments
 (0)