leakTest.py :  » Windows » pyExcelerator » pywin32-214 » com » win32comext » AXScript » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Windows » pyExcelerator 
pyExcelerator » pywin32 214 » com » win32comext » AXScript » test » leakTest.py
import sys
from win32com.axscript.server.error import Exception
from win32com.axscript import axscript
from win32com.axscript.server import axsite
import pythoncom
from win32com.server import util,connect
import win32com.server.policy

class MySite(axsite.AXSite):

  def OnScriptError(self, error):
    exc = error.GetExceptionInfo()
    context, line, char = error.GetSourcePosition()
    print " >Exception:", exc[1]
    try:
      st = error.GetSourceLineText()
    except pythoncom.com_error:
      st = None
    if st is None: st = ""
    text = st + "\n" + (" " * (char-1)) + "^" + "\n" + exc[2]
    for line in text.splitlines():
      print "  >" + line

class MyCollection(util.Collection):
  def _NewEnum(self):
    print "Making new Enumerator"
    return util.Collection._NewEnum(self)

class Test:
  _public_methods_ = [ 'echo' ]
  _public_attrs_ = ['collection', 'verbose']
  def __init__(self):
    self.verbose = 0
    self.collection = util.wrap( MyCollection( [1,'Two',3] ))
    self.last = ""
#    self._connect_server_ = TestConnectServer(self)

  def echo(self, *args):
    self.last = ''.join(map(str, args))
    if self.verbose:
      for arg in args:
        print arg,
      print
#    self._connect_server_.Broadcast(last)


#### Connections currently wont work, as there is no way for the engine to
#### know what events we support.  We need typeinfo support.

IID_ITestEvents = pythoncom.MakeIID("{8EB72F90-0D44-11d1-9C4B-00AA00125A98}")

class TestConnectServer(connect.ConnectableServer):
  _connect_interfaces_ = [IID_ITestEvents]
  # The single public method that the client can call on us
  # (ie, as a normal COM server, this exposes just this single method.
  def __init__(self, object):
    self.object = object
    
  def Broadcast(self,arg):
    # Simply broadcast a notification.
    self._BroadcastNotify(self.NotifyDoneIt, (arg,))

  def NotifyDoneIt(self, interface, arg):
    interface.Invoke(1000, 0, pythoncom.DISPATCH_METHOD, 1, arg)

VBScript = """\
prop = "Property Value"

sub hello(arg1)
   test.echo arg1
end sub
  
sub testcollection
   test.verbose = 1
   for each item in test.collection
     test.echo "Collection item is", item
   next
end sub
"""
PyScript = """\
print "PyScript is being parsed..."
prop = "Property Value"
def hello(arg1):
   test.echo(arg1)
   pass
   
def testcollection():
   test.verbose = 1
#   test.collection[1] = "New one"
   for item in test.collection:
     test.echo("Collection item is", item)
   pass
"""

ErrScript = """\
bad code for everyone!
"""


def TestEngine(engineName, code, bShouldWork = 1):
  echoer = Test()
  model = {
    'test' : util.wrap(echoer),
    }

  site = MySite(model)
  engine = site._AddEngine(engineName)
  engine.AddCode(code, axscript.SCRIPTTEXT_ISPERSISTENT)
  try:
    engine.Start()
  finally:
    if not bShouldWork:
      engine.Close()
      return
  doTestEngine(engine, echoer)
  # re-transition the engine back to the UNINITIALIZED state, a-la ASP.
  engine.eScript.SetScriptState(axscript.SCRIPTSTATE_UNINITIALIZED)
  engine.eScript.SetScriptSite(util.wrap(site))
  print "restarting"
  engine.Start()
  # all done!
  engine.Close()

def doTestEngine(engine, echoer):    
  # Now call into the scripts IDispatch
  from win32com.client.dynamic import Dispatch
  ob = Dispatch(engine.GetScriptDispatch())
  try:
    ob.hello("Goober")
  except pythoncom.com_error, exc:
    print "***** Calling 'hello' failed", exc
    return
  if echoer.last != "Goober":
    print "***** Function call didnt set value correctly", repr(echoer.last)
    
  if str(ob.prop) != "Property Value":
    print "***** Property Value not correct - ", repr(ob.prop)

  ob.testcollection()

  # Now make sure my engines can evaluate stuff.
  result = engine.eParse.ParseScriptText("1+1", None, None, None, 0, 0, axscript.SCRIPTTEXT_ISEXPRESSION)
  if result != 2:
    print "Engine could not evaluate '1+1' - said the result was", result

def dotestall():
  for i in xrange(10):
    TestEngine("Python", PyScript)
    print sys.gettotalrefcount()
##  print "Testing Exceptions"
##  try:
##    TestEngine("Python", ErrScript, 0)
##  except pythoncom.com_error:
##    pass
   

def testall():
  dotestall()
  pythoncom.CoUninitialize()
  print "AXScript Host worked correctly - %d/%d COM objects left alive." % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())

if __name__ == '__main__':
  testall()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.