Skip to content

Commit

Permalink
Correctly report if an extension raises a TypeError.
Browse files Browse the repository at this point in the history
Also Raise a `KeyError` when attempting to delete a nonexistent key from the extension registry.
  • Loading branch information
merriam committed Apr 19, 2020
1 parent bbfdd55 commit 7daa674
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
5 changes: 4 additions & 1 deletion docs/change_log/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ Python-Markdown Change Log
Under development: version 3.2.2 (a bug-fix release).

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

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

Expand Down
21 changes: 12 additions & 9 deletions markdown/extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,18 @@ def _extendMarkdown(self, *args):
md = args[0]
try:
self.extendMarkdown(md)
except TypeError:
# Must be a 2.x extension. Pass in a dumby md_globals.
self.extendMarkdown(md, {})
warnings.warn(
"The 'md_globals' parameter of '{}.{}.extendMarkdown' is "
"deprecated.".format(self.__class__.__module__, self.__class__.__name__),
category=DeprecationWarning,
stacklevel=2
)
except TypeError as e:
if "missing 1 required positional argument" in str(e):
# Must be a 2.x extension. Pass in a dumby md_globals.
self.extendMarkdown(md, {})
warnings.warn(
"The 'md_globals' parameter of '{}.{}.extendMarkdown' is "
"deprecated.".format(self.__class__.__module__, self.__class__.__name__),
category=DeprecationWarning,
stacklevel=2
)
else:
raise

def extendMarkdown(self, md):
"""
Expand Down
2 changes: 1 addition & 1 deletion markdown/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def __delitem__(self, key):
stacklevel=2,
)
else:
raise TypeError
raise KeyError('Cannot delete key {}, not registered.'.format(key))

def add(self, key, value, location):
""" Register a key by location. """
Expand Down
4 changes: 2 additions & 2 deletions tests/test_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def testRegistrySetItem(self):
def testRegistryDelItem(self):
r = markdown.util.Registry()
r.register(Item('a'), 'a', 20)
with self.assertRaises(TypeError):
with self.assertRaises(KeyError):
del r[0]
# TODO: restore this when deprecated __del__ is removed.
# with self.assertRaises(TypeError):
Expand All @@ -352,7 +352,7 @@ def testRegistryDelItem(self):
self.assertEqual(list(r), ['a', 'c'])
del r['a']
self.assertEqual(list(r), ['c'])
with self.assertRaises(TypeError):
with self.assertRaises(KeyError):
del r['badname']
del r['c']
self.assertEqual(list(r), [])
Expand Down

0 comments on commit 7daa674

Please sign in to comment.