From e6e2cc8f9db204130dae778d878fc5e03923902e Mon Sep 17 00:00:00 2001 From: Min RK Date: Sat, 4 Apr 2015 15:55:07 -0700 Subject: [PATCH] copy utils.process.check_pid to html.utils since jupyter_notebook will not depend on IPython --- IPython/html/notebookapp.py | 3 +-- IPython/html/utils.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/IPython/html/notebookapp.py b/IPython/html/notebookapp.py index 2cea088e1..7ddf075cc 100644 --- a/IPython/html/notebookapp.py +++ b/IPython/html/notebookapp.py @@ -78,7 +78,6 @@ from IPython.kernel.zmq.session import Session from IPython.nbformat.sign import NotebookNotary from IPython.utils.importstring import import_item from IPython.utils import submodule -from IPython.utils.process import check_pid from IPython.utils.traitlets import ( Dict, Unicode, Integer, List, Bool, Bytes, Instance, TraitError, Type, @@ -88,7 +87,7 @@ from IPython.utils.path import filefind, get_ipython_dir from IPython.utils.sysinfo import get_sys_info from .nbextensions import SYSTEM_NBEXTENSIONS_DIRS -from .utils import url_path_join +from .utils import url_path_join, check_pid #----------------------------------------------------------------------------- # Module globals diff --git a/IPython/html/utils.py b/IPython/html/utils.py index 908830247..017a8d56e 100644 --- a/IPython/html/utils.py +++ b/IPython/html/utils.py @@ -5,8 +5,10 @@ from __future__ import print_function +import errno import os import stat +import sys try: from urllib.parse import quote, unquote @@ -139,3 +141,30 @@ def to_api_path(os_path, root=''): path = '/'.join(parts) return path + +# Copy of IPython.utils.process.check_pid: + +def _check_pid_win32(pid): + import ctypes + # OpenProcess returns 0 if no such process (of ours) exists + # positive int otherwise + return bool(ctypes.windll.kernel32.OpenProcess(1,0,pid)) + +def _check_pid_posix(pid): + """Copy of IPython.utils.pro""" + try: + os.kill(pid, 0) + except OSError as err: + if err.errno == errno.ESRCH: + return False + elif err.errno == errno.EPERM: + # Don't have permission to signal the process - probably means it exists + return True + raise + else: + return True + +if sys.platform == 'win32': + check_pid = _check_pid_win32 +else: + check_pid = _check_pid_posix