Skip to content

Commit

Permalink
Merge pull request #749 from CristiFati/master
Browse files Browse the repository at this point in the history
win32structures.RECT refinements and fixes
  • Loading branch information
airelil committed Jun 6, 2019
2 parents 1e7d0f4 + 8aa052d commit 06b4945
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 57 deletions.
39 changes: 30 additions & 9 deletions pywinauto/unittests/test_win32functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
import unittest

import sys
import ctypes
sys.path.append(".")
from pywinauto.win32structures import POINT # noqa: E402
from pywinauto.win32structures import RECT # noqa: E402
from pywinauto.win32structures import Structure, POINT, RECT # noqa: E402
from pywinauto.win32functions import MakeLong, HiWord, LoWord # noqa: E402


Expand Down Expand Up @@ -137,19 +137,40 @@ def testPOINTcomparision(self):
self.assertNotEqual(p0, 1)

def test_RECT_hash(self):
"""Test RECT is hashable"""
r0 = RECT(0)
r1 = RECT(1)
d = { "r0": r0, "r1": r1 }
self.assertEqual(r0, d["r0"])
self.assertEqual(r1, d["r1"])
self.assertNotEqual(r0, r1)
"""Test RECT is not hashable"""
self.assertRaises(TypeError, hash, RECT())

def test_RECT_eq(self):
r0 = RECT(1, 2, 3, 4)
self.assertEqual(r0, RECT(1, 2, 3, 4))
self.assertEqual(r0, [1, 2, 3, 4])
self.assertNotEqual(r0, RECT(1, 2, 3, 5))
self.assertNotEqual(r0, [1, 2, 3, 5])
self.assertNotEqual(r0, [1, 2, 3])
self.assertNotEqual(r0, [1, 2, 3, 4, 5])
r0.bottom = 5
self.assertEqual(r0, RECT(1, 2, 3, 5))
self.assertEqual(r0, (1, 2, 3, 5))

def test_RECT_repr(self):
"""Test RECT repr"""
r0 = RECT(0)
self.assertEqual(r0.__repr__(), "<RECT L0, T0, R0, B0>")

def test_Structure(self):
class Structure0(Structure):
_fields_ = [("f0", ctypes.c_int)]

class Structure1(Structure):
_fields_ = [("f1", ctypes.c_int)]

s0 = Structure0(0)
self.assertEqual(str(s0), "%20s\t%s" % ("f0", s0.f0))
s1 = Structure1(0)
self.assertNotEqual(s0, s1)
s0._fields_.append(("f1", ctypes.c_int))
self.assertNotEqual(s0, [0, 1])


if __name__ == "__main__":
unittest.main()
77 changes: 29 additions & 48 deletions pywinauto/win32structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,47 +51,51 @@ def __str__(self):
fields in exceptList will not be printed"""
lines = []
for f in self._fields_:
name = f[0]
lines.append("%20s\t%s"% (name, getattr(self, name)))
for field_name, _ in getattr(self, "_fields_", []):
lines.append("%20s\t%s"% (field_name, getattr(self, field_name)))

return "\n".join(lines)

#----------------------------------------------------------------
def __eq__(self, other_struct):
"""Return True if the two structures have the same coordinates"""
if isinstance(other_struct, Struct):
def __eq__(self, other):
"""Return True if the two instances have the same coordinates"""
fields = getattr(self, "_fields_", [])
if isinstance(other, Struct):
try:
# pretend they are two structures - check that they both
# have the same value for all fields
are_equal = True
for field in self._fields_:
name = field[0]
if getattr(self, name) != getattr(other_struct, name):
are_equal = False
break

return are_equal
if len(fields) != len(getattr(other, "_fields_", [])):
return False
for field_name, _ in fields:
if getattr(self, field_name) != getattr(other, field_name):
return False
return True

except AttributeError:
return False

if isinstance(other_struct, (list, tuple)):
elif isinstance(other, (list, tuple)):
# Now try to see if we have been passed in a list or tuple
if len(fields) != len(other):
return False
try:
are_equal = True
for i, field in enumerate(self._fields_):
name = field[0]
if getattr(self, name) != other_struct[i]:
are_equal = False
break
return are_equal
for i, (field_name, _) in enumerate(fields):
if getattr(self, field_name) != other[i]:
return False
return True

except Exception:
return False

return False

#----------------------------------------------------------------
def __ne__(self, other):
"""Return False if the two instances have the same coordinates"""
return not self.__eq__(other)

__hash__ = None


class Structure(Struct, StructureMixIn):

Expand Down Expand Up @@ -185,7 +189,7 @@ def __getitem__(self, key):


# ====================================================================
class RECT(wintypes.RECT):
class RECT(wintypes.RECT, StructureMixIn):

"""Wrap the RECT structure and add extra functionality"""

Expand Down Expand Up @@ -214,28 +218,6 @@ def __init__(self, otherRect_or_left=0, top=0, right=0, bottom=0):
self.top = long_int(top)
self.bottom = long_int(bottom)

# ----------------------------------------------------------------
def __eq__(self, otherRect):
"""Return true if the two rectangles have the same coordinates"""
try:
return \
self.left == otherRect.left and \
self.top == otherRect.top and \
self.right == otherRect.right and \
self.bottom == otherRect.bottom
except AttributeError:
return False

# ----------------------------------------------------------------
def __ne__(self, otherRect):
"""Return true if the two rectangles do not have the same coordinates"""
return not self == otherRect

# ----------------------------------------------------------------
def __hash__(self):
"""Return unique object ID to make the instance hashable"""
return id(self)

# ----------------------------------------------------------------
def __str__(self):
"""Return a string representation of the RECT"""
Expand Down Expand Up @@ -292,14 +274,13 @@ def mid_point(self):
pt.y = self.top + int(float(self.height()) / 2.)
return pt

#def __hash__(self):
# return hash (self.left, self.top, self.right, self.bottom)
__reduce__ = _reduce

RECT.__reduce__ = _reduce

assert sizeof(RECT) == 16, sizeof(RECT)
assert alignment(RECT) == 4, alignment(RECT)


class SETTEXTEX(Structure):
_pack_ = 1
_fields_ = [
Expand Down

0 comments on commit 06b4945

Please sign in to comment.