Ejemplo n.º 1
0
 def entity_name(self, name, table=CHARS_REPLACE_TABLE):
     """Format AMQP queue name into a legal SQS queue name."""
     if name.endswith('.fifo'):
         partial = name.rstrip('.fifo')
         partial = text_t(safe_str(partial)).translate(table)
         return partial + '.fifo'
     else:
         return text_t(safe_str(name)).translate(table)
Ejemplo n.º 2
0
 def entity_name(self, name, table=CHARS_REPLACE_TABLE):
     """Format AMQP queue name into a legal SQS queue name."""
     if name.endswith('.fifo'):
         partial = name.rstrip('.fifo')
         partial = text_t(safe_str(partial)).translate(table)
         return partial + '.fifo'
     else:
         return text_t(safe_str(name)).translate(table)
Ejemplo n.º 3
0
 def default(
     self,
     o,
     dates=(datetime.datetime, datetime.date),
     times=(datetime.time, ),
     textual=(decimal.Decimal, uuid.UUID, DjangoPromise),
     isinstance=isinstance,
     datetime=datetime.datetime,
     text_t=text_t,
 ):
     reducer = getattr(o, "__json__", None)
     if reducer is not None:
         return reducer()
     else:
         if isinstance(o, dates):
             if not isinstance(o, datetime):
                 o = datetime(o.year, o.month, o.day, 0, 0, 0, 0)
             r = o.isoformat()
             if r.endswith("+00:00"):
                 r = r[:-6] + "Z"
             return r
         elif isinstance(o, times):
             return o.isoformat()
         elif isinstance(o, textual):
             return text_t(o)
         return super(JSONEncoder, self).default(o)
Ejemplo n.º 4
0
def loads(s, _loads=json.loads, decode_bytes=IS_PY3):
    # None of the json implementations supports decoding from
    # a buffer/memoryview, or even reading from a stream
    #    (load is just loads(fp.read()))
    # but this is Python, we love copying strings, preferably many times
    # over.  Note that pickle does support buffer/memoryview
    # </rant>
    if isinstance(s, memoryview):
        s = s.tobytes().decode('utf-8')
    elif isinstance(s, bytearray):
        s = s.decode('utf-8')
    elif decode_bytes and isinstance(s, bytes_t):
        s = s.decode('utf-8')
    elif isinstance(s, buffer_t):
        s = text_t(s)  # ... awwwwwww :(
    return _loads(s)
Ejemplo n.º 5
0
def loads(s, _loads=json.loads, decode_bytes=IS_PY3):
    # None of the json implementations supports decoding from
    # a buffer/memoryview, or even reading from a stream
    #    (load is just loads(fp.read()))
    # but this is Python, we love copying strings, preferably many times
    # over.  Note that pickle does support buffer/memoryview
    # </rant>
    if isinstance(s, memoryview):
        s = s.tobytes().decode('utf-8')
    elif isinstance(s, bytearray):
        s = s.decode('utf-8')
    elif decode_bytes and isinstance(s, bytes_t):
        s = s.decode('utf-8')
    elif isinstance(s, buffer_t):
        s = text_t(s)  # ... awwwwwww :(
    return _loads(s)
Ejemplo n.º 6
0
def loads(s, _loads=json.loads, decode_bytes=PY3):
    """Deserialize json from string."""
    # None of the json implementations supports decoding from
    # a buffer/memoryview, or even reading from a stream
    #    (load is just loads(fp.read()))
    # but this is Python, we love copying strings, preferably many times
    # over.  Note that pickle does support buffer/memoryview
    # </rant>
    if isinstance(s, memoryview):
        s = s.tobytes().decode("utf-8")
    elif isinstance(s, bytearray):
        s = s.decode("utf-8")
    elif decode_bytes and isinstance(s, bytes_t):
        s = s.decode("utf-8")
    elif isinstance(s, buffer_t):
        s = text_t(s)  # ... awwwwwww :(

    try:
        return _loads(s)
    except _DecodeError:
        # catch "Unpaired high surrogate" error
        return stdjson.loads(s)
Ejemplo n.º 7
0
def loads(s, _loads=json.loads, decode_bytes=PY3):
    """Deserialize json from string."""
    # None of the json implementations supports decoding from
    # a buffer/memoryview, or even reading from a stream
    #    (load is just loads(fp.read()))
    # but this is Python, we love copying strings, preferably many times
    # over.  Note that pickle does support buffer/memoryview
    # </rant>
    if isinstance(s, memoryview):
        s = s.tobytes().decode('utf-8')
    elif isinstance(s, bytearray):
        s = s.decode('utf-8')
    elif decode_bytes and isinstance(s, bytes_t):
        s = s.decode('utf-8')
    elif isinstance(s, buffer_t):
        s = text_t(s)  # ... awwwwwww :(

    try:
        return _loads(s)
    except _DecodeError:
        # catch "Unpaired high surrogate" error
        return stdjson.loads(s)
Ejemplo n.º 8
0
 def default(self, o,
             dates=(datetime.datetime, datetime.date),
             times=(datetime.time,),
             textual=(decimal.Decimal, uuid.UUID, DjangoPromise),
             isinstance=isinstance,
             datetime=datetime.datetime,
             text_t=text_t):
     reducer = getattr(o, '__json__', None)
     if reducer is not None:
         return reducer()
     else:
         if isinstance(o, dates):
             if not isinstance(o, datetime):
                 o = datetime(o.year, o.month, o.day, 0, 0, 0, 0)
             r = o.isoformat()
             if r.endswith("+00:00"):
                 r = r[:-6] + "Z"
             return r
         elif isinstance(o, times):
             return o.isoformat()
         elif isinstance(o, textual):
             return text_t(o)
         return super(JSONEncoder, self).default(o)
Ejemplo n.º 9
0
def loads(s, _loads=json.loads, decode_bytes=IS_PY3):
    # None of the json implementations supports decoding from
    # a buffer/memoryview, or even reading from a stream
    #    (load is just loads(fp.read()))
    # but this is Python, we love copying strings, preferably many times
    # over.  Note that pickle does support buffer/memoryview
    # </rant>
    if isinstance(s, memoryview):
        s = s.tobytes().decode('utf-8')
    elif isinstance(s, bytearray):
        s = s.decode('utf-8')
    elif decode_bytes and isinstance(s, bytes_t):
        s = s.decode('utf-8')
    elif isinstance(s, buffer_t):
        s = text_t(s)  # ... awwwwwww :(

    if json.__name__ == 'simplejson':
        try:
            return _loads(s)
        # catch simplejson.decoder.JSONDecodeError: Unpaired high surrogate
        except json.decoder.JSONDecodeError:
            return stdjson.loads(s)
    else:
        return _loads(s)
Ejemplo n.º 10
0
def loads(s, _loads=json.loads, decode_bytes=IS_PY3):
    # None of the json implementations supports decoding from
    # a buffer/memoryview, or even reading from a stream
    #    (load is just loads(fp.read()))
    # but this is Python, we love copying strings, preferably many times
    # over.  Note that pickle does support buffer/memoryview
    # </rant>
    if isinstance(s, memoryview):
        s = s.tobytes().decode('utf-8')
    elif isinstance(s, bytearray):
        s = s.decode('utf-8')
    elif decode_bytes and isinstance(s, bytes_t):
        s = s.decode('utf-8')
    elif isinstance(s, buffer_t):
        s = text_t(s)  # ... awwwwwww :(

    if json.__name__ == 'simplejson':
        try:
            return _loads(s)
        # catch simplejson.decoder.JSONDecodeError: Unpaired high surrogate
        except json.decoder.JSONDecodeError:
            return stdjson.loads(s)
    else:
        return _loads(s)
Ejemplo n.º 11
0
 def test_str(self, subscriber):
     assert text_t(subscriber)
Ejemplo n.º 12
0
 def test_postencode(self):
     m = Message(self.channel, text_t('FOO'), postencode='ccyzz')
     with self.assertRaises(LookupError):
         m._reraise_error()
     m.ack()
Ejemplo n.º 13
0
 def test_postencode(self):
     m = Message(text_t('FOO'), channel=self.channel, postencode='ccyzz')
     with pytest.raises(LookupError):
         m._reraise_error()
     m.ack()
Ejemplo n.º 14
0
 def test_postencode(self):
     m = Message(text_t('FOO'), channel=self.channel, postencode='ccyzz')
     with pytest.raises(LookupError):
         m._reraise_error()
     m.ack()
Ejemplo n.º 15
0
 def test_str(self):
     self.assertTrue(text_t(self.subscriber))
Ejemplo n.º 16
0
 def test_Decimal(self):
     d = Decimal('3314132.13363235235324234123213213214134')
     assert loads(dumps({'d': d})), {'d': text_t(d)}
Ejemplo n.º 17
0
 def test_postencode(self):
     with self.assertRaises(LookupError):
         Message(self.channel, text_t('FOO'), postencode='ccyzz')
Ejemplo n.º 18
0
 def test_Decimal(self):
     d = Decimal('3314132.13363235235324234123213213214134')
     self.assertDictEqual(loads(dumps({'d': d})), {
         'd': text_t(d),
     })
Ejemplo n.º 19
0
 def test_postencode(self):
     m = Message(self.channel, text_t('FOO'), postencode='ccyzz')
     with self.assertRaises(LookupError):
         m._reraise_error()
     m.ack()
Ejemplo n.º 20
0
 def test_UUID(self):
     id = uuid4()
     assert loads(dumps({'u': id})), {'u': text_t(id)}
Ejemplo n.º 21
0
 def test_UUID(self):
     id = uuid4()
     self.assertDictEqual(loads(dumps({'u': id})), {
         'u': text_t(id),
     })
Ejemplo n.º 22
0
 def test_Decimal(self):
     d = Decimal('3314132.13363235235324234123213213214134')
     assert loads(dumps({'d': d})), {'d': text_t(d)}
Ejemplo n.º 23
0
 def entity_name(self, name, table=CHARS_REPLACE_TABLE):
     """Format AMQP queue name into a valid SLQS queue name."""
     return text_t(safe_str(name)).translate(table)
Ejemplo n.º 24
0
 def test_Decimal(self):
     d = Decimal('3314132.13363235235324234123213213214134')
     self.assertDictEqual(loads(dumps({'d': d})), {
         'd': text_t(d),
     })
Ejemplo n.º 25
0
 def test_UUID(self):
     id = uuid4()
     assert loads(dumps({'u': id})), {'u': text_t(id)}
Ejemplo n.º 26
0
 def test_UUID(self):
     id = uuid4()
     self.assertDictEqual(loads(dumps({'u': id})), {
         'u': text_t(id),
     })
Ejemplo n.º 27
0
 def test_postencode(self):
     with self.assertRaises(LookupError):
         Message(self.channel, text_t('FOO'), postencode='ccyzz')
Ejemplo n.º 28
0
 def test_str(self, subscriber):
     assert text_t(subscriber)
Ejemplo n.º 29
0
 def entity_name(self, name, table=CHARS_REPLACE_TABLE):
     """Format AMQP queue name into a valid SLQS queue name."""
     return text_t(safe_str(name)).translate(table)
Ejemplo n.º 30
0
 def test_postencode(self):
     with self.assertRaises(LookupError):
         Message(self.channel, text_t("FOO"), postencode="ccyzz")