summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Merriam <charles.merriam@gmail.com>2020-04-19 15:29:28 -0700
committerGitHub <noreply@github.com>2020-04-19 18:29:28 -0400
commit7daa6740e143aea914bf81f5f843eb8c5562f2ba (patch)
treeedd6d36d6b61b742fe4c0aed12d7b0cd3756769d
parentbbfdd5548d7930a281dc4f70a2ed6d80ed266bf9 (diff)
downloadpython-markdown-7daa6740e143aea914bf81f5f843eb8c5562f2ba.tar.gz
Correctly report if an extension raises a `TypeError`.
Also Raise a `KeyError` when attempting to delete a nonexistent key from the extension registry.
-rw-r--r--docs/change_log/index.md5
-rw-r--r--markdown/extensions/__init__.py21
-rw-r--r--markdown/util.py2
-rw-r--r--tests/test_apis.py4
4 files changed, 19 insertions, 13 deletions
diff --git a/docs/change_log/index.md b/docs/change_log/index.md
index 8f1b9db..121dab0 100644
--- a/docs/change_log/index.md
+++ b/docs/change_log/index.md
@@ -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).
diff --git a/markdown/extensions/__init__.py b/markdown/extensions/__init__.py
index 010e310..4bc8e5f 100644
--- a/markdown/extensions/__init__.py
+++ b/markdown/extensions/__init__.py
@@ -75,15 +75,18 @@ class Extension:
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):
"""
diff --git a/markdown/util.py b/markdown/util.py
index 056fd72..a8db7bd 100644
--- a/markdown/util.py
+++ b/markdown/util.py
@@ -399,7 +399,7 @@ class Registry:
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. """
diff --git a/tests/test_apis.py b/tests/test_apis.py
index 39236f2..6564c66 100644
--- a/tests/test_apis.py
+++ b/tests/test_apis.py
@@ -337,7 +337,7 @@ class RegistryTests(unittest.TestCase):
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):
@@ -352,7 +352,7 @@ class RegistryTests(unittest.TestCase):
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), [])