Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yaml.load(s, Loader=...) requires Loader= argument always #561

Merged
merged 4 commits into from Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion Makefile
@@ -1,5 +1,5 @@

.PHONY: default build buildext force forceext install installext test testext dist clean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this unintended stuff for this PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

.PHONY: build dist

PYTHON=/usr/bin/python3
TEST=
Expand Down Expand Up @@ -42,3 +42,10 @@ windist:

clean:
${PYTHON} setup.py --with-libyaml clean -a
rm -fr \
dist/ \
lib/PyYAML.egg-info/ \
lib/yaml/__pycache__/ \
tests/lib/__pycache__/ \
yaml/_yaml.c \

47 changes: 5 additions & 42 deletions lib/yaml/__init__.py
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
2 changes: 1 addition & 1 deletion tests/lib/test_appliance.py
Expand Up @@ -124,7 +124,7 @@ def run(collections, args=None):
for function in test_functions:
if include_functions and function.__name__ not in include_functions:
continue
if function.unittest:
if function.unittest and function.unittest is not True:
for base, exts in test_filenames:
if include_filenames and base not in include_filenames:
continue
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/test_dump_load.py
@@ -0,0 +1,24 @@
import yaml

def test_dump(verbose=False):
assert yaml.dump(['foo'])
test_dump.unittest = True

def test_load_no_loader(verbose=False):
try:
yaml.load("- foo\n")
except TypeError:
return True
assert(False, "load() require Loader=...")

test_load_no_loader.unittest = True

def test_load_safeloader(verbose=False):
nitzmahone marked this conversation as resolved.
Show resolved Hide resolved
assert yaml.load("- foo\n", Loader=yaml.SafeLoader)
test_load_safeloader.unittest = True

if __name__ == '__main__':
import sys, test_load
sys.modules['test_load'] = sys.modules['__main__']
import test_appliance
test_appliance.run(globals())
1 change: 1 addition & 0 deletions tests/lib/test_yaml.py
@@ -1,4 +1,5 @@

from test_dump_load import *
from test_mark import *
from test_reader import *
from test_canonical import *
Expand Down