ccnmtl / fdfgen

port of PDF fdfgen library for filling in PDF forms to Python
BSD 3-Clause "New" or "Revised" License
171 stars 35 forks source link

TypeError in python 3.6 #25

Open caver456 opened 7 years ago

caver456 commented 7 years ago

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!

thraxil commented 7 years ago

Pull requests definitely welcome :)

caver456 commented 7 years ago

done - also I was mistaken about the TypeError not showing up in 3.4: I actually was not able to test the current version of fdfgen with python 3.4 for some local install oddity reasons, so, I'm not sure if it shows up in 3.4 or not.

sontek commented 6 years ago

This is fixed, correct?

caver456 commented 6 years ago

I ran into some similar comfusion a few months back, as to whether or not this had been included. Looks like it was accepted and merged in pull request #26 https://github.com/ccnmtl/fdfgen/pull/26 on May 9 2017, but, the version that you get with pip install is still a version from before that time as far as I can tell. So you need to download the files directly from the github page, until the version that pip draws from gets updated. Anders, is that correct?

On Mon, Nov 20, 2017 at 12:05 PM, John Anderson notifications@github.com wrote:

This is fixed, correct?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ccnmtl/fdfgen/issues/25#issuecomment-345814243, or mute the thread https://github.com/notifications/unsubscribe-auth/AR4iZqOdpnFnEKFJVivowONJfueE_qSkks5s4dtxgaJpZM4NUyoI .

sontek commented 6 years ago

I tested on python3.6 and everything seems to be working.

thraxil commented 6 years ago

Yeah, sorry. Looks like we forgot to cut a release after that merge. PR in for it now.

thraxil commented 6 years ago

0.16.1 should be up on PyPI now.