micropython / micropython-lib

Core Python libraries ported to MicroPython
Other
2.4k stars 997 forks source link

urequests: Compatibility with CPython #379

Closed amotl closed 2 years ago

amotl commented 4 years ago

Hi there,

when invoking the urequests module on CPython, it will croak with

TypeError: %b requires a bytes-like object, or an object that implements __bytes__, not 'str'

when starting to write to the socket stream:

s.write(b"%s /%s HTTP/1.0\r\n" % (method, path))

This has also been reported on https://github.com/adafruit/Adafruit_CircuitPython_Requests/issues/10 and was fixed by https://github.com/adafruit/Adafruit_CircuitPython_Requests/issues/11.

With kind regards, Andreas.

mattytrentini commented 2 years ago

@andrewleech I think this is also still valid for urequests though I haven't tried it on CPython.

andrewleech commented 2 years ago

urequests is currently not really compatible with CPython at all. For a start, the import would need to handle usocket vs socket, then on cpython there is no socket.write function, only socket.send.

Whether urequests should be compatible with cpython is an interesting discussion, which there could be conveniences to being able to test code in cpython, not much of the micropython-lib codebase would be compatible currently.

Separately to that, the micropython b"%s" % str clearly has an auto-convert which cpython does not support - I guess this is a confusing difference between the platforms, though I feel it's one of many cases micropython is more "tolerant" of things than cpython.

While that patch from circuitpython works to avoid the b"%s" % str error on CPython, it does add extra code (flash&ram) to the module that is not technically needed on micropython currently, so I'm not sure it's really worth adding?

dpgeorge commented 2 years ago

I don't think it's worth supporting urequests (or any other library in this repo) running under CPython. That would be too restrictive and not allow us to implement things in an efficient way.