Skip to content

Commit 1b5ea91

Browse files
committed
Implement encoding_name() to get the encoding name
1 parent 125bba3 commit 1b5ea91

5 files changed

Lines changed: 30 additions & 1 deletion

File tree

grnpy/context.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ cdef class Context:
2323
cdef grn_ctx *_ctx
2424

2525
cdef grn_ctx *unwrap(self)
26+
cdef encoding_name(self)

grnpy/context.pyx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
# distutils: sources = grnpy/grnpy_ctx.c
1919

2020
from grnpy.grn_ctx cimport grn_ctx
21+
from grnpy.grn_encoding cimport grn_encoding
2122
from grnpy.grn_error cimport grn_rc
2223
from grnpy.grn_id cimport grn_id
2324
from grnpy.grn_obj cimport grn_obj
2425

26+
cimport grnpy.grn_encoding
27+
2528
from grnpy.object cimport build_object
2629

2730
from .error import Error
@@ -39,6 +42,7 @@ cdef extern from "groonga.h":
3942
cdef extern from "grnpy_ctx.h":
4043
grn_rc grnpy_ctx_get_rc(grn_ctx *ctx)
4144
const char *grnpy_ctx_get_error_message(grn_ctx *ctx)
45+
grn_encoding grnpy_ctx_get_encoding(grn_ctx *ctx)
4246

4347
cdef class Context:
4448
def __cinit__(self, flags=0):
@@ -56,6 +60,23 @@ cdef class Context:
5660
cdef grn_ctx *unwrap(self):
5761
return self._ctx
5862

63+
cdef encoding_name(self):
64+
# https://docs.python.org/3/library/codecs.html
65+
cdef grn_encoding encoding = grnpy_ctx_get_encoding(self._ctx)
66+
if encoding == grnpy.grn_encoding.UTF8:
67+
return "utf_8"
68+
elif encoding == grnpy.grn_encoding.EUC_JP:
69+
return "euc_jp"
70+
elif encoding == grnpy.grn_encoding.SJIS:
71+
# CP932 has better coverage of special characters.
72+
return "cp932"
73+
elif encoding == grnpy.grn_encoding.LATIN1:
74+
return "latin_1"
75+
elif encoding == grnpy.grn_encoding.KOI8R:
76+
return "koi8_r"
77+
else:
78+
raise NotImplementedError(f"unsupported encoding: <{encoding}>")
79+
5980
def rc(self):
6081
return grnpy_ctx_get_rc(self._ctx)
6182

grnpy/grnpy_ctx.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ grnpy_ctx_get_error_message(grn_ctx *ctx)
2929
{
3030
return ctx->errbuf;
3131
}
32+
33+
grn_encoding
34+
grnpy_ctx_get_encoding(grn_ctx *ctx)
35+
{
36+
return GRN_CTX_GET_ENCODING(ctx);
37+
}

grnpy/grnpy_ctx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@
2222

2323
grn_rc grnpy_ctx_get_rc(grn_ctx *ctx);
2424
const char *grnpy_ctx_get_error_message(grn_ctx *ctx);
25+
grn_encoding grnpy_ctx_get_encoding(grn_ctx *ctx);

grnpy/object.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ cdef class Object:
8989
elif isinstance(value, float):
9090
rc = grnpy_obj_set_float(ctx, self.unwrap(), id, value)
9191
elif isinstance(value, str):
92-
value_bytes = value.encode()
92+
value_bytes = value.encode(context.encoding_name())
9393
rc = grnpy_obj_set_text(ctx, self.unwrap(), id, value_bytes, len(value_bytes))
9494
else:
9595
raise TypeError(f"unsupported value type: <{type(value)}>")

0 commit comments

Comments
 (0)