Main Content

polyspace.project.FunctionStub Class

Namespace: polyspace.project

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

Since R2026a

Description

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

Creation

Description

fcnStub = proj.FunctionStubs.create(functionToStub) creates a default stub for function functionToStub in a polyspace.project.Project object proj. The stub name is the name of the original function.

example

fcnStub = proj.FunctionStubs.create(functionToStub, PropertyName=Value) creates a stub for function functionToStub and assigns one or more of its properties during creation.

fcnStub = proj.FunctionStubs.createFrom(existingFunctionStubObj) creates a new function stub in the polyspace.project.Project object proj by copying an existing function stub object from another project.

Input Arguments

expand all

Function to stub, specified as a polyspace.project.Function object.

Get the polyspace.project.Function object for a function by parsing the code associated with a polyspace.project.Project object proj and using the getFunctionBySignature method for the resulting polyspace.project.CodeInfo object.

For example, use this code to get the polyspace.project.Function object for the function with signature void init(int) in the project proj:

codeInfo = polyspace.project.parseCode(proj)
functionToMock = codeInfo.getFunctionBySignature("void init(int)")

For more information, see polyspace.project.CodeInfo.

Existing function stub to copy from another project, specified as a polyspace.project.FunctionStub object.

Properties

expand all

Name of the function stub, specified as a string.

Names of arguments to the stubbed function, specified as an array of strings. Each stub argument has the same type as the type of the corresponding function argument.

If you do not set this property explicitly, the stub argument names are the same as the function argument names. For unnamed function arguments, the stub argument names are automatically generated.

Example: ["arg0", "arg1", "arg2"]

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"'

Code that goes inside the stub body, specified as a string.

Example: "int aGlobal1 = num;"

Variable declarations to export from the stub, specified as a string.

Example: "extern int statusVar;"

List of variables whose declarations are exported from a stub using the ExportedDeclarations property. You can use the variables exported from a stub to create inputs and assessments in a test. To do so, look up stub variables by name and access them from the inputs or assessments of a test.

Examples

collapse all

Identify all undefined functions in a project and create a default project-specific function 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("funcStubProject.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 Functions property that contains all the functions in the source code, returned as a list of polyspace.project.Function objects.

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

for f in codeInfo.Functions:
    if f.StubbingSupport:
        proj.FunctionStubs.create(f)

In many cases, the default stubs are all you need. If necessary, you can modify the default properties of the stub to create a custom stub implementation. For even greater customization, write your stubs in an external C/C++ source file. For more information see polyspace.project.Stubs.

Version History

Introduced in R2026a