Skip to content

Commit a2eee01

Browse files
committed
Add test to show how to subclass FullLoader with new blacklist
1 parent 32f7c92 commit a2eee01

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- !!python/object/new:yaml.MappingNode
2+
args:
3+
state:
4+
mymethod: test
5+
wrong_method: test2

tests/lib3/test_constructor.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pprint
44

55
import datetime
6+
import re
67
import yaml.tokens
78

89
def execute(code):
@@ -14,7 +15,7 @@ def _make_objects():
1415
global MyLoader, MyDumper, MyTestClass1, MyTestClass2, MyTestClass3, YAMLObject1, YAMLObject2, \
1516
AnObject, AnInstance, AState, ACustomState, InitArgs, InitArgsWithState, \
1617
NewArgs, NewArgsWithState, Reduce, ReduceWithState, Slots, MyInt, MyList, MyDict, \
17-
FixedOffset, today, execute
18+
FixedOffset, today, execute, MyFullLoader
1819

1920
class MyLoader(yaml.Loader):
2021
pass
@@ -222,6 +223,9 @@ def tzname(self, dt):
222223
def dst(self, dt):
223224
return datetime.timedelta(0)
224225

226+
class MyFullLoader(yaml.FullLoader):
227+
state_blacklist_regexp = re.compile('(^mymethod$|^wrong_.*$)')
228+
225229
today = datetime.date.today()
226230

227231
def _load_code(expression):
@@ -274,6 +278,18 @@ def test_constructor_types(data_filename, code_filename, verbose=False):
274278

275279
test_constructor_types.unittest = ['.data', '.code']
276280

281+
def test_subclass_blacklist_types(data_filename, verbose=False):
282+
_make_objects()
283+
try:
284+
yaml.load(open(data_filename, 'rb').read(), MyFullLoader)
285+
except yaml.YAMLError as exc:
286+
if verbose:
287+
print("%s:" % exc.__class__.__name__, exc)
288+
else:
289+
raise AssertionError("expected an exception")
290+
291+
test_subclass_blacklist_types.unittest = ['.subclass_blacklist']
292+
277293
if __name__ == '__main__':
278294
import sys, test_constructor
279295
sys.modules['test_constructor'] = sys.modules['__main__']

0 commit comments

Comments
 (0)