Skip to content

Commit

Permalink
Create a load() method in BaseConstructor and call from yaml.load()
Browse files Browse the repository at this point in the history
  • Loading branch information
ingydotnet committed Sep 26, 2021
1 parent da67d4b commit d6bb6cf
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 28 deletions.
40 changes: 14 additions & 26 deletions lib/yaml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .loader import *
from .dumper import *

from .reader import Reader
from warnings import warn as w

__version__ = '5.4.1'
try:
Expand Down Expand Up @@ -79,41 +79,29 @@ def load(stream, Loader):
and produce the corresponding Python object.
"""

# Loader is a *Loader instance:
if isinstance(Loader, Reader):
if hasattr(Loader, '_yaml_instance'):
loader = Loader

# TODO Loader is a _LoaderConfig instance:
# elif isinstance(Loader, _LoaderConfig):
# loader = Loader.get('loader_class')(stream)

else:
loader = Loader(stream)


if loader.stream is None:
if stream is None:
raise TypeError("load() requires stream=...")
from .scanner import Scanner
from .parser import Parser
from .composer import Composer
Reader.__init__(loader, stream)
Scanner.__init__(loader)
Parser.__init__(loader)
Composer.__init__(loader)

try:
return loader.get_single_data()
finally:
loader.dispose()
loader.stream = None
return loader.load(stream)

def load_all(stream, Loader):
"""
Parse all YAML documents in a stream
and produce corresponding Python objects.
"""
loader = Loader(stream)
if hasattr(Loader, '_yaml_instance'):
loader = Loader
else:
loader = Loader(stream)

# XXX Not working yet
# return loader.load(stream)

if hasattr(loader, 'stream') and loader.stream is None:
loader._init_loader(stream)

try:
while loader.check_data():
yield loader.get_data()
Expand Down
36 changes: 36 additions & 0 deletions lib/yaml/constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
from .error import *
from .nodes import *

from .reader import Reader
from .scanner import Scanner
from .parser import Parser
from .composer import Composer

import collections.abc, datetime, base64, binascii, re, sys, types

class ConstructorError(MarkedYAMLError):
Expand All @@ -22,6 +27,8 @@ class BaseConstructor:
yaml_multi_constructors = {}

def __init__(self):
self._yaml_instance = True

self._yaml_constructors = {}
self.add_constructor = self._add_constructor
self._yaml_multi_constructors = {}
Expand All @@ -32,6 +39,35 @@ def __init__(self):
self.state_generators = []
self.deep_construct = False

def load(self, stream):
self._init_loader(stream)

try:
return self.get_single_data()
finally:
self.dispose()
self.stream = None

def load_all(self, stream):
assert False, 'XXX Not working yet'

self._init_loader(stream)

try:
while self.check_data():
yield self.get_data()
finally:
self.dispose()

def _init_loader(self, stream):
if hasattr(self, 'stream') and self.stream is None:
if stream is None:
raise TypeError("load() requires stream=...")
Reader.__init__(self, stream)
Scanner.__init__(self)
Parser.__init__(self)
Composer.__init__(self)

def check_data(self):
# If there are more documents available?
return self.check_node()
Expand Down
2 changes: 1 addition & 1 deletion lib/yaml/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(self, stream=None):
# to ensure backwards compatibility.
class UnsafeLoader(Reader, Scanner, Parser, Composer, Constructor, Resolver):

def __init__(self, stream):
def __init__(self, stream=None):
Reader.__init__(self, stream)
Scanner.__init__(self)
Parser.__init__(self)
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/canonical.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def canonical_load(stream):

yaml.canonical_load = canonical_load

def canonical_load_all(stream):
def canonical_load_all(stream=None):
return yaml.load_all(stream, Loader=CanonicalLoader)

yaml.canonical_load_all = canonical_load_all
Expand Down

0 comments on commit d6bb6cf

Please sign in to comment.