主要内容

polyspace.project.Global Class

R2026b

Namespace: polyspace.project

(Python) Store information about a global variable in parsed source code

Since R2025a

Description

This Python® class contains information about a global variable in your source code. A polyspace.project.Global object stores the variable name and support information for stubbing.

You obtain polyspace.project.Global objects from the Globals property of a polyspace.project.CodeInfo object, or by using the getGlobalByName method.

Creation

Description

globalList = codeInfo.Globals returns a list of polyspace.project.Global objects for all the global variables in the parsed source code. Here, codeInfo is a polyspace.project.CodeInfo object.

example

globalVar = codeInfo.getGlobalByName(name) returns the polyspace.project.Global object for the global variable with the specified name. The global variable name must be qualified with the namespace.

Input Arguments

expand all

Name of the global variable, specified as a string. The name must be qualified with the namespace.

Example: "minValue"

Properties

expand all

Name of the global variable, returned as a string.

Example: "minValue"

Whether the variable has a stub in an external stub file, returned as True or False.

This property is set to True only if a variable stub is sourced from an external C/C++ file. It is set to False if the variable is stubbed with a project-specific polyspace.project.VariableStub object.

Whether the variable is undefined and requires a stub, returned as a polyspace.project.SymbolSupport object. The object consists of these properties.

PropertyDescription
SupportedBoolean value that evaluates to True if the variable requires a stub, and False otherwise
DiagnosticList of strings with reasons why a variable might not require or not be supported for stubbing. If there is more than one string, the later strings act as further elaborations of the former strings.

If the object itself is used in a boolean context, its value translates to the value of the Supported supported property.

Examples

collapse all

Find global variables that are undefined and require stubs.

Import the required modules, create a project, and add the source files.

## Import modules
import polyspace.project
import os

## Create project
examples_path = os.path.join(polyspace.__install_path__, "polyspace",
                            "examples", "doc_pstest", "getting_started")

proj = polyspace.project.Project("undefinedGlobals.psprjx")

## Add source files and include path
proj.Code.Files.add(os.path.join(examples_path, "algo.c"))
proj.Code.Files.add(os.path.join(examples_path, "saturate.c"))
proj.IncludePaths.add(os.path.join(examples_path))

Parse the code to get information about the source code in the project.

codeInfo = polyspace.project.parseCode(proj)

Iterate over the global variables in the parsed source code. For each variable that is undefined and requires a stub, print the variable name and diagnostic reasons.

for g in codeInfo.Globals:
    if not g.StubbingSupport:
        print(f"Global: {g.Name}")
        for reason in g.StubbingSupport.Diagnostic:
            print(f"  Reason: {reason}")

You should see an output like the following:

Global: minValue
  Reason: Variable already defined
Global: maxValue
  Reason: Variable already defined
Global: ErrorMsg
  Reason: Variable already defined
Global: WarningMsg
  Reason: Variable already defined
Global: hasError
  Reason: Variable already defined

Version History

Introduced in R2025a

expand all