1 |
commit: 27b6a21bd8b4f9372c328b979ee0ba0a8020a2b9 |
2 |
Author: Zac Medico <zmedico <AT> gentoo <DOT> org> |
3 |
AuthorDate: Fri Dec 2 01:43:57 2011 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Fri Dec 2 01:43:57 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/portage.git;a=commit;h=27b6a21b |
7 |
|
8 |
AsynchronousLock: use os.read/write |
9 |
|
10 |
Similar to commit b432a1b3051d91546649e8f3190675767461d8e8, don't use |
11 |
unecessary file objects. It also happens that these changes fix |
12 |
compatibility issues with PyPy. |
13 |
|
14 |
--- |
15 |
pym/_emerge/AsynchronousLock.py | 43 +++++++++++++++++++++++++------------- |
16 |
1 files changed, 28 insertions(+), 15 deletions(-) |
17 |
|
18 |
diff --git a/pym/_emerge/AsynchronousLock.py b/pym/_emerge/AsynchronousLock.py |
19 |
index be498b8..39e36c8 100644 |
20 |
--- a/pym/_emerge/AsynchronousLock.py |
21 |
+++ b/pym/_emerge/AsynchronousLock.py |
22 |
@@ -3,6 +3,7 @@ |
23 |
|
24 |
import dummy_threading |
25 |
import fcntl |
26 |
+import errno |
27 |
import logging |
28 |
import sys |
29 |
|
30 |
@@ -114,12 +115,12 @@ class _LockThread(AbstractPollTask): |
31 |
def _start(self): |
32 |
pr, pw = os.pipe() |
33 |
self._files = {} |
34 |
- self._files['pipe_read'] = os.fdopen(pr, 'rb', 0) |
35 |
- self._files['pipe_write'] = os.fdopen(pw, 'wb', 0) |
36 |
+ self._files['pipe_read'] = pr |
37 |
+ self._files['pipe_write'] = pw |
38 |
for k, f in self._files.items(): |
39 |
- fcntl.fcntl(f.fileno(), fcntl.F_SETFL, |
40 |
- fcntl.fcntl(f.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK) |
41 |
- self._reg_id = self.scheduler.register(self._files['pipe_read'].fileno(), |
42 |
+ fcntl.fcntl(f, fcntl.F_SETFL, |
43 |
+ fcntl.fcntl(f, fcntl.F_GETFL) | os.O_NONBLOCK) |
44 |
+ self._reg_id = self.scheduler.register(self._files['pipe_read'], |
45 |
PollConstants.POLLIN, self._output_handler) |
46 |
self._registered = True |
47 |
threading_mod = threading |
48 |
@@ -130,10 +131,16 @@ class _LockThread(AbstractPollTask): |
49 |
|
50 |
def _run_lock(self): |
51 |
self._lock_obj = lockfile(self.path, wantnewlockfile=True) |
52 |
- self._files['pipe_write'].write(b'\0') |
53 |
+ os.write(self._files['pipe_write'], b'\0') |
54 |
|
55 |
def _output_handler(self, f, event): |
56 |
- buf = self._read_buf(self._files['pipe_read'], event) |
57 |
+ buf = None |
58 |
+ if event & PollConstants.POLLIN: |
59 |
+ try: |
60 |
+ buf = os.read(self._files['pipe_read'], self._bufsize) |
61 |
+ except IOError as e: |
62 |
+ if e.errno not in (errno.EAGAIN,): |
63 |
+ raise |
64 |
if buf: |
65 |
self._unregister() |
66 |
self.returncode = os.EX_OK |
67 |
@@ -171,7 +178,7 @@ class _LockThread(AbstractPollTask): |
68 |
|
69 |
if self._files is not None: |
70 |
for f in self._files.values(): |
71 |
- f.close() |
72 |
+ os.close(f) |
73 |
self._files = None |
74 |
|
75 |
class _LockProcess(AbstractPollTask): |
76 |
@@ -190,8 +197,8 @@ class _LockProcess(AbstractPollTask): |
77 |
in_pr, in_pw = os.pipe() |
78 |
out_pr, out_pw = os.pipe() |
79 |
self._files = {} |
80 |
- self._files['pipe_in'] = os.fdopen(in_pr, 'rb', 0) |
81 |
- self._files['pipe_out'] = os.fdopen(out_pw, 'wb', 0) |
82 |
+ self._files['pipe_in'] = in_pr |
83 |
+ self._files['pipe_out'] = out_pw |
84 |
fcntl.fcntl(in_pr, fcntl.F_SETFL, |
85 |
fcntl.fcntl(in_pr, fcntl.F_GETFL) | os.O_NONBLOCK) |
86 |
self._reg_id = self.scheduler.register(in_pr, |
87 |
@@ -219,7 +226,7 @@ class _LockProcess(AbstractPollTask): |
88 |
except KeyError: |
89 |
pass |
90 |
else: |
91 |
- pipe_out.close() |
92 |
+ os.close(pipe_out) |
93 |
|
94 |
if proc.returncode != os.EX_OK: |
95 |
# Typically, this will happen due to the |
96 |
@@ -263,7 +270,13 @@ class _LockProcess(AbstractPollTask): |
97 |
return self.returncode |
98 |
|
99 |
def _output_handler(self, f, event): |
100 |
- buf = self._read_buf(self._files['pipe_in'], event) |
101 |
+ buf = None |
102 |
+ if event & PollConstants.POLLIN: |
103 |
+ try: |
104 |
+ buf = os.read(self._files['pipe_in'], self._bufsize) |
105 |
+ except IOError as e: |
106 |
+ if e.errno not in (errno.EAGAIN,): |
107 |
+ raise |
108 |
if buf: |
109 |
self._acquired = True |
110 |
self._unregister() |
111 |
@@ -283,7 +296,7 @@ class _LockProcess(AbstractPollTask): |
112 |
except KeyError: |
113 |
pass |
114 |
else: |
115 |
- pipe_in.close() |
116 |
+ os.close(pipe_in) |
117 |
|
118 |
def unlock(self): |
119 |
if self._proc is None: |
120 |
@@ -294,8 +307,8 @@ class _LockProcess(AbstractPollTask): |
121 |
raise AssertionError("lock process failed with returncode %s" \ |
122 |
% (self.returncode,)) |
123 |
self._unlocked = True |
124 |
- self._files['pipe_out'].write(b'\0') |
125 |
- self._files['pipe_out'].close() |
126 |
+ os.write(self._files['pipe_out'], b'\0') |
127 |
+ os.close(self._files['pipe_out']) |
128 |
self._files = None |
129 |
self._proc.wait() |
130 |
self._proc = None |