Skip to content
Closed
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
51 changes: 24 additions & 27 deletions python/pyarrow/types.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.

import atexit
import re
import warnings

Expand Down Expand Up @@ -589,32 +590,6 @@ cdef class UnknownExtensionType(ExtensionType):
return self.serialized


cdef class _ExtensionTypesInitializer:
#
# A private object that handles process-wide registration of the Python
# ExtensionType.
#

def __cinit__(self):
cdef:
DataType storage_type
shared_ptr[CExtensionType] cpy_ext_type

# Make a dummy C++ ExtensionType
storage_type = null()
check_status(CPyExtensionType.FromClass(storage_type.sp_type,
ExtensionType, &cpy_ext_type))
check_status(
RegisterPyExtensionType(<shared_ptr[CDataType]> cpy_ext_type))

def __dealloc__(self):
# This needs to be done explicitly before the Python interpreter is
# finalized. If the C++ type is destroyed later in the process
# teardown stage, it will invoke CPython APIs such as Py_DECREF
# with a destroyed interpreter.
check_status(UnregisterPyExtensionType())


cdef class Field:
"""
A named field, with a data type, nullability, and optional metadata.
Expand Down Expand Up @@ -1863,4 +1838,26 @@ def is_float_value(object obj):
return IsPyFloat(obj)


_extension_types_initializer = _ExtensionTypesInitializer()
def _register_py_extension_type():
cdef:
DataType storage_type
shared_ptr[CExtensionType] cpy_ext_type

# Make a dummy C++ ExtensionType
storage_type = null()
check_status(CPyExtensionType.FromClass(storage_type.sp_type,
ExtensionType, &cpy_ext_type))
check_status(
RegisterPyExtensionType(<shared_ptr[CDataType]> cpy_ext_type))


def _unregister_py_extension_type():
# This needs to be done explicitly before the Python interpreter is
# finalized. If the C++ type is destroyed later in the process
# teardown stage, it will invoke CPython APIs such as Py_DECREF
# with a destroyed interpreter.
check_status(UnregisterPyExtensionType())


_register_py_extension_type()
atexit.register(_unregister_py_extension_type)