Ejemplo n.º 1
0
def _unpack_response(response, cursor_id=None):
    """Unpack a response from the database.

    Check the response for errors and unpack, returning a dictionary
    containing the response data.

    :Parameters:
      - `response`: byte string as returned from the database
      - `cursor_id` (optional): cursor_id we sent to get this response -
        used for raising an informative exception when we get cursor id not
        valid at server response
    """
    response_flag = struct.unpack("<i", response[:4])[0]
    if response_flag == 1:
        # Shouldn't get this response if we aren't doing a getMore
        assert cursor_id is not None

        raise OperationFailure("cursor id '%s' not valid at server" %
                               cursor_id)
    elif response_flag == 2:
        error_object = bson.BSON(response[20:]).to_dict()
        if error_object["$err"] == "not master":
            raise AutoReconnect("master has changed")
        raise OperationFailure("database error: %s" % error_object["$err"])
    else:
        assert response_flag == 0

    result = {}
    result["cursor_id"] = struct.unpack("<q", response[4:12])[0]
    result["starting_from"] = struct.unpack("<i", response[12:16])[0]
    result["number_returned"] = struct.unpack("<i", response[16:20])[0]
    result["data"] = bson._to_dicts(response[20:])
    assert len(result["data"]) == result["number_returned"]
    return result
Ejemplo n.º 2
0
def _unpack_response(response, cursor_id=None, as_class=dict, tz_aware=False):
    """Unpack a response from the database.

    Check the response for errors and unpack, returning a dictionary
    containing the response data.

    :Parameters:
      - `response`: byte string as returned from the database
      - `cursor_id` (optional): cursor_id we sent to get this response -
        used for raising an informative exception when we get cursor id not
        valid at server response
      - `as_class` (optional): class to use for resulting documents
    """
    response_flag = struct.unpack("<i", response[:4])[0]
    if response_flag & 1:
        # Shouldn't get this response if we aren't doing a getMore
        assert cursor_id is not None

        raise OperationFailure("cursor id '%s' not valid at server" %
                               cursor_id)
    elif response_flag & 2:
        error_object = bson.BSON(response[20:]).to_dict()
        if error_object["$err"] == "not master":
            raise AutoReconnect("master has changed")
        raise OperationFailure("database error: %s" %
                               error_object["$err"])

    result = {}
    result["cursor_id"] = struct.unpack("<q", response[4:12])[0]
    result["starting_from"] = struct.unpack("<i", response[12:16])[0]
    result["number_returned"] = struct.unpack("<i", response[16:20])[0]
    result["data"] = bson._to_dicts(response[20:], as_class, tz_aware)
    assert len(result["data"]) == result["number_returned"]
    return result
Ejemplo n.º 3
0
 def _put_request_get_response(self, op, data):
     self._put_request(op, data)
     header = receive(HEADER_SIZE)
     length, id, to, code = struct.unpack('<4i', header)
     message = receive(length - HEADER_SIZE)
     cutoff = struct.calcsize('<iqii')
     flag, cid, start, numret = struct.unpack('<iqii', message[:cutoff])
     body = _to_dicts(message[cutoff:])
     return (cid, start, numret, body)
Ejemplo n.º 4
0
 def test_basic_to_dict(self):
     self.assertEqual({"test": u"hello world"},
                      BSON("\x1B\x00\x00\x00\x0E\x74\x65\x73\x74\x00\x0C"
                           "\x00\x00\x00\x68\x65\x6C\x6C\x6F\x20\x77\x6F"
                           "\x72\x6C\x64\x00\x00").to_dict())
     self.assertEqual([{"test": u"hello world"}, {}],
                      _to_dicts("\x1B\x00\x00\x00\x0E\x74\x65\x73\x74\x00"
                                "\x0C\x00\x00\x00\x68\x65\x6C\x6C\x6F\x20"
                                "\x77\x6F\x72\x6C\x64\x00\x00\x05\x00\x00"
                                "\x00\x00"))
Ejemplo n.º 5
0
 def test_basic_to_dict(self):
     self.assertEqual({"test": u"hello world"},
                      BSON("\x1B\x00\x00\x00\x0E\x74\x65\x73\x74\x00\x0C"
                           "\x00\x00\x00\x68\x65\x6C\x6C\x6F\x20\x77\x6F"
                           "\x72\x6C\x64\x00\x00").to_dict())
     self.assertEqual([{
         "test": u"hello world"
     }, {}],
                      _to_dicts("\x1B\x00\x00\x00\x0E\x74\x65\x73\x74\x00"
                                "\x0C\x00\x00\x00\x68\x65\x6C\x6C\x6F\x20"
                                "\x77\x6F\x72\x6C\x64\x00\x00\x05\x00\x00"
                                "\x00\x00"))