Skip to content

Commit

Permalink
Add ActiveObjTest to test_comserver. (#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
junkmd authored Jan 27, 2025
1 parent 547e736 commit 00be866
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
13 changes: 6 additions & 7 deletions comtypes/test/TestComServer.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import sys, os
import logging
import os
import sys

logging.basicConfig()
##logging.basicConfig(level=logging.DEBUG)
##logger = logging.getLogger(__name__)

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), r"..\..")))

import ctypes
import comtypes
from comtypes.hresult import *
import comtypes.client
import comtypes.connectionpoints
import comtypes.errorinfo
import comtypes.server
import comtypes.server.connectionpoints
import comtypes.typeinfo
from comtypes.hresult import S_OK

################################################################

Expand All @@ -30,6 +31,7 @@

# Import the wrapper
from comtypes.gen import TestComServerLib
from comtypes.gen.TestComServerLib import ITestComServer # noqa

################################################################

Expand Down Expand Up @@ -151,7 +153,4 @@ def MixedInOut(self, a, c):
import traceback

traceback.print_exc()
if sys.version_info >= (3, 0):
input()
else:
raw_input()
input()
32 changes: 31 additions & 1 deletion comtypes/test/test_comserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from ctypes import pointer
from typing import Any

import comtypes.server
import comtypes.test.TestComServer
from comtypes import BSTR
from comtypes.automation import VARIANT, _midlSAFEARRAY
from comtypes.client import CreateObject
from comtypes.client import CreateObject, GetActiveObject
from comtypes.server.register import register, unregister
from comtypes.test.find_memleak import find_memleak

Expand Down Expand Up @@ -171,6 +172,35 @@ def test_mixedinout(self):
pass


class ActiveObjTest(unittest.TestCase):
def get_active_object(self) -> comtypes.test.TestComServer.ITestComServer:
return GetActiveObject(
comtypes.test.TestComServer.TestComServer,
interface=comtypes.test.TestComServer.ITestComServer,
)

def _doit(self, weak: bool):
comobj = comtypes.test.TestComServer.TestComServer()
with self.assertRaises(OSError):
self.get_active_object()
handle = comtypes.server.RegisterActiveObject(comobj, weak=weak)
activeobj = self.get_active_object()
self.assertEqual(activeobj.MixedInOut(2, 4), (3, 5))
self.assertEqual(activeobj.AddRef(), 3)
self.assertEqual(activeobj.Release(), 2)
punk = comobj.QueryInterface(comtypes.test.TestComServer.ITestComServer)
self.assertEqual(activeobj, punk)
comtypes.server.RevokeActiveObject(handle)
with self.assertRaises(OSError):
self.get_active_object()

def test_activeobj_weak_registration(self):
self._doit(weak=True)

def test_activeobj_strong_registration(self):
self._doit(weak=False)


class VariantTest(unittest.TestCase):
def test_UDT(self):
from comtypes.gen.TestComServerLib import MYCOLOR
Expand Down

0 comments on commit 00be866

Please sign in to comment.