Skip to content

Commit

Permalink
Merge pull request #134 from cash/func_wrappers
Browse files Browse the repository at this point in the history
replaces copied functions with internal functions in engine module
  • Loading branch information
niklasf committed Jun 18, 2017
2 parents cc88fb0 + bfe67be commit c68e893
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 42 deletions.
40 changes: 40 additions & 0 deletions chess/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,43 @@ def pid(self):

def __repr__(self):
return "<SpurProcess at {0} (pid={1})>".format(hex(id(self)), self.pid())


def _popen_engine(command, engine_cls, setpgrp=False, _popen_lock=threading.Lock(), **kwargs):
"""
Opens a local chess engine process.
:param engine_cls: Engine class
:param setpgrp: Open the engine process in a new process group. This will
stop signals (such as keyboards interrupts) from propagating from the
parent process. Defaults to ``False``.
"""
engine = engine_cls()

popen_args = {}
if setpgrp:
try:
# Windows
popen_args["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP
except AttributeError:
# Unix
popen_args["preexec_fn"] = os.setpgrp
popen_args.update(kwargs)

# Work around possible race condition in Python 2 subprocess module,
# that can occur when concurrently opening processes.
with _popen_lock:
PopenProcess(engine, command, **popen_args)

return engine


def _spur_spawn_engine(shell, command, engine_cls):
"""
Spawns a remote engine using a `Spur`_ shell.
.. _Spur: https://pypi.python.org/pypi/spur
"""
engine = engine_cls()
SpurProcess(engine, shell, command)
return engine
25 changes: 4 additions & 21 deletions chess/uci.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from chess.engine import SpurProcess
from chess.engine import LOGGER
from chess.engine import FUTURE_POLL_TIMEOUT
from chess.engine import _popen_engine
from chess.engine import _spur_spawn_engine

import chess

Expand Down Expand Up @@ -1179,24 +1181,7 @@ def popen_engine(command, engine_cls=Engine, setpgrp=False, _popen_lock=threadin
stop signals (such as keyboards interrupts) from propagating from the
parent process. Defaults to ``False``.
"""
engine = engine_cls()

popen_args = {}
if setpgrp:
try:
# Windows
popen_args["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP
except AttributeError:
# Unix
popen_args["preexec_fn"] = os.setpgrp
popen_args.update(kwargs)

# Work around possible race condition in Python 2 subprocess module,
# that can occur when concurrently opening processes.
with _popen_lock:
PopenProcess(engine, command, **popen_args)

return engine
return _popen_engine(command, engine_cls, setpgrp, _popen_lock, **kwargs)


def spur_spawn_engine(shell, command, engine_cls=Engine):
Expand All @@ -1210,6 +1195,4 @@ def spur_spawn_engine(shell, command, engine_cls=Engine):
.. _Spur: https://pypi.python.org/pypi/spur
"""
engine = engine_cls()
SpurProcess(engine, shell, command)
return engine
return _spur_spawn_engine(shell, command, engine_cls)
26 changes: 5 additions & 21 deletions chess/xboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from chess.engine import SpurProcess
from chess.engine import LOGGER
from chess.engine import FUTURE_POLL_TIMEOUT
from chess.engine import _popen_engine
from chess.engine import _spur_spawn_engine

import chess

Expand Down Expand Up @@ -725,7 +727,7 @@ def is_alive(self):
return self.process.is_alive()


def popen_engine(command, engine_cls=Engine, setpgrp=False, _popen_lock=threading.Lock()):
def popen_engine(command, engine_cls=Engine, setpgrp=False, _popen_lock=threading.Lock(), **kwargs):
"""
Opens a local chess engine process.
Expand All @@ -739,23 +741,7 @@ def popen_engine(command, engine_cls=Engine, setpgrp=False, _popen_lock=threadin
stop signals (such as keyboards interrupts) from propagating from the
parent process. Defaults to ``False``.
"""
engine = engine_cls()

kwargs = {}
if setpgrp:
try:
# Windows
kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP
except AttributeError:
# Unix
kwargs["preexec_fn"] = os.setpgrp

# Work around possible race condition in Python 2 subprocess module,
# that can occur when concurrently opening processes.
with _popen_lock:
PopenProcess(engine, command, **kwargs)

return engine
return _popen_engine(command, engine_cls, setpgrp, _popen_lock, **kwargs)


def spur_spawn_engine(shell, command, engine_cls=Engine):
Expand All @@ -769,6 +755,4 @@ def spur_spawn_engine(shell, command, engine_cls=Engine):
.. _Spur: https://pypi.python.org/pypi/spur
"""
engine = engine_cls()
SpurProcess(engine, shell, command)
return engine
return _spur_spawn_engine(shell, command, engine_cls)

0 comments on commit c68e893

Please sign in to comment.