Ticket #7676: threadsafe_serializers-same_public_api.diff

File threadsafe_serializers-same_public_api.diff, 3.0 KB (added by mrts, 16 years ago)

Don't break the public API and be explicit about what is and is not thread-safe.

  • django/core/serializers/__init__.py

     
    3535_serializers = {}
    3636
    3737def register_serializer(format, serializer_module):
    38     """Register a new serializer by passing in a module name."""
    39     module = __import__(serializer_module, {}, {}, [''])
    40     _serializers[format] = module
     38    """Register a new serializer by passing in a module name.
     39    Not thread-safe."""
     40    _register_serializer(_serializers, format, serializer_module)
    4141
    4242def unregister_serializer(format):
    43     """Unregister a given serializer"""
     43    """Unregister a given serializer. Not thread-safe."""
    4444    del _serializers[format]
    4545
    4646def get_serializer(format):
     47    """Thread-safe."""
    4748    if not _serializers:
    4849        _load_serializers()
    4950    return _serializers[format].Serializer
    5051
    5152def get_serializer_formats():
     53    """Thread-safe."""
    5254    if not _serializers:
    5355        _load_serializers()
    5456    return _serializers.keys()
    5557
    5658def get_public_serializer_formats():
     59    """Thread-safe."""
    5760    if not _serializers:
    5861        _load_serializers()
    5962    return [k for k, v in _serializers.iteritems() if not v.Serializer.internal_use_only]
    6063
    6164def get_deserializer(format):
     65    """Thread-safe."""
    6266    if not _serializers:
    6367        _load_serializers()
    6468    return _serializers[format].Deserializer
     
    6771    """
    6872    Serialize a queryset (or any iterator that returns database objects) using
    6973    a certain serializer.
     74    Thread-safe.
    7075    """
    7176    s = get_serializer(format)()
    7277    s.serialize(queryset, **options)
     
    7883    m2m_relation_dict)``, where ``obj`` is a instantiated -- but *unsaved* --
    7984    object, and ``m2m_relation_dict`` is a dictionary of ``{m2m_field_name :
    8085    list_of_related_objects}``.
     86    Thread-safe.
    8187    """
    8288    d = get_deserializer(format)
    8389    return d(stream_or_string)
     
    8793    Register built-in and settings-defined serializers. This is done lazily so
    8894    that user code has a chance to (e.g.) set up custom settings without
    8995    needing to be careful of import order.
     96    Thread-safe.
    9097    """
     98    global _serializers
     99    serializers = {}
    91100    for format in BUILTIN_SERIALIZERS:
    92         register_serializer(format, BUILTIN_SERIALIZERS[format])
     101        _register_serializer(serializers, format, BUILTIN_SERIALIZERS[format])
    93102    if hasattr(settings, "SERIALIZATION_MODULES"):
    94103        for format in settings.SERIALIZATION_MODULES:
    95             register_serializer(format, settings.SERIALIZATION_MODULES[format])
     104            _register_serializer(serializers, format,
     105                    settings.SERIALIZATION_MODULES[format])
     106    _serializers = serializers
     107
     108def _register_serializer(serializers, format, serializer_module):
     109    """Register a new serializer by passing in a module name."""
     110    module = __import__(serializer_module, {}, {}, [''])
     111    serializers[format] = module
Back to Top