From 53aab70fe6bdcd80a44ec3e14e0b858b4c479aca Mon Sep 17 00:00:00 2001 From: Lex Li Date: Mon, 14 Oct 2024 04:36:38 -0400 Subject: [PATCH] Fixed runtime errors. --- pysmi/borrower/base.py | 6 ++++-- pysmi/compiler.py | 43 +++++++++++++++++------------------------- pysmi/error.py | 11 +++++++++++ 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/pysmi/borrower/base.py b/pysmi/borrower/base.py index 7e7bc7d..e7125a7 100644 --- a/pysmi/borrower/base.py +++ b/pysmi/borrower/base.py @@ -5,11 +5,13 @@ # License: https://www.pysnmp.com/pysmi/license.html # from pysmi import debug, error +from pysmi.reader.base import AbstractReader class AbstractBorrower: genTexts = False exts = "" + _reader: "AbstractReader" def __init__(self, reader, genTexts=False): """Creates an instance of *Borrower* class. @@ -31,7 +33,7 @@ def __str__(self): return f"{self.__class__.__name__}{{{self._reader}, genTexts={self.genTexts}, exts={self.exts}}}" def set_options(self, **kwargs): - self._reader.setOptions(**kwargs) + self._reader.set_options(**kwargs) for k in kwargs: setattr(self, k, kwargs[k]) @@ -52,4 +54,4 @@ def get_data(self, mibname, **options): if "exts" not in options: options["exts"] = self.exts - return self._reader.getData(mibname, **options) + return self._reader.get_data(mibname, **options) diff --git a/pysmi/compiler.py b/pysmi/compiler.py index 155f5e9..0bddd19 100644 --- a/pysmi/compiler.py +++ b/pysmi/compiler.py @@ -10,15 +10,11 @@ import time import warnings -from pysmi.codegen.base import AbstractCodeGen -from pysmi.reader.base import AbstractReader -from pysmi.searcher.base import AbstractSearcher -from pysmi.writer.base import AbstractWriter - from pysmi import __name__ as package_name from pysmi import __version__ as package_version from pysmi import debug from pysmi import error +from pysmi.borrower.base import AbstractBorrower from pysmi.codegen.base import AbstractCodeGen from pysmi.codegen.symtable import SymtableCodeGen from pysmi.mibinfo import MibInfo @@ -83,6 +79,8 @@ class MibCompiler: indexFile = "index" _searchers: list[AbstractSearcher] _sources: list[AbstractReader] + _borrowers: list[AbstractBorrower] + _parsedMibs: dict[str, tuple] def __init__(self, parser, codegen: AbstractCodeGen, writer: AbstractWriter): """Creates an instance of *MibCompiler* class. @@ -286,8 +284,7 @@ class instances (values) ) continue - except error.PySmiError: - exc_class, exc, tb = sys.exc_info() + except error.PySmiError as exc: exc.source = source exc.mibname = mibname exc.msg += f" at MIB {mibname}" @@ -331,7 +328,7 @@ class instances (values) for searcher in self._searchers: try: searcher.file_exists( - mibname, fileInfo.mtime, rebuild=options.get("rebuild") + mibname, fileInfo.mtime, rebuild=options.get("rebuild") # type: ignore ) except error.PySmiFileNotFoundError: @@ -348,8 +345,7 @@ class instances (values) processed[mibname] = status_untouched break - except error.PySmiError: - exc_class, exc, tb = sys.exc_info() + except error.PySmiError as exc: exc.searcher = searcher exc.mibname = mibname exc.msg += f" at MIB {mibname}" @@ -412,8 +408,7 @@ class instances (values) f"{mibname} read from {fileInfo.path} and compiled by {self._writer}" ) - except error.PySmiError: - exc_class, exc, tb = sys.exc_info() + except error.PySmiError as exc: exc.handler = self._codegen exc.mibname = mibname exc.msg += f" at MIB {mibname}" @@ -447,7 +442,7 @@ class instances (values) f"trying to borrow {mibname} from {borrower}" ) try: - fileInfo, fileData = borrower.getData( + fileInfo, fileData = borrower.get_data( mibname, genTexts=options.get("genTexts") ) @@ -487,7 +482,7 @@ class instances (values) for searcher in self._searchers: try: searcher.file_exists( - mibname, fileInfo.mtime, rebuild=options.get("rebuild") + mibname, fileInfo.mtime, rebuild=options.get("rebuild") # type: ignore ) except error.PySmiFileNotFoundError: @@ -504,8 +499,7 @@ class instances (values) processed[mibname] = status_untouched break - except error.PySmiError: - exc_class, exc, tb = sys.exc_info() + except error.PySmiError as exc: exc.searcher = searcher exc.mibname = mibname exc.msg += f" at MIB {mibname}" @@ -570,7 +564,7 @@ class instances (values) try: if options.get("writeMibs", True): self._writer.put_data( - mibname, mibData, dryRun=options.get("dryRun") + mibname, mibData, dryRun=options.get("dryRun") # type: ignore ) debug.logger & debug.FLAG_COMPILER and debug.logger( @@ -624,17 +618,16 @@ def build_index(self, processedMibs, **options): ] try: - self._writer.putData( + self._writer.put_data( self.indexFile, self._codegen.genIndex( processedMibs, comments=comments, - old_index_data=self._writer.getData(self.indexFile), + old_index_data=self._writer.get_data(self.indexFile), ), - dryRun=options.get("dryRun"), + dryRun=options.get("dryRun"), # type: ignore ) - except error.PySmiError: - exc_class, exc, tb = sys.exc_info() + except error.PySmiError as exc: exc.msg += f" at MIB index {self.indexFile}" debug.logger & debug.FLAG_COMPILER and debug.logger( @@ -644,10 +637,7 @@ def build_index(self, processedMibs, **options): if options.get("ignoreErrors"): return - if hasattr(exc, "with_traceback"): - raise exc.with_traceback(tb) - else: - raise exc + raise exc # compatibility with legacy code # Old to new attribute mapping @@ -657,6 +647,7 @@ def build_index(self, processedMibs, **options): } def __getattr__(self, attr: str): + """Handle deprecated attributes.""" if new_attr := self.deprecated_attributes.get(attr): warnings.warn( f"{attr} is deprecated. Please use {new_attr} instead.", diff --git a/pysmi/error.py b/pysmi/error.py index c8a28da..924f999 100644 --- a/pysmi/error.py +++ b/pysmi/error.py @@ -14,7 +14,18 @@ # +from pysmi.codegen.base import AbstractCodeGen +from pysmi.reader.base import AbstractReader +from pysmi.searcher.base import AbstractSearcher + + class PySmiError(Exception): + msg: str + source: AbstractReader + mibname: str + searcher: AbstractSearcher + handler: AbstractCodeGen + def __init__(self, *args, **kwargs): Exception.__init__(self, *args) self.msg = args and args[0] or ""