Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-1122

test_bson.py: test_bad_encode fails on OS X Python 2.6

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 3.4
    • Affects Version/s: 3.3, 3.4
    • Component/s: None
    • None
    • Fully Compatible

      On OS X running the test suite with python2.6:

      (python2.6) ➜  mongo-python-driver git:(master) ✗ python2.6 setup.py test
      running test
      running egg_info
      writing requirements to pymongo.egg-info/requires.txt
      writing pymongo.egg-info/PKG-INFO
      writing top-level names to pymongo.egg-info/top_level.txt
      writing dependency_links to pymongo.egg-info/dependency_links.txt
      reading manifest file 'pymongo.egg-info/SOURCES.txt'
      reading manifest template 'MANIFEST.in'
      writing manifest file 'pymongo.egg-info/SOURCES.txt'
      running build_ext
      test_uri_options (test_auth.TestAuthURIOptions) ... skipped 'Authentication is not enabled on the server'
      test_delegated_auth (test_auth.TestDelegatedAuth) ... skipped 'Authentication is not enabled on the server'
      skipped 'Kerberos module not available.'
      skipped 'Must set SASL_HOST, SASL_USER, and SASL_PASS to test SASL'
      test_scram_sha1 (test_auth.TestSCRAMSHA1) ... skipped 'Authentication is not enabled on the server'
      test_binary (test_binary.TestBinary) ... ok
      test_equality (test_binary.TestBinary) ... ok
      test_exceptions (test_binary.TestBinary) ... ok
      test_hash (test_binary.TestBinary) ... ok
      test_legacy_csharp_uuid (test_binary.TestBinary) ... ok
      test_legacy_csharp_uuid_roundtrip (test_binary.TestBinary) ... ok
      test_legacy_java_uuid (test_binary.TestBinary) ... ok
      test_legacy_java_uuid_roundtrip (test_binary.TestBinary) ... ok
      test_pickle (test_binary.TestBinary) ... ok
      test_repr (test_binary.TestBinary) ... ok
      test_subtype (test_binary.TestBinary) ... ok
      test_uri_to_uuid (test_binary.TestBinary) ... ok
      test_uuid_queries (test_binary.TestBinary) ... ok
      test_aware_datetime (test_bson.TestBSON) ... ok
      test_bad_dbref (test_bson.TestBSON) ... ok
      test_bad_encode (test_bson.TestBSON) ... Exception RuntimeError: 'maximum recursion depth exceeded while calling a Python object' in <type 'exceptions.AttributeError'> ignored
      ERROR
      ======================================================================
      ERROR: test_bad_encode (test_bson.TestBSON)
      ----------------------------------------------------------------------
      Traceback (most recent call last):
        File "/Users/shane/git/mongo-python-driver/test/test_bson.py", line 568, in test_bad_encode
          self.assertRaises(RuntimeError, BSON.encode, evil_data)
        File "/Users/shane/venv/python2.6/lib/python2.6/site-packages/unittest2/case.py", line 748, in assertRaises
          return context.handle('assertRaises', args, kwargs)
        File "/Users/shane/venv/python2.6/lib/python2.6/site-packages/unittest2/case.py", line 194, in handle
          callable_obj(*args, **kwargs)
        File "/Users/shane/git/mongo-python-driver/bson/__init__.py", line 935, in encode
          return cls(_dict_to_bson(document, check_keys, codec_options))
        File "/Users/shane/git/mongo-python-driver/bson/__init__.py", line 743, in _dict_to_bson
          raise TypeError("encoder expected a mapping type but got: %r" % (doc,))
      TypeError: encoder expected a mapping type but got: {'a': {...}}
      

      The problem seems to be that the exception raised by bson.BSON.encode in test_bad_encode depends on the recursion limit that was set.

      For example, this code:

      import sys
      from bson import BSON
      
      evil_data = {}
      evil_data["a"] = evil_data
      
      for i in range(4, 10):
          try:
              sys.setrecursionlimit(i)
              BSON.encode(evil_data)
          except Exception as e:
              print("recursion limit %d: %r" % (i, e))
      

      produces this output:

      recursion limit 4: AttributeError("'dict' object has no attribute '_type_marker'",)
      recursion limit 5: AttributeError("'dict' object has no attribute '_type_marker'",)
      recursion limit 6: RuntimeError('maximum recursion depth exceeded',)
      recursion limit 7: TypeError("encoder expected a mapping type but got: {'a': {...}}",)
      recursion limit 8: TypeError("encoder expected a mapping type but got: {'a': {...}}",)
      recursion limit 9: RuntimeError('maximum recursion depth exceeded',)
      

      A simple fix would be to change assertRaises(RuntimeError,...) to assertRaises(Exception,...) in test_bad_encode:
      https://github.com/mongodb/mongo-python-driver/blob/34dev/test/test_bson.py#L568

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            shane.harvey@mongodb.com Shane Harvey
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: