| 1 |
#------------------------------------------------------------------------- |
| 2 |
# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 |
# Licensed under the MIT License. See License.txt in the project root for |
| 4 |
# license information. |
| 5 |
#-------------------------------------------------------------------------- |
| 6 |
|
| 7 |
# Python imports |
| 8 |
import logging |
| 9 |
|
| 10 |
# C imports |
| 11 |
cimport c_strings |
| 12 |
|
| 13 |
|
| 14 |
_logger = logging.getLogger(__name__) |
| 15 |
|
| 16 |
|
| 17 |
cpdef create_empty_string(): |
| 18 |
new_string = AMQPString() |
| 19 |
return new_string |
| 20 |
|
| 21 |
|
| 22 |
cpdef create_string_from_value(value, encoding='UTF-8'): |
| 23 |
if isinstance(value, str): |
| 24 |
value = value.encode(encoding) |
| 25 |
new_string = AMQPString() |
| 26 |
new_string.construct(<const char*>value) |
| 27 |
return new_string |
| 28 |
|
| 29 |
|
| 30 |
cdef class AMQPString(StructBase): |
| 31 |
|
| 32 |
cdef c_strings.STRING_HANDLE _c_value |
| 33 |
|
| 34 |
def __cinit__(self): |
| 35 |
self._c_value = c_strings.STRING_new() |
| 36 |
self._validate() |
| 37 |
|
| 38 |
def __dealloc__(self): |
| 39 |
_logger.debug("Deallocating AMQPString") |
| 40 |
self.destroy() |
| 41 |
|
| 42 |
def __bytes__(self): |
| 43 |
return c_strings.STRING_c_str(self._c_value) |
| 44 |
|
| 45 |
def __str__(self): |
| 46 |
as_bytes = c_strings.STRING_c_str(self._c_value) |
| 47 |
return str(as_bytes, encoding="UTF-8", errors="ignore" ) |
| 48 |
|
| 49 |
def __unicode__(self): |
| 50 |
as_bytes = c_strings.STRING_c_str(self._c_value) |
| 51 |
try: |
| 52 |
return str(as_bytes.decode('UTF-8')) |
| 53 |
except UnicodeDecodeError: |
| 54 |
return str(as_bytes) |
| 55 |
|
| 56 |
def __eq__(self, AMQPString other): |
| 57 |
if c_strings.STRING_compare(self._c_value, other._c_value) == 0: |
| 58 |
return True |
| 59 |
return False |
| 60 |
|
| 61 |
def __ne__(self, AMQPString other): |
| 62 |
if c_strings.STRING_compare(self._c_value, other._c_value) == 0: |
| 63 |
return False |
| 64 |
return True |
| 65 |
|
| 66 |
cdef _validate(self): |
| 67 |
if <void*>self._c_value is NULL: |
| 68 |
self._memory_error() |
| 69 |
|
| 70 |
cpdef destroy(self): |
| 71 |
try: |
| 72 |
if <void*>self._c_value is not NULL: |
| 73 |
_logger.debug("Destroying AMQPString") |
| 74 |
c_strings.STRING_delete(self._c_value) |
| 75 |
except KeyboardInterrupt: |
| 76 |
pass |
| 77 |
finally: |
| 78 |
self._c_value = <c_strings.STRING_HANDLE>NULL |
| 79 |
|
| 80 |
cdef wrap(self, c_strings.STRING_HANDLE value): |
| 81 |
self.destroy() |
| 82 |
self._c_value = value |
| 83 |
self._validate() |
| 84 |
|
| 85 |
cdef construct(self, const char* value): |
| 86 |
self.destroy() |
| 87 |
self._c_value = c_strings.STRING_construct(value) |
| 88 |
self._validate() |
| 89 |
|
| 90 |
cpdef append(self, other): |
| 91 |
if isinstance(other, AMQPString): |
| 92 |
if c_strings.STRING_concat_with_STRING(self._c_value, <c_strings.STRING_HANDLE>other._c_value) != 0: |
| 93 |
self._value_error("Failed to append AMQPString value.") |
| 94 |
else: |
| 95 |
if c_strings.STRING_concat(self._c_value, <char*>other) != 0: |
| 96 |
self._value_error("Failed to append string.") |
| 97 |
|
| 98 |
cpdef clone(self): |
| 99 |
cdef c_strings.STRING_HANDLE value |
| 100 |
value = c_strings.STRING_clone(self._c_value) |
| 101 |
if <void*>value == NULL: |
| 102 |
self._null_error("Failed to clone AMQPString value.") |
| 103 |
cloned_value = AMQPString() |
| 104 |
cloned_value.wrap(value) |
| 105 |
return cloned_value |