# Path: toolbox.py
# def log(*args):
# 	global loglock
# 	loglock.acquire()
# 	encoding=sys.stdout.encoding
# 	for arg in args:
# 		try:
# 			if type(arg)==type(str('abc')):
# 				arg=arg.decode('utf-8',errors='replace')
# 			elif type(arg)!=type(u'abc'):
# 				try:
# 					arg=str(arg)
# 				except:
# 					arg=unicode(arg,errors='replace')
# 				arg=arg.decode('utf-8',errors='replace')
# 			arg=arg.encode(encoding, errors='replace')
# 			print arg,
# 		except:
# 			print "?"*len(arg),
# 	print
# 	loglock.release()
# 
# class GRPException(Exception):
# 	def __init__(self,msg):
# 		if type(msg)==type(u"abc"):
# 			self.utf_msg=msg
# 			self.str_msg=msg.encode("utf-8",errors='replace')
# 		else:
# 			self.str_msg=msg
# 			self.utf_msg=msg.decode("utf-8",errors='replace')
# 		log("===")
# 		log(format_exc())
# 		log("===")
# 		Exception.__init__(self,self.str_msg)
# 
# 	def __unicode__(self):
# 		return self.utf_msg


import subprocess,sys
import threading, Queue
from time import sleep
from toolbox import log,GRPException
		self.size=0
		self.stderr_queue=Queue.Queue()
		self.stdout_queue=Queue.Queue()
		threading.Thread(target=self.consume_stderr).start()
		self.free_handicap_stones=[]
		self.history=[]

	####low level function####
	def consume_stderr(self):
		while 1:
			try:
				err_line=self.process.stderr.readline().decode("utf-8")
				if err_line:
					log("#",err_line.strip())
					self.stderr_queue.put(err_line)
				else:
					log("leaving consume_stderr thread")
					return
			except Exception,e:
				log("leaving consume_stderr thread due to exception")
				log(e)
				return
	
	def consume_stdout(self):
		while 1:
			try:
				line=self.process.stdout.readline().decode("utf-8")
				if line:
					self.stdout_queue.put(line)
				else:

# Path: toolbox.py
# def log(*args):
# 	global loglock
# 	loglock.acquire()
# 	encoding=sys.stdout.encoding
# 	for arg in args:
# 		try:
# 			if type(arg)==type(str('abc')):
# 				arg=arg.decode('utf-8',errors='replace')
# 			elif type(arg)!=type(u'abc'):
# 				try:
# 					arg=str(arg)
# 				except:
# 					arg=unicode(arg,errors='replace')
# 				arg=arg.decode('utf-8',errors='replace')
# 			arg=arg.encode(encoding, errors='replace')
# 			print arg,
# 		except:
# 			print "?"*len(arg),
# 	print
# 	loglock.release()
# 
# class GRPException(Exception):
# 	def __init__(self,msg):
# 		if type(msg)==type(u"abc"):
# 			self.utf_msg=msg
# 			self.str_msg=msg.encode("utf-8",errors='replace')
# 		else:
# 			self.str_msg=msg
# 			self.utf_msg=msg.decode("utf-8",errors='replace')
# 		log("===")
# 		log(format_exc())
# 		log("===")
# 		Exception.__init__(self,self.str_msg)
# 
# 	def __unicode__(self):
# 		return self.utf_msg


# Path: gtp.py
import subprocess,sys
import threading, Queue
from time import sleep
from toolbox import log,GRPException

		self.size=0
		self.stderr_queue=Queue.Queue()
		self.stdout_queue=Queue.Queue()
		threading.Thread(target=self.consume_stderr).start()
		self.free_handicap_stones=[]
		self.history=[]

	####low level function####
	def consume_stderr(self):
		while 1:
			try:
				err_line=self.process.stderr.readline().decode("utf-8")
				if err_line:
					log("#",err_line.strip())
					self.stderr_queue.put(err_line)
				else:
					log("leaving consume_stderr thread")
					return
			except Exception,e:
				log("leaving consume_stderr thread due to exception")
				log(e)
				return
	
	def consume_stdout(self):
		while 1:
			try:
				line=self.process.stdout.readline().decode("utf-8")
				if line:
					self.stdout_queue.put(line)
				else:

					log("leaving consume_stdout thread")