Changes between Version 64 and Version 65 of DeveloperGuidelines/CodeConventions
- Timestamp:
- 01/20/18 21:34:14 (7 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
DeveloperGuidelines/CodeConventions
v64 v65 28 28 === Print Statements === 29 29 30 For compatibility with WSGI as well as newer web2py (>=2.15), using the {{{print}}} statement in web application sources is strongly discouraged.31 32 CLI scripts containing print statements will even crash since gluon/shell.py now future-imports the Python-3 print_function.30 For compatibility with WSGI as well as newer web2py (>=2.15), using the {{{print}}} statement in web application sources is '''strongly discouraged'''. 31 32 CLI scripts containing print statements will even '''crash''' since gluon/shell.py now future-imports the {{{print()}}}-function. 33 33 34 34 For everything that is to be executed in the restricted environment (i.e. all server code): 35 - use sys.stderr.write() 36 - better yet (and mandatory for any permanent debug/error messages): use {{{current.log.*}}}, as it can be turned on/off centrally, and routed to a log file 37 - if absolutely necessary, add the future-import to the file and use the print-function 35 - for messages explaining mandatory crashes that prevent the system from running at all (e.g. mandatory module import failures), use {{{sys.stderr.write()}}} 36 - for all other permanent debug/error messages: use {{{current.log.*}}}, as it can be controlled on/off centrally, and routed to a log file when web2py isn't console-run 37 - for temporary debug output in code under development, use {{{sys.stderr.write()}}} 38 - if absolutely necessary (re-think your design!), add the future-import to the file and use the print-''function'' 38 39 39 40 For CLI scripts: 40 - use sys.stderr.write() for status/error messages, or sys.stdout.write()for results41 - use {{{sys.stderr.write()}}} for status/error messages, or {{{sys.stdout.write()}}} for results 41 42 - alternatively, add the future-import to the script and use the print-function 42 43 43 Using {{{sys.std*.write()}}} (remember that write expects string/bu gger, so must convertexplicitly):44 Using {{{sys.std*.write()}}} (remember that write expects string/buffer, so you must convert any parameters explicitly): 44 45 {{{#!python 45 46 import sys … … 56 57 from gluon import current 57 58 58 # Use this for any permanent message output in server scripts:59 # Use this for any permanent message output in server code: 59 60 current.log.error("Here is an error message") 61 62 current.log.debug("And this is a debug message") 60 63 }}} 61 64 … … 71 74 print("Direct output to stderr", file=sys.stderr) 72 75 }}} 73 74 76 === Quotes === 75 77