Upstream version 8.36.161.0
[platform/framework/web/crosswalk.git] / src / third_party / pyelftools / elftools / common / construct_utils.py
1 #-------------------------------------------------------------------------------
2 # elftools: common/construct_utils.py
3 #
4 # Some complementary construct utilities
5 #
6 # Eli Bendersky (eliben@gmail.com)
7 # This code is in the public domain
8 #-------------------------------------------------------------------------------
9 from ..construct import Subconstruct, ConstructError, ArrayError
10
11
12 class RepeatUntilExcluding(Subconstruct):
13     """ A version of construct's RepeatUntil that doesn't include the last 
14         element (which casued the repeat to exit) in the return value.
15         
16         Only parsing is currently implemented.
17         
18         P.S. removed some code duplication
19     """
20     __slots__ = ["predicate"]
21     def __init__(self, predicate, subcon):
22         Subconstruct.__init__(self, subcon)
23         self.predicate = predicate
24         self._clear_flag(self.FLAG_COPY_CONTEXT)
25         self._set_flag(self.FLAG_DYNAMIC)
26     def _parse(self, stream, context):
27         obj = []
28         try:
29             context_for_subcon = context
30             if self.subcon.conflags & self.FLAG_COPY_CONTEXT:
31                 context_for_subcon = context.__copy__()
32             
33             while True:
34                 subobj = self.subcon._parse(stream, context_for_subcon)
35                 if self.predicate(subobj, context):
36                     break
37                 obj.append(subobj)
38         except ConstructError as ex:
39             raise ArrayError("missing terminator", ex)
40         return obj
41     def _build(self, obj, stream, context):
42         raise NotImplementedError('no building')
43     def _sizeof(self, context):
44         raise SizeofError("can't calculate size")
45