@@ -193,8 +193,98 @@ Python 3
193
193
:ref: `TLS <pymongo-troubleshoot-tls >` section of the Troubleshooting guide.
194
194
.. [#three-six-compat ] Pymongo 4.1 requires Python 3.6.2 or later.
195
195
196
+ For more information about how to read the compatibility tables, see
197
+ :ref: `MongoDB Compatibility Tables. <about-driver-compatibility >`
198
+
196
199
Python 2
197
200
~~~~~~~~
198
201
199
- {+driver-short+} versions 3.7 through 3.12 are compatible with Python 2.7 and PyPy, a Python 2.7-
200
- compatible alternative interpreter.
202
+ {+driver-short+} versions 3.7 through 3.12 are compatible with Python 2.7 and PyPy, a
203
+ Python 2.7-compatible alternative interpreter. However, in some cases, {+driver-short+}
204
+ applications behave differently when running in a Python 2 environment.
205
+
206
+ The following sections describe the differences in behavior between Python 2 and Python 3
207
+ when using {+driver-short+}.
208
+
209
+ Binary Data
210
+ ```````````
211
+
212
+ In all versions of Python, {+driver-short+} encodes instances of the
213
+ `bytes <https://docs.python.org/3/library/stdtypes.html#bytes >`__ class
214
+ as binary data with subtype 0, the default subtype for binary data. In Python 3,
215
+ {+driver-short+} decodes these values to instances of the ``bytes `` class. In Python 2,
216
+ the driver decodes them to instances of the
217
+ `Binary <https://pymongo.readthedocs.io/en/4.11/api/bson/binary.html#bson.binary.Binary >`__
218
+ class with subtype 0. For code examples that show the differences, see the
219
+ :ref: `Extended JSON <pymongo-serialization-binary-data >` page.
220
+
221
+ The driver behaves the same way when decoding JSON binary values with subtype 0. In
222
+ Python 3, it decodes these values to instances of the ``bytes `` class. In Python 2,
223
+ the driver decodes them to instances of the ``Binary `` class with subtype 0. For code
224
+ examples that show the differences, see the
225
+ :ref: `Extended JSON <pymongo-extended-json-binary-values >` page.
226
+
227
+ Pickled ObjectIds
228
+ `````````````````
229
+
230
+ If you pickled an ``ObjectId `` in Python 2 and want to unpickle it in Python 3, you must
231
+ pass ``encoding='latin-1' `` as an argument to the ``pickle.loads() `` method.
232
+
233
+ The following example shows how to use Python 3 to unpickle an ``ObjectId `` that was
234
+ pickled in Python 2:
235
+
236
+ .. code-block :: python
237
+ :emphasize- lines: 2
238
+
239
+ import pickle
240
+ pickle.loads(b ' <ObjectId byte stream>' , encoding = ' latin-1' )
241
+
242
+ If a Python 3 application uses a compatible serialization protocol to pickle an ``ObjectId ``,
243
+ you can use Python 2 to unpickle it. To specify a compatible protocol in Python 3, pass
244
+ a value of 0, 1, or 2 for the ``protocol `` parameter of the ``pickle.dumps() `` method.
245
+
246
+ The following example pickles an ``ObjectId `` in Python 3, then prints the ``ObjectId ``
247
+ and resulting ``bytes `` instance:
248
+
249
+ .. io-code-block ::
250
+ :copyable: true
251
+
252
+ .. input ::
253
+ :language: python
254
+
255
+ import pickle
256
+ from bson.objectid import ObjectId
257
+
258
+ oid = ObjectId()
259
+ oid_bytes = pickle.dumps(oid, protocol=2)
260
+ print("ObjectId: {}".format(oid))
261
+ print("ObjectId bytes: {}".format(oid_bytes))
262
+
263
+ .. output ::
264
+ :language: shell
265
+
266
+ ObjectId: 67af9b1fae9260c0e97eb9eb
267
+ ObjectId bytes: b'\x 80\x 02cbson.objectid\n ObjectId\n q\x 00...
268
+
269
+ The following example unpickles the ``ObjectId `` from the previous example, and then
270
+ prints the ``bytes `` and ``ObjectId `` instances:
271
+
272
+ .. io-code-block ::
273
+ :copyable: true
274
+
275
+ .. input ::
276
+ :language: python
277
+
278
+ import pickle
279
+ from bson.objectid import ObjectId
280
+
281
+ oid_bytes = b'\x 80\x 02cbson.objectid\n ObjectId\n q\x 00...'
282
+ oid = pickle.loads(oid_bytes)
283
+ print("ObjectId bytes: {}".format(oid_bytes))
284
+ print("ObjectId: {}".format(oid))
285
+
286
+ .. output ::
287
+ :language: shell
288
+
289
+ ObjectId bytes: b'\x 80\x 02cbson.objectid\n ObjectId\n q\x 00)...
290
+ ObjectId: 67af9b1fae9260c0e97eb9eb
0 commit comments