nfcpy / ndeflib

Python package for parsing and generating NFC Data Exchange Format messages.
ISC License
63 stars 12 forks source link

Errors parameter for message_encoder() helper #3

Closed henrycjc closed 6 years ago

henrycjc commented 6 years ago

In order to support strict error reporting for NDEF data encoding, ndeflib exposes two helper methods: message_decoder and message_encoder (see https://nfcpy.readthedocs.io/en/latest/modules/tag.html#nfc.tag.Tag.NDEF.records and https://ndeflib.readthedocs.io/en/stable/ndef.html#message-encoder) for more info.

In the docs, the example code is as follows:

from ndef import message_decoder, message_encoder
records = message_decoder(tag.ndef.octets, errors='strict')
octets = b''.join(message_encoder(records, errors='strict'))
tag.ndef.octets = octets

However, message_encoder does not accept the keyword arg errors. Thus I have a few questions for @nehpetsde:

  1. Should / can message_encoder accept this keyword argument? Has this just yet to be implemented?

  2. Should message_encoder not support this keyword argument? If so, what limitations are associated with encoding that would mean it is not possible to support this?

  3. In the meantime, would you like me to create pull request updating the docs?

Error output:

  File "tool.py", line 41, in <module>
    ret = clf.connect(rdwr=config)
  File "/usr/local/lib/python2.7/dist-packages/nfc/clf/__init__.py", line 580, in connect
    result = self._rdwr_connect(rdwr_options, terminate)
  File "/usr/local/lib/python2.7/dist-packages/nfc/clf/__init__.py", line 611, in _rdwr_connect
    if options['on-connect'](tag):
  File "tool.py", line 17, in on_connect
    octets = b''.join(message_encoder([rec], errors='strict'))
TypeError: message_encoder() got an unexpected keyword argument 'errors'

Platform:

pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.4.50-v7+ #970 SMP Mon Feb 20 19:18:29 GMT 2017 armv7l GNU/Linux

pi@raspberrypi:~ $ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
NAME="Raspbian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"

Lib version:

pi@raspberrypi:~$ pip freeze
# ommited
ndeflib==0.2.0
nfcpy==0.13.2

Python version:

pi@raspberrypi:~ $ python --version
Python 2.7.9
nehpetsde commented 6 years ago

Many thanks for discovering and reporting this issue, especially also for the excellent reporting format.

The ndeflib.message_encoder is not supposed to generate erroneous NDEF data and instead verify input data when record instances are created. Thus there is no need for an error handling strategy other than "strict", hence the input parameter not needed. This is different from ndeflib.message_decoder which must potentially accept NDEF data with minor correctable errors (error strategy "relax") or gracefully skip or stop at uncorrectable errors (error strategy "ignore").

The problem you've found is a documentation error. Unfortunately it is within the function docstring and will require a new bugfix release 0.13.3. I'll do that as soon as possible.

nehpetsde commented 6 years ago

I have updated the documentation string with the erroneous example and released nfcpy 0.13.3 with that fix. Many thanks again for your report.