Example #1
0
	def checkpoint(self, name):
		""" to be used with with_statement.
			
			with stream.checkpoint():
				stream.next()
				stream.peek(0)
				raise Foo

			will automatically roll back the stream for you upon exception. 
			will eat the exception. 
		"""
		depth = len(self.checkpoints)
		if DEBUG:
			if STEP:
				import pdb; pdb.set_trace()
			print " BEGIN _________________________%s%s" % ('|   '*depth, name)
			print str(self)
		context_id = randid()
		self.checkpoints.append( (context_id, []) ) # the 'next' method will append to this list
		try:
			yield context_id
		except ParseError, e:
			# unroll
			last_checkpoint = self.checkpoints.pop()
			assert last_checkpoint[0] == context_id, "oops, control stack got borked. did you mess with rollback checkpoints? expected %s but got %s" % (context_id, last_checkpoint[0])
			self.push_back_tokens = last_checkpoint[1] + self.push_back_tokens
			if DEBUG:
				print " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%s%s %s %s" % ('|   '*depth, name, 'FAIL', e.message)
Example #2
0
	def register(self, id, timeout, callable, *vargs, **kwargs):
		with self.__lock_expiration:
			# should the alarm thread be sleeping, we wake it up.
			with self.__cond_alarm_sleep:
				self.__cond_alarm_sleep.notifyAll()
			# push the new alarm_item to the heap
			now = time.time()
			tix = "tix" + randid()
			heapq.heappush(self.expiration_heap, (now + timeout, id, tix, callable, vargs, kwargs) )
			self.expiration_tix_lookup[id] = tix