|
26 | 26 | import struct
|
27 | 27 | import NodeInfo
|
28 | 28 |
|
| 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 | + |
29 | 37 | class YYASTParserException (Exception):
|
30 | 38 | def __init__(self, message):
|
31 | 39 | Exeption.__init__(self, args=(message,))
|
@@ -82,6 +90,7 @@ def parse_branch_node(self, factory, node_info, internal_data):
|
82 | 90 | return node
|
83 | 91 |
|
84 | 92 | def parse_text_node(self, factory, node_info, internal_data):
|
| 93 | + internal_data = strip_null(internal_data) |
85 | 94 | value = internal_data.decode("UTF-8")
|
86 | 95 | node = factory(node_info, value)
|
87 | 96 | node.parsing_done()
|
@@ -169,19 +178,22 @@ def parse_node(self, data):
|
169 | 178 | )
|
170 | 179 |
|
171 | 180 | # 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) |
173 | 185 |
|
174 | 186 | try:
|
175 | 187 | subparser = self.subparsers[node_type]
|
176 | 188 | except KeyError:
|
177 |
| - raise YYASTParserException("Unknown node type %i.", node_type) |
| 189 | + raise YYASTParserException("Unknown node type %i." % node_type) |
178 | 190 |
|
179 | 191 | node = subparser(factory, node_info, internal_data)
|
180 | 192 | return node, rest_data
|
181 | 193 |
|
182 | 194 |
|
183 | 195 | def parse(self, fd):
|
184 |
| - m = mmap.mmap(fd.fileno(), 0) |
| 196 | + m = mmap.mmap(fd.fileno(), 0, access=mmap.ACCESS_READ) |
185 | 197 | node, rest_data = self.parse_node(m)
|
186 | 198 |
|
187 | 199 | if len(rest_data) != 0:
|
|
0 commit comments