def wrapped_func_with_error_reporting(*args, **kwargs): if not _get_backend_host(): warnings.warn("flawless server hostport not set", RuntimeWarning, stacklevel=2) try: return func(*args, **kwargs) except: type, value, sys_traceback = sys.exc_info() # Check to try and prevent multiple reports of the same exception if hasattr(value, "_flawless_already_caught"): if reraise_exception: raise_(value, None, sys_traceback) else: return # Get trackback & report it hostname = socket.gethostname() record_error( hostname=hostname, exc_info=(type, value, sys_traceback), preceding_stack=preceding_stack, error_threshold=error_threshold, ) # Reraise exception if so desired if reraise_exception: object.__setattr__(value, "_flawless_already_caught", True) raise_(value, None, sys_traceback)
def do_obj(self, attrs): for name, value in attrs: if name == "id": obj = self.objects.get(value) if not obj: raise_(SyntaxError, "id " + value + " not defined!") self.output_obj(obj)
def test_create_mesh_from_regions_check_segs(self): '''Test that create_mesh_from_regions fails when an interior region is outside bounding polygon. ''' # These are the absolute values min_x = 10 min_y = 88 polygon = [[min_x, min_y], [1000, 100], [1000, 1000], [100, 1000]] boundary_tags = {'walls': [0, 1, 3], 'bom': [2]} # This one is inside bounding polygon - should pass inner_polygon = [[800, 400], [900, 500], [800, 600]] interior_regions = [(inner_polygon, 5)] m = create_mesh_from_regions(polygon, boundary_tags, 10000000, interior_regions=interior_regions) boundary_tags = {'walls': [0, 1, 3, 4], 'bom': [2]} try: m = create_mesh_from_regions(polygon, boundary_tags, 10000000, interior_regions=interior_regions) except: pass else: msg = 'Segment out of bounds not caught ' raise_(Exception, msg)
def insert(self, db, fatId): if self.highDOM is None: if self.lowDOM is None: raise ValueError("Unknown high and low DOMs") else: raise ValueError("Unknown high DOM") elif self.lowDOM is None: raise ValueError("Unknown low DOM") hiProdId = db.getDOMId(self.highDOM) if hiProdId is None: raise_(ValueError, 'Could not get Product ID for high DOM#' + \ self.highDOM) loProdId = db.getDOMId(self.lowDOM) if loProdId is None: raise_(ValueError, 'Could not get Product ID for low DOM#' + \ self.lowDOM) cursor = db.executeQuery(('insert into FATLCChain(fat_id' + ',hi_prod_id,lo_prod_id' + ',down_neg,down_pos,up_neg,up_pos)' + 'values(%d,%d,%d,%d,%d,%d,%d)') % (fatId, hiProdId, loProdId, self.boolVal(self.downNeg), self.boolVal(self.downPos), self.boolVal(self.upNeg), self.boolVal(self.upPos))) cursor.close()
def executemany(self, operation, *param): """ Prepare a database operation (query or command) and then execute it against all parameter sequences or mappings found in the sequence seq_of_parameters. Modules are free to implement this method using multiple calls to the execute() method or by using array operations to have the database process the sequence as a whole in one call. Use of this method for an operation which produces one or more result sets constitutes undefined behavior, and the implementation is permitted (but not required) to raise an exception when it detects that a result set has been created by an invocation of the operation. The same comments as for execute() also apply accordingly to this method. Return values are not defined. """ if self.closed: raise_(ProgrammingError, "Cursor " + self.name + " is closed") raise Error("Unimplemented")
def unknown_starttag(self, tag, attributes): method_name = "start_" + attribute_type_dict.get(tag, "") if hasattr(self, method_name): method = getattr(self, method_name) attributes["name"] = tag return method(attributes) raise_(XmlException, "Unknown tag: "+tag)
def __init__(self, domain=None, function=None, default_stage=0.0): """ Instantiate a Characteristic_stage_boundary. domain is the domain containing the boundary function is the function to apply the wave default_stage is the assumed stage pre the application of wave """ raise Exception('This boundary type is not implemented yet') Boundary.__init__(self) if domain is None: msg = 'Domain must be specified for this type boundary' raise_(Exception, msg) if function is None: msg = 'Function must be specified for this type boundary' raise_(Exception, msg) self.domain = domain self.function = function self.default_stage = default_stage self.Elev = domain.quantities['elevation'] self.Stage = domain.quantities['stage'] self.Height = domain.quantities['height']
def __getitem__(self, key): if key in self.specs: avmdt = self.specs[key] return avmdt.get_data(self.xmp) else: raise_(KeyError, "The key '%s' is not an AVM field" % key)
def input_elements(self, instruction_id, expected_targets, abort_callback=None): """ Generator to retrieve elements for an instruction_id input_elements should be called only once for an instruction_id Args: instruction_id(str): instruction_id for which data is read expected_targets(collection): expected targets """ received = self._receiving_queue(instruction_id) done_targets = [] abort_callback = abort_callback or (lambda: False) try: while len(done_targets) < len(expected_targets): try: data = received.get(timeout=1) except queue.Empty: if self._closed: raise RuntimeError('Channel closed prematurely.') if abort_callback(): return if self._exc_info: t, v, tb = self._exc_info raise_(t, v, tb) else: if not data.data and data.target in expected_targets: done_targets.append(data.target) else: assert data.target not in done_targets yield data finally: # Instruction_ids are not reusable so Clean queue once we are done with # an instruction_id self._clean_receiving_queue(instruction_id)
def fetchone(self): """ Fetch the next row of a query result set, returning a single sequence, or None when no more data is available. An Error (or subclass) exception is raised if the previous call to executeXXX() did not produce any result set or no call was issued yet. """ if self.closed: raise_(ProgrammingError, "Cursor " + self.name + " is closed") if self.activeResult is None: return None if self.rowcount == 0: return None result = self.activeResult[0] self.rowcount = len(self.activeResult) - 1 if self.rowcount == 0: self.activeResult = None else: del self.activeResult[0] if self.debug: print('FETCHONE => ' + str(result) + '(' + str(type(result)) + \ ') [' + str(self.rowcount) + ' rows remain] ' + \ str(self.activeResult)) return result
def _raise_error(self, exec_info): """Raise an exception from the provided exception info. Used to cause the main thread to stop with an error. """ # Rethrow exception with its original stack trace following advice from: # http://nedbatchelder.com/blog/200711/rethrowing_exceptions_in_python.html raise_(exec_info[1], exec_info[2])
def DictToProtoMessage(values, out_message): for name, field in out_message.DESCRIPTOR.fields_by_name.items(): if name not in values: if field.label == field.LABEL_REQUIRED: raise_(ValueError, "Missing required field %s" % name) continue value = values.get(name) if field.type == field.TYPE_MESSAGE: inner_message = getattr(out_message, name) if field.label == field.LABEL_REPEATED: for subval in value: DictToProtoMessage(subval, inner_message.add()) else: DictToProtoMessage(value, inner_message) else: if field.label == field.LABEL_REPEATED: out = getattr(out_message, name) for v in value: if isinstance(v, datetime.datetime): v = util.datetime_to_iso8601str(v, TIME_ZONE) out.append(v) else: if isinstance(value, datetime.datetime): value = util.datetime_to_iso8601str(value, TIME_ZONE) setattr(out_message, name, value) return out_message
def test_create_mesh_with_segments_out_of_bounds(self): """Test that create_mesh_from_regions fails when a segment is out of bounds. """ # These are the absolute values min_x = 10 min_y = 88 polygon = [[min_x, min_y], [1000, 100], [1000, 1000], [100, 1000]] boundary_tags = {'walls': [0, 1], 'bom': [2, 3], 'out': [5]} # This one is inside bounding polygon - should pass inner_polygon = [[800, 400], [900, 500], [800, 600]] interior_regions = [(inner_polygon, 5)] try: m = create_mesh_from_regions(polygon, boundary_tags, 10000000, interior_regions=interior_regions) except: pass else: msg = 'Tags are listed repeatedly, but create mesh from regions ' msg += 'does not cause an Exception to be raised' raise_(Exception, msg)
def recv(self, max_bytes): """ Receive up to max_bytes data from the target. Args: max_bytes (int): Maximum number of bytes to receive. Returns: Received data. """ data = b"" try: data = self._sock.recv(max_bytes) except socket.timeout: data = b"" except socket.error as e: if e.errno == errno.ECONNABORTED: raise_( exception.BoofuzzTargetConnectionAborted(socket_errno=e.errno, socket_errmsg=e.strerror), None, sys.exc_info()[2], ) elif (e.errno == errno.ECONNRESET) or (e.errno == errno.ENETRESET) or (e.errno == errno.ETIMEDOUT): raise_(exception.BoofuzzTargetConnectionReset(), None, sys.exc_info()[2]) elif e.errno == errno.EWOULDBLOCK: # timeout condition if using SO_RCVTIMEO or SO_SNDTIMEO data = b"" else: raise return data
def send(self, data): """ Send data to the target. Only valid after calling open! Some protocols will truncate; see self.MAX_PAYLOADS. Args: data: Data to send. Returns: int: Number of bytes actually sent. """ num_sent = 0 try: if self.server: if self._udp_client_port is None: raise exception.BoofuzzError("recv() must be called before send with udp fuzzing servers.") num_sent = self._sock.sendto(data, self._udp_client_port) else: num_sent = self._sock.sendto(data, (self.host, self.port)) except socket.error as e: if e.errno == errno.ECONNABORTED: raise_( exception.BoofuzzTargetConnectionAborted(socket_errno=e.errno, socket_errmsg=e.strerror), None, sys.exc_info()[2], ) elif e.errno in [errno.ECONNRESET, errno.ENETRESET, errno.ETIMEDOUT, errno.EPIPE]: raise_(exception.BoofuzzTargetConnectionReset(), None, sys.exc_info()[2]) else: raise return num_sent
def _raise_error(self, exec_info): """Raise an exception from the provided exception info. Used to cause the main thread to stop with an error. """ # Rethrow exception with its original stack trace following advice from: # http://nedbatchelder.com/blog/200711/rethrowing_exceptions_in_python.html raise_(exec_info[1], None, exec_info[2])
def evaluate_expression(expression, bindings, error_value=RAISE): typechecks.require_string(expression) # Since Python 2 doesn't have a nonlocal keyword, we have to box up the # error_value, so we can reassign to it in the ``on_error`` function # below. error_box = [error_value] try: # Give some basic modules. standard_environment = dict(STANDARD_EVALUATION_ENVIRONMENT) # Add our "on_error" hack. def on_error(value): error_box[0] = value standard_environment["on_error"] = on_error return eval(expression, standard_environment, bindings) except Exception as e: if error_box[0] is not RAISE: return error_box[0] extra = "Error while evaluating: \n\t%s\non:\n%s" % (expression, bindings) traceback = sys.exc_info()[2] raise_(ValueError, str(e) + "\n" + extra, traceback)
def getParameter(self, param_name): """ Return a particular parameter. Note for vector parameters (value_index>1) the parameter is automatically converted and returned as a Python sequence. """ c = self.db.cursor() c.execute( """ SELECT value_index,value FROM STFResultParameter LEFT JOIN STFParameter USING(stf_param_id) WHERE stf_result_id=%s AND name=%s ORDER BY value_index """, (self.stf_result_id, param_name)) a = c.fetchall() if len(a) == 0: raise_(LookupError, "Parameter " + param_name + " not found.") elif len(a) == 1: return a[0][1] else: return [x[1] for x in a]
def read_polygon(filename, delimiter=',', closed=True, verbose=False): """ Read points assumed to form a (closed) polygon. Can also be used to read in a polyline (closed=False) Also checks to make sure polygon (polyline) is not complex (self-intersecting). filename Path to file containing polygon data. delimiter Delimiter to split polygon data with. A list of point data from the polygon file. There must be exactly two numbers in each line separated by the delimiter. No header. """ fid = open(filename) lines = fid.readlines() fid.close() polygon = [] for line in lines: fields = line.split(delimiter) polygon.append([float(fields[0]), float(fields[1])]) # check this is a valid polygon (polyline). if is_complex(polygon, closed, verbose=verbose): msg = 'ERROR: Self-intersecting polygon detected in file ' msg += filename + '. A complex polygon will not ' msg += 'necessarily break the algorithms within ANUGA, but it' msg += 'usually signifies pathological data. Please fix this file.' raise_(Exception, msg) return polygon
def fetchmany(self, size=None): """ Fetch the next set of rows of a query result, returning a sequence of sequences (e.g. a list of tuples). An empty sequence is returned when no more rows are available. The number of rows to fetch per call is specified by the parameter. If it is not given, the cursor's arraysize determines the number of rows to be fetched. The method should try to fetch as many rows as indicated by the size parameter. If this is not possible due to the specified number of rows not being available, fewer rows may be returned. An Error (or subclass) exception is raised if the previous call to executeXXX() did not produce any result set or no call was issued yet. Note there are performance considerations involved with the size parameter. For optimal performance, it is usually best to use the arraysize attribute. If the size parameter is used, then it is best for it to retain the same value from one fetchmany() call to the next. """ if self.closed: raise_(ProgrammingError, "Cursor " + self.name + " is closed") if arraysize == None: arraysize = self.arraysize raise Error("Unimplemented")
def fetchall(self): """ Fetch all (remaining) rows of a query result, returning them as a sequence of sequences (e.g. a list of tuples). Note that the cursor's arraysize attribute can affect the performance of this operation. An Error (or subclass) exception is raised if the previous call to executeXXX() did not produce any result set or no call was issued yet. """ if self.closed: raise_(ProgrammingError, "Cursor " + self.name + " is closed") if self.activeResult is None: raise DataError("No data found") if self.rowcount == 0: return None result = self.activeResult self.rowcount = 0 self.activeResult = None if self.debug: print('FETCHALL => ' + str(result) + '(' + str(type(result)) + \ ') [' + str(self.rowcount) + ' rows remain] ' + \ str(self.activeResult)) return result
def sync(clx, clk0, err, tol=1E-08, bin_width=1.0): """The DOM to pulser synchronization routine.""" brk = [clk0 - err, clk0, clk0 + err] hist = perhist(clx, bin_width) for i in range(500): vals = list(map(hist.fill, brk)) # Harbinger of bad things to come if brk[2] == brk[1] or brk[1] == brk[0]: brk[2] = brk[1] + random.random() brk[0] = brk[1] - random.random() if vals[1] - vals[0] < vals[1] - vals[2]: # Move up point between 0 and 1 trp = brk[0] + 0.5 * (brk[1] - brk[0]) tva = hist.fill(trp) m = tva < vals[1] # print "Move -" else: # Test point between 1 and 2 trp = brk[1] + 0.5 * (brk[2] - brk[1]) tva = hist.fill(trp) m = 2 - (tva < vals[1]) # print "Move +" brk[m] = trp vals[m] = tva #print i, brk, vals if (brk[2] - brk[0]) / brk[0] < tol: hist.fill(brk[1]) return (hist.mode(), brk[1]) # Fail raise_(LockInSyncFailure, i)
def send(self, data): """ Send data to the target. Only valid after calling open! Args: data: Data to send. Returns: int: Number of bytes actually sent. """ num_sent = 0 try: num_sent = self._sock.send(data) except socket.error as e: if e.errno == errno.ECONNABORTED: raise_( exception.BoofuzzTargetConnectionAborted(socket_errno=e.errno, socket_errmsg=e.strerror), None, sys.exc_info()[2], ) elif e.errno in [errno.ECONNRESET, errno.ENETRESET, errno.ETIMEDOUT, errno.EPIPE]: raise_(exception.BoofuzzTargetConnectionReset(), None, sys.exc_info()[2]) else: raise return num_sent
def adb(self, command, timeout_secs=5 * 60, capture_output=False, **kwargs): """Run any ADB command. For example, the following code will use "adb shell am start" to launch an app on the device:: d = AdbDevice(...) d.adb(["shell", "am", "start", "-S", "com.example.myapp/com.example.myapp.MainActivity"]) ``command`` and ``kwargs`` are the same as `subprocess.check_output`, except that ``shell``, ``stdout`` and ``stderr`` are not allowed. Raises `AdbError` if the command fails. """ try: if self.tcpip: self._connect(timeout_secs) output = self._adb(command, timeout_secs, **kwargs) except subprocess.CalledProcessError as e: raise_(AdbError(e.returncode, e.cmd, e.output.decode("utf-8"), self), None, sys.exc_info()[2]) if capture_output: return output else: sys.stderr.write(output) return None
def send(self, data): """ Send data to the target. Only valid after calling open! Data will be trunctated to self.packet_size (Default: 1500 bytes). Args: data: Data to send. Returns: int: Number of bytes actually sent. """ num_sent = 0 data = data[: self.packet_size] try: num_sent = self._sock.sendto(data, (self.interface, self.ethernet_proto, 0, 0, self.l2_dst)) except socket.error as e: if e.errno == errno.ECONNABORTED: raise_( exception.BoofuzzTargetConnectionAborted(socket_errno=e.errno, socket_errmsg=e.strerror), None, sys.exc_info()[2], ) elif e.errno in [errno.ECONNRESET, errno.ENETRESET, errno.ETIMEDOUT, errno.EPIPE]: raise_(exception.BoofuzzTargetConnectionReset(), None, sys.exc_info()[2]) else: raise return num_sent
def save(self, fName): "Save bigrams to a file." self.fName = fName if os.path.exists(fName): raise_(RuntimeError, "I won't overwrite the file: " + fName) f = open(fName, "w") if self.NOTA: f.write("NOTA = 1\n") else: f.write("NOTA = 0\n") f.write(string.join(self.c) + "\n") for i in range(len(self.c)): f.write("P(len=%d) = %s\n" % (i + 1, str(self.plength[i]))) for c in self.c: f.write("P(%s) = %s\n" % (c, str(self.p1[c]))) for d in self.c: if c == d: continue f.write(" P(%s|%s) = %s\n" % (d, c, str(self.p2[c][d]))) f.close()
def _compile(*key): # internal: compile pattern pattern, flags = key bypass_cache = flags & DEBUG if not bypass_cache: cachekey = (type(key[0]), ) + key try: p, loc = _cache[cachekey] if loc is None or loc == _locale.setlocale(_locale.LC_CTYPE): return p except KeyError: pass if isinstance(pattern, _pattern_type): if flags: raise ValueError( 'Cannot process flags argument with a compiled pattern') return pattern if not sre_compile.isstring(pattern): raise TypeError("first argument must be string or compiled pattern") try: p = sre_compile.compile(pattern, flags) except error as v: raise_(error, v) # invalid expression if not bypass_cache: if len(_cache) >= _MAXCACHE: _cache.clear() if p.flags & LOCALE: if not _locale: return p loc = _locale.setlocale(_locale.LC_CTYPE) else: loc = None _cache[cachekey] = p, loc return p
def wrapped_func_with_error_reporting(*args, **kwargs): if not _get_backend_host(): warnings.warn("flawless server hostport not set", RuntimeWarning, stacklevel=2) try: return func(*args, **kwargs) except: type, value, sys_traceback = sys.exc_info() # Check to try and prevent multiple reports of the same exception if hasattr(value, "_flawless_already_caught"): if reraise_exception: raise_(value, None, sys_traceback) else: return # Get trackback & report it hostname = socket.gethostname() record_error( hostname=hostname, exc_info=(type, value, sys_traceback), preceding_stack=preceding_stack, error_threshold=error_threshold, ) # Reraise exception if so desired if reraise_exception: setattr(value, "_flawless_already_caught", True) raise_(value, None, sys_traceback)
def lookup(name, namespace): """ Get a method or class from any imported module from its name. Usage: lookup(functionName, globals()) """ dots = name.count('.') if dots > 0: moduleName, objName = '.'.join( name.split('.')[:-1]), name.split('.')[-1] module = __import__(moduleName) return getattr(module, objName) else: modules = [ obj for obj in list(namespace.values()) if (str(type(obj)) == "<class 'module'>" or str(type(obj)) == "<type 'module'>") ] options = [ getattr(module, name) for module in modules if name in dir(module) ] options += [ obj[1] for obj in list(namespace.items()) if obj[0] == name ] if len(options) == 1: return options[0] if len(options) > 1: raise Exception('Name conflict for %s') raise_(Exception, '%s not found as a method or class' % name)
def set_column(self, column_name, column_values, overwrite=False): """ Add a column to the 'end' (with the right most column being the end) of the csv file. Set overwrite to True if you want to overwrite a column. Note, in column_name white space is removed and case is not checked. Precondition The column_name and column_values cannot have comma's in it. """ # sanity checks value_row_count = \ len(self._attribute_dic[list(self._title_index_dic.keys())[0]]) if len(column_values) != value_row_count: msg = 'The number of column values must equal the number of rows.' raise_(DataMissingValuesError, msg) # check new column name isn't already used, and we aren't overwriting if column_name in self._attribute_dic: if not overwrite: msg = 'Column name %s already in use!' % column_name raise_(TitleValueError, msg) else: # New title. Add it to the title index. self._title_index_dic[column_name] = len(self._title_index_dic) self._attribute_dic[column_name] = column_values
def MGMT_ED_SCAN(self, channelMask, count, period, scanDuration, dstAddr, timeout): self._execute_and_check('energy scan {} {} {} {} {}'.format( channelMask, count, period, scanDuration, dstAddr, )) self._sleep(timeout) result = self._execute_and_check('energy report {}'.format(dstAddr)) if result[0] == 'null': raise commissioner.Error( 'No energy report found for {}'.format(dstAddr)) try: result = json.loads(' '.join(result[:-1])) return OTCommissioner.EnergyReport( channelMask=self._channel_mask_from_json_obj( result['ChannelMask']), energyList=self._hex_to_bytes(result['EnergyList']), ) except Exception as e: raise_(commissioner.Error, repr(e), sys.exc_info()[2])
def recv(self, max_bytes): """Receive up to max_bytes data from the target. Args: max_bytes(int): Maximum number of bytes to receive. Returns: Received data. """ data = b"" try: if self.bind or self.server: data, self._udp_client_port = self._sock.recvfrom(max_bytes) else: raise exception.SullyRuntimeError( "UDPSocketConnection.recv() requires a bind address/port." " Current value: {}".format(self.bind) ) except socket.timeout: data = b"" except socket.error as e: if e.errno == errno.ECONNABORTED: raise_( exception.BoofuzzTargetConnectionAborted(socket_errno=e.errno, socket_errmsg=e.strerror), None, sys.exc_info()[2], ) elif e.errno in [errno.ECONNRESET, errno.ENETRESET, errno.ETIMEDOUT]: raise_(exception.BoofuzzTargetConnectionReset(), None, sys.exc_info()[2]) elif e.errno == errno.EWOULDBLOCK: data = b"" else: raise return data
def _test_connect(self, client_version=None, server_version=None): server = FakeServer(minecraft_version=server_version) addr, port = server.listen_socket.getsockname() cond = threading.Condition() def handle_client_exception(exc, exc_info): with cond: cond.exc_info = exc_info cond.notify_all() def client_write(packet, *args, **kwds): def packet_write(*args, **kwds): logging.debug('[C-> ] %s' % packet) return real_packet_write(*args, **kwds) real_packet_write = packet.write packet.write = packet_write return real_client_write(packet, *args, **kwds) def client_react(packet, *args, **kwds): logging.debug('[ ->C] %s' % packet) return real_client_react(packet, *args, **kwds) client = connection.Connection( addr, port, username='******', initial_version=client_version, handle_exception=handle_client_exception) real_client_react = client._react real_client_write = client.write_packet client.write_packet = client_write client._react = client_react try: with cond: server_thread = threading.Thread( name='_ConnectTest server', target=self._test_connect_server, args=(server, cond)) server_thread.daemon = True server_thread.start() self._test_connect_client(client, cond) cond.exc_info = Ellipsis cond.wait(THREAD_TIMEOUT_S) if cond.exc_info is Ellipsis: self.fail('Timed out.') elif cond.exc_info is not None: raise_(*cond.exc_info) finally: # Wait for all threads to exit. for thread in server_thread, client.networking_thread: if thread is not None and thread.is_alive(): thread.join(THREAD_TIMEOUT_S) if thread is not None and thread.is_alive(): if cond.exc_info is None: self.fail('Thread "%s" timed out.' % thread.name) else: # Keep the earlier exception, if there is one. break
def await_completion(self): update = self.visible_updates.take() try: if update.exception: t, v, tb = update.exc_info raise_(t, v, tb) finally: self.executor_service.shutdown() self.executor_service.await_completion()
def __call__(self, environ, start_response): try: return self.app(environ, start_response) except: type, value, tb = sys.exc_info() reconstructed_req = self._reconstruct_request(environ) flawless.client.record_error(hostname=self.hostname, tb=tb, exception_message=repr(value), additional_info=reconstructed_req) raise_(value, None, tb)
def add_extra_error_message(e): etype, value, traceback = sys.exc_info() extra_message = common_errors[(type(e), str(e))] if extra_message: if sys.version_info >= (3,): raise_from(AutogradHint(extra_message), e) else: raise_(AutogradHint, (extra_message, etype, value), traceback) raise_(etype, value, traceback)
def make_unicode(input_str): try: return input_str.decode('utf-8') except AttributeError as ae: if ae.args[0] == "'NoneType' object has no attribute 'decode'": return None # None input elif ae.args[0] == "'str' object has no attribute 'decode'": return input_str # Python 3 str else: # other; re-throw error traceback = sys.exc_info()[2] raise_(AttributeError, ae.args[0], traceback)
def handle_cb_error(self, event): """a callback running your code raised an exception""" # get exception info exc_info = event.data # keyboard interrupt if exc_info[0] is KeyboardInterrupt: if self._runtime._run_cfg._catch_kb_intr: self._log.debug("keyboard interrupt (in callback)") return CPU_EVENT_USER_ABORT # re-raise other error self._log.error("handle CALLBACK raised: %s", exc_info[0].__name__) raise_(*exc_info)
def _run(self): while True: if self.interrupt: break # Attempt to write out as many as 300 packets as possible every # 0.05 seconds (20 ticks per second) num_packets = 0 self.connection._write_lock.acquire() try: while self.connection._pop_packet(): num_packets += 1 if num_packets >= 300: break exc_info = None except: exc_info = sys.exc_info() self.connection._write_lock.release() # Read and react to as many as 50 packets num_packets = 0 packet = self.connection.reactor.read_packet( self.connection.file_object) while packet: num_packets += 1 # Do not raise an IOError if it occurred while a disconnect # packet was received, as this may be part of an orderly # disconnection. if packet.packet_name == 'disconnect' and \ exc_info is not None and \ isinstance(exc_info[1], IOError): exc_info = None try: self.connection.reactor.react(packet) for listener in self.connection.packet_listeners: listener.call_packet(packet) except IgnorePacket: pass if num_packets >= 50: break packet = self.connection.reactor.read_packet( self.connection.file_object) if exc_info is not None: raise_(*exc_info) time.sleep(0.05)
def _handle_exception(self, exc, exc_info): try: exc.exc_info = exc_info # For backward compatibility. except (TypeError, AttributeError): pass if self.reactor.handle_exception(exc, exc_info): return self.exception, self.exc_info = exc, exc_info if self.handle_exception is None: raise_(*exc_info) elif self.handle_exception is not False: self.handle_exception(exc, exc_info)
def wrapper(*args, **kwargs): def call_fn(): try: fn(*args, **kwargs) except: # pylint: disable=bare-except exc_info[:] = sys.exc_info() thread = threading.Thread(target=call_fn) thread.daemon = True thread.start() thread.join(timeout_secs) if exc_info: t, v, tb = exc_info # pylint: disable=unbalanced-tuple-unpacking raise_(t, v, tb) assert not thread.is_alive(), 'timed out after %s seconds' % timeout_secs
def _resolve_dotted_name(dotted_name): try: if '.' in dotted_name: modules, _kls_name = dotted_name.rsplit('.', 1) _module = __import__(modules, fromlist=[_kls_name]) _kls = getattr(_module, _kls_name) assert callable(_kls), 'Dotted-name entity types should be callable.' return _kls elif 'self' != dotted_name: raise ValueError('Invalid class name for EntityField: %s' % dotted_name) except Exception: raise_( ValueError, 'Invalid class name for EntityField: %s' % dotted_name, sys.exc_info()[2] )
def _http_get(self, url): """HTTP GET and return response. Args: url: Target to GET. Returns: Response contents. Raises: EzOutletResetError: If the reset fails due to: - no response in self._timeout seconds """ try: return requests.get(url, timeout=self._timeout, proxies={"http": None, "https": None}).text except requests.exceptions.ConnectTimeout: raise_(exceptions.EzOutletError(self.NO_RESPONSE_MSG.format(self._timeout)), None, sys.exc_info()[2])
def _load_map(self): """ loads metadata of dictionary map from persisted file. """ try: with secure_open(self._map_file, 'rb') as f: return self._map_serializer.load(f) # get session except IOError as e: return # file does not exist except EOFError as e: if os.stat(self._map_file).st_size == 0: return # file exists but it is empty e, msg, traceback = sys.exc_info() msg.args = ( msg.args[0] + ". Memoized data appears to be corrupt.",) raise_(e, msg, traceback)
def flatten_unknown_value(self, value, caps, freezing): # Flatten enums if isinstance(value, enum.Enum): return self.flatten_enum_value(value, caps, freezing) # Flatten types and interfaces if isinstance(value, (type, InterfaceClass)): return self.flatten_type_value(value, caps, freezing) if self._externalizer is not None: extid = self._externalizer.identify(value) if extid is not None: return self.flatten_external(extid, caps, freezing) # Checks if value support the current required protocol # Could be ISnapshotable or ISerializable if freezing: try: snapshotable = ISnapshotable(value) except TypeError: raise_( TypeError, "Freezing of type %s values " "not supported by %s. Value = %r." % (type(value).__name__, reflect.canonical_name(self), value), sys.exc_info()[2] ) return self.flatten_instance(snapshotable, caps, freezing) else: try: serializable = ISerializable(value) except TypeError: raise_( TypeError, "Serialization of type %s values " "not supported by %s. Value = %r." % (reflect.canonical_name(value), reflect.canonical_name(self), value), sys.exc_info()[2] ) return self.flatten_instance(serializable, caps, freezing)
def _blocking_request(self, request): request.id = self._next_id() request.instruction_reference = self._context.process_instruction_id self._responses_by_id[request.id] = future = _Future() self._requests.put(request) while not future.wait(timeout=1): if self._exc_info: t, v, tb = self._exc_info raise_(t, v, tb) elif self._done: raise RuntimeError() del self._responses_by_id[request.id] response = future.get() if response.error: raise RuntimeError(response.error) else: return response
def send(self, data): """ Send data to the target. Only valid after calling open! Some protocols will truncate; see self.MAX_PAYLOADS. :param data: Data to send. :rtype int :return: Number of bytes actually sent. """ try: data = data[:self.MAX_PAYLOADS[self.proto]] except KeyError: pass # data = data try: if self.proto in ["tcp", "ssl"]: num_sent = self._sock.send(data) elif self.proto == "udp": num_sent = self._sock.sendto(data, (self.host, self.port)) elif self.proto == "raw-l2": num_sent = self._sock.sendto(data, (self.host, 0)) elif self.proto == "raw-l3": # Address tuple: (interface string, # Ethernet protocol number, # packet type (recv only), # hatype (recv only), # Ethernet address) # See man 7 packet for more details. num_sent = self._sock.sendto(data, (self.host, self.ethernet_proto, 0, 0, self.l2_dst)) else: raise sex.SullyRuntimeError("INVALID PROTOCOL SPECIFIED: %s" % self.proto) except socket.error as e: if e.errno == errno.ECONNABORTED: raise_(sex.BoofuzzTargetConnectionAborted(socket_errno=e.errno, socket_errmsg=e.strerror), None, sys.exc_info()[2]) elif (e.errno == errno.ECONNRESET) or \ (e.errno == errno.ENETRESET) or \ (e.errno == errno.ETIMEDOUT): raise_(sex.BoofuzzTargetConnectionReset, None, sys.exc_info()[2]) else: raise return num_sent
def _reraise_augmented(self, exn): if getattr(exn, '_tagged_with_step', False) or not self.step_name: raise step_annotation = " [while running '%s']" % self.step_name # To emulate exception chaining (not available in Python 2). original_traceback = sys.exc_info()[2] try: # Attempt to construct the same kind of exception # with an augmented message. new_exn = type(exn)(exn.args[0] + step_annotation, *exn.args[1:]) new_exn._tagged_with_step = True # Could raise attribute error. except: # pylint: disable=bare-except # If anything goes wrong, construct a RuntimeError whose message # records the original exception's type and message. new_exn = RuntimeError( traceback.format_exception_only(type(exn), exn)[-1].strip() + step_annotation) new_exn._tagged_with_step = True raise_(type(new_exn), new_exn, original_traceback)
def _handle_exception(self, exc, exc_info): handle_exception = self.handle_exception try: if self.reactor.handle_exception(exc, exc_info): return if handle_exception not in (None, False): self.handle_exception(exc, exc_info) except BaseException as new_exc: exc, exc_info = new_exc, sys.exc_info() try: exc.exc_info = exc_info # For backward compatibility. except (TypeError, AttributeError): pass self.exception, self.exc_info = exc, exc_info with self._write_lock: if self.networking_thread and not self.networking_thread.interrupt: self.disconnect(immediate=True) if handle_exception is None: raise_(*exc_info)
def _run(self): while not self.interrupt: # Attempt to write out as many as 300 packets. num_packets = 0 with self.connection._write_lock: try: while not self.interrupt and self.connection._pop_packet(): num_packets += 1 if num_packets >= 300: break exc_info = None except IOError: exc_info = sys.exc_info() # If any packets remain to be written, resume writing as soon # as possible after reading any available packets; otherwise, # wait for up to 50ms (1 tick) for new packets to arrive. if self.connection._outgoing_packet_queue: read_timeout = 0 else: read_timeout = 0.05 # Read and react to as many as 50 packets. while num_packets < 50 and not self.interrupt: packet = self.connection.reactor.read_packet( self.connection.file_object, timeout=read_timeout) if not packet: break num_packets += 1 self.connection._react(packet) read_timeout = 0 # Ignore the earlier exception if a disconnect packet is # received, as it may have been caused by trying to write to # the closed socket, which does not represent a program error. if exc_info is not None and packet.packet_name == "disconnect": exc_info = None if exc_info is not None: raise_(*exc_info)
def recv(self, max_bytes): """ Receive up to max_bytes data from the target. :param max_bytes: Maximum number of bytes to receive. :type max_bytes: int :return: Received data. """ try: if self.proto in ['tcp', 'ssl']: data = self._sock.recv(max_bytes) elif self.proto == 'udp': if self.bind: data, _ = self._sock.recvfrom(max_bytes) else: raise sex.SullyRuntimeError( "SocketConnection.recv() for UDP requires a bind address/port." " Current value:".format(self.bind)) elif self.proto in ['raw-l2', 'raw-l3']: # receive on raw is not supported. Since there is no specific protocol for raw, we would just have to # dump everything off the interface anyway, which is probably not what the user wants. data = bytes('') else: raise sex.SullyRuntimeError("INVALID PROTOCOL SPECIFIED: %s" % self.proto) except socket.timeout: data = bytes('') except socket.error as e: if e.errno == errno.ECONNABORTED: raise_(sex.BoofuzzTargetConnectionAborted, None, sys.exc_info()[2]) elif (e.errno == errno.ECONNRESET) or \ (e.errno == errno.ENETRESET) or \ (e.errno == errno.ETIMEDOUT): raise_(sex.BoofuzzTargetConnectionReset, None, sys.exc_info()[2]) else: raise return data
parser.add_argument("--encoding", dest="encoding", default="utf-8", help="File encoding to assume (default: %(default)s)") parser.add_argument("-n", "--nproc", dest="nproc", default=multiprocessing.cpu_count(), type=int, help="Number of processors to use (default: %(default)s)") parser.add_argument("engine", help="Database connection string, e.g., 'sqlite+pysqlite:///file.db'") subparsers = parser.add_subparsers(help="sub-command help") # discrete parser_discrete = subparsers.add_parser("discrete", help="Perform a discrete control analysis") parser_discrete.set_defaults(func=main_discrete) # continuous parser_continuous = subparsers.add_parser("continuous", help="Perform a continuous control analysis") parser_continuous.set_defaults(func=main_continuous) args = parser.parse_args() dictConfig({"version": 1, "incremental": True, "root": {"level": args.log_level}}) if args.log_level != "DEBUG": logging.getLogger("pyorganism").setLevel(logging.WARN) try: sys.exit(args.func(args)) except: # we want to catch everything (err, msg, trace) = sys.exc_info() # do something raise_(err, msg, trace) finally: logging.shutdown()
def _test_connect(self, client_versions=None, server_version=None, server_type=None, client_handler_type=None, connection_type=None, compression_threshold=None, private_key=None, public_key_bytes=None, ignore_extra_exceptions=None): if client_versions is None: client_versions = self.client_versions if server_version is None: server_version = self.server_version if server_type is None: server_type = self.server_type if client_handler_type is None: client_handler_type = self.client_handler_type if connection_type is None: connection_type = self.connection_type if compression_threshold is None: compression_threshold = self.compression_threshold if private_key is None: private_key = self.private_key if public_key_bytes is None: public_key_bytes = self.public_key_bytes if ignore_extra_exceptions is None: ignore_extra_exceptions = self.ignore_extra_exceptions server = server_type(minecraft_version=server_version, compression_threshold=compression_threshold, client_handler_type=client_handler_type, private_key=private_key, public_key_bytes=public_key_bytes, test_case=self) addr = "localhost" port = server.listen_socket.getsockname()[1] cond = threading.Condition() server_lock = threading.Lock() server_exc_info = [None] client_lock = threading.Lock() client_exc_info = [None] def handle_client_exception(exc, exc_info): with client_lock: client_exc_info[0] = exc_info with cond: cond.notify_all() client = connection_type( addr, port, username='******', allowed_versions=client_versions, handle_exception=handle_client_exception) client.register_packet_listener( lambda packet: logging.debug('[ ->C] %s' % packet), packets.Packet, early=True) client.register_packet_listener( lambda packet: logging.debug('[C-> ] %s' % packet), packets.Packet, early=True, outgoing=True) server_thread = threading.Thread( name='FakeServer', target=self._test_connect_server, args=(server, cond, server_lock, server_exc_info)) server_thread.daemon = True errors = [] try: try: with cond: server_thread.start() self._start_client(client) cond.wait(THREAD_TIMEOUT_S) finally: # Wait for all threads to exit. server.stop() for thread in server_thread, client.networking_thread: if thread is not None and thread.is_alive(): thread.join(THREAD_TIMEOUT_S) if thread is not None and thread.is_alive(): errors.append({ 'msg': 'Thread "%s" timed out.' % thread.name}) if client_exc_info[0] is None: client_exc_info[0] = client.exc_info except Exception: errors.insert(0, { 'msg': 'Exception in main thread', 'exc_info': sys.exc_info()}) else: timeout = True for lock, [exc_info], thread_name in ( (client_lock, client_exc_info, 'client thread'), (server_lock, server_exc_info, 'server thread') ): with lock: if exc_info is None: continue timeout = False if not issubclass(exc_info[0], FakeServerTestSuccess): errors.insert(0, { 'msg': 'Exception in %s:' % thread_name, 'exc_info': exc_info}) elif ignore_extra_exceptions: del errors[:] break if timeout: errors.insert(0, {'msg': 'Test timed out.'}) if len(errors) > 1: for error in errors: logging.error(**error) self.fail('Multiple errors: see logging output.') elif errors and 'exc_info' in errors[0]: raise_(*errors[0]['exc_info']) elif errors: self.fail(errors[0]['msg'])
def import_cmdset(path, cmdsetobj, emit_to_obj=None, no_logging=False): """ This helper function is used by the cmdsethandler to load a cmdset instance from a python module, given a python_path. It's usually accessed through the cmdsethandler's add() and add_default() methods. path - This is the full path to the cmdset object on python dot-form Args: path (str): The path to the command set to load. cmdsetobj (CmdSet): The database object/typeclass on which this cmdset is to be assigned (this can be also channels and exits, as well as players but there will always be such an object) emit_to_obj (Object, optional): If given, error is emitted to this object (in addition to logging) no_logging (bool, optional): Don't log/send error messages. This can be useful if import_cmdset is just used to check if this is a valid python path or not. Returns: cmdset (CmdSet): The imported command set. If an error was encountered, `commands.cmdsethandler._ErrorCmdSet` is returned for the benefit of the handler. """ python_paths = [path] + ["%s.%s" % (prefix, path) for prefix in _CMDSET_PATHS if not path.startswith(prefix)] errstring = "" for python_path in python_paths: if "." in path: modpath, classname = python_path.rsplit(".", 1) else: raise ImportError("The path '%s' is not on the form modulepath.ClassName" % path) try: # first try to get from cache cmdsetclass = _CACHED_CMDSETS.get(python_path, None) if not cmdsetclass: try: module = import_module(modpath, package="evennia") except ImportError: if len(trace()) > 2: # error in module, make sure to not hide it. exc = sys.exc_info() raise_(exc[1], None, exc[2]) else: # try next suggested path errstring += _("\n(Unsuccessfully tried '%s')." % python_path) continue try: cmdsetclass = getattr(module, classname) except AttributeError: if len(trace()) > 2: # Attribute error within module, don't hide it exc = sys.exc_info() raise_(exc[1], None, exc[2]) else: errstring += _("\n(Unsuccessfully tried '%s')." % python_path) continue _CACHED_CMDSETS[python_path] = cmdsetclass #instantiate the cmdset (and catch its errors) if callable(cmdsetclass): cmdsetclass = cmdsetclass(cmdsetobj) errstring = "" return cmdsetclass except ImportError as e: logger.log_trace() errstring += _("\nError loading cmdset {path}: \"{error}\"") errstring = errstring.format(path=python_path, error=e) break except KeyError: logger.log_trace() errstring += _("\nError in loading cmdset: No cmdset class '{classname}' in {path}.") errstring = errstring.format(classname=classname, path=python_path) break except SyntaxError as e: logger.log_trace() errstring += _("\nSyntaxError encountered when loading cmdset '{path}': \"{error}\".") errstring = errstring.format(path=python_path, error=e) break except Exception as e: logger.log_trace() errstring += _("\nCompile/Run error when loading cmdset '{path}': \"{error}\".") errstring = errstring.format(path=python_path, error=e) break if errstring: # returning an empty error cmdset errstring = errstring.strip() if not no_logging: logger.log_err(errstring) if emit_to_obj and not ServerConfig.objects.conf("server_starting_mode"): emit_to_obj.msg(errstring) err_cmdset = _ErrorCmdSet() err_cmdset.errmessage = errstring + _("\n (See log for details.)") return err_cmdset
def with_traceback(): try: raise ValueError("An error") except Exception as e: _, _, traceback = sys.exc_info() raise_(IOError, str(e), traceback)
def with_value(): raise_(IOError, "This is an error")
def valerror(): try: raise ValueError("Apples!") except Exception as e: raise_(e)
def run(self): try: self.saferun() except Exception as e: import sys _, exception, tb = sys.exc_info() raise_(exception, None, tb)
def raise_custom_exception(): try: raise CustomException(1, "hello") except CustomException: raise_(*sys.exc_info())