Skip to content

Commit

Permalink
Fixes #75 + test. Need an explicit handling for utf-encoded Python 2.…
Browse files Browse the repository at this point in the history
…7 strings
  • Loading branch information
Lee Kamentsky committed Mar 10, 2016
1 parent 276de5e commit 01d5719
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion javabridge/jutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,9 @@ def get_nice_arg(arg, sig):
if arg is None:
return None
else:
arg = unicode(arg)
if sys.version_info.major == 2:
if isinstance(arg, str):
arg = arg.decode("utf-8")
return env.new_string_utf(arg)
if sig == 'Ljava/lang/Integer;' and type(arg) in [int, long, bool]:
return make_instance('java/lang/Integer', '(I)V', int(arg))
Expand Down
12 changes: 12 additions & 0 deletions javabridge/tests/test_jutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,5 +662,17 @@ def test_12_03_jref_create_and_lock(self):
javabridge.unlock_jref(ref_self)
self.assertRaises(KeyError, javabridge.redeem_jref, ref_self)

def test_13_01_unicode_arg(self):
# On 2.x, check that a unicode argument is properly prepared
s = u"Hola ni\u00F1os"
s1, s2 = s.split(" ")
if sys.version_info.major == 2:
s2 = s2.encode("utf-8")
env = javabridge.get_env()
js1 = env.new_string(s1+" ")
result = javabridge.call(
js1, "concat", "(Ljava/lang/String;)Ljava/lang/String;", s2)
self.assertEqual(s, result)

if __name__=="__main__":
unittest.main()

0 comments on commit 01d5719

Please sign in to comment.