From b5b27bf249cc0e4a7fbb3054b44a9df15c25a680 Mon Sep 17 00:00:00 2001 From: Jonas 'Mayou36' Eschle Date: Sat, 10 Feb 2018 21:39:11 +0100 Subject: [PATCH 1/2] Change behavior of C version fallback, also works with no fallback allowed when importing non-C version --- tests/test_c_fallback.py | 10 ++++++++++ yamlloader/ordereddict/__init__.py | 25 +++++++++++++++++-------- yamlloader/ordereddict/dumpers.py | 28 ++++++++++++++-------------- yamlloader/ordereddict/loaders.py | 28 ++++++++++++++-------------- 4 files changed, 55 insertions(+), 36 deletions(-) create mode 100644 tests/test_c_fallback.py 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..4fcc8ac 100644 --- a/yamlloader/ordereddict/dumpers.py +++ b/yamlloader/ordereddict/dumpers.py @@ -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 - - - -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, 'CDumper'): + if yamlloader.settings.ALLOW_NON_C_FALLBACK: + CDumper = Dumper +else: + class CDumper(OrderedDumperMixin, yaml.CDumper): + __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 From 1b9cd5f8049fc478a805c01f99393a3a007c4eb7 Mon Sep 17 00:00:00 2001 From: Jonas 'Mayou36' Eschle Date: Sat, 10 Feb 2018 21:52:14 +0100 Subject: [PATCH 2/2] Fix doc typo --- yamlloader/ordereddict/dumpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yamlloader/ordereddict/dumpers.py b/yamlloader/ordereddict/dumpers.py index 4fcc8ac..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)