فهرست منبع

Escapes single quotes in kwargsrepr strings (#4640)

* Escapes single quotes in kwargsrepr strings

* Adds test case for single quote in saferepr
Kareem Zidane 7 سال پیش
والد
کامیت
d300169a50
2فایلهای تغییر یافته به همراه6 افزوده شده و 1 حذف شده
  1. 1 1
      celery/utils/saferepr.py
  2. 5 0
      t/unit/utils/test_saferepr.py

+ 1 - 1
celery/utils/saferepr.py

@@ -167,7 +167,7 @@ def _format_chars(val, maxlen):
     if isinstance(val, bytes):  # pragma: no cover
         return _format_binary_bytes(val, maxlen)
     else:
-        return "'{0}'".format(truncate(val, maxlen))
+        return "'{0}'".format(truncate(val, maxlen).replace("'", "\\'"))
 
 
 def _repr(obj):

+ 5 - 0
t/unit/utils/test_saferepr.py

@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 from __future__ import absolute_import, unicode_literals
 
+import ast
 import re
 import struct
 from decimal import Decimal
@@ -186,6 +187,10 @@ class test_saferepr:
         native = old_repr(value)
         assert saferepr(value) == native
 
+    def test_single_quote(self):
+        val = {"foo's": "bar's"}
+        assert ast.literal_eval(saferepr(val)) == val
+
     @skip.if_python3()
     def test_bytes_with_unicode(self):
         class X(object):