メインコンテンツ

polyspace.project.Stubs Class

Namespace: polyspace.project

(Python) Manage external C/C++ stub files

Since R2026a

Description

Manage external C/C++ files containing stub implementations for undefined callees in a Polyspace® Platform project.

Creation

Description

proj.Stubs.Files.add(stubFile) adds the external C/C++ file stubFile to the polyspace.project.Project object proj. The file stubFile contains definitions for undefined functions and variables in the proj project.

example

proj.Stubs.Folders.add(stubFolder, Recursive) adds the folder stubFolder of external C/C++ files to the polyspace.project.Project object proj. Set the Recursive argument to False to exclude subfolders.

Input Arguments

expand all

Full or relative path to the C/C++ file containing stub definitions for undefined functions or variables in the project, specified as a string. Relative paths are considered relative to the location of the project.

Example: "myStubFile.c"

Example: "C:/Projects/StubFiles/myStubFile.cpp"

Full or relative path to a folder of stub files, specified as a string. Relative paths are considered relative to the location of the project.

Example: "C:/Projects/StubFiles"

Example: "StubFiles"

Option to include subfolders of the stubFolder folder, specified as True or False.

Properties

expand all

List of external C/C++ stub files added to the project, returned as a polyspace.project.SourceFileList object. Use the add, pop, and clear methods of the polyspace.project.SourceFileList object to manage the custom list of external stub files.

  • To add a C/C++ stub source file, use the Files.add() method.

    externalStub = proj.Stubs
    externalStub.Files.add("myStub.c")

  • To remove a stub source file using its name or index, use the Files.pop() method. If you do not provide an argument to the pop() method, the last file in the list is removed.

    externalStub = proj.Stubs
    externalStub.Files.pop("myStub.c")
    externalStub.Files.pop(2)
    

  • To remove all stub source files use the Files.clear() method.

    externalStub = proj.Stubs
    externalStub.Files.clear()
    

List of external stub folders added to the project, returned as a polyspace.project.SourceFolderList object. Use the add, pop, and clear methods of the polyspace.project.SourceFolderList object to manage the custom list of external stub folders.

  • To add folders containing stub source files, use the Folders.add() method. By default, the Recursive argument is set to True. Exclude subfolders by setting the Recursive argument to False.

    externalStub = proj.Stubs
    externalStub.Folders.add("source/stubs", Recursive=False)
    

  • To remove a stub source folder using its name or index, use the Folders.pop() method. If you do not provide an argument to the pop() method, the last folder in the list is removed.

    externalStub = proj.Stubs
    externalStub.Folders.pop("source/stubs")
    externalStub.Folders.pop(2)
    

  • To remove all stub source folders use the Folders.clear() method.

    externalStub = proj.Stubs
    externalStub.Folders.clear()
    

Examples

collapse all

Stub undefined functions and variables using an externally-authored C file.

Create a project 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("stubsProject.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 custom list of polyspace.project.Function objects. The codeInfo object also 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.Function and polyspace.project.Global objects to identify which functions and variables are undefined and require stubbing.

undefinedFuncList = [f.Signature for f in codeInfo.Functions if f.StubbingSupport]
undefinedVarList = [v.Name for v in codeInfo.Globals if v.StubbingSupport]

print("Undefined Function Signatures: ", undefinedFuncList)
print("Undefined Variable Names: ", undefinedVarList)
Undefined Function Signatures:  ['void printError(const char *)']
Undefined Variable Names:  ['minValue', 'maxValue']

Create a stub file. Use the file myStub.c, which has the following definitions:

/* Stub Definitions for Undefined Callees */

/* Header Dependencies */

/* Variable Definitions */
int minValue = -2;
int maxValue = 2;

/* Function Definitions */
void printError(const char *ErrorMsg) {}

Add the myStub.c stub file to the project and parse the code again.

proj.Stubs.Files.add(os.path.join(examples_path, "stubs", "myStub.c"))
codeInfo = polyspace.project.parseCode(proj)

All undefined functions and variables that were successfully stubbed through the external file myStub.c have their IsStub property set to True.

View the lists of functions and variables stubbed through the file myStub.c.

stubbedFuncList = [f.Signature for f in codeInfo.Functions if f.IsStub]
stubbedVarList = [v.Name for v in codeInfo.Globals if v.IsStub]

print("Stubbed Function Signatures: ", stubbedFuncList)
print("Stubbed Variable Names: ", stubbedVarList)
Stubbed Function Signatures:  ['void printError(const char *)']
Stubbed Variable Names:  ['maxValue', 'minValue']

Now that all undefined callees are stubbed, you can author your tests and build the project.

Version History

Introduced in R2026a