Changes between Version 64 and Version 65 of DeveloperGuidelines/CodeConventions


Ignore:
Timestamp:
01/20/18 21:34:14 (4 years ago)
Author:
Dominic König
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DeveloperGuidelines/CodeConventions

    v64 v65  
    2828=== Print Statements ===
    2929
    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.
     30For compatibility with WSGI as well as newer web2py (>=2.15), using the {{{print}}} statement in web application sources is '''strongly discouraged'''.
     31
     32CLI scripts containing print statements will even '''crash''' since gluon/shell.py now future-imports the {{{print()}}}-function.
    3333
    3434For 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''
    3839
    3940For CLI scripts:
    40  - use sys.stderr.write() for status/error messages, or sys.stdout.write() for results
     41 - use {{{sys.stderr.write()}}} for status/error messages, or {{{sys.stdout.write()}}} for results
    4142 - alternatively, add the future-import to the script and use the print-function
    4243
    43 Using {{{sys.std*.write()}}} (remember that write expects string/bugger, so must convert explicitly):
     44Using {{{sys.std*.write()}}} (remember that write expects string/buffer, so you must convert any parameters explicitly):
    4445{{{#!python
    4546import sys
     
    5657from gluon import current
    5758
    58 # Use this for any permanent message output in server scripts:
     59# Use this for any permanent message output in server code:
    5960current.log.error("Here is an error message")
     61
     62current.log.debug("And this is a debug message")
    6063}}}
    6164
     
    7174print("Direct output to stderr", file=sys.stderr)
    7275}}}
    73 
    7476=== Quotes ===
    7577