Skip to content

Commit 7daa674

Browse files
authored
Correctly report if an extension raises a TypeError.
Also Raise a `KeyError` when attempting to delete a nonexistent key from the extension registry.
1 parent bbfdd55 commit 7daa674

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

docs/change_log/index.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ Python-Markdown Change Log
66
Under development: version 3.2.2 (a bug-fix release).
77

88
* Load entry_points (for extensions) only once using `importlib.metadata`.
9-
* Fixed issue where double escaped entities could end up in TOC.
9+
* Do not double escape entities in TOC.
10+
* Correctly report if an extension raises a `TypeError` (#939).
11+
* Raise a `KeyError` when attempting to delete a nonexistent key from the
12+
extension registry (#939).
1013

1114
Feb 12, 2020: Released version 3.2.1 (a bug-fix release).
1215

markdown/extensions/__init__.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,18 @@ def _extendMarkdown(self, *args):
7575
md = args[0]
7676
try:
7777
self.extendMarkdown(md)
78-
except TypeError:
79-
# Must be a 2.x extension. Pass in a dumby md_globals.
80-
self.extendMarkdown(md, {})
81-
warnings.warn(
82-
"The 'md_globals' parameter of '{}.{}.extendMarkdown' is "
83-
"deprecated.".format(self.__class__.__module__, self.__class__.__name__),
84-
category=DeprecationWarning,
85-
stacklevel=2
86-
)
78+
except TypeError as e:
79+
if "missing 1 required positional argument" in str(e):
80+
# Must be a 2.x extension. Pass in a dumby md_globals.
81+
self.extendMarkdown(md, {})
82+
warnings.warn(
83+
"The 'md_globals' parameter of '{}.{}.extendMarkdown' is "
84+
"deprecated.".format(self.__class__.__module__, self.__class__.__name__),
85+
category=DeprecationWarning,
86+
stacklevel=2
87+
)
88+
else:
89+
raise
8790

8891
def extendMarkdown(self, md):
8992
"""

markdown/util.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def __delitem__(self, key):
399399
stacklevel=2,
400400
)
401401
else:
402-
raise TypeError
402+
raise KeyError('Cannot delete key {}, not registered.'.format(key))
403403

404404
def add(self, key, value, location):
405405
""" Register a key by location. """

tests/test_apis.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def testRegistrySetItem(self):
337337
def testRegistryDelItem(self):
338338
r = markdown.util.Registry()
339339
r.register(Item('a'), 'a', 20)
340-
with self.assertRaises(TypeError):
340+
with self.assertRaises(KeyError):
341341
del r[0]
342342
# TODO: restore this when deprecated __del__ is removed.
343343
# with self.assertRaises(TypeError):
@@ -352,7 +352,7 @@ def testRegistryDelItem(self):
352352
self.assertEqual(list(r), ['a', 'c'])
353353
del r['a']
354354
self.assertEqual(list(r), ['c'])
355-
with self.assertRaises(TypeError):
355+
with self.assertRaises(KeyError):
356356
del r['badname']
357357
del r['c']
358358
self.assertEqual(list(r), [])

0 commit comments

Comments
 (0)