Skip to content

Commit df5b85b

Browse files
jdcaballerovmgautierfr
authored andcommitted
Allow to transparently construct ZimBlob with bytes or str
1 parent db4bb5e commit df5b85b

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

libzim/examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def should_index(self):
3636
return True
3737

3838
def get_data(self):
39-
return ZimBlob(self.content.encode('UTF-8'))
39+
return ZimBlob(self.content)
4040

4141
# Create a ZimTestArticle article
4242

libzim/libzim.pyx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ from collections import defaultdict
1818
cdef class ZimBlob:
1919
cdef Blob* c_blob
2020

21-
def __init__(self, bytes content):
22-
ref_content = content
23-
self.c_blob = new Blob(<char *> ref_content, len(content))
21+
def __init__(self, content):
22+
23+
if isinstance(content, str):
24+
ref_content = content.encode('UTF-8')
25+
else:
26+
ref_content = content
27+
28+
self.c_blob = new Blob(<char *> ref_content, len(ref_content))
2429

2530
def __dealloc__(self):
2631
if self.c_blob != NULL:
@@ -71,7 +76,11 @@ cdef class ZimArticle:
7176
@property
7277
def content(self):
7378
blob = self.c_article.getData()
74-
return blob.data()[:blob.size()]
79+
content = blob.data()[:blob.size()]
80+
try:
81+
return content.decode('UTF-8')
82+
except UnicodeDecodeError:
83+
return content
7584

7685
# This changes with implementation
7786
@property

0 commit comments

Comments
 (0)