Skip to content

Commit 5743831

Browse files
committed
Added yajl_version & checking + updated unit tests
1 parent f91627d commit 5743831

File tree

4 files changed

+75
-15
lines changed

4 files changed

+75
-15
lines changed

tests/test_yajl.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def test_ctxIsPassedToAllCallbacks(self):
124124
% {'ctx': repr(yajl.addressof(ctx))}
125125
)
126126

127-
def test_bufSizeLessThanZeroRaisesException(self):
127+
def test_bufSizeLessThanOneRaisesException(self):
128128
for buf_siz in range(-5, 1):
129129
self.failUnlessRaises(
130130
yajl.YajlConfigError,
@@ -140,3 +140,41 @@ def test_raisesExceptionOnInvalidJson(self):
140140
self.failUnlessRaises(
141141
yajl.YajlError,
142142
parser.parse, invalid_json)
143+
144+
def test_load_yajl_raisesOSErrorIfYajlNotFound(self):
145+
self.mock('yajl.cdll.LoadLibrary', raises=OSError('ABCD'))
146+
e = None
147+
try:
148+
yajl.load_yajl()
149+
except OSError, e:
150+
pass
151+
self.failUnless('Yajl shared object cannot be found.' in str(e))
152+
153+
def test_get_yajl_version_correctlyParsesYajlVersion(self):
154+
for major in [0, 1, 3, 7]:
155+
for minor in [0, 1, 2, 5]:
156+
for micro in [0, 5, 10, 15, 20]:
157+
yajl_version = major * 10000 + minor * 100 + micro
158+
self.mock('yajl.yajl.yajl_version', returns=yajl_version)
159+
self.failUnlessEqual(
160+
'%s.%s.%s' %(major, minor, micro),
161+
yajl.get_yajl_version())
162+
163+
def test_check_yajl_version_warnsOnlyWhenMismatchedVersions(self):
164+
import warnings
165+
self.mock('warnings.warn', [locals()])
166+
self.mock('yajl.__version__', mock_obj='1.1.1')
167+
self.mock('yajl.yajl_version', mock_obj='1.1.1')
168+
self.failUnless(yajl.check_yajl_version())
169+
self.assertSameTrace('')
170+
self.mock('yajl.yajl_version', mock_obj='1.1.0')
171+
self.failIf(yajl.check_yajl_version())
172+
self.assertSameTrace(
173+
"Called warnings.warn("
174+
"'Using Yajl-Py v1.1.1 with Yajl v1.1.0. "
175+
"It is advised to use the same Yajl-Py and Yajl versions',"
176+
"<type 'exceptions.RuntimeWarning'>, stacklevel=3)"
177+
)
178+
179+
def test_checkYajlPyAndYajlHaveSameVersion(self):
180+
self.failUnless(yajl.check_yajl_version())

tests/test_yajl_c.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ def setUp(self):
4040

4141
def assertSameAsGold(self, filename):
4242
with open('%s.gold' %filename) as f:
43-
expected = f.read() #f.readlines()[:-1]
43+
expected = f.read()
4444
# we currently do not test for memory leaks
4545
expected = expected.replace('memory leaks:\t0\n', '')
4646
self.out.seek(0)
47-
got = self.out.read() #.splitlines(1)
47+
got = self.out.read()
4848
self.failUnlessEqual(expected, got)
4949

5050
def resetOutput(self):
@@ -56,9 +56,9 @@ def test(self):
5656
kwargs = {}
5757
if testname.startswith('dc_'):
5858
kwargs['allow_comments'] = False
59-
for buf_siz in range(32):
59+
for buf_siz in range(1, 32):
6060
# try out a range of buffer_sizes to stress stream parsing
61-
kwargs['buf_siz'] = buf_siz + 1
61+
kwargs['buf_siz'] = buf_siz
6262
parser = yajl.YajlParser(self.content_handler, **kwargs)
6363
with open(filename) as f:
6464
try:

yajl/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
'''
22
Pure Python wrapper to the Yajl C library
33
'''
4-
__version__ = '1.0.10'
54

65
from yajl_common import *
76
from yajl_parse import *
87
from yajl_gen import *
8+
9+
__version__ = '1.0.10'
10+
yajl_version = get_yajl_version()
11+
12+
def check_yajl_version():
13+
if __version__ != yajl_version:
14+
import warnings
15+
warnings.warn(
16+
'Using Yajl-Py v%s with Yajl v%s. '
17+
'It is advised to use the same Yajl-Py and Yajl versions' %(
18+
__version__, yajl_version),
19+
RuntimeWarning, stacklevel=3)
20+
return False
21+
return True
22+
23+
check_yajl_version()

yajl/yajl_common.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
from ctypes import *
22

3-
for yajlso in 'libyajl.so', 'libyajl.dylib':
4-
try:
5-
yajl = cdll.LoadLibrary(yajlso)
6-
except OSError:
7-
pass
3+
def load_yajl():
4+
global yajl
5+
for yajlso in 'libyajl.so', 'libyajl.dylib':
6+
try:
7+
yajl = cdll.LoadLibrary(yajlso)
8+
except OSError:
9+
pass
10+
else:
11+
break
812
else:
9-
break
10-
else:
11-
raise OSError('Yajl shared object cannot be found. '
12-
'Please install Yajl and confirm it is on your shared lib path.')
13+
raise OSError('Yajl shared object cannot be found. '
14+
'Please install Yajl and confirm it is on your shared lib path.')
1315

16+
def get_yajl_version():
17+
v = '%0.6d' %yajl.yajl_version()
18+
return '%s.%s.%s' %tuple(map(int, [v[:-4], v[-4:-2], v[-2:]]))
19+
20+
load_yajl()

0 commit comments

Comments
 (0)