Skip to content

TypeError in python 3.6 #25

@caver456

Description

@caver456

I have a program (github.com/ncssar/radiolog) that makes various calls to fdfgen. Python 3.6 (but not 3.4) generates a Type Error on startswith if the value is bytes:

Traceback (most recent call last):
File "radiolog.py", line 3413, in accept
self.parent.parent.printClueReport(clueData)
File "radiolog.py", line 1511, in printClueReport
fdf=forge_fdf("",fields,[],[],[])
File "C:\Python36\lib\site-packages\fdfgen_init_.py", line 136, in forge_fdf
checkbox_checked_name)))
File "C:\Python36\lib\site-packages\fdfgen_init_.py", line 75, in handle_data_strings
value = FDFIdentifier(checkbox_checked_name).value
File "C:\Python36\lib\site-packages\fdfgen_init_.py", line 52, in init
if value.startswith('/'):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

and if I try hardcoding line 52 to bytes instead, I get the opposite TypeError:

Traceback (most recent call last):
File "radiolog.py", line 3413, in accept
self.parent.parent.printClueReport(clueData)
File "radiolog.py", line 1511, in printClueReport
fdf=forge_fdf("",fields,[],[],[])
File "C:\Python36\lib\site-packages\fdfgen_init_.py", line 136, in forge_fdf
checkbox_checked_name)))
File "C:\Python36\lib\site-packages\fdfgen_init_.py", line 77, in handle_data_strings
value = FDFIdentifier('Off').value
File "C:\Python36\lib\site-packages\fdfgen_init_.py", line 52, in init
if value.startswith(b'/'):
TypeError: startswith first arg must be str or a tuple of str, not bytes

I think the solution is just to place the type check and conversion code before the startswith clause instead of after.

Old:

    if value.startswith('/'):
        value = value[1:]

    if isinstance(value, bytes):
        value = value.decode('utf-8')

New: (just swapped the two clauses)

    if isinstance(value, bytes):
        value = value.decode('utf-8')

    if value.startswith('/'):
        value = value[1:]

This works in my program on python 3.6, but, I'm not an expert. Let me know if I should do this as a fork and pull request instead.

Thanks. This project has been very helpful!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions