Modify ↓
Ticket #243 (new defect)
Opened 12 months ago
loads_all produces an error when yaml loads classes serialization in file
| Reported by: | xancorreu@… | Owned by: | xi |
|---|---|---|---|
| Priority: | normal | Component: | pyyaml |
| Severity: | normal | Keywords: | yaml.parser.ParserError |
| Cc: |
Description
Hi,
I have this code:
#!./bin/python3
import yaml
class ExcepcioTasca(Exception):
def __init__(self, valor):
self.value=valor
def __str__(self):
return repr(self.value)
class Tasca:
def __init__(self, nombre):
if isinstance(nombre, int):
self.num = nombre
else:
self.num = None
raise ExcepcioTasca("S'ha passat un valor no enter com a nombre de la tasca de la instància {0}".format(repr(self)))
def __str__(self):
return str(self.num)
class Enregistrador:
def afegeix(self, tasca, fitxer='./tasques.yaml', protocol='yaml'):
if protocol=='yaml':
try:
with open(fitxer, mode='a', encoding='utf-8') as f:
yaml.dump(tasca, f)
except TypeError as e:
print("No es pot enregistrar aquest tipus de dada: {0} és {1}".format(tasca, type(tasca)))
else:
print("Protocol no suportat. En futures versions, els protocols suportats poden ser XML, JSON, CSV, etc.")
def main():
try:
t = Tasca(3)
print(t)
t2 = Tasca(0)
print(t2)
e = Enregistrador()
e.afegeix(t)
e.afegeix(t2)
e.afegeix(t)
e.afegeix(lambda x:0)
with open('./tasques.yaml', mode='r', encoding='utf-8') as f:
for data in yaml.load_all(f):
print(data)
except ExcepcioTasca as e:
print("Ha ocorregut una excepció: {0}".format(e.value))
if __name__=='__main__':
main()
The file is called y.py
When I run y.py (python3), I receive:
$ ./y.py
3
0
No es pot enregistrar aquest tipus de dada: <function <lambda> at 0xa29e82c> és <class 'function'>
3
Traceback (most recent call last):
File "./y.py", line 57, in <module>
main()
File "./y.py", line 51, in main
for data in yaml.load_all(f):
File "/home/xan/proves/xitz/simple/virtual/lib/python3.2/site-packages/yaml/__init__.py", line 83, in load_all
while loader.check_data():
File "/home/xan/proves/xitz/simple/virtual/lib/python3.2/site-packages/yaml/constructor.py", line 26, in check_data
return self.check_node()
File "/home/xan/proves/xitz/simple/virtual/lib/python3.2/site-packages/yaml/composer.py", line 18, in check_node
if self.check_event(StreamStartEvent):
File "/home/xan/proves/xitz/simple/virtual/lib/python3.2/site-packages/yaml/parser.py", line 98, in check_event
self.current_event = self.state()
File "/home/xan/proves/xitz/simple/virtual/lib/python3.2/site-packages/yaml/parser.py", line 174, in parse_document_start
self.peek_token().start_mark)
yaml.parser.ParserError: expected '<document start>', but found '<tag>'
in "./tasques.yaml", line 2, column 1
Why I receive that? If pyyaml was able to serialize my class Tasca, then it's logic that it were able to deserialize. I don't know reasons to fail, so I report bug.
Thanks in advance,
Xan.
PS: I run ubuntu with virtualenv. So I get the lastest version of pyyaml via pypi.
Attachments
Note: See
TracTickets for help on using
tickets.
