Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

matrix-org / synapse / 4532

23 Sep 2019 - 19:39 coverage decreased (-49.7%) to 17.596%
4532

Pull #6079

buildkite

Richard van der Hoff
update changelog
Pull Request #6079: Add submit_url response parameter to msisdn /requestToken

359 of 12986 branches covered (2.76%)

Branch coverage included in aggregate %.

0 of 7 new or added lines in 1 file covered. (0.0%)

18869 existing lines in 281 files now uncovered.

8809 of 39116 relevant lines covered (22.52%)

0.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

20.83
/synapse/util/stringutils.py
1
# -*- coding: utf-8 -*-
2
# Copyright 2014-2016 OpenMarket Ltd
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15

16
import random
1×
17
import string
1×
18

19
import six
1×
20
from six import PY2, PY3
1×
21
from six.moves import range
1×
22

23
_string_with_symbols = string.digits + string.ascii_letters + ".,;:^&*-_+=#~@"
1×
24

25
# random_string and random_string_with_symbols are used for a range of things,
26
# some cryptographically important, some less so. We use SystemRandom to make sure
27
# we get cryptographically-secure randoms.
28
rand = random.SystemRandom()
1×
29

30

31
def random_string(length):
1×
32
    return "".join(rand.choice(string.ascii_letters) for _ in range(length))
1×
33

34

35
def random_string_with_symbols(length):
1×
UNCOV
36
    return "".join(rand.choice(_string_with_symbols) for _ in range(length))
Branches [[0, 36], [0, 35]] missed. !
37

38

39
def is_ascii(s):
1×
40

UNCOV
41
    if PY3:
Branches [[0, 42], [0, 51]] missed. !
UNCOV
42
        if isinstance(s, bytes):
Branches [[0, 43], [0, 51]] missed. !
UNCOV
43
            try:
!
UNCOV
44
                s.decode("ascii").encode("ascii")
!
45
            except UnicodeDecodeError:
Branches [[0, 46], [0, 47]] missed. !
46
                return False
!
47
            except UnicodeEncodeError:
!
48
                return False
!
UNCOV
49
            return True
!
50

51
    try:
!
52
        s.encode("ascii")
!
53
    except UnicodeEncodeError:
Branches [[0, 54], [0, 55]] missed. !
54
        return False
!
55
    except UnicodeDecodeError:
!
56
        return False
!
57
    else:
58
        return True
!
59

60

61
def to_ascii(s):
1×
62
    """Converts a string to ascii if it is ascii, otherwise leave it alone.
63

64
    If given None then will return None.
65
    """
UNCOV
66
    if PY3:
Branches [[0, 67], [0, 69]] missed. !
UNCOV
67
        return s
!
68

69
    if s is None:
Branches [[0, 70], [0, 72]] missed. !
70
        return None
!
71

72
    try:
!
73
        return s.encode("ascii")
!
74
    except UnicodeEncodeError:
!
75
        return s
!
76

77

78
def exception_to_unicode(e):
1×
79
    """Helper function to extract the text of an exception as a unicode string
80

81
    Args:
82
        e (Exception): exception to be stringified
83

84
    Returns:
85
        unicode
86
    """
87
    # urgh, this is a mess. The basic problem here is that psycopg2 constructs its
88
    # exceptions with PyErr_SetString, with a (possibly non-ascii) argument. str() will
89
    # then produce the raw byte sequence. Under Python 2, this will then cause another
90
    # error if it gets mixed with a `unicode` object, as per
91
    # https://github.com/matrix-org/synapse/issues/4252
92

93
    # First of all, if we're under python3, everything is fine because it will sort this
94
    # nonsense out for us.
UNCOV
95
    if not PY2:
Branches [[0, 96], [0, 102]] missed. !
UNCOV
96
        return str(e)
!
97

98
    # otherwise let's have a stab at decoding the exception message. We'll circumvent
99
    # Exception.__str__(), which would explode if someone raised Exception(u'non-ascii')
100
    # and instead look at what is in the args member.
101

102
    if len(e.args) == 0:
Branches [[0, 103], [0, 104]] missed. !
103
        return ""
!
104
    elif len(e.args) > 1:
Branches [[0, 105], [0, 107]] missed. !
105
        return six.text_type(repr(e.args))
!
106

107
    msg = e.args[0]
!
108
    if isinstance(msg, bytes):
Branches [[0, 109], [0, 111]] missed. !
109
        return msg.decode("utf-8", errors="replace")
!
110
    else:
111
        return msg
!
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2019 Coveralls, LLC