Skip to content

#4079 Continue and Break - #4136

Merged
pekkaklarck merged 50 commits into
robotframework:masterfrom
bbpatel2001:4079_C_B
Jan 11, 2022
Merged

pekkaklarck merged 50 commits into
robotframework:masterfrom
bbpatel2001:4079_C_B

Conversation

@bbpatel2001

Copy link
Copy Markdown
Contributor

No description provided.

bbpatel2001 and others added 18 commits October 27, 2021 23:42
Break and Continue
Continue and Break Lexers modified
Break and Continue Lexer added in blocklexers.py
Continue and Break definitions   added in Builtin.py
Implementation continue and break.
Initial commit
Initial commit for the Continue and Break statements.
Initial level review comments incorporated.
Exit and Continue for loop
Changes in BuiltIn file removed.
KeywordCallLexer was removed by mistake
White space removed.
Tried to add unit tests for the Break and Continue. [not done completely.]

@pekkaklarck pekkaklarck left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Work still needed to actually build Continue and Break model objects and execute and log them. See individual comments for things to improve with the current code and tests.

Comment thread src/robot/parsing/model/statements.py Resolved
Comment thread src/robot/parsing/model/statements.py Resolved
Comment thread src/robot/running/model.py Outdated


@Body.register
class Continue(model.Return):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be Continue and Break classes in the model package that are extended here. They are also needed in the result package.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added in result pkg but need you guidance a not able to understand this pkg fully.

Comment thread src/robot/running/model.py Outdated
__slots__ = ['lineno']

def __init__(self, values=(), parent=None, lineno=None):
model.Return.__init__(self, values, parent)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither CONTINUE nor BREAK accepts values in data (RETURN does) so model objects shouldn't have values arguments either.

@bbpatel2001 bbpatel2001 Jan 4, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed following code blocked and directly raised... Is this correct or should I just remove values .... ?
with StatusReporter(self, ReturnResult(self.values), context, run):
if run:
raise ExitForLoop(self.values)

New Code:

 def run(self, context, run=True, templated=False):
        raise ContinueForLoop()
   def run(self, context, run=True, templated=False):
    raise ExitForLoop()

self._verify(data, expected)

def test_in_test(self):
# This is not valid usage but that's not recognized during lexing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's true using CONTINUE and BREAK directly under a test isn't valid, but using them directly inside a user keyword isn't valid either. It should be possible to use them only inside FOR either directly or inside IF and TRY structures. This is different to RETURN that can be used anywhere inside a user keyword, also directly under them. In other words, this comment applies also to the above test.

Notice that it's possible to use Continue For Loop and Exit For Loop keywords directly inside user keywords as long as those user keywords are used inside a loop. That's bad design and not something I want to repeat with CONTINUE and BREAK. Although those keywords will be eventually deprecated and removed, I wouldn't like to change their behavior to avoid backwards incompatible change. We need to see can that be handled nicely when we get further with CONTINUE/BREAK.

Notice also that I have an idea how these error situations could be detected already during lexing/parsing. I can look at that after this PR has been merged and update tests then.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I am following all your points mentioned here however I have modified test little.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ought to be fine. Can be changed later if parser is enhanced to detect RETURN and others when used in a wrong place. That's not something this PR needs to care.

Comment thread utest/parsing/test_lexer.py Resolved

@pekkaklarck pekkaklarck left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parser seems to be now in pretty good shape. What's missing is transforming the parsing model into an executable model.

Comment thread src/robot/running/model.py Outdated
__slots__ = ['lineno']

def __init__(self, values=(), parent=None, lineno=None):
model.Return.__init__(self)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be model.Continue, not model.Return here! Even better would be using super() that we have now started to use when we don't need to care about Python 2 (super() syntax was different with it). Also need to pass parent to the parent class. In other words, this should be super().__init__(parent).

Comment thread src/robot/running/model.py Outdated
__slots__ = ['lineno']

def __init__(self, values=(), parent=None, lineno=None):
model.Return.__init__(self)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problems as with Continue here as well.

Comment thread utest/parsing/test_lexer.py Resolved
Comment thread utest/parsing/test_lexer.py Resolved
Comment thread utest/parsing/test_lexer.py Outdated

def test_in_keyword(self):
data = ' BREAK'
expected = [(T.CONTINUE, 'BREAK', 3, 4),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be T.BREAK here and in tests below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

corrected

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected.

Comment thread utest/parsing/test_model.py Outdated
),
body=[
KeywordCall([Token(Token.KEYWORD, 'Break', 3, 4)]),
ReturnStatement([Token(Token.BREAK, 'BREAK', 4, 4)])

@pekkaklarck pekkaklarck Jan 5, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be Continue, not ReturnStatement here!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

corrected.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected.

self._verify(data, expected)

def test_in_test(self):
# This is not valid usage but that's not recognized during lexing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ought to be fine. Can be changed later if parser is enhanced to detect RETURN and others when used in a wrong place. That's not something this PR needs to care.

@pekkaklarck pekkaklarck left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great progress!


EndLexer, ReturnLexer, ContinueLexer, BreakLexer)

class BlockLexer(Lexer):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An empty line has been accidentally removed. PEP-8 says there should be two such lines between imports and the class declaration.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected.

Comment thread src/robot/parsing/lexer/blocklexers.py Outdated
def lexer_classes(self):
return (InlineIfLexer, IfHeaderLexer, ElseIfHeaderLexer, ElseHeaderLexer,
ForLexer, TryLexer, EndLexer, ReturnLexer, KeywordCallLexer)
ForLexer, TryLexer, EndLexer, ReturnLexer, KeywordCallLexer, ContinueLexer, BreakLexer)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not absolutely sure about this, but I believe these new lexers should be before KeywordCallLexer here and in other lexer_classes. Otherwise BREAK and CONTINUE are likely to be lexed as keyword calls.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected.

Comment thread src/robot/result/model.py Outdated
__slots__ = ['status', 'starttime', 'endtime']

def __init__(self, values=(), status='FAIL', starttime=None, endtime=None, parent=None):
super().__init__(values, parent)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continue shouldn't accept values. Same also with Break.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had checked this, and I was thinking that it will be a empty, so did not remove.
Removing now.

def visit_Break(self, node):
self.test.body.create_keyword(name='BREAK',lineno=node.lineno)

class KeywordBuilder(NodeVisitor):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. When mapping CONTINUE and BREAK used outside a loop to a keyword call, we should pass also their possible arguments. That's done with RETURN above, but using node.values isn't possible here because CONTINUE and BREAK nodes don't currently have such attribute. Probably NoArgumentsHeader should get this property to allow that:
     @property
     def values(self):
         return self.get_values(Token.ARGUMENT)
  2. There should be two empty lines between classes.

@bpateldg bpateldg Jan 7, 2022 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added above property in the NoArgumentsHeader.

In this case, I am adding back args=node.values to the method.

def visit_Continue(self, node):
self.test.body.create_keyword(name='CONTINUE', args=node.values,
lineno=node.lineno)

Comment thread src/robot/running/model.py Outdated
from robot import model
from robot.conf import RobotSettings
from robot.errors import ReturnFromKeyword
from robot.errors import ContinueForLoop, ExitForLoop

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combine the above two import lines together.

__slots__ = ['lineno']

def __init__(self, values=(), parent=None, lineno=None):
super().__init__(parent)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continue, and Break, shouldn't accept values.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected. Thought it will be empty so had kept.

Removed

@pekkaklarck pekkaklarck left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good progress again! Small change still needed to get BREAK and CONTINUE visible in log.


def __init__(self, parent=None):
self.parent = parent
#self.body = None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the commented code here and in Break. These structures don't have body.

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove accidentally added whitespace.


def start(self, elem, result):
return result.body.create_break()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to creating these handlers for <break> and <continue>, they need to be listed in children for IterationHandler and BranchHandler. After this change BREAK and CONTINUE should be visible in log as well.

<return> is also used with KeywordHandler, but these statements aren't usable directly under keywords so that shouldn't be needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants