Uploaded Test files

This commit is contained in:
Batuhan Berk Başoğlu 2020-11-12 11:05:57 -05:00
parent f584ad9d97
commit 2e81cb7d99
16627 changed files with 2065359 additions and 102444 deletions

View file

@ -0,0 +1,45 @@
import sys
import time
class Tools:
_public_methods_ = [ 'reload', 'adddir', 'echo', 'sleep' ]
def reload(self, module):
if module in sys.modules:
try:
from imp import reload
except ImportError:
pass # builtin in py2k
reload(sys.modules[module])
return "reload succeeded."
return "no reload performed."
def adddir(self, dir):
if type(dir) == type(''):
sys.path.append(dir)
return str(sys.path)
def echo(self, arg):
return repr(arg)
def sleep(self, t):
time.sleep(t)
if __name__=='__main__':
from win32com.server.register import RegisterServer, UnregisterServer
clsid = "{06ce7630-1d81-11d0-ae37-c2fa70000000}"
progid = "Python.Tools"
verprogid = "Python.Tools.1"
if "--unregister" in sys.argv:
print("Unregistering...")
UnregisterServer(clsid, progid, verprogid)
print("Unregistered OK")
else:
print("Registering COM server...")
RegisterServer(clsid,
"win32com.servers.PythonTools.Tools",
"Python Tools",
progid,
verprogid)
print("Class registered.")

View file

@ -0,0 +1,130 @@
"""Python.Dictionary COM Server.
This module implements a simple COM server that acts much like a Python
dictionary or as a standard string-keyed VB Collection. The keys of
the dictionary are strings and are case-insensitive.
It uses a highly customized policy to fine-tune the behavior exposed to
the COM client.
The object exposes the following properties:
int Count (readonly)
VARIANT Item(BSTR key) (propget for Item)
Item(BSTR key, VARIANT value) (propput for Item)
Note that 'Item' is the default property, so the following forms of
VB code are acceptable:
set ob = CreateObject("Python.Dictionary")
ob("hello") = "there"
ob.Item("hi") = ob("HELLO")
All keys are defined, returning VT_NULL (None) if a value has not been
stored. To delete a key, simply assign VT_NULL to the key.
The object responds to the _NewEnum method by returning an enumerator over
the dictionary's keys. This allows for the following type of VB code:
for each name in ob
debug.print name, ob(name)
next
"""
import pythoncom
from win32com.server import util, policy
from win32com.server.exception import COMException
import winerror
import types
import pywintypes
from pythoncom import DISPATCH_METHOD, DISPATCH_PROPERTYGET
from winerror import S_OK
class DictionaryPolicy(policy.BasicWrapPolicy):
### BasicWrapPolicy looks for this
_com_interfaces_ = [ ]
### BasicWrapPolicy looks for this
_name_to_dispid_ = {
'item' : pythoncom.DISPID_VALUE,
'_newenum' : pythoncom.DISPID_NEWENUM,
'count' : 1,
}
### Auto-Registration process looks for these...
_reg_desc_ = 'Python Dictionary'
_reg_clsid_ = '{39b61048-c755-11d0-86fa-00c04fc2e03e}'
_reg_progid_ = 'Python.Dictionary'
_reg_verprogid_ = 'Python.Dictionary.1'
_reg_policy_spec_ = 'win32com.servers.dictionary.DictionaryPolicy'
def _CreateInstance_(self, clsid, reqIID):
self._wrap_({ })
return pythoncom.WrapObject(self, reqIID)
def _wrap_(self, ob):
self._obj_ = ob # ob should be a dictionary
def _invokeex_(self, dispid, lcid, wFlags, args, kwargs, serviceProvider):
if dispid == 0: # item
l = len(args)
if l < 1:
raise COMException(desc="not enough parameters", scode=winerror.DISP_E_BADPARAMCOUNT)
key = args[0]
if type(key) not in [str, str]:
### the nArgErr thing should be 0-based, not reversed... sigh
raise COMException(desc="Key must be a string", scode=winerror.DISP_E_TYPEMISMATCH)
key = key.lower()
if wFlags & (DISPATCH_METHOD | DISPATCH_PROPERTYGET):
if l > 1:
raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT)
try:
return self._obj_[key]
except KeyError:
return None # unknown keys return None (VT_NULL)
if l != 2:
raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT)
if args[1] is None:
# delete a key when None is assigned to it
try:
del self._obj_[key]
except KeyError:
pass
else:
self._obj_[key] = args[1]
return S_OK
if dispid == 1: # count
if not wFlags & DISPATCH_PROPERTYGET:
raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND) # not found
if len(args) != 0:
raise COMException(scode=winerror.DISP_E_BADPARAMCOUNT)
return len(self._obj_)
if dispid == pythoncom.DISPID_NEWENUM:
return util.NewEnum(list(self._obj_.keys()))
raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND)
def _getidsofnames_(self, names, lcid):
### this is a copy of MappedWrapPolicy._getidsofnames_ ...
name = names[0].lower()
try:
return (self._name_to_dispid_[name],)
except KeyError:
raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND,
desc="Member not found")
def Register():
from win32com.server.register import UseCommandLine
return UseCommandLine(DictionaryPolicy)
if __name__ == '__main__':
Register()

View file

@ -0,0 +1,52 @@
"""Python.Interpreter COM Server
This module implements a very very simple COM server which
exposes the Python interpreter.
This is designed more as a demonstration than a full blown COM server.
General functionality and Error handling are both limited.
To use this object, ensure it is registered by running this module
from Python.exe. Then, from Visual Basic, use "CreateObject('Python.Interpreter')",
and call its methods!
"""
from win32com.server.exception import Exception
import winerror
# Expose the Python interpreter.
class Interpreter:
"""The interpreter object exposed via COM
"""
_public_methods_ = [ 'Exec', 'Eval' ]
# All registration stuff to support fully automatic register/unregister
_reg_verprogid_ = "Python.Interpreter.2"
_reg_progid_ = "Python.Interpreter"
_reg_desc_ = "Python Interpreter"
_reg_clsid_ = "{30BD3490-2632-11cf-AD5B-524153480001}"
_reg_class_spec_ = "win32com.servers.interp.Interpreter"
def __init__(self):
self.dict = {}
def Eval(self, exp):
"""Evaluate an expression.
"""
if type(exp) not in [str, str]:
raise Exception(desc="Must be a string",scode=winerror.DISP_E_TYPEMISMATCH)
return eval(str(exp), self.dict)
def Exec(self, exp):
"""Execute a statement.
"""
if type(exp) not in [str, str]:
raise Exception(desc="Must be a string",scode=winerror.DISP_E_TYPEMISMATCH)
exec(str(exp), self.dict)
def Register():
import win32com.server.register
return win32com.server.register.UseCommandLine(Interpreter)
if __name__=='__main__':
print("Registering COM server...")
Register()

View file

@ -0,0 +1,27 @@
"""A COM Server which exposes the NT Performance monitor in a very rudimentary way
Usage from VB:
set ob = CreateObject("Python.PerfmonQuery")
freeBytes = ob.Query("Memory", "Available Bytes")
"""
from win32com.server import exception, register
import pythoncom, win32pdhutil, winerror
class PerfMonQuery:
_reg_verprogid_ = "Python.PerfmonQuery.1"
_reg_progid_ = "Python.PerfmonQuery"
_reg_desc_ = "Python Performance Monitor query object"
_reg_clsid_ = "{64cef7a0-8ece-11d1-a65a-00aa00125a98}"
_reg_class_spec_ = "win32com.servers.perfmon.PerfMonQuery"
_public_methods_ = [ 'Query' ]
def Query(self, object, counter, instance = None, machine = None):
try:
return win32pdhutil.GetPerformanceAttributes(object, counter, instance, machine=machine)
except win32pdhutil.error as exc:
raise exception.Exception(desc=exc.strerror)
except TypeError as desc:
raise exception.Exception(desc=desc,scode=winerror.DISP_E_TYPEMISMATCH)
if __name__=='__main__':
print("Registering COM server...")
register.UseCommandLine(PerfMonQuery)

View file

@ -0,0 +1,168 @@
# This is part of the Python test suite.
# The object is registered when you first run the test suite.
# (and hopefully unregistered once done ;-)
# Ensure the vtables in the tlb are known.
from win32com import universal
from win32com.server.exception import COMException
from win32com.client import gencache
import winerror
from win32com.client import constants
from win32com.server.util import wrap
import pythoncom
pythoncom.__future_currency__ = True
# We use the constants from the module, so must insist on a gencache.
# Otherwise, use of gencache is not necessary (tho still advised)
gencache.EnsureModule('{6BCDCB60-5605-11D0-AE5F-CADD4C000000}', 0, 1, 1)
class PyCOMTest:
_typelib_guid_ = "{6BCDCB60-5605-11D0-AE5F-CADD4C000000}"
_typelib_version = 1,0
_com_interfaces_ = ['IPyCOMTest']
_reg_clsid_ = "{e743d9cd-cb03-4b04-b516-11d3a81c1597}"
_reg_progid_ = "Python.Test.PyCOMTest"
def DoubleString(self, str):
return str*2
def DoubleInOutString(self, str):
return str*2
def Fire(self, nID):
raise COMException(hresult=winerror.E_NOTIMPL)
def GetLastVarArgs(self):
raise COMException(hresult=winerror.E_NOTIMPL)
def GetMultipleInterfaces(self, outinterface1, outinterface2):
raise COMException(hresult=winerror.E_NOTIMPL)
def GetSafeArrays(self, attrs, attrs2, ints):
raise COMException(hresult=winerror.E_NOTIMPL)
def GetSetDispatch(self, indisp):
raise COMException(hresult=winerror.E_NOTIMPL)
# Result is of type IPyCOMTest
def GetSetInterface(self, ininterface):
return wrap(self)
def GetSetVariant(self, indisp):
return indisp
def TestByRefVariant(self, v):
return v * 2
def TestByRefString(self, v):
return v * 2
# Result is of type IPyCOMTest
def GetSetInterfaceArray(self, ininterface):
raise COMException(hresult=winerror.E_NOTIMPL)
def GetSetUnknown(self, inunk):
raise COMException(hresult=winerror.E_NOTIMPL)
# Result is of type ISimpleCounter
def GetSimpleCounter(self):
raise COMException(hresult=winerror.E_NOTIMPL)
def GetSimpleSafeArray(self, ints):
raise COMException(hresult=winerror.E_NOTIMPL)
def GetStruct(self):
raise COMException(hresult=winerror.E_NOTIMPL)
def SetIntSafeArray(self, ints):
return len(ints)
def SetLongLongSafeArray(self, ints):
return len(ints)
def SetULongLongSafeArray(self, ints):
return len(ints)
def SetBinSafeArray(self, buf):
return len(buf)
def SetVarArgs(self, *args):
raise COMException(hresult=winerror.E_NOTIMPL)
def SetVariantSafeArray(self, vars):
raise COMException(hresult=winerror.E_NOTIMPL)
def Start(self):
raise COMException(hresult=winerror.E_NOTIMPL)
def Stop(self, nID):
raise COMException(hresult=winerror.E_NOTIMPL)
def StopAll(self):
raise COMException(hresult=winerror.E_NOTIMPL)
def TakeByRefDispatch(self, inout):
raise COMException(hresult=winerror.E_NOTIMPL)
def TakeByRefTypedDispatch(self, inout):
raise COMException(hresult=winerror.E_NOTIMPL)
def Test(self, key, inval):
return not inval
def Test2(self, inval):
return inval
def Test3(self, inval):
raise COMException(hresult=winerror.E_NOTIMPL)
def Test4(self, inval):
raise COMException(hresult=winerror.E_NOTIMPL)
def Test5(self, inout):
if inout == constants.TestAttr1:
return constants.TestAttr1_1
elif inout == constants.TestAttr1_1:
return constants.TestAttr1
else:
return -1
def Test6(self, inval):
return inval
def TestOptionals(self, strArg='def', sval=0, lval=1, dval=3.1400001049041748):
raise COMException(hresult=winerror.E_NOTIMPL)
def TestOptionals2(self, dval, strval='', sval=1):
raise COMException(hresult=winerror.E_NOTIMPL)
def CheckVariantSafeArray(self, data):
return 1
def LongProp(self):
return self.longval
def SetLongProp(self, val):
self.longval = val
def ULongProp(self):
return self.ulongval
def SetULongProp(self, val):
self.ulongval = val
def IntProp(self):
return self.intval
def SetIntProp(self, val):
self.intval = val
class PyCOMTestMI(PyCOMTest):
_typelib_guid_ = "{6BCDCB60-5605-11D0-AE5F-CADD4C000000}"
_typelib_version = 1,0
# Interfaces with a interface name, a real IID, and an IID as a string
_com_interfaces_ = ['IPyCOMTest',
pythoncom.IID_IStream,
str(pythoncom.IID_IStorage),
]
_reg_clsid_ = "{F506E9A1-FB46-4238-A597-FA4EB69787CA}"
_reg_progid_ = "Python.Test.PyCOMTestMI"
if __name__ == '__main__':
import win32com.server.register
win32com.server.register.UseCommandLine(PyCOMTest)
win32com.server.register.UseCommandLine(PyCOMTestMI)