oasis-open / cti-python-stix2

OASIS TC Open Repository: Python APIs for STIX 2
https://stix2.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
356 stars 113 forks source link

Complete pattern visitor #545

Closed chisholm closed 2 years ago

chisholm commented 2 years ago

Fixes #538 . Also completes work begun in PR #539 .

Hopefully all AST node classes are overridable now. Added a few unit tests.

This also addresses an issue where the module_suffix and module_name parameters to create_pattern_object() would be honored on first call and thereafter ignored. This happened because the class overrides were read and cached in a class-level field, so it was shared among all instances of the visitor. That means new visitor instances were using overrides set up for old instances. Unit tests uncovered this problem because the v20 and v21 unit tests use different AST node class overrides. Whoever ran first would set the class overrides and break the unit tests for whoever ran second.

The new implementation does not cache classes at all. It loads the module with the classes, and looks up classes directly on that module without caching anything. Each visitor instance can have a different module instance, so they each can have their own overrides.

rpiazza commented 2 years ago

@chisholm this looks good to me - much improved!