adafruit / Adafruit_CircuitPython_WSGI

WSGI library for simple web servers
MIT License
20 stars 18 forks source link

Exception handling tries to use function.__name__ #16

Closed dannystaple closed 2 years ago

dannystaple commented 2 years ago

While testing AirLift code, I must have triggered the RuntimeError in WSGIApp.__call__. Which lead to this error:

Traceback (most recent call last):
  File "code.py", line 1, in <module>
  File "counting.py", line 23, in <module>
  File "robot_wifi.py", line 52, in start_wifi_server
  File "adafruit_esp32spi/adafruit_esp32spi_wsgiserver.py", line 105, in update_poll
  File "adafruit_wsgi/wsgi_app.py", line 74, in __call__
AttributeError: 'function' object has no attribute '__name__'

CircuitPython does not implement the function.__name__ magic. The following demonstrates this:

Adafruit CircuitPython 7.3.0-beta.2 on 2022-04-27; Raspberry Pi Pico with rp2040
>>> def a():
...     return 1
...
...
>>> a.__name__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute '__name__'

This is different from CPython:

Python 3.7.4 (v3.7.4:e09359112e, Jul  8 2019, 14:54:52) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def a():
...   return 1
... 
>>> a.__name__
'a'

The current exception handler looks like:

                raise RuntimeError(
                    "Proper HTTP response return not given for request handler '{}'".format(
                        route["func"].__name__
                    )
                ) from err

I recommend maybe a getattr with a default?

>>> getattr('a', '__name__', '')
''

Which means where it is available it will format in the name, otherwise it's empty.

dannystaple commented 2 years ago

Or perhaps use the route name...