Additional Unsafe fixes (#82376)

* Allow older pickle protocols to pickle unsafe classes. Fixes #82356

* Address issues when iterating or getting single index from AnsibleUnsafeBytes. Fixes #82375

* clog frag
pull/82399/head
Matt Martz 12 months ago committed by GitHub
parent d949af5093
commit afe3fc184f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,3 @@
bugfixes:
- unsafe data - Address an incompatibility with ``AnsibleUnsafeText`` and ``AnsibleUnsafeBytes`` when pickling with ``protocol=0``
- unsafe data - Address an incompatibility when iterating or getting a single index from ``AnsibleUnsafeBytes``

@ -71,6 +71,9 @@ class AnsibleUnsafeBytes(bytes, AnsibleUnsafe):
def _strip_unsafe(self): def _strip_unsafe(self):
return super().__bytes__() return super().__bytes__()
def __reduce__(self, /):
return (self.__class__, (self._strip_unsafe(),))
def __str__(self, /): # pylint: disable=invalid-str-returned def __str__(self, /): # pylint: disable=invalid-str-returned
return self.decode() return self.decode()
@ -84,12 +87,10 @@ class AnsibleUnsafeBytes(bytes, AnsibleUnsafe):
return AnsibleUnsafeText(super().__format__(format_spec)) return AnsibleUnsafeText(super().__format__(format_spec))
def __getitem__(self, key, /): def __getitem__(self, key, /):
if isinstance(key, int):
return super().__getitem__(key)
return self.__class__(super().__getitem__(key)) return self.__class__(super().__getitem__(key))
def __iter__(self, /):
cls = self.__class__
return (cls(c) for c in super().__iter__())
def __reversed__(self, /): def __reversed__(self, /):
return self[::-1] return self[::-1]
@ -192,6 +193,9 @@ class AnsibleUnsafeText(str, AnsibleUnsafe):
def _strip_unsafe(self, /): def _strip_unsafe(self, /):
return super().__str__() return super().__str__()
def __reduce__(self, /):
return (self.__class__, (self._strip_unsafe(),))
def __str__(self, /): # pylint: disable=invalid-str-returned def __str__(self, /): # pylint: disable=invalid-str-returned
return self return self

@ -19,6 +19,13 @@
- "{'foo': 'bar', 'baz': 'buz'}|urlencode == 'foo=bar&baz=buz'" - "{'foo': 'bar', 'baz': 'buz'}|urlencode == 'foo=bar&baz=buz'"
- "()|urlencode == ''" - "()|urlencode == ''"
- name: verify urlencode works for unsafe strings
assert:
that:
- thing|urlencode == 'foo%3Abar'
vars:
thing: !unsafe foo:bar
# Needed (temporarily) due to coverage reports not including the last task. # Needed (temporarily) due to coverage reports not including the last task.
- assert: - assert:
that: true that: true

Loading…
Cancel
Save