Modify

Ticket #54 (closed defect: fixed)

Opened 7 years ago

Last modified 30 hours ago

load() fails to load consecutive documents

Reported by: cems at lanl dot gov Owned by: xi
Priority: high Component: pyyaml
Severity: major Keywords: load
Cc:

Description

Description: if a file contains multiple documents, yaml.load() fails to work after the first document is read.

I have a text file called test.yml that looks like this: --- as: "333 775260" --- zz: "top"

the following code works as expected:

f = open("test.xml") x = yaml.load_all(f) [ i for i in x]

that loads both documents. it does it lazily using a generator x.

The following does NOT work as exepcted: f = open("test.xml") yaml.load(f) yaml.load(f)

the first yaml.load(f) works and loads the first document. the second one fails. From the documentation I beleive yaml.load should consecutively load the next document upon each invocation on the stream. The error message upon failing to load varies depending upon what is in the second document. In the case shown here is the stack trace from ipython2.5

In [153]: f = open("test4.yml")

In [154]: yaml.load(f) Out[154]: {'as': '333 775260'}

In [155]: yaml.load(f)


<type 'exceptions.AttributeError?'> Traceback (most recent call last)

/Users/cems/Documents/Collaboration/brettin/poisson_estimates/ace_to_yaml/<ipython console> in <module>()

/sw/lib/python2.5/site-packages/yaml/init.py in load(stream, Loader)

64 loader = Loader(stream) 65 if loader.check_data():

---> 66 return loader.get_data()

67 68 def safe_load_all(stream):

/sw/lib/python2.5/site-packages/yaml/constructor.py in get_data(self)

36 # Construct and return the next document. 37 if self.check_node():

---> 38 return self.construct_document(self.get_node())

39 40 def g(): yield None

/sw/lib/python2.5/site-packages/yaml/composer.py in get_node(self)

21 # Get the root node of the next document. 22 if not self.check_event(StreamEndEvent?):

---> 23 return self.compose_document()

24 25 def compose_document(self):

/sw/lib/python2.5/site-packages/yaml/composer.py in compose_document(self)

33 34 # Compose the root node.

---> 35 node = self.compose_node(None, None)

36 37 # Drop the DOCUMENT-END event.

/sw/lib/python2.5/site-packages/yaml/composer.py in compose_node(self, parent, index)

50 return self.anchors[anchor] 51 event = self.peek_event()

---> 52 anchor = event.anchor

53 if anchor is not None: 54 if anchor in self.anchors:

<type 'exceptions.AttributeError?'>: 'NoneType?' object has no attribute 'anchor'

In [156]:

Attachments

Change History

comment:1 Changed 7 years ago by cems at lanl dot gov

oops the formatting got garbled. Here it is with the proper code blocks

By the way I'm looking for a tutorial on using pyYaml. know of any?

I have a text file called test.yml that looks like this:

---
as: "333 775260"
---
zz: "top"

the following code works as expected:

f = open("test.xml")
x = yaml.load_all(f)
[ i for i in x]

that loads both documents. it does it lazily using a generator x.

The following does not work:

f = open("test.xml")
yaml.load(f)
yaml.load(f)

the first yaml.load(f) works and loads the first document. the second one fails. From the documentation I beleive yaml.load should consecutively load the next document upon each invocation on the stream. The error message upon failing to load varies depending upon what is in the second document. In the case shown here is the stack trace from ipython2.5

In [153]: f = open("test4.yml")      

In [154]: yaml.load(f)
Out[154]: {'as': '333 775260'}

In [155]: yaml.load(f)
---------------------------------------------------------------------------
<type 'exceptions.AttributeError'>        Traceback (most recent call last)

/Users/cems/Documents/Collaboration/brettin/poisson_estimates/ace_to_yaml/<ipython console> in <module>()

/sw/lib/python2.5/site-packages/yaml/__init__.py in load(stream, Loader)
     64     loader = Loader(stream)
     65     if loader.check_data():
---> 66         return loader.get_data()
     67
     68 def safe_load_all(stream):

/sw/lib/python2.5/site-packages/yaml/constructor.py in get_data(self)
     36         # Construct and return the next document.
     37         if self.check_node():
---> 38             return self.construct_document(self.get_node())
     39
     40     def g(): yield None

/sw/lib/python2.5/site-packages/yaml/composer.py in get_node(self)
     21         # Get the root node of the next document.
     22         if not self.check_event(StreamEndEvent):
---> 23             return self.compose_document()
     24
     25     def compose_document(self):

/sw/lib/python2.5/site-packages/yaml/composer.py in compose_document(self)
     33
     34         # Compose the root node.
---> 35         node = self.compose_node(None, None)
     36
     37         # Drop the DOCUMENT-END event.

/sw/lib/python2.5/site-packages/yaml/composer.py in compose_node(self, parent, index)
     50             return self.anchors[anchor]
     51         event = self.peek_event()
---> 52         anchor = event.anchor
     53         if anchor is not None:
     54             if anchor in self.anchors:

<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'anchor'

In [156]:

comment:2 Changed 7 years ago by xi

  • Status changed from new to closed
  • Resolution set to fixed

Fixed in [258].

It is not feasible for consecutive calls of load() to produce consecutive documents. The function load() reads the input stream in large chunks, which makes the current file position after the load() call unpredictable.

Rather than that, load() now checks that the stream contains a single document.

comment:3 Changed 30 hours ago by maskodok <galihadiputro87@…>

The only thing more I could hope for is documentation of all these features (other than reading through the code).  Cipto Junaedy Is this in process? Can I help? About  Unit Link Terbaik di Indonesia Commonwealth Life Investra Link

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.