Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Use of a print statement at module level

ID: py/print-during-importKind: problemSecurity severity: Severity: recommendationPrecision: highTags:   - quality   - reliability   - correctnessQuery suites:   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

Usingprint 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 theprint statements with calls to some form of logging function or use thewarnings 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:importfast_systemassystemexceptImportError:print("Cannot import fast system, falling back on slow system")importslow_systemassystem#Fixed versionimportloggingtry:importfast_systemassystemexceptImportError:logging.info("Cannot import fast system, falling back on slow system")importslow_systemassystem

References


[8]ページ先頭

©2009-2025 Movatter.jp