# Buggy code
def default_fill_value(obj):
    """
    Return the default fill value for the argument object.

    The default filling value depends on the datatype of the input
    array or the type of the input scalar:

       ========  ========
       datatype  default
       ========  ========
       bool      True
       int       999999
       float     1.e20
       complex   1.e20+0j
       object    '?'
       string    'N/A'
       ========  ========


    Parameters
    ----------
    obj : ndarray, dtype or scalar
        The array data-type or scalar for which the default fill value
        is returned.

    Returns
    -------
    fill_value : scalar
        The default fill value.

    Examples
    --------
    >>> np.ma.default_fill_value(1)
    999999
    >>> np.ma.default_fill_value(np.array([1.1, 2., np.pi]))
    1e+20
    >>> np.ma.default_fill_value(np.dtype(complex))
    (1e+20+0j)

    """
    if hasattr(obj, "dtype"):
        defval = _check_fill_value(None, obj.dtype)
    elif isinstance(obj, np.dtype):
        if obj.subdtype:
            defval = default_filler.get(obj.subdtype[0].kind, "?")
        elif obj.kind in "Mm":
            defval = default_filler.get(obj.str[1:], "?")
        else:
            defval = default_filler.get(obj.kind, "?")
    elif isinstance(obj, float):
        defval = default_filler["f"]
    elif isinstance(obj, int) or isinstance(obj, long):
        defval = default_filler["i"]
    elif isinstance(obj, bytes):
        defval = default_filler["S"]
    elif isinstance(obj, unicode):
        defval = default_filler["U"]
    elif isinstance(obj, complex):
        defval = default_filler["c"]
    else:
        defval = default_filler["O"]
    return defval


# Traceback (TypeError)
In [32]: A = np.arange(100, dtype='i8').view("(4)i4,(4)i4")

In [33]: Am = np.ma.MaskedArray(A, np.zeros_like(A))

In [34]: Am.sort()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-9b8a457bb910> in <module>()
----> 1 Am.sort()

/home/users/gholl/venv/stable-3.5/lib/python3.5/site-packages/numpy/ma/core.py in sort(self, axis, kind, order, endwith, fill_value)
5417                 filler = fill_value
5418
-> 5419             sidx = self.filled(filler).argsort(axis=axis, kind=kind,
5420                                                order=order)
5421             # save meshgrid memory for 1d arrays

/home/users/gholl/venv/stable-3.5/lib/python3.5/site-packages/numpy/ma/core.py in filled(self, fill_value)
3625             fill_value = self.fill_value
3626         else:
-> 3627             fill_value = _check_fill_value(fill_value, self.dtype)
3628
3629         if self is masked_singleton:

/home/users/gholl/venv/stable-3.5/lib/python3.5/site-packages/numpy/ma/core.py in _check_fill_value(fill_value, ndtype)
428             descr = ndtype.descr
429             fill_value = np.asarray(fill_value, dtype=object)
--> 430             fill_value = np.array(_recursive_set_fill_value(fill_value, descr),
431                                   dtype=ndtype)
432     else:

/home/users/gholl/venv/stable-3.5/lib/python3.5/site-packages/numpy/ma/core.py in _recursive_set_fill_value(fillvalue, dtypedescr)
392             output_value.append(tuple(_recursive_set_fill_value(fval, cdtype)))
393         else:
--> 394             output_value.append(np.array(fval, dtype=cdtype).item())
395     return tuple(output_value)
396

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

# Fixed code
def default_fill_value(obj):
    """
    Return the default fill value for the argument object.

    The default filling value depends on the datatype of the input
    array or the type of the input scalar:

       ========  ========
       datatype  default
       ========  ========
       bool      True
       int       999999
       float     1.e20
       complex   1.e20+0j
       object    '?'
       string    'N/A'
       ========  ========

    For structured types, a structured scalar is returned, with each field the
    default fill value for its type.

    For subarray types, the fill value is an array of the same size containing
    the default scalar fill value.

    Parameters
    ----------
    obj : ndarray, dtype or scalar
        The array data-type or scalar for which the default fill value
        is returned.

    Returns
    -------
    fill_value : scalar
        The default fill value.

    Examples
    --------
    >>> np.ma.default_fill_value(1)
    999999
    >>> np.ma.default_fill_value(np.array([1.1, 2., np.pi]))
    1e+20
    >>> np.ma.default_fill_value(np.dtype(complex))
    (1e+20+0j)

    """

    def _scalar_fill_value(dtype):
        if dtype.kind in "Mm":
            return default_filler.get(dtype.str[1:], "?")
        else:
            return default_filler.get(dtype.kind, "?")

    dtype = _get_dtype_of(obj)
    return _recursive_fill_value(dtype, _scalar_fill_value)