Skip to content

Commit 9681451

Browse files
committed
Allowing for generation reset.
1 parent 3dc700a commit 9681451

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

yajl/yajl_gen.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __init__(self, **kwargs):
4141
:type indent: string
4242
4343
.. attribute:: g
44-
44+
4545
instance of :ctype:`yajl_gen` returned by
4646
:cfunc:`yajl.yajl_gen_alloc`.
4747
This should not be used directly.
@@ -55,10 +55,12 @@ def __init__(self, **kwargs):
5555
('gen_escape_solidus', yajl_gen_escape_solidus),
5656
])
5757
for k,v in kwargs.items():
58-
yajl.yajl_gen_config(self.g, config_map[k], v)
58+
self._yajl_gen('yajl_gen_config', config_map[k], v)
5959

6060
def __del__(self):
61-
yajl.yajl_gen_free(self.g)
61+
self._yajl_gen('yajl_gen_free')
62+
def yajl_gen_reset(self, sep=''):
63+
self._yajl_gen('yajl_gen_reset', sep)
6264
def _assert_retval(self, retval):
6365
'''
6466
:param retval: yajl_gen_status return code
@@ -79,39 +81,48 @@ def yajl_gen_get_buf(self):
7981
l = c_uint()
8082
buf = POINTER(c_ubyte)()
8183
self._assert_retval(
82-
yajl.yajl_gen_get_buf(self.g, byref(buf), byref(l))
84+
self._yajl_gen('yajl_gen_get_buf', byref(buf), byref(l))
8385
)
8486
try:
8587
return string_at(buf, l.value)
8688
finally:
87-
yajl.yajl_gen_clear(self.g)
89+
self._yajl_gen('yajl_gen_clear')
90+
def _yajl_gen(self, name, *args):
91+
'''
92+
Call the underlying yajl_gen c function/method
93+
``self.g`` must be already allocated
94+
'''
95+
return getattr(yajl, name)(self.g, *args)
8896
def _dispatch(self, name, *args):
8997
'''
9098
:param name: yajl func ``name`` to dispatch to
9199
:type name: string
92100
:param args: arguments to pass to the dispatchee
93101
:type args: list or tuple
102+
103+
Asserts that the returned value is proper or raises the proper
104+
``YajlGenException``
94105
'''
95106
self._assert_retval(
96-
getattr(yajl, name)(self.g, *args)
107+
self._yajl_gen(name, *args)
97108
)
98109
def yajl_gen_null(self):
99110
''' Generate json value ``null`` '''
100111
self._dispatch('yajl_gen_null')
101112
def yajl_gen_bool(self, b):
102-
'''
113+
'''
103114
:param b: flag to be jsonified
104115
:type b: bool
105116
'''
106117
self._dispatch('yajl_gen_bool', b)
107118
def yajl_gen_integer(self, n):
108-
'''
119+
'''
109120
:param n: number to be jsonified
110121
:type n: int
111122
'''
112123
self._dispatch('yajl_gen_integer', c_longlong(n))
113124
def yajl_gen_double(self, n):
114-
'''
125+
'''
115126
:param n: number to be jsonified
116127
:type n: float
117128
'''
@@ -126,7 +137,7 @@ def yajl_gen_number(self, s):
126137
'''
127138
self._dispatch('yajl_gen_number', c_char_p(s), len(s))
128139
def yajl_gen_string(self, s):
129-
'''
140+
'''
130141
:param s: string to be jsonified
131142
:type s: string
132143
'''

0 commit comments

Comments
 (0)