Skip to content

pamqp.encode

pamqp.encode

Functions for encoding data of various types including field tables and arrays

__getattr__(name)

Warn when the removed DEPRECATED_RABBITMQ_SUPPORT flag is read

Source code in pamqp/encode.py
26
27
28
29
30
31
def __getattr__(name: str) -> typing.Any:
    """Warn when the removed ``DEPRECATED_RABBITMQ_SUPPORT`` flag is read"""
    if name == 'DEPRECATED_RABBITMQ_SUPPORT':
        warnings.warn(_DEPRECATED_MESSAGE, DeprecationWarning, stacklevel=2)
        return False
    raise AttributeError(f'module {__name__!r} has no attribute {name!r}')

support_deprecated_rabbitmq(enabled=True)

Deprecated. This function has no effect and will be removed.

Field-table integers are always encoded as I or l, which all supported RabbitMQ versions accept.

Parameters:

Name Type Description Default
enabled bool

Ignored

True
Source code in pamqp/encode.py
34
35
36
37
38
39
40
41
42
43
def support_deprecated_rabbitmq(enabled: bool = True) -> None:
    """Deprecated. This function has no effect and will be removed.

    Field-table integers are always encoded as ``I`` or ``l``, which all
    supported RabbitMQ versions accept.

    :param enabled: Ignored

    """
    warnings.warn(_DEPRECATED_MESSAGE, DeprecationWarning, stacklevel=2)

by_type(value, data_type)

Takes a value of any type and tries to encode it with the specified encoder.

Parameters:

Name Type Description Default
value FieldValue

The value to encode

required
data_type str

The data type name to use for encoding

required

Raises:

Type Description
TypeError

when the :data:data_type is unknown

Source code in pamqp/encode.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def by_type(value: common.FieldValue, data_type: str) -> bytes:
    """Takes a value of any type and tries to encode it with the specified
    encoder.

    :param value: The value to encode
    :type value: :const:`pamqp.common.FieldValue`
    :param data_type: The data type name to use for encoding
    :raises TypeError: when the :data:`data_type` is unknown

    """
    try:
        return METHODS[str(data_type)](value)
    except KeyError as err:
        raise TypeError(f'Unknown type: {data_type}') from err

bit(value, byte, position)

Encode a bit value

Parameters:

Name Type Description Default
value int

Value to encode

required
byte int

The byte to apply the value to

required
position int

The position in the byte to set the bit on

required
Source code in pamqp/encode.py
62
63
64
65
66
67
68
69
70
def bit(value: int, byte: int, position: int) -> int:
    """Encode a bit value

    :param value: Value to encode
    :param byte: The byte to apply the value to
    :param position: The position in the byte to set the bit on

    """
    return byte | (value << position)

boolean(value)

Encode a boolean value

Parameters:

Name Type Description Default
value bool

Value to encode

required

Raises:

Type Description
TypeError

when the value is not the correct type

Source code in pamqp/encode.py
73
74
75
76
77
78
79
80
81
82
def boolean(value: bool) -> bytes:
    """Encode a boolean value

    :param value: Value to encode
    :raises TypeError: when the value is not the correct type

    """
    if not isinstance(value, bool):
        raise TypeError(f'bool required, received {type(value)}')
    return common.Struct.short_short_uint.pack(int(value))

byte_array(value)

Encode a byte array value

Parameters:

Name Type Description Default
value bytes | bytearray

Value to encode

required

Raises:

Type Description
TypeError

when the value is not the correct type

Source code in pamqp/encode.py
85
86
87
88
89
90
91
92
93
94
def byte_array(value: bytes | bytearray) -> bytes:
    """Encode a byte array value

    :param value: Value to encode
    :raises TypeError: when the value is not the correct type

    """
    if not isinstance(value, (bytes, bytearray)):
        raise TypeError(f'bytes or bytearray required, received {type(value)}')
    return common.Struct.integer.pack(len(value)) + value

decimal(value)

Encode a decimal.Decimal value

Parameters:

Name Type Description Default
value Decimal

Value to encode

required

Raises:

Type Description
TypeError

when the value is not the correct type

Source code in pamqp/encode.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def decimal(value: _decimal.Decimal) -> bytes:
    """Encode a decimal.Decimal value

    :param value: Value to encode
    :raises TypeError: when the value is not the correct type

    """
    if not isinstance(value, _decimal.Decimal):
        raise TypeError(f'decimal.Decimal required, received {type(value)}')
    exponent = value.as_tuple().exponent
    if not isinstance(exponent, int):  # NaN, Infinity, sNaN
        raise TypeError(f'Cannot encode {value} as a decimal')
    elif exponent > 0:  # scale a positive exponent up into the mantissa
        scale = 0
        mantissa = int(value)
    else:  # value == mantissa * 10 ** -scale
        scale = -exponent
        mantissa = int(value.scaleb(scale))
    if not (0 <= scale <= 255):
        raise TypeError('Decimal scale range: 0 to 255')
    elif not (-2147483648 <= mantissa <= 2147483647):
        raise TypeError('Decimal mantissa range: -2147483648 to 2147483647')
    return struct.pack('>Bi', scale, mantissa)

double(value)

Encode a floating point value as a double

Parameters:

Name Type Description Default
value float

Value to encode

required

Raises:

Type Description
TypeError

when the value is not the correct type

Source code in pamqp/encode.py
122
123
124
125
126
127
128
129
130
131
def double(value: float) -> bytes:
    """Encode a floating point value as a double

    :param value: Value to encode
    :raises TypeError: when the value is not the correct type

    """
    if not isinstance(value, float):
        raise TypeError(f'float required, received {type(value)}')
    return common.Struct.double.pack(value)

floating_point(value)

Encode a floating point value

Parameters:

Name Type Description Default
value float

Value to encode

required

Raises:

Type Description
TypeError

when the value is not the correct type

Source code in pamqp/encode.py
134
135
136
137
138
139
140
141
142
143
def floating_point(value: float) -> bytes:
    """Encode a floating point value

    :param value: Value to encode
    :raises TypeError: when the value is not the correct type

    """
    if not isinstance(value, float):
        raise TypeError(f'float required, received {type(value)}')
    return common.Struct.float.pack(value)

long_int(value)

Encode a long integer

Parameters:

Name Type Description Default
value int

Value to encode

required

Raises:

Type Description
TypeError

when the value is not the correct type or outside the acceptable range for the data type

Source code in pamqp/encode.py
146
147
148
149
150
151
152
153
154
155
156
157
158
def long_int(value: int) -> bytes:
    """Encode a long integer

    :param value: Value to encode
    :raises TypeError: when the value is not the correct type or outside the
        acceptable range for the data type

    """
    if not isinstance(value, int):
        raise TypeError(f'int required, received {type(value)}')
    elif not (-2147483648 <= value <= 2147483647):
        raise TypeError('Long integer range: -2147483648 to 2147483647')
    return common.Struct.long.pack(value)

long_uint(value)

Encode a long unsigned integer

Parameters:

Name Type Description Default
value int

Value to encode

required

Raises:

Type Description
TypeError

when the value is not the correct type or outside the acceptable range for the data type

Source code in pamqp/encode.py
161
162
163
164
165
166
167
168
169
170
171
172
173
def long_uint(value: int) -> bytes:
    """Encode a long unsigned integer

    :param value: Value to encode
    :raises TypeError: when the value is not the correct type or outside the
        acceptable range for the data type

    """
    if not isinstance(value, int):
        raise TypeError(f'int required, received {type(value)}')
    elif not (0 <= value <= 4294967295):
        raise TypeError('Long unsigned-integer range: 0 to 4294967295')
    return common.Struct.ulong.pack(value)

long_long_int(value)

Encode a long-long int

Parameters:

Name Type Description Default
value int

Value to encode

required

Raises:

Type Description
TypeError

when the value is not the correct type or outside the acceptable range for the data type

Source code in pamqp/encode.py
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
def long_long_int(value: int) -> bytes:
    """Encode a long-long int

    :param value: Value to encode
    :raises TypeError: when the value is not the correct type or outside the
        acceptable range for the data type

    """
    if not isinstance(value, int):
        raise TypeError(f'int required, received {type(value)}')
    elif not (-9223372036854775808 <= value <= 9223372036854775807):
        raise TypeError(
            'long-long integer range: '
            '-9223372036854775808 to 9223372036854775807'
        )
    return common.Struct.long_long_int.pack(value)

long_string(value)

Encode a "long string"

Parameters:

Name Type Description Default
value str

Value to encode

required

Raises:

Type Description
TypeError

when the value is not the correct type

Source code in pamqp/encode.py
194
195
196
197
198
199
200
201
def long_string(value: str) -> bytes:
    """Encode a "long string"

    :param value: Value to encode
    :raises TypeError: when the value is not the correct type

    """
    return _string(common.Struct.integer, value)

octet(value)

Encode an octet value

Parameters:

Name Type Description Default
value int

Value to encode

required

Raises:

Type Description
TypeError

when the value is not the correct type

Source code in pamqp/encode.py
204
205
206
207
208
209
210
211
212
213
def octet(value: int) -> bytes:
    """Encode an octet value

    :param value: Value to encode
    :raises TypeError: when the value is not the correct type

    """
    if not isinstance(value, int):
        raise TypeError(f'int required, received {type(value)}')
    return common.Struct.byte.pack(value)

short_int(value)

Encode a short integer

Parameters:

Name Type Description Default
value int

Value to encode

required

Raises:

Type Description
TypeError

when the value is not the correct type or outside the acceptable range for the data type

Source code in pamqp/encode.py
216
217
218
219
220
221
222
223
224
225
226
227
228
def short_int(value: int) -> bytes:
    """Encode a short integer

    :param value: Value to encode
    :raises TypeError: when the value is not the correct type or outside the
        acceptable range for the data type

    """
    if not isinstance(value, int):
        raise TypeError(f'int required, received {type(value)}')
    elif not (-32768 <= value <= 32767):
        raise TypeError('Short integer range: -32768 to 32767')
    return common.Struct.short.pack(value)

short_uint(value)

Encode an unsigned short integer

Parameters:

Name Type Description Default
value int

Value to encode

required

Raises:

Type Description
TypeError

when the value is not the correct type or outside the acceptable range for the data type

Source code in pamqp/encode.py
231
232
233
234
235
236
237
238
239
240
241
242
243
def short_uint(value: int) -> bytes:
    """Encode an unsigned short integer

    :param value: Value to encode
    :raises TypeError: when the value is not the correct type or outside the
        acceptable range for the data type

    """
    if not isinstance(value, int):
        raise TypeError(f'int required, received {type(value)}')
    elif not (0 <= value <= 65535):
        raise TypeError('Short unsigned integer range: 0 to 65535')
    return common.Struct.ushort.pack(value)

short_string(value)

Encode a string

Parameters:

Name Type Description Default
value str

Value to encode

required

Raises:

Type Description
TypeError

when the value is not the correct type

Source code in pamqp/encode.py
246
247
248
249
250
251
252
253
def short_string(value: str) -> bytes:
    """Encode a string

    :param value: Value to encode
    :raises TypeError: when the value is not the correct type

    """
    return _string(common.Struct.byte, value)

timestamp(value)

Encode a datetime.datetime object or time.struct_time

Parameters:

Name Type Description Default
value datetime | struct_time

Value to encode

required

Raises:

Type Description
TypeError

when the value is not the correct type

Source code in pamqp/encode.py
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
def timestamp(
    value: datetime.datetime | time.struct_time,
) -> bytes:
    """Encode a datetime.datetime object or time.struct_time

    :param value: Value to encode
    :raises TypeError: when the value is not the correct type

    """
    if isinstance(value, datetime.datetime):
        if value.tzinfo is None or value.tzinfo.utcoffset(value) is None:
            # assume datetime object is UTC
            value = value.replace(tzinfo=datetime.UTC)
        return common.Struct.timestamp.pack(int(value.timestamp()))
    if isinstance(value, time.struct_time):
        return common.Struct.timestamp.pack(calendar.timegm(value))
    raise TypeError(
        f'datetime.datetime or time.struct_time required, received {type(value)}'
    )

field_array(value)

Encode a field array from a list of values

Parameters:

Name Type Description Default
value FieldArray

Value to encode

required

Raises:

Type Description
TypeError

when the value is not the correct type

Source code in pamqp/encode.py
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
def field_array(value: common.FieldArray) -> bytes:
    """Encode a field array from a list of values

    :param value: Value to encode
    :type value: :const:`pamqp.common.FieldArray`
    :raises TypeError: when the value is not the correct type

    """
    if not isinstance(value, list):
        raise TypeError(f'list of values required, received {type(value)}')
    data = []
    for item in value:
        data.append(encode_table_value(item))
    output = b''.join(data)
    return common.Struct.integer.pack(len(output)) + output

field_table(value)

Encode a field table from a dict

Parameters:

Name Type Description Default
value FieldTable

Value to encode

required

Raises:

Type Description
TypeError

when the value is not the correct type

Source code in pamqp/encode.py
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
def field_table(value: common.FieldTable) -> bytes:
    """Encode a field table from a dict

    :param value: Value to encode
    :type value: :const:`pamqp.common.FieldTable`
    :raises TypeError: when the value is not the correct type

    """
    if not value:  # If there is no value, return a standard 4 null bytes
        return common.Struct.integer.pack(0)
    elif not isinstance(value, dict):
        raise TypeError(f'dict required, received {type(value)}')
    data = []
    for key, item in sorted(value.items()):
        encoded_key = key.encode('utf-8')
        if len(encoded_key) > 128:  # field names have a 128 byte max
            LOGGER.warning('Truncating key %s to 128 bytes', key)
            # ``ignore`` drops a multi-byte character split at the boundary
            key = encoded_key[0:128].decode('utf-8', 'ignore')
        data.append(short_string(key))
        try:
            data.append(encode_table_value(item))
        except TypeError as err:
            raise TypeError(f'{key} error: {err}') from err
    output = b''.join(data)
    return common.Struct.integer.pack(len(output)) + output

table_integer(value)

Encode an integer as a signed 32-bit long-int (I) when it fits, otherwise as a signed 64-bit long-long-int (l). This is the same as the Java, .NET and Go reference clients.

Parameters:

Name Type Description Default
value int

Value to encode

required

Raises:

Type Description
TypeError

when the value is outside the signed 64-bit range

Source code in pamqp/encode.py
322
323
324
325
326
327
328
329
330
331
332
333
334
335
def table_integer(value: int) -> bytes:
    """Encode an integer as a signed 32-bit long-int (``I``) when it fits,
    otherwise as a signed 64-bit long-long-int (``l``). This is the same as
    the Java, .NET and Go reference clients.

    :param value: Value to encode
    :raises TypeError: when the value is outside the signed 64-bit range

    """
    if -2147483648 <= value <= 2147483647:
        return b'I' + long_int(value)
    elif -9223372036854775808 <= value <= 9223372036854775807:
        return b'l' + long_long_int(value)
    raise TypeError(f'Unsupported numeric value: {value}')

encode_table_value(value)

Takes a value of any type and tries to encode it with the proper encoder

Parameters:

Name Type Description Default
value FieldArray | FieldTable | FieldValue

Value to encode

required

Raises:

Type Description
TypeError

when the type of the value is not supported

Source code in pamqp/encode.py
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
def encode_table_value(
    value: common.FieldArray | common.FieldTable | common.FieldValue,
) -> bytes:
    """Takes a value of any type and tries to encode it with the proper encoder

    :param value: Value to encode
    :type value: :const:`pamqp.common.FieldArray` or
                 :const:`pamqp.common.FieldTable` or
                 :const:`pamqp.common.FieldValue`
    :raises TypeError: when the type of the value is not supported

    """
    if isinstance(value, bool):
        return b't' + boolean(value)
    elif isinstance(value, int):
        return table_integer(value)
    elif isinstance(value, _decimal.Decimal):
        return b'D' + decimal(value)
    elif isinstance(value, float):
        return b'd' + double(value)
    elif isinstance(value, str):
        return b'S' + long_string(value)
    elif isinstance(value, (datetime.datetime, time.struct_time)):
        return b'T' + timestamp(value)
    elif isinstance(value, dict):
        return b'F' + field_table(value)
    elif isinstance(value, list):
        return b'A' + field_array(value)
    elif isinstance(value, (bytes, bytearray)):
        return b'x' + byte_array(value)
    elif value is None:
        return b'V'
    raise TypeError(f'Unknown type: {type(value)} ({value!r})')