Eittipat / pyrtmp

PyRTMP: Pure Python RTMP server
MIT License
90 stars 20 forks source link

Fix error on Python 3.11 #4

Closed parly closed 1 year ago

parly commented 1 year ago

Due to a change in the Enum specification in Python 3.11, the following code will cause an error:

from enum import Enum

# pyrtmp/amf/types.py
class AMF0(int, Enum):
    NUMBER = 0x00
    BOOLEAN = 0x01
    STRING = 0x02
    OBJECT = 0x03
    NULL = 0x05
    ARRAY = 0x08
    OBJECT_END = 0x09

# pyrtmp/amf/serializers.py
"0000{:02x}".format(AMF0.OBJECT_END)
>>> "0000{:02x}".format(AMF0.OBJECT_END)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.11/enum.py", line 1227, in __format__
    return str.__format__(str(self), format_spec)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Unknown format code 'x' for object of type 'str'

This patch fixes the problem by first casting the value of AMF0 to int.

Eittipat commented 1 year ago

Do you know if this is backward compatible with other python versions?

Thank you for your contribution.

parly commented 1 year ago

I ran the edited line in Python 3.6.15 and 3.9.16 (on REPL) and got no errors, so I assume it is at least backwards compatible with that versions.