3232from .writer import blocking_writer
3333from .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
3835try :
3936 import amazon .ion .ionc as ionc
37+ __IS_C_EXTENSION_SUPPORTED = True
4038except 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=
554557def 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 :
0 commit comments