'Register a handler displaying the traceback on a user signal. Raise the
signal and check the written traceback.
If chain is True, check that the previous signal handler is called.
Raise an error if the output doesn\'t match the expected format.'

@unittest.skipIf((not hasattr(faulthandler, 'register')), 'need    faulthandler.register')
def check_register(self, filename=False, all_threads=False, unregister=False, chain=False):

    signum = signal.SIGUSR1
    code = '\n                                                import    faulthandler\n                                                import    os\n                                                import    signal\n                                                import    sys\n\n                                                def    func(signum):\n                                                                os.kill(os.getpid(),    signum)\n\n                                                def    handler(signum,    frame):\n                                                                handler.called    =    True\n                                                handler.called    =    False\n\n                                                exitcode    =    0\n                                                signum    =    {signum}\n                                                unregister    =    {unregister}\n                                                chain    =    {chain}\n\n                                                if    {has_filename}:\n                                                                file    =    open({filename},    "wb")\n                                                else:\n                                                                file    =    None\n                                                if    chain:\n                                                                signal.signal(signum,    handler)\n                                                faulthandler.register(signum,    file=file,\n                                                                                                                                        all_threads={all_threads},    chain={chain})\n                                                if    unregister:\n                                                                faulthandler.unregister(signum)\n                                                func(signum)\n                                                if    chain    and    not    handler.called:\n                                                                if    file    is    not    None:\n                                                                                output    =    file\n                                                                else:\n                                                                                output    =    sys.stderr\n                                                                print("Error:    signal    handler    not    called!",    file=output)\n                                                                exitcode    =    1\n                                                if    file    is    not    None:\n                                                                file.close()\n                                                sys.exit(exitcode)\n                                                '
    code = code.format(filename=repr(filename), has_filename=bool(filename), all_threads=all_threads, signum=signum, unregister=unregister, chain=chain)
    (trace, exitcode) = self.get_output(code, filename)
    trace = '\n'.join(trace)
    if (not unregister):
        if all_threads:
            regex = 'Current    thread    XXX    \\(most    recent    call    first\\):\n'
        else:
            regex = 'Stack    \\(most    recent    call    first\\):\n'
        regex = expected_traceback(7, 28, regex)
        self.assertRegex(trace, regex)
    else:
        self.assertEqual(trace, '')
    if unregister:
        self.assertNotEqual(exitcode, 0)
    else:
        self.assertEqual(exitcode, 0)
