Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/icalendar/config.py: 50%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

16 statements  

1"""Runtime configuration for icalendar.""" 

2 

3from __future__ import annotations 

4 

5from xml.etree.ElementTree import Element, ElementTree, parse 

6 

7MAX_ALARM_REPEAT: int = 10_000 

8"""Cap on additional triggers expanded from a ``VALARM`` ``REPEAT`` property. 

9 

10:rfc:`5545#section-3.8.6.2` defines ``REPEAT`` as an ``INTEGER`` data type, which is 

11defined in :rfc:`5545#section-3.3.8` as an integer between -2147483648 and 2147483647. 

12A value for ``REPEAT`` that is greater than approximately ``10_000`` can exhaust memory 

13or CPU. The default value of ``10_000`` is a practical setting, but may be adjusted to 

14specific use cases. Set to ``-1`` to disable the cap, which should be done only for 

15fully trusted input. 

16 

17.. versionadded:: 7.2.1 

18""" 

19 

20 

21def _clamp_repeat(n: int) -> int: 

22 """Return *n* clamped to ``[0, MAX_ALARM_REPEAT]``. 

23 

24 Negative values are treated as 0. When :data:`MAX_ALARM_REPEAT` is ``-1`` 

25 the value is returned unchanged after clamping negatives to 0. 

26 """ 

27 if n < 0: 

28 return 0 

29 if MAX_ALARM_REPEAT < 0: 

30 return n 

31 return min(n, MAX_ALARM_REPEAT) 

32 

33 

34def parse_xml(source) -> ElementTree[Element[str]]: 

35 """Parse xml from source. 

36 

37 This wraps :func:`xml.etree.ElementTree.parse`. 

38 It makes the XML parser configurable. 

39 

40 If you would like to use another XML parser, 

41 you can replace this function. 

42 

43 .. seealso:: 

44 

45 :ref:`safe-xml-parsing` 

46 

47 Parameters: 

48 source: A file-like object to with a ``read`` method. 

49 

50 Returns: 

51 The parsed XML tree. 

52 

53 Raises: 

54 TypeError: If the wrong type is passed to ``source``. 

55 

56 """ 

57 if not hasattr(source, "read"): 

58 raise TypeError("Expected source.read() method.") 

59 # S314 Using `xml` to parse untrusted data is known to be vulnerable to XML attacks 

60 return parse(source) # noqa: S314 

61 

62 

63__all__ = [ 

64 "MAX_ALARM_REPEAT", 

65 "parse_xml", 

66]