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

UnboundLocalError: local variable 'i' referenced before assignment when shallow copying a c3d file with no events #48

Open
jaraujo98 opened this issue Oct 18, 2022 · 3 comments · May be fixed by #51

Comments

@jaraujo98
Copy link

When copying a c3d file with no events the variable i in this line is never defined, and the code crashes.

write_count = min(i + 1, 18)

Is fixing this as easy as just returning an empty list, or is there more to it? If it's just returning an empty list, happy to open a PR for it.

@friggog
Copy link
Collaborator

friggog commented Oct 19, 2022

Just adding something like

if not events:
   return

at the top of this function seems like it would work?

@MattiasFredriksson
Copy link
Contributor

MattiasFredriksson commented Oct 19, 2022

Not sure if returning immediately is the best option as it's not a 'protected' function and a user should be allowed to pass in an empty list to 'clear' the event block. Not sure what the best option is however since the loop is a bit odd as it allows an iterator to be passed in which doesn't have len(). Removing the line and refactoring the loop might be a better solution:

write_count = 0 # Initiate counter in-case events is an empty iterator
for time, label in events:
    if write_count == 18:
        # Don't raise Error, header events are rarely used.
        warnings.warn('Maximum of 18 events can be encoded in the header, skipping remaining events.')
        break

    event_timings[write_count] = time
    event_labels[write_count] = label
    label_bytes[write_count * 4:(write_count + 1) * 4] = label.encode('utf-8')
    write_count += 1

Extending the doc-string might be good idea as well to just to inform the caller that it accepts an empty list.

@MattiasFredriksson
Copy link
Contributor

MattiasFredriksson commented Oct 20, 2022

Updated the answer to adjust the behavior of the for loop.

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