Example #1
0
def run(arg_parser, entry_function):
    arg_parser.add_argument("--pdb", action='store_true')
    arg_parser.add_argument("--ipdb", action='store_true')

    args, other_args = arg_parser.parse_known_args()

    def log(out_file):
        subprocess.call("git rev-parse HEAD", shell=True, stdout=out_file)
        subprocess.call("git --no-pager diff", shell=True, stdout=out_file)
        out_file.write('\n\n')
        out_file.write(' '.join(sys.argv))
        out_file.write('\n\n')
        json.dump(vars(args), out_file)
        out_file.write('\n\n')

    log(sys.stdout)
    # if 'save_dir' in vars(args) and args.save_dir:
    #     with open(os.path.join(args.save_dir, 'invoke.log'), 'w') as f:
    #         log(f)

    if args.ipdb:
        import ipdb
        ipdb.runcall(entry_function, args)
    elif args.pdb:
        import pdb
        pdb.runcall(entry_function, args)
    else:
        entry_function(args)
Example #2
0
    def failfast_or_pdb(step):
        has_traceback = step.why

        if not has_traceback:
            return

        sys.stdout.write(step.why.traceback + '\n')

        try:
            from IPython.core.debugger import Pdb
            pdb = Pdb()
        except ImportError:
            try:
                from IPython.Debugger import Pdb
                from IPython.Shell import IPShell
                IPShell(argv=[''])
                pdb = Pdb()
            except ImportError:
                import pdb

        matched, defined = step.pre_run(False)
        if matched:
            args = matched.groups()
            kwargs = matched.groupdict()
            pdb.runcall(defined.function, step, *args, **kwargs)
Example #3
0
def run(arg_parser, entry_function, functions=None):

    args = get_arguments(arg_parser)
    print('parameters:')
    print(json.dumps(vars(args), indent=2))

    args = DotDict(vars(args))

    args.RESULT_DIR = os.path.join('tasks/{}/experiments/'.format(args.prefix),
                                   args.experiment_name, 'results')
    args.SNAPSHOT_DIR = os.path.join(
        'tasks/{}/experiments/'.format(args.prefix), args.experiment_name,
        'snapshots')
    args.PLOT_DIR = os.path.join('tasks/{}/experiments/'.format(args.prefix),
                                 args.experiment_name, 'plots')

    make_dirs([args.RESULT_DIR, args.SNAPSHOT_DIR, args.PLOT_DIR])

    import torch.cuda
    torch.cuda.disabled = args.no_cuda

    if entry_function == None:
        print('will run', args.function)
        entry_function = functions[args.function]
    if args.ipdb:
        import ipdb
        ipdb.runcall(entry_function, args)
    elif args.pdb:
        import pdb
        pdb.runcall(entry_function, args)
    else:
        entry_function(args)
Example #4
0
def runReactorWithLogging(config, oldstdout, oldstderr):
    from twisted.internet import reactor
    try:
        if config['profile']:
            if not config['nothotshot']:
                runWithHotshot(reactor, config)
            else:
                runWithProfiler(reactor, config)
        elif config['debug']:
            sys.stdout = oldstdout
            sys.stderr = oldstderr
            if runtime.platformType == 'posix':
                signal.signal(signal.SIGUSR2, lambda *args: pdb.set_trace())
                signal.signal(signal.SIGINT, lambda *args: pdb.set_trace())
            fixPdb()
            pdb.runcall(reactor.run)
        else:
            reactor.run()
    except:
        if config['nodaemon']:
            file = oldstdout
        else:
            file = open("TWISTD-CRASH.log", 'a')
        traceback.print_exc(file=file)
        file.flush()
Example #5
0
def test_only(args):
    testFiles = [str(PreviousCwd / arg) for arg in args]
    if args[0] == "--pdb":
        import pdb
        pdb.runcall(runPyTest, testFiles[1:])
    else:
        runPyTest(testFiles)
def runReactorWithLogging(config, oldstdout, oldstderr):
    from twisted.internet import reactor
    try:
        if config['profile']:
            if not config['nothotshot']:
                runWithHotshot(reactor, config)
            else:
                runWithProfiler(reactor, config)
        elif config['debug']:
            sys.stdout = oldstdout
            sys.stderr = oldstderr
            if runtime.platformType == 'posix':
                signal.signal(signal.SIGUSR2, lambda *args: pdb.set_trace())
                signal.signal(signal.SIGINT, lambda *args: pdb.set_trace())
            fixPdb()
            pdb.runcall(reactor.run)
        else:
            reactor.run()
    except:
        if config['nodaemon']:
            file = oldstdout
        else:
            file = open("TWISTD-CRASH.log",'a')
        traceback.print_exc(file=file)
        file.flush()
Example #7
0
def run(arg_parser, entry_function):
    arg_parser.add_argument("--pdb", action='store_true')
    arg_parser.add_argument("--ipdb", action='store_true')
    arg_parser.add_argument("--no_cuda", action='store_true')
    arg_parser.add_argument("--experiment_name", type=str, default='debug')
    arg_parser.add_argument("--batch_size", type=int, default=64)
    arg_parser.add_argument("--save_args", action='store_false')

    args = arg_parser.parse_args()
    print('parameters:')
    print(json.dumps(vars(args), indent=2))

    args = DotDict(vars(args))
    args.RESULT_DIR = os.path.join('tasks/R2R/experiments/',
                                   args.experiment_name, 'results')
    args.SNAPSHOT_DIR = os.path.join('tasks/R2R/experiments/',
                                     args.experiment_name, 'snapshots')
    args.PLOT_DIR = os.path.join('tasks/R2R/experiments/',
                                 args.experiment_name, 'plots')
    make_dirs([args.RESULT_DIR, args.SNAPSHOT_DIR, args.PLOT_DIR])

    import torch.cuda
    torch.cuda.disabled = args.no_cuda

    def log(out_file):
        ''' git updates '''
        subprocess.call("git rev-parse HEAD", shell=True, stdout=out_file)
        subprocess.call("git --no-pager diff", shell=True, stdout=out_file)
        out_file.write('\n\n')
        out_file.write(' '.join(sys.argv))
        out_file.write('\n\n')
        json.dump(dict(args), out_file)
        out_file.write('\n\n')

    #log(sys.stdout)
    if args.save_args:
        import datetime
        now = datetime.datetime.now()
        args_fn = 'args-%d-%d-%d,%d:%d:%d' % (now.year, now.month, now.day,
                                              now.hour, now.minute, now.second)
        with open(os.path.join(args.PLOT_DIR, args_fn), 'w') as f:
            log(f)

    # Make the folders
    args.RESULT_DIR = os.path.join('tasks/R2R/experiments/',
                                   args.experiment_name, 'results')
    args.SNAPSHOT_DIR = os.path.join('tasks/R2R/experiments/',
                                     args.experiment_name, 'snapshots')
    args.PLOT_DIR = os.path.join('tasks/R2R/experiments/',
                                 args.experiment_name, 'plots')
    make_dirs([args.RESULT_DIR, args.SNAPSHOT_DIR, args.PLOT_DIR])

    if args.ipdb:
        import ipdb
        ipdb.runcall(entry_function, args)
    elif args.pdb:
        import pdb
        pdb.runcall(entry_function, args)
    else:
        entry_function(args)
Example #8
0
  def __call__(self, test_state):
    """Invoke this Phase, passing in the appropriate args.

    By default, an openhtf.TestApi is passed as the first positional arg, but if
    the 'requires_state' option is set, then a test_state.TestState is passed
    instead. If no positional args are expected, then neither is passed in. In
    any case, keyword args are passed in based on extra_kwargs, set via
    with_args(), combined with plugs (plugs override extra_kwargs).

    Args:
      test_state: test_state.TestState for the currently executing Test.

    Returns:
      The return value from calling the underlying function.
    """
    kwargs = dict(self.extra_kwargs)
    kwargs.update(test_state.plug_manager.provide_plugs(
        (plug.name, plug.cls) for plug in self.plugs if plug.update_kwargs))

    if sys.version_info[0] < 3:
      arg_info = inspect.getargspec(self.func)
      keywords = arg_info.keywords
    else:
      arg_info = inspect.getfullargspec(self.func)
      keywords = arg_info.varkw
    # Pass in test_api if the phase takes *args, or **kwargs with at least 1
    # positional, or more positional args than we have keyword args.
    if arg_info.varargs or (keywords and len(arg_info.args) >= 1) or (
        len(arg_info.args) > len(kwargs)):
      # Underlying function has room for test_api as an arg. If it doesn't
      # expect it but we miscounted args, we'll get another error farther down.
      old_logger = test_state.logger

      # The logging module has a module _lock instance that is a threading.RLock
      # instance; it can cause deadlocks in Python 2.7 when a KillableThread is
      # killed while its release method is running.
      with threads.safe_lock_release_context(logging._lock):  # pylint: disable=protected-access
        # Update test_state's logger so that it is a phase-specific one.
        record_logger = logs.get_record_logger_for(test_state.execution_uid)
        test_state.logger = record_logger.getChild('phase').getChild(self.name)
      try:
        args = []
        if self.options.requires_state:
          args.append(test_state)
        else:
          args.append(test_state.test_api)

        if self.options.run_under_pdb:
          return pdb.runcall(self.func, *args, **kwargs)
        else:
          return self.func(*args, **kwargs)
      finally:
        test_state.logger = old_logger
    if self.options.run_under_pdb:
      return pdb.runcall(self.func, **kwargs)
    else:
      return self.func(**kwargs)
Example #9
0
def translate(inputObjectFileAssemblyName, outputObjectFileAssemblyName,
              exportedSections, sectionsAreSymbols):
    if False:  #sectionsAreSymbols:
        pdb.runcall(translate2, inputObjectFileAssemblyName,
                    outputObjectFileAssemblyName, exportedSections,
                    sectionsAreSymbols)
    else:
        translate2(inputObjectFileAssemblyName, outputObjectFileAssemblyName,
                   exportedSections, sectionsAreSymbols)
Example #10
0
File: main.py Project: dmick/pdbcs
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('script')
    args, scriptargs = parser.parse_known_args()

    script_name = os.path.basename(args.script)
    ep = pkg_resources.iter_entry_points('console_scripts', script_name).next()
    f = ep.load()

    sys.argv = [args.script]
    sys.argv.extend(scriptargs)
    pdb.runcall(f)
Example #11
0
 def _run(self):
     with os_timer():
         if self.opts.profiler:
             with tempfile.NamedTemporaryFile() as tmp:
                 cProfile.runctx('self._main()', globals(), locals(),
                                 tmp.name)
                 s = pstats.Stats(tmp.name)
                 s.sort_stats("cumulative").print_stats(50)
         elif self.opts.debugger:
             pdb.runcall(self._main)
         else:
             self._main()
Example #12
0
def runReactorWithLogging(config,
                          oldstdout,
                          oldstderr,
                          profiler=None,
                          reactor=None):
    """
    Start the reactor, using profiling if specified by the configuration, and
    log any error happening in the process.

    @param config: configuration of the twistd application.
    @type config: L{ServerOptions}

    @param oldstdout: initial value of C{sys.stdout}.
    @type oldstdout: C{file}

    @param oldstderr: initial value of C{sys.stderr}.
    @type oldstderr: C{file}

    @param profiler: object used to run the reactor with profiling.
    @type profiler: L{AppProfiler}

    @param reactor: The reactor to use.  If L{None}, the global reactor will
        be used.
    """
    if reactor is None:
        from twisted.internet import reactor
    try:
        if config['profile']:
            if profiler is not None:
                profiler.run(reactor)
        elif config['debug']:
            sys.stdout = oldstdout
            sys.stderr = oldstderr
            if runtime.platformType == 'posix':
                signal.signal(signal.SIGUSR2, lambda *args: pdb.set_trace())
                signal.signal(signal.SIGINT, lambda *args: pdb.set_trace())
            fixPdb()
            pdb.runcall(reactor.run)
        else:
            reactor.run()
    except:
        close = False
        if config['nodaemon']:
            file = oldstdout
        else:
            file = open("TWISTD-CRASH.log", "a")
            close = True
        try:
            traceback.print_exc(file=file)
            file.flush()
        finally:
            if close:
                file.close()
Example #13
0
def runReactorWithLogging(config, oldstdout, oldstderr, profiler=None,
                          reactor=None):
    """
    Start the reactor, using profiling if specified by the configuration, and
    log any error happening in the process.

    @param config: configuration of the twistd application.
    @type config: L{ServerOptions}

    @param oldstdout: initial value of C{sys.stdout}.
    @type oldstdout: C{file}

    @param oldstderr: initial value of C{sys.stderr}.
    @type oldstderr: C{file}

    @param profiler: object used to run the reactor with profiling.
    @type profiler: L{AppProfiler}

    @param reactor: The reactor to use.  If L{None}, the global reactor will
        be used.
    """
    if reactor is None:
        from twisted.internet import reactor
    try:
        if config['profile']:
            if profiler is not None:
                profiler.run(reactor)
        elif config['debug']:
            sys.stdout = oldstdout
            sys.stderr = oldstderr
            if runtime.platformType == 'posix':
                signal.signal(signal.SIGUSR2, lambda *args: pdb.set_trace())
                signal.signal(signal.SIGINT, lambda *args: pdb.set_trace())
            fixPdb()
            pdb.runcall(reactor.run)
        else:
            reactor.run()
    except:
        close = False
        if config['nodaemon']:
            file = oldstdout
        else:
            file = open("TWISTD-CRASH.log", "a")
            close = True
        try:
            traceback.print_exc(file=file)
            file.flush()
        finally:
            if close:
                file.close()
Example #14
0
  def __call__(self,
               running_test_state: 'test_state.TestState') -> PhaseReturnT:
    """Invoke this Phase, passing in the appropriate args.

    By default, an openhtf.TestApi is passed as the first positional arg, but if
    the 'requires_state' option is set, then a test_state.TestState is passed
    instead. If no positional args are expected, then neither is passed in. In
    any case, keyword args are passed in based on extra_kwargs, set via
    with_args(), combined with plugs (plugs override extra_kwargs).

    Args:
      running_test_state: test_state.TestState for the currently executing Test.

    Returns:
      The return value from calling the underlying function.
    """
    kwargs = {}
    if six.PY3:
      arg_info = inspect.getfullargspec(self.func)
      keywords = arg_info.varkw
    else:
      arg_info = inspect.getargspec(self.func)  # pylint: disable=deprecated-method
      keywords = arg_info.keywords
    if arg_info.defaults is not None:
      for arg_name, arg_value in zip(arg_info.args[-len(arg_info.defaults):],
                                     arg_info.defaults):
        kwargs[arg_name] = arg_value
    kwargs.update(self.extra_kwargs)
    kwargs.update(
        running_test_state.plug_manager.provide_plugs(
            (plug.name, plug.cls) for plug in self.plugs if plug.update_kwargs))
    # Pass in test_api if the phase takes *args, or **kwargs with at least 1
    # positional, or more positional args than we have keyword args.
    if arg_info.varargs or (keywords and len(arg_info.args) >= 1) or (len(
        arg_info.args) > len(kwargs)):
      args = []
      if self.options.requires_state:
        args.append(running_test_state)
      else:
        args.append(running_test_state.test_api)

      if self.options.run_under_pdb:
        return pdb.runcall(self.func, *args, **kwargs)
      else:
        return self.func(*args, **kwargs)
    if self.options.run_under_pdb:
      return pdb.runcall(self.func, **kwargs)
    else:
      return self.func(**kwargs)
Example #15
0
    def _reemit(self, single_event_path=None):
        last_event_path = None
        deferred = True
        for event_path, observer_path, method_name in self._storage.notices(
                single_event_path):
            event_handle = Handle.from_path(event_path)

            if last_event_path != event_path:
                if not deferred and last_event_path is not None:
                    self._storage.drop_snapshot(last_event_path)
                last_event_path = event_path
                deferred = False

            try:
                event = self.load_snapshot(event_handle)
            except NoTypeError:
                self._storage.drop_notice(event_path, observer_path,
                                          method_name)
                continue

            event.deferred = False
            observer = self._observer.get(observer_path)
            if observer:
                if single_event_path is None:
                    logger.debug("Re-emitting %s.", event)
                custom_handler = getattr(observer, method_name, None)
                if custom_handler:
                    event_is_from_juju = isinstance(event, charm.HookEvent)
                    event_is_action = isinstance(event, charm.ActionEvent)
                    if (event_is_from_juju or
                            event_is_action) and 'hook' in self._juju_debug_at:
                        # Present the welcome message and run under PDB.
                        self._show_debug_code_message()
                        pdb.runcall(custom_handler, event)
                    else:
                        # Regular call to the registered method.
                        custom_handler(event)

            if event.deferred:
                deferred = True
            else:
                self._storage.drop_notice(event_path, observer_path,
                                          method_name)
            # We intentionally consider this event to be dead and reload it from
            # scratch in the next path.
            self.framework._forget(event)

        if not deferred and last_event_path is not None:
            self._storage.drop_snapshot(last_event_path)
    def execute(self, context):

        binpath = bpy.app.binary_path

        addonspath = (
            binpath[0 : binpath.rindex("\\") + 1]
            + str(bpy.app.version[0])
            + "."
            + str(bpy.app.version[1])
            + "\\scripts\\addons\\"
        )

        print(addonspath)

        sc = context.space_data
        text = sc.text

        br = " #breakpoint"

        filepath = addonspath + "debug.py"
        file = open(filepath, "w")
        file.write("import pdb\n")

        for line in text.lines:
            l = line.body

            if line.body.find(br) > -1:
                indent = ""
                for letter in line.body:

                    if not letter.isalpha():
                        indent += letter
                    else:
                        break
                file.write(l[0 : -len(br)] + "\n")

                file.write(indent + "pdb.set_trace()\n")

            else:
                file.write(line.body + "\n")

        file.close()

        import pdb
        import debug

        pdb.runcall("debug")

        return {"FINISHED"}
Example #17
0
    def debug(self, kind='ipdb'):
        """
        Run callable in debug mode.

        Parameters
        ----------
        kind : str ('ipdb' or 'pdb')
            Which debugger to use 'ipdb' for IPython debugger or 'pdb' for
            debugger from the standard library

        Notes
        -----
        Be careful when debugging tasks. If the task has run
        successfully, you overwrite products but don't save the
        updated source code, your DAG will enter an inconsistent state where
        the metadata won't match the overwritten product.
        """
        opts = {'ipdb', 'pdb'}

        if kind not in opts:
            raise ValueError('"kind" must be one of {}, got: "{}"'.format(
                opts, kind))

        if self.exec_status == TaskStatus.WaitingRender:
            raise TaskBuildError('Error in task "{}". '
                                 'Cannot call task.debug() on a task that has '
                                 'not been '
                                 'rendered, call DAG.render() first'.format(
                                     self.name))

        if 'upstream' in self.params and self._unserializer:
            params = _unserialize_params(self.params, self._unserializer)
        else:
            params = self.params.to_dict()

        if self._serializer:
            params.pop('product')

        if kind == 'ipdb':
            try:
                # this seems to only work in a Terminal
                ipdb = TerminalPdb()
            except AttributeError:
                # this works in a Jupyter notebook
                ipdb = Pdb()

            ipdb.runcall(self.source.primitive, **params)
        elif kind == 'pdb':
            pdb.runcall(self.source.primitive, **params)
Example #18
0
def really_start(main=None):
  """Initializes flag values, and calls main with non-flag arguments.

  Only non-flag arguments are passed to main().  The return value of main() is
  used as the exit status.

  Args:
    main: Main function to run with the list of non-flag arguments, or None
      so that sys.modules['__main__'].main is to be used.
  """
  argv = RegisterAndParseFlagsWithUsage()

  if main is None:
    main = sys.modules['__main__'].main

  try:
    if FLAGS.run_with_pdb:
      sys.exit(pdb.runcall(main, argv))
    else:
      if FLAGS.run_with_profiling:
        # Avoid import overhead since most apps (including performance-sensitive
        # ones) won't be run with profiling.
        import atexit
        if FLAGS.use_cprofile_for_profiling:
          import cProfile as profile
        else:
          import profile
        profiler = profile.Profile()
        atexit.register(profiler.print_stats)
        retval = profiler.runcall(main, argv)
        sys.exit(retval)
      else:
        sys.exit(main(argv))
  except UsageError, error:
    usage(shorthelp=1, detailed_error=error, exitcode=error.exitcode)
Example #19
0
    def __call__(self, *args):
        """Execute main function with given args."""
        import importlib
        import os
        import pdb
        import tempfile
        import sys

        # Create temporary code file.
        code_fd, code_path = tempfile.mkstemp(
            prefix="myia_backend_python_code_", suffix=".py")
        # Get module directory and name.
        module_dir = os.path.dirname(code_path)
        module_name = os.path.splitext(os.path.basename(code_path))[0]
        # Add module to sys.path
        sys.path.append(module_dir)
        try:
            # Save code into module file.
            with open(code_path, "w") as code_file:
                code_file.write(self.code)
            # Import module.
            module = importlib.import_module(module_name)
            # Run main function.
            output = pdb.runcall(getattr(module, "main"), *args)

        # NB: I don't know why, but code executed after PDB call is
        # systematically reported as uncovered by pytest-cov, so I am
        # excluding following lines from coverage.
        finally:  # pragma: no cover
            # Reset sys.path
            sys.path.remove(module_dir)
            # Close and delete code file.
            os.close(code_fd)
            os.remove(code_path)
        return output  # pragma: no cover
Example #20
0
 def __call__(self, args=None):
     sys.excepthook = self.handle
     if args is None:
         args = sys.argv[1:]
     if optcomplete:
         optcomplete.autocomplete(self.parser, self.arg_completer)
     elif 'COMP_LINE' in os.environ:
         return 0
     (options, args) = self.parser.parse_args(expand_args(args))
     _CONSOLE.setLevel(options.loglevel)
     if options.logfile:
         logfile = logging.FileHandler(options.logfile)
         logfile.setFormatter(
             logging.Formatter('%(asctime)s, %(levelname)s, %(message)s'))
         logfile.setLevel(logging.DEBUG)
         logging.getLogger().addHandler(logfile)
     if options.debug:
         logging.getLogger().setLevel(logging.DEBUG)
     else:
         logging.getLogger().setLevel(logging.INFO)
     if options.debug:
         import pdb
         return pdb.runcall(self.main, options, args)
     else:
         return self.main(options, args) or 0
Example #21
0
def really_start(main=None):
    """Initializes flag values, and calls main with non-flag arguments.

  Only non-flag arguments are passed to main().  The return value of main() is
  used as the exit status.

  Args:
    main: Main function to run with the list of non-flag arguments, or None
      so that sys.modules['__main__'].main is to be used.
  """
    argv = RegisterAndParseFlagsWithUsage()

    if main is None:
        main = sys.modules['__main__'].main

    try:
        if FLAGS.run_with_pdb:
            sys.exit(pdb.runcall(main, argv))
        else:
            if FLAGS.run_with_profiling:
                # Avoid import overhead since most apps (including performance-sensitive
                # ones) won't be run with profiling.
                import atexit
                if FLAGS.use_cprofile_for_profiling:
                    import cProfile as profile
                else:
                    import profile
                profiler = profile.Profile()
                atexit.register(profiler.print_stats)
                retval = profiler.runcall(main, argv)
                sys.exit(retval)
            else:
                sys.exit(main(argv))
    except UsageError as error:
        usage(shorthelp=1, detailed_error=error, exitcode=error.exitcode)
def testPolyFit():
    import numpy as np
    import pdb
    x = np.array([1, 2, 3, 4, 5])
    y = np.array([100, 50, 20, 30, 0])
    fit = pdb.runcall(np.polyfit, x, y, 1, w=y)
    print(x, y, fit)
Example #23
0
 def __call__(self, args=None):
     if args is None:
         args = sys.argv[1:]
     (options, args) = self.parser.parse_args(self.expand_args(args))
     console = logging.StreamHandler(sys.stderr)
     console.setFormatter(logging.Formatter('%(message)s'))
     console.setLevel(options.loglevel)
     logging.getLogger().addHandler(console)
     if options.logfile:
         logfile = logging.FileHandler(options.logfile)
         logfile.setFormatter(logging.Formatter('%(asctime)s, %(levelname)s, %(message)s'))
         logfile.setLevel(logging.DEBUG)
         logging.getLogger().addHandler(logfile)
     if options.debug:
         console.setLevel(logging.DEBUG)
         logging.getLogger().setLevel(logging.DEBUG)
     else:
         logging.getLogger().setLevel(logging.INFO)
     if options.debug:
         import pdb
         return pdb.runcall(self.main, options, args)
     else:
         try:
             return self.main(options, args) or 0
         except:
             return self.handle(*sys.exc_info())
Example #24
0
    def _run_command_handler(self, handler, context=None, debug_command=False, **kwargs):
        cls = handler.cls

        if handler.pass_context and not context:
            raise Exception('mach command class requires context.')

        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)

        if debug_command:
            import pdb
            result = pdb.runcall(fn, **kwargs)
        else:
            result = fn(**kwargs)

        result = result or 0
        assert isinstance(result, (int, long))
        return result
def testPolyFit():
    import numpy as np
    import pdb
    x=np.array([1,2,3,4,5])
    y=np.array([100,50,20,30,0])
    fit=pdb.runcall(np.polyfit,x,y,1,w=y)
    print(x,y,fit)
Example #26
0
 def __call__(self, args=None):
     sys.excepthook = self.handle
     if args is None:
         args = sys.argv[1:]
     if optcomplete:
         optcomplete.autocomplete(self.parser, self.arg_completer)
     elif 'COMP_LINE' in os.environ:
         return 0
     (options, args) = self.parser.parse_args(expand_args(args))
     _CONSOLE.setLevel(options.loglevel)
     if options.logfile:
         logfile = logging.FileHandler(options.logfile)
         logfile.setFormatter(
             logging.Formatter('%(asctime)s, %(levelname)s, %(message)s'))
         logfile.setLevel(logging.DEBUG)
         logging.getLogger().addHandler(logfile)
     if options.debug:
         logging.getLogger().setLevel(logging.DEBUG)
     else:
         logging.getLogger().setLevel(logging.INFO)
     if options.debug:
         import pdb
         return pdb.runcall(self.main, options, args)
     else:
         return self.main(options, args) or 0
Example #27
0
 def __call__(self, args=None):
     if args is None:
         args = sys.argv[1:]
     (options, args) = self.parser.parse_args(self.expand_args(args))
     console = logging.StreamHandler(sys.stderr)
     console.setFormatter(logging.Formatter('%(message)s'))
     console.setLevel(options.loglevel)
     logging.getLogger().addHandler(console)
     if options.logfile:
         logfile = logging.FileHandler(options.logfile)
         logfile.setFormatter(
             logging.Formatter('%(asctime)s, %(levelname)s, %(message)s'))
         logfile.setLevel(logging.DEBUG)
         logging.getLogger().addHandler(logfile)
     if options.debug:
         console.setLevel(logging.DEBUG)
         logging.getLogger().setLevel(logging.DEBUG)
     else:
         logging.getLogger().setLevel(logging.INFO)
     if options.debug:
         import pdb
         return pdb.runcall(self.main, options, args)
     else:
         try:
             return self.main(options, args) or 0
         except:
             return self.handle(*sys.exc_info())
Example #28
0
    def _run_command_handler(self,
                             handler,
                             context=None,
                             debug_command=False,
                             **kwargs):
        cls = handler.cls

        if handler.pass_context and not context:
            raise Exception('mach command class requires context.')

        if context:
            prerun = getattr(context, 'pre_dispatch_handler', None)
            if prerun:
                prerun(context, handler, args=kwargs)

        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)

        start_time = time.time()

        if debug_command:
            import pdb
            result = pdb.runcall(fn, **kwargs)
        else:
            result = fn(**kwargs)

        end_time = time.time()

        result = result or 0
        assert isinstance(result, (int, long))

        if context and not debug_command:
            postrun = getattr(context, 'post_dispatch_handler', None)
            if postrun:
                postrun(context,
                        handler,
                        instance,
                        result,
                        start_time,
                        end_time,
                        args=kwargs)

        return result
Example #29
0
def main():
  ap = argparse.ArgumentParser()
  default_output = os.environ.get('TEST_UNDECLARED_OUTPUTS_DIR', None)
  ap.add_argument('--output', '-o', help='Output directory',
                  default=default_output, required=default_output is None)
  ap.add_argument('-P', dest='pdb_on_error', action='store_true',
                  help='Run pdb on exception')
  ap.add_argument('--no-minify', dest='minify', action='store_false')
  ap.add_argument('--sphinx-help', action='store_true',
                  help='Show sphinx build command-line help')
  ap.add_argument('--pdb', action='store_true', help='Run under pdb')
  args, unknown = ap.parse_known_args()
  if args.pdb:
    import pdb
    pdb.runcall(run, args, unknown)
  else:
    run(args, unknown)
Example #30
0
	def execute(self, context):
		
		binpath = bpy.app.binary_path
			
		addonspath = binpath[0:binpath.rindex("\\")+1]+str(bpy.app.version[0])+"."+str(bpy.app.version[1])+"\\scripts\\addons\\"
		
		print(addonspath)
			
		sc = context.space_data
		text = sc.text
		
		br = " #breakpoint"
	
		filepath = addonspath+"debug.py"
		file = open(filepath, "w")
		file.write("import pdb\n")
		
		for line in text.lines:
			l = line.body
			
			if line.body.find(br)>-1:
				indent = ""
				for letter in line.body:
					
					if not letter.isalpha():
						indent+=letter
					else:
						break
				file.write(l[0:-len(br)]+"\n")
				
				file.write(indent+"pdb.set_trace()\n")
				
			else:
				file.write(line.body+"\n")
						
				
		
		file.close()
		
		import pdb
		import debug
		
		pdb.runcall("debug")	
		
		
		return {'FINISHED'}
Example #31
0
    def __call__(self, test_state):
        """Invoke this Phase, passing in the appropriate args.

    By default, an openhtf.TestApi is passed as the first positional arg, but if
    the 'requires_state' option is set, then a test_state.TestState is passed
    instead. If no positional args are expected, then neither is passed in. In
    any case, keyword args are passed in based on extra_kwargs, set via
    with_args(), combined with plugs (plugs override extra_kwargs).

    Args:
      test_state: test_state.TestState for the currently executing Test.

    Returns:
      The return value from calling the underlying function.
    """
        kwargs = dict(self.extra_kwargs)
        kwargs.update(
            test_state.plug_manager.provide_plugs((plug.name, plug.cls)
                                                  for plug in self.plugs
                                                  if plug.update_kwargs))

        if sys.version_info[0] < 3:
            arg_info = inspect.getargspec(self.func)
            keywords = arg_info.keywords
        else:
            arg_info = inspect.getfullargspec(self.func)
            keywords = arg_info.varkw
        # Pass in test_api if the phase takes *args, or **kwargs with at least 1
        # positional, or more positional args than we have keyword args.
        if arg_info.varargs or (keywords and len(arg_info.args) >= 1) or (len(
                arg_info.args) > len(kwargs)):
            args = []
            if self.options.requires_state:
                args.append(test_state)
            else:
                args.append(test_state.test_api)

            if self.options.run_under_pdb:
                return pdb.runcall(self.func, *args, **kwargs)
            else:
                return self.func(*args, **kwargs)
        if self.options.run_under_pdb:
            return pdb.runcall(self.func, **kwargs)
        else:
            return self.func(**kwargs)
Example #32
0
def runReactorWithLogging(config, oldstdout, oldstderr, profiler=None):
    """
    Start the reactor, using profiling if specified by the configuration, and
    log any error happening in the process.

    @param config: configuration of the twistd application.
    @type config: L{ServerOptions}

    @param oldstdout: initial value of C{sys.stdout}.
    @type oldstdout: C{file}

    @param oldstderr: initial value of C{sys.stderr}.
    @type oldstderr: C{file}

    @param profiler: object used to run the reactor with profiling.
    @type profiler: L{AppProfiler}
    """
    from twisted.internet import reactor
    try:
        if config['profile']:
            if profiler is not None:
                profiler.run(reactor)
            else:
                # Backward compatible code
                if not config['nothotshot']:
                    runWithHotshot(reactor, config)
                else:
                    runWithProfiler(reactor, config)
        elif config['debug']:
            sys.stdout = oldstdout
            sys.stderr = oldstderr
            if runtime.platformType == 'posix':
                signal.signal(signal.SIGUSR2, lambda *args: pdb.set_trace())
                signal.signal(signal.SIGINT, lambda *args: pdb.set_trace())
            fixPdb()
            pdb.runcall(reactor.run)
        else:
            reactor.run()
    except:
        if config['nodaemon']:
            file = oldstdout
        else:
            file = open("TWISTD-CRASH.log", 'a')
        traceback.print_exc(file=file)
        file.flush()
Example #33
0
def runReactorWithLogging(config, oldstdout, oldstderr, profiler=None):
    """
    Start the reactor, using profiling if specified by the configuration, and
    log any error happening in the process.

    @param config: configuration of the twistd application.
    @type config: L{ServerOptions}

    @param oldstdout: initial value of C{sys.stdout}.
    @type oldstdout: C{file}

    @param oldstderr: initial value of C{sys.stderr}.
    @type oldstderr: C{file}

    @param profiler: object used to run the reactor with profiling.
    @type profiler: L{AppProfiler}
    """
    from twisted.internet import reactor
    try:
        if config['profile']:
            if profiler is not None:
                profiler.run(reactor)
            else:
                # Backward compatible code
                if not config['nothotshot']:
                    runWithHotshot(reactor, config)
                else:
                    runWithProfiler(reactor, config)
        elif config['debug']:
            sys.stdout = oldstdout
            sys.stderr = oldstderr
            if runtime.platformType == 'posix':
                signal.signal(signal.SIGUSR2, lambda *args: pdb.set_trace())
                signal.signal(signal.SIGINT, lambda *args: pdb.set_trace())
            fixPdb()
            pdb.runcall(reactor.run)
        else:
            reactor.run()
    except:
        if config['nodaemon']:
            file = oldstdout
        else:
            file = open("TWISTD-CRASH.log",'a')
        traceback.print_exc(file=file)
        file.flush()
Example #34
0
def run(arg_parser, entry_function):
  arg_parser.add_argument("--pdb", action='store_true')
  arg_parser.add_argument("--ipdb", action='store_true')
  arg_parser.add_argument("--no_cuda", action='store_true')
  
  args = arg_parser.parse_args()
  
  import torch.cuda
  torch.cuda.disabled = args.no_cuda
  
  if args.ipdb:
    import ipdb
    ipdb.runcall(entry_function, args)
  elif args.pdb:
    import pdb
    pdb.runcall(entry_function, args)
  else:
    entry_function(args)
Example #35
0
def cli_bidsme_pdb():
    """
    Funtion hook for setup-tools executable with debugger
    """
    import pdb
    try:
        res = pdb.runcall(main, sys.argv[1:])
    except Exception:
        pdb.post_mortem()
        res = 1
    return res
Example #36
0
 def runDebug(test, err_info, flavour, reporter, real_add):
     if options.timeout is not None:
         from signal import alarm
         alarm(0) # Don't timeout on debug.
     assert flavour in ("error", "failure")
     real_add(test, err_info)
     print "\nDebugging for %s in test: %s" % (
             flavour, reporter.getDescription(test))
     if options.rerun_on_fail is not None:
         #test.funcname will be our current test function
         #use that to get the function object for our method
         #and call it manually. WD-rpw 10-31-06
         methodName = test.funcname()
         method = getattr( test.fixture,  methodName)
         print "rerunning test for failed %s()" % (methodName)
         try:
             pdb.runcall( method )
         except:
             pdb.post_mortem( err_info.traceback() )
     else:
         pdb.post_mortem(err_info.traceback())
Example #37
0
def main():
    args = parser.parse_args()

    configure_logging(verbose=args.verbose, silent=args.silent)
    logger.debug('parsed args %s, ', vars(args))

    if args.subcommand == 'help':
        parser.print_help()
        sys.exit(0)

    if not args.subcommand:
        parser.print_help()
        sys.exit(1)

    if args.debug:
        logger.info('running %s command interactively for debug mode',
                    args.subcommand)
        pdb.runcall(COMMANDS[args.subcommand], args)
    else:
        logger.debug('invoking main routine of %s', command)
        COMMANDS[args.subcommand](args)
Example #38
0
    def CommandRun(self, argv):
        """Execute the command with given arguments.

    First register and parse additional flags. Then run the command.

    Returns:
      Command return value.

    Args:
      argv: Remaining command line arguments after parsing command and flags
            (that is a copy of sys.argv at the time of the function call with
            all parsed flags removed).
    """
        # Register flags global when run normally
        FLAGS.AppendFlagValues(self._command_flags)
        # Prepare flags parsing, to redirect help, to show help for command
        orig_app_usage = app.usage

        def ReplacementAppUsage(shorthelp=0,
                                writeto_stdout=1,
                                detailed_error=None,
                                exitcode=None):
            AppcommandsUsage(shorthelp,
                             writeto_stdout,
                             detailed_error,
                             exitcode=1,
                             show_cmd=self._command_name,
                             show_global_flags=True)

        app.usage = ReplacementAppUsage
        # Parse flags and restore app.usage afterwards
        try:
            try:
                argv = ParseFlagsWithUsage(argv)
                # Run command
                if FLAGS.run_with_pdb:
                    ret = pdb.runcall(self.Run, argv)
                else:
                    ret = self.Run(argv)
                if ret is None:
                    ret = 0
                else:
                    assert isinstance(ret, int)
                return ret
            except app.UsageError, error:
                app.usage(shorthelp=1,
                          detailed_error=error,
                          exitcode=error.exitcode)
        finally:
            # Restore app.usage and remove this command's flags from the global flags.
            app.usage = orig_app_usage
            for flag_name in self._command_flags.FlagDict():
                delattr(FLAGS, flag_name)
Example #39
0
 def run(self, server):
   self.jtoolkitserver = server
   self.errorhandler = self.jtoolkitserver.errorhandler
   if self.options.trace:
     def tracefunc(frame, event, arg):
       if frame.f_code.co_name != "?":
         space = ""
       below = frame.f_back
       while below:
         below = below.f_back
         space += " "
       code = frame.f_code
       logstring = "%s%s %s:%d, %r, %s\n" % (space, code.co_filename, code.co_name, frame.f_lineno, event, arg)
       open("pytrace.log", "a").write(logstring)
       return tracefunc
     sys.settrace(tracefunc)
   # this means the server has to be interrupted manually...
   if self.options.debug:
     import pdb
     pdb.runcall(self.serve_forever)
   else:
     self.serve_forever()
Example #40
0
    def compute_code_lambdas(self):
        # initialize
        code_lambda = []
        ji = {}
        for tid in self.gene_transcripts:
            ji[tid] = 0
            
        # for each window
        gene_len = len(self.isoform1.labels)
        for window_start in range(1,gene_len-self.window_size+2):
            # update junctions index
            for tid in self.gene_transcripts:
                tjunctions = self.gene_transcripts[tid].junctions
                if ji[tid] < len(tjunctions) and tjunctions[ji[tid]] <= window_start:
                    ji[tid] += 1

            # compute lambda
            code_lambda.append(clip_peaks.convolute_lambda(window_start, window_start+self.window_size-1, self.gene_transcripts, ji, self.total_reads))
            if False:
                pdb.runcall(code_lambda.append(clip_peaks.convolute_lambda(window_start, window_start+self.window_size-1, self.gene_transcripts, ji, self.total_reads)))

        return code_lambda
Example #41
0
def run(arg_parser, entry_function):
    arg_parser.add_argument("--pdb", action="store_true")
    arg_parser.add_argument("--ipdb", action="store_true")
    arg_parser.add_argument("--no_cuda", action="store_true")

    args = arg_parser.parse_args()

    import torch.cuda

    # todo: yuck
    torch.cuda.disabled = args.no_cuda

    def log(out_file):
        # subprocess.call("git rev-parse HEAD", shell=True, stdout=out_file)
        # subprocess.call("git --no-pager diff", shell=True, stdout=out_file)
        # out_file.write('\n\n')
        # out_file.write(' '.join(sys.argv))
        # out_file.write('\n\n')
        # json.dump(vars(args), out_file)
        out_file.write("\n\n")

    log(sys.stdout)
    # if 'save_dir' in vars(args) and args.save_dir:
    #     with open(os.path.join(args.save_dir, 'invoke.log'), 'w') as f:
    #         log(f)

    if args.ipdb:
        import ipdb

        ipdb.runcall(entry_function, args)
    elif args.pdb:
        import pdb

        pdb.runcall(entry_function, args)
    else:
        entry_function(args)
Example #42
0
def really_start(is_old_style):
  """
  This initializes flag values, and calls __main__.main().  Only non-flag
  arguments are passed to main().  The return value of main() is used as the
  exit status.
  """
  argv = RegisterAndParseFlagsWithUsage(is_old_style)

  try:
    if FLAGS.run_with_pdb:
      sys.exit(pdb.runcall(sys.modules['__main__'].main, argv))
    else:
      sys.exit(sys.modules['__main__'].main(argv))
  except UsageError, error:
    usage(shorthelp=1, detailed_error=error)
    sys.exit(1)
Example #43
0
  def CommandRun(self, argv):
    """Execute the command with given arguments.

    First register and parse additional flags. Then run the command.

    Returns:
      Command return value.

    Args:
      argv: Remaining command line arguments after parsing command and flags
            (that is a copy of sys.argv at the time of the function call with
            all parsed flags removed).
    """
    # Register flags global when run normally
    FLAGS.AppendFlagValues(self._command_flags)
    # Prepare flags parsing, to redirect help, to show help for command
    orig_app_usage = app.usage

    def ReplacementAppUsage(shorthelp=0, writeto_stdout=1, detailed_error=None,
                            exitcode=None):
      AppcommandsUsage(shorthelp, writeto_stdout, detailed_error, exitcode=1,
                       show_cmd=self._command_name, show_global_flags=True)
    app.usage = ReplacementAppUsage
    # Parse flags and restore app.usage afterwards
    try:
      try:
        argv = ParseFlagsWithUsage(argv)
        # Run command
        if FLAGS.run_with_pdb:
          ret = pdb.runcall(self.Run, argv)
        else:
          ret = self.Run(argv)
        if ret is None:
          ret = 0
        else:
          assert isinstance(ret, int)
        return ret
      except app.UsageError, error:
        app.usage(shorthelp=1, detailed_error=error, exitcode=error.exitcode)
      except:
        if FLAGS.pdb_post_mortem:
          traceback.print_exc()
          pdb.post_mortem()
        raise
Example #44
0
def _run_main(main, argv):
  """Calls main, optionally with pdb or profiler."""
  if FLAGS.run_with_pdb:
    sys.exit(pdb.runcall(main, argv))
  elif FLAGS.run_with_profiling or FLAGS.profile_file:
    # Avoid import overhead since most apps (including performance-sensitive
    # ones) won't be run with profiling.
    import atexit
    if FLAGS.use_cprofile_for_profiling:
      import cProfile as profile
    else:
      import profile
    profiler = profile.Profile()
    if FLAGS.profile_file:
      atexit.register(profiler.dump_stats, FLAGS.profile_file)
    else:
      atexit.register(profiler.print_stats)
    retval = profiler.runcall(main, argv)
    sys.exit(retval)
  else:
    sys.exit(main(argv))
  def CommandRun(self, argv):
    """Execute the command with given arguments.

    First register and parse additional flags. Then run the command.

    Returns:
      Command return value.

    Args:
      argv: Remaining command line arguments after parsing command and flags
            (that is a copy of sys.argv at the time of the function call with
            all parsed flags removed).
    """
    # Register flags global when run normally
    FLAGS.AppendFlagValues(self._command_flags)
    # Prepare flags parsing, to redirect help, to show help for command
    orig_app_usage = app.usage
    app.usage = lambda shorthelp=0, writeto_stdout=1, detailed_error=None, \
                exitcode=None: AppcommandsUsage(shorthelp, writeto_stdout,
                                                detailed_error, exitcode=1,
                                                show_cmd=self._command_name,
                                                show_global_flags=True)
    # Parse flags and restore app.usage afterwards
    try:
      argv = ParseFlagsWithUsage(argv)
      if swig_flags:
        swig_flags.UpdateFlags()
    finally:
      app.usage = orig_app_usage
    # Run command
    if FLAGS.run_with_pdb:
      ret = pdb.runcall(self.Run, argv)
    else:
      ret = self.Run(argv)
    if ret is None:
      ret = 0
    else:
      assert isinstance(ret, int)
    return ret
Example #46
0
def pdb_runcall(object, args, request):
    """If the request has the pdb_runcall key then we run the result
    of request traversal in the debugger.  Othwise, do it normally.

    A cookie for pdb_runcall may also be set or removed if the request
    has the toggle_runcall key."""
    response = request.response

    if request.has_key('toggle_runcall') and request.toggle_runcall:
        runcall_cookie = request.cookies.get('pdb_runcall', False)
        if runcall_cookie:
            response.expireCookie('pdb_runcall')
            return Publish.call_object(object, args, request) 
        else:
            response.setCookie('pdb_runcall', 1)

    if request.has_key('set_runcall_ignore'):
        if request.set_runcall_ignore:
            for ignore in request.set_runcall_ignore:
                response.appendCookie('runcall_ignore', ignore)
        else:
            response.expireCookie('runcall_ignore')
            
    if request.has_key('pdb_runcall'):
        if request.pdb_runcall:
            ignores = request.get('runcall_ignore', [])
            if ignores:
                ignores = ignores.split(':')
            for ignore in ignores:
                obj = resolveDottedName(ignore)
                if obj.im_func is getattr(object, 'im_func', None):
                    break
            else:
                return pdb.runcall(object, *args)

    return Publish.call_object(object, args, request) 
Example #47
0
        self.return_dict = self.tmp_dict
        self.reset()
        return return_dict
    def reset(self):
        '''clean up'''
        self.tmp_dict = {}
        self.return_dict = {}
    def get_number_dict(self, some_string):
        '''do slightly better exception handling here'''
        try:
            return self.walk_string(some_string)
        except:
            #if we hit an exception, we can rely on tmp_dict 
            # being a backup to the point of the exception
            return_dict = self.tmp_dict
            self.reset()
            return return_dict

def main():
    ctd = ConvertToDict()
    for line in file(sys.argv[1]):
        line = line.strip()
        print "*" * 40
        print "line>>", line
        print ctd.get_number_dict(line)
        print "*" * 40

if __name__ == "__main__":
  pdb.runcall(main)
  #main()
Example #48
0
 def method(*args, **kwargs):
     return pdb.runcall(func, *args, **kwargs)
Example #49
0
    args = arg_parser.parse_args()

    builder = Builder(args)
    builder.build()

    application = builder.factory['Application']
    application.setup_signal_handlers()
    exit_code = application.run_sync()

    if exit:
        sys.exit(exit_code)
    else:
        return exit_code


if __name__ == '__main__':
    if os.environ.get('RUN_PROFILE'):
        import cProfile
        cProfile.run('main()', 'stats-{0}.profile'.format(int(time.time())))
        # I suggest installing runsnakerun to view the profile file graphically
    elif os.environ.get('RUN_PDB'):
        import pdb

        def wrapper():
            main(exit=False)
            pdb.set_trace()

        pdb.runcall(wrapper)
    else:
        main()
Example #50
0
    def ConnectionDispatch(self, conn):

        # config
        config = conn.base_server.get_config()
        debug = config.has_key("PythonDebug")

        try:

            handler = conn.hlist.handler
            
            # split module::handler
            l = handler.split('::', 1)
            module_name = l[0]
            if len(l) == 1:
                # no oject, provide default
                object_str = "connectionhandler"
            else:
                object_str = l[1]

            # add the directory to pythonpath if
            # not there yet, or pythonpath specified
            
            if config.has_key("PythonPath"):
                # we want to do as little evaling as possible,
                # so we remember the path in un-evaled form and
                # compare it
                global _path
                pathstring = config["PythonPath"]
                if pathstring != _path:
                    _path = pathstring
                    newpath = eval(pathstring)
                    if sys.path != newpath:
                        sys.path[:] = newpath
            else:
                if filter.dir not in sys.path:
                    sys.path[:0] = [filter.dir]

            # import module
            module = import_module(module_name, config)

            # find the object
            object = resolve_object(module, object_str,
                                    arg=conn, silent=0)

            if object:

                # call the object
                if config.has_key("PythonEnablePdb"):
                    result = pdb.runcall(object, conn)
                else:
                    result = object(conn)

                assert (type(result) == type(int())), \
                       "ConnectionHandler '%s' returned invalid return code." % handler

        except PROG_TRACEBACK, traceblock:
            # Program run-time error
            try:
                (etype, value, traceback) = traceblock
                result = self.ReportError(etype, value, traceback, srv=conn.base_server,
                                          phase="ConnectionHandler",
                                          hname=handler, debug=debug)
            finally:
                traceback = None
Example #51
0
    def HandlerDispatch(self, req):
        """
        This is the handler dispatcher.
        """

        # be cautious
        result = HTTP_INTERNAL_SERVER_ERROR

        # config
        config = req.get_config()
        debug = config.has_key("PythonDebug")

        try:
            hlist = req.hlist

            while hlist.handler:

                # split module::handler
                l = hlist.handler.split('::', 1)

                module_name = l[0]
                if len(l) == 1:
                    # no oject, provide default
                    object_str = req.phase[len("python"):].lower()
                else:
                    object_str = l[1]

                # add the directory to pythonpath if
                # not there yet, or pythonpath specified
                if config.has_key("PythonPath"):
                    # we want to do as little evaling as possible,
                    # so we remember the path in un-evaled form and
                    # compare it
                    global _path
                    pathstring = config["PythonPath"]
                    if pathstring != _path:
                        _path = pathstring
                        newpath = eval(pathstring)
                        if sys.path != newpath:
                            sys.path[:] = newpath
                else:
                    dir = hlist.directory
                    if dir not in sys.path:
                        sys.path[:0] = [dir]

                # import module
                module = import_module(module_name, config)

                # find the object
                object = resolve_object(module, object_str,
                                        arg=req, silent=hlist.silent)

                if object:

                    # call the object
                    if config.has_key("PythonEnablePdb"):
                        result = pdb.runcall(object, req)
                    else:
                        result = object(req)

                    assert (type(result) == type(int())), \
                           "Handler '%s' returned invalid return code." % hlist.handler

                    # stop cycling through handlers
                    if result != OK:
                        break
                    
                elif hlist.silent:
                    result = DECLINED

                hlist.next()

        except SERVER_RETURN, value:
            # SERVER_RETURN indicates an abort from below
            # with value as (result, status) or (result, None) or result
            try:
                if len(value.args) == 2:
                    (result, status) = value.args
                    if status:
                        req.status = status
                else:
                    result = value.args[0]
                    
                if type(result) != type(7):
                    s = "Value raised with SERVER_RETURN is invalid. It is a "
                    s = s + "%s, but it must be a tuple or an int." % type(result)
                    _apache.log_error(s, APLOG_NOERRNO|APLOG_ERR, req.server)
                    return HTTP_INTERNAL_SERVER_ERROR

            except:
                pass
Example #52
0
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import pdb

pdb.set_trace()

def pyramid1(n):
    for i in range(1, n+1): 
        print((str(i)+" ")*i)

pdb.run("pyramid1(4)")
print pdb.runeval("2+3")
pdb.runcall(pyramid1,7)


print "oioioi" 



Example #53
0
File: app.py Project: jrydberg/edgy
def runApplication(args, app):
    """
    Start specified application with the given arguments.
    """
    c = app.options()
    try:
        c.parseOptions(args)
    except usage.UsageError:
        raise
    checkPID(c['pidfile'])

    def cbStart(result):
        if not c['no-daemon']:
            daemonize()
        if c['pidfile']:
            f = open(c['pidfile'],'wb')
            f.write(str(os.getpid()))
            f.close()

    def ebStart(reason):
        print "EB", reason
        reason.printTraceback()
        global errflag
        errflag = 1
        reactor.stop()
        return reason

    def cbStop(result):
        removePID(c['pidfile'])
        return result

    def stopApp():
        stopDeferred = defer.maybeDeferred(app.stopApplication)
        stopDeferred.addBoth(cbStop)

    def reload():
        try:
            app.reloadApplication()
        except NotImplemented:
            pass

    def sigHUP(*args):
        reactor.callFromThread(reload)
    signal.signal(signal.SIGHUP, sigHUP)
        
    oldstdout, oldstderr = sys.stdout, sys.stderr
    switchUID(c['uid'], c['gid'])

    if c['logfile'] is not None:
        if c['logfile'] == '-':
            lf = sys.stdout
        else:
            lf = logfile.LogFile(os.path.basename(c['logfile']),
                                 os.path.dirname(c['logfile']) or '.')
        log.startLogging(lf)

    def start():
        startDeferred = defer.maybeDeferred(app.startApplication, c)
        startDeferred.addCallback(cbStart).addErrback(ebStart)
    reactor.callWhenRunning(start)

    reactor.addSystemEventTrigger('before', 'shutdown', stopApp)

    if c['debug']:
        sys.stdout = oldstdout
        sys.stderr = oldstderr
        signal.signal(signal.SIGUSR2, lambda *args: pdb.set_trace())
        signal.signal(signal.SIGINT, lambda *args: pdb.set_trace())
        fixPdb()
        pdb.runcall(reactor.run)
    else:
        reactor.run()
    
    if errflag:
        os.exit(1)
Example #54
0
	def testAddProcess(self):
		assert len(self.tasks) > 0
		self.pool.addProcesses(self.tasks)
		self.assertEqual(len(self.tasks) - self.pool.waitingPSize(), 
				 self.pool.pSize())

	def log(self):
		print "\n"
		print "%d-%d" % (self.pool.waitingPSize(), self.pool.pSize())
		print "\n"


import time

# The method that each task will run.
def runMethod1():
	time.sleep(2)

def runMethod():
	time.sleep(3)


def suite():
	suite = unittest.TestSuite()
	suite.addTest(TestThreadPool)
	suite.addTest(TestThreadPool)
	return suite

if __name__ == '__main__':
	pdb.runcall(unittest.main)
Example #55
0
    def FilterDispatch(self, filter):

        req = filter.req

        # config
        config = req.get_config()
        debug = config.has_key("PythonDebug")

        try:

            # split module::handler
            l = filter.handler.split('::', 1)
            module_name = l[0]
            if len(l) == 1:
                # no oject, provide default
                if filter.is_input:
                    object_str = "inputfilter"
                else:
                    object_str = "outputfilter"
            else:
                object_str = l[1]

            # add the directory to pythonpath if
            # not there yet, or pythonpath specified
            
            if config.has_key("PythonPath"):
                # we want to do as little evaling as possible,
                # so we remember the path in un-evaled form and
                # compare it
                global _path
                pathstring = config["PythonPath"]
                if pathstring != _path:
                    _path = pathstring
                    newpath = eval(pathstring)
                    if sys.path != newpath:
                        sys.path[:] = newpath
            else:
                if filter.dir not in sys.path:
                    sys.path[:0] = [filter.dir]

            # import module
            module = import_module(module_name, config)

            # find the object
            object = resolve_object(module, object_str, arg=filter, silent=0)

            if object:

                # call the object
                if config.has_key("PythonEnablePdb"):
                    pdb.runcall(object, filter)
                else:
                    object(filter)

                # always flush the filter. without a FLUSH or EOS bucket,
                # the content is never written to the network.
                # XXX an alternative is to tell the user to flush() always
                filter.flush()

        except SERVER_RETURN, value:
            # SERVER_RETURN indicates a non-local abort from below
            # with value as (result, status) or (result, None) or result
            try:
                if len(value.args) == 2:
                    (result, status) = value.args
                    if status:
                        req.status = status
                else:
                    result = value.args[0]
                    
                if type(result) != type(7):
                    s = "Value raised with SERVER_RETURN is invalid. It is a "
                    s = s + "%s, but it must be a tuple or an int." % type(result)
                    _apache.log_error(s, APLOG_NOERRNO|APLOG_ERR, req.server)

                    return

            except:
                pass
# s(tep) : execute exactly one line (smallest possible step forward)
# n(ext) : execute exactly one line in the current function
# unt(il) : continue executing until a higher line number is reached
# r(eturn) : continue executing until the current function returns
# c(ont(inue)) : continue executing until next break
# j(ump) lineno : lets you go back or forward to any line
# q(uit) : quit the debugger
#
# There are a few other commands that I don't use much (like setting
# conditional breakpoints etc.).

# Test out some of those commands
print bm.func3()
pdb.pm()

# The command 'pdb.set_trace()' sets an explicit breakpoint at some
# point in your code:
print bm.func2(4)

# You can also run a function under debugger control from the outset using:
pdb.run("bm.func3()")
# In effect, this puts a breakpoint before the function's first statement

# Two variants of 'run' are:
pdb.runeval("X")
# and
def func0(X):
    Z = 10 - Y
    return Z
pdb.runcall(func0, 3)