def myav_new(self,pages=3): base_url=u"http://www.myav.com.tw/market/" try: find_results=[] find_results_url=[] for page in range(1,pages+1): url="http://www.myav.com.tw/market/forumdisplay.php?forumid=44&pagenumber=%s" %page result=urllib.urlopen(url).read() a= unicode(result, 'big5') # work aroud : skip illegal parameter setting in this web page b=a.split(u"硬體新品市場置頂及延長時數收費方式")[1] soup = BeautifulSoup(b) tmp=soup.find_all("td") for i in tmp: if i != None: if i.a != None: if i.a.has_attr("target"): if i.a["target"] == u"newwin": # if i.a.string != None: if i.a.string not in escape_strs: find_results.append(i.a.string) find_results_url.append(base_url+i.a["href"]) self.all_results.append(i.a.string) if self.show: print "=======myav=======" for i in find_results: print i print "==================" return find_results , find_results_url except Exception: print "Exception in myav" print sys.exc_info()[1] print traceback.extract_tb(sys.exc_info()[2]) return find_results , find_results_url
def export_table(host, port, auth_key, db, table, directory, fields, format, error_queue, exit_event): task_queue = multiprocessing.queues.SimpleQueue() if format == "json": filename = directory + "/%s/%s.json" % (db, table) writer = multiprocessing.Process(target=json_writer, args=(filename, fields, task_queue, error_queue)) elif format == "csv": filename = directory + "/%s/%s.csv" % (db, table) writer = multiprocessing.Process(target=csv_writer, args=(filename, fields, task_queue, error_queue)) else: error_queue.put((RuntimeError, RuntimeError("unknown format type: %s" % format), traceback.extract_tb(sys.exc_info()[2]))) return try: conn = r.connect(host, port, auth_key=auth_key) write_table_metadata(conn, db, table, directory) writer.start() read_table_into_queue(conn, db, table, task_queue, exit_event) except (r.RqlClientError, r.RqlDriverError) as ex: error_queue.put((RuntimeError, RuntimeError(ex.message), traceback.extract_tb(sys.exc_info()[2]))) except: ex_type, ex_class, tb = sys.exc_info() error_queue.put((ex_type, ex_class, traceback.extract_tb(tb))) finally: if writer.is_alive(): task_queue.put(("exit", "event")) # Exit is triggered by sending a message with two objects writer.join() else: error_queue.put((RuntimeError, RuntimeError("writer unexpectedly stopped"), traceback.extract_tb(sys.exc_info()[2])))
def export_table(host, port, auth_key, db, table, directory, fields, delimiter, format, error_queue, progress_info, sindex_counter, exit_event): writer = None try: # This will open at least one connection for each rdb_call_wrapper, which is # a little wasteful, but shouldn't be a big performance hit conn_fn = lambda: r.connect(host, port, auth_key=auth_key) table_info = rdb_call_wrapper(conn_fn, "info", write_table_metadata, db, table, directory) sindex_counter.value += len(table_info["indexes"]) task_queue = SimpleQueue() writer = launch_writer(format, directory, db, table, fields, delimiter, task_queue, error_queue) writer.start() rdb_call_wrapper(conn_fn, "table scan", read_table_into_queue, db, table, table_info["primary_key"], task_queue, progress_info, exit_event) except (r.ReqlError, r.ReqlDriverError) as ex: error_queue.put((RuntimeError, RuntimeError(ex.message), traceback.extract_tb(sys.exc_info()[2]))) except: ex_type, ex_class, tb = sys.exc_info() error_queue.put((ex_type, ex_class, traceback.extract_tb(tb))) finally: if writer is not None and writer.is_alive(): task_queue.put(StopIteration()) writer.join()
def read_file(self, path): #读取存储目录日志 if os.path.exists(path): try: history_dicts = {} fp = open(path, 'r') #数据读入缓存 cache = fp.read() for line in cache.splitlines(): #行解析 self.param_check(line, history_dicts) fp.close() #上报内容 ret = self.aggresion(history_dicts) if ret: #上报成功将文件删除 os.remove(path) except: info = sys.exc_info() traceback.extract_tb(info[2]) self.log.error('history error %s %s %s', info[0], info[1], path) return 0 else: self.log.debug('history file is not exit %s', path) return 0 return 1
def exec_code(self, c): try: std_streams = sys.stdout, sys.stderr sys.stdout = StringIO.StringIO() sys.stderr = StringIO.StringIO() try: exec c in self.locals finally: text_out = sys.stdout.getvalue() text_err = sys.stderr.getvalue() sys.stdout, sys.stderr = std_streams self.output.print_to_stdout(text_out) self.output.print_to_stderr(text_err) # Include these lines to actually exit on a sys.exit() call # except SystemExit, value: # raise SystemExit, value except: exc_type, exc_value, exc_traceback = sys.exc_info() l = len(traceback.extract_tb(sys.exc_traceback)) try: 1 / 0 except: m = len(traceback.extract_tb(sys.exc_traceback)) type, value, tb = sys.exc_info() sys.last_type = exc_type sys.last_value = exc_value sys.last_traceback = exc_traceback tblist = traceback.extract_tb(exc_traceback) del tblist[:1] list = traceback.format_list(tblist) if list: list.insert(0, "Traceback (most recent call last):\n") list[len(list) :] = traceback.format_exception_only(exc_type, exc_value) map(self.output.print_to_stderr, list) return
def export_table(host, port, auth_key, db, table, directory, fields, format, error_queue, progress_info, stream_semaphore, exit_event): writer = None try: # This will open at least one connection for each rdb_call_wrapper, which is # a little wasteful, but shouldn't be a big performance hit conn_fn = lambda: r.connect(host, port, auth_key=auth_key) rdb_call_wrapper(conn_fn, "count", get_table_size, db, table, progress_info) table_info = rdb_call_wrapper(conn_fn, "info", write_table_metadata, db, table, directory) with stream_semaphore: task_queue = multiprocessing.queues.SimpleQueue() writer = launch_writer(format, directory, db, table, fields, task_queue, error_queue) writer.start() rdb_call_wrapper(conn_fn, "table scan", read_table_into_queue, db, table, table_info["primary_key"], task_queue, progress_info, exit_event) except (r.RqlError, r.RqlDriverError) as ex: error_queue.put((RuntimeError, RuntimeError(ex.message), traceback.extract_tb(sys.exc_info()[2]))) except: ex_type, ex_class, tb = sys.exc_info() error_queue.put((ex_type, ex_class, traceback.extract_tb(tb))) finally: if writer is not None and writer.is_alive(): task_queue.put(("exit", "event")) # Exit is triggered by sending a message with two objects writer.join() else: error_queue.put((RuntimeError, RuntimeError("writer unexpectedly stopped"), traceback.extract_tb(sys.exc_info()[2])))
def table_reader(options, file_info, task_queue, error_queue, exit_event): try: db = file_info["db"] table = file_info["table"] primary_key = file_info["info"]["primary_key"] conn = r.connect(options["host"], options["port"], auth_key=options["auth_key"]) if table not in r.db(db).table_list().run(conn): r.db(db).table_create(table, primary_key=primary_key).run(conn) if file_info["format"] == "json": json_reader(task_queue, file_info["file"], db, table, primary_key, options["fields"], exit_event) elif file_info["format"] == "csv": csv_reader(task_queue, file_info["file"], db, table, primary_key, options, exit_event) else: raise RuntimeError("unknown file format specified") except (r.RqlClientError, r.RqlDriverError, r.RqlRuntimeError) as ex: error_queue.put((RuntimeError, RuntimeError(ex.message), traceback.extract_tb(sys.exc_info()[2]))) except InterruptedError: pass # Don't save interrupted errors, they are side-effects except: ex_type, ex_class, tb = sys.exc_info() error_queue.put((ex_type, ex_class, traceback.extract_tb(tb), file_info["file"]))
def aggresion(self, *kw): try: area = self.find_view_area(kw[0]) custid = int(kw[1]) domain = kw[2] lasttime = int(kw[3]) iflag = ((lasttime > 61000) and [1] or [0])[0] # stringkey = dumps(key) dir_key = dumps({"area":area,"domain":domain}) dir_value = self.dir_tmp.get(dir_key,None) if None == dir_value: value = {custid:iflag} self.dir_tmp.update({dir_key:value}) else: dir_valuevalue = dir_value.get(custid,None) if None == dir_valuevalue: self.dir_tmp[dir_key].update({custid:iflag}) else: if iflag and not dir_valuevalue: self.dir_tmp[dir_key].update({custid:iflag}) except: info = sys.exc_info() traceback.extract_tb(info[2]) self.log.error('aggresion %s %s', info[0], info[1])
def export_table(host, port, auth_key, db, table, directory, fields, format, error_queue, progress_info, stream_semaphore, exit_event): writer = None try: conn = r.connect(host, port, auth_key=auth_key) table_size = r.db(db).table(table).count().run(conn) progress_info[1].value = table_size progress_info[0].value = 0 write_table_metadata(conn, db, table, directory) with stream_semaphore: task_queue = multiprocessing.queues.SimpleQueue() writer = launch_writer(format, directory, db, table, fields, task_queue, error_queue) writer.start() read_table_into_queue(conn, db, table, task_queue, progress_info, exit_event) except (r.RqlError, r.RqlDriverError) as ex: error_queue.put((RuntimeError, RuntimeError(ex.message), traceback.extract_tb(sys.exc_info()[2]))) except: ex_type, ex_class, tb = sys.exc_info() error_queue.put((ex_type, ex_class, traceback.extract_tb(tb))) finally: if writer is not None and writer.is_alive(): task_queue.put(("exit", "event")) # Exit is triggered by sending a message with two objects writer.join() else: error_queue.put((RuntimeError, RuntimeError("writer unexpectedly stopped"), traceback.extract_tb(sys.exc_info()[2])))
def handle_exception( exc_type, exc_value, exc_traceback ): import sys import os.path import traceback filename, line, dummy, dummy = traceback.extract_tb( exc_traceback ).pop() filename = os.path.basename( filename ) error = "%s: %s" % ( exc_type.__name__, exc_value ) strace = "" stacktrace = traceback.extract_tb( exc_traceback ) while len(stacktrace) > 0: (filename, line, func, txt) = stacktrace.pop() strace += "----\n" strace += "line: %s\n" %txt strace += "func: %s\n" %func strace += "line no.: %d\n" %line strace += "file: %s\n" %filename msg_box = QMessageBox() msg_box.setDetailedText(strace) msg_box.setIcon(QMessageBox.Critical) msg_box.setWindowTitle( "Houston, we have a problem...") msg_box.setText("Whoops. A critical error has occured. This is most likely a bug " "in Qubes Manager.<br><br>" "<b><i>%s</i></b>" % error + "<br/>at line <b>%d</b><br/>of file %s.<br/><br/>" % ( line, filename )) msg_box.exec_()
def convert_exc_to_user_error(exc_info, error_info, msg_args=None, nested_exc_info=None, user_error_class=UserError): """Create a user error from an exception. exc_info is the exception info array returned from sys.exc_info(). The user message, error code, etc are taken from error_info. The exception type and value are stored in the developer message and the stack traceback used to create a context stack. If a nested exception's information is provided through nested_exc_info, this is addeded to the end of the context list. Here is an example of using this function. try: call_something_that_can_throw_an_exception() except UserError: # if this call can throw a user error, # let it propagage up raise except: exc_info = sys.exc_info() raise convert_exc_to_user_error(exc_info, errors[ERR_WSGI_SCRIPT], msg_args={'script':config_mod_wsgi_file}) """ (exc_class, exc_val, exc_tb) = exc_info exc_name = exc_class.__name__ if nested_exc_info != None: context = traceback.extract_tb(exc_tb) + traceback.extract_tb(nested_exc_info[2]) else: context = traceback.extract_tb(exc_tb) return user_error_class(error_info, msg_args, developer_msg="%s: %s" % (exc_name, format_string(exc_val)), context=context)
def test_traceback_stack(self): import sys import traceback def C(): raise Exception def B(): C() def A(): try: B() except: return sys.exc_info()[2] lineno = C.func_code.co_firstlineno tb = A() a = traceback.extract_tb(tb) b = traceback.extract_stack(tb.tb_frame, 1) self.assertEqual(a, [(__file__, 8+lineno, 'A', 'B()'), (__file__, 4+lineno, 'B', 'C()'), (__file__, 1+lineno, 'C', 'raise Exception')]) self.assertEqual([x[2] for x in b], ['A']) # only check that we're in the proper function, the rest does not work properly tb = tb.tb_next a = traceback.extract_tb(tb) b = traceback.extract_stack(tb.tb_frame, 2) self.assertEqual(a, [(__file__, 4+lineno, 'B', 'C()'), (__file__, 1+lineno, 'C', 'raise Exception')]) self.assertEqual([x[2] for x in b], ['A', 'B']) # only check that we're in the proper function, the rest does not work properly tb = tb.tb_next a = traceback.extract_tb(tb) b = traceback.extract_stack(tb.tb_frame, 3) self.assertEqual(a, [(__file__, 1+lineno, 'C', 'raise Exception')]) self.assertEqual([x[2] for x in b], ['A', 'B', 'C']) # only check that we're in the proper function, the rest does not work properly
def __call__(self, request, methodname): handler = self._fmap.get(methodname) if handler is None: return JSONNotFound("No method " + methodname) try: data = request.POST.get("data") if data: try: arguments = self._decoder.decode(data) # First argument is method name. # Allow for keyword arguments as sole argument. if len(arguments) == 2 and isinstance(arguments[1], dict): args = () kwargs = arguments[1] else: # otherwise, use positional arguments. args = tuple(arguments[1:]) kwargs = {} except: # error in parameter conversion ex, val, tb = sys.exc_info() tblist = traceback.extract_tb(tb) request.log_error("JSONDispatcher args: %s (%s)\n" % (ex, val)) return JSONServerError(ex, val, tblist) else: args = () kwargs = {} with GlobalRequest(request): rv = handler(*args, **kwargs) json = self._encoder.encode(rv) return HttpResponse(json, "application/json") except: # all exceptions are sent back to client. ex, val, tb = sys.exc_info() tblist = traceback.extract_tb(tb) del tb request.log_error("JSONDispatcher: %s (%s)\n" % (ex, val)) return JSONServerError(ex, val, tblist)
def errorCheck(exception,sys): """ Creates a human readable error string from a system error object. Input: exception: an exception instance sys: a system error object occuring after a python exception Output: none: see response() """ try: error = sys.exc_info() errorStr = error[1][0] errorLine = traceback.extract_tb(error[2])[0][1] errorType = error[0] errorFile = traceback.extract_tb(error[2])[0][0] errorMod = traceback.extract_tb(error[2])[0][2] try: trace = traceback.extract_tb(error[2])[1] errorOut = str(errorType)+' ERROR IN '+errorFile+'/'+ errorMod+' AT LINE '+str(errorLine)+':: ERROR: '+errorStr + ' | TRACEBACK: ' + trace[0] + ' LINE ' + str(trace[1]) except: errorOut = str(errorType)+' ERROR IN '+errorFile+'/'+ errorMod+' AT LINE '+str(errorLine)+':: ERROR: '+errorStr return response(0, ujson.dumps(errorOut),{}) except: try: #If error check fails, try to return some useful info. return response(0, str(exception),{}) except: return response(0, 'ERROR: ERROR CHECK FAILED ON EXCEPTION.',{})
def execute_plugins(self, network, trigger, *arguments): for plugin in plugin_handler.all_plugins(): try: if plugin.__class__.__dict__.has_key(trigger): # FIXME this is rather ugly, for compatiblity with pynik if plugin.__class__.__dict__[trigger].func_code.co_argcount == len(arguments) + 2: plugin.__class__.__dict__[trigger](plugin, self, *arguments) # Call without network elif plugin.__class__.__dict__[trigger].func_code.co_argcount == len(arguments) + 3: plugin.__class__.__dict__[trigger](plugin, self, *arguments, **{'network': network}) else: raise NotImplementedError("Plugin '%s' argument count missmatch, was %s." % ( plugin, plugin.__class__.__dict__[trigger].func_code.co_argcount)) except: error_handler.output_message("%s %s Plugin '%s' threw exception, exinfo: '%s', traceback: '%s'" % ( datetime.datetime.now().strftime("[%H:%M:%S]"), network, plugin, sys.exc_info(), traceback.extract_tb(sys.exc_info()[2]))) if trigger != "timer_beat": try: self.tell(self.settings.admin_network, self.settings.admin_channel, "%s %s Plugin '%s' threw exception, exinfo: '%s', traceback: '%s'" % ( datetime.datetime.now().strftime("[%H:%M:%S]"), network, plugin, sys.exc_info(), traceback.extract_tb(sys.exc_info()[2])[::-1])) except: error_handler.output_message("%s %s Unable to send exception to admin channel, exinfo: '%s', traceback: '%s'" % ( datetime.datetime.now().strftime("[%H:%M:%S]"), network, sys.exc_info(), traceback.extract_tb(sys.exc_info()[2])))
def test_old_style_exception(self): class OldStyle: # Does not inherit from object def __str__(self): return 'doh!' callback_exc_info = [None] def fn(callback_future): callback_exc_info[0] = callback_future.exception_info() f = Future() f.add_done_callback(fn) try: raise OldStyle() except OldStyle: want_exc_info = sys.exc_info() f.set_exception_info(*want_exc_info[1:]) self.assertEqual(f.exception_info(), want_exc_info[1:]) self.assertEqual(callback_exc_info[0], want_exc_info[1:]) try: f.result() except OldStyle: got_exc_info = sys.exc_info() else: self.fail('OldStyle exception not raised') self.assertEqual(got_exc_info[:2], want_exc_info[:2]) got_tb = traceback.extract_tb(got_exc_info[2]) want_tb = traceback.extract_tb(want_exc_info[2]) self.assertEqual(got_tb[-len(want_tb):], want_tb)
def decorator(*args, **kwargs): if not hasattr(trace, 'local'): trace.local = threading.local() tl = trace.local if not hasattr(tl, 'log_indent'): tl.log_indent = 0 funcname = func.__module__ + "." + func.__name__ # List all positional arguments margs = [str("'%s'" % arg if isinstance(arg, str) else arg) for arg in [("********" if i in redact else arg) for i, arg in enumerate(args)]] # List all keyword arguments margs.extend(["%s=%s" % (key, str("'%s'" % val if isinstance(val, str) else val)) for key, val in [(key, ("********" if key in redact else val)) for key, val in kwargs.items()]]) try: logger.debug("\t" * tl.log_indent + "Entering %s(%s)" % (funcname, ", ".join(margs))) tl.log_indent+=1 retval = func(*args, **kwargs) tl.log_indent-=1 logger.debug("\t" * tl.log_indent + "Leaving %s = %s" % (funcname, retval)) return retval except Exception as e: tl.log_indent -= 1 file = traceback.extract_tb()[-1][0] line = traceback.extract_tb()[-1][1] clsfunc = e.__class__.__name__ logger.error("\t" * tl.log_indent + "Encountered error in %s: %s(%s) [%s:%i]" % (funcname, clsfunc, e.message, file, line)) raise e, None, exc_info()[2]
def test_get_raises_exception_with_full_traceback(self): future = self.future_class() try: raise NameError("foo") except NameError as error: exc_class_set, exc_instance_set, exc_traceback_set = sys.exc_info() future.set_exception() # We could move to another thread at this point try: future.get() except NameError: exc_class_get, exc_instance_get, exc_traceback_get = sys.exc_info() self.assertEqual(exc_class_set, exc_class_get) self.assertEqual(exc_instance_set, exc_instance_get) exc_traceback_list_set = list(reversed(traceback.extract_tb(exc_traceback_set))) exc_traceback_list_get = list(reversed(traceback.extract_tb(exc_traceback_get))) # All frames from the first traceback should be included in the # traceback from the future.get() reraise self.assert_(len(exc_traceback_list_set) < len(exc_traceback_list_get)) for i, frame in enumerate(exc_traceback_list_set): self.assertEquals(frame, exc_traceback_list_get[i])
def decorator(self, *args, **kwargs): if self.ncconnected: self.logger.debug('netconfbuilder: instance %s of class %s is now decorated with netconfbuilder, whee!' % (self, self.__class__)) self.logger.debug("netconfbuilder: Building rpc to send to ".format(self.host)) self.logger.debug("netconfbuilder: method {}".format(func)) self.logger.debug("netconfbuilder: args {}".format(str(args))) self.logger.debug("netconfbuilder: kwargs {}".format(str(kwargs))) rpcmessageid = str(self.rpcmessageid) try: nxosmessage = func(self, *args, **kwargs) except: exc_type, exc_value, exc_traceback = sys.exc_info() stacktrace = traceback.extract_tb(exc_traceback) self.logger.critical("netconfbuilder: Error building the rpc command ") self.logger.debug(sys.exc_info()) self.logger.debug(stacktrace) raise self.rpcmessageid = None logger.debug("netconfbuilder: Constructed nxos message: ".format(str(nxosmessage))) logger.debug("netconfbuilder: Sending message to server") try: return self._send(nxosmessage, rpcmessageid=rpcmessageid) except: exc_type, exc_value, exc_traceback = sys.exc_info() stacktrace = traceback.extract_tb(exc_traceback) logger.critical("netconfbuilder: Error sending xml message") logger.debug(sys.exc_info()) logger.debug(stacktrace) raise else: self.logger.error("netconfbuilder: The ssh connection to {} is currently closed. Please reconnect and try again.".format(self.host)) raise nxos_XML_errors.NotConnectedError( "The ssh connection to {} is currently closed. Please reconnect and try again.".format(self.host))
def traceback_info(self, last_traceback): """ Extracts the informations from the traceback. Add the keys ``filename`` and ``line`` pointing to the root of the exception. Also adds the key ``traceback`` containing a dump of the traceback as a :class:`list`. """ all_tb = [dict(zip(('filename', 'line', 'in', 'call'), x)) for x in traceback.extract_tb(last_traceback)] for i, frame in enumerate(all_tb): if not frame['filename'].startswith(self.napix_path): break else: i = 0 all_tb = all_tb[i:] filename, lineno, function_name, text = traceback.extract_tb( last_traceback)[-1] return { 'traceback': all_tb, 'filename': filename, 'line': lineno, }
def runit(): sys.path.insert(0, os.getcwd()) if not mode == "check": sys.path.insert(0, os.path.dirname(program)) virtualEnvActivate = os.getenv("VIRTUALENV_ACTIVATE", None) if not virtualEnvActivate is None: execfile(virtualEnvActivate, dict(__file__=virtualEnvActivate)) try: if mode == "check": r = runpy.run_module(program) else: r = runpy.run_path(program, run_name="__main__") except SyntaxError as se: ei = sys.exc_info(); traceback.print_exc(); eip = (ei[0], str(ei[1]), traceback.extract_tb(ei[2])) try: eip[2].append(ei[1][1]) except IndexError: eip[2].append((se.filename, se.lineno, None, None)) return (eip) except Exception as e: ei = sys.exc_info(); traceback.print_exc(); eip = (ei[0], str(ei[1]), traceback.extract_tb(ei[2])) return (eip) return ((None, "None", [(program, None, None, None)]))
def run_poller_worker(executor): process = multiprocessing.current_process() log.debug("Poller/executor %s started", process.name) initializer = self.initializer initializer(executor) while executor._worker_shutdown.empty(): work_callable = None with poller_semaphore: while work_callable is None: # make sure that after we wake up we're still relevant if not executor._worker_shutdown.empty(): return try: work_callable = executor._worker.poll_for_activities() except Exception as err: _, _, tb = sys.exc_info() tb_list = traceback.extract_tb(tb) handler = executor._worker.unhandled_exception_handler handler(err, tb_list) try: work_callable() except Exception as err: _, _, tb = sys.exc_info() tb_list = traceback.extract_tb(tb) handler = executor._worker.unhandled_exception_handler handler(err, tb_list)
def process_exception(self,request, exception): f = open('exception.log', 'a') f.write(str(exception) + "\n") f.close() #print traceback.print_stack() # From interpretter get name that caused exception # # Use name to query the database, get newest one, update is_good to False # # Query for benign and malicious input # # Pass these two data sets to the GA # # Handle the results to update filter try: type, value, tb = sys.exc_info() print type print value print traceback.extract_tb(tb) finally: del tb return HttpResponsePermanentRedirect(request.get_full_path().split("?")[0])
def test_derived_traceback(): """Test python exception traceback in class derived from managed base""" class DerivedClass(SubClassTest): __namespace__ = "Python.Test.traceback" def foo(self): print (xyzname) return None import sys,traceback ob = DerivedClass() # direct call try: ob.foo() assert False except: e = sys.exc_info() assert "xyzname" in str(e[1]) location = traceback.extract_tb(e[2])[-1] assert location[2] == "foo" # call through managed code try: FunctionsTest.test_foo(ob) assert False except: e = sys.exc_info() assert "xyzname" in str(e[1]) location = traceback.extract_tb(e[2])[-1] assert location[2] == "foo"
def runPythonFuncWithPickle(infil): data = pickleLoad(infil) pyfil = data['path'] method = data['method'] params = data['params'] map = data['map'] try: _module = loadModule(pyfil) _callable = getattr(_module, method) if callable(_callable): data['result'] = _callable(*params, **map) else: data['except'] = "Unknown method: " + method + "() in Python file '" + pyfil + "'" # https://docs.python.org/2/library/exceptions.html#exceptions.SyntaxError # https://docs.python.org/2/library/traceback.html except SyntaxError: data['except'] = "Fail to compile Python file '" + pyfil + "'" except AttributeError: data['except'] = "Unknown method: '" + method + "()' in Python file '" + pyfil + "'" except: exc_type, exc_value, exc_traceback = sys.exc_info() data['except'] = repr(traceback.extract_tb(exc_traceback)[1:]).encode('unicode-escape').decode().replace("\\\\", "\\").replace("\\\\", "\\") data['except'] = traceback.extract_tb(exc_traceback)[1:] outfil = infil + '.out' pickleDump(data, outfil) return outfil
def __init__(self, *a, **b): super(ApplicationException,self).__init__(*a,**b) self._stacks = [] # save current stack self._stack_init = traceback.extract_stack() self.add_stack(self.__class__.__name__ + ': ' + str(self), self._stack_init) # WARNING this is unreliable! only use if cause passed as argument cause_info = sys.exc_info() #if retain_cause else (None,None,None) # add stacks and labels for cause if 'cause' in b: if isinstance(b['cause'],Application): self._cause = b['cause'] cause_label = 'caused by: ' + self._cause.__class__.__name__ + ': ' + str(self._cause) # if ApplicationException, get stacks from its list if isinstance(self._cause,ApplicationException) and len(self._cause._stacks): first = True for label,stack in self._cause._stacks: if first: self.add_stack(cause_label, stack) first = False else: self.add_stack(label, stack) # otherwise if this is current exception in exc_info, use its stack elif self._cause==cause_info[1] and cause_info[2]: self._stack_cause = traceback.extract_tb(cause_info[2]) self.add_stack(cause_label, self._stack_cause) # cause is not an exception? treat as boolean, use exc_info elif b['cause']: self._cause=cause_info[1] if cause_info[2]: self._stack_cause = traceback.extract_tb(cause_info[2]) self.add_stack(cause_label, self._stack_cause)
def replaceStackTrace(nextHandler, thisFile, type, value, tb): chunks = [[384, 0]] if len(value.args) == 0: resultDict = {} resultDict["dictId"] = u"9D6B6AA1-92FC-453E-8B9A-91D0E02A17B1" resultDict["stackInfo"] = traceback.extract_tb(tb) value.args = value.args +(resultDict, ) else: resultDict = value.args[-1] if type(resultDict) != type({}): resultDict = {} resultDict["dictId"] = u"9D6B6AA1-92FC-453E-8B9A-91D0E02A17B1" resultDict["stackInfo"] = traceback.extract_tb(tb) value.args = value.args +(resultDict, ) if "dictId" not in resultDict or resultDict["dictId"] != u"9D6B6AA1-92FC-453E-8B9A-91D0E02A17B1": resultDict = {} resultDict["dictId"] = u"9D6B6AA1-92FC-453E-8B9A-91D0E02A17B1" resultDict["stackInfo"] = traceback.extract_tb(tb) value.args = value.args +(resultDict, ) resultDict['stackInfo'] = revealLiterate("使用noweb对python进行文学编程.nw", thisFile, chunks, resultDict["stackInfo"]) if '<built-in function excepthook>' == str(nextHandler): print 'Unhandled Exception, trace back:' for stackInfo in resultDict['stackInfo']: print ur' File "' + unicode(stackInfo[0]) + ur'", line ' + unicode(stackInfo[1]) + ur' in ' + unicode(stackInfo[2]) print ur' ' + unicode(stackInfo[3]) value.args = value.args[:-1] print re.compile(r"<type '([^']+)'>").match(str(type)).group(1)+":", value elif None != nextHandler: nextHandler(type, value, tb)
def fogbugzOnFail(self,logfp): print "Creating FogBuz Ticket" cfp=Config(self.__fpath) attempts=0 run=True while run is True and attempts < 3: try: site=FogBugz(cfp.getVar("fogbugz","site","string")) try: site.logon(cfp.getVar("fogbugz","user","string"), cfp.getVar("fogbugz","pass","string")) cfp=Config(self.__fpath) with open(logfp,'rb') as fp: print site.new(sTitle="The Parser "+os.path.join(self.__logbase,self.__execute)+" Failed",ixPersonAssignedTo="Andy",Files={"faillog.txt":fp}) attempts+=1 run=False except Exception,e: print str(e) for frame in traceback.extract_tb(sys.exc_info()[2]): print '\n'.join([str(x) for x in frame]) finally: site.logoff() except Exception,e: print str(e) for frame in traceback.extract_tb(sys.exc_info()[2]): print '\n'.join([str(x) for x in frame])
def update_sql(self, line): sqlconpool_sem.acquire() #获取相关库的链接 sqlclient,table = sqlconpool_dict.get("qita", None) #上传日表 sql = "INSERT INTO "+ table+"(BW_DATE, BW_DOMAIN, BW_IP, BW_BANDWIDTH) VALUES(%s,%s,%s,%s)" if not sqlclient: self.log.error("history work sqlclient is not set %s", table) sqlconpool_sem.release() return 0 else: try: param = line #插入多行 ret = sqlclient.insertMany(sql, param) #事务提交 sqlclient.end('commit') except: #发生错误, 事务回滚 info = sys.exc_info() traceback.extract_tb(info[2]) self.log.error("%s %s %s %s", info[0], info[1], sql, str(param)) sqlconpool_sem.release() return 0 sqlconpool_sem.release() return 1
def _calculate(worksheet, usercode, private_key): worksheet.clear_values() worksheet._console_text = '' worksheet._usercode_error = None context = { 'worksheet': worksheet, 'load_constants': load_constants, 'undefined': undefined, 'CellRange': CellRange, 'DateTime': DateTime, 'FormulaError': FormulaError, '_raise': _raise, 'sys': sys, } context['run_worksheet'] = lambda url, overrides=None: run_worksheet(url, overrides, private_key) context['evaluate_formulae'] = lambda worksheet: evaluate_formulae_in_context(worksheet, context) sys.stdout = MyStdout(worksheet) try: execute_usercode(usercode, context) except Exception, e: if isinstance(e, SyntaxError): error = 'Syntax error at character %d' % (e.offset,) line_no = e.lineno worksheet.add_console_text("%s (line %s)\n" % (error, line_no)) else: error = "%s: %s" % (type(e).__name__, str(e)) tb = sys.exc_info()[2] line_no = traceback.extract_tb(tb)[-1][1] worksheet.add_console_text("%s\n%s\n" % (error, format_traceback(traceback.extract_tb(tb)))) worksheet._usercode_error = {"message": error, "line": line_no}
def read_inputrc(self, inputrcpath=None): if inputrcpath is None: inputrcpath = config_path def bind_key(key, name): if hasattr(self, name): self._bind_key(key, getattr(self, name)) else: print "Warning %s is not a valid function for bind_key" % name def un_bind_key(key): keyinfo = key_text_to_keyinfo(key) if keyinfo in self.key_dispatch: del self.key_dispatch[keyinfo] def bind_exit_key(key): self._bind_exit_key(key) def un_bind_exit_key(key): keyinfo = key_text_to_keyinfo(key) if keyinfo in self.exit_dispatch: del self.exit_dispatch[keyinfo] def sethistoryfilename(filename): self.history_filename = os.path.expanduser(filename) def setbellstyle(mode): self.bell_style = mode def sethistorylength(length): self.history_length = int(length) def show_all_if_ambiguous(mode): self.show_all_if_ambiguous = mode def mark_directories(mode): self.mark_directories = mode def completer_delims(mode): self.completer_delims = mode def debug_output(on, filename="pyreadline_debug_log.txt" ): #Not implemented yet logger.start_log(on, filename) logger.log("STARTING LOG") loc = { "branch": release.branch, "bind_key": bind_key, "bind_exit_key": bind_exit_key, "un_bind_key": un_bind_key, "un_bind_exit_key": un_bind_exit_key, "bell_style": setbellstyle, "mark_directories": mark_directories, "show_all_if_ambiguous": show_all_if_ambiguous, "completer_delims": completer_delims, "debug_output": debug_output, "history_filename": sethistoryfilename, "history_length": sethistorylength } if os.path.isfile(inputrcpath): try: execfile(inputrcpath, loc, loc) except Exception, x: import traceback print >> sys.stderr, "Error reading .pyinputrc" filepath, lineno = traceback.extract_tb( sys.exc_traceback)[1][:2] print >> sys.stderr, "Line: %s in file %s" % (lineno, filepath) print >> sys.stderr, x raise ReadlineError("Error reading .pyinputrc")
def _do_run_file(cli, conf_file): def clear_pipeline(): cli.softnic.pause_all() cli.softnic.reset_all() if not os.path.exists(conf_file): cli.err('Cannot open file "%s"' % conf_file) return xformed = sugar.xform_file(conf_file) new_globals = { '__builtins__': __builtins__, 'softnic': cli.softnic, 'ConfError': ConfError, '__bess_env__': __bess_env__, '__bess_module__': __bess_module__, } class_names = cli.softnic.list_mclasses() # Add the special Port class. TODO: per-driver classes new_globals['Port'] = type('Port', (Port,), {'softnic': cli.softnic, 'choose_arg': _choose_arg}) # Add SoftNIC module classes for name in class_names: if name in new_globals: raise cli.InternalError('Invalid module class name: %s' % name) new_globals[name] = type(name, (Module,), {'softnic': cli.softnic, 'choose_arg': _choose_arg}) code = compile(xformed, conf_file, 'exec') if is_pipeline_empty(cli): cli.softnic.pause_all() else: ret = warn(cli, 'The current pipeline will be reset.', clear_pipeline) if ret is False: return try: exec(code, new_globals) if cli.interactive: cli.fout.write('Done.\n') except cli.softnic.Error: raise except cli.softnic.APIError: raise except ConfError as e: cli.err(e.message) except: cur_frame = inspect.currentframe() cur_func = inspect.getframeinfo(cur_frame).function t, v, tb = sys.exc_info() stack = traceback.extract_tb(tb) while len(stack) > 0 and stack.pop(0)[2] != cur_func: pass cli.err('Unhandled exception in the script (most recent call last)') cli.fout.write(''.join(traceback.format_list(stack))) cli.fout.write(''.join(traceback.format_exception_only(t, v))) finally: cli.softnic.resume_all()
def format_traceback(cls, tb): tb = traceback.extract_tb(tb) for i in tb: yield {'file': i[0], 'line': i[1], 'method': i[2]}
def exception_to_string(exc): exc_type, exc_value, exc_traceback = exc return (str(exc_type) + os.linesep + str(exc_value) + os.linesep + str(traceback.extract_tb(exc_traceback)))
drawer.save(img, 'l_FinalResult') print(f'Total Groups: {len(obvious_groups)} (cnt num: {[len(g["group_cnts"]) for g in obvious_groups]})') print(f'Finished in {time.time() - start} s') print('-----------------------------------------------------------') if evaluate: resize_ratio = resize_height / float(img_height) tp, fp, fn, pr, re, fm, er = evaluate_detection_performance(img, img_name, group_cnts, resize_ratio, evaluate_csv_path) evaluation_csv.append([img_name, tp, fp, fn, pr, re, fm, er]) for i, img_path in enumerate(img_list): try: main(i, img_path) except KeyboardInterrupt: break except Exception as e: error_class = e.__class__.__name__ detail = e.args[0] cl, exc, tb = sys.exc_info() lastCallStack = traceback.extract_tb(tb)[-1] fileName = lastCallStack[0] lineNum = lastCallStack[1] funcName = lastCallStack[2] errMsg = f'File "{fileName}", line {lineNum}, in {funcName}: [{error_class}] {detail}' print(f'[{img_path}] {errMsg}') continue
def _print_exception(t, value, tb, realfile, text, context): error = [] try: exception = traceback.format_exception_only(t, value) error.append('Error executing a python function in %s:\n' % realfile) # Strip 'us' from the stack (better_exec call) tb = tb.tb_next textarray = text.split('\n') linefailed = tb.tb_lineno tbextract = traceback.extract_tb(tb) tbformat = traceback.format_list(tbextract) error.append( "The stack trace of python calls that resulted in this exception/failure was:" ) error.append("File: '%s', lineno: %s, function: %s" % (tbextract[0][0], tbextract[0][1], tbextract[0][2])) error.extend(_print_trace(textarray, linefailed)) # See if this is a function we constructed and has calls back into other functions in # "text". If so, try and improve the context of the error by diving down the trace level = 0 nexttb = tb.tb_next while nexttb is not None and (level + 1) < len(tbextract): error.append("File: '%s', lineno: %s, function: %s" % (tbextract[level + 1][0], tbextract[level + 1][1], tbextract[level + 1][2])) if tbextract[level][0] == tbextract[level + 1][0] and tbextract[ level + 1][2] == tbextract[level][0]: # The code was possibly in the string we compiled ourselves error.extend(_print_trace(textarray, tbextract[level + 1][1])) elif tbextract[level + 1][0].startswith("/"): # The code looks like it might be in a file, try and load it try: with open(tbextract[level + 1][0], "r") as f: text = f.readlines() error.extend( _print_trace(text, tbextract[level + 1][1])) except: error.append(tbformat[level + 1]) elif "d" in context and tbextract[level + 1][2]: # Try and find the code in the datastore based on the functionname d = context["d"] functionname = tbextract[level + 1][2] text = d.getVar(functionname, True) if text: error.extend( _print_trace(text.split('\n'), tbextract[level + 1][1])) else: error.append(tbformat[level + 1]) else: error.append(tbformat[level + 1]) nexttb = tb.tb_next level = level + 1 error.append("Exception: %s" % ''.join(exception)) finally: logger.error("\n".join(error))
di.close() # remove existing filterList and survey sheets try: os.remove('../data/Genre_Labels/filtered_netlocs.txt') survey_path = '../data/Genre_Labels/survey' if os.path.exists(survey_path): shutil.rmtree(survey_path) os.makedirs(survey_path) except Exception: # Get current system exception ex_type, ex_value, ex_traceback = sys.exc_info() # Extract unformatter stack traces as tuples trace_back = traceback.extract_tb(ex_traceback) # Format stacktrace stack_trace = list() for trace in trace_back: stack_trace.append( "File : %s , Line : %d, Func.Name : %s, Message : %s" % (trace[0], trace[1], trace[2], trace[3])) # print("Exception type : %s " % ex_type.__name__) logger.info(ex_value) logger.debug(stack_trace) # prepare filterList prepare_filterList()
def handle_excepthook(exc_type, exc_value, exc_traceback): logging.error("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback)) traceback_details = "\n".join(traceback.extract_tb(exc_traceback).format()) print(f"Uncaught Exception: {traceback_details}")
def _run(self, argv): context = CommandContext(cwd=self.cwd, settings=self.settings, log_manager=self.log_manager, commands=Registrar) if self.populate_context_handler: self.populate_context_handler(context) parser = self.get_argument_parser(context) if not len(argv): # We don't register the usage until here because if it is globally # registered, argparse always prints it. This is not desired when # running with --help. parser.usage = Mach.USAGE parser.print_usage() return 0 try: args = parser.parse_args(argv) except NoCommandError: print(NO_COMMAND_ERROR) return 1 except UnknownCommandError as e: print(UNKNOWN_COMMAND_ERROR % (e.verb, e.command)) return 1 except UnrecognizedArgumentError as e: print(UNRECOGNIZED_ARGUMENT_ERROR % (e.command, ' '.join(e.arguments))) return 1 # Add JSON logging to a file if requested. if args.logfile: self.log_manager.add_json_handler(args.logfile) # Up the logging level if requested. log_level = logging.INFO if args.verbose: log_level = logging.DEBUG self.log_manager.register_structured_logger(logging.getLogger('mach')) write_times = True if args.log_no_times or 'MACH_NO_WRITE_TIMES' in os.environ: write_times = False # Always enable terminal logging. The log manager figures out if we are # actually in a TTY or are a pipe and does the right thing. self.log_manager.add_terminal_logging(level=log_level, write_interval=args.log_interval, write_times=write_times) self.load_settings(args) if not hasattr(args, 'mach_handler'): raise MachError('ArgumentParser result missing mach handler info.') handler = getattr(args, 'mach_handler') cls = handler.cls if handler.pass_context: instance = cls(context) else: instance = cls() if handler.conditions: fail_conditions = [] for c in handler.conditions: if not c(instance): fail_conditions.append(c) if fail_conditions: print(self._condition_failed_message(handler.name, fail_conditions)) return 1 fn = getattr(instance, handler.method) try: result = fn(**vars(args.command_args)) if not result: result = 0 assert isinstance(result, (int, long)) return result except KeyboardInterrupt as ki: raise ki except Exception as e: exc_type, exc_value, exc_tb = sys.exc_info() # The first frame is us and is never used. stack = traceback.extract_tb(exc_tb)[1:] # If we have nothing on the stack, the exception was raised as part # of calling the @Command method itself. This likely means a # mismatch between @CommandArgument and arguments to the method. # e.g. there exists a @CommandArgument without the corresponding # argument on the method. We handle that here until the module # loader grows the ability to validate better. if not len(stack): print(COMMAND_ERROR) self._print_exception(sys.stdout, exc_type, exc_value, traceback.extract_tb(exc_tb)) return 1 # Split the frames into those from the module containing the # command and everything else. command_frames = [] other_frames = [] initial_file = stack[0][0] for frame in stack: if frame[0] == initial_file: command_frames.append(frame) else: other_frames.append(frame) # If the exception was in the module providing the command, it's # likely the bug is in the mach command module, not something else. # If there are other frames, the bug is likely not the mach # command's fault. self._print_error_header(argv, sys.stdout) if len(other_frames): print(MODULE_ERROR) else: print(COMMAND_ERROR) self._print_exception(sys.stdout, exc_type, exc_value, stack) return 1
def _get_traceback(self, exc_tb): # Latest entry originates from this module so it can be removed entries = traceback.extract_tb(exc_tb)[1:] trace = ''.join(traceback.format_list(entries)) return 'Traceback (most recent call last):\n' + trace
return self.DNS.TXT(self.address) elif self.dnsType == 28: self.getRecord() return self.DNS.AAAA(self.address) elif self.dnsType == 15: self.getRecord() return self.DNS.MX(self.preference, self.address) else: self.address="None" return self.DNS.none() except: return self.DNS.none() # pass if __name__ == "__main__": print("FuYuanDNS Server") while 1: udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) udps.bind(('',53)) data, addr = udps.recvfrom(1024) query = FuYuanDNS(data) try: udps.sendto(query.server(), addr) print("Client: ", addr[0], "Port: ", addr[1], "Query Type: ", query.dnsType, "Value: ", query.address) except BaseException as e: exc_type, exc_obj, exc_tb = sys.exc_info() tb = traceback.extract_tb(exc_tb)[-1] print(exc_type, tb[2], tb[1]) pass
def test_strategy(description, insample_args, outsample_args, benchmark_type, benchmark, impact, train_time, test_time, max_time, seed, grader): """Test StrategyLearner. Requires test description, insample args (dict), outsample args (dict), benchmark_type (str), benchmark (float) max time (seconds), points for this test case (int), random seed (long), and a grader fixture. """ points_earned = 0.0 # initialize points for this test case try: incorrect = True if not 'StrategyLearner' in globals(): import importlib m = importlib.import_module('StrategyLearner') globals()['StrategyLearner'] = m outsample_cr_to_beat = None if benchmark_type == 'clean': outsample_cr_to_beat = benchmark def timeoutwrapper_strategylearner(): #Set fixed seed for repetability np.random.seed(seed) random.seed(seed) learner = StrategyLearner.StrategyLearner(verbose=False, impact=impact) tmp = time.time() learner.addEvidence(**insample_args) train_t = time.time() - tmp tmp = time.time() insample_trades_1 = learner.testPolicy(**insample_args) test_t = time.time() - tmp insample_trades_2 = learner.testPolicy(**insample_args) tmp = time.time() outsample_trades = learner.testPolicy(**outsample_args) out_test_t = time.time() - tmp return insample_trades_1, insample_trades_2, outsample_trades, train_t, test_t, out_test_t msgs = [] in_trades_1, in_trades_2, out_trades, train_t, test_t, out_test_t = run_with_timeout( timeoutwrapper_strategylearner, max_time, (), {}) incorrect = False if len(in_trades_1.shape) != 2 or in_trades_1.shape[1] != 1: incorrect = True msgs.append( " First insample trades DF has invalid shape: {}".format( in_trades_1.shape)) elif len(in_trades_2.shape) != 2 or in_trades_2.shape[1] != 1: incorrect = True msgs.append( " Second insample trades DF has invalid shape: {}".format( in_trades_2.shape)) elif len(out_trades.shape) != 2 or out_trades.shape[1] != 1: incorrect = True msgs.append( " Out-of-sample trades DF has invalid shape: {}".format( out_trades.shape)) else: tmp_csum = 0.0 for date, trade in in_trades_1.iterrows(): tmp_csum += trade.iloc[0] if (trade.iloc[0]!=0) and\ (trade.abs().iloc[0]!=MAX_HOLDINGS) and\ (trade.abs().iloc[0]!=2*MAX_HOLDINGS): incorrect = True msgs.append( " illegal trade in first insample DF. abs(trade) not one of ({},{},{}).\n Date {}, Trade {}" .format(0, MAX_HOLDINGS, 2 * MAX_HOLDINGS, date, trade)) break elif abs(tmp_csum) > MAX_HOLDINGS: incorrect = True msgs.append( " holdings more than {} long or short in first insample DF. Date {}, Trade {}" .format(MAX_HOLDINGS, date, trade)) break tmp_csum = 0.0 for date, trade in in_trades_2.iterrows(): tmp_csum += trade.iloc[0] if (trade.iloc[0]!=0) and\ (trade.abs().iloc[0]!=MAX_HOLDINGS) and\ (trade.abs().iloc[0]!=2*MAX_HOLDINGS): incorrect = True msgs.append( " illegal trade in second insample DF. abs(trade) not one of ({},{},{}).\n Date {}, Trade {}" .format(0, MAX_HOLDINGS, 2 * MAX_HOLDINGS, date, trade)) break elif abs(tmp_csum) > MAX_HOLDINGS: incorrect = True msgs.append( " holdings more than {} long or short in second insample DF. Date {}, Trade {}" .format(MAX_HOLDINGS, date, trade)) break tmp_csum = 0.0 for date, trade in out_trades.iterrows(): tmp_csum += trade.iloc[0] if (trade.iloc[0]!=0) and\ (trade.abs().iloc[0]!=MAX_HOLDINGS) and\ (trade.abs().iloc[0]!=2*MAX_HOLDINGS): incorrect = True msgs.append( " illegal trade in out-of-sample DF. abs(trade) not one of ({},{},{}).\n Date {}, Trade {}" .format(0, MAX_HOLDINGS, 2 * MAX_HOLDINGS, date, trade)) break elif abs(tmp_csum) > MAX_HOLDINGS: incorrect = True msgs.append( " holdings more than {} long or short in out-of-sample DF. Date {}, Trade {}" .format(MAX_HOLDINGS, date, trade)) break # if (((in_trades_1.abs()!=0) & (in_trades_1.abs()!=MAX_HOLDINGS) & (in_trades_1.abs()!=2*MAX_HOLDINGS)).any().any() or\ # ((in_trades_2.abs()!=0) & (in_trades_2.abs()!=MAX_HOLDINGS) & (in_trades_2.abs()!=2*MAX_HOLDINGS)).any().any() or\ # ((out_trades.abs()!=0) & (out_trades.abs()!=MAX_HOLDINGS) & (out_trades.abs()!=2*MAX_HOLDINGS)).any().any()): # incorrect = True # msgs.append(" illegal trade. abs(trades) not one of ({},{},{})".format(0,MAX_HOLDINGS,2*MAX_HOLDINGS)) # if ((in_trades_1.cumsum().abs()>MAX_HOLDINGS).any()[0]) or ((in_trades_2.cumsum().abs()>MAX_HOLDINGS).any()[0]) or ((out_trades.cumsum().abs()>MAX_HOLDINGS).any()[0]): # incorrect = True # msgs.append(" holdings more than {} long or short".format(MAX_HOLDINGS)) if not (incorrect): if train_t > train_time: incorrect = True msgs.append( " addEvidence() took {} seconds, max allowed {}".format( train_t, train_time)) else: points_earned += 1.0 if test_t > test_time: incorrect = True msgs.append( " testPolicy() took {} seconds, max allowed {}".format( test_t, test_time)) else: points_earned += 2.0 if not ((in_trades_1 == in_trades_2).all()[0]): incorrect = True mismatches = in_trades_1.join(in_trades_2, how='outer', lsuffix='1', rsuffix='2') mismatches = mismatches[ mismatches.iloc[:, 0] != mismatches.iloc[:, 1]] msgs.append( " consecutive calls to testPolicy() with same input did not produce same output:" ) msgs.append(" Mismatched trades:\n {}".format(mismatches)) else: points_earned += 2.0 student_insample_cr = evalPolicy2(insample_args['symbol'], in_trades_1, insample_args['sv'], insample_args['sd'], insample_args['ed'], market_impact=impact, commission_cost=0.0) student_outsample_cr = evalPolicy2(outsample_args['symbol'], out_trades, outsample_args['sv'], outsample_args['sd'], outsample_args['ed'], market_impact=impact, commission_cost=0.0) if student_insample_cr <= benchmark: incorrect = True msgs.append( " in-sample return ({}) did not beat benchmark ({})". format(student_insample_cr, benchmark)) else: points_earned += 5.0 if outsample_cr_to_beat is None: if out_test_t > test_time: incorrect = True msgs.append( " out-sample took {} seconds, max of {}".format( out_test_t, test_time)) else: points_earned += 5.0 else: if student_outsample_cr < outsample_cr_to_beat: incorrect = True msgs.append( " out-sample return ({}) did not beat benchmark ({})". format(student_outsample_cr, outsample_cr_to_beat)) else: points_earned += 5.0 if incorrect: inputs_str = " insample_args: {}\n" \ " outsample_args: {}\n" \ " benchmark_type: {}\n" \ " benchmark: {}\n" \ " train_time: {}\n" \ " test_time: {}\n" \ " max_time: {}\n" \ " seed: {}\n".format(insample_args, outsample_args, benchmark_type, benchmark, train_time, test_time, max_time,seed) raise IncorrectOutput( "Test failed on one or more output criteria.\n Inputs:\n{}\n Failures:\n{}" .format(inputs_str, "\n".join(msgs))) except Exception as e: # Test result: failed msg = "Test case description: {}\n".format(description) # Generate a filtered stacktrace, only showing erroneous lines in student file(s) tb_list = tb.extract_tb(sys.exc_info()[2]) for i in range(len(tb_list)): row = tb_list[i] tb_list[i] = (os.path.basename(row[0]), row[1], row[2], row[3] ) # show only filename instead of long absolute path # tb_list = [row for row in tb_list if row[0] in ['QLearner.py','StrategyLearner.py']] if tb_list: msg += "Traceback:\n" msg += ''.join(tb.format_list(tb_list)) # contains newlines elif 'grading_traceback' in dir(e): msg += "Traceback:\n" msg += ''.join(tb.format_list(e.grading_traceback)) msg += "{}: {}".format(e.__class__.__name__, str(e)) # Report failure result to grader, with stacktrace grader.add_result( GradeResult(outcome='failed', points=points_earned, msg=msg)) raise else: # Test result: passed (no exceptions) grader.add_result( GradeResult(outcome='passed', points=points_earned, msg=None))
tr = TrumpReport("Test Report") for fakesym in list('ABCDE'): sr = SymbolReport(fakesym) for fakefeed in list('123'): fr = FeedReport(fakefeed) rp = ReportPoint("fetched feed", "somecheck", True) fr.add_reportpoint(rp) rp = ReportPoint("fetched feed", "othercheck", 50) fr.add_reportpoint(rp) try: a = b + "a problem" except: typ, val, tback = sys.exc_info() tbextract = trcbm.extract_tb(tback) hp = HandlePointReport("a+b", tbextract) fr.add_handlepoint(hp) rp = ReportPoint("add a and b", "goodcheck", "ab") fr.add_reportpoint(rp) try: a = 4 + "a problem" except: typ, val, tback = sys.exc_info() tbextract = trcbm.extract_tb(tback) hp = HandlePointReport("4th problem", tbextract) fr.add_handlepoint(hp) sr.add_feedreport(fr)
def report_internal_error( err: Exception, file: Optional[str], line: int, errors: Errors, options: Options, stdout: Optional[TextIO] = None, stderr: Optional[TextIO] = None, ) -> None: """Report internal error and exit. This optionally starts pdb or shows a traceback. """ stdout = (stdout or sys.stdout) stderr = (stderr or sys.stderr) # Dump out errors so far, they often provide a clue. # But catch unexpected errors rendering them. try: for msg in errors.new_messages(): print(msg) except Exception as e: print("Failed to dump errors:", repr(e), file=stderr) # Compute file:line prefix for official-looking error messages. if file: if line: prefix = '{}:{}: '.format(file, line) else: prefix = '{}: '.format(file) else: prefix = '' # Print "INTERNAL ERROR" message. print('{}error: INTERNAL ERROR --'.format(prefix), 'Please try using mypy master on Github:\n' 'https://mypy.readthedocs.io/en/stable/common_issues.html' '#using-a-development-mypy-build', file=stderr) if options.show_traceback: print('Please report a bug at https://github.com/python/mypy/issues', file=stderr) else: print( 'If this issue continues with mypy master, ' 'please report a bug at https://github.com/python/mypy/issues', file=stderr) print('version: {}'.format(mypy_version), file=stderr) # If requested, drop into pdb. This overrides show_tb. if options.pdb: print('Dropping into pdb', file=stderr) import pdb pdb.post_mortem(sys.exc_info()[2]) # If requested, print traceback, else print note explaining how to get one. if options.raise_exceptions: raise err if not options.show_traceback: if not options.pdb: print('{}: note: please use --show-traceback to print a traceback ' 'when reporting a bug'.format(prefix), file=stderr) else: tb = traceback.extract_stack()[:-2] tb2 = traceback.extract_tb(sys.exc_info()[2]) print('Traceback (most recent call last):') for s in traceback.format_list(tb + tb2): print(s.rstrip('\n')) print('{}: {}'.format(type(err).__name__, err), file=stdout) print('{}: note: use --pdb to drop into pdb'.format(prefix), file=stderr) # Exit. The caller has nothing more to say. # We use exit code 2 to signal that this is no ordinary error. raise SystemExit(2)
async def pulpit_control_wrapper(self, *args, **kwargs): debug('add_pulpit_control: starting') # The 'msg' keyword is used. try: msg = kwargs['msg'] except KeyError: msg = None try: debug('add_pulpit_control: starting wait_for_pulpit') print('add_pulpit_control: kwargs: {}'.format(kwargs)) print('add_pulpit_control: self: {}'.format(self)) print('add_pulpit_control: msg: {}'.format(msg)) await self.wait_for_pulpit(self, msg=msg) debug('add_pulpit_control: running frame_function') ret_val = await frame_function(self, *args, **kwargs) except ExitCommandGracefully as e: debug('Trying to exit command gracefully: {}'.format(e.msg)) self.flag_error = True self.error_message = e.msg except: debug( "add_pulpit_control: got an error that cannot be handled: ", sys.exc_info()[0]) exc_type, exc_value, exc_traceback = sys.exc_info() exc_type, exc_value, exc_traceback = sys.exc_info() print("*** print_tb:") traceback.print_tb(exc_traceback, limit=2, file=sys.stdout) print("*** print_exception:") # exc_type below is ignored on 3.5 and later traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout) print("*** print_exc:") traceback.print_exc(limit=2, file=sys.stdout) print("*** format_exc, first and last line:") formatted_lines = traceback.format_exc().splitlines() print(formatted_lines[0]) print(formatted_lines[-1]) print("*** format_exception:") # exc_type below is ignored on 3.5 and later print( repr( traceback.format_exception(exc_type, exc_value, exc_traceback))) print("*** extract_tb:") print(repr(traceback.extract_tb(exc_traceback))) print("*** format_tb:") print(repr(traceback.format_tb(exc_traceback))) print("*** tb_lineno:", exc_traceback.tb_lineno) raise else: return ret_val finally: debug('add_pulpit_control: stepping off pulpit') self.step_off_pulpit(self, msg=msg) print('add_pulpit_control: EVERYTHING IS DONE')
def main(): pa = argparse.ArgumentParser( usage="%(prog)s [options] alignment_file gff_file", description="This script takes one alignment file in SAM/BAM " + "format and a feature file in GFF format and calculates for each feature " + "the number of reads mapping to it, accounting for barcodes. See " + "http://htseq.readthedocs.io/en/master/count.html for details.", epilog="Written by Simon Anders ([email protected]), " + "European Molecular Biology Laboratory (EMBL) and Fabio Zanini " + "([email protected]), UNSW Sydney. (c) 2010-2020. " + "Released under the terms of the GNU General Public License v3. " + "Part of the 'HTSeq' framework, version %s." % HTSeq.__version__) pa.add_argument( "samfilename", type=str, help="Path to the SAM/BAM file containing the barcoded, mapped " + "reads. If '-' is selected, read from standard input") pa.add_argument("featuresfilename", type=str, help="Path to the GTF file containing the features") pa.add_argument( "-f", "--format", dest="samtype", choices=("sam", "bam", "auto"), default="auto", help="Type of <alignment_file> data. DEPRECATED: " + "file format is detected automatically. This option is ignored.") pa.add_argument( "-r", "--order", dest="order", choices=("pos", "name"), default="name", help= "'pos' or 'name'. Sorting order of <alignment_file> (default: name). Paired-end sequencing " + "data must be sorted either by position or by read name, and the sorting order " + "must be specified. Ignored for single-end data.") pa.add_argument( "--max-reads-in-buffer", dest="max_buffer_size", type=int, default=30000000, help="When <alignment_file> is paired end sorted by position, " + "allow only so many reads to stay in memory until the mates are " + "found (raising this number will use more memory). Has no effect " + "for single end or paired end sorted by name") pa.add_argument( "-s", "--stranded", dest="stranded", choices=("yes", "no", "reverse"), default="yes", help="Whether the data is from a strand-specific assay. Specify 'yes', " + "'no', or 'reverse' (default: yes). " + "'reverse' means 'yes' with reversed strand interpretation") pa.add_argument( "-a", "--minaqual", type=int, dest="minaqual", default=10, help="Skip all reads with MAPQ alignment quality lower than the given " + "minimum value (default: 10). MAPQ is the 5th column of a SAM/BAM " + "file and its usage depends on the software used to map the reads.") pa.add_argument( "-t", "--type", type=str, dest="featuretype", default="exon", help="Feature type (3rd column in GTF file) to be used, " + "all features of other type are ignored (default, suitable for Ensembl " + "GTF files: exon)") pa.add_argument("-i", "--idattr", type=str, dest="idattr", default="gene_id", help="GTF attribute to be used as feature ID (default, " + "suitable for Ensembl GTF files: gene_id)") pa.add_argument( "--additional-attr", type=str, action='append', default=[], help="Additional feature attributes (default: none, " + "suitable for Ensembl GTF files: gene_name). Use multiple times " + "for each different attribute") pa.add_argument( "-m", "--mode", dest="mode", choices=("union", "intersection-strict", "intersection-nonempty"), default="union", help="Mode to handle reads overlapping more than one feature " + "(choices: union, intersection-strict, intersection-nonempty; default: union)" ) pa.add_argument( "--nonunique", dest="nonunique", type=str, choices=("none", "all"), default="none", help="Whether to score reads that are not uniquely aligned " + "or ambiguously assigned to features") pa.add_argument("--secondary-alignments", dest="secondary_alignments", type=str, choices=("score", "ignore"), default="ignore", help="Whether to score secondary alignments (0x100 flag)") pa.add_argument( "--supplementary-alignments", dest="supplementary_alignments", type=str, choices=("score", "ignore"), default="ignore", help="Whether to score supplementary alignments (0x800 flag)") pa.add_argument( "-o", "--samout", type=str, dest="samout", default=None, help="Write out all SAM alignment records into a" + "SAM/BAM file, annotating each line " + "with its feature assignment (as an optional field with tag 'XF')" + ". See the -p option to use BAM instead of SAM.") pa.add_argument("-p", '--samout-format', type=str, dest='samout_format', choices=('SAM', 'BAM', 'sam', 'bam'), default='SAM', help="Format to use with the --samout option.") pa.add_argument("-d", '--delimiter', type=str, dest='output_delimiter', default='\t', help="Column delimiter in output (default: TAB).") pa.add_argument( "-c", '--counts_output', type=str, dest='output_filename', default='', help="TSV/CSV filename to output the counts to instead of stdout.") pa.add_argument( '--cell-barcode', type=str, dest='cb_tag', default='CB', help='BAM tag used for the cell barcode (default compatible ' + 'with 10X Genomics Chromium is CB).', ) pa.add_argument( '--UMI', type=str, dest='ub_tag', default='UB', help='BAM tag used for the unique molecular identifier, also ' + ' known as molecular barcode (default compatible ' + 'with 10X Genomics Chromium is UB).', ) pa.add_argument("-q", "--quiet", action="store_true", dest="quiet", help="Suppress progress report") # and warnings" ) pa.add_argument("--version", action="store_true", help='Show software version and exit') args = pa.parse_args() if args.version: print(HTSeq.__version__) sys.exit() warnings.showwarning = my_showwarning try: count_reads_in_features( args.samfilename, args.featuresfilename, args.order, args.max_buffer_size, args.stranded, args.mode, args.nonunique, args.secondary_alignments, args.supplementary_alignments, args.featuretype, args.idattr, args.additional_attr, args.quiet, args.minaqual, args.samout, args.samout_format, args.output_delimiter, args.output_filename, args.cb_tag, args.ub_tag, ) except: sys.stderr.write(" %s\n" % str(sys.exc_info()[1])) sys.stderr.write( " [Exception type: %s, raised in %s:%d]\n" % (sys.exc_info()[1].__class__.__name__, os.path.basename(traceback.extract_tb(sys.exc_info()[2])[-1][0]), traceback.extract_tb(sys.exc_info()[2])[-1][1])) sys.exit(1)
def parse(self, cloudformation_yml, parameter_values_json=None): if self.debug: print( "\n\n#######################################################") print('CfnParser - parse') print('Beginning to parse cloudformation template') print('cloudformation_yml type: ' + str(type(cloudformation_yml))) print( "##########################################################\n\n" ) try: self.pre_validate_model(cloudformation_yml) except ParserError as e: tb = sys.exc_info()[-1] if self.debug: print('tb: ' + str(tb) + lineno()) stk = traceback.extract_tb(tb, 1) if self.debug: print('stk: ' + str(stk) + lineno()) fname = stk[0][2] if self.debug: print('The failing function was', fname, lineno()) raise if self.debug: print("\n\n#########################################") print('cloudformation template pre_validated' + lineno()) print('Prevalidating cloudformation template') print("#############################################\n\n") # Transform raw resources in template as performed by # transforms transformer = TransformRegistry.TransformRegistry(debug=self.debug) cloudformation_yml = transformer.perform_transforms(cloudformation_yml) if self.debug: print("\n\n#################################################") print('Done prevalidating cloudformation template') print('cloudformation_yml type: ' + str(type(cloudformation_yml)) + lineno()) print('Begin to validate referenses' + lineno()) print("#####################################################\n\n") self.validate_references(cloudformation_yml) if self.debug: print("\n\n##########################################") print('Begin transform_hash_into_model elements') print('Creating the cfn_model objects') print("#############################################\n") cfn_model = CfnModel.CfnModel(debug=self.debug) cfn_model.raw_model = cloudformation_yml # pass 1: wire properties into ModelElement objects cfn_model = self.transform_hash_into_model_elements( cloudformation_yml, cfn_model) if self.debug: print("\n\n#################################################") print("Iterate through each resource in the model") print("######################################################\n") for r in cfn_model.resources: print("############### RESOURCE INFO ###################") print(r) print('resource_type: ' + str(cfn_model.resources[r].resource_type) + lineno()) print('logical_resource_id: ' + str(cfn_model.resources[r].logical_resource_id) + lineno()) print('metadata: ' + str(cfn_model.resources[r].metadata) + lineno()) print(str(vars(cfn_model.resources[r])) + lineno()) print( str(cfn_model.resources[r].raw_model.raw_model) + lineno()) print("##################################################\n") print("\n##################################################") print('properties wired into model element objects' + lineno()) print("##################################################\n") if self.debug: print("\n##################################################") print('Transforming hash into parameters' + lineno()) print("##################################################\n") # Transform cloudformation parameters into parameters object cfn_model = self.transform_hash_into_parameters( cloudformation_yml, cfn_model) if self.debug: print("\n##################################################") print('Done transforming hash into parameters' + lineno()) print('Beginning post process resource model elements' + lineno()) print("##################################################\n") # pass 2: tie together separate resources only where necessary to make life easier for rule logic cfn_model = self.post_process_resource_model_elements(cfn_model) if self.debug: print("\n##################################################") print('Done Post processing resource model elements' + lineno()) print('Beginning to apply parameter values to model' + lineno()) print("##################################################\n") cfn_model = self.apply_parameter_values(cfn_model, parameter_values_json) if self.debug: print("\n##################################################") print('Done applying parameter values to model' + lineno()) print("##################################################\n") return cfn_model
util.clean_tempfiles(opt, tmp_init=False) if __name__ == '__main__': if opt.debug: main() sys.exit(0) try: main() print('Finished!') except Exception as ex: print('--------------------ERROR--------------------') print('--------------Environment--------------') print('DeepMosaics: 0.5.1') print('Python:', sys.version) import torch print('Pytorch:', torch.__version__) import cv2 print('OpenCV:', cv2.__version__) import platform print('Platform:', platform.platform()) print('--------------BUG--------------') ex_type, ex_val, ex_stack = sys.exc_info() print('Error Type:', ex_type) print(ex_val) for stack in traceback.extract_tb(ex_stack): print(stack) input('Please press any key to exit.\n') #util.clean_tempfiles(tmp_init = False) sys.exit(0)
def state_check(state, next_state, action): global fail_flag global stop_count try: assert isinstance(state, MansionState) # for e in state.ElevatorStates: for i in range(len(state.ElevatorStates)): ele = copy.deepcopy(state.ElevatorStates[i]) assert isinstance(ele, ElevatorState) next_ele = copy.deepcopy(next_state.ElevatorStates[i]) assert isinstance(next_ele, ElevatorState) act = copy.deepcopy(action[i]) assert isinstance(act, ElevatorAction) # type ele_Floor = ele.Floor ele_Velocity = ele.Velocity ele_LoadWeight = ele.LoadWeight next_ele_Floor = next_ele.Floor next_ele_Velocity = next_ele.Velocity next_ele_LoadWeight = next_ele.LoadWeight assert isinstance(ele_Floor, float) assert isinstance(ele.MaximumFloor, int) assert isinstance(ele_Velocity, float) assert isinstance(ele.MaximumSpeed, float) assert isinstance(ele.Direction, int) assert isinstance(ele.CurrentDispatchTarget, int) assert isinstance(ele.DispatchTargetDirection, int) assert isinstance(ele_LoadWeight, float) assert isinstance(ele.MaximumLoad, int) assert isinstance(ele.OverloadedAlarm, float) assert isinstance(ele.DoorIsOpening, bool) assert isinstance(ele.DoorIsClosing, bool) assert isinstance(ele.ReservedTargetFloors, list) # change ele_Floor = round(ele_Floor, 2) ele_Velocity = round(ele_Velocity, 2) ele_LoadWeight = round(ele_LoadWeight, 2) next_ele_Velocity = round(next_ele_Velocity, 2) ele_Velocity = round(ele_Velocity, 2) next_ele_LoadWeight = round(next_ele_LoadWeight, 2) # range assert ele_Floor > 0 and ele_Floor <= ele.MaximumFloor assert ele_Velocity >= ( 0 - ele.MaximumSpeed) and ele_Velocity <= ele.MaximumSpeed assert ele.Direction in [-1, 0, 1] assert ele.CurrentDispatchTarget >= -1 and ele.CurrentDispatchTarget <= ele.MaximumFloor assert ele.DispatchTargetDirection in [-1, 1] assert ele_LoadWeight >= 0 and ele_LoadWeight <= ele.MaximumLoad assert ele.OverloadedAlarm >= 0 and ele.OverloadedAlarm <= 2.0 assert ele.DoorState >= 0 and ele.DoorState <= 1 assert ele.DoorIsClosing in [True, False] assert ele.DoorIsOpening in [True, False] for t in ele.ReservedTargetFloors: assert t >= 1 and t <= ele.MaximumFloor #relation if (ele_Velocity == 0 and ele.Direction != 0): assert (ele_Floor % 1) == 0 or \ (ele_Floor % 1 != 0 and next_ele.Direction == 0) if (round(ele_Floor, 1) % 1 != 0 and ele.Direction != 0): assert ele_Velocity != 0 or next_ele_Velocity != 0 or\ next_ele.Direction == 0 or ele_Floor == ele.CurrentDispatchTarget assert (ele.DoorIsClosing and ele.DoorIsOpening) == False if (ele.DoorState < 1 and ele.DoorState > 0): assert (ele.DoorIsClosing or ele.DoorIsOpening) == True assert ele_Floor % 1 == 0 # if(ele.DoorState in [0.0, 1.0]): # assert (ele.DoorIsClosing or ele.DoorIsOpening) == False # ignore if (ele.DoorState in [0.0, 1.0]): if ((ele.DoorIsClosing or ele.DoorIsOpening) == True): if (next_ele.DoorState in [0.0, 1.0]): assert (next_ele.DoorIsClosing or next_ele.DoorIsOpening) == False if ((ele_Floor % 1 != 0) or ((ele.DoorIsClosing and ele.DoorIsOpening) == True)): assert ele.DoorState == 0.0 assert ele.DoorIsClosing == False or next_ele.DoorIsClosing == False assert ele.DoorIsOpening == False if (ele_Velocity != 0.0 and ele.Direction != 0): assert ele.DoorState == 0.0 if (ele_Velocity != 0.0 and len(ele.ReservedTargetFloors) > 0): assert ele_LoadWeight > 0 if (ele_Velocity != 0.0 and ele_LoadWeight > 0): assert len(ele.ReservedTargetFloors) > 0 if (next_ele.OverloadedAlarm > 0 and ele.OverloadedAlarm == 0): assert next_ele_LoadWeight >= ele.MaximumLoad - 200 if (len(ele.ReservedTargetFloors) != 0): assert ele_LoadWeight >= 20 # dynamic check delta_Floor = round(next_ele_Floor - ele_Floor, 2) assert delta_Floor * next_ele_Velocity >= 0 or delta_Floor * ele_Velocity >= 0 target_list = ele.ReservedTargetFloors[:] # if(ele.CurrentDispatchTarget != 0): # target_list.append(ele.CurrentDispatchTarget) if (delta_Floor > 0 and ele_Velocity != 0.0 and ele_Floor % 1 != 0): # going up min_target = min(target_list) if len( target_list) > 0 else ele.MaximumFloor + 1 assert ele_Floor <= min_target assert next_ele_Velocity > 0 or ele_Velocity > 0 or ele.Direction == 0 if (delta_Floor < 0 and ele_Velocity != 0.0 and ele_Floor % 1 != 0): # going down max_target = max(target_list) if len(target_list) > 0 else 0 assert ele_Floor >= max_target assert next_ele_Velocity < 0 or ele_Velocity < 0 or ele.Direction == 0 # if(delta_Floor == 0): # assert next_ele_Velocity == 0 or ele_Velocity * next_ele_Velocity <= 0 if ((next_ele_LoadWeight - ele_LoadWeight) > 0.01): assert ele.DoorState > 0 or ele.DoorIsOpening or ele.DoorIsClosing if ((next_ele_LoadWeight - ele_LoadWeight) < -0.01): assert ele.DoorState > 0 or ele.DoorIsOpening or ele.DoorIsClosing if (ele.OverloadedAlarm < next_ele.OverloadedAlarm): assert ele.DoorState > 0 or ele.DoorIsOpening or ele.DoorIsClosing assert len(next_ele.ReservedTargetFloors) == len( ele.ReservedTargetFloors) #????? # assert next_ele_LoadWeight >= ele_LoadWeight # not right if (len(next_ele.ReservedTargetFloors) > len( ele.ReservedTargetFloors)): assert (next_ele_LoadWeight - ele_LoadWeight) >= 0 #!!! assert ele.DoorState > 0 or ele.DoorIsOpening or ele.DoorIsClosing if (len(next_ele.ReservedTargetFloors) < len( ele.ReservedTargetFloors)): # assert (next_ele_LoadWeight - ele_LoadWeight) < 0 # not right assert ele.DoorState > 0 or ele.DoorIsOpening or ele.DoorIsClosing # if(ele.OverloadedAlarm > 0): # assert ele.ReservedTargetFloors == next_ele.ReservedTargetFloors # assert ele_LoadWeight == next_ele_LoadWeight # assert ele.DoorState > 0 or ele.DoorIsOpening or ele.DoorIsClosing if (fail_flag): stop_count -= 1 if (stop_count == 0): print( '\n\nSome error appear before several steps, please check\n\n' ) exit(1) except AssertionError: _, _, tb = sys.exc_info() traceback.print_tb(tb) # Fixed format tb_info = traceback.extract_tb(tb) filename, line, func, text = tb_info[-1] print('An error occurred on line {} in statement {}'.format( line, text)) print('\n========================== ele num: ', i) print('\nlast: ', ele) print('\nthis: ', next_ele) print('\n========================== please check\n\n') fail_flag = True
def extract(**kwargs): return traceback.extract_tb(tb, **kwargs)
try: a[2]() except: pass covNew = covertool.getCoverage() if len(covNew) > len(cov): elapsed = time.time() - start print "FOUND NEW BRANCH", elapsed, len(covNew) try: t.check() except: _, _, tb = sys.exc_info() traceback.print_tb(tb) tbInfo = traceback.extract_tb(tb) filename, line, func, text = tbInfo[-1] print "TEST:" for step in test: print step print "EXITING DUE TO FAILED TEST" sys.exit(1) elapsed = time.time() - start if elapsed > config.timeout: print "EXITING DUE TO TIMEOUT" print ntests, "EXECUTED" sys.exit(2) break print ntests, "EXECUTED"
# Use globals as our "locals" dictionary so that # something that tries to import __main__ (e.g. the unittest # module) will see the right things. exec f.read() in globals(), globals() except SystemExit, e: logging.basicConfig() gen_log.info("Script exited with status %s", e.code) except Exception, e: logging.basicConfig() gen_log.warning("Script exited with uncaught exception", exc_info=True) # If an exception occurred at import time, the file with the error # never made it into sys.modules and so we won't know to watch it. # Just to make sure we've covered everything, walk the stack trace # from the exception and watch every file. for (filename, lineno, name, line) in traceback.extract_tb(sys.exc_info()[2]): watch(filename) if isinstance(e, SyntaxError): # SyntaxErrors are special: their innermost stack frame is fake # so extract_tb won't see it and we have to get the filename # from the exception object. watch(e.filename) else: logging.basicConfig() gen_log.info("Script exited normally") # restore sys.argv so subsequent executions will include autoreload sys.argv = original_argv if mode == 'module': # runpy did a fake import of the module as __main__, but now it's # no longer in sys.modules. Figure out where it is and watch it.
def get_all_organizations(test_class_instance, url): """ get_all_organizations() function returns all of the created organizations. :param test_class_instance: instance of the test class :param url: gateway url,for example: http://localhost:9999 :return: status_code, list of organization's dictionaries: {u'id': u'02a51e4f52a22000', u'links': { u'users': u'/api/v2/orgs/02a51e4f52a22000/users', u'buckets': u'/api/v2/buckets?org=gxfrp', u'tasks': u'/api/v2/tasks?org=gxfrp', u'dashboards': u'/api/v2/dashboards?org=gxfrp', u'self': u'/api/v2/orgs/02a51e4f52a22000'}, u'name': u'gxfrp'} """ test_class_instance.mylog.info( 'org_util.get_all_organizations() function is being called') test_class_instance.mylog.info( '---------------------------------------------------------') test_class_instance.mylog.info('') list_of_organizations = [] response = test_class_instance.rl.get(base_url=url, path=ORG_URL) try: # response.json() returns dictionary: # { # u'orgs': # [{u'id': u'02a51e4f52a22000', # u'links': { # u'users': u'/api/v2/orgs/02a51e4f52a22000/users', # u'buckets': u'/api/v2/buckets?org=gxfrp', # u'tasks': u'/api/v2/tasks?org=gxfrp', # u'dashboards': u'/api/v2/dashboards?org=gxfrp', # u'self': u'/api/v2/orgs/02a51e4f52a22000'}, # u'name': u'gxfrp'} # ], # u'links': {u'self': u'/api/v2/orgs'} # } list_of_organizations = response.json()['orgs'] if type(list_of_organizations) == list: test_class_instance.mylog.info( 'org_util.get_all_organizations() LIST OF ORGANIZATIONS=' + str(list_of_organizations)) else: test_class_instance.mylog.info( 'gateway_util.get_all_organizations() ERROR=' + response.headers['X-Influx-Error']) except: test_class_instance.mylog.info( 'org_util.get_all_organizations() Exception:') clt_error_type, clt_error_message, clt_error_traceback = sys.exc_info() test_class_instance.mylog.info('litmus_util.execCmd:' + str(clt_error_message)) test_class_instance.mylog.info( 'litmus_util.execCmd:' + str(traceback.extract_tb(clt_error_traceback))) test_class_instance.mylog.info('litmus_util.execCmd:' + str(clt_error_message)) test_class_instance.mylog.info( 'org_util.get_all_organizations() function is done') test_class_instance.mylog.info('') return response.status_code, list_of_organizations
def report_exception(self, msg, notify_user=True, level="error", error_frame=None): """ Report details of an exception to the user. This should only be called within an except: block Details of the exception are reported eg filename, line number and exception type. Because stack trace information outside of py3status or it's modules is not helpful in actually finding and fixing the error, we try to locate the first place that the exception affected our code. Alternatively if the error occurs in a module via a Py3 call that catches and reports the error then we receive an error_frame and use that as the source of the error. NOTE: msg should not end in a '.' for consistency. """ # Get list of paths that our stack trace should be found in. py3_paths = [Path(__file__).resolve()] + self.config["include_paths"] traceback = None try: # We need to make sure to delete tb even if things go wrong. exc_type, exc_obj, tb = sys.exc_info() stack = extract_tb(tb) error_str = f"{exc_type.__name__}: {exc_obj}\n" traceback = [error_str] if error_frame: # The error occurred in a py3status module so the traceback # should be made to appear correct. We caught the exception # but make it look as though we did not. traceback += format_stack(error_frame, 1) + format_tb(tb) filename = Path(error_frame.f_code.co_filename).name line_no = error_frame.f_lineno else: # This is a none module based error traceback += format_tb(tb) # Find first relevant trace in the stack. # it should be in py3status or one of it's modules. found = False for item in reversed(stack): filename = item[0] for path in py3_paths: if filename.startswith(path): # Found a good trace filename = item[0].name line_no = item[1] found = True break if found: break # all done! create our message. msg = "{} ({}) {} line {}.".format(msg, exc_type.__name__, filename, line_no) except: # noqa e722 # something went wrong report what we can. msg = f"{msg}." finally: # delete tb! del tb # log the exception and notify user self.py3_wrapper.log(msg, "warning") if traceback: # if debug is not in the config then we are at an early stage of # running py3status and logging is not yet available so output the # error to STDERR so it can be seen if "debug" not in self.config: print_stderr("\n".join(traceback)) elif self.config.get("log_file"): self.py3_wrapper.log("".join(["Traceback\n"] + traceback)) if notify_user: self.py3_wrapper.notify_user(msg, level=level)
def update_organization(test_class_instance, url, org_id, new_org_name, status='active'): """ update_organization() function updates the name of the organization :param test_class_instance: instance of the test class :param url: gateway url,for example: http://localhost:9999 :param org_id: id of the organization to update :param new_org_name: new organization name :param status: status of the org, default is active, availbale values are active/inactive :return: dictionary """ test_class_instance.mylog.info( 'org_util.update_organization() function is being called') test_class_instance.mylog.info( '-------------------------------------------------------') test_class_instance.mylog.info('') data = '{"name":"%s", "status":"%s"}' % (new_org_name, status) updated_org_name, new_org_id, error_message, tasks, log, dashboards, members, buckets \ = None, None, None, None, None, None, None, None response = test_class_instance.rl.patch(base_url=url, path=ORG_URL + '/' + str(org_id), data=data) try: new_org_id = response.json().get('id') updated_org_name = response.json().get('name') tasks = response.json().get('links').get('tasks') log = response.json().get('links').get('log') dashboards = response.json().get('links').get('dashboards') members = response.json().get('links').get('members') buckets = response.json().get('links').get('buckets') if org_id is not None and updated_org_name is not None: test_class_instance.mylog.info( 'org_util.update_organization() ORG_ID=' + str(new_org_id)) test_class_instance.mylog.info( 'org_util.update_organization() ORG_NAME=' + str(updated_org_name)) test_class_instance.mylog.info( 'org_util.update_organization() TASKS_LINK=' + str(tasks)) test_class_instance.mylog.info( 'org_util.update_organization() LOG_LINK=' + str(log)) test_class_instance.mylog.info( 'org_util.update_organization() DASHBOARD_LINK=' + str(dashboards)) test_class_instance.mylog.info( 'org_util.update_organization() MEMBERS_LINK=' + str(members)) test_class_instance.mylog.info( 'org_util.update_organization() BUCKETS_LINK=' + str(buckets)) else: test_class_instance.mylog.info( 'org_util.update_organization() ' 'REQUESTED_ORG_ID AND REQUESTED_ORG_NAME ARE NONE') error_message = response.headers['X-Influx-Error'] test_class_instance.mylog.info( 'gateway_util.create_organization() ERROR=' + error_message) except: test_class_instance.mylog.info( 'org_util.update_organization() Exception:') clt_error_type, clt_error_message, clt_error_traceback = sys.exc_info() test_class_instance.mylog.info('litmus_util.execCmd:' + str(clt_error_message)) test_class_instance.mylog.info( 'litmus_util.execCmd:' + str(traceback.extract_tb(clt_error_traceback))) test_class_instance.mylog.info('litmus_util.execCmd:' + str(clt_error_message)) error_message = response.headers['X-Influx-Error'] test_class_instance.mylog.info( 'org_util.update_organization() function is done') test_class_instance.mylog.info('') return { 'status': response.status_code, 'updated_org_id': new_org_id, 'updated_org_name': updated_org_name, 'tasks_link': tasks, 'log_link': log, 'dashboards_link': dashboards, 'members_link': members, 'buckets_link': buckets, 'error_message': error_message }
def create_organization(test_class_instance, url, org_name, status='active'): """ create_organization() function creates an organization :param test_class_instance: instance of the test class :param url: gateway url,for example: http://localhost:9999 :param org_name: name of the organization to create :param status: status of the org, default is active, available values are active/inactive :return: dictionary """ test_class_instance.mylog.info( 'org_util.create_organization() function is being called') test_class_instance.mylog.info( '-------------------------------------------------------') test_class_instance.mylog.info('') test_class_instance.mylog.info( 'org_util.create_organization() Creating Organization with \'%s\' name ' 'and status \'%s\'' % (org_name, status)) data = '{"name":"%s","status:":"%s"}' % (org_name, status) org_id, created_org_name, error_message, tasks, log, dashboards, members, buckets \ = None, None, None, None, None, None, None, None response = test_class_instance.rl.post(base_url=url, path=ORG_URL, data=data) """ { u'id': u'02e54dc99c0dc000', u'links': {u'tasks': u'/api/v2/tasks?org=test_org_1', u'log': u'/api/v2/orgs/02e54dc99c0dc000/log', u'self': u'/api/v2/orgs/02e54dc99c0dc000', u'dashboards': u'/api/v2/dashboards?org=test_org_1', u'members': u'/api/v2/orgs/02e54dc99c0dc000/members', u'buckets': u'/api/v2/buckets?org=test_org_1'}, u'name': u'test_org_1'} """ try: org_id = response.json().get('id') created_org_name = response.json().get('name') tasks = response.json().get('links').get('tasks') log = response.json().get('links').get('log') dashboards = response.json().get('links').get('dashboards') members = response.json().get('links').get('members') buckets = response.json().get('links').get('buckets') if org_id is not None and created_org_name is not None: test_class_instance.mylog.info( 'org_util.create_organization() ORG_ID=' + str(org_id)) test_class_instance.mylog.info( 'org_util.create_organization() ORG_NAME=' + str(created_org_name)) test_class_instance.mylog.info( 'org_util.create_organization() TASKS_LINK=' + str(tasks)) test_class_instance.mylog.info( 'org_util.create_organization() LOG_LINK=' + str(log)) test_class_instance.mylog.info( 'org_util.create_organization() DASHBOARD_LINK=' + str(dashboards)) test_class_instance.mylog.info( 'org_util.create_organization() MEMBERS_LINK=' + str(members)) test_class_instance.mylog.info( 'org_util.create_organization() BUCKETS_LINK=' + str(buckets)) else: test_class_instance.mylog.info( 'org_util.create_organization() ' 'REQUESTED_ORG_ID AND REQUESTED_ORG_NAME ARE NONE') error_message = response.headers['X-Influx-Error'] test_class_instance.mylog.info( 'org_util.create_organization() ERROR=' + error_message) except: test_class_instance.mylog.info( 'org_util.create_organization() Exception:') clt_error_type, clt_error_message, clt_error_traceback = sys.exc_info() test_class_instance.mylog.info('litmus_util.execCmd:' + str(clt_error_message)) test_class_instance.mylog.info( 'litmus_util.execCmd:' + str(traceback.extract_tb(clt_error_traceback))) test_class_instance.mylog.info('litmus_util.execCmd:' + str(clt_error_message)) error_message = response.headers['X-Influx-Error'] test_class_instance.mylog.info( 'org_util.create_organization() ERROR=' + error_message) test_class_instance.mylog.info( 'org_util.create_organization() function is done') test_class_instance.mylog.info('') return { 'status': response.status_code, 'org_id': org_id, 'org_name': created_org_name, 'tasks_link': tasks, 'log_link': log, 'dashboards_link': dashboards, 'members_link': members, 'buckets_link': buckets, 'error_message': error_message }
level=(logging.ERROR - args.verbose * 10)) try: # Run the function module = importlib.import_module('src.{name}.index'.format(name=args.name)) event_json = get_payload(args.input, '{}') event = json.loads(event_json) start_time = time.time() response = module.handler(event, context) end_time = time.time() - start_time except Exception as exc: exc_type, exc_value, exc_traceback = sys.exc_info() response = { 'errorMessage': str(exc_value), 'stackTrace': traceback.extract_tb(exc_traceback), 'errorType': exc_type.__name__ } del exc_traceback print("\nOutput:\n--------") pprint(response) # pprint(json.dumps(response, indent=4, separators=(',', ': '))) if event is not None: print("\nInput:\n--------") pprint(event) if 'end_time' in locals(): print('\nLambda EXEC TIME') print("-> %s seconds" % end_time)
def get_organization_by_id(test_class_instance, url, org_id): """ get_organization_by_id() function return organization info based on the organization id :param test_class_instance: instance of the test class :param url: gateway url,for example: http://localhost:9999 :param org_id: id of the organization to get :return: dictionary """ test_class_instance.mylog.info( 'org_util.get_organization_by_id() function is being called') test_class_instance.mylog.info( '----------------------------------------------------------') test_class_instance.mylog.info('') test_class_instance.mylog.info('org_util.get_organization_by_id() ' 'Getting Organization with \'%s\' id' % org_id) requested_org_id, requested_org_name, error_message, tasks, log, dashboards, members, buckets \ = None, None, None, None, None, None, None, None response = test_class_instance.rl.get(base_url=url, path=ORG_URL + '/' + str(org_id)) try: requested_org_id = response.json().get('id') requested_org_name = response.json().get('name') tasks = response.json().get('links').get('tasks') log = response.json().get('links').get('log') dashboards = response.json().get('links').get('dashboards') members = response.json().get('links').get('members') buckets = response.json().get('links').get('buckets') if requested_org_id is not None and requested_org_name is not None: test_class_instance.mylog.info( 'org_util.get_organization_by_id() REQUESTED_ORG_ID=' + str(requested_org_id)) test_class_instance.mylog.info( 'org_util.get_organization_by_id() REQUESTED_ORG_NAME=' + str(requested_org_name)) test_class_instance.mylog.info( 'org_util.get_organization_by_id() TASKS_LINK=' + str(tasks)) test_class_instance.mylog.info( 'org_util.get_organization_by_id() LOG_LINK=' + str(log)) test_class_instance.mylog.info( 'org_util.get_organization_by_id() DASHBOARD_LINK=' + str(dashboards)) test_class_instance.mylog.info( 'org_util.get_organization_by_id() MEMBERS_LINK=' + str(members)) test_class_instance.mylog.info( 'org_util.get_organization_by_id() BUCKETS_LINK=' + str(buckets)) else: test_class_instance.mylog.info( 'org_util.get_organization_by_id() ' 'REQUESTED_ORG_ID AND REQUESTED_ORG_NAME ARE NONE') error_message = response.headers['X-Influx-Error'] test_class_instance.mylog.info( 'org_util.get_organization_by_id() ERROR=' + error_message) except: test_class_instance.mylog.info( 'org_util.get_organization_by_id() Exception:') clt_error_type, clt_error_message, clt_error_traceback = sys.exc_info() test_class_instance.mylog.info('litmus_util.execCmd:' + str(clt_error_message)) test_class_instance.mylog.info( 'litmus_util.execCmd:' + str(traceback.extract_tb(clt_error_traceback))) test_class_instance.mylog.info('litmus_util.execCmd:' + str(clt_error_message)) error_message = response.headers['X-Influx-Error'] test_class_instance.mylog.info( 'org_util.get_organization_by_id() function is done') test_class_instance.mylog.info('') return { 'status': response.status_code, 'requested_org_id': requested_org_id, 'requested_org_name': requested_org_name, 'tasks_link': tasks, 'log_link': log, 'dashboards_link': dashboards, 'members_link': members, 'buckets_link': buckets, 'error_message': error_message }
def _init(self, trcback): """format a traceback from sys.exc_info() into 7-item tuples, containing the regular four traceback tuple items, plus the original template filename, the line number adjusted relative to the template source, and code line from that line number of the template.""" import mako.template mods = {} rawrecords = traceback.extract_tb(trcback) new_trcback = [] for filename, lineno, function, line in rawrecords: if not line: line = '' try: (line_map, template_lines) = mods[filename] except KeyError: try: info = mako.template._get_module_info(filename) module_source = info.code template_source = info.source template_filename = info.template_filename or filename except KeyError: # A normal .py file (not a Template) if not compat.py3k: try: fp = open(filename, 'rb') encoding = util.parse_encoding(fp) fp.close() except IOError: encoding = None if encoding: line = line.decode(encoding) else: line = line.decode('ascii', 'replace') new_trcback.append((filename, lineno, function, line, None, None, None, None)) continue template_ln = 1 source_map = mako.template.ModuleInfo. \ get_module_source_metadata( module_source, full_line_map=True) line_map = source_map['full_line_map'] template_lines = [line_ for line_ in template_source.split("\n")] mods[filename] = (line_map, template_lines) template_ln = line_map[lineno - 1] if template_ln <= len(template_lines): template_line = template_lines[template_ln - 1] else: template_line = None new_trcback.append((filename, lineno, function, line, template_filename, template_ln, template_line, template_source)) if not self.source: for l in range(len(new_trcback) - 1, 0, -1): if new_trcback[l][5]: self.source = new_trcback[l][7] self.lineno = new_trcback[l][5] break else: if new_trcback: try: # A normal .py file (not a Template) fp = open(new_trcback[-1][0], 'rb') encoding = util.parse_encoding(fp) fp.seek(0) self.source = fp.read() fp.close() if encoding: self.source = self.source.decode(encoding) except IOError: self.source = '' self.lineno = new_trcback[-1][1] return new_trcback
print none if circles is not None: # convert the (x, y) coordinates and radius of the circles to integers circles = np.round(circles[0, :]).astype("int") # loop over the (x, y) coordinates and radius of the circles for (x, y, r) in circles: # draw the circle in the output image, then draw a rectangle corresponding to the center of the circle cv2.circle(output, (x, y), r, (255, 255, 0), 4) cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1) #save each image out = np.hstack([image, output]) cv2.imwrite( 'C:\\Users\\Devin Roach\\Dropbox (GaTech)\\177A_BigPrinter\\Devin\\Machine Vision\\Code\\images\\imgmindist' + str(minDist) + '_1param' + str(param1) + '_2param,' + str(param2) + '_.jpg', out) #plt.imsave('imgmindist'+str(minDist)+'p1_'+str(param1), 'p2_'+ str(param2),np.hstack([image,output])) # show the output image #cv2.imshow("output", np.hstack([image, output])) #cv2.waitKey(0) except Exception as e: print(e) tb = sys.exc_info()[-1] print(traceback.extract_tb(tb, limit=1)[-1][1]) exc_type, exc_obj, exc_tb = sys.exc_info() print('\n\n\n\nError on Line: ' + str(exc_tb.tb_lineno) + ' Error Information: ' + str(sys.exc_info()[:]))