GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Raising a tuple — CodeQL query help documentation

Raising a tuple¶

ID: py/raises-tuple
Kind: problem
Security severity: 
Severity: warning
Precision: very-high
Tags:
   - maintainability
Query suites:
   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

In Python 2, if a tuple is raised then all elements but the first are ignored and only the first part is raised. If the first element is itself a tuple, then the first element of that is used and so on. This unlikely to be the intended effect and will most likely indicate some sort of error.

It is important to note that the exception in raise Exception, message is not a tuple, whereas the exception in ex = Exception, message; raise ex is a tuple.

In Python 3, raising a tuple is an error.

Recommendation¶

Given that all but the first element of the tuple is ignored, the tuple should be replaced with its first element in order to improve the clarity of the code. If the subsequent parts of the tuple were intended to form the message, then they should be passed as an argument when creating the exception.

Example¶

In the following example the intended error message is mistakenly used to form a tuple.



def raise_tuple():
    ex = Exception, "Important diagnostic information"
    raise ex

This can be fixed, either by using the message to create the exception or using the message in the raise statement, as shown below.



def fixed_raise_tuple1():
    ex = Exception("Important diagnostic information")
    raise ex


def fixed_raise_tuple2():
    raise Exception, "Important diagnostic information"

References¶

  • Python Language Reference: Exceptions.

  • Python Tutorial: Handling Exceptions.

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.