主要内容

polyspace.project.VariableStub Class

Namespace: polyspace.project

(Python) Create and update project-specific stub for undefined variable

Since R2026a

Description

Create a project-specific stub for an undefined variable in the Polyspace® Platform project.

Creation

Description

varStub = proj.VariableStubs.create(variableToStub) creates a stub for the variable variableToStub in a polyspace.project.Project object proj. The stub name is the name of the original variable.

example

varStub = proj.VariableStubs.create(variableToStub, PropertyName=Value) creates a stub for variable variableToStub and assigns one or more of its properties during creation.

varStub = proj.VariableStubs.createFrom(existingVariableStubObj) creates a new variable stub in the polyspace.project.Project object proj by copying an existing variable stub object from another project.

Input Arguments

expand all

Variable to stub, specified as a polyspace.project.Global object.

Get the polyspace.project.Global object for a variable by parsing the code associated with a polyspace.project.Project object proj and using the getGlobalByName method for the resulting polyspace.project.CodeInfo object.

For example, use this code to get the polyspace.project.Global object for the variable named minValue in the project proj:

codeInfo = polyspace.project.parseCode(proj)
variableToStub = codeInfo.getGlobalByName("minValue")

For more information, see polyspace.project.CodeInfo.

Existing variable stub to copy from another project, specified as a polyspace.project.VariableStub object.

Properties

expand all

Name of the variable stub, specified as a string.

Code that goes outside the stub body, such as global variable declarations or type declarations, specified as a string. This code allows the stub definition to be compiled in isolation. In most cases, you add #include statements to header files containing the required declarations.

Example: '#include "types.h"'

Value of the stub variable, specified as a string. If the value does not matter for your test, you can leave it empty.

Example: "-2"

Examples

collapse all

Identify all undefined variables in a project and create a default project-specific stub for each.

Create the project, add your source files, and parse the code.

## Import modules
import polyspace.project
import os

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

proj = polyspace.project.Project("varStubProject.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 code
codeInfo = polyspace.project.parseCode(proj)

The codeInfo object has a Globals property that contains all the global variables in the source code, returned as a custom list of polyspace.project.Global objects.

Use the StubbingSupport property of the polyspace.project.Global objects to identify which variables are undefined and require stubbing. Write a for-loop to identify and create default stubs for all undefined variables in the project.

for v in codeInfo.Globals:
    if v.StubbingSupport:
        proj.VariableStubs.create(v)        

Set the Value of the minValue and maxValue variable stubs to -2 and 2, respectively.

## Set the Value property of the minValue variable stub to -2
minValueStub = proj.VariableStubs[0]
minValueStub.Value = "-2"

## Set the Value property of the maxValue variable stub to 2
maxValueStub = proj.VariableStubs[1]
maxValueStub.Value = "2"

Alternatively, for greater flexibility and reuse, write your stubs in an external C/C++ stub source file. For more information see polyspace.project.Stubs.

Version History

Introduced in R2026a