Skip to content

Commit c046308

Browse files
committed
Fix generic parser.
1 parent aade828 commit c046308

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

pyext/Makefile.am

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

2-
pkgpython_PYTHON = __init__.py
2+
pkgpython_PYTHON = __init__.py yyast.py Parser.py NodeInfo.py
33

pyext/__init__.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (c) 2011-2013, Take Vos
2+
# All rights reserved.
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions are met:
6+
#
7+
# - Redistributions of source code must retain the above copyright notice,
8+
# this list of conditions and the following disclaimer.
9+
# - Redistributions in binary form must reproduce the above copyright notice,
10+
# this list of conditions and the following disclaimer in the documentation
11+
# and/or other materials provided with the distribution.
12+
#
13+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23+

pyext/parser.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
import struct
2727
import NodeInfo
2828

29+
def strip_null(x):
30+
"""Strip null bytes at the end of a string that is located in memory.
31+
"""
32+
for i in range(len(x) -1, -1, -1):
33+
if x[i] != '\0':
34+
break
35+
return x[0:i+1]
36+
2937
class YYASTParserException (Exception):
3038
def __init__(self, message):
3139
Exeption.__init__(self, args=(message,))
@@ -82,6 +90,7 @@ def parse_branch_node(self, factory, node_info, internal_data):
8290
return node
8391

8492
def parse_text_node(self, factory, node_info, internal_data):
93+
internal_data = strip_null(internal_data)
8594
value = internal_data.decode("UTF-8")
8695
node = factory(node_info, value)
8796
node.parsing_done()
@@ -169,19 +178,22 @@ def parse_node(self, data):
169178
)
170179

171180
# Get the factory class for the node.
172-
factory = self.factories[node_name]
181+
try:
182+
factory = self.factories[node_name]
183+
except KeyError:
184+
raise YYASTParserException("Could not find factory for '%s' node" % node_name)
173185

174186
try:
175187
subparser = self.subparsers[node_type]
176188
except KeyError:
177-
raise YYASTParserException("Unknown node type %i.", node_type)
189+
raise YYASTParserException("Unknown node type %i." % node_type)
178190

179191
node = subparser(factory, node_info, internal_data)
180192
return node, rest_data
181193

182194

183195
def parse(self, fd):
184-
m = mmap.mmap(fd.fileno(), 0)
196+
m = mmap.mmap(fd.fileno(), 0, access=mmap.ACCESS_READ)
185197
node, rest_data = self.parse_node(m)
186198

187199
if len(rest_data) != 0:

0 commit comments

Comments
 (0)