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)
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)
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)
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)
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)
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)
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)
def test_str(self, subscriber): assert text_t(subscriber)
def test_postencode(self): m = Message(self.channel, text_t('FOO'), postencode='ccyzz') with self.assertRaises(LookupError): m._reraise_error() m.ack()
def test_postencode(self): m = Message(text_t('FOO'), channel=self.channel, postencode='ccyzz') with pytest.raises(LookupError): m._reraise_error() m.ack()
def test_str(self): self.assertTrue(text_t(self.subscriber))
def test_Decimal(self): d = Decimal('3314132.13363235235324234123213213214134') assert loads(dumps({'d': d})), {'d': text_t(d)}
def test_postencode(self): with self.assertRaises(LookupError): Message(self.channel, text_t('FOO'), postencode='ccyzz')
def test_Decimal(self): d = Decimal('3314132.13363235235324234123213213214134') self.assertDictEqual(loads(dumps({'d': d})), { 'd': text_t(d), })
def test_UUID(self): id = uuid4() assert loads(dumps({'u': id})), {'u': text_t(id)}
def test_UUID(self): id = uuid4() self.assertDictEqual(loads(dumps({'u': id})), { 'u': text_t(id), })
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)
def test_postencode(self): with self.assertRaises(LookupError): Message(self.channel, text_t("FOO"), postencode="ccyzz")