File: git-pythonclose

package info (click to toggle)
brltty 5.6-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 25,116 kB
  • sloc: ansic: 117,871; sh: 6,590; java: 4,785; xml: 3,451; makefile: 1,973; tcl: 1,499; awk: 611; ml: 293; python: 250; lisp: 52
file content (55 lines) | stat: -rw-r--r-- 1,647 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
commit 816084d46d117158a3767330ee8a7d0d536136bc
Author: Samuel Thibault <samuel.thibault@ens-lyon.org>
Date:   Tue Apr 3 18:13:03 2018 +0200

    Add closeConnection() python method
    
    so applications can be sure they have released the file descriptor and only
    memory potentially remains until python garbage collection.

---
 Bindings/Python/apitest.py |    3 +--
 Bindings/Python/brlapi.pyx |   12 ++++++++++--
 2 files changed, 11 insertions(+), 4 deletions(-)

--- a/Bindings/Python/apitest.py
+++ b/Bindings/Python/apitest.py
@@ -122,8 +122,7 @@ if __name__ == "__main__":
         sys.stdout.write(line + "\n")
 
       brl.leaveTtyMode()
-    finally:
-      del brl
+      brl.closeConnection()
   except brlapi.ConnectionError as e:
     if e.brlerrno == brlapi.ERROR_CONNREFUSED:
       logMessage("Connection to %s refused. BRLTTY is too busy..." % e.host)
--- a/Bindings/Python/brlapi.pyx
+++ b/Bindings/Python/brlapi.pyx
@@ -40,6 +40,7 @@ try:
   b.readKey()
 
   b.leaveTtyMode()
+  b.closeConnection()
 
 except brlapi.ConnectionError as e:
   if e.brlerrno == brlapi.ERROR_CONNREFUSED:
@@ -303,9 +304,16 @@ cdef class Connection:
 			c_brlapi.free(self.h)
 			raise ConnectionError(self.settings.host, self.settings.auth)
 
-	def __del__(self):
+	def closeConnection(self):
 		"""Close the BrlAPI connection"""
-		c_brlapi.brlapi__closeConnection(self.h)
+		if self.fd != -1:
+			c_brlapi.brlapi__closeConnection(self.h)
+			self.fd = -1
+
+	def __del__(self):
+		"""Release resources used by the connection"""
+		if self.fd != -1:
+			c_brlapi.brlapi__closeConnection(self.h)
 		c_brlapi.free(self.h)
 
 	property host: