示例#1
0
    def exceptionThrow(self, id, exc, hasHandler):
        if hasHandler:
            return
        currentCtx = self.engine().currentContext()
        ctx = currentCtx
        tb = []
        while True:
            #generate traceback entry
            info = QtScript.QScriptContextInfo(ctx)
            if not (info.functionName() or info.functionType()
                    == QtScript.QScriptContextInfo.ScriptFunction):
                #python code; we can stop because from here on python's
                #traceback mechanism takes over.
                break
            lineNumber = "?" if info.lineNumber() < 1 else info.lineNumber()
            try:
                line = unicode(
                    self._scripts[info.scriptId()][lineNumber]).strip()
            except KeyError:
                line = "<content unknown>"
            tb.append(
                TRACEBACK_TEMPLATE.format(filename=info.fileName(),
                                          lineNumber=lineNumber,
                                          funcName=info.functionName()
                                          or "<global>",
                                          line=line))

            #get next context
            ctx = ctx.parentContext()

        tb.reverse()
        self.traceback = u"\n".join(tb)
示例#2
0
 def _newProxy(self, ProxyClass, value):
     proxy = ProxyClass(value, self._toJSValue, self._toPythonValue,
                        self._engine)
     self._proxyReferences.add(proxy)
     id = self._newId()
     self._objectCache[id] = value
     return self._engine.newObject(proxy, QtScript.QScriptValue(id))
示例#3
0
 def _convertImmutablePythonValue(self, value):
     #immutable values first
     with utils.ignored(TypeError):
         return QtScript.QScriptValue(value)
     #including null (None)
     if value is None:
         return self._engine.nullValue()
     raise ValueError("Unknown value type")
示例#4
0
def run():

    t = DoNothing()

    engine = QtScript.QScriptEngine()

    button = QtGui.QPushButton()
    button = engine.newQObject(button)
    engine.globalObject().setProperty("button", button)

    app.connect(button, SIGNAL("clicked()"), t.button_click)

    engine.evaluate("button.text = 'Do Twisted Gui Trial'")
    engine.evaluate("button.styleSheet = 'font-style: italic'")
    engine.evaluate("button.show()")

    app.exec_()
    print('fell off the bottom?...')
示例#5
0
    def __init__(self, *args, **kwargs):
        super(JSEvaluator, self).__init__(*args, **kwargs)

        sys.excepthook = self._excepthook

        if not QtCore.QCoreApplication.instance():
            self._app = QtCore.QCoreApplication(sys.argv)

        self._engine = QtScript.QScriptEngine()
        self._trackingAgent = TrackingAgent(self._engine)
        self._engine.setAgent(self._trackingAgent)

        self._functionCache = {}
        self._objectCache = {}
        self._proxyReferences = set()
        self._counter = itertools.count()

        self.eval(jsproxies.PYTHON_ERROR_DEFINITION)
        self.eval(CONSOLE_DEFINITION)
        self["console"]["_pythonPrint"] = self._pythonPrint
示例#6
0
    def _wrapPythonFunction(self, value):
        if not value in self._functionCache:

            def wrapper(context, engine):
                try:
                    result = self._callPythonFunction(context, value)
                except BaseException, e:
                    #store the exception class so it can be reused
                    #later.
                    pythonExceptionStore[e.__class__.__name__] = e.__class__

                    #convert exceptions to JS exceptions
                    #
                    #tb_next to hide JSEvaluator internals that are
                    #only distraction to library users.
                    pythonTbInfo = sys.exc_info()[2].tb_next.tb_next
                    newTraceback = StringIO.StringIO()
                    limit = self._tbLength(pythonTbInfo)
                    if hasattr(e, "oldTraceback"):
                        #same here: remove distracting internals
                        limit -= 2
                    traceback.print_tb(pythonTbInfo,
                                       file=newTraceback,
                                       limit=limit)
                    combinedTraceback = (
                        newTraceback.getvalue().strip("\n") + "\n" +
                        getattr(e, "oldTraceback", "")).strip("\n")
                    del pythonTbInfo

                    args = (e.__class__.__name__, unicode(e),
                            combinedTraceback)
                    jsError = self["JSEvaluatorPythonError"].new(*args)
                    context.throwValue(self._toJSValue(jsError))
                    return self._engine.undefinedValue()
                return QtScript.QScriptValue(self._toJSValue(result))

            self._functionCache[value] = self._engine.newFunction(wrapper)
示例#7
0
            self.running = False
            print 'CLICK: calling reactor stop...'
            reactor.stop()
            print 'reactor stop called....'
        else:
            self.running = True
            print 'CLICK: entering run'
            reactor.run()
            print 'reactor run returned...'

    def printStat(self):
        print 'tick...'


t = doNothing()

engine = QtScript.QScriptEngine()

button = QtGui.QPushButton()
scriptButton = engine.newQObject(button)
engine.globalObject().setProperty("button", scriptButton)

app.connect(button, SIGNAL("clicked()"), t.buttonClick)

engine.evaluate("button.text = 'Hello World!'")
engine.evaluate("button.styleSheet = 'font-style: italic'")
engine.evaluate("button.show()")

app.exec_()
print 'fell off the bottom?...'
示例#8
0
def debugprint(ctx, eng):  #for javascript to print to console
    print ctx.argument(0).toString()
    return QtScript.QScriptValue(eng, 0)