Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/simplejson/errors.py: 79%

29 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:20 +0000

1"""Error classes used by simplejson 

2""" 

3__all__ = ['JSONDecodeError'] 

4 

5 

6def linecol(doc, pos): 

7 lineno = doc.count('\n', 0, pos) + 1 

8 if lineno == 1: 

9 colno = pos + 1 

10 else: 

11 colno = pos - doc.rindex('\n', 0, pos) 

12 return lineno, colno 

13 

14 

15def errmsg(msg, doc, pos, end=None): 

16 lineno, colno = linecol(doc, pos) 

17 msg = msg.replace('%r', repr(doc[pos:pos + 1])) 

18 if end is None: 

19 fmt = '%s: line %d column %d (char %d)' 

20 return fmt % (msg, lineno, colno, pos) 

21 endlineno, endcolno = linecol(doc, end) 

22 fmt = '%s: line %d column %d - line %d column %d (char %d - %d)' 

23 return fmt % (msg, lineno, colno, endlineno, endcolno, pos, end) 

24 

25 

26class JSONDecodeError(ValueError): 

27 """Subclass of ValueError with the following additional properties: 

28 

29 msg: The unformatted error message 

30 doc: The JSON document being parsed 

31 pos: The start index of doc where parsing failed 

32 end: The end index of doc where parsing failed (may be None) 

33 lineno: The line corresponding to pos 

34 colno: The column corresponding to pos 

35 endlineno: The line corresponding to end (may be None) 

36 endcolno: The column corresponding to end (may be None) 

37 

38 """ 

39 # Note that this exception is used from _speedups 

40 def __init__(self, msg, doc, pos, end=None): 

41 ValueError.__init__(self, errmsg(msg, doc, pos, end=end)) 

42 self.msg = msg 

43 self.doc = doc 

44 self.pos = pos 

45 self.end = end 

46 self.lineno, self.colno = linecol(doc, pos) 

47 if end is not None: 

48 self.endlineno, self.endcolno = linecol(doc, end) 

49 else: 

50 self.endlineno, self.endcolno = None, None 

51 

52 def __reduce__(self): 

53 return self.__class__, (self.msg, self.doc, self.pos, self.end)