Skip to content

Commit 9983f4a

Browse files
committed
Use __enter__ and __exit__ instead of __init__ and __del__
Execution of __del__ is not guaranteed due to the garbage collector.
1 parent 6344810 commit 9983f4a

5 files changed

Lines changed: 20 additions & 28 deletions

File tree

linux/drm.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ def __init__(self):
4444
self.drmGetVersion.restype = drmVersionPtr
4545

4646
class DRM:
47-
def __init__(self, device):
47+
def __enter__(self, device):
4848
self.libdrm = libdrm()
4949

5050
if type(device) == str:
5151
self.fd = os.open(device, os.O_RDWR)
5252
elif type(device) == int:
5353
self.fd = device
5454

55-
def __del__(self):
55+
def __exit__(self):
5656
os.close(self.fd)
5757

5858
def GetVersion(self):
@@ -61,15 +61,7 @@ def GetVersion(self):
6161
self.libdrm.drmFreeVersion(version)
6262
return result
6363

64-
def open(device):
65-
if type(device) == str:
66-
fd = os.open(device, os.O_RDWR)
67-
elif type(device) == int:
68-
fd = device
69-
70-
return DRM(fd)
71-
7264
if __name__ == '__main__':
73-
drm = open(sys.argv[1])
74-
version = drm.GetVersion()
75-
print(version)
65+
with DRM(sys.argv[1]) as drm:
66+
version = drm.GetVersion()
67+
print(version)

linux/system.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class Watchdog():
184184
WDIOS_DISABLECARD = 0x0001
185185
WDIOS_ENABLECARD = 0x0002
186186

187-
def __init__(self, path):
187+
def __enter__(self, path):
188188
self.fd = os.open(path, os.O_RDWR)
189189

190190
def disable(self):
@@ -202,7 +202,7 @@ def set_timeout(self, timeout):
202202

203203
libc.ioctl(self.fd, Watchdog.WDIOC_SETTIMEOUT, timeout)
204204

205-
def __del__(self):
205+
def __exit__(self):
206206
options = ctypes.pointer(ctypes.c_uint(Watchdog.WDIOS_DISABLECARD))
207207
libc.ioctl(self.fd, Watchdog.WDIOC_SETOPTIONS, options)
208208
os.close(self.fd)

linux/watchdog.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Watchdog():
1111
WDIOS_DISABLECARD = 0x0001
1212
WDIOS_ENABLECARD = 0x0002
1313

14-
def __init__(self, path):
14+
def __enter__(self, path):
1515
self.fd = os.open(path, os.O_RDWR)
1616

1717
def disable(self):
@@ -29,7 +29,7 @@ def set_timeout(self, timeout):
2929

3030
libc.ioctl(self.fd, Watchdog.WDIOC_SETTIMEOUT, timeout)
3131

32-
def __del__(self):
32+
def __exit__(self):
3333
options = ctypes.pointer(ctypes.c_uint(Watchdog.WDIOS_DISABLECARD))
3434
libc.ioctl(self.fd, Watchdog.WDIOC_SETOPTIONS, options)
3535
os.close(self.fd)

tests/drm.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ def test_list_devices(self):
2727
print(' %s (%u, %u)' % (device.device_node, os.major(devno),
2828
os.minor(devno)))
2929

30-
dev = linux.drm.open(device.device_node)
31-
version = dev.GetVersion()
32-
print(' Driver:', version.name)
33-
print(' Description:', version.description)
34-
print(' Version: %u.%u.%u (%s)' % (version.major,
35-
version.minor,
36-
version.patch,
37-
version.date))
30+
with linux.drm.DRM(device.device_node) as dev:
31+
version = dev.GetVersion()
32+
print(' Driver:', version.name)
33+
print(' Description:', version.description)
34+
print(' Version: %u.%u.%u (%s)' % (version.major,
35+
version.minor,
36+
version.patch,
37+
version.date))
3838

3939
submit_vic_path = '/root/drm/tests/tegra/submit_vic'
4040
def submit_vic_exists():

tests/system.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ class watchdog(unittest.TestCase):
4545
def test(self):
4646
print('Testing watchdog')
4747

48-
wdt = system.Watchdog('/dev/watchdog')
49-
wdt.set_timeout(30)
50-
wdt.enable()
48+
with system.Watchdog('/dev/watchdog') as wdt:
49+
wdt.set_timeout(30)
50+
wdt.enable()

0 commit comments

Comments
 (0)