def test(): '''Test basic workings of `TempRecursionLimitSetter`.''' old_recursion_limit = sys.getrecursionlimit() assert sys.getrecursionlimit() == old_recursion_limit with TempRecursionLimitSetter(old_recursion_limit + 3): assert sys.getrecursionlimit() == old_recursion_limit + 3 assert sys.getrecursionlimit() == old_recursion_limit
def main(): lock = threading.Lock() with lock: t = threading.Thread(target=foo, args=(lock,)) t.start() sys.getrecursionlimit() # C function: sys_getrecursionlimit() t.join()
def test_accessibility_on_very_deep_graph(): gr = graph() gr.add_nodes(range(0,311)) # 2001 for i in range(0,310): #2000 gr.add_edge((i,i+1)) recursionlimit = getrecursionlimit() accessibility(gr) assert getrecursionlimit() == recursionlimit
def test_dfs_very_deep_graph(self): gr = pygraph.classes.graph.graph() gr.add_nodes(range(0,20001)) for i in range(0,20000): gr.add_edge((i,i+1)) recursionlimit = getrecursionlimit() depth_first_search(gr, 0) assert getrecursionlimit() == recursionlimit
def test_topological_sort_on_very_deep_graph(self): gr = pygraph.classes.graph.graph() gr.add_nodes(list(range(0,20001))) for i in range(0,20000): gr.add_edge((i,i+1)) recursionlimit = getrecursionlimit() topological_sorting(gr) assert getrecursionlimit() == recursionlimit
def test_recursionlimit(self): raises(TypeError, sys.getrecursionlimit, 42) oldlimit = sys.getrecursionlimit() raises(TypeError, sys.setrecursionlimit) raises(ValueError, sys.setrecursionlimit, -42) sys.setrecursionlimit(10000) assert sys.getrecursionlimit() == 10000 sys.setrecursionlimit(oldlimit)
def test_recursionlimit(self): self.assertRaises(TypeError, sys.getrecursionlimit, 42) oldlimit = sys.getrecursionlimit() self.assertRaises(TypeError, sys.setrecursionlimit) self.assertRaises(ValueError, sys.setrecursionlimit, -42) sys.setrecursionlimit(10000) self.assertEqual(sys.getrecursionlimit(), 10000) sys.setrecursionlimit(oldlimit)
def test_accessibility_on_very_deep_graph(self): gr = pygraph.classes.graph.graph() gr.add_nodes(range(0,2001)) for i in range(0,2000): gr.add_edge((i,i+1)) recursionlimit = getrecursionlimit() accessibility(gr) assert getrecursionlimit() == recursionlimit
def test_cut_edges_on_very_deep_graph(self): gr = pygraph.classes.graph.graph() gr.add_nodes(range(0,5001)) for i in range(0,5000): gr.add_edge((i,i+1)) recursionlimit = getrecursionlimit() cut_edges(gr) assert getrecursionlimit() == recursionlimit
def test_recursionlimit(self): import sys raises(TypeError, sys.getrecursionlimit, 42) oldlimit = sys.getrecursionlimit() raises(TypeError, sys.setrecursionlimit) raises(ValueError, sys.setrecursionlimit, -42) sys.setrecursionlimit(10000) assert sys.getrecursionlimit() == 10000 sys.setrecursionlimit(oldlimit) raises(OverflowError, sys.setrecursionlimit, 1<<31)
def test_as_decorator(): '''Test `TempRecursionLimitSetter` when used as a decorator.''' old_recursion_limit = sys.getrecursionlimit() @TempRecursionLimitSetter(1234) def f(): assert sys.getrecursionlimit() == 1234 assert sys.getrecursionlimit() == old_recursion_limit f() assert sys.getrecursionlimit() == old_recursion_limit cute_testing.assert_polite_wrapper(f)
def test_standardOptions(self): """ L{WorkerOptions} supports a subset of standard options supported by trial. """ self.addCleanup(sys.setrecursionlimit, sys.getrecursionlimit()) if gc.isenabled(): self.addCleanup(gc.enable) gc.enable() self.options.parseOptions(["--recursionlimit", "2000", "--disablegc"]) self.assertEqual(2000, sys.getrecursionlimit()) self.assertFalse(gc.isenabled())
def Save(session, filename=None): """ save your session to use it later. Returns the filename of the written file. If not filename is given, a file named `androguard_session_<DATE>.ag` will be created in the current working directory. `<DATE>` is a timestamp with the following format: `%Y-%m-%d_%H%M%S`. This function will overwrite existing files without asking. If the file could not written, None is returned. example:: s = session.Session() session.Save(s, "msession.ag") :param session: A Session object to save :param filename: output filename to save the session :type filename: string """ if not filename: filename = "androguard_session_{:%Y-%m-%d_%H%M%S}.ag".format(datetime.datetime.now()) if os.path.isfile(filename): log.warning("{} already exists, overwriting!") # Setting the recursion limit according to the documentation: # https://docs.python.org/3/library/pickle.html#what-can-be-pickled-and-unpickled # # Some larger APKs require a high recursion limit. # Tested to be above 35000 for some files, setting to 50k to be sure. # You might want to set this even higher if you encounter problems reclimit = sys.getrecursionlimit() sys.setrecursionlimit(50000) saved = False try: with open(filename, "wb") as fd: pickle.dump(session, fd) saved = True except RecursionError: log.exception("Recursion Limit hit while saving. " "Current Recursion limit: {}. " "Please report this error!".format(sys.getrecursionlimit())) # Remove partially written file os.unlink(filename) sys.setrecursionlimit(reclimit) return filename if saved else None
def _nestingTest(self, nestedObject, expected): limit = sys.getrecursionlimit() sys.setrecursionlimit(100) try: self.assertStringEqual(self.flatten(nestedObject), expected) finally: sys.setrecursionlimit(limit)
def calculate_acc_index(dir_arr, origin_upper_left=True): # modify maximum recursion depth if required import sys rec_depth = sys.getrecursionlimit() sys.setrecursionlimit(max(dir_arr.shape[0] * dir_arr.shape[1], rec_depth)) acc_index = np.ones(dir_arr.shape) cache = -np.ones(dir_arr.shape) n1, n2 = acc_index.shape for i in range(n1): if i % 100 == 0: print("{}/{} ...".format(i, n1)) for j in range(n2): acc_index[i, j] = calculate_acc_index_for_point(i, j, dir_arr, cache, origin_upper_left=origin_upper_left) # print(acc_index.min(), acc_index.max()) return acc_index
def test_setrecursionlimit_recursion_depth(self): # Issue #25274: Setting a low recursion limit must be blocked if the # current recursion depth is already higher than the "lower-water # mark". Otherwise, it may not be possible anymore to # reset the overflowed flag to 0. from _testcapi import get_recursion_depth def set_recursion_limit_at_depth(depth, limit): recursion_depth = get_recursion_depth() if recursion_depth >= depth: with self.assertRaises(RecursionError) as cm: sys.setrecursionlimit(limit) self.assertRegex(str(cm.exception), "cannot set the recursion limit to [0-9]+ " "at the recursion depth [0-9]+: " "the limit is too low") else: set_recursion_limit_at_depth(depth, limit) oldlimit = sys.getrecursionlimit() try: sys.setrecursionlimit(1000) for limit in (10, 25, 50, 75, 100, 150, 200): # formula extracted from _Py_RecursionLimitLowerWaterMark() if limit > 200: depth = limit - 50 else: depth = limit * 3 // 4 set_recursion_limit_at_depth(depth, limit) finally: sys.setrecursionlimit(oldlimit)
def tag_molecules(struct): """ Sets the ``marked`` attribute of every Atom in struct to the molecule number it is a part of. If no bonds are present, every atom is its own molecule. Parameters ---------- struct : :class:`parmed.Structure` Input structure to tag the molecules for """ # Make sure our recursion limit is large enough, but never shrink it from sys import setrecursionlimit, getrecursionlimit setrecursionlimit(max(len(struct.atoms), getrecursionlimit())) if not struct.bonds: for i, atom in enumerate(struct.atoms): atom.marked = i + 1 return # We do have bonds, this is the interesting part struct.atoms.unmark() mol_id = 1 for atom in struct.atoms: if atom.marked: continue atom.marked = mol_id _set_owner(atom, mol_id) mol_id += 1
def blowstack(fxn, arg, compare_to): # Make sure that calling isinstance with a deeply nested tuple for its # argument will raise RuntimeError eventually. tuple_arg = (compare_to,) for cnt in xrange(sys.getrecursionlimit()+5): tuple_arg = (tuple_arg,) fxn(arg, tuple_arg)
def main(): max_recursion = sys.getrecursionlimit() / 4 if len(sys.argv) < 2: n = 250 else: n = int(sys.argv[1]) print n / max_recursion for i in range(n / max_recursion): c = (i+1)*max_recursion # print i, c f(c) catalan(c) print_fac(n) n = 100 for i in range(n / max_recursion): c = (i+1)*max_recursion catalan(c) for i in range(n): print(catalan(i)) print catalan(n) print "="*20 for i in range(30): print i, ci(i)
def test_main(type="short"): oldRecursionDepth = sys.getrecursionlimit() try: sys.setrecursionlimit(1001) t0 = clock() import b0 import b1 import b2 import b3 import b4 import b5 import b6 print 'import time = %.2f' % (clock()-t0) tests = [b0,b1,b2,b3,b4,b5,b6] N = { "short" : 1, "full" : 1, "medium" : 2, "long" : 4 }[type] results = {} t0 = clock() for i in range(N): for test in tests: ts0 = clock() test.main() tm = (clock()-ts0) results.setdefault(test, []).append(tm) print '%.2f sec running %s' % ( tm, test.__name__) for test in tests: print '%s = %f -- %r' % (test.__name__, sum(results[test])/N, results[test]) print 'all done in %.2f sec' % (clock()-t0) finally: sys.setrecursionlimit(oldRecursionDepth)
def printSmiles(self, smiles): self.limit = sys.getrecursionlimit() sys.setrecursionlimit(10000); self.memo = {} retval = self._printSmiles(smiles, 1, 1) sys.setrecursionlimit(self.limit) return retval
def test_create(self): '''construct a group from an object instance''' # get a useful backtrace # must restore recursion limit or later tests will be VERY odd with temp_set(sys.setrecursionlimit, 35, sys.getrecursionlimit()): ga = GroupAlias({})
def test_tco_decorator(self): def recur_accumulate(origin, f=operator.add, acc=0): n = next(origin, None) if n is None: return acc return recur_accumulate(origin, f, f(acc, n)) # this works normally self.assertEqual(10, recur_accumulate(iter(range(5)))) limit = sys.getrecursionlimit() * 10 # such count of recursive calls should fail on CPython, # for PyPy we skip this test cause on PyPy the limit is # approximative and checked at a lower level if not hasattr(sys, 'pypy_version_info'): self.assertRaises(RuntimeError, recur_accumulate, iter(range(limit))) # with recur decorator it should run without problems @recur.tco def tco_accumulate(origin, f=operator.add, acc=0): n = next(origin, None) if n is None: return False, acc return True, (origin, f, f(acc, n)) self.assertEqual(sum(range(limit)), tco_accumulate(iter(range(limit))))
def calculate_n_mer_significances(seqs, n, background=None): ''' Counts all n-mers in the sequences and assesses the significance of each count w.r.t. the background_model If the background model is not specified, a uniform distribution over the bases is assumed ''' from sys import getrecursionlimit all = _AllNMers() hmm.count_mers(seqs, n, all) collapsed = collapse_rev_comps(all.n_mers) log_fact = _LogFactorial() total_counts = sum(count for mer, count in collapsed) for i in xrange(1,total_counts,getrecursionlimit()/2): log_fact[i] log_fact_total = log_fact[total_counts] if None == background: background_LL = n * math.log(.25) foreground_LL = math.log(1.0 - math.exp(background_LL)) result = [] for mer, count in collapsed: if None != background: background_LL = background.LL(mer) foreground_LL = math.log(1.0 - math.exp(background_LL)) log_bernoulli = ( log_fact_total - log_fact[count] - log_fact[total_counts-count] + count * background_LL + (total_counts-count) * foreground_LL ) result.append((mer, count, log_bernoulli)) result.sort(cmp=lambda x,y: cmp(x[2], y[2])) return result
def __init__(self, G1, G2): """Initialize GraphMatcher. Suppose G1 and G2 are undirected graphs. >>> G1=nx.path_graph(4) >>> G2=nx.path_graph(4) >>> GM = nx.GraphMatcher(G1,G2) creates a GraphMatcher which only checks for syntactic feasibility. """ self.G1 = G1 self.G2 = G2 # Set recursion limit. self.old_recursion_limit = sys.getrecursionlimit() expected_max_recursion_level = len(self.G2) if self.old_recursion_limit < 1.5 * expected_max_recursion_level: # Give some breathing room. sys.setrecursionlimit(int(1.5 * expected_max_recursion_level)) # Declare that we will be searching for a graph-graph isomorphism. self.test = 'graph' # Initialize the isomorphism mapping. self.state = GMState(self)
def recursion_limit(n): """Context manager that temporarily sets Python's recursion limit to 'n', and restores the previous recursion limit when the context is exited.""" m = sys.getrecursionlimit() sys.setrecursionlimit(n) yield sys.setrecursionlimit(m)
def _eval_(self, x): """ EXAMPLES:: sage: [dickman_rho(n) for n in [1..10]] [1.00000000000000, 0.306852819440055, 0.0486083882911316, 0.00491092564776083, 0.000354724700456040, 0.0000196496963539553, 8.74566995329392e-7, 3.23206930422610e-8, 1.01624828273784e-9, 2.77017183772596e-11] sage: dickman_rho(0) 1.00000000000000 """ if not is_RealNumber(x): try: x = RR(x) except (TypeError, ValueError): return None # PrimitiveFunction.__call__(self, SR(x)) if x < 0: return x.parent()(0) elif x <= 1: return x.parent()(1) elif x <= 2: return 1 - x.log() n = x.floor() if self._cur_prec < x.parent().prec() or n not in self._f: self._cur_prec = rel_prec = x.parent().prec() # Go a bit beyond so we're not constantly re-computing. max = x.parent()(1.1) * x + 10 abs_prec = (-self.approximate(max).log2() + rel_prec + 2 * max.log2()).ceil() self._f = {} if sys.getrecursionlimit() < max + 10: sys.setrecursionlimit(int(max) + 10) self._compute_power_series(max.floor(), abs_prec, cache_ring=x.parent()) return self._f[n](2 * (x - n - x.parent()(0.5)))
def test_repr_deep(self): d = self._empty_mapping() for i in range(sys.getrecursionlimit() + 100): d0 = d d = self._empty_mapping() d[1] = d0 self.assertRaises(RuntimeError, repr, d)
def __init__(self, G1, G2): """Initialize GraphMatcher. Parameters ---------- G1,G2: NetworkX Graph or MultiGraph instances. The two graphs to check for isomorphism. Examples -------- To create a GraphMatcher which checks for syntactic feasibility: >>> G1 = nx.path_graph(4) >>> G2 = nx.path_graph(4) >>> GM = nx.GraphMatcher(G1,G2) """ self.G1 = G1 self.G2 = G2 self.G1_nodes = set(G1.nodes()) self.G2_nodes = set(G2.nodes()) # Set recursion limit. self.old_recursion_limit = sys.getrecursionlimit() expected_max_recursion_level = len(self.G2) if self.old_recursion_limit < 1.5 * expected_max_recursion_level: # Give some breathing room. sys.setrecursionlimit(int(1.5 * expected_max_recursion_level)) # Declare that we will be searching for a graph-graph isomorphism. self.test = 'graph' # Initialize state self.initialize()
def test_pickle(self, net_fitted, X_test, y_pred): recursionlimit = sys.getrecursionlimit() sys.setrecursionlimit(10000) pickled = pickle.dumps(net_fitted, -1) net_loaded = pickle.loads(pickled) assert np.array_equal(net_loaded.predict(X_test), y_pred) sys.setrecursionlimit(recursionlimit)
def __init__(self, args): self.args = args self.display = Display("info_%s.log" % escape(args.bookid)) self.display.intro() self.session = requests.Session() if USE_PROXY: # DEBUG self.session.proxies = PROXIES self.session.verify = False self.session.headers.update(self.HEADERS) self.jwt = {} if not args.cred: if not os.path.isfile(COOKIES_FILE): self.display.fatal( "Login: unable to find `cookies.json` file.\n" " Please use the `--cred` or `--login` options to perform the login." ) self.session.cookies.update(json.load(open(COOKIES_FILE))) else: self.display.info("Logging into Safari Books Online...", state=True) self.do_login(*args.cred) if not args.no_cookies: json.dump(self.session.cookies.get_dict(), open(COOKIES_FILE, 'w')) self.check_login() self.book_id = args.bookid self.api_url = self.API_TEMPLATE.format(self.book_id) self.display.info("Retrieving book info...") self.book_info = self.get_book_info() self.display.book_info(self.book_info) self.display.info("Retrieving book chapters...") self.book_chapters = self.get_book_chapters() self.chapters_queue = self.book_chapters[:] if len(self.book_chapters) > sys.getrecursionlimit(): sys.setrecursionlimit(len(self.book_chapters)) self.book_title = self.book_info["title"] self.base_url = self.book_info["web_url"] self.clean_book_title = "".join(self.escape_dirname(self.book_title).split(",")[:2]) \ + " ({0})".format(self.book_id) books_dir = os.path.join(PATH, "Books") if not os.path.isdir(books_dir): os.mkdir(books_dir) self.BOOK_PATH = os.path.join(books_dir, self.clean_book_title) self.display.set_output_dir(self.BOOK_PATH) self.css_path = "" self.images_path = "" self.create_dirs() self.chapter_title = "" self.filename = "" self.chapter_stylesheets = [] self.css = [] self.images = [] self.display.info("Downloading book contents... (%s chapters)" % len(self.book_chapters), state=True) self.BASE_HTML = self.BASE_01_HTML + ( self.KINDLE_HTML if not args.kindle else "") + self.BASE_02_HTML self.cover = False self.get() if not self.cover: self.cover = self.get_default_cover() cover_html = self.parse_html( html.fromstring( "<div id=\"sbo-rt-content\"><img src=\"Images/{0}\"></div>" .format(self.cover)), True) self.book_chapters = [{ "filename": "default_cover.xhtml", "title": "Cover" }] + self.book_chapters self.filename = self.book_chapters[0]["filename"] self.save_page_html(cover_html) self.css_done_queue = Queue( 0) if "win" not in sys.platform else WinQueue() self.display.info("Downloading book CSSs... (%s files)" % len(self.css), state=True) self.collect_css() self.images_done_queue = Queue( 0) if "win" not in sys.platform else WinQueue() self.display.info("Downloading book images... (%s files)" % len(self.images), state=True) self.collect_images() self.display.info("Creating EPUB file...", state=True) self.create_epub() if not args.no_cookies: json.dump(self.session.cookies.get_dict(), open(COOKIES_FILE, "w")) self.display.done(os.path.join(self.BOOK_PATH, self.book_id + ".epub")) self.display.unregister() if not self.display.in_error and not args.log: os.remove(self.display.log_file)
import sys default_limit = sys.getrecursionlimit() print("Default limit:", default_limit) sys.setrecursionlimit(2000) updated_limit = sys.getrecursionlimit() print("Updated limit:", updated_limit)
def _set_recursion_limit(self): old_reclimit = sys.getrecursionlimit() new_reclimit = 10 * len(self._possible_edges) if new_reclimit > old_reclimit: sys.setrecursionlimit(new_reclimit)
1 / 0 except ZeroDivisionError as exc: exc_info = sys.exc_info() assert exc_info[0] == type(exc) == ZeroDivisionError assert exc_info[1] == exc # Recursion: def recursive_call(n): if n > 0: recursive_call(n - 1) sys.setrecursionlimit(200) assert sys.getrecursionlimit() == 200 with assert_raises(RecursionError): recursive_call(300) if sys.platform.startswith("win"): winver = sys.getwindowsversion() print(f'winver: {winver} {winver.platform_version}') # the biggest value of wSuiteMask (https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-osversioninfoexa#members). all_masks = 0x00000004 | 0x00000400 | 0x00004000 | 0x00000080 | 0x00000002 | 0x00000040 | 0x00000200 | \ 0x00000100 | 0x00000001 | 0x00000020 | 0x00002000 | 0x00000010 | 0x00008000 | 0x00020000 # We really can't test if the results are correct, so it just checks for meaningful value assert winver.major > 0 assert winver.minor >= 0
def test_repr_deep(self): d = {} for i in range(sys.getrecursionlimit() + 100): d = {1: d} self.assertRaises(RecursionError, repr, d)
def main(): parser = argparse.ArgumentParser( description='Process changed modules in a git repo') parser.add_argument('--config-path', type=str, default=os.environ['YANGCATALOG_CONFIG_PATH'], help='Set path to config file') args = parser.parse_args() config_path = args.config_path config = create_config(config_path) log_directory = config.get('Directory-Section', 'logs') yang_models = config.get('Directory-Section', 'yang-models-dir') changes_cache_path = config.get('Directory-Section', 'changes-cache') failed_changes_cache_path = config.get('Directory-Section', 'changes-cache-failed') delete_cache_path = config.get('Directory-Section', 'delete-cache') lock_file = config.get('Directory-Section', 'lock') lock_file_cron = config.get('Directory-Section', 'lock-cron') json_ytree = config.get('Directory-Section', 'json-ytree') save_file_dir = config.get('Directory-Section', 'save-file-dir') threads = int(config.get('General-Section', 'threads')) LOGGER = log.get_logger( 'process_changed_mods', os.path.join(log_directory, 'process-changed-mods.log')) LOGGER.info('Starting process-changed-mods.py script') if os.path.exists(lock_file) or os.path.exists(lock_file_cron): # we can exist since this is run by cronjob every 3 minutes of every day LOGGER.warning( 'Temporary lock file used by something else. Exiting script !!!') sys.exit() try: open(lock_file, 'w').close() open(lock_file_cron, 'w').close() except Exception: os.unlink(lock_file) os.unlink(lock_file_cron) LOGGER.error( 'Temporary lock file could not be created although it is not locked' ) sys.exit() changes_cache = load_changes_cache(changes_cache_path) delete_cache = load_delete_cache(delete_cache_path) if not changes_cache and not delete_cache: LOGGER.info('No new modules are added or removed. Exiting script!!!') os.unlink(lock_file) os.unlink(lock_file_cron) sys.exit() LOGGER.info('Pulling latest YangModels/yang repository') repoutil.pull(yang_models) LOGGER.info('Trying to initialize Elasticsearch indices') es_manager = ESManager() for index in ESIndices: if not es_manager.index_exists(index): es_manager.create_index(index) logging.getLogger('elasticsearch').setLevel(logging.ERROR) LOGGER.info('Running cache files backup') backup_cache_files(delete_cache_path) backup_cache_files(changes_cache_path) os.unlink(lock_file) if delete_cache: for module in delete_cache: name, rev_org = module.split('@') revision, organization = rev_org.split('/') revision = validate_revision(revision) module = { 'name': name, 'revision': revision, 'organization': organization } es_manager.delete_from_indices(module) if changes_cache: recursion_limit = sys.getrecursionlimit() sys.setrecursionlimit(50000) x = 0 try: for module_key, module_path in changes_cache.items(): x += 1 name, rev_org = module_key.split('@') revision, organization = rev_org.split('/') revision = validate_revision(revision) name_revision = '{}@{}'.format(name, revision) module = { 'name': name, 'revision': revision, 'organization': organization, 'path': module_path } LOGGER.info('yindex on module {}. module {} out of {}'.format( name_revision, x, len(changes_cache))) check_file_availability(module, LOGGER) try: build_indices(es_manager, module, save_file_dir, json_ytree, threads, LOGGER) except Exception: LOGGER.exception( 'Problem while processing module {}'.format( module_key)) try: with open(failed_changes_cache_path, 'r') as reader: failed_modules = json.load(reader) except (FileNotFoundError, json.decoder.JSONDecodeError): failed_modules = {} if module_key not in failed_modules: failed_modules[module_key] = module_path with open(failed_changes_cache_path, 'w') as writer: json.dump(failed_modules, writer) except Exception: sys.setrecursionlimit(recursion_limit) os.unlink(lock_file_cron) LOGGER.exception('Error while running build_yindex.py script') LOGGER.info('Job failed execution') sys.exit() sys.setrecursionlimit(recursion_limit) os.unlink(lock_file_cron) LOGGER.info('Job finished successfully')
def explain_exception(exc, depth=16): """ Method to take an exception and translate the Python internal traceback into a list of the pyparsing expressions that caused the exception to be raised. Parameters: - exc - exception raised during parsing (need not be a ParseException, in support of Python exceptions that might be raised in a parse action) - depth (default=16) - number of levels back in the stack trace to list expression and function names; if None, the full stack trace names will be listed; if 0, only the failing input line, marker, and exception string will be shown Returns a multi-line string listing the ParserElements and/or function names in the exception's stack trace. Note: the diagnostic output will include string representations of the expressions that failed to parse. These representations will be more helpful if you use `setName` to give identifiable names to your expressions. Otherwise they will use the default string forms, which may be cryptic to read. """ import inspect from .core import ParserElement if depth is None: depth = sys.getrecursionlimit() ret = [] if isinstance(exc, ParseBaseException): ret.append(exc.line) ret.append(" " * (exc.col - 1) + "^") ret.append("{}: {}".format(type(exc).__name__, exc)) if depth > 0: callers = inspect.getinnerframes(exc.__traceback__, context=depth) seen = set() for i, ff in enumerate(callers[-depth:]): frm = ff[0] f_self = frm.f_locals.get("self", None) if isinstance(f_self, ParserElement): if frm.f_code.co_name not in ("parseImpl", "_parseNoCache"): continue if id(f_self) in seen: continue seen.add(id(f_self)) self_type = type(f_self) ret.append("{}.{} - {}".format(self_type.__module__, self_type.__name__, f_self)) elif f_self is not None: self_type = type(f_self) ret.append("{}.{}".format(self_type.__module__, self_type.__name__)) else: code = frm.f_code if code.co_name in ("wrapper", "<module>"): continue ret.append("{}".format(code.co_name)) depth -= 1 if not depth: break return "\n".join(ret)
def get_recursion_limit(self): ''' Get the current value of the recursion limit. ''' return sys.getrecursionlimit( ) # Return the current value of the recursion value
items = list(L) while items: front = items.pop(0) if not isinstance(front, list): tot += front else: items[:0] = front return tot sumtree2(L) ## checking and setting recursive deph import sys sys.getrecursionlimit() sys.setrecursionlimit(10000) ## first class object model in Python ## adding user-defined attributes to functions func.count = 1 ### annotations # only for def def func(a: int = 1, b: 'spam' = 4) -> int: return (a + b) for arg in func.__annotations__: print(arg, "=>", func.__annotations__[arg])
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """ import sys sys.setrecursionlimit(4 * sys.getrecursionlimit()) import json import os import warnings import imageio import numpy as np import matplotlib matplotlib.use('QT5Agg') from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QComboBox, QPushButton, QLineEdit, QLabel, QCheckBox from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt from matplotlib.backends.backend_qt5agg import (FigureCanvasQTAgg as FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
import random import sys from queue import Queue n = 12 m = 15 # n = 4 # m = 4 print("n = ", n, "m = ", m) SIZE = (n, m) # n rows, m columns if sys.getrecursionlimit() < SIZE[0] * SIZE[1]: sys.setrecursionlimit(SIZE[0] * SIZE[1]) # if max recursion limit is lower than needed, adjust it sys.setrecursionlimit(30000) my_list = [0] * 55 + [1] * 45 # weights # initializing labyrinth with 0s and 1s at weighted random lab = list( list(random.choice(my_list) for i in range(SIZE[0])) for j in range(SIZE[1])) # lab = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] # parameters: start, destination def print_labyrinth(xs, ys, xd, yd): print(" ", end='') for i in range(n): if i < n - 1:
class APNSFeedbackWrapper(object): """ This object wrap Apple Push Notification Feedback Service tuples. Object support for iterations and may work with routine cycles like for. """ sandbox = True apnsHost = 'feedback.push.apple.com' apnsSandboxHost = 'feedback.sandbox.push.apple.com' apnsPort = 2196 feedbacks = None connection = None testingParser = False blockSize = 1024 # default size of SSL reply block is 1Kb feedbackHeaderSize = 6 enlargeRecursionLimit = lambda self: \ sys.setrecursionlimit(sys.getrecursionlimit() + 100) _currentTuple = 0 _tuplesCount = 0 def __init__(self, certificate=None, sandbox=True, \ force_ssl_command=False, debug_ssl=False, \ passphrase=None): self.debug_ssl = debug_ssl self.force_ssl_command = False self.connection = APNSConnection(certificate=certificate, \ force_ssl_command=self.force_ssl_command, \ debug=self.debug_ssl, \ passphrase=passphrase) self.sandbox = sandbox self.feedbacks = [] self._currentTuple = 0 self._tuplesCount = 0 def __iter__(self): return self def next(self): if self._currentTuple >= self._tuplesCount: raise StopIteration obj = self.feedbacks[self._currentTuple] self._currentTuple += 1 return obj def _parse_reply(self, reply): flag = True offset = 0 while (flag): try: feedbackTime, tokenLength = struct.unpack_from(\ '!lh', reply, offset) deviceToken = struct.unpack_from(\ '%ds' % tokenLength, reply, offset + 6)[0] offset += 6 + len(deviceToken) self._append(feedbackTime, deviceToken) except: flag = False def tuples(self): """ This method return a list with all received deviceTokens: ( datetime, deviceToken ) """ return self.feedbacks def _append(self, fTime, token): self.feedbacks.append((datetime.datetime.fromtimestamp(fTime), token)) self._tuplesCount = len(self.feedbacks) def _parseHeader(self, Buff): """ Parse header of Feedback Service tuple. Format of Buff is |xxxx|yy|zzzzzzzz| where: x is time_t (UNIXTIME, long, 4 bytes) y is length of z (two bytes) z is device token """ try: feedbackTime, tokenLength = struct.unpack_from('!lh', Buff, 0) if Buff >= self.feedbackHeaderSize + tokenLength: recoursiveInvoke = lambda: self._parseTuple(\ feedbackTime, tokenLength, Buff[self.feedbackHeaderSize:]) # enlarge recursion limit if it is exceeded try: return recoursiveInvoke() except RuntimeError: self.enlargeRecursionLimit() return recoursiveInvoke() else: return Buff except: return Buff def _parseTuple(self, tTime, tLen, Buff): """ Get body by length tLen of current Feedback Service tuple. If body length is equal to tLen than append new tuple item and recoursive parse next item. """ try: token = struct.unpack_from('!%ds' % tLen, Buff, 0)[0] self._append(tTime, token) except: pass recurrenceInvoke = lambda: self._parseHeader(Buff[tLen:]) # enlarge recursion limit if it is exceeded try: return recurrenceInvoke() except RuntimeError: self.enlargeRecursionLimit() return recurrenceInvoke() def _testFeedbackFile(self): fh = open('feedbackSampleTuple.dat', 'r') return fh def receive(self): """ Receive Feedback tuples from APNS: 1) make connection to APNS server and receive 2) unpack feedback tuples to arrays """ apnsConnection = self.connection if self.sandbox != True: apnsHost = self.apnsHost else: apnsHost = self.apnsSandboxHost apnsConnection.connect(apnsHost, self.apnsPort) tRest = None blockSize = self.blockSize # replace connectionContext to similar I/O function but work # with binary Feedback Service sample file if self.testingParser: connectionContext = self._testFeedbackFile() replyBlock = apnsConnection.read(blockSize) while replyBlock: if tRest and len(tRest) > 0: # merge previous rest of replyBlock and new replyBlock = struct.pack('!%ds%ds' % (\ len(tRest), len(replyBlock)), tRest, replyBlock) tRest = self._parseHeader(replyBlock) replyBlock = apnsConnection.read(blockSize) # close sample binary file if self.testingParser: connectionContext.close() apnsConnection.close() return True
''' This code is for a problem with finding minimum element in the given range with point updates as well as range updates- without lazy propagation implemented. ''' # If you get a recursion depth error feel free to update the multiplier, currently it is set to 10^5 . import sys sys.setrecursionlimit(sys.getrecursionlimit() * 100) import math # Function can take any built in value {min,max,sum} or any user defined function. # It must take an array of two elements as input and provide a single output def buildTree(Tree, arr, start, end, nodeIndex, debug, function=min): if start == end: Tree[nodeIndex] = arr[start] return mid = (start + end) // 2 buildTree(Tree, arr, start, mid, 2 * nodeIndex + 1, debug) buildTree(Tree, arr, mid + 1, end, 2 * nodeIndex + 2, debug) Tree[nodeIndex] = function( [Tree[2 * nodeIndex + 1], Tree[2 * nodeIndex + 2]]) return def query(Tree, nodeStart, nodeEnd, queryStart, queryEnd,
'''Write a Python program to get the current value of the recursion limit.''' import sys print("Current value of the recursion limit:") print(sys.getrecursionlimit())
import sys, re, os, signal import subprocess if os.name == "posix": import resource from copy import deepcopy from select import select from time import time from queue import Queue, Empty from threading import Thread # This is needed so that the recursive SMT2 S-expression parser # does not run out of stack frames when parsing large expressions if os.name == "posix": smtio_reclimit = 64 * 1024 if sys.getrecursionlimit() < smtio_reclimit: sys.setrecursionlimit(smtio_reclimit) current_rlimit_stack = resource.getrlimit(resource.RLIMIT_STACK) if current_rlimit_stack[0] != resource.RLIM_INFINITY: smtio_stacksize = 128 * 1024 * 1024 if os.uname().sysname == "Darwin": # MacOS has rather conservative stack limits smtio_stacksize = 8 * 1024 * 1024 if current_rlimit_stack[1] != resource.RLIM_INFINITY: smtio_stacksize = min(smtio_stacksize, current_rlimit_stack[1]) if current_rlimit_stack[0] < smtio_stacksize: try: resource.setrlimit(resource.RLIMIT_STACK, (smtio_stacksize, current_rlimit_stack[1])) except ValueError: # couldn't get more stack, just run with what we have
#!/usr/bin/env python3 import sys print("Current recursion limit is {}.".format(sys.getrecursionlimit())) limit = 200 print("Setting recursion limit to {}.".format(limit)) sys.setrecursionlimit(limit) def get_depth(n): """ print depth of current recursion. """ print(n, end=" ") get_depth(n + 1) get_depth(1)
import sys rec_depth = 0 def fac(n): global rec_depth if n == 0: return 1 rec_depth += 1 return n * fac(n - 1) print(fac(5)) print("Глубина рекурсии: " + str(rec_depth - 1)) print("Глубина рекурсии по getrecursionlimit: " + str(sys.getrecursionlimit()))
# 40 - RECURSION # Recursion = A function that calls itself import sys print(sys.getrecursionlimit()) # recursion limit is 1000 by default sys.setrecursionlimit(2000) # change the limit to 2000 i = 0 def rec(): global i i += 1 print('hello', i) rec() rec()
def test_repr_deep(self): a = self.type2test([]) for i in range(sys.getrecursionlimit() + 100): a = self.type2test([a]) self.assertRaises(RecursionError, repr, a)
#!/usr/bin/env python # -*- coding:utf-8 -*- import sys print("系统最大递归深度", sys.getrecursionlimit()) # 阶乘--递归 def factorial(n): if n == 1: return 1 else: return n * factorial(n - 1) # 阶乘-- 迭代 def factorial1(n): result1 = n for i in range(1, n): result1 *= i return result1 number = int(input("请输入一个正整数:")) while number != 0: result = factorial1(number) print("%d 的阶乘是 %d (通过迭代实现)" % (number, result)) result = factorial1(number) print("%d 的阶乘是 %d (通过递归实现)" % (number, result))
def wordladder(self, start, end): """wordladder: Traverses our adjacency graph with O(n^2*m) """ # start with our word in our graph first_node = self.graph[start] first_node.visited = True try: if start == self.path[0]: first_node.visited = False print('BACK TO FIRST NODE') return else: print('APPENDING ' + str(start)) self.path.append(start) except IndexError: print('INDEXERROR, APPENDING FIRST') self.path.append(start) # if start is end, exit if start == end: self.found = True self.path.append(end) return # look for words that are up to likeness_dict4, likeness_dict3, likeness_dict2, likeness_dict1 = dict( ), dict(), dict(), dict() for neighbor in first_node.neighbors: next_diff = self.word_likeness(end, neighbor) if next_diff == len(start) - 3: likeness_dict1[neighbor] = next_diff elif next_diff == len(start) - 2: likeness_dict2[neighbor] = next_diff elif next_diff == len(start) - 1: likeness_dict3[neighbor] = next_diff elif next_diff == len(start): likeness_dict4[neighbor] = next_diff for word in likeness_dict4: if not self.graph[word].visited and not self.found: sys.setrecursionlimit(sys.getrecursionlimit() + 1) print('WIN ' + str(len(start)) + ' ' + word) self.wordladder(word, end) if self.found: return else: print('removing ' + str(self.path.pop())) for word in likeness_dict3: if not self.graph[word].visited and not self.found: sys.setrecursionlimit(sys.getrecursionlimit() + 1) print('GOOD MATCH ' + str(len(start)) + ' ' + word) self.wordladder(word, end) if self.found: return else: print('removing ' + str(self.path.pop())) for word in likeness_dict2: if not self.graph[word].visited and not self.found: sys.setrecursionlimit(sys.getrecursionlimit() + 1) print('OK MATCH ' + str(len(start)) + ' ' + word) self.wordladder(word, end) if self.found: return else: print('removing ' + str(self.path.pop())) for word in likeness_dict1: if not self.graph[word].visited and not self.found: sys.setrecursionlimit(sys.getrecursionlimit() + 1) print('MEH MATCH ' + str(len(start)) + ' ' + word) self.wordladder(word, end) if self.found: return else: print('removing ' + str(self.path.pop()))
def download(self, args): self.display.info("Retrieving book info...") self.book_info = self.get_book_info() self.display.book_info(self.book_info) self.display.info("Retrieving book chapters...") self.book_chapters = self.get_book_chapters() self.chapters_queue = self.book_chapters[:] if len(self.book_chapters) > sys.getrecursionlimit(): sys.setrecursionlimit(len(self.book_chapters)) self.book_title = self.book_info["title"] self.base_url = self.book_info["web_url"] self.clean_book_title = "".join(self.escape_dirname(self.book_title).split(",")[:2]) \ + " ({0})".format(self.book_id) books_dir = os.path.join(PATH, "Books") if not os.path.isdir(books_dir): os.mkdir(books_dir) self.BOOK_PATH = os.path.join(books_dir, self.clean_book_title) self.css_path = "" self.images_path = "" self.create_dirs() self.display.info("Output directory:\n %s" % self.BOOK_PATH) self.chapter_title = "" self.filename = "" self.css = [] self.images = [] self.display.info("Downloading book contents... (%s chapters)" % len(self.book_chapters), state=True) self.BASE_HTML = self.BASE_01_HTML + ( self.KINDLE_HTML if not args.no_kindle else "") + self.BASE_02_HTML self.cover = False self.get() if not self.cover: self.cover = self.get_default_cover() cover_html = self.parse_html( html.fromstring( "<div id=\"sbo-rt-content\"><img src=\"Images/{0}\"></div>" .format(self.cover)), True) self.book_chapters = [{ "filename": "default_cover.xhtml", "title": "Cover" }] + self.book_chapters self.filename = self.book_chapters[0]["filename"] self.save_page_html(cover_html) self.css_done_queue = Queue( 0) if "win" not in sys.platform else WinQueue() self.display.info("Downloading book CSSs... (%s files)" % len(self.css), state=True) self.collect_css() self.images_done_queue = Queue( 0) if "win" not in sys.platform else WinQueue() self.display.info("Downloading book images... (%s files)" % len(self.images), state=True) self.collect_images() self.display.info("Creating EPUB file...", state=True) self.create_epub() if not args.no_cookies: json.dump(self.cookies, open(COOKIES_FILE, "w")) self.display.done(os.path.join(self.BOOK_PATH, self.book_id + ".epub")) self.display.unregister() if args.mobi: self.display.info("Creating MOBI file...", state=True) self.convert_mobi() if not self.display.in_error and not args.log: os.remove(self.display.log_file) return os.path.join(self.BOOK_PATH, self.book_id + ".epub")
# Make sure we can recurse at least 900 times on the three different types # of stacks that we have: import sys TEST_DEPTH = sys.getrecursionlimit() - 20 def recurse(n): if n > 0: return recurse(n - 1) return n print "Recursing on main thread..." recurse(TEST_DEPTH) print "Recursing in a generator..." def gen(): yield recurse(TEST_DEPTH) print list(gen()) print "Recursing in a thread..." from thread import start_new_thread import time done = 0
def __init__(self, limit): self.limit = limit self.old_limit = sys.getrecursionlimit()
result= (lambdak: functools.reduce(int.__mul__,range(1,k+1),1))(3) print(result) #-> 6 找到列表中出现最频繁的数 # 利用 list 的 count 函数 list1 = [1,2,3,4,2,2,3,1,4,4,4] print(max(set(list1),key=list1.count)) # -> 4 重置递归限制 # Python 限制递归次数到 1000,我们可以重置这个值 # 请只在必要的时候采用。 import sys x = 1001 print(sys.getrecursionlimit()) # -> 1000 sys.setrecursionlimit(x) print(sys.getrecursionlimit()) # -> 1001 检查一个对象的内存使用 # 在 Python 2.7 中,一个 32 比特的整数占用 24 字节,在 Python 3.5 中利用 28 字节。为确定内存使用,我们可以调用 getsizeof 方法: # 在 Python 2.7 中 import sys x=1 print(sys.getsizeof(x)) # -> 24 # 在 Python 3.5 中 import sys
np.array(m1_mfts), \ np.array(m2_mfts), \ np.array(mp_pfts) mpairs = model.get_mpairs(m1_efts + m2_efts + [m1_mfts, m2_mfts, mp_pfts]) for mp, (am, cm) in zip(mpairs, pairs): s.mpairs[am][cm] = mp print("Feature extracted - %.2fs\n" % timer.end('feature_extraction')) # Model evaluation print('\nEvaluating trained model') model.decode_clusters([s.reset() for s in Sall]) p, r, f = BCubeEvaluator().evaluate_documents([s.gCs for s in Strn], [s.aCs for s in Strn]) print('Trn - %.4f/%.4f/%.4f' % (p, r, f)) p, r, f = BCubeEvaluator().evaluate_documents([s.gCs for s in Sdev], [s.aCs for s in Sdev]) print('Dev - %.4f/%.4f/%.4f' % (p, r, f)) p, r, f = BCubeEvaluator().evaluate_documents([s.gCs for s in Stst], [s.aCs for s in Stst]) print('Tst - %.4f/%.4f/%.4f' % (p, r, f)) default_recursion_limit = sys.getrecursionlimit() sys.setrecursionlimit(default_recursion_limit * 2) with open(data_out, 'wb') as fout: pickle.dump([Strn, Sdev, Stst], fout, protocol=2) sys.setrecursionlimit(default_recursion_limit)
def run_application(arguments): debug = {'python_recursion_limit': sys.getrecursionlimit()} cmd_line_parser = argparse.ArgumentParser(prog='cycle-parser') cmd_line_parser.add_argument( "file", help="Source filename to start the search on.") cmd_line_parser.add_argument( '-i', nargs=1, action='append', default=[["."]], help= 'Additional include path to look up relative source filenames. Can be used multiple times to add more than one path.', metavar='INCLUDE_PATH') cmd_line_parser.add_argument( '-s', type=int, choices=range(1, debug['python_recursion_limit']), default=500, help= 'Maximum search depth before giving up. Default is 500, which is already insanely high.', metavar='SEARCH_DEPTH') cmd_line_parser.add_argument( '-j', action='store_true', help='Print report as JSON object instead of a human readable format.') cmd_line_args = cmd_line_parser.parse_args(arguments[1:]) project_include_path_array_list = cmd_line_args.i project_include_path_list = sum(project_include_path_array_list, []) start_source_filename = cmd_line_args.file language = detect_programming_language(start_source_filename) behaviour = language_behaviour(language) global_options = { 'start_source_filename': start_source_filename, 'project_include_path_list': project_include_path_list, 'language': language, 'language_behaviour': behaviour, 'max_recursion_depth': cmd_line_args.s, 'json_report': cmd_line_args.j } stats = {'file_check_cnt': 0, 'max_recursion_depth': 0} recursion_history = set() backtrace_recording = [] cycle_found = follow_module_references(start_source_filename, recursion_history, backtrace_recording, global_options, stats, 0) result = { 'cycle_found': cycle_found, 'cycle_backtrace': backtrace_recording } print_report({ 'result': result, 'global_options': global_options, 'stats': stats, 'debug': debug })
def change_recursion_limit(limit): """Temporarily changes the recursion limit.""" old_limit = sys.getrecursionlimit() sys.setrecursionlimit(limit) yield sys.setrecursionlimit(old_limit)
def set_python_recursion_limit(n) -> None: "Sets the required python recursion limit given $RecursionLimit value" python_depth = python_recursion_depth(n) sys.setrecursionlimit(python_depth) if sys.getrecursionlimit() != python_depth: raise OverflowError
total = int(match.group(1)) + int(match.group(2)) return total def find_least_signal_noise_intersection(intersections): min_signal_noise = 50000 closest_intersection = grid_center for intersection in intersections: signal_noise = get_combined_signal_noise(intersection) if signal_noise < min_signal_noise and signal_noise > 0: min_signal_noise = signal_noise closest_intersection = intersection return min_signal_noise if __name__ == "__main__": old_limit = sys.getrecursionlimit() new_limit = old_limit + 10 sys.setrecursionlimit(new_limit) print('plotting line 1') plot_named_line('line1', line1) print('plotting line 2') plot_named_line('line2', line2) # intersections = find_all_intersections('line1', 'line2') intersections = global_intersections # print(str(find_closest_intersection_distance(intersections))) print('calculating signal noise...') print(str(find_least_signal_noise_intersection(intersections))) sys.setrecursionlimit(old_limit)