Skip to content

Commit c0a4ef5

Browse files
authored
Fix c_ext logic so that users cannot enable the c extension when it is not loaded (amazon-ion#280)
1 parent 0860431 commit c0a4ef5

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

amazon/ion/simpleion.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@
3232
from .writer import blocking_writer
3333
from .writer_binary import binary_writer
3434

35-
36-
# Using C extension as default, and pure python implementation if C extension doesn't exist.
37-
c_ext = True
3835
try:
3936
import amazon.ion.ionc as ionc
37+
__IS_C_EXTENSION_SUPPORTED = True
4038
except ModuleNotFoundError:
41-
c_ext = False
39+
__IS_C_EXTENSION_SUPPORTED = False
40+
# TODO: when we release a new major version, come up with a better way to encapsulate these two variables.
41+
# __IS_C_EXTENSION_SUPPORTED is a private flag indicating whether the c extension was loaded/is supported.
42+
# c_ext is a user-facing flag to check whether the c extension is available and/or disable the c extension.
43+
# However, if you mutate it, then it can no longer be used to see if the c extension is available.
44+
c_ext = __IS_C_EXTENSION_SUPPORTED
4245

4346
_ION_CONTAINER_END_EVENT = IonEvent(IonEventType.CONTAINER_END)
4447
_IVM = b'\xe0\x01\x00\xea'
@@ -537,7 +540,7 @@ def dump(obj, fp, imports=None, binary=True, sequence_as_stream=False, skipkeys=
537540
use_decimal=True, namedtuple_as_object=True, tuple_as_array=True, bigint_as_string=False, sort_keys=False,
538541
item_sort_key=None, for_json=None, ignore_nan=False, int_as_string_bitcount=None, iterable_as_array=False,
539542
tuple_as_sexp=False, omit_version_marker=False, **kw):
540-
if c_ext and (imports is None and indent is None):
543+
if c_ext and __IS_C_EXTENSION_SUPPORTED and (imports is None and indent is None):
541544
return dump_extension(obj, fp, binary=binary, sequence_as_stream=sequence_as_stream,
542545
tuple_as_sexp=tuple_as_sexp, omit_version_marker=omit_version_marker)
543546
else:
@@ -554,7 +557,7 @@ def dump(obj, fp, imports=None, binary=True, sequence_as_stream=False, skipkeys=
554557
def load(fp, catalog=None, single_value=True, encoding='utf-8', cls=None, object_hook=None, parse_float=None,
555558
parse_int=None, parse_constant=None, object_pairs_hook=None, use_decimal=None, parse_eagerly=True,
556559
text_buffer_size_limit=None, **kw):
557-
if c_ext and catalog is None:
560+
if c_ext and __IS_C_EXTENSION_SUPPORTED and catalog is None:
558561
return load_extension(fp, parse_eagerly=parse_eagerly, single_value=single_value,
559562
text_buffer_size_limit=text_buffer_size_limit)
560563
else:

tests/test_simpleion.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from pytest import raises
2626

27+
from amazon.ion import simpleion
2728
from amazon.ion.exceptions import IonException
2829
from amazon.ion.symbols import SymbolToken, SYSTEM_SYMBOL_TABLE
2930
from amazon.ion.writer_binary import _IVM
@@ -752,3 +753,11 @@ def test_loads_large_string():
752753
except Exception:
753754
assert False
754755

756+
757+
# This test ensures that the c_ext flag does not override whether the extension is actually supported.
758+
def test_setting_c_ext_flag():
759+
if not simpleion.c_ext:
760+
simpleion.c_ext = True
761+
762+
value = loads("{a:1}")
763+
dumps(value)

0 commit comments

Comments
 (0)