~ubuntu-branches/ubuntu/quantal/python2.7/quantal

« back to all changes in this revision

Viewing changes to Lib/test/test_os.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2012-03-09 19:28:43 UTC
  • mto: (36.1.11 sid)
  • mto: This revision was merged to the branch mainline in revision 51.
  • Revision ID: package-import@ubuntu.com-20120309192843-n84bbtrkfxw34p6n
Tags: upstream-2.7.3~rc1
ImportĀ upstreamĀ versionĀ 2.7.3~rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
import signal
11
11
import subprocess
12
12
import time
 
13
 
13
14
from test import test_support
14
15
import mmap
15
16
import uuid
361
362
                value = popen.read().strip()
362
363
                self.assertEqual(value, "World")
363
364
 
 
365
    # On FreeBSD < 7 and OS X < 10.6, unsetenv() doesn't return a value (issue
 
366
    # #13415).
 
367
    @unittest.skipIf(sys.platform.startswith(('freebsd', 'darwin')),
 
368
                     "due to known OS bug: see issue #13415")
 
369
    def test_unset_error(self):
 
370
        if sys.platform == "win32":
 
371
            # an environment variable is limited to 32,767 characters
 
372
            key = 'x' * 50000
 
373
            self.assertRaises(ValueError, os.environ.__delitem__, key)
 
374
        else:
 
375
            # "=" is not allowed in a variable name
 
376
            key = 'key='
 
377
            self.assertRaises(OSError, os.environ.__delitem__, key)
 
378
 
364
379
class WalkTests(unittest.TestCase):
365
380
    """Tests for os.walk()."""
366
381
 
512
527
        f.close()
513
528
 
514
529
class URandomTests (unittest.TestCase):
515
 
    def test_urandom(self):
516
 
        try:
517
 
            self.assertEqual(len(os.urandom(1)), 1)
518
 
            self.assertEqual(len(os.urandom(10)), 10)
519
 
            self.assertEqual(len(os.urandom(100)), 100)
520
 
            self.assertEqual(len(os.urandom(1000)), 1000)
521
 
            # see http://bugs.python.org/issue3708
522
 
            self.assertRaises(TypeError, os.urandom, 0.9)
523
 
            self.assertRaises(TypeError, os.urandom, 1.1)
524
 
            self.assertRaises(TypeError, os.urandom, 2.0)
525
 
        except NotImplementedError:
526
 
            pass
 
530
 
 
531
    def test_urandom_length(self):
 
532
        self.assertEqual(len(os.urandom(0)), 0)
 
533
        self.assertEqual(len(os.urandom(1)), 1)
 
534
        self.assertEqual(len(os.urandom(10)), 10)
 
535
        self.assertEqual(len(os.urandom(100)), 100)
 
536
        self.assertEqual(len(os.urandom(1000)), 1000)
 
537
 
 
538
    def test_urandom_value(self):
 
539
        data1 = os.urandom(16)
 
540
        data2 = os.urandom(16)
 
541
        self.assertNotEqual(data1, data2)
 
542
 
 
543
    def get_urandom_subprocess(self, count):
 
544
        # We need to use repr() and eval() to avoid line ending conversions
 
545
        # under Windows.
 
546
        code = '\n'.join((
 
547
            'import os, sys',
 
548
            'data = os.urandom(%s)' % count,
 
549
            'sys.stdout.write(repr(data))',
 
550
            'sys.stdout.flush()',
 
551
            'print >> sys.stderr, (len(data), data)'))
 
552
        cmd_line = [sys.executable, '-c', code]
 
553
        p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
 
554
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
555
        out, err = p.communicate()
 
556
        self.assertEqual(p.wait(), 0, (p.wait(), err))
 
557
        out = eval(out)
 
558
        self.assertEqual(len(out), count, err)
 
559
        return out
 
560
 
 
561
    def test_urandom_subprocess(self):
 
562
        data1 = self.get_urandom_subprocess(16)
 
563
        data2 = self.get_urandom_subprocess(16)
 
564
        self.assertNotEqual(data1, data2)
527
565
 
528
566
    def test_execvpe_with_bad_arglist(self):
529
567
        self.assertRaises(ValueError, os.execvpe, 'notepad', [], None)