Skip to content

2to3 breaks LongField #1253

@gilbsgilbs

Description

@gilbsgilbs

Hi,
It appears that 2to3 changes long() to int() since long type does not exist any longer in python 3. However, this is problematic since pymongo cannot cast to the correct BSON type then. If your LongField is small enough, it will be considered as a standard 32 bits int field. The reciprocal is also true: if your IntField is big enough, it will become a 64bits integer (because of Python 3 casting to long automatically), contradicting the docstring. Still, for this last case, I'm not sure whether it is the expected behaviour or not.
BTW, you can easilly note that IntField and LongField have the exact same body in python 3 (after running setup.py which runs 2to3).

In addition to the potential storage and performance issues it can lead to (I guess), this can become really annoying when you start dealing with capped collections, because you have to keep fixed-size documents. If you update a LongField value from 3 (can be an int32) to 4294967294 (must be an int64), it will break your capped collection.

Here is an easy script that will let you reproduce this LongField issue with Python 3:

from mongoengine import connect, Document, LongField

connect('longfield-issue')

class SomeDocument(Document):
    some_int = LongField()
    meta = { 'max_size': 1024000, }  # Capped collection

doc = SomeDocument(some_int=3)
doc.save()  # This is ok

doc.some_int = 2147483647  # MAX INT32; still ok
doc.save()

doc.some_int = 2147483648  # Long int; Not ok because collection is capped
doc.save()  # Fails although it shouldn't:
# Cannot change the size of a document in a capped collection: 36 != 40

For the IntField issue, I will let you decide whether it's a bug or not. Python/PyMongo/BSON are smart enough to cast the int into the correct type for mongo. Personally, I would prefer it to throw an exception and force me to explicitely set the type of my Field to LongField.

If I can help you in any other manner, please let me know.
Thank you.
gilbsgilbs

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions