Python simplejson.JSONDecodeError() Examples

The following are code examples for showing how to use simplejson.JSONDecodeError(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: sd-agent-core-plugins   Author: serverdensity   File: check.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _rest_request_to_json(self, address, object_path, service_name, *args, **kwargs):
        '''
        Query the given URL and return the JSON response
        '''
        response = self._rest_request(address, object_path, service_name, *args, **kwargs)

        try:
            response_json = response.json()

        except JSONDecodeError as e:
            self.service_check(service_name,
                AgentCheck.CRITICAL,
                tags=['url:%s' % self._get_url_base(address)],
                message='JSON Parse failed: {0}'.format(e))
            raise

        return response_json 
Example 2
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_unicode.py    MIT License 6 votes vote down vote up
def test_invalid_escape_sequences(self):
        # incomplete escape sequence
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u12')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u123')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1234')
        # invalid escape sequence
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u123x"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u12x4"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1x34"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\ux234"')
        if sys.maxunicode > 65535:
            # invalid escape sequence for low surrogate
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u0"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u00"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u000"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u000x"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u00x0"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u0x00"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\ux000"') 
Example 3
Project: tra-tracking-on-gae   Author: elliot79313   File: test_unicode.py    MIT License 6 votes vote down vote up
def test_invalid_escape_sequences(self):
        # incomplete escape sequence
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u12')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u123')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1234')
        # invalid escape sequence
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u123x"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u12x4"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1x34"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\ux234"')
        if sys.maxunicode > 65535:
            # invalid escape sequence for low surrogate
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u0"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u00"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u000"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u000x"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u00x0"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u0x00"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\ux000"') 
Example 4
Project: ableton-live-webapi   Author: valsteen   File: test_unicode.py    The Unlicense 6 votes vote down vote up
def test_invalid_escape_sequences(self):
        # incomplete escape sequence
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u12')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u123')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1234')
        # invalid escape sequence
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u123x"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u12x4"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1x34"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\ux234"')
        if sys.maxunicode > 65535:
            # invalid escape sequence for low surrogate
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u0"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u00"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u000"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u000x"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u00x0"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u0x00"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\ux000"') 
Example 5
Project: pytablereader   Author: thombashi   File: core.py    MIT License 6 votes vote down vote up
def load_dict(self):
        self._validate()
        self._logger.logging_load()
        self.encoding = get_file_encoding(self.source, self.encoding)

        buffer = []
        with io.open(self.source, "r", encoding=self.encoding) as fp:
            for line_idx, line in enumerate(fp):
                line = line.strip()
                if not line:
                    continue

                try:
                    buffer.append(json.loads(line, object_pairs_hook=OrderedDict))
                except JSONDecodeError as e:
                    raise ValidationError(
                        "line {line_idx}: {msg}: {value}".format(
                            line_idx=line_idx + 1, msg=e, value=line
                        )
                    )

        return buffer 
Example 6
Project: pytablereader   Author: thombashi   File: core.py    MIT License 6 votes vote down vote up
def load_dict(self):
        self._validate()
        self._logger.logging_load()

        buffer = []
        for line_idx, line in enumerate(self.source.splitlines()):
            line = line.strip()
            if not line:
                continue

            try:
                buffer.append(json.loads(line, object_pairs_hook=OrderedDict))
            except JSONDecodeError as e:
                raise ValidationError(
                    "line {line_idx}: {msg}: {value}".format(
                        line_idx=line_idx + 1, msg=e, value=line
                    )
                )

        return buffer 
Example 7
Project: eclcli   Author: nttcom   File: api.py    Apache License 2.0 6 votes vote down vote up
def create(
        self,
        url,
        session=None,
        method=None,
        **params
    ):
        """Create a new resource

        :param string url:
            The API-specific portion of the URL path
        :param Session session:
            HTTP client session
        :param string method:
            HTTP method (default POST)
        """

        if not method:
            method = 'POST'
        ret = self._request(method, url, session=session, **params)
        # Should this move into _requests()?
        try:
            return ret.json()
        except json.JSONDecodeError:
            return ret 
Example 8
Project: deep-Q-network-trader   Author: Jaewan-Yun   File: load_data.py    MIT License 6 votes vote down vote up
def __request_slice(symbol, start, end, granularity):
    client = gdax.PublicClient()
    retries = 20
    for retry_count in range(retries):
        try:
            r = client.get_historic_rates(symbol, start, end, granularity)
        except JSONDecodeError:
            print('JSONDecodeError | retrying')
            continue

        if 'message' in r and r['message'] == 'Rate limit exceeded':
            print(r)
            time.sleep(1.5 ** retry_count)
        else:
            print('date:', start, '| fetch successful | retries:', retry_count)
            return r 
Example 9
Project: nefertari   Author: ramses-tech   File: view.py    Apache License 2.0 6 votes vote down vote up
def prepare_request_params(self, _query_params, _json_params):
        """ Prepare query and update params. """
        self._query_params = dictset(
            _query_params or self.request.params.mixed())
        self._json_params = dictset(_json_params)

        ctype = self.request.content_type
        if self.request.method in ['POST', 'PUT', 'PATCH']:
            if ctype == 'application/json':
                try:
                    self._json_params.update(self.request.json)
                except simplejson.JSONDecodeError:
                    log.error(
                        "Expecting JSON. Received: '{}'. "
                        "Request: {} {}".format(
                            self.request.body, self.request.method,
                            self.request.url))

            self._json_params = BaseView.convert_dotted(self._json_params)
            self._query_params = BaseView.convert_dotted(self._query_params)

        self._params = self._query_params.copy()
        self._params.update(self._json_params) 
Example 10
Project: homeassistant-config   Author: eifinger   File: alarm_control_panel.py    MIT License 6 votes vote down vote up
def init(self):
        """Initialize."""
        try:
            from simplejson import JSONDecodeError
            data = await self.alexa_api.get_guard_details(self._login)
            guard_dict = (data['locationDetails']
                          ['locationDetails']['Default_Location']
                          ['amazonBridgeDetails']['amazonBridgeDetails']
                          ['LambdaBridge_AAA/OnGuardSmartHomeBridgeService']
                          ['applianceDetails']['applianceDetails'])
        except (KeyError, TypeError, JSONDecodeError):
            guard_dict = {}
        for _, value in guard_dict.items():
            if value['modelName'] == "REDROCK_GUARD_PANEL":
                self._appliance_id = value['applianceId']
                self._guard_entity_id = value['entityId']
                self._friendly_name += " " + self._appliance_id[-5:]
                _LOGGER.debug("%s: Discovered %s: %s %s",
                              self.account,
                              self._friendly_name,
                              self._appliance_id,
                              self._guard_entity_id)
        if not self._appliance_id:
            _LOGGER.debug("%s: No Alexa Guard entity found", self.account) 
Example 11
Project: fund   Author: Frank-qlu   File: test_unicode.py    Apache License 2.0 6 votes vote down vote up
def test_invalid_escape_sequences(self):
        # incomplete escape sequence
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u12')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u123')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1234')
        # invalid escape sequence
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u123x"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u12x4"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1x34"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\ux234"')
        if sys.maxunicode > 65535:
            # invalid escape sequence for low surrogate
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u0"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u00"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u000"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u000x"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u00x0"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u0x00"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\ux000"') 
Example 12
Project: vidcutter   Author: ozmartian   File: updater.py    GNU General Public License v3.0 6 votes vote down vote up
def done(self, reply: QNetworkReply) -> None:
        if reply.error() != QNetworkReply.NoError:
            self.logger.error(reply.errorString())
            sys.stderr.write(reply.errorString())
            return
        if os.getenv('DEBUG', False):
            self.log_request(reply)
        try:
            jsonobj = loads(str(reply.readAll(), 'utf-8'))
            reply.deleteLater()
            latest = jsonobj.get('tag_name')
            current = qApp.applicationVersion()
            self.mbox.show_result(latest, current)
        except JSONDecodeError:
            self.logger.exception('Updater JSON decoding error', exc_info=True)
            raise 
Example 13
Project: schematizer   Author: Yelp   File: schema_migrations.py    Apache License 2.0 6 votes vote down vote up
def get_schema_migration(request):
    new_schema_json = request.json_body.get('new_schema')
    old_schema_json = request.json_body.get('old_schema')
    target_schema_type = request.json_body.get('target_schema_type')

    _get_migration = SCHEMA_MIGRATION_STRATEGY_MAP.get(target_schema_type)
    if _get_migration is None:
        raise exceptions_v1.unsupported_target_schema_exception()

    try:
        return _get_migration(
            new_avro_schema=json.loads(new_schema_json),
            old_avro_schema=json.loads(old_schema_json)
            if old_schema_json else {}
        )
    except json.JSONDecodeError:
        raise exceptions_v1.invalid_schema_exception() 
Example 14
Project: schematizer   Author: Yelp   File: compatibility.py    Apache License 2.0 6 votes vote down vote up
def is_avro_schema_compatible(request):
    try:
        req = requests_v1.AvroSchemaCompatibilityRequest(**request.json_body)
        return _is_schema_compatible(
            req.schema_json,
            req.namespace,
            req.source
        )
    except simplejson.JSONDecodeError as e:
        log.exception("Failed to construct AvroSchemaCompatibilityRequest. {}"
                      .format(request.json_body))
        raise exceptions_v1.invalid_schema_exception(
            'Error "{error}" encountered decoding JSON: "{schema}"'.format(
                error=str(e),
                schema=request.json_body['schema']
            )
        ) 
Example 15
Project: oscars2016   Author: 0x0ece   File: test_unicode.py    Apache License 2.0 6 votes vote down vote up
def test_invalid_escape_sequences(self):
        # incomplete escape sequence
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u12')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u123')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1234')
        # invalid escape sequence
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u123x"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u12x4"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1x34"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\ux234"')
        if sys.maxunicode > 65535:
            # invalid escape sequence for low surrogate
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u0"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u00"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u000"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u000x"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u00x0"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u0x00"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\ux000"') 
Example 16
Project: that-startpage-rocks   Author: the-duck   File: test_unicode.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_invalid_escape_sequences(self):
        # incomplete escape sequence
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u12')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u123')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1234')
        # invalid escape sequence
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u123x"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u12x4"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1x34"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\ux234"')
        if sys.maxunicode > 65535:
            # invalid escape sequence for low surrogate
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u0"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u00"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u000"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u000x"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u00x0"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u0x00"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\ux000"') 
Example 17
Project: cohesive-networks-vns3-vpn-monitoring   Author: trek10inc   File: test_unicode.py    MIT License 6 votes vote down vote up
def test_invalid_escape_sequences(self):
        # incomplete escape sequence
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u12')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u123')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1234')
        # invalid escape sequence
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u123x"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u12x4"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\u1x34"')
        self.assertRaises(json.JSONDecodeError, json.loads, '"\\ux234"')
        if sys.maxunicode > 65535:
            # invalid escape sequence for low surrogate
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u0"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u00"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u000"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u000x"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u00x0"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\u0x00"')
            self.assertRaises(json.JSONDecodeError, json.loads, '"\\ud800\\ux000"') 
Example 18
Project: pennai   Author: EpistasisLab   File: test_get_metafeatures.py    GNU General Public License v3.0 5 votes vote down vote up
def test_validate_main_good(self, file_path, target, identifier_type):
        result = io.StringIO()
        testargs = ["program.py", file_path]

        if target: testargs.extend(['-target', target])
        if identifier_type: testargs.extend(['-identifier_type', identifier_type])

        logger.debug("testargs: " + str(testargs))

        with patch.object(sys, 'argv', testargs):
            sys.stdout = result
            mf.main()
            sys.stdout = sys.__stdout__

        logger.debug(f'result:\n=====\n{result.getvalue()}\n=====')

        self.assertTrue(result.getvalue())
        
        objResult = None
        try:
            objResult = simplejson.loads(result.getvalue())
        except simplejson.JSONDecodeError as e:
            self.assertIsNone(e)

        self.assertIsInstance(objResult, dict)
        self.assertNotIn("success", objResult.keys())
        self.assertIn("n_classes", objResult.keys()) 
Example 19
Project: pennai   Author: EpistasisLab   File: test_get_metafeatures.py    GNU General Public License v3.0 5 votes vote down vote up
def test_validate_main_bad(self, file_path, target, identifier_type):
        result = io.StringIO()
        testargs = ["program.py", file_path]

        if target: testargs.extend(['-target', target])
        if identifier_type: testargs.extend(['-identifier_type', identifier_type])

        logger.debug("testargs: " + str(testargs))

        with patch.object(sys, 'argv', testargs):
            sys.stdout = result
            mf.main()
            sys.stdout = sys.__stdout__

        logger.debug(f'result:\n=====\n{result.getvalue()}\n=====')

        self.assertTrue(result.getvalue())
        
        objResult = None
        try:
            objResult = simplejson.loads(result.getvalue())
        except simplejson.JSONDecodeError as e:
            self.assertIsNone(e)

        self.assertIsInstance(objResult, dict)
        self.assertIn("success", objResult.keys())
        self.assertFalse(objResult['success']) 
Example 20
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_errors.py    MIT License 5 votes vote down vote up
def test_decode_error(self):
        err = None
        try:
            json.loads('{}\na\nb')
        except json.JSONDecodeError:
            err = sys.exc_info()[1]
        else:
            self.fail('Expected JSONDecodeError')
        self.assertEqual(err.lineno, 2)
        self.assertEqual(err.colno, 1)
        self.assertEqual(err.endlineno, 3)
        self.assertEqual(err.endcolno, 2) 
Example 21
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_errors.py    MIT License 5 votes vote down vote up
def test_scan_error(self):
        err = None
        for t in (text_type, b):
            try:
                json.loads(t('{"asdf": "'))
            except json.JSONDecodeError:
                err = sys.exc_info()[1]
            else:
                self.fail('Expected JSONDecodeError')
            self.assertEqual(err.lineno, 1)
            self.assertEqual(err.colno, 10) 
Example 22
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_errors.py    MIT License 5 votes vote down vote up
def test_error_is_pickable(self):
        err = None
        try:
            json.loads('{}\na\nb')
        except json.JSONDecodeError:
            err = sys.exc_info()[1]
        else:
            self.fail('Expected JSONDecodeError')
        s = pickle.dumps(err)
        e = pickle.loads(s)

        self.assertEqual(err.msg, e.msg)
        self.assertEqual(err.doc, e.doc)
        self.assertEqual(err.pos, e.pos)
        self.assertEqual(err.end, e.end) 
Example 23
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_fail.py    MIT License 5 votes vote down vote up
def test_failures(self):
        for idx, doc in enumerate(JSONDOCS):
            idx = idx + 1
            if idx in SKIPS:
                json.loads(doc)
                continue
            try:
                json.loads(doc)
            except json.JSONDecodeError:
                pass
            else:
                self.fail("Expected failure for fail%d.json: %r" % (idx, doc)) 
Example 24
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_fail.py    MIT License 5 votes vote down vote up
def test_truncated_input(self):
        test_cases = [
            ('', 'Expecting value', 0),
            ('[', "Expecting value or ']'", 1),
            ('[42', "Expecting ',' delimiter", 3),
            ('[42,', 'Expecting value', 4),
            ('["', 'Unterminated string starting at', 1),
            ('["spam', 'Unterminated string starting at', 1),
            ('["spam"', "Expecting ',' delimiter", 7),
            ('["spam",', 'Expecting value', 8),
            ('{', 'Expecting property name enclosed in double quotes', 1),
            ('{"', 'Unterminated string starting at', 1),
            ('{"spam', 'Unterminated string starting at', 1),
            ('{"spam"', "Expecting ':' delimiter", 7),
            ('{"spam":', 'Expecting value', 8),
            ('{"spam":42', "Expecting ',' delimiter", 10),
            ('{"spam":42,', 'Expecting property name enclosed in double quotes',
             11),
            ('"', 'Unterminated string starting at', 0),
            ('"spam', 'Unterminated string starting at', 0),
            ('[,', "Expecting value", 1),
        ]
        for data, msg, idx in test_cases:
            try:
                json.loads(data)
            except json.JSONDecodeError:
                e = sys.exc_info()[1]
                self.assertEqual(
                    e.msg[:len(msg)],
                    msg,
                    "%r doesn't start with %r for %r" % (e.msg, msg, data))
                self.assertEqual(
                    e.pos, idx,
                    "pos %r != %r for %r" % (e.pos, idx, data))
            except Exception:
                e = sys.exc_info()[1]
                self.fail("Unexpected exception raised %r %s" % (e, e))
            else:
                self.fail("Unexpected success parsing '%r'" % (data,)) 
Example 25
Project: Serverless-Design-Patterns-and-Best-Practices   Author: PacktPublishing   File: decorators.py    MIT License 5 votes vote down vote up
def decode_json(fn):
    def _decode_json_from_payload(payload):
        try:
            json_payload = json.loads(payload['body'])
        except (TypeError, json.JSONDecodeError) as e:
            json_payload = None
        return fn(json_payload)
    return _decode_json_from_payload 
Example 26
Project: Serverless-Design-Patterns-and-Best-Practices   Author: PacktPublishing   File: decorators.py    MIT License 5 votes vote down vote up
def decode_json(fn):
    def _decode_json_from_payload(payload):
        try:
            json_payload = json.loads(payload['body'])
        except (TypeError, json.JSONDecodeError) as e:
            json_payload = None
        return fn(json_payload)
    return _decode_json_from_payload 
Example 27
Project: gemstone   Author: vladcalin   File: structs.py    MIT License 5 votes vote down vote up
def from_string(cls, string):
        try:
            return cls.from_dict(json.loads(string))
        except json.JSONDecodeError:
            raise JsonRpcParseError() 
Example 28
Project: gemstone   Author: vladcalin   File: structs.py    MIT License 5 votes vote down vote up
def parse_json_structure(string_item):
    """
    Given a raw representation of a json structure, returns the parsed corresponding data
    structure (``JsonRpcRequest`` or ``JsonRpcRequestBatch``)

    :param string_item:
    :return:
    """
    if not isinstance(string_item, str):
        raise TypeError("Expected str but got {} instead".format(type(string_item).__name__))

    try:
        item = json.loads(string_item)
    except json.JSONDecodeError:
        raise JsonRpcParseError()

    if isinstance(item, dict):
        return JsonRpcRequest.from_dict(item)
    elif isinstance(item, list):
        if len(item) == 0:
            raise JsonRpcInvalidRequestError()

        request_batch = JsonRpcRequestBatch([])
        for d in item:
            try:
                # handles the case of valid batch but with invalid
                # requests.
                if not isinstance(d, dict):
                    raise JsonRpcInvalidRequestError()
                # is dict, all fine
                parsed_entry = JsonRpcRequest.from_dict(d)
            except JsonRpcInvalidRequestError:
                parsed_entry = GenericResponse.INVALID_REQUEST
            request_batch.add_item(parsed_entry)
        return request_batch 
Example 29
Project: tra-tracking-on-gae   Author: elliot79313   File: test_errors.py    MIT License 5 votes vote down vote up
def test_decode_error(self):
        err = None
        try:
            json.loads('{}\na\nb')
        except json.JSONDecodeError:
            err = sys.exc_info()[1]
        else:
            self.fail('Expected JSONDecodeError')
        self.assertEqual(err.lineno, 2)
        self.assertEqual(err.colno, 1)
        self.assertEqual(err.endlineno, 3)
        self.assertEqual(err.endcolno, 2) 
Example 30
Project: tra-tracking-on-gae   Author: elliot79313   File: test_errors.py    MIT License 5 votes vote down vote up
def test_scan_error(self):
        err = None
        for t in (u, b):
            try:
                json.loads(t('{"asdf": "'))
            except json.JSONDecodeError:
                err = sys.exc_info()[1]
            else:
                self.fail('Expected JSONDecodeError')
            self.assertEqual(err.lineno, 1)
            self.assertEqual(err.colno, 10) 
Example 31
Project: tra-tracking-on-gae   Author: elliot79313   File: test_errors.py    MIT License 5 votes vote down vote up
def test_error_is_pickable(self):
        err = None
        try:
            json.loads('{}\na\nb')
        except json.JSONDecodeError:
            err = sys.exc_info()[1]
        else:
            self.fail('Expected JSONDecodeError')
        s = pickle.dumps(err)
        e = pickle.loads(s)

        self.assertEqual(err.msg, e.msg)
        self.assertEqual(err.doc, e.doc)
        self.assertEqual(err.pos, e.pos)
        self.assertEqual(err.end, e.end) 
Example 32
Project: tra-tracking-on-gae   Author: elliot79313   File: test_fail.py    MIT License 5 votes vote down vote up
def test_failures(self):
        for idx, doc in enumerate(JSONDOCS):
            idx = idx + 1
            if idx in SKIPS:
                json.loads(doc)
                continue
            try:
                json.loads(doc)
            except json.JSONDecodeError:
                pass
            else:
                self.fail("Expected failure for fail%d.json: %r" % (idx, doc)) 
Example 33
Project: tra-tracking-on-gae   Author: elliot79313   File: test_fail.py    MIT License 5 votes vote down vote up
def test_truncated_input(self):
        test_cases = [
            ('', 'Expecting value', 0),
            ('[', "Expecting value or ']'", 1),
            ('[42', "Expecting ',' delimiter", 3),
            ('[42,', 'Expecting value', 4),
            ('["', 'Unterminated string starting at', 1),
            ('["spam', 'Unterminated string starting at', 1),
            ('["spam"', "Expecting ',' delimiter", 7),
            ('["spam",', 'Expecting value', 8),
            ('{', 'Expecting property name enclosed in double quotes', 1),
            ('{"', 'Unterminated string starting at', 1),
            ('{"spam', 'Unterminated string starting at', 1),
            ('{"spam"', "Expecting ':' delimiter", 7),
            ('{"spam":', 'Expecting value', 8),
            ('{"spam":42', "Expecting ',' delimiter", 10),
            ('{"spam":42,', 'Expecting property name enclosed in double quotes',
             11),
            ('"', 'Unterminated string starting at', 0),
            ('"spam', 'Unterminated string starting at', 0),
            ('[,', "Expecting value", 1),
        ]
        for data, msg, idx in test_cases:
            try:
                json.loads(data)
            except json.JSONDecodeError:
                e = sys.exc_info()[1]
                self.assertEqual(
                    e.msg[:len(msg)],
                    msg,
                    "%r doesn't start with %r for %r" % (e.msg, msg, data))
                self.assertEqual(
                    e.pos, idx,
                    "pos %r != %r for %r" % (e.pos, idx, data))
            except Exception:
                e = sys.exc_info()[1]
                self.fail("Unexpected exception raised %r %s" % (e, e))
            else:
                self.fail("Unexpected success parsing '%r'" % (data,)) 
Example 34
Project: invenio-oauthclient   Author: inveniosoftware   File: test_views.py    MIT License 5 votes vote down vote up
def test_invalid_authorized_response(views_fixture):
    """Test login."""
    app = views_fixture
    oauth = app.extensions['oauthlib.client']
    with app.test_client() as client:
        # Fake an authorized request
        # Ensure remote apps have been loaded (due to before first
        # request)
        client.get(url_for('invenio_oauthclient.login', remote_app='test'))

        oauth.remote_apps['test'].handle_oauth2_response = MagicMock(
            side_effect=JSONDecodeError('Expecting value', '', 0)
        )

        state = serializer.dumps({
            'app': 'test',
            'sid': _create_identifier(),
            'next': None,
        })

        with pytest.raises(JSONDecodeError):
            client.get(url_for(
                'invenio_oauthclient.authorized',
                remote_app='test',
                code='test',
                state=state
            )) 
Example 35
Project: ip-navigator   Author: ip-tools   File: opaquelinks.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def request_payload(request):
    payload = {}
    if request.content_type == 'application/json':
        try:
            payload = request.json
        except JSONDecodeError as error:
            log.error('Could not derive data from json request: %s body=%s', error, request.body)

    payload.update(dict(request.params))
    return payload 
Example 36
Project: ip-navigator   Author: ip-tools   File: opaquelinks.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def token_payload(request):
    token = None
    if request.content_type == 'application/json':

        try:
            token = str(request.json)
        except JSONDecodeError as error:
            log.error('Could not extract token from json request: %s body=%s', error, request.body)

    if not token:
        log.error('Could not extract token from request: content-type=%s, body=%s', request.content_type, request.body)

    return token 
Example 37
Project: OpenFuji   Author: EverOddish   File: request.py    MIT License 5 votes vote down vote up
def _to_json(data):
    try:
        content = simplejson.loads(data)
        return content
    except JSONDecodeError:
        raise JSONDecodeError('Error decoding data', data, 0) 
Example 38
Project: ableton-live-webapi   Author: valsteen   File: test_errors.py    The Unlicense 5 votes vote down vote up
def test_decode_error(self):
        err = None
        try:
            json.loads('{}\na\nb')
        except json.JSONDecodeError:
            err = sys.exc_info()[1]
        else:
            self.fail('Expected JSONDecodeError')
        self.assertEqual(err.lineno, 2)
        self.assertEqual(err.colno, 1)
        self.assertEqual(err.endlineno, 3)
        self.assertEqual(err.endcolno, 2) 
Example 39
Project: ableton-live-webapi   Author: valsteen   File: test_errors.py    The Unlicense 5 votes vote down vote up
def test_scan_error(self):
        err = None
        for t in (u, b):
            try:
                json.loads(t('{"asdf": "'))
            except json.JSONDecodeError:
                err = sys.exc_info()[1]
            else:
                self.fail('Expected JSONDecodeError')
            self.assertEqual(err.lineno, 1)
            self.assertEqual(err.colno, 10) 
Example 40
Project: ableton-live-webapi   Author: valsteen   File: test_errors.py    The Unlicense 5 votes vote down vote up
def test_error_is_pickable(self):
        err = None
        try:
            json.loads('{}\na\nb')
        except json.JSONDecodeError:
            err = sys.exc_info()[1]
        else:
            self.fail('Expected JSONDecodeError')
        s = pickle.dumps(err)
        e = pickle.loads(s)

        self.assertEqual(err.msg, e.msg)
        self.assertEqual(err.doc, e.doc)
        self.assertEqual(err.pos, e.pos)
        self.assertEqual(err.end, e.end) 
Example 41
Project: ableton-live-webapi   Author: valsteen   File: test_fail.py    The Unlicense 5 votes vote down vote up
def test_failures(self):
        for idx, doc in enumerate(JSONDOCS):
            idx = idx + 1
            if idx in SKIPS:
                json.loads(doc)
                continue
            try:
                json.loads(doc)
            except json.JSONDecodeError:
                pass
            else:
                self.fail("Expected failure for fail%d.json: %r" % (idx, doc)) 
Example 42
Project: ableton-live-webapi   Author: valsteen   File: test_fail.py    The Unlicense 5 votes vote down vote up
def test_truncated_input(self):
        test_cases = [
            ('', 'Expecting value', 0),
            ('[', "Expecting value or ']'", 1),
            ('[42', "Expecting ',' delimiter", 3),
            ('[42,', 'Expecting value', 4),
            ('["', 'Unterminated string starting at', 1),
            ('["spam', 'Unterminated string starting at', 1),
            ('["spam"', "Expecting ',' delimiter", 7),
            ('["spam",', 'Expecting value', 8),
            ('{', 'Expecting property name enclosed in double quotes', 1),
            ('{"', 'Unterminated string starting at', 1),
            ('{"spam', 'Unterminated string starting at', 1),
            ('{"spam"', "Expecting ':' delimiter", 7),
            ('{"spam":', 'Expecting value', 8),
            ('{"spam":42', "Expecting ',' delimiter", 10),
            ('{"spam":42,', 'Expecting property name enclosed in double quotes',
             11),
            ('"', 'Unterminated string starting at', 0),
            ('"spam', 'Unterminated string starting at', 0),
            ('[,', "Expecting value", 1),
        ]
        for data, msg, idx in test_cases:
            try:
                json.loads(data)
            except json.JSONDecodeError:
                e = sys.exc_info()[1]
                self.assertEqual(
                    e.msg[:len(msg)],
                    msg,
                    "%r doesn't start with %r for %r" % (e.msg, msg, data))
                self.assertEqual(
                    e.pos, idx,
                    "pos %r != %r for %r" % (e.pos, idx, data))
            except Exception:
                e = sys.exc_info()[1]
                self.fail("Unexpected exception raised %r %s" % (e, e))
            else:
                self.fail("Unexpected success parsing '%r'" % (data,)) 
Example 43
Project: charms.openstack   Author: openstack   File: test_charms_openstack_sdn_odl.py    Apache License 2.0 5 votes vote down vote up
def test_get_networks_notjson(self):
        url = self.odlc.netmap_url
        httpretty.register_uri(httpretty.GET, url, status=200, body=NOT_JSON)
        with self.assertRaises(simplejson.JSONDecodeError):
            self.odlc.get_networks() 
Example 44
Project: charms.openstack   Author: openstack   File: test_charms_openstack_sdn_odl.py    Apache License 2.0 5 votes vote down vote up
def test_get_odl_registered_notjson(self):
        url = self.odlc.node_query_url
        httpretty.register_uri(httpretty.GET, url, status=200, body=NOT_JSON)
        with self.assertRaises(simplejson.JSONDecodeError):
            self.odlc.get_odl_registered_nodes() 
Example 45
Project: pysia   Author: jnmclarty   File: client.py    MIT License 5 votes vote down vote up
def __call__(self, verb, url, data=None):
        user_agent = {'User-agent': 'Sia-Agent'}
        full_url = self._url_base + url
        if verb == GET:
            resp = r.get(full_url, headers=user_agent, params=data)
        elif verb == POST:
            resp = r.post(full_url, headers=user_agent, data=data)
        try:
            return resp.json()
        except JSONDecodeError:
            return resp.ok 
Example 46
Project: socli   Author: gautamkrishnar   File: socli.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def retrieveSavedProfile():
    """
    Retrieves the user's saved profile after a "socli -u" command.
    Asks the user to enter a User ID and saves it if a previous file is not found.

    :return: The user's ID as an integer
    """
    global data_file
    global app_data
    user = None
    try:
        load_datafile()
        if "user" in app_data:
            user = app_data["user"]
        else:
            raise FileNotFoundError  # Manually raising to get value
    except JSONDecodeError:
        # This maybe some write failures
        del_datafile()
        print_warning("Error in parsing the data file, it will be now deleted. Please rerun the "
                      "socli -u command.")
        exit(1)
    except FileNotFoundError:
        print_warning("Default user not set...\n")
        try:
            # Code to execute when first time user runs socli -u
            app_data['user'] = int(inputs("Enter your Stackoverflow User ID: "))
            save_datafile()
            user = app_data['user']
            print_green("\nUserID saved...\n")
        except ValueError:
            print_warning("\nUser ID must be an integer.")
            print(
                "\nFollow the instructions on this page to get your User ID: http://meta.stackexchange.com/a/111130")
            exit(1)
    return user 
Example 47
Project: supybot-pulptriage   Author: pulp   File: plugin.py    GNU General Public License v2.0 5 votes vote down vote up
def _redmine_query(self, irc, url, **kwargs):
        redmine = irc.getCallback('Redmine')
        response = redmine.resource.get(url, **kwargs)
        try:
            result = json.loads(response.body_string())
        except json.JSONDecodeError:
            self.log.error('Unable to parse redmine data:')
            self.log.error(data)
            raise
        return result 
Example 48
Project: faircoop-market   Author: sarantapichos   File: http.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, *args):
        super(JsonRequest, self).__init__(*args)

        self.jsonp_handler = None

        args = self.httprequest.args
        jsonp = args.get('jsonp')
        self.jsonp = jsonp
        request = None
        request_id = args.get('id')
        
        if jsonp and self.httprequest.method == 'POST':
            # jsonp 2 steps step1 POST: save call
            def handler():
                self.session['jsonp_request_%s' % (request_id,)] = self.httprequest.form['r']
                self.session.modified = True
                headers=[('Content-Type', 'text/plain; charset=utf-8')]
                r = werkzeug.wrappers.Response(request_id, headers=headers)
                return r
            self.jsonp_handler = handler
            return
        elif jsonp and args.get('r'):
            # jsonp method GET
            request = args.get('r')
        elif jsonp and request_id:
            # jsonp 2 steps step2 GET: run and return result
            request = self.session.pop('jsonp_request_%s' % (request_id,), '{}')
        else:
            # regular jsonrpc2
            request = self.httprequest.stream.read()

        # Read POST content or POST Form Data named "request"
        try:
            self.jsonrequest = simplejson.loads(request)
        except simplejson.JSONDecodeError:
            msg = 'Invalid JSON data: %r' % (request,)
            _logger.error('%s: %s', self.httprequest.path, msg)
            raise werkzeug.exceptions.BadRequest(msg)

        self.params = dict(self.jsonrequest.get("params", {}))
        self.context = self.params.pop('context', dict(self.session.context)) 
Example 49
Project: nefertari   Author: ramses-tech   File: test_view.py    Apache License 2.0 5 votes vote down vote up
def test_init_json_error(self, run):
        import simplejson
        request = Mock(
            content_type='application/json',
            method='POST',
            accept=['application/json'],
        )
        type(request).json = PropertyMock(
            side_effect=simplejson.JSONDecodeError(
                'foo', 'asdasdasdasd', pos=1))
        request.params.mixed.return_value = {'param2.foo': 'val2'}
        view = DummyBaseView(context={'foo': 'bar'}, request=request)
        assert request.override_renderer == 'nefertari_json'
        assert list(view._params.keys()) == ['param2'] 
Example 50
Project: fund   Author: Frank-qlu   File: test_errors.py    Apache License 2.0 5 votes vote down vote up
def test_decode_error(self):
        err = None
        try:
            json.loads('{}\na\nb')
        except json.JSONDecodeError:
            err = sys.exc_info()[1]
        else:
            self.fail('Expected JSONDecodeError')
        self.assertEqual(err.lineno, 2)
        self.assertEqual(err.colno, 1)
        self.assertEqual(err.endlineno, 3)
        self.assertEqual(err.endcolno, 2) 
Example 51
Project: fund   Author: Frank-qlu   File: test_errors.py    Apache License 2.0 5 votes vote down vote up
def test_scan_error(self):
        err = None
        for t in (text_type, b):
            try:
                json.loads(t('{"asdf": "'))
            except json.JSONDecodeError:
                err = sys.exc_info()[1]
            else:
                self.fail('Expected JSONDecodeError')
            self.assertEqual(err.lineno, 1)
            self.assertEqual(err.colno, 10) 
Example 52
Project: fund   Author: Frank-qlu   File: test_errors.py    Apache License 2.0 5 votes vote down vote up
def test_error_is_pickable(self):
        err = None
        try:
            json.loads('{}\na\nb')
        except json.JSONDecodeError:
            err = sys.exc_info()[1]
        else:
            self.fail('Expected JSONDecodeError')
        s = pickle.dumps(err)
        e = pickle.loads(s)

        self.assertEqual(err.msg, e.msg)
        self.assertEqual(err.doc, e.doc)
        self.assertEqual(err.pos, e.pos)
        self.assertEqual(err.end, e.end) 
Example 53
Project: fund   Author: Frank-qlu   File: test_fail.py    Apache License 2.0 5 votes vote down vote up
def test_failures(self):
        for idx, doc in enumerate(JSONDOCS):
            idx = idx + 1
            if idx in SKIPS:
                json.loads(doc)
                continue
            try:
                json.loads(doc)
            except json.JSONDecodeError:
                pass
            else:
                self.fail("Expected failure for fail%d.json: %r" % (idx, doc)) 
Example 54
Project: fund   Author: Frank-qlu   File: test_fail.py    Apache License 2.0 5 votes vote down vote up
def test_truncated_input(self):
        test_cases = [
            ('', 'Expecting value', 0),
            ('[', "Expecting value or ']'", 1),
            ('[42', "Expecting ',' delimiter", 3),
            ('[42,', 'Expecting value', 4),
            ('["', 'Unterminated string starting at', 1),
            ('["spam', 'Unterminated string starting at', 1),
            ('["spam"', "Expecting ',' delimiter", 7),
            ('["spam",', 'Expecting value', 8),
            ('{', 'Expecting property name enclosed in double quotes', 1),
            ('{"', 'Unterminated string starting at', 1),
            ('{"spam', 'Unterminated string starting at', 1),
            ('{"spam"', "Expecting ':' delimiter", 7),
            ('{"spam":', 'Expecting value', 8),
            ('{"spam":42', "Expecting ',' delimiter", 10),
            ('{"spam":42,', 'Expecting property name enclosed in double quotes',
             11),
            ('"', 'Unterminated string starting at', 0),
            ('"spam', 'Unterminated string starting at', 0),
            ('[,', "Expecting value", 1),
        ]
        for data, msg, idx in test_cases:
            try:
                json.loads(data)
            except json.JSONDecodeError:
                e = sys.exc_info()[1]
                self.assertEqual(
                    e.msg[:len(msg)],
                    msg,
                    "%r doesn't start with %r for %r" % (e.msg, msg, data))
                self.assertEqual(
                    e.pos, idx,
                    "pos %r != %r for %r" % (e.pos, idx, data))
            except Exception:
                e = sys.exc_info()[1]
                self.fail("Unexpected exception raised %r %s" % (e, e))
            else:
                self.fail("Unexpected success parsing '%r'" % (data,)) 
Example 55
Project: vidcutter   Author: ozmartian   File: videoservice.py    GNU General Public License v3.0 5 votes vote down vote up
def probe(self, source: str) -> Munch:
        try:
            args = '-v error -show_streams -show_format -of json "{}"'.format(source)
            json_data = self.cmdExec(self.backends.ffprobe, args, output=True, mergechannels=False)
            return Munch.fromDict(loads(json_data))
        except FileNotFoundError:
            self.logger.exception('FFprobe could not find media file: {}'.format(source), exc_info=True)
            raise
        except JSONDecodeError:
            self.logger.exception('FFprobe JSON decoding error', exc_info=True)
            raise 
Example 56
Project: schematizer   Author: Yelp   File: schemas.py    Apache License 2.0 5 votes vote down vote up
def register_schema(request):
    try:
        req = requests_v1.RegisterSchemaRequest(**request.json_body)
        validate_names([req.namespace, req.source])
        docs_required = (
            req.namespace not in get_config().namespace_no_doc_required
        )

        return _register_avro_schema(
            schema_json=req.schema_json,
            namespace=req.namespace,
            source=req.source,
            source_owner_email=req.source_owner_email,
            contains_pii=req.contains_pii,
            cluster_type=req.cluster_type,
            base_schema_id=req.base_schema_id,
            docs_required=docs_required
        )
    except simplejson.JSONDecodeError as e:
        log.exception("Failed to construct RegisterSchemaRequest. {}"
                      .format(request.json_body))
        raise exceptions_v1.invalid_schema_exception(
            'Error "{error}" encountered decoding JSON: "{schema}"'.format(
                error=str(e),
                schema=request.json_body['schema']
            )
        ) 
Example 57
Project: WooGenerator   Author: derwentx   File: core.py    GNU General Public License v2.0 5 votes vote down vote up
def create_item(self, data, **kwargs):
        """
        Creates an item in the API
        """
        # TODO: what's with these lines?
        if hasattr(data, 'items'):
            data = dict([(key, value) for key, value in data.items()])
        if isinstance(data, dict):
            if str(data.get('parent')) == str(-1):
                del data['parent']

        endpoint, kwargs = self.get_endpoint(**kwargs)
        if self.page_nesting:
            endpoint_singular = self.endpoint_singular
            endpoint_singular = re.sub('/', '_', endpoint_singular)
            page_key = getattr(self, 'page_key', endpoint_singular)
            data = {page_key: data}
        if Registrar.DEBUG_API:
            Registrar.register_message(
                "creating %s: %s" % (endpoint, pformat(data)[:1000]))
        # TODO: add a better read timeout
        try:
            response = self.service.post(endpoint, data, **kwargs)
        except ReadTimeout as exc:
            Registrar.register_warning(exc)
            kwargs['timeout'] = 30
            response = self.service.post(endpoint, data, **kwargs)
        assert response.status_code not in [400, 401], "API ERROR"
        try:
            response.json()
        except JSONDecodeError as exc:
            raise UserWarning("Should be able to decode JSON: %s\n%s" % (repr(
                response.text), exc))
        assert response.json(), "json should exist"
        assert not isinstance(
            response.json(),
            int), "could not convert response to json: %s %s" % (
                str(response), str(response.text))
        assert 'errors' not in response.json(
        ), "response has errors: %s" % str(response.json()['errors'])
        return response 
Example 58
Project: influx-client   Author: AxiomExergy   File: __init__.py    Apache License 2.0 5 votes vote down vote up
def _check_and_raise(response):
        """
        Raises a smart exception if *response* is an error.

        :param request.Response response: A response object

        """

        # Only pay attention to codes over 400
        if response.status_code < 400:
            return

        if isinstance(response.reason, bytes):
            # We attempt to decode utf-8 first because some servers
            # choose to localize their reason strings. If the string
            # isn't utf-8, we fall back to iso-8859-1 for all other
            # encodings. (See PR #3538)
            try:
                reason = response.reason.decode('utf-8')
            except UnicodeDecodeError:
                reason = response.reason.decode('iso-8859-1')
        else:
            reason = response.reason

        # Try to decode the JSON body from Influx
        try:
            msg = response.json()
            # Try to get the error from all possible places
            msg = msg.get('error', (msg.get('results', []) +
                                    [{}]).pop().get('error', None))
        except simplejson.JSONDecodeError:
            msg = None

        if msg:
            reason = msg

        # Format a sane error message
        http_error_msg = "{} {} for {}".format(response.status_code, reason,
                                               response.url)

        raise HTTPError(http_error_msg, response=response) 
Example 59
Project: mycroft   Author: Yelp   File: mr_json.py    MIT License 5 votes vote down vote up
def mapper(self, _, value):
        """
        This takes a single json line and loads a python dict based.
        It then emits the key, value based on the YAML schema
        """
        try:
            single_entry_dict = simplejson.loads(value)
            for k, v in self.emit_rows_for_tables(single_entry_dict):
                yield k, v
        except simplejson.JSONDecodeError:
            yield self.emit_exception_row(value) 
Example 60
Project: mycroft   Author: Yelp   File: test_cluster_actions.py    MIT License 5 votes vote down vote up
def test_post_no_kwargs(self):
        with pytest.raises(JSONDecodeError):
            post_cluster(None, "") 
Example 61
Project: mycroft   Author: Yelp   File: test_job_actions.py    MIT License 5 votes vote down vote up
def test_post_no_kwargs(self):
        with pytest.raises(JSONDecodeError):
            self.post_job(None, None, "") 
Example 62
Project: mycroft   Author: Yelp   File: test_job_actions.py    MIT License 5 votes vote down vote up
def test_put_no_kwargs(self):
        with pytest.raises(JSONDecodeError):
            put_job(None, None, "") 
Example 63
Project: oscars2016   Author: 0x0ece   File: test_errors.py    Apache License 2.0 5 votes vote down vote up
def test_decode_error(self):
        err = None
        try:
            json.loads('{}\na\nb')
        except json.JSONDecodeError:
            err = sys.exc_info()[1]
        else:
            self.fail('Expected JSONDecodeError')
        self.assertEqual(err.lineno, 2)
        self.assertEqual(err.colno, 1)
        self.assertEqual(err.endlineno, 3)
        self.assertEqual(err.endcolno, 2) 
Example 64
Project: oscars2016   Author: 0x0ece   File: test_errors.py    Apache License 2.0 5 votes vote down vote up
def test_scan_error(self):
        err = None
        for t in (u, b):
            try:
                json.loads(t('{"asdf": "'))
            except json.JSONDecodeError:
                err = sys.exc_info()[1]
            else:
                self.fail('Expected JSONDecodeError')
            self.assertEqual(err.lineno, 1)
            self.assertEqual(err.colno, 10) 
Example 65
Project: oscars2016   Author: 0x0ece   File: test_errors.py    Apache License 2.0 5 votes vote down vote up
def test_error_is_pickable(self):
        err = None
        try:
            json.loads('{}\na\nb')
        except json.JSONDecodeError:
            err = sys.exc_info()[1]
        else:
            self.fail('Expected JSONDecodeError')
        s = pickle.dumps(err)
        e = pickle.loads(s)

        self.assertEqual(err.msg, e.msg)
        self.assertEqual(err.doc, e.doc)
        self.assertEqual(err.pos, e.pos)
        self.assertEqual(err.end, e.end) 
Example 66
Project: oscars2016   Author: 0x0ece   File: test_fail.py    Apache License 2.0 5 votes vote down vote up
def test_failures(self):
        for idx, doc in enumerate(JSONDOCS):
            idx = idx + 1
            if idx in SKIPS:
                json.loads(doc)
                continue
            try:
                json.loads(doc)
            except json.JSONDecodeError:
                pass
            else:
                self.fail("Expected failure for fail%d.json: %r" % (idx, doc)) 
Example 67
Project: oscars2016   Author: 0x0ece   File: test_fail.py    Apache License 2.0 5 votes vote down vote up
def test_truncated_input(self):
        test_cases = [
            ('', 'Expecting value', 0),
            ('[', "Expecting value or ']'", 1),
            ('[42', "Expecting ',' delimiter", 3),
            ('[42,', 'Expecting value', 4),
            ('["', 'Unterminated string starting at', 1),
            ('["spam', 'Unterminated string starting at', 1),
            ('["spam"', "Expecting ',' delimiter", 7),
            ('["spam",', 'Expecting value', 8),
            ('{', 'Expecting property name enclosed in double quotes', 1),
            ('{"', 'Unterminated string starting at', 1),
            ('{"spam', 'Unterminated string starting at', 1),
            ('{"spam"', "Expecting ':' delimiter", 7),
            ('{"spam":', 'Expecting value', 8),
            ('{"spam":42', "Expecting ',' delimiter", 10),
            ('{"spam":42,', 'Expecting property name enclosed in double quotes',
             11),
            ('"', 'Unterminated string starting at', 0),
            ('"spam', 'Unterminated string starting at', 0),
            ('[,', "Expecting value", 1),
        ]
        for data, msg, idx in test_cases:
            try:
                json.loads(data)
            except json.JSONDecodeError:
                e = sys.exc_info()[1]
                self.assertEqual(
                    e.msg[:len(msg)],
                    msg,
                    "%r doesn't start with %r for %r" % (e.msg, msg, data))
                self.assertEqual(
                    e.pos, idx,
                    "pos %r != %r for %r" % (e.pos, idx, data))
            except Exception:
                e = sys.exc_info()[1]
                self.fail("Unexpected exception raised %r %s" % (e, e))
            else:
                self.fail("Unexpected success parsing '%r'" % (data,)) 
Example 68
Project: that-startpage-rocks   Author: the-duck   File: test_errors.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_decode_error(self):
        err = None
        try:
            json.loads('{}\na\nb')
        except json.JSONDecodeError:
            err = sys.exc_info()[1]
        else:
            self.fail('Expected JSONDecodeError')
        self.assertEqual(err.lineno, 2)
        self.assertEqual(err.colno, 1)
        self.assertEqual(err.endlineno, 3)
        self.assertEqual(err.endcolno, 2) 
Example 69
Project: that-startpage-rocks   Author: the-duck   File: test_errors.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_scan_error(self):
        err = None
        for t in (u, b):
            try:
                json.loads(t('{"asdf": "'))
            except json.JSONDecodeError:
                err = sys.exc_info()[1]
            else:
                self.fail('Expected JSONDecodeError')
            self.assertEqual(err.lineno, 1)
            self.assertEqual(err.colno, 10) 
Example 70
Project: that-startpage-rocks   Author: the-duck   File: test_errors.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_error_is_pickable(self):
        err = None
        try:
            json.loads('{}\na\nb')
        except json.JSONDecodeError:
            err = sys.exc_info()[1]
        else:
            self.fail('Expected JSONDecodeError')
        s = pickle.dumps(err)
        e = pickle.loads(s)

        self.assertEqual(err.msg, e.msg)
        self.assertEqual(err.doc, e.doc)
        self.assertEqual(err.pos, e.pos)
        self.assertEqual(err.end, e.end) 
Example 71
Project: that-startpage-rocks   Author: the-duck   File: test_fail.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_failures(self):
        for idx, doc in enumerate(JSONDOCS):
            idx = idx + 1
            if idx in SKIPS:
                json.loads(doc)
                continue
            try:
                json.loads(doc)
            except json.JSONDecodeError:
                pass
            else:
                self.fail("Expected failure for fail%d.json: %r" % (idx, doc)) 
Example 72
Project: that-startpage-rocks   Author: the-duck   File: test_fail.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_truncated_input(self):
        test_cases = [
            ('', 'Expecting value', 0),
            ('[', "Expecting value or ']'", 1),
            ('[42', "Expecting ',' delimiter", 3),
            ('[42,', 'Expecting value', 4),
            ('["', 'Unterminated string starting at', 1),
            ('["spam', 'Unterminated string starting at', 1),
            ('["spam"', "Expecting ',' delimiter", 7),
            ('["spam",', 'Expecting value', 8),
            ('{', 'Expecting property name enclosed in double quotes', 1),
            ('{"', 'Unterminated string starting at', 1),
            ('{"spam', 'Unterminated string starting at', 1),
            ('{"spam"', "Expecting ':' delimiter", 7),
            ('{"spam":', 'Expecting value', 8),
            ('{"spam":42', "Expecting ',' delimiter", 10),
            ('{"spam":42,', 'Expecting property name enclosed in double quotes',
             11),
            ('"', 'Unterminated string starting at', 0),
            ('"spam', 'Unterminated string starting at', 0),
            ('[,', "Expecting value", 1),
        ]
        for data, msg, idx in test_cases:
            try:
                json.loads(data)
            except json.JSONDecodeError:
                e = sys.exc_info()[1]
                self.assertEqual(
                    e.msg[:len(msg)],
                    msg,
                    "%r doesn't start with %r for %r" % (e.msg, msg, data))
                self.assertEqual(
                    e.pos, idx,
                    "pos %r != %r for %r" % (e.pos, idx, data))
            except Exception:
                e = sys.exc_info()[1]
                self.fail("Unexpected exception raised %r %s" % (e, e))
            else:
                self.fail("Unexpected success parsing '%r'" % (data,)) 
Example 73
Project: cohesive-networks-vns3-vpn-monitoring   Author: trek10inc   File: test_errors.py    MIT License 5 votes vote down vote up
def test_decode_error(self):
        err = None
        try:
            json.loads('{}\na\nb')
        except json.JSONDecodeError:
            err = sys.exc_info()[1]
        else:
            self.fail('Expected JSONDecodeError')
        self.assertEqual(err.lineno, 2)
        self.assertEqual(err.colno, 1)
        self.assertEqual(err.endlineno, 3)
        self.assertEqual(err.endcolno, 2) 
Example 74
Project: sd-agent-core-plugins   Author: serverdensity   File: check.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def _rest_request_to_json(self, address, disable_ssl_validation, object_path, query_params):
        '''
        Query the given URL and return the JSON response
        '''
        response_json = None

        service_check_tags = ['namenode_url:' + address]

        url = address

        if object_path:
            url = self._join_url_dir(url, object_path)

        # Add query_params as arguments
        if query_params:
            query = '&'.join(['{0}={1}'.format(key, value) for key, value in query_params.iteritems()])
            url = urljoin(url, '?' + query)

        self.log.debug('Attempting to connect to "%s"' % url)

        try:
            response = requests.get(url, timeout=self.default_integration_http_timeout, verify=not disable_ssl_validation)
            response.raise_for_status()
            response_json = response.json()

        except Timeout as e:
            self.service_check(JMX_SERVICE_CHECK,
                AgentCheck.CRITICAL,
                tags=service_check_tags,
                message="Request timeout: {0}, {1}".format(url, e))
            raise

        except (HTTPError,
                InvalidURL,
                ConnectionError) as e:
            self.service_check(JMX_SERVICE_CHECK,
                AgentCheck.CRITICAL,
                tags=service_check_tags,
                message="Request failed: {0}, {1}".format(url, e))
            raise

        except JSONDecodeError as e:
            self.service_check(JMX_SERVICE_CHECK,
                AgentCheck.CRITICAL,
                tags=service_check_tags,
                message='JSON Parse failed: {0}, {1}'.format(url, e))
            raise

        except ValueError as e:
            self.service_check(JMX_SERVICE_CHECK,
                AgentCheck.CRITICAL,
                tags=service_check_tags,
                message=str(e))
            raise

        return response_json 
Example 75
Project: sd-agent-core-plugins   Author: serverdensity   File: check.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def _rest_request_to_json(self, address, object_path, service_name, *args, **kwargs):
        '''
        Query the given URL and return the JSON response
        '''
        response_json = None

        service_check_tags = ['url:%s' % self._get_url_base(address)]

        url = address

        if object_path:
            url = self._join_url_dir(url, object_path)

        # Add args to the url
        if args:
            for directory in args:
                url = self._join_url_dir(url, directory)

        self.log.debug('Attempting to connect to "%s"' % url)

        # Add kwargs as arguments
        if kwargs:
            query = '&'.join(['{0}={1}'.format(key, value) for key, value in kwargs.iteritems()])
            url = urljoin(url, '?' + query)

        try:
            response = requests.get(url, timeout=self.default_integration_http_timeout)
            response.raise_for_status()
            response_json = response.json()

        except Timeout as e:
            self.service_check(service_name,
                AgentCheck.CRITICAL,
                tags=service_check_tags,
                message="Request timeout: {0}, {1}".format(url, e))
            raise

        except (HTTPError,
                InvalidURL,
                ConnectionError) as e:
            self.service_check(service_name,
                AgentCheck.CRITICAL,
                tags=service_check_tags,
                message="Request failed: {0}, {1}".format(url, e))
            raise

        except JSONDecodeError as e:
            self.service_check(service_name,
                AgentCheck.CRITICAL,
                tags=service_check_tags,
                message='JSON Parse failed: {0}, {1}'.format(url, e))
            raise

        except ValueError as e:
            self.service_check(service_name,
                AgentCheck.CRITICAL,
                tags=service_check_tags,
                message=str(e))
            raise

        return response_json 
Example 76
Project: LibCloud   Author: stewnorriss   File: pricing.py    Apache License 2.0 4 votes vote down vote up
def download_pricing_file(file_url=DEFAULT_FILE_URL,
                          file_path=CUSTOM_PRICING_FILE_PATH):
    """
    Download pricing file from the file_url and save it to file_path.

    :type file_url: ``str``
    :param file_url: URL pointing to the pricing file.

    :type file_path: ``str``
    :param file_path: Path where a download pricing file will be saved.
    """
    dir_name = os.path.dirname(file_path)

    if not os.path.exists(dir_name):
        # Verify a valid path is provided
        msg = ('Can\'t write to %s, directory %s, doesn\'t exist' %
               (file_path, dir_name))
        raise ValueError(msg)

    if os.path.exists(file_path) and os.path.isdir(file_path):
        msg = ('Can\'t write to %s file path because it\'s a'
               ' directory' % (file_path))
        raise ValueError(msg)

    response = get_response_object(file_url)
    body = response.body

    # Verify pricing file is valid
    try:
        data = json.loads(body)
    except JSONDecodeError:
        msg = 'Provided URL doesn\'t contain valid pricing data'
        raise Exception(msg)

    # pylint: disable=maybe-no-member
    if not data.get('updated', None):
        msg = 'Provided URL doesn\'t contain valid pricing data'
        raise Exception(msg)

    # No need to stream it since file is small
    with open(file_path, 'w') as file_handle:
        file_handle.write(body) 
Example 77
Project: gemstone   Author: vladcalin   File: handlers.py    MIT License 4 votes vote down vote up
def post(self):
        if self.request.headers.get("Content-type").split(";")[0] != "application/json":
            self.write_single_response(GenericResponse.INVALID_REQUEST)
            return

        req_body_raw = self.request.body.decode()
        try:
            req_object = json.loads(req_body_raw)
        except json.JSONDecodeError:
            self.write_single_response(GenericResponse.PARSE_ERROR)
            return

        # handle the actual call
        if isinstance(req_object, dict):
            # single call
            try:
                req_object = JsonRpcRequest.from_dict(req_object)
            except JsonRpcInvalidRequestError:
                self.write_single_response(GenericResponse.INVALID_REQUEST)
                return

            if req_object.is_notification():
                self.write_single_response(GenericResponse.NOTIFICATION_RESPONSE)

            result = yield self.handle_single_request(req_object)
            self.write_single_response(result)
        elif isinstance(req_object, list):
            if len(req_object) == 0:
                self.write_single_response(GenericResponse.INVALID_REQUEST)
                return

            # batch call
            invalid_requests = []
            requests_futures = []
            notification_futures = []

            for item in req_object:
                try:
                    if not isinstance(item, dict):
                        raise JsonRpcInvalidRequestError()
                    current_rpc_call = JsonRpcRequest.from_dict(item)

                    # handle notifications
                    if current_rpc_call.is_notification():
                        # we trigger their execution, but we don't yield for their results
                        notification_futures.append(self.handle_single_request(current_rpc_call))
                    else:
                        requests_futures.append(self.handle_single_request(current_rpc_call))
                except JsonRpcInvalidRequestError:
                    invalid_requests.append(GenericResponse.INVALID_REQUEST)

            finished_rpc_calls = yield requests_futures
            self.write_batch_response(JsonRpcResponseBatch(invalid_requests + finished_rpc_calls))
        else:
            self.write_single_response(GenericResponse.INVALID_REQUEST) 
Example 78
Project: cerberus-core   Author: ovh   File: request.py    GNU General Public License v3.0 4 votes vote down vote up
def _wrapper(
    url,
    method="GET",
    auth=None,
    params=None,
    as_dict=True,
    headers=None,
    timeout=30,
    requests_lib="requests",
    **kwargs
):
    """
        Python-requests wrapper
    """
    response = None
    func_params = {
        "headers": headers,
        "auth": auth,
        "params": params,
        "data": params,
        "verify": True,
        "timeout": timeout,
    }
    func_params.update(**kwargs)

    # Because sometimes network or backend is instable (TCP RST, HTTP 500 ...)
    max_tries = 3

    for retry in range(max_tries):
        try:
            if method == "GET":
                func_params.pop("data", None)
            else:
                func_params.pop("params", None)

            func = getattr(__import__(requests_lib), method.lower())
            response = func(url, **func_params)
            response.raise_for_status()
            if as_dict:
                return response.json()
            return response
        except HTTPError as ex:
            if 500 <= int(ex.response.status_code) <= 599:
                if retry == max_tries - 1:
                    raise RequestException(
                        _get_exception(response, ex), ex.response.status_code
                    )
                sleep(2)
            else:
                raise RequestException(
                    _get_exception(response, ex),
                    ex.response.status_code,
                    response=response,
                )
        except Timeout as ex:
            if retry == max_tries - 1:
                raise RequestException(_get_exception(response, ex))
        except (ChunkedEncodingError, ConnectionError, JSONDecodeError) as ex:
            if retry == max_tries - 1:
                raise RequestException(_get_exception(response, ex))
            sleep(1) 
Example 79
Project: eclcli   Author: nttcom   File: api.py    Apache License 2.0 4 votes vote down vote up
def list(
        self,
        path,
        session=None,
        body=None,
        detailed=False,
        **params
    ):
        """Return a list of resources

        GET ${ENDPOINT}/${PATH}?${PARAMS}

        path is often the object's plural resource type

        :param string path:
            The API-specific portion of the URL path
        :param Session session:
            HTTP client session
        :param body: data that will be encoded as JSON and passed in POST
            request (GET will be sent by default)
        :param bool detailed:
            Adds '/details' to path for some APIs to return extended attributes
        :returns:
            JSON-decoded response, could be a list or a dict-wrapped-list
        """

        if detailed:
            path = '/'.join([path.rstrip('/'), 'details'])

        if body:
            ret = self._request(
                'POST',
                path,
                # service=self.service_type,
                json=body,
                params=params,
            )
        else:
            ret = self._request(
                'GET',
                path,
                # service=self.service_type,
                params=params,
            )
        try:
            return ret.json()
        except json.JSONDecodeError:
            return ret

    # Layered actions built on top of the basic action methods do not
    # explicitly take a Session but one may still be passed in kwargs 
Example 80
Project: community-networks-monitoring-tools   Author: netCommonsEU   File: owner_graph_metrics.py    GNU General Public License v3.0 4 votes vote down vote up
def initialize_names_dictionary(self, file_name):
        """ the alias dictionary is a dictionary that mathces multiple
        "From" email fields to one person. It is derived from "From" fields
        taken from parsing a mailing list mbox. It is in the form:
        {
            "name surname <[email protected]>": [
                [
                    match score, #  (1=high)
                    "email->email" # what matches
                ],
                [ # clean values for candidate element
                    "namesurname", # name stripepd of any non text char
                    "user",
                    "domain"
                ],
                [ # clean values for best match
                    "namesurname2",
                    "user2",
                    "domain2"
                ],
                "name2 surname2 <[email protected]>"
            ],

        see the fromdiff library:
        """

        try:
            namesFile = open(file_name, "r")
        except IOError:
            print "ERROR: Could not load the name dictionary file", file_name
            return
        try:
            self.namesDictionary = simplejson.load(namesFile)
        except simplejson.JSONDecodeError:
            print "ERROR: could not parse the JSON names file"
        if not namesFile.closed:
            namesFile.close()
        for e in self.namesDictionary.items():
            # merge aliases into unique names
            alias_email = re.search(r'[\w\.-][email protected][\w\.-]+',
                                    e[0]).group(0)
            unique_email = e[1][2][1] + "@" + e[1][2][2]
            self.email_aliases[alias_email] = unique_email