diff --git a/tests/test_c_fallback.py b/tests/test_c_fallback.py new file mode 100644 index 0000000..f6b72aa --- /dev/null +++ b/tests/test_c_fallback.py @@ -0,0 +1,10 @@ +"""Test that the C version fallback/fail works for PyYAML cyaml.""" +from __future__ import print_function, division, absolute_import + +from unittest import TestCase + + +class TestCVersionHandling(TestCase): + + def test_c_fallback(self): + import yamlloader diff --git a/yamlloader/ordereddict/__init__.py b/yamlloader/ordereddict/__init__.py index 415df5d..83da323 100644 --- a/yamlloader/ordereddict/__init__.py +++ b/yamlloader/ordereddict/__init__.py @@ -2,14 +2,23 @@ """YAML loaders and dumpers for PyYAML allowing to keep keys order.""" from __future__ import print_function, division, absolute_import -from .loaders import Loader, SafeLoader, CLoader, CSafeLoader -from .dumpers import Dumper, SafeDumper, CDumper, CSafeDumper +from .loaders import Loader, SafeLoader +from .dumpers import Dumper, SafeDumper -__all__ = ['CLoader', - 'Loader', - 'CDumper', +__c_handler_names = [] + +try: + from .loaders import CLoader, CSafeLoader + from .dumpers import CDumper, CSafeDumper + __c_handler_names += ['CLoader', + 'CDumper', + 'CSafeLoader', + 'CSafeDumper', + ] +except ImportError: # PyYAML.cyaml not available -> error + pass # C version not available + +__all__ = ['Loader', 'Dumper', - 'CSafeLoader', 'SafeLoader', - 'CSafeDumper', - 'SafeDumper'] + 'SafeDumper'] + __c_handler_names diff --git a/yamlloader/ordereddict/dumpers.py b/yamlloader/ordereddict/dumpers.py index 2653f52..09023b3 100644 --- a/yamlloader/ordereddict/dumpers.py +++ b/yamlloader/ordereddict/dumpers.py @@ -20,7 +20,7 @@ def __init__(self, *args, **kwargs): sub_doc = self.__doc__ if sub_doc is None: sub_doc = "" - self.__doc__ = """Dump `:py:class:~collections.OrderedDict` to YAML preserving the order.""" + self.__doc__ = """Dump :py:class:`~collections.OrderedDict` to YAML preserving the order.""" self.__doc__ += sub_doc super(OrderedDumperMixin, self).__init__(*args, **kwargs) self.add_representer(OrderedDict, type(self).represent_ordereddict) @@ -44,17 +44,17 @@ class SafeDumper(OrderedDumperMixin, yaml.SafeDumper): """ -if not hasattr(yaml, 'CDumper') and yamlloader.settings.ALLOW_NON_C_FALLBACK: - yaml.CDumper = yaml.Dumper +if not hasattr(yaml, 'CDumper'): + if yamlloader.settings.ALLOW_NON_C_FALLBACK: + CDumper = Dumper +else: + class CDumper(OrderedDumperMixin, yaml.CDumper): + __doc__ = doc_extension_Cversion - -class CDumper(OrderedDumperMixin, yaml.CDumper): - __doc__ = doc_extension_Cversion - - -if not hasattr(yaml, 'CSafeDumper') and yamlloader.settings.ALLOW_NON_C_FALLBACK: - yaml.CSafeDumper = yaml.SafeDumper - -class CSafeDumper(OrderedDumperMixin, yaml.CSafeDumper): - __doc__ = doc_extension_Cversion +if not hasattr(yaml, 'CSafeDumper'): + if yamlloader.settings.ALLOW_NON_C_FALLBACK: + CSafeDumper = SafeDumper +else: + class CSafeDumper(OrderedDumperMixin, yaml.CSafeDumper): + __doc__ = doc_extension_Cversion diff --git a/yamlloader/ordereddict/loaders.py b/yamlloader/ordereddict/loaders.py index 63f6e9a..63f7fef 100644 --- a/yamlloader/ordereddict/loaders.py +++ b/yamlloader/ordereddict/loaders.py @@ -58,17 +58,17 @@ class SafeLoader(OrderedLoaderMixin, yaml.SafeLoader): pass -if not hasattr(yaml, 'CLoader') and yamlloader.settings.ALLOW_NON_C_FALLBACK: - yaml.CLoader = yaml.Loader - - -class CLoader(OrderedLoaderMixin, yaml.CLoader): - pass - - -if not hasattr(yaml, 'CSafeLoader') and yamlloader.settings.ALLOW_NON_C_FALLBACK: - yaml.CSafeLoader = yaml.SafeLoader - - -class CSafeLoader(OrderedLoaderMixin, yaml.CSafeLoader): - pass +if not hasattr(yaml, 'CLoader'): + if yamlloader.settings.ALLOW_NON_C_FALLBACK: + CLoader = Loader +else: + class CLoader(OrderedLoaderMixin, yaml.CLoader): + pass + + +if not hasattr(yaml, 'CSafeLoader'): + if yamlloader.settings.ALLOW_NON_C_FALLBACK: + CSafeLoader = SafeLoader +else: + class CSafeLoader(OrderedLoaderMixin, yaml.CSafeLoader): + pass