Skip to content

Removed encoding parameter from bytes call #21

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

Merged
merged 2 commits into from
Mar 4, 2020

Conversation

jimbobbennett
Copy link

As dicsussed in #20, the encoding parameter passed to the byes call gives a TypeError: wrong number of arguments

As dicsussed in adafruit#20, the encoding parameter passed to the `byes` call gives a **TypeError: wrong number of arguments**
Added test for bytearray to keep the existing behavior for strings
@FoamyGuy
Copy link
Contributor

FoamyGuy commented Mar 3, 2020

Do you know of public API that accepts bytearray data via POST like this that you can share? If so I can test this out with a pyportal.

@jimbobbennett
Copy link
Author

I've been testing with the Azure Custom Vision service at customvision.ai. I created a model and posted to the prediction service. Source for how to do it is here: https://github.com/jimbobbennett/mandmcounter

This code has my fixed version of adafruit_requests in requests.py. You'd need to create an object detection model in Custom Vision and set the values in the secrets.py file for your WiFi and keys for the model.

I'm on a plane at the moment, but tomorrow I can provide detailed instructions on how to do it if that would help.

@FoamyGuy
Copy link
Contributor

FoamyGuy commented Mar 3, 2020

Thank you! I think I should be able to get it tested using what you've posted. I will give it a try either tonight or tomorrow.

@tannewt
Copy link
Member

tannewt commented Mar 3, 2020

I'm ok removing this but would love to see an issue for adding support for the encoding parameter to CircuitPython. In general, we should ensure CircuitPython matches CPython.

@jimbobbennett
Copy link
Author

@tannewt - if you look at #20, @FoamyGuy has compared the behavior to CPython and CircuitPython, and the behavior is roughly the same.

If you call bytes() against a string, you can pass the encoding parameter and it works correctly on both platforms.
If you call bytes() against a bytearray, you get an error passing the encoding parameter, albeit a different, more descriptive error on CPython

The difference here is the error given by CircuitPython is different, and less instructive than the CPython one. Where is the right place to raise an issue to get the error message improved? Is it in https://github.com/adafruit/circuitpython?

@FoamyGuy
Copy link
Contributor

FoamyGuy commented Mar 3, 2020

@tannewt do you mean adding encoding parameter to the request() method in this library? bytes() seems to be supporting the encoding parameter already, it just doesn't work with a bytearray as the data, that fails in CPython as well, but with a slightly different error.

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, and works as expected. I ended up testing with a basic Flask server locally, my msoft account was not allowing me to use Azure without signing up for some more stuff. I was able to verify with these changes to the library we can now use this Circuit Python code:

# ... imports ... 
# ... connect to esp and AP etc ...
requests.set_socket(socket, esp)

f = open("small_image.bmp", "rb")
file_data = f.read()
f.close()

copy_data = bytearray(file_data)

response = requests.post("http://192.168.1.117:5000/test", data=copy_data)
print(response)

to POST some image data to a server and we now get the the same (successful) results as this CPython code:

import requests

f = open("small_image.bmp", "rb")
file_data = f.read()
f.close()

copy_data = bytearray(file_data)

r = requests.post("http://localhost:5000/test", data=copy_data)

Whereas prior to these changes it was the error noted in the issue:

  File "/lib/adafruit_requests.py", line 285, in post
  File "/lib/adafruit_requests.py", line 242, in request
  File "/lib/adafruit_requests.py", line 224, in request
TypeError: wrong number of arguments

@tannewt
Copy link
Member

tannewt commented Mar 4, 2020

@tannewt - if you look at #20, @FoamyGuy has compared the behavior to CPython and CircuitPython, and the behavior is roughly the same.

If you call bytes() against a string, you can pass the encoding parameter and it works correctly on both platforms.
If you call bytes() against a bytearray, you get an error passing the encoding parameter, albeit a different, more descriptive error on CPython

The difference here is the error given by CircuitPython is different, and less instructive than the CPython one. Where is the right place to raise an issue to get the error message improved? Is it in https://github.com/adafruit/circuitpython?

Ah! Ok. I misunderstood that. Sounds like it is close enough. It would be nice to ensure it is the same exception thrown but it's much less important.

This change looks good to me.

@FoamyGuy
Copy link
Contributor

FoamyGuy commented Mar 4, 2020

Thank you @jimbobbennett!

@FoamyGuy FoamyGuy merged commit c052e15 into adafruit:master Mar 4, 2020
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Mar 5, 2020
Updating https://github.com/adafruit/Adafruit_CircuitPython_HT16K33 to 3.2.3 from 3.2.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_HT16K33#66 from FoamyGuy/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_IRRemote to 3.4.2 from 3.4.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_IRRemote#33 from mfeif/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_BLE to 6.0.0 from 5.2.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_BLE#72 from tannewt/remove_radio

Updating https://github.com/adafruit/Adafruit_CircuitPython_Requests to 1.3.0 from 1.2.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_Requests#21 from jimbobbennett/master
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.

3 participants