Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A probably small bug: Instance of LabeledTextChunk ❯ .to_bytes() ❯ Exception #174

Closed
schittli opened this issue Sep 8, 2023 · 2 comments

Comments

@schittli
Copy link

schittli commented Sep 8, 2023

Good evening

Important note:
Because I am not a Python programmer, I have asked what ChtaGPT would propose and the answer looks pretty good. I have added it to the next comment below so that someone who knows what is happening, can check it.

I guess I have found a small bug 😃, but I don't understand what is happening in Python.

I just create an LabeledTextChunk instance and then, I call .to_bytes():

# Create the instance
labeledTextChunk = LabeledTextChunk(id = id_cue, sample_length = cue.CueLenSample,
                                     purpose = 'purpose', country = 0, language = 0, dialect = 0, codepage = 0, label = 'label')

# Here, the following exception will be created:
resBytes = labeledTextChunk.to_bytes()

Python creates this exception:

File "C:\Program Files\Python311\Lib\site-packages\wave_chunk_parser\chunks.py", line 1168, in to_bytes
    return pack(
           ^^^^^
struct.error: argument for 's' must be a bytes object

In GitHub, here is Line 1168 of chunks.py

I hope someone can easily see this issue.

Thanks a lot, kind regards,
Thomas

@schittli
Copy link
Author

schittli commented Sep 8, 2023

Here is the ChatGPT answer to this issue:

The error is occurring due to an issue in the to_bytes method of your LabeledTextChunk class. The method uses the struct.pack function to convert the data into the desired binary format. The error message suggests that the problem lies in this section of code:

pack(
    f"<4sIII4sHHHH{encoded_label_length}s",
    self.HEADER_LABEL,
    encoded_label_length + self.OFFSET_LABEL,
    self.__id,
    self.__sample_length,
    self.__purpose,
    self.__country,
    self.__language,
    self.__dialect,
    self.__codepage,
    encoded_label,
)

The error is likely occurring because self.__purpose is a string value (str), but the format string 4s is being used, which expects a bytes object (specifically, a 4-byte bytes object). The same issue applies to self.__label. The pack function expects bytes for bytes objects, not str.

To fix the error, you need to ensure that you convert the string values to bytes before passing them to pack. You can use the encode method to convert strings to bytes. Here's a revised version of the to_bytes method:

@word_align
def to_bytes(self) -> bytes:
    encoded_label = null_terminate(encode_string(self.__label), True)
    encoded_label_length = len(encoded_label)

    return pack(
        f"<4sIII4sHHHH{encoded_label_length}s",
        self.HEADER_LABEL,
        encoded_label_length + self.OFFSET_LABEL,
        self.__id,
        self.__sample_length,
        self.__purpose.encode(),  # Convert to bytes
        self.__country,
        self.__language,
        self.__dialect,
        self.__codepage,
        encoded_label,
    )

In this revised version, self.__purpose is converted to bytes using .encode() to match the expectations of struct.pack. You should do the same for self.__label if necessary.

With these changes, the error should be resolved, and the to_bytes method should run without issues. Make sure to also review other parts of your code to ensure that the correct data types are used to avoid such errors.

@steelegbr
Copy link
Owner

Looks like a reasonable thing to change. I'll have to dig out the spec to see what the byte encoding for this sort of chunk is and remember how it all works as I've not touched this in a while. 🙂

Either way, thanks for getting in touch and I'll report back when I get some time to have a proper go at this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants