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

SyntaxError: Compiler improperly indenting try/else blocks #20

Closed
eric-spitler opened this issue Mar 30, 2021 · 5 comments
Closed

SyntaxError: Compiler improperly indenting try/else blocks #20

eric-spitler opened this issue Mar 30, 2021 · 5 comments

Comments

@eric-spitler
Copy link
Contributor

After updating to Sanic 21.3 and bringing in sanic-routing, I am running into issues starting the server.

I can't find where the try/else part is being built to propose a solution, but here is a quick example that produces the error.

EXAMPLE APP

from multiprocessing import Process
import time

import requests
from sanic import Blueprint, HTTPResponse, Request, Sanic


async def get(request: Request, *args, **kwargs) -> HTTPResponse:
    print(request.route.path)
    print(args)
    print(kwargs)
    return HTTPResponse()


bp0 = Blueprint(name='base')
bp1 = Blueprint(name='first', url_prefix='/<first>')
bp2 = Blueprint(name='second', url_prefix='/<first>/<second>')
bp3 = Blueprint(name='third', url_prefix='/<first>/<second>/<third>')

bp0.add_route(get, '/')
bp1.add_route(get, '/')
bp2.add_route(get, '/')
bp3.add_route(get, '/')

app = Sanic(name='test')
app.blueprint(bp0)
app.blueprint(bp1)
app.blueprint(bp2)
app.blueprint(bp3)

p = Process(target=app.run, kwargs={'port': 9000})
try:
    p.start()
    time.sleep(2)

    print(requests.get('http://localhost:9000/'))
    print(requests.get('http://localhost:9000/1'))
    print(requests.get('http://localhost:9000/1/2'))
    print(requests.get('http://localhost:9000/1/2/3'))
finally:
    p.terminate()

EXCEPTION

[2021-03-30 23:04:01 +0000] [6332] [ERROR] Experienced exception while trying to serve
Traceback (most recent call last):
  File "/home/my-project/env/lib/python3.7/site-packages/sanic/app.py", line 918, in run
    serve_single(server_settings)
  File "/home/my-project/env/lib/python3.7/site-packages/sanic/server.py", line 725, in serve_single
    serve(**server_settings)
  File "/home/my-project/env/lib/python3.7/site-packages/sanic/server.py", line 554, in serve
    trigger_events(before_start, loop)
  File "/home/my-project/env/lib/python3.7/site-packages/sanic/server.py", line 354, in trigger_events
    loop.run_until_complete(result)
  File "uvloop/loop.pyx", line 1494, in uvloop.loop.Loop.run_until_complete
  File "/home/my-project/env/lib/python3.7/site-packages/sanic/app.py", line 1280, in finalize
    app.router.finalize()
  File "/home/my-project/env/lib/python3.7/site-packages/sanic/router.py", line 179, in finalize
    super().finalize(*args, **kwargs)
  File "/home/my-project/env/lib/python3.7/site-packages/sanic_routing/router.py", line 185, in finalize
    self._render(do_compile)
  File "/home/my-project/env/lib/python3.7/site-packages/sanic_routing/router.py", line 263, in _render
    "exec",
  File "<string>", line 34
    basket['__params__']['first'] = str(basket[0])
         ^
IndentationError: expected an indented block

ERROR
When I make a local modification to router.py to print(self.find_route_src), it reveals this:

def find_route(path, router, basket, extra):
    parts = tuple(path[1:].split(router.delimiter))
    try:
        route = router.static_routes[parts]
        basket['__raw_path__'] = path
        return route, basket
    except KeyError:
        pass
    num = len(parts)
    if num > 0:
        basket[0] = parts[0]
        if num > 1:
            basket[1] = parts[1]
            if num == 3:
                basket[2] = parts[2]
                try:
                    basket['__params__']['first'] = str(basket[0])
                    basket['__params__']['second'] = str(basket[1])
                    basket['__params__']['third'] = str(basket[2])
                except ValueError:
                    ...
                else:
                    basket['__raw_path__'] = '<first>/<second>/<third>'
                    return router.dynamic_routes[('<first>', '<second>', '<third>')], basket
    try:
        basket['__params__']['first'] = str(basket[0])
        basket['__params__']['second'] = str(basket[1])
    except ValueError:
        ...
    else:
        basket['__raw_path__'] = '<first>/<second>'
        return router.dynamic_routes[('<first>', '<second>')], basket
try:
basket['__params__']['first'] = str(basket[0])
except ValueError:
...
else:
basket['__raw_path__'] = '<first>'
return router.dynamic_routes[('<first>',)], basket
@ashleysommer
Copy link
Member

Duplicate of #18 (I think).
The fix is being worked on.

For now, please pin Sanic-Router to version 0.4.1 and you won't see this error.

@eric-spitler
Copy link
Contributor Author

Thanks, that version appears to behave as expected for this case.

I assume there is a reason, but this repository is saniic-router and the PyPi artifact is sanic-routing. Not a big deal but might trip up some users that are trying to find it.

@ahopkins
Copy link
Member

ahopkins commented Apr 4, 2021

Thanks, that version appears to behave as expected for this case.

I assume there is a reason, but this repository is saniic-router and the PyPi artifact is sanic-routing. Not a big deal but might trip up some users that are trying to find it.

Yup. I'm going to make that change

@ahopkins
Copy link
Member

Can we close?

@eric-spitler
Copy link
Contributor Author

Confirmed functional in new releases.

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

No branches or pull requests

3 participants