Skip to content

Pass current field name to raw BSON decoder #326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def _get_string(data, position, obj_end, opts, dummy):
opts.unicode_decode_error_handler, True)[0], end + 1


def _get_object(data, position, obj_end, opts, dummy):
def _get_object(data, position, obj_end, opts, element_name):
"""Decode a BSON subdocument to opts.document_class or bson.dbref.DBRef."""
obj_size = _UNPACK_INT(data[position:position + 4])[0]
end = position + obj_size - 1
Expand All @@ -143,7 +143,7 @@ def _get_object(data, position, obj_end, opts, dummy):
if end >= obj_end:
raise InvalidBSON("invalid object length")
if _raw_document_class(opts.document_class):
return (opts.document_class(data[position:end + 1], opts),
return (opts.document_class(data[position:end + 1], opts, element_name),
position + obj_size)

obj = _elements_to_dict(data, position + 4, end, opts)
Expand Down
4 changes: 2 additions & 2 deletions bson/_cbsonmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1769,8 +1769,8 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,

if (options->is_raw_bson) {
value = PyObject_CallFunction(
options->document_class, BYTES_FORMAT_STRING "O",
buffer + *position, size, options->options_obj);
options->document_class, BYTES_FORMAT_STRING "OO",
buffer + *position, size, options->options_obj, name);
if (!value) {
goto invalid;
}
Expand Down
3 changes: 2 additions & 1 deletion bson/raw_bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ class RawBSONDocument(collections.Mapping):
__slots__ = ('__raw', '__inflated_doc', '__codec_options')
_type_marker = _RAW_BSON_DOCUMENT_MARKER

def __init__(self, bson_bytes, codec_options=DEFAULT_CODEC_OPTIONS):
def __init__(self, bson_bytes, codec_options=DEFAULT_CODEC_OPTIONS, element_name=None):
"""Create a new :class:`RawBSONDocument`.

:Parameters:
- `bson_bytes`: the BSON bytes that compose this document
- `codec_options` (optional): An instance of
:class:`~bson.codec_options.CodecOptions`.
- `element_name` (optional): the current BSON document field name
"""
self.__raw = bson_bytes
self.__inflated_doc = None
Expand Down