web2py / py4web

Other
261 stars 128 forks source link

SyntaxWarnings when running py4web on Python 3.12 #856

Closed kszys closed 6 months ago

kszys commented 9 months ago

It appears that py4web is not using Python raw strings (at least not everywhere it should), and with Python 3.12 it starts to throw a lot of warnings. Consider the log output for a just started vanila installation of py4web using Python 3.12:

 /#######  /##     /##/##   /## /##      /## /######## /####### 
| ##__  ##|  ##   /##/ ##  | ##| ##  /# | ##| ##_____/| ##__  ##
| ##  \ ## \  ## /##/| ##  | ##| ## /###| ##| ##      | ##  \ ##
| #######/  \  ####/ | ########| ##/## ## ##| #####   | ####### 
| ##____/    \  ##/  |_____  ##| ####_  ####| ##__/   | ##__  ##
| ##          | ##         | ##| ###/ \  ###| ##      | ##  \ ##
| ##          | ##         | ##| ##/   \  ##| ########| #######/
|__/          |__/         |__/|__/     \__/|________/|_______/
Is still experimental...
Py4web: 1.20231115.1 on Python 3.12.1 (main, Dec  9 2023, 00:44:52) [GCC 13.2.1 20231014]
[ ] loading _scaffold ...
[X] loaded _scaffold       
[ ] loading _dashboard ...
    output _dashboard       
/apps/_dashboard/__init__.py:197: SyntaxWarning: invalid escape sequence '\w'
  @action("delete_app/<name:re:\w+>", method="POST")
/apps/_dashboard/__init__.py:212: SyntaxWarning: invalid escape sequence '\w'
  @action("new_file/<name:re:\w+>/<file_name:path>", method="POST")
[X] loaded _dashboard       
[ ] loading _documentation ...
[X] loaded _documentation       
[ ] loading showcase ...
    output showcase       
/apps/showcase/__init__.py:76: SyntaxWarning: invalid escape sequence '\w'
  templates = re.compile("[/\w]+\.html").findall(controller)
[X] loaded showcase       
[ ] loading _minimal ...
[X] loaded _minimal       
[ ] loading _default ...
[X] loaded _default       
Dashboard is at: http://0.0.0.0:8000/_dashboard
watching (lazy-mode) python file changes in: /apps
Ombott v1.0.0 server starting up (using <class 'py4web.server_adapters.rocketServer.<locals>.RocketServer'>(reloader=False, logging_level=30))...
Listening on http://0.0.0.0:8000/
Hit Ctrl-C to quit.

A couple of issues are identified in _dashboard app:

/apps/_dashboard/__init__.py:197: SyntaxWarning: invalid escape sequence '\w'
  @action("delete_app/<name:re:\w+>", method="POST")
/apps/_dashboard/__init__.py:212: SyntaxWarning: invalid escape sequence '\w'
  @action("new_file/<name:re:\w+>/<file_name:path>", method="POST")

and in showcase app:

/apps/showcase/__init__.py:76: SyntaxWarning: invalid escape sequence '\w'
  templates = re.compile("[/\w]+\.html").findall(controller)

The solution is rather simple - to replace the regular strings with Python raw strings, e.g., for the showcase app, the line should read:

templates = re.compile(r"[/\w]+\.html").findall(controller)

The use of escape sequences has been already deprecated since Python 3.6 (https://docs.python.org/dev/whatsnew/3.6.html#deprecated-python-behavior), and in the future they will start generating errors. I think it maybe the time to update the code?

nicozanf commented 6 months ago

issue confirmed