sklearn/utils/_testing.py
def assert_run_python_script(source_code, timeout=60):
    """Utility to check assertions in an independent Python subprocess.

    The script provided in the source code should return 0 and not print
    anything on stderr or stdout.

    This is a port from cloudpickle https://github.com/cloudpipe/cloudpickle

    Parameters
    ----------
    source_code : str
        The Python source code to execute.
    timeout : int, default=60
        Time in seconds before timeout.
    """
    fd, source_file = tempfile.mkstemp(suffix='_src_test_sklearn.py')
    os.close(fd)
    try:
        with open(source_file, 'wb') as f:
            f.write(source_code.encode('utf-8'))
        cmd = [sys.executable, source_file]
        cwd = op.normpath(op.join(op.dirname(sklearn.__file__), '..'))
        env = os.environ.copy()
        try:
            env["PYTHONPATH"] = os.pathsep.join([cwd, env["PYTHONPATH"]])
        except KeyError:
            env["PYTHONPATH"] = cwd
        kwargs = {
            'cwd': cwd,
            'stderr': STDOUT,
            'env': env
        }
        # If coverage is running, pass the config file to the subprocess
        coverage_rc = os.environ.get("COVERAGE_PROCESS_START")
        if coverage_rc:
            kwargs['env']['COVERAGE_PROCESS_START'] = coverage_rc

        kwargs['timeout'] = timeout
        try:
            try:
                out = check_output(cmd, **kwargs)
            except CalledProcessError as e:
                raise RuntimeError(u"script errored with output:\n%s"
                                   % e.output.decode('utf-8'))
            if out != b"":
                raise AssertionError(out.decode('utf-8'))
        except TimeoutExpired as e:
            raise RuntimeError(u"script timeout, output so far:\n%s"
                               % e.output.decode('utf-8'))
    finally:
        os.unlink(source_file)
Utility to check assertions in an independent Python subprocess.

    The script provided in the source code should return 0 and not print
    anything on stderr or stdout.

    This is a port from cloudpickle https://github.com/cloudpipe/cloudpickle

    Parameters
    ----------
    source_code : str
        The Python source code to execute.
    timeout : int, default=60
        Time in seconds before timeout.
    
Utility to check assertions in an independent Python subprocess.
The script provided in the source code should return 0 and not print
anything on stderr or stdout.



Parameters

source_code : str
The Python source code to execute.
timeout : int, default=60
Time in seconds before timeout.
Utility to check assertions in an independent Python subprocess . The script provided in the source code should return 0 and not print anything on stderr or stdout . Parameters source_code : str The Python source code to execute . timeout : int default = 60 Time in seconds before timeout .
def assert_run_python_script(source_code, timeout=60):
    fd, source_file = tempfile.mkstemp(suffix='_src_test_sklearn.py')
    os.close(fd)
    try:
        with open(source_file, 'wb') as f:
            f.write(source_code.encode('utf-8'))
        cmd = [sys.executable, source_file]
        cwd = op.normpath(op.join(op.dirname(sklearn.__file__), '..'))
        env = os.environ.copy()
        try:
            env["PYTHONPATH"] = os.pathsep.join([cwd, env["PYTHONPATH"]])
        except KeyError:
            env["PYTHONPATH"] = cwd
        kwargs = {
            'cwd': cwd,
            'stderr': STDOUT,
            'env': env
        }
        coverage_rc = os.environ.get("COVERAGE_PROCESS_START")
        if coverage_rc:
            kwargs['env']['COVERAGE_PROCESS_START'] = coverage_rc
        kwargs['timeout'] = timeout
        try:
            try:
                out = check_output(cmd, **kwargs)
            except CalledProcessError as e:
                raise RuntimeError(u"script errored with output:\n%s"
                                   % e.output.decode('utf-8'))
            if out != b"":
                raise AssertionError(out.decode('utf-8'))
        except TimeoutExpired as e:
            raise RuntimeError(u"script timeout, output so far:\n%s"
                               % e.output.decode('utf-8'))
    finally:
        os.unlink(source_file)
def assert_run_python_script ( source_code , timeout = 60 ) : fd , source_file = tempfile . mkstemp ( suffix = '_src_test_sklearn.py' ) os . close ( fd ) try : with open ( source_file , 'wb' ) as f : f . write ( source_code . encode ( 'utf-8' ) ) cmd = [ sys . executable , source_file ] cwd = op . normpath ( op . join ( op . dirname ( sklearn . __file__ ) , '..' ) ) env = os . environ . copy ( ) try : env [ "PYTHONPATH" ] = os . pathsep . join ( [ cwd , env [ "PYTHONPATH" ] ] ) except KeyError : env [ "PYTHONPATH" ] = cwd kwargs = { 'cwd' : cwd , 'stderr' : STDOUT , 'env' : env } coverage_rc = os . environ . get ( "COVERAGE_PROCESS_START" ) if coverage_rc : kwargs [ 'env' ] [ 'COVERAGE_PROCESS_START' ] = coverage_rc kwargs [ 'timeout' ] = timeout try : try : out = check_output ( cmd , ** kwargs ) except CalledProcessError as e : raise RuntimeError ( u"script errored with output:\n%s" % e . output . decode ( 'utf-8' ) ) if out != b"" : raise AssertionError ( out . decode ( 'utf-8' ) ) except TimeoutExpired as e : raise RuntimeError ( u"script timeout, output so far:\n%s" % e . output . decode ( 'utf-8' ) ) finally : os . unlink ( source_file )
Utility to check assertions in an independent Python subprocess.
Utility to check assertions in an independent Python subprocess .
"""Utility to check assertions in an independent Python subprocess.

    The script provided in the source code should return 0 and not print
    anything on stderr or stdout.

    This is a port from cloudpickle https://github.com/cloudpipe/cloudpickle

    Parameters
    ----------
    source_code : str
        The Python source code to execute.
    timeout : int, default=60
        Time in seconds before timeout.
    """ # If coverage is running, pass the config file to the subprocess
source_code
timeout