def testExceptionContextTraceback(self): exception_context = None try: int('bogus number') except ValueError as e: exception_context = exceptions.ExceptionContext(e) self.assertIsNotNone(exception_context) try: exception_context.Reraise() except ValueError as e: context = exceptions.ExceptionContext(e) traceback_lines = traceback.format_tb(context._traceback) self.assertTrue( any("int('bogus number')" in x for x in traceback_lines)) else: self.fail('ValueError exception not raised')
def testExceptionContext(self): exception_context = None try: int('bogus number') except ValueError as e: exception_context = exceptions.ExceptionContext(e) self.assertIsNotNone(exception_context) with self.assertRaises(ValueError): exception_context.Reraise()
def testExceptionContextOutsideExceptClause(self): exception_value = None try: int('bogus number') except ValueError as e: exception_value = e self.assertIsNotNone(exception_value) # Python 2 retains sys.exc_info until exiting the frame where it was caught, # so clear it manually here. if six.PY2: sys.exc_clear() with self.assertRaises(exceptions.InternalError): exceptions.ExceptionContext(exception_value)
def ParseDateTime(string, fmt=None, tzinfo=LOCAL): """Parses a date/time string and returns a datetime.datetime object. Args: string: The date/time string to parse. This can be a parser.parse() date/time or an ISO 8601 duration after Now(tzinfo) or before if prefixed by '-'. fmt: The input must satisfy this strptime(3) format string. tzinfo: A default timezone tzinfo object to use if string has no timezone. Raises: DateTimeSyntaxError: Invalid date/time/duration syntax. DateTimeValueError: A date/time numeric constant exceeds its range. Returns: A datetime.datetime object for the given date/time string. """ # Check explicit format first. if fmt: dt = _StrPtime(string, fmt) if tzinfo and not dt.tzinfo: dt = dt.replace(tzinfo=tzinfo) return dt # Use tzgetter to determine if string contains an explicit timezone name or # offset. defaults = GetDateTimeDefaults(tzinfo=tzinfo) tzgetter = _TzInfoOrOffsetGetter() exc = None try: dt = parser.parse(string, tzinfos=tzgetter.Get, default=defaults) if tzinfo and not tzgetter.timezone_was_specified: # The string had no timezone name or offset => localize dt to tzinfo. dt = parser.parse(string, tzinfos=None, default=defaults) dt = dt.replace(tzinfo=tzinfo) return dt except OverflowError as e: exc = exceptions.ExceptionContext(DateTimeValueError(six.text_type(e))) except (AttributeError, ValueError, TypeError) as e: exc = exceptions.ExceptionContext(DateTimeSyntaxError( six.text_type(e))) if not tzgetter.timezone_was_specified: # Good ole parser.parse() has a tzinfos kwarg that it sometimes ignores. # Compensate here when the string ends with a tz. prefix, explicit_tzinfo = _SplitTzFromDate(string) if explicit_tzinfo: try: dt = parser.parse(prefix, default=defaults) except OverflowError as e: exc = exceptions.ExceptionContext( DateTimeValueError(six.text_type(e))) except (AttributeError, ValueError, TypeError) as e: exc = exceptions.ExceptionContext( DateTimeSyntaxError(six.text_type(e))) else: return dt.replace(tzinfo=explicit_tzinfo) try: # Check if it's an iso_duration string. return ParseDuration(string).GetRelativeDateTime(Now(tzinfo=tzinfo)) except Error: # Not a duration - reraise the datetime parse error. exc.Reraise()