<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def expand(x, pending, stage):
 """ Expand the pending files in the current stage. Parameters x: str The file to expand. pending : str The list of pending files to expand. stage: str The current stage for file expansion, used for matching the prefix of files. """

    if x in history and x not in ['mshadow/mshadow/expr_scalar-inl.h']: # MULTIPLE includes
        return

    if x in pending:
        #print('loop found: {} in {}'.format(x, pending))
        return

    whtspace = '  ' * expand.treeDepth
    expand.fileCount += 1
    comment = u"//=====[{:3d}] STAGE:{:>4} {}EXPANDING: {} =====\n\n".format(expand.fileCount, stage, whtspace, x)
    out.write(comment.encode('ascii'))
    print(comment)

    with open(x, 'rb') as x_h:
        for line in x_h.readlines():
            uline = line.decode('utf-8')
            if '#define DMLC_LOG_STACK_TRACE 1' in uline.strip():
                # Do not enable stacktrace logging
                continue
            if uline.find('#include') < 0:
                out.write(line)
                continue
            if uline.strip().find('#include') > 0:
                print(uline)
                continue
            m = re1.search(uline)
            if not m:
                m = re2.search(uline)
            if m:
                path = m.groups()[0]
            else:
                m = re3.search(uline)
                if m:
                    path = 'execinfo.h'
                else:
                    print(uline + ' not found')
                    continue
            h = path.strip('./') if "../3rdparty/" not in path else path
            if h.endswith('complex.h') and x.endswith('openblas_config.h'):
                source = ''
            elif h.startswith('ps/'):
                source = '../3rdparty/ps-lite/include/' + h
            else:
                source = find_source(h, x, stage)
            if not source:
                if (h not in blacklist and
                    h not in sysheaders and
                    'mkl' not in h and
                    'nnpack' not in h and
                    'tensorrt' not in h and
                    not h.endswith('.cuh')): sysheaders.append(h)
            else:
                expand.treeDepth += 1
                expand(source, pending + [x], stage)
                expand.treeDepth -= 1

    out.write(u"//===== EXPANDED  : {} =====\n\n".format(x).encode('ascii'))
    history.add(x)