Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for named values of BITS used via types #6

Merged
merged 1 commit into from
Oct 21, 2024

Conversation

dcvmoole
Copy link

This is a bugfix for an actual pysmi code generation problem that I ran into. Summarizing by example, the following (simplified) construction:

someObject OBJECT-TYPE
    SYNTAX      BITS { bitZero(0), bitOne(1) }

...is parsed properly and makes available bitZero(0) and bitOne(1) for enumeration via someObject.getSyntax().namedValues, as it should. However, the following (effectively equivalent) alternative:

MyTextualConvention ::= TEXTUAL-CONVENTION
    SYNTAX      BITS { bitZero(0), bitOne(1) }

someObject OBJECT-TYPE
    SYNTAX      MyTextualConvention

...is also parsed properly, but does not make available bitZero(0) and bitOne(1) for enumeration via someObject.getSyntax().namedValues, nor in any other way for that matter. Such uses of types with BITS syntax are just as valid as using BITS directly, and so I can only conclude that this behavior is not intended. This issue seems to have been present since at least pysmi's switch to the use of a Jinja2 template. The problem is fortunately also limited to that template.

The attached fix addresses the problem, thus ensuring that the bit values also end up in namedValues in the second case. I have used my test script from PR #3 to ensure that this fix does not break anything in the mibs.pysnmp.com collection: the exact same statistics come out before and after. However, from that collection, 292 modules end up with additional named-values lists as a result of this fix, and I believe it is therefore purely an improvement for any users of those MIBs.

When an OBJECT-TYPE definition uses BITS in its SYNTAX, the resulting
Python code will add the definition of the bits (each with name and
number) to the object as named values, as is intended and expected.
However, when an OBJECT-TYPE definition uses a SYNTAX that refers to a
simple type (for SMIv1) or a textual convention (for SMIv2) that uses a
BITS syntax, then the resulting Python code does *not* make available
the bits as named values in any way. Such "indirect" use of BITS via
types is however perfectly valid as well, and indeed used in practice.

Fortunately, this problem is limited to the Python code generation
template, which simply does not generate the named values for the typed
(indirect) case. The necessary information is available internally.

This commit extends the template to generate the named values for
BITS-type simple types and textual conventions. That also makes the
generation consistent with similar constructions already present in the
template (e.g., for generation of named values for enumerated INTEGERs).
The change affects only MIBs whose generated Python code was missing the
named values for BITS.

New unit test cases are added for the cases that previously failed.
@lextm lextm merged commit bbec4f5 into lextudio:main Oct 21, 2024
17 of 19 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Oct 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants