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

Fix packet output in Python 3 #80

Closed
wants to merge 2 commits into from

Conversation

argilo
Copy link
Contributor

@argilo argilo commented Aug 29, 2020

On Python 3, RFlisten fails to print received packets:

TypeError                                 Traceback (most recent call last)
~/git/rfcat/rflib/__init__.py in <module>
      6 d.setMdmSyncWord(0x02dd4)
      7 d.makePktVLEN()
----> 8 d.RFlisten()

~/git/rfcat/rflib/chipcon_nic.py in RFlisten(self)
   1211             try:
   1212                 y, t = self.RFrecv()
-> 1213                 print("(%5.3f) Received:  %s  | %s" % (t, hexlify(y), makeFriendlyAscii(y)))
   1214 
   1215             except ChipconUsbTimeoutException:

~/git/rfcat/rflib/chipcon_nic.py in makeFriendlyAscii(instring)
     44         out.append( instring[ start: ] )
     45 
---> 46     return b''.join(out)
     47 
     48 

TypeError: sequence item 1: expected a bytes-like object, str found

I rewrote and simplified makeFriendlyAscii to correct this problem. It also now returns a string instead of bytes, to avoid being printed as b'.......).\."u..'. I also added .decode() after each call to hexlify to avoid the hex being printed as b'010c0101019f8d29f55cef22750791'.

After these changes, packets are printed the same in Python 2 and Python 3.

@argilo argilo changed the title Fix packet output Fix packet output in Python 3 Aug 29, 2020
@atlas0fd00m
Copy link
Owner

thanks @argilo , i'm tempted to accept this just for the white-space cleanup. :)

i like the simplication, and i'm really happy to get strings instead of bytes. however, it creates a new string for every character, which is very costly for Python (or at least it was in Python2). could you take another go at it using lists and one string creation at the end?

in the interim, i just committed and pushed a new bit of code to solve this makeFriendlyAscii() bug. please test and see if it's broken.

@argilo
Copy link
Contributor Author

argilo commented Sep 27, 2020

i'm tempted to accept this just for the white-space cleanup.

The whitespace cleanup is in a separate commit, so you could still cherry-pick that.

@argilo
Copy link
Contributor Author

argilo commented Sep 27, 2020

@atlas0fd00m Your change does fix the crash; the only problem that remains is that the bytes are printed in their repr() form:

(1601174272.130) Received:  b'010a010101b8143d7533b98626db8f'  | b'.......=u3..&..'
(1601174284.359) Received:  b'010c0101019f8d29f55cef22750791'  | b'.......).\\."u..'
(1601174291.225) Received:  b'010d010101e9395df5c4cb7a32219f'  | b'......9]...z2!.'

@argilo
Copy link
Contributor Author

argilo commented Sep 27, 2020

I'll close off this pull request for now, since the problems it was meant to solve are mostly gone now.

@argilo argilo closed this Sep 27, 2020
@argilo argilo deleted the fix-packet-output branch September 27, 2020 02:45
@argilo argilo mentioned this pull request Sep 27, 2020
@atlas0fd00m
Copy link
Owner

@argilo thank you for your submission. we can make the output a string if that makes sense. i'm still getting my sea-legs with strings and bytes with py3. what do you think?

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

Successfully merging this pull request may close these issues.

2 participants