CodeQL documentation

Use of a print statement at module level

ID: py/print-during-import
Kind: problem
Security severity: 
Severity: recommendation
Precision: high
Tags:
   - quality
   - reliability
   - correctness
Query suites:
   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

Using print statements in level scope may result in surprising output at import time. This in turn means that other code cannot safely import the module in question if the program may only write real output to standard out.

Recommendation

Replace the print statements with calls to some form of logging function or use the warnings module.

Example

In the example, importing the module may cause a message to be printed, which may interfere with the operation of the program.


try:
    import fast_system as system
except ImportError:
    print ("Cannot import fast system, falling back on slow system")
    import slow_system as system

#Fixed version
import logging

try:
    import fast_system as system
except ImportError:
    logging.info("Cannot import fast system, falling back on slow system")
    import slow_system as system

References

  • © GitHub, Inc.
  • Terms
  • Privacy
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy