summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-09-29 21:11:11 +0100
committerMichael Williamson <mike@zwobble.org>2013-09-29 21:11:11 +0100
commit0db42091514c60d9d6a09d77fb085c09376e1327 (patch)
tree56c1394dd6a9e02e95800a80b389d1c85db6e1fb
parent51189b3e5a4559419fbd33b48932e98be246a9ef (diff)
Add unicode support
-rw-r--r--jq.pyx10
-rw-r--r--tests/jq_tests.py18
2 files changed, 25 insertions, 3 deletions
diff --git a/jq.pyx b/jq.pyx
index 1290bf4..6c568eb 100644
--- a/jq.pyx
+++ b/jq.pyx
@@ -29,13 +29,15 @@ cdef extern from "jq.h":
jv jq_next(jq_state *)
-def jq(char* program):
+def jq(object program):
+ cdef object program_bytes_obj = program.encode("utf8")
+ cdef char* program_bytes = program_bytes_obj
cdef jq_state *jq = jq_init()
if not jq:
raise Exception("jq_init failed")
# TODO: jq_compile prints error to stderr
- cdef int compiled = jq_compile(jq, program)
+ cdef int compiled = jq_compile(jq, program_bytes)
if not compiled:
raise ValueError("program was not valid")
@@ -53,7 +55,9 @@ cdef class _Program(object):
def transform(self, input, raw_input=False, raw_output=False, multiple_output=False):
string_input = input if raw_input else json.dumps(input)
- result_strings = self._string_to_strings(string_input)
+ bytes_input = string_input.encode("utf8")
+ result_bytes = self._string_to_strings(bytes_input)
+ result_strings = result_bytes
if raw_output:
return "\n".join(result_strings)
elif multiple_output:
diff --git a/tests/jq_tests.py b/tests/jq_tests.py
index e293aa1..192c4db 100644
--- a/tests/jq_tests.py
+++ b/tests/jq_tests.py
@@ -1,3 +1,5 @@
+# coding=utf8
+
from nose.tools import istest, assert_equal, assert_raises
from jq import jq
@@ -70,3 +72,19 @@ def multiple_inputs_in_raw_input_are_separated_by_newlines():
@istest
def value_error_is_raised_if_program_is_invalid():
assert_raises(ValueError, lambda: jq("!"))
+
+
+@istest
+def unicode_strings_can_be_used_as_input():
+ assert_equal(
+ u"‽",
+ jq(".").transform(u'"‽"', raw_input=True)
+ )
+
+
+@istest
+def unicode_strings_can_be_used_as_programs():
+ assert_equal(
+ u"Dragon‽",
+ jq(u'.+"‽"').transform(u'"Dragon"', raw_input=True)
+ )