From f084ad27243869fe97194031ba3517b48b31e529 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 15 Aug 2023 17:26:08 +0200 Subject: [PATCH] Remove UnescapedJID MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It hadn’t been functional for many years, producing invalid JIDs and being confusing for users anyway. Better remove it. --- slixmpp/jid.py | 99 ----------------------------------------------- tests/test_jid.py | 9 ----- 2 files changed, 108 deletions(-) diff --git a/slixmpp/jid.py b/slixmpp/jid.py index 5a9257cc..450a17c2 100644 --- a/slixmpp/jid.py +++ b/slixmpp/jid.py @@ -31,18 +31,6 @@ JID_PATTERN = re.compile( JID_ESCAPE_SEQUENCES = {'\\20', '\\22', '\\26', '\\27', '\\2f', '\\3a', '\\3c', '\\3e', '\\40', '\\5c'} -#: The reverse mapping of escape sequences to their original forms. -JID_UNESCAPE_TRANSFORMATIONS = {'\\20': ' ', - '\\22': '"', - '\\26': '&', - '\\27': "'", - '\\2f': '/', - '\\3a': ':', - '\\3c': '<', - '\\3e': '>', - '\\40': '@', - '\\5c': '\\'} - # TODO: Find the best cache size for a standard usage. @lru_cache(maxsize=1024) @@ -173,31 +161,6 @@ def _validate_resource(resource: Optional[str]): return resource -def _unescape_node(node: str): - """Unescape a local portion of a JID. - - .. note:: - The unescaped local portion is meant ONLY for presentation, - and should not be used for other purposes. - """ - unescaped = [] - seq = '' - for i, char in enumerate(node): - if char == '\\': - seq = node[i:i+3] - if seq not in JID_ESCAPE_SEQUENCES: - seq = '' - if seq: - if len(seq) == 3: - unescaped.append(JID_UNESCAPE_TRANSFORMATIONS.get(seq, char)) - - # Pop character off the escape sequence, and ignore it - seq = seq[1:] - else: - unescaped.append(char) - return ''.join(unescaped) - - def _format_jid( local: Optional[str] = None, domain: Optional[str] = None, @@ -231,51 +194,6 @@ class InvalidJID(ValueError): full JID while the local and resource portions still exist. """ -# pylint: disable=R0903 -class UnescapedJID: - - """ - .. versionadded:: 1.1.10 - """ - - __slots__ = ('_node', '_domain', '_resource') - - def __init__( - self, - node: Optional[str], - domain: Optional[str], - resource: Optional[str], - ): - self._node = node - self._domain = domain - self._resource = resource - - def __getattribute__(self, name: str): - """Retrieve the given JID component. - - :param name: one of: user, server, domain, resource, - full, or bare. - """ - if name == 'resource': - return self._resource or '' - if name in ('user', 'username', 'local', 'node'): - return self._node or '' - if name in ('server', 'domain', 'host'): - return self._domain or '' - if name in ('full', 'jid'): - return _format_jid(self._node, self._domain, self._resource) - if name == 'bare': - return _format_jid(self._node, self._domain) - return object.__getattribute__(self, name) - - def __str__(self): - """Use the full JID as the string value.""" - return _format_jid(self._node, self._domain, self._resource) - - def __repr__(self): - """Use the full JID as the representation.""" - return _format_jid(self._node, self._domain, self._resource) - class JID: @@ -330,21 +248,6 @@ class JID: self._resource = jid._resource if not bare else '' self._update_bare_full() - def unescape(self): - """Return an unescaped JID object. - - Using an unescaped JID is preferred for displaying JIDs - to humans, and they should NOT be used for any other - purposes than for presentation. - - :return: :class:`UnescapedJID` - - .. versionadded:: 1.1.10 - """ - return UnescapedJID(_unescape_node(self._node), - self._domain, - self._resource) - def _update_bare_full(self): """Format the given JID into a bare and a full JID. """ @@ -424,8 +327,6 @@ class JID: # pylint: disable=W0212 def __eq__(self, other): """Two JIDs are equal if they have the same full JID value.""" - if isinstance(other, UnescapedJID): - return False if not isinstance(other, JID): try: other = JID(other) diff --git a/tests/test_jid.py b/tests/test_jid.py index 1233eb37..4093c5a1 100644 --- a/tests/test_jid.py +++ b/tests/test_jid.py @@ -267,15 +267,6 @@ class TestJIDClass(SlixTest): self.assertEqual(jid.domain.encode('utf-8'), b'b\xc3\xbccher.ch') - def testJIDUnescape(self): - jid = JID('here\\27s_a_wild_\\26_\\2fcr%zy\\2f_\\40ddress\\20for\\3a\\3cwv\\3e(\\22IMPS\\22)\\5c@example.com') - ujid = jid.unescape() - self.assertEqual(ujid.local, 'here\'s_a_wild_&_/cr%zy/_@ddress for:("imps")\\') - - jid = JID('blah\\5cfoo\\5c20bar@example.com') - ujid = jid.unescape() - self.assertEqual(ujid.local, 'blah\\foo\\20bar') - def testStartOrEndWithEscapedSpaces(self): local = ' foo' self.assertRaises(InvalidJID, JID, '%s@example.com' % local)