Skip to content
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

PyMongo C extension + LazyProxy = 💔 #5

Open
ramnes opened this issue Aug 8, 2018 · 3 comments
Open

PyMongo C extension + LazyProxy = 💔 #5

ramnes opened this issue Aug 8, 2018 · 3 comments

Comments

@ramnes
Copy link
Member

ramnes commented Aug 8, 2018

PyMongo does not seem to like LazyProxy objects passed in a query (and most likely in all other places where a dict that will later be serialized to BSON is required).

The bug, in action:

>>> from pymongo import MongoClient
>>> from lazify import LazyProxy
>>> collection = MongoClient().database.collection
>>> collection.find_one({"test": LazyProxy(list)})
... stacktrace ...
InvalidDocument: Cannot encode object: <lazify.LazyProxy object at 0x7f7daa50d990>
>>> 

But surprisingly, it does not happen when the LazyProxy.value is a dict:

>>> # works just as expected (returns nothings)
>>> collection.find_one({"test": LazyProxy(dict)})
>>> 

Also, it's worth noting that the pure-Python version of PyMongo's bson works just as expected:

>>> import sys
>>> sys.modules["bson._cbson"] = None
>>> from pymongo import MongoClient
>>> from lazify import LazyProxy
>>> collection = MongoClient().database.collection
>>> collection = MongoClient().database.collection
>>> collection.find_one({"test": LazyProxy(list)})
>>> 

The issue is most likely an incompatible way of checking the query contents type somewhere in pymongo/bson/_cbsonmodule.c.

Versions in place:

Lazify==0.4.0
pymongo==3.7.1
@ramnes ramnes changed the title PyMongo C extension + LazyProxy = </3 PyMongo C extension + LazyProxy = 💔 Aug 9, 2018
@ramnes
Copy link
Member Author

ramnes commented Aug 17, 2018

Might be related to #4.

@prashantmital
Copy link

PyMongo 3.8 (due for release in the next 3-4 weeks) will introduce a new feature for encoding and decoding custom, user-defined types that might help you solve this problem. If you get the chance, you can take a look at an example of this new functionality in action at https://github.com/mongodb/mongo-python-driver/blob/master/doc/examples/custom_type.rst and also test drive it by installing PyMongo from master.

@ramnes
Copy link
Member Author

ramnes commented Mar 18, 2019

Thanks @prashantmital!

@trubesv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants