Skip to content

Commit

Permalink
fix: don't overwrite tracebacklimit when parsing
Browse files Browse the repository at this point in the history
This commit updates the __call__ method of both DomainParser and ProblemParser classes in such a way that sys.tracebacklimit is not overwritten to 'None' if it has been already set by the caller.
  • Loading branch information
marcofavorito committed Dec 31, 2023
1 parent 538a78d commit 50a2f24
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
31 changes: 31 additions & 0 deletions pddl/helpers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""Helper functions."""

import re
import sys
from pathlib import Path
from typing import (
AbstractSet,
Expand All @@ -25,8 +26,11 @@
Sequence,
Set,
Type,
TypeVar,
)

from lark import Lark, Transformer


def _get_current_path() -> Path:
"""Get the path to the file where the function is called."""
Expand Down Expand Up @@ -191,3 +195,30 @@ def find_cycle(graph: Dict[str, Optional[AbstractSet[str]]]) -> Optional[Sequenc
stack.append((neighbor, path + [current]))

return None


T = TypeVar("T")


def call_parser(text: str, parser: Lark, transformer: Transformer[Any, T]) -> T:
"""
Parse a text with a Lark parser and transformer.
To produce a better traceback in case of an error, the function will temporarily overwrite the sys.tracebacklimit
value of the current interpreter.
:param text: the text to parse
:param parser: the Lark parser object
:param transformer: the Lark transformer object
:return: the object returned by the parser
"""
old_tracebacklimit = getattr(sys, "tracebacklimit", None)
try:
sys.tracebacklimit = 0 # noqa
tree = parser.parse(text)
sys.tracebacklimit = None # type: ignore
result = transformer.transform(tree)
finally:
if old_tracebacklimit is not None:
sys.tracebacklimit = old_tracebacklimit
return result
9 changes: 2 additions & 7 deletions pddl/parser/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#

"""Implementation of the PDDL domain parser."""
import sys
from typing import Any, Dict, Optional, Set, Tuple

from lark import Lark, ParseError, Transformer
Expand All @@ -20,7 +19,7 @@
from pddl.core import Domain
from pddl.custom_types import name
from pddl.exceptions import PDDLMissingRequirementError, PDDLParsingError
from pddl.helpers.base import assert_
from pddl.helpers.base import assert_, call_parser
from pddl.logic.base import And, ExistsCondition, ForallCondition, Imply, Not, OneOf, Or
from pddl.logic.effects import AndEffect, Forall, When
from pddl.logic.functions import Assign, Decrease, Divide
Expand Down Expand Up @@ -462,8 +461,4 @@ def __init__(self):

def __call__(self, text: str) -> Domain:
"""Call."""
sys.tracebacklimit = 0 # noqa
tree = self._parser.parse(text)
sys.tracebacklimit = None # type: ignore
formula = self._transformer.transform(tree)
return formula
return call_parser(text, self._parser, self._transformer)
11 changes: 3 additions & 8 deletions pddl/parser/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
#

"""Implementation of the PDDL problem parser."""
import sys
from typing import Any, Dict

from lark import Lark, ParseError, Transformer

from pddl.core import Problem
from pddl.exceptions import PDDLParsingError
from pddl.helpers.base import assert_
from pddl.helpers.base import assert_, call_parser
from pddl.logic.base import And, Not
from pddl.logic.functions import Divide
from pddl.logic.functions import EqualTo as FunctionEqualTo
Expand Down Expand Up @@ -242,10 +241,6 @@ def __init__(self):
_problem_parser_lark, parser="lalr", import_paths=[PARSERS_DIRECTORY]
)

def __call__(self, text) -> Problem:
def __call__(self, text: str) -> Problem:
"""Call."""
sys.tracebacklimit = 0 # noqa
tree = self._parser.parse(text)
sys.tracebacklimit = None # type: ignore
formula = self._transformer.transform(tree)
return formula
return call_parser(text, self._parser, self._transformer)

0 comments on commit 50a2f24

Please sign in to comment.