@@ -115,6 +115,8 @@ def write_to_stdout(b):
115
115
self .linesep = os .linesep .decode ('ascii' )
116
116
# This can handle unicode in both Python 2 and 3
117
117
self .write_to_stdout = sys .stdout .write
118
+ # storage for async transport
119
+ self .async_pw_transport = None
118
120
119
121
def _log (self , s , direction ):
120
122
if self .logfile is not None :
@@ -221,7 +223,7 @@ def compile_pattern_list(self, patterns):
221
223
self ._pattern_type_err (p )
222
224
return compiled_pattern_list
223
225
224
- def expect (self , pattern , timeout = - 1 , searchwindowsize = - 1 , async = False ):
226
+ def expect (self , pattern , timeout = - 1 , searchwindowsize = - 1 , async_ = False , ** kw ):
225
227
'''This seeks through the stream until a pattern is matched. The
226
228
pattern is overloaded and may take several types. The pattern can be a
227
229
StringType, EOF, a compiled re, or a list of any of those types.
@@ -305,23 +307,27 @@ def expect(self, pattern, timeout=-1, searchwindowsize=-1, async=False):
305
307
If you are trying to optimize for speed then see expect_list().
306
308
307
309
On Python 3.4, or Python 3.3 with asyncio installed, passing
308
- ``async =True`` will make this return an :mod:`asyncio` coroutine,
310
+ ``async_ =True`` will make this return an :mod:`asyncio` coroutine,
309
311
which you can yield from to get the same result that this method would
310
312
normally give directly. So, inside a coroutine, you can replace this code::
311
313
312
314
index = p.expect(patterns)
313
315
314
316
With this non-blocking form::
315
317
316
- index = yield from p.expect(patterns, async =True)
318
+ index = yield from p.expect(patterns, async_ =True)
317
319
'''
320
+ if 'async' in kw :
321
+ async_ = kw .pop ('async' )
322
+ if kw :
323
+ raise TypeError ("Unknown keyword arguments: {}" .format (kw ))
318
324
319
325
compiled_pattern_list = self .compile_pattern_list (pattern )
320
326
return self .expect_list (compiled_pattern_list ,
321
- timeout , searchwindowsize , async )
327
+ timeout , searchwindowsize , async_ )
322
328
323
329
def expect_list (self , pattern_list , timeout = - 1 , searchwindowsize = - 1 ,
324
- async = False ):
330
+ async_ = False , ** kw ):
325
331
'''This takes a list of compiled regular expressions and returns the
326
332
index into the pattern_list that matched the child output. The list may
327
333
also contain EOF or TIMEOUT(which are not compiled regular
@@ -331,21 +337,25 @@ def expect_list(self, pattern_list, timeout=-1, searchwindowsize=-1,
331
337
the expect() method. This is called by expect().
332
338
333
339
334
- Like :meth:`expect`, passing ``async =True`` will make this return an
340
+ Like :meth:`expect`, passing ``async_ =True`` will make this return an
335
341
asyncio coroutine.
336
342
'''
337
343
if timeout == - 1 :
338
344
timeout = self .timeout
345
+ if 'async' in kw :
346
+ async_ = kw .pop ('async' )
347
+ if kw :
348
+ raise TypeError ("Unknown keyword arguments: {}" .format (kw ))
339
349
340
350
exp = Expecter (self , searcher_re (pattern_list ), searchwindowsize )
341
- if async :
342
- from .async import expect_async
351
+ if async_ :
352
+ from ._async import expect_async
343
353
return expect_async (exp , timeout )
344
354
else :
345
355
return exp .expect_loop (timeout )
346
356
347
357
def expect_exact (self , pattern_list , timeout = - 1 , searchwindowsize = - 1 ,
348
- async = False ):
358
+ async_ = False , ** kw ):
349
359
350
360
'''This is similar to expect(), but uses plain string matching instead
351
361
of compiled regular expressions in 'pattern_list'. The 'pattern_list'
@@ -359,11 +369,15 @@ def expect_exact(self, pattern_list, timeout=-1, searchwindowsize=-1,
359
369
This method is also useful when you don't want to have to worry about
360
370
escaping regular expression characters that you want to match.
361
371
362
- Like :meth:`expect`, passing ``async =True`` will make this return an
372
+ Like :meth:`expect`, passing ``async_ =True`` will make this return an
363
373
asyncio coroutine.
364
374
'''
365
375
if timeout == - 1 :
366
376
timeout = self .timeout
377
+ if 'async' in kw :
378
+ async_ = kw .pop ('async' )
379
+ if kw :
380
+ raise TypeError ("Unknown keyword arguments: {}" .format (kw ))
367
381
368
382
if (isinstance (pattern_list , self .allowed_string_types ) or
369
383
pattern_list in (TIMEOUT , EOF )):
@@ -383,8 +397,8 @@ def prepare_pattern(pattern):
383
397
pattern_list = [prepare_pattern (p ) for p in pattern_list ]
384
398
385
399
exp = Expecter (self , searcher_string (pattern_list ), searchwindowsize )
386
- if async :
387
- from .async import expect_async
400
+ if async_ :
401
+ from ._async import expect_async
388
402
return expect_async (exp , timeout )
389
403
else :
390
404
return exp .expect_loop (timeout )
@@ -415,7 +429,7 @@ def read(self, size=-1):
415
429
416
430
# I could have done this more directly by not using expect(), but
417
431
# I deliberately decided to couple read() to expect() so that
418
- # I would catch any bugs early and ensure consistant behavior.
432
+ # I would catch any bugs early and ensure consistent behavior.
419
433
# It's a little less efficient, but there is less for me to
420
434
# worry about if I have to later modify read() or expect().
421
435
# Note, it's OK if size==-1 in the regex. That just means it
@@ -487,7 +501,7 @@ def isatty(self):
487
501
# For 'with spawn(...) as child:'
488
502
def __enter__ (self ):
489
503
return self
490
-
504
+
491
505
def __exit__ (self , etype , evalue , tb ):
492
506
# We rely on subclasses to implement close(). If they don't, it's not
493
507
# clear what a context manager should do.
0 commit comments