def onError(self): '''What must happen when an exception is raised during test execution? Returns True if the test failed.''' self.errorDump = Traceback.get() self.report.say('Exception occurred:') self.report.say(self.errorDump) return True
def dump(buffer, message, withinElement=None, removeFirstLine=False, dumpTb=True): '''Dumps the error p_message in p_buffer.''' # Define some handful shortcuts e = buffer.env ns = e.namespaces dcNs = e.ns(e.NS_DC) officeNs = e.ns(e.NS_OFFICE) textNs = e.ns(e.NS_TEXT) if withinElement: buffer.write('<%s>' % withinElement.OD.elem) for subTag in withinElement.subTags: buffer.write('<%s>' % subTag.elem) buffer.write('<%s:annotation><%s:creator>POD</%s:creator>' \ '<%s:date>%s</%s:date><%s:p>' % \ (officeNs, dcNs, dcNs, dcNs, time.strftime('%Y-%m-%dT%H:%M:%S'), dcNs, textNs)) buffer.dumpContent(message) buffer.write('</%s:p>' % textNs) if dumpTb: # We don't dump the traceback if it is an expression error (it is # already included in the error message) PodError.dumpTraceback(buffer, Traceback.get(), textNs, removeFirstLine) buffer.write('</%s:annotation>' % officeNs) if withinElement: subTags = withinElement.subTags[:] subTags.reverse() for subTag in subTags: buffer.write('</%s>' % subTag.elem) buffer.write('</%s>' % withinElement.OD.elem)
def manageError(self, result, context, errorMessage, originalError=None): '''Manage the encountered error: dump it into the buffer or raise an exception.''' if self.buffer.env.raiseOnError: if not self.buffer.pod: # Add in the error message the line nb where the errors occurs # within the PX. locator = self.buffer.env.parser.locator # The column number may not be given col = locator.getColumnNumber() if col == None: col = '' else: col = ', column %d' % col errorMessage += ' (line %s%s)' % (locator.getLineNumber(), col) # Integrate the traceback (at least, its last lines) errorMessage += '\n' + Traceback.get(6).decode('utf-8') if originalError: raise EvaluationError(originalError, errorMessage) raise Exception(errorMessage) # Create a temporary buffer to dump the error. If I reuse this buffer to # dump the error (what I did before), and we are, at some depth, in a # for loop, this buffer will contain the error message and not the # content to repeat anymore. It means that this error will also show up # for every subsequent iteration. tempBuffer = self.buffer.clone() PodError.dump(tempBuffer, errorMessage, withinElement=self.elem) tempBuffer.evaluate(result, context)
def evaluate(self, result, context, subElements=True, removeMainElems=False): '''Evaluates this buffer given the current p_context and add the result into p_result. With pod, p_result is the root file buffer; with px it is a memory buffer.''' if not subElements: # Dump the root tag in this buffer, but not its content res = self.reTagContent.match(self.content.strip()) if not res: result.write(self.content) else: g = res.group result.write('<%s:%s%s></%s:%s>' % (g(1), g(2), g(3), g(1), g(2))) else: if removeMainElems: self.removeAutomaticExpressions() iter = BufferIterator(self) currentIndex = self.getStartIndex(removeMainElems) while iter.hasNext(): index, evalEntry = next(iter) result.write(self.content[currentIndex:index]) currentIndex = index + 1 if isinstance(evalEntry, Expression): try: res, escape = evalEntry.evaluate(context) if escape: result.dumpContent(res) else: result.write(res) except EvaluationError as e: # This exception has already been treated (see the # "except" block below). Simply re-raise it when needed. if self.env.raiseOnError: raise e except Exception as e: if not self.env.raiseOnError: PodError.dump( result, EVAL_EXPR_ERROR % (evalEntry.expr, e)) else: raise EvaluationError(EVAL_EXPR_ERROR % \ (evalEntry.expr, '\n'+Traceback.get(5))) elif isinstance(evalEntry, Attributes) or \ isinstance(evalEntry, Attribute): result.write(evalEntry.evaluate(context)) else: # It is a subBuffer if evalEntry.action: evalEntry.action.execute(result, context) else: result.write(evalEntry.content) stopIndex = self.getStopIndex(removeMainElems) if currentIndex < (stopIndex - 1): result.write(self.content[currentIndex:stopIndex])
def evaluate(self, result, context, subElements=True, removeMainElems=False): '''Evaluates this buffer given the current p_context and add the result into p_result. With pod, p_result is the root file buffer; with px it is a memory buffer.''' if not subElements: # Dump the root tag in this buffer, but not its content res = self.reTagContent.match(self.content.strip()) if not res: result.write(self.content) else: g = res.group result.write('<%s:%s%s></%s:%s>' % (g(1),g(2),g(3),g(1),g(2))) else: if removeMainElems: self.removeAutomaticExpressions() iter = BufferIterator(self) currentIndex = self.getStartIndex(removeMainElems) while iter.hasNext(): index, evalEntry = next(iter) result.write(self.content[currentIndex:index]) currentIndex = index + 1 if isinstance(evalEntry, Expression): try: res, escape = evalEntry.evaluate(context) if escape: result.dumpContent(res) else: result.write(res) except EvaluationError as e: # This exception has already been treated (see the # "except" block below). Simply re-raise it when needed. if self.env.raiseOnError: raise e except Exception as e: if not self.env.raiseOnError: PodError.dump(result, EVAL_EXPR_ERROR % ( evalEntry.expr, e)) else: raise EvaluationError(EVAL_EXPR_ERROR % \ (evalEntry.expr, '\n'+Traceback.get(5))) elif isinstance(evalEntry, Attributes) or \ isinstance(evalEntry, Attribute): result.write(evalEntry.evaluate(context)) else: # It is a subBuffer if evalEntry.action: evalEntry.action.execute(result, context) else: result.write(evalEntry.content) stopIndex = self.getStopIndex(removeMainElems) if currentIndex < (stopIndex-1): result.write(self.content[currentIndex:stopIndex])
def manageError(self, result, context, errorMessage): '''Manage the encountered error: dump it into the buffer or raise an exception.''' if self.buffer.env.raiseOnError: if not self.buffer.pod: # Add in the error message the line nb where the errors occurs # within the PX. locator = self.buffer.env.parser.locator # The column number may not be given col = locator.getColumnNumber() if col == None: col = '' else: col = ', column %d' % col errorMessage += ' (line %s%s)' % (locator.getLineNumber(), col) # Integrate the traceback (at least, it last lines) errorMessage += '\n' + Traceback.get(4) raise Exception(errorMessage) # Create a temporary buffer to dump the error. If I reuse this buffer to # dump the error (what I did before), and we are, at some depth, in a # for loop, this buffer will contain the error message and not the # content to repeat anymore. It means that this error will also show up # for every subsequent iteration. tempBuffer = self.buffer.clone() PodError.dump(tempBuffer, errorMessage, withinElement=self.elem) tempBuffer.evaluate(result, context)