Changeset 258 for pyyaml/trunk/lib

Show
Ignore:
Timestamp:
08/21/07 16:25:34 (5 years ago)
Author:
xi
Message:

Make compose() and load() ensure that the input stream contains a single document. Fixes #54.

Location:
pyyaml/trunk/lib/yaml
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • pyyaml/trunk/lib/yaml/__init__.py

    r252 r258  
    3636    """ 
    3737    loader = Loader(stream) 
    38     if loader.check_node(): 
    39         return loader.get_node() 
     38    return loader.get_single_node() 
    4039 
    4140def compose_all(stream, Loader=Loader): 
     
    4847        yield loader.get_node() 
    4948 
     49def load(stream, Loader=Loader): 
     50    """ 
     51    Parse the first YAML document in a stream 
     52    and produce the corresponding Python object. 
     53    """ 
     54    loader = Loader(stream) 
     55    return loader.get_single_data() 
     56 
    5057def load_all(stream, Loader=Loader): 
    5158    """ 
     
    5764        yield loader.get_data() 
    5865 
    59 def load(stream, Loader=Loader): 
     66def safe_load(stream): 
    6067    """ 
    6168    Parse the first YAML document in a stream 
    6269    and produce the corresponding Python object. 
    63     """ 
    64     loader = Loader(stream) 
    65     if loader.check_data(): 
    66         return loader.get_data() 
     70    Resolve only basic YAML tags. 
     71    """ 
     72    return load(stream, SafeLoader) 
    6773 
    6874def safe_load_all(stream): 
     
    7379    """ 
    7480    return load_all(stream, SafeLoader) 
    75  
    76 def safe_load(stream): 
    77     """ 
    78     Parse the first YAML document in a stream 
    79     and produce the corresponding Python object. 
    80     Resolve only basic YAML tags. 
    81     """ 
    82     return load(stream, SafeLoader) 
    8381 
    8482def emit(events, stream=None, Dumper=Dumper, 
  • pyyaml/trunk/lib/yaml/composer.py

    r233 r258  
    2626        if not self.check_event(StreamEndEvent): 
    2727            return self.compose_document() 
     28 
     29    def get_single_node(self): 
     30        # Drop the STREAM-START event. 
     31        self.get_event() 
     32 
     33        # Compose a document if the stream is not empty. 
     34        document = None 
     35        if not self.check_event(StreamEndEvent): 
     36            document = self.compose_document() 
     37 
     38        # Ensure that the stream contains no more documents. 
     39        if not self.check_event(StreamEndEvent): 
     40            event = self.get_event() 
     41            raise ComposerError("expected a single document in the stream", 
     42                    document.start_mark, "but found another document", 
     43                    event.start_mark) 
     44 
     45        # Drop the STREAM-END event. 
     46        self.get_event() 
     47 
     48        return document 
    2849 
    2950    def compose_document(self): 
  • pyyaml/trunk/lib/yaml/constructor.py

    r251 r258  
    3737        if self.check_node(): 
    3838            return self.construct_document(self.get_node()) 
     39 
     40    def get_single_data(self): 
     41        # Ensure that the stream contains a single document and construct it. 
     42        node = self.get_single_node() 
     43        if node is not None: 
     44            return self.construct_document(node) 
     45        return None 
    3946 
    4047    def construct_document(self, node):