Skip to content

Commit

Permalink
The yaml.load{,_all} functions require Loader= now
Browse files Browse the repository at this point in the history
  • Loading branch information
ingydotnet committed Sep 23, 2021
1 parent 258ac95 commit af0275b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 46 deletions.
47 changes: 5 additions & 42 deletions lib/yaml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,12 @@
import io

#------------------------------------------------------------------------------
# Warnings control
# XXX "Warnings control" is now deprecated. Leaving in the API function to not
# break code that uses it.
#------------------------------------------------------------------------------

# 'Global' warnings state:
_warnings_enabled = {
'YAMLLoadWarning': True,
}

# Get or set global warnings' state
def warnings(settings=None):
if settings is None:
return _warnings_enabled

if type(settings) is dict:
for key in settings:
if key in _warnings_enabled:
_warnings_enabled[key] = settings[key]

# Warn when load() is called without Loader=...
class YAMLLoadWarning(RuntimeWarning):
pass

def load_warning(method):
if _warnings_enabled['YAMLLoadWarning'] is False:
return

import warnings

message = (
"calling yaml.%s() without Loader=... is deprecated, as the "
"default Loader is unsafe. Please read "
"https://msg.pyyaml.org/load for full details."
) % method

warnings.warn(message, YAMLLoadWarning, stacklevel=3)
return {}

#------------------------------------------------------------------------------
def scan(stream, Loader=Loader):
Expand Down Expand Up @@ -100,30 +71,22 @@ def compose_all(stream, Loader=Loader):
finally:
loader.dispose()

def load(stream, Loader=None):
def load(stream, Loader):
"""
Parse the first YAML document in a stream
and produce the corresponding Python object.
"""
if Loader is None:
load_warning('load')
Loader = FullLoader

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

def load_all(stream, Loader=None):
def load_all(stream, Loader):
"""
Parse all YAML documents in a stream
and produce corresponding Python objects.
"""
if Loader is None:
load_warning('load_all')
Loader = FullLoader

loader = Loader(stream)
try:
while loader.check_data():
Expand Down
4 changes: 0 additions & 4 deletions tests/lib/test_dump_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ def test_dump(verbose=False):
assert yaml.dump(['foo'])
test_dump.unittest = True

def test_load(verbose=False):
assert yaml.load("- foo\n")
test_load.unittest = True

def test_load_safeloader(verbose=False):
assert yaml.load("- foo\n", Loader=yaml.SafeLoader)
test_load_safeloader.unittest = True
Expand Down

0 comments on commit af0275b

Please sign in to comment.