Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Feature/impr c fallback #13

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/test_c_fallback.py
Original file line number Diff line number Diff line change
@@ -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
25 changes: 17 additions & 8 deletions yamlloader/ordereddict/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 13 additions & 13 deletions yamlloader/ordereddict/dumpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
28 changes: 14 additions & 14 deletions yamlloader/ordereddict/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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