<commit_before>#  Copyright 2008-2015 Nokia Solutions and Networks
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  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.

import time

from robot.utils import (Sortable, py2to3, secs_to_timestr, timestr_to_secs,
                         IRONPYTHON, JYTHON, WINDOWS)
from robot.errors import TimeoutError, DataError, FrameworkError

if JYTHON:
    from .jython import Timeout
elif IRONPYTHON:
    from .ironpython import Timeout
elif WINDOWS:
    from .windows import Timeout
else:
    from .posix import Timeout


@py2to3
class _Timeout(Sortable):

    def __init__(self, timeout=None, message='', variables=None):
        self.string = timeout or ''
        self.message = message
        self.secs = -1
        self.starttime = -1
        self.error = None
        if variables:
            self.replace_variables(variables)

    @property
    def active(self):
        return self.starttime > 0

    def replace_variables(self, variables):
        try:
            self.string = variables.replace_string(self.string)
            if not self:
                return
            self.secs = timestr_to_secs(self.string)
            self.string = secs_to_timestr(self.secs)
            self.message = variables.replace_string(self.message)
        except (DataError, ValueError) as err:
            self.secs = 0.000001 # to make timeout active
            self.error = 'Setting %s timeout failed: %s' \
                    % (self.type.lower(), err.message)

    def start(self):
        if self.secs > 0:
            self.starttime = time.time()

    def time_left(self):
        if not self.active:
            return -1
        elapsed = time.time() - self.starttime
        # Timeout granularity is 1ms. Without rounding some timeout tests fail
        # intermittently on Windows, probably due to threading.Event.wait().
        return round(self.secs - elapsed, 3)<commit_msg>Fixed handling invalid timeout values with py3<commit_after>#  Copyright 2008-2015 Nokia Solutions and Networks
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  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.

import time

from robot.utils import (Sortable, py2to3, secs_to_timestr, timestr_to_secs,
                         IRONPYTHON, JYTHON, WINDOWS)
from robot.errors import TimeoutError, DataError, FrameworkError

if JYTHON:
    from .jython import Timeout
elif IRONPYTHON:
    from .ironpython import Timeout
elif WINDOWS:
    from .windows import Timeout
else:
    from .posix import Timeout


@py2to3
class _Timeout(Sortable):

    def __init__(self, timeout=None, message='', variables=None):
        self.string = timeout or ''
        self.message = message
        self.secs = -1
        self.starttime = -1
        self.error = None
        if variables:
            self.replace_variables(variables)

    @property
    def active(self):
        return self.starttime > 0

    def replace_variables(self, variables):
        try:
            self.string = variables.replace_string(self.string)
            if not self:
                return
            self.secs = timestr_to_secs(self.string)
            self.string = secs_to_timestr(self.secs)
            self.message = variables.replace_string(self.message)
        except (DataError, ValueError) as err:
            self.secs = 0.000001  # to make timeout active
            self.error = (u'Setting %s timeout failed: %s'
                          % (self.type.lower(), err))

    def start(self):
        if self.secs > 0:
            self.starttime = time.time()

    def time_left(self):
        if not self.active:
            return -1
        elapsed = time.time() - self.starttime
        # Timeout granularity is 1ms. Without rounding some timeout tests fail
        # intermittently on Windows, probably due to threading.Event.wait().
        return round(self.secs - elapsed, 3)
{'fraction_non_alphanumeric': 0.07915017704644865, 'fraction_numerical': 0.012705686315350968, 'mean_word_length': 2.4771904417089066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7135487', 'n_tokens_mistral': 1400, 'n_tokens_neox': 1345, 'n_words': 459}