2001-07-17 17:52:51 -03:00
|
|
|
# As a test suite for the os module, this is woefully inadequate, but this
|
|
|
|
# does add tests for a few functions which have been determined to be more
|
2003-10-20 11:01:56 -03:00
|
|
|
# portable than they had been thought to be.
|
2001-07-17 17:52:51 -03:00
|
|
|
|
|
|
|
import os
|
2009-01-30 21:42:55 -04:00
|
|
|
import errno
|
2001-07-17 17:52:51 -03:00
|
|
|
import unittest
|
2001-08-20 17:10:01 -03:00
|
|
|
import warnings
|
2006-05-04 07:08:42 -03:00
|
|
|
import sys
|
2010-04-02 20:26:06 -03:00
|
|
|
import signal
|
|
|
|
import subprocess
|
2014-12-26 13:09:00 -04:00
|
|
|
import sysconfig
|
2015-02-24 09:30:43 -04:00
|
|
|
import textwrap
|
2010-04-02 20:26:06 -03:00
|
|
|
import time
|
2013-08-16 15:44:38 -03:00
|
|
|
try:
|
|
|
|
import resource
|
|
|
|
except ImportError:
|
|
|
|
resource = None
|
2012-02-20 21:42:21 -04:00
|
|
|
|
2003-05-01 14:45:56 -03:00
|
|
|
from test import test_support
|
2013-08-24 15:52:27 -03:00
|
|
|
from test.script_helper import assert_python_ok
|
2010-10-08 06:41:13 -03:00
|
|
|
import mmap
|
|
|
|
import uuid
|
2001-07-17 17:52:51 -03:00
|
|
|
|
2001-08-22 16:24:42 -03:00
|
|
|
warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, __name__)
|
|
|
|
warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, __name__)
|
|
|
|
|
2006-07-02 15:44:00 -03:00
|
|
|
# Tests creating TESTFN
|
|
|
|
class FileTests(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
if os.path.exists(test_support.TESTFN):
|
|
|
|
os.unlink(test_support.TESTFN)
|
|
|
|
tearDown = setUp
|
|
|
|
|
|
|
|
def test_access(self):
|
|
|
|
f = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR)
|
|
|
|
os.close(f)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(os.access(test_support.TESTFN, os.W_OK))
|
2006-07-03 05:23:19 -03:00
|
|
|
|
2008-01-19 16:22:13 -04:00
|
|
|
def test_closerange(self):
|
2008-08-17 11:43:41 -03:00
|
|
|
first = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR)
|
|
|
|
# We must allocate two consecutive file descriptors, otherwise
|
|
|
|
# it will mess up other file descriptors (perhaps even the three
|
|
|
|
# standard ones).
|
|
|
|
second = os.dup(first)
|
|
|
|
try:
|
|
|
|
retries = 0
|
|
|
|
while second != first + 1:
|
|
|
|
os.close(first)
|
|
|
|
retries += 1
|
|
|
|
if retries > 10:
|
|
|
|
# XXX test skipped
|
2009-05-16 15:44:34 -03:00
|
|
|
self.skipTest("couldn't allocate two consecutive fds")
|
2008-08-17 11:43:41 -03:00
|
|
|
first, second = second, os.dup(second)
|
|
|
|
finally:
|
|
|
|
os.close(second)
|
2008-01-19 16:22:13 -04:00
|
|
|
# close a fd that is open, and one that isn't
|
2008-08-17 11:43:41 -03:00
|
|
|
os.closerange(first, first + 2)
|
|
|
|
self.assertRaises(OSError, os.write, first, "a")
|
2008-01-19 16:22:13 -04:00
|
|
|
|
2010-06-30 14:11:08 -03:00
|
|
|
@test_support.cpython_only
|
2008-09-08 20:03:47 -03:00
|
|
|
def test_rename(self):
|
|
|
|
path = unicode(test_support.TESTFN)
|
|
|
|
old = sys.getrefcount(path)
|
|
|
|
self.assertRaises(TypeError, os.rename, path, 0)
|
|
|
|
new = sys.getrefcount(path)
|
|
|
|
self.assertEqual(old, new)
|
|
|
|
|
2006-07-02 15:44:00 -03:00
|
|
|
|
2001-07-17 17:52:51 -03:00
|
|
|
class TemporaryFileTests(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.files = []
|
2003-05-01 14:45:56 -03:00
|
|
|
os.mkdir(test_support.TESTFN)
|
2001-07-17 17:52:51 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
for name in self.files:
|
|
|
|
os.unlink(name)
|
2003-05-01 14:45:56 -03:00
|
|
|
os.rmdir(test_support.TESTFN)
|
2001-07-17 17:52:51 -03:00
|
|
|
|
|
|
|
def check_tempfile(self, name):
|
|
|
|
# make sure it doesn't already exist:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(os.path.exists(name),
|
2001-07-17 17:52:51 -03:00
|
|
|
"file already exists for temporary file")
|
|
|
|
# make sure we can create the file
|
|
|
|
open(name, "w")
|
|
|
|
self.files.append(name)
|
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'tempnam'), 'test needs os.tempnam()')
|
2001-07-17 17:52:51 -03:00
|
|
|
def test_tempnam(self):
|
2011-01-02 16:04:52 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
|
|
|
|
r"test_os$")
|
|
|
|
warnings.filterwarnings("ignore", "tempnam", DeprecationWarning)
|
|
|
|
self.check_tempfile(os.tempnam())
|
2001-07-17 17:52:51 -03:00
|
|
|
|
2011-01-02 16:04:52 -04:00
|
|
|
name = os.tempnam(test_support.TESTFN)
|
|
|
|
self.check_tempfile(name)
|
2001-07-17 17:52:51 -03:00
|
|
|
|
2011-01-02 16:04:52 -04:00
|
|
|
name = os.tempnam(test_support.TESTFN, "pfx")
|
|
|
|
self.assertTrue(os.path.basename(name)[:3] == "pfx")
|
|
|
|
self.check_tempfile(name)
|
2001-07-17 17:52:51 -03:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'tmpfile'), 'test needs os.tmpfile()')
|
2001-07-17 17:52:51 -03:00
|
|
|
def test_tmpfile(self):
|
2008-03-06 02:55:22 -04:00
|
|
|
# As with test_tmpnam() below, the Windows implementation of tmpfile()
|
|
|
|
# attempts to create a file in the root directory of the current drive.
|
|
|
|
# On Vista and Server 2008, this test will always fail for normal users
|
|
|
|
# as writing to the root directory requires elevated privileges. With
|
|
|
|
# XP and below, the semantics of tmpfile() are the same, but the user
|
|
|
|
# running the test is more likely to have administrative privileges on
|
|
|
|
# their account already. If that's the case, then os.tmpfile() should
|
|
|
|
# work. In order to make this test as useful as possible, rather than
|
|
|
|
# trying to detect Windows versions or whether or not the user has the
|
|
|
|
# right permissions, just try and create a file in the root directory
|
|
|
|
# and see if it raises a 'Permission denied' OSError. If it does, then
|
|
|
|
# test that a subsequent call to os.tmpfile() raises the same error. If
|
|
|
|
# it doesn't, assume we're on XP or below and the user running the test
|
|
|
|
# has administrative privileges, and proceed with the test as normal.
|
2011-01-02 16:04:52 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning)
|
|
|
|
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
name = '\\python_test_os_test_tmpfile.txt'
|
|
|
|
if os.path.exists(name):
|
|
|
|
os.remove(name)
|
2008-03-06 02:55:22 -04:00
|
|
|
try:
|
2011-01-02 16:04:52 -04:00
|
|
|
fp = open(name, 'w')
|
|
|
|
except IOError, first:
|
|
|
|
# open() failed, assert tmpfile() fails in the same way.
|
|
|
|
# Although open() raises an IOError and os.tmpfile() raises an
|
|
|
|
# OSError(), 'args' will be (13, 'Permission denied') in both
|
|
|
|
# cases.
|
|
|
|
try:
|
|
|
|
fp = os.tmpfile()
|
|
|
|
except OSError, second:
|
|
|
|
self.assertEqual(first.args, second.args)
|
|
|
|
else:
|
|
|
|
self.fail("expected os.tmpfile() to raise OSError")
|
2013-12-10 18:06:46 -04:00
|
|
|
return
|
2008-03-06 02:55:22 -04:00
|
|
|
else:
|
2011-01-02 16:04:52 -04:00
|
|
|
# open() worked, therefore, tmpfile() should work. Close our
|
|
|
|
# dummy file and proceed with the test as normal.
|
|
|
|
fp.close()
|
|
|
|
os.remove(name)
|
|
|
|
|
|
|
|
fp = os.tmpfile()
|
|
|
|
fp.write("foobar")
|
|
|
|
fp.seek(0,0)
|
|
|
|
s = fp.read()
|
|
|
|
fp.close()
|
|
|
|
self.assertTrue(s == "foobar")
|
2001-07-17 17:52:51 -03:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'tmpnam'), 'test needs os.tmpnam()')
|
2001-07-17 17:52:51 -03:00
|
|
|
def test_tmpnam(self):
|
2011-01-02 16:04:52 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning,
|
|
|
|
r"test_os$")
|
|
|
|
warnings.filterwarnings("ignore", "tmpnam", DeprecationWarning)
|
|
|
|
|
|
|
|
name = os.tmpnam()
|
|
|
|
if sys.platform in ("win32",):
|
|
|
|
# The Windows tmpnam() seems useless. From the MS docs:
|
|
|
|
#
|
|
|
|
# The character string that tmpnam creates consists of
|
|
|
|
# the path prefix, defined by the entry P_tmpdir in the
|
|
|
|
# file STDIO.H, followed by a sequence consisting of the
|
|
|
|
# digit characters '0' through '9'; the numerical value
|
|
|
|
# of this string is in the range 1 - 65,535. Changing the
|
|
|
|
# definitions of L_tmpnam or P_tmpdir in STDIO.H does not
|
|
|
|
# change the operation of tmpnam.
|
|
|
|
#
|
|
|
|
# The really bizarre part is that, at least under MSVC6,
|
|
|
|
# P_tmpdir is "\\". That is, the path returned refers to
|
|
|
|
# the root of the current drive. That's a terrible place to
|
|
|
|
# put temp files, and, depending on privileges, the user
|
|
|
|
# may not even be able to open a file in the root directory.
|
|
|
|
self.assertFalse(os.path.exists(name),
|
|
|
|
"file already exists for temporary file")
|
|
|
|
else:
|
|
|
|
self.check_tempfile(name)
|
2001-07-20 22:41:30 -03:00
|
|
|
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
# Test attributes on return values from os.*stat* family.
|
|
|
|
class StatAttributeTests(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2003-05-01 14:45:56 -03:00
|
|
|
os.mkdir(test_support.TESTFN)
|
|
|
|
self.fname = os.path.join(test_support.TESTFN, "f1")
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
f = open(self.fname, 'wb')
|
|
|
|
f.write("ABC")
|
|
|
|
f.close()
|
2001-10-18 18:57:37 -03:00
|
|
|
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
def tearDown(self):
|
|
|
|
os.unlink(self.fname)
|
2003-05-01 14:45:56 -03:00
|
|
|
os.rmdir(test_support.TESTFN)
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'stat'), 'test needs os.stat()')
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
def test_stat_attributes(self):
|
|
|
|
import stat
|
|
|
|
result = os.stat(self.fname)
|
|
|
|
|
|
|
|
# Make sure direct access works
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(result[stat.ST_SIZE], 3)
|
|
|
|
self.assertEqual(result.st_size, 3)
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
|
|
|
|
# Make sure all the attributes are there
|
|
|
|
members = dir(result)
|
|
|
|
for name in dir(stat):
|
|
|
|
if name[:3] == 'ST_':
|
|
|
|
attr = name.lower()
|
2005-01-23 05:19:22 -04:00
|
|
|
if name.endswith("TIME"):
|
|
|
|
def trunc(x): return int(x)
|
|
|
|
else:
|
|
|
|
def trunc(x): return x
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(trunc(getattr(result, attr)),
|
|
|
|
result[getattr(stat, name)])
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn(attr, members)
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
|
|
|
|
try:
|
|
|
|
result[200]
|
2012-12-18 15:27:37 -04:00
|
|
|
self.fail("No exception raised")
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Make sure that assignment fails
|
|
|
|
try:
|
|
|
|
result.st_mode = 1
|
2012-12-18 15:27:37 -04:00
|
|
|
self.fail("No exception raised")
|
2010-06-30 15:41:08 -03:00
|
|
|
except (AttributeError, TypeError):
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
result.st_rdev = 1
|
2012-12-18 15:27:37 -04:00
|
|
|
self.fail("No exception raised")
|
2001-10-18 18:19:31 -03:00
|
|
|
except (AttributeError, TypeError):
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
result.parrot = 1
|
2012-12-18 15:27:37 -04:00
|
|
|
self.fail("No exception raised")
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Use the stat_result constructor with a too-short tuple.
|
|
|
|
try:
|
|
|
|
result2 = os.stat_result((10,))
|
2012-12-18 15:27:37 -04:00
|
|
|
self.fail("No exception raised")
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
2011-03-15 13:55:01 -03:00
|
|
|
# Use the constructor with a too-long tuple.
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
try:
|
|
|
|
result2 = os.stat_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
2001-10-18 18:57:37 -03:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'statvfs'), 'test needs os.statvfs()')
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
def test_statvfs_attributes(self):
|
2002-06-11 03:22:31 -03:00
|
|
|
try:
|
|
|
|
result = os.statvfs(self.fname)
|
|
|
|
except OSError, e:
|
|
|
|
# On AtheOS, glibc always returns ENOSYS
|
|
|
|
if e.errno == errno.ENOSYS:
|
2013-12-10 16:09:20 -04:00
|
|
|
self.skipTest('glibc always returns ENOSYS on AtheOS')
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
|
|
|
|
# Make sure direct access works
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(result.f_bfree, result[3])
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
|
2008-05-15 21:37:42 -03:00
|
|
|
# Make sure all the attributes are there.
|
|
|
|
members = ('bsize', 'frsize', 'blocks', 'bfree', 'bavail', 'files',
|
|
|
|
'ffree', 'favail', 'flag', 'namemax')
|
|
|
|
for value, member in enumerate(members):
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(getattr(result, 'f_' + member), result[value])
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
|
|
|
|
# Make sure that assignment really fails
|
|
|
|
try:
|
|
|
|
result.f_bfree = 1
|
2012-12-18 15:27:37 -04:00
|
|
|
self.fail("No exception raised")
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
result.parrot = 1
|
2012-12-18 15:27:37 -04:00
|
|
|
self.fail("No exception raised")
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Use the constructor with a too-short tuple.
|
|
|
|
try:
|
|
|
|
result2 = os.statvfs_result((10,))
|
2012-12-18 15:27:37 -04:00
|
|
|
self.fail("No exception raised")
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
2011-03-15 13:55:01 -03:00
|
|
|
# Use the constructor with a too-long tuple.
|
SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
This is a big one, touching lots of files. Some of the platforms
aren't tested yet. Briefly, this changes the return value of the
os/posix functions stat(), fstat(), statvfs(), fstatvfs(), and the
time functions localtime(), gmtime(), and strptime() from tuples into
pseudo-sequences. When accessed as a sequence, they behave exactly as
before. But they also have attributes like st_mtime or tm_year. The
stat return value, moreover, has a few platform-specific attributes
that are not available through the sequence interface (because
everybody expects the sequence to have a fixed length, these couldn't
be added there). If your platform's struct stat doesn't define
st_blksize, st_blocks or st_rdev, they won't be accessible from Python
either.
(Still missing is a documentation update.)
2001-10-18 17:34:25 -03:00
|
|
|
try:
|
|
|
|
result2 = os.statvfs_result((0,1,2,3,4,5,6,7,8,9,10,11,12,13,14))
|
|
|
|
except TypeError:
|
|
|
|
pass
|
2001-07-17 17:52:51 -03:00
|
|
|
|
2006-10-15 05:43:33 -03:00
|
|
|
def test_utime_dir(self):
|
|
|
|
delta = 1000000
|
|
|
|
st = os.stat(test_support.TESTFN)
|
2006-10-15 08:02:07 -03:00
|
|
|
# round to int, because some systems may support sub-second
|
|
|
|
# time stamps in stat, but not in utime.
|
|
|
|
os.utime(test_support.TESTFN, (st.st_atime, int(st.st_mtime-delta)))
|
2006-10-15 05:43:33 -03:00
|
|
|
st2 = os.stat(test_support.TESTFN)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(st2.st_mtime, int(st.st_mtime-delta))
|
2006-10-15 05:43:33 -03:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
# Restrict tests to Win32, since there is no guarantee other
|
2006-10-09 17:44:25 -03:00
|
|
|
# systems support centiseconds
|
2013-11-03 17:15:46 -04:00
|
|
|
def get_file_system(path):
|
|
|
|
if sys.platform == 'win32':
|
2008-08-20 01:13:28 -03:00
|
|
|
root = os.path.splitdrive(os.path.abspath(path))[0] + '\\'
|
2007-08-30 16:04:09 -03:00
|
|
|
import ctypes
|
2008-08-20 13:15:28 -03:00
|
|
|
kernel32 = ctypes.windll.kernel32
|
|
|
|
buf = ctypes.create_string_buffer("", 100)
|
|
|
|
if kernel32.GetVolumeInformationA(root, None, 0, None, None, None, buf, len(buf)):
|
2007-08-30 16:04:09 -03:00
|
|
|
return buf.value
|
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
|
2013-11-03 17:25:42 -04:00
|
|
|
@unittest.skipUnless(get_file_system(test_support.TESTFN) == "NTFS",
|
2013-11-03 17:15:46 -04:00
|
|
|
"requires NTFS")
|
|
|
|
def test_1565150(self):
|
|
|
|
t1 = 1159195039.25
|
|
|
|
os.utime(self.fname, (t1, t1))
|
|
|
|
self.assertEqual(os.stat(self.fname).st_mtime, t1)
|
|
|
|
|
|
|
|
@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
|
2013-11-03 17:25:42 -04:00
|
|
|
@unittest.skipUnless(get_file_system(test_support.TESTFN) == "NTFS",
|
2013-11-03 17:15:46 -04:00
|
|
|
"requires NTFS")
|
|
|
|
def test_large_time(self):
|
|
|
|
t1 = 5000000000 # some day in 2128
|
|
|
|
os.utime(self.fname, (t1, t1))
|
|
|
|
self.assertEqual(os.stat(self.fname).st_mtime, t1)
|
|
|
|
|
|
|
|
@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
|
|
|
|
def test_1686475(self):
|
|
|
|
# Verify that an open file can be stat'ed
|
|
|
|
try:
|
|
|
|
os.stat(r"c:\pagefile.sys")
|
|
|
|
except WindowsError, e:
|
|
|
|
if e.errno == 2: # file does not exist; cannot run test
|
2013-12-10 16:09:20 -04:00
|
|
|
self.skipTest(r'c:\pagefile.sys does not exist')
|
2013-11-03 17:15:46 -04:00
|
|
|
self.fail("Could not stat pagefile.sys")
|
2007-04-04 15:30:36 -03:00
|
|
|
|
2004-05-31 13:29:04 -03:00
|
|
|
from test import mapping_tests
|
2003-03-09 03:05:43 -04:00
|
|
|
|
2004-05-31 13:29:04 -03:00
|
|
|
class EnvironTests(mapping_tests.BasicTestMappingProtocol):
|
2003-03-09 03:05:43 -04:00
|
|
|
"""check that os.environ object conform to mapping protocol"""
|
2004-06-02 15:42:25 -03:00
|
|
|
type2test = None
|
2003-03-09 03:05:43 -04:00
|
|
|
def _reference(self):
|
|
|
|
return {"KEY1":"VALUE1", "KEY2":"VALUE2", "KEY3":"VALUE3"}
|
|
|
|
def _empty_mapping(self):
|
|
|
|
os.environ.clear()
|
|
|
|
return os.environ
|
|
|
|
def setUp(self):
|
|
|
|
self.__save = dict(os.environ)
|
|
|
|
os.environ.clear()
|
|
|
|
def tearDown(self):
|
|
|
|
os.environ.clear()
|
|
|
|
os.environ.update(self.__save)
|
|
|
|
|
2005-01-29 09:29:23 -04:00
|
|
|
# Bug 1110478
|
2005-02-17 17:23:20 -04:00
|
|
|
def test_update2(self):
|
2005-01-29 09:29:23 -04:00
|
|
|
if os.path.exists("/bin/sh"):
|
|
|
|
os.environ.update(HELLO="World")
|
2010-10-30 18:29:52 -03:00
|
|
|
with os.popen("/bin/sh -c 'echo $HELLO'") as popen:
|
|
|
|
value = popen.read().strip()
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(value, "World")
|
2005-01-29 09:29:23 -04:00
|
|
|
|
2011-11-27 08:05:14 -04:00
|
|
|
# On FreeBSD < 7 and OS X < 10.6, unsetenv() doesn't return a value (issue
|
|
|
|
# #13415).
|
|
|
|
@unittest.skipIf(sys.platform.startswith(('freebsd', 'darwin')),
|
|
|
|
"due to known OS bug: see issue #13415")
|
2011-11-22 17:20:13 -04:00
|
|
|
def test_unset_error(self):
|
|
|
|
if sys.platform == "win32":
|
|
|
|
# an environment variable is limited to 32,767 characters
|
|
|
|
key = 'x' * 50000
|
2011-11-22 17:30:19 -04:00
|
|
|
self.assertRaises(ValueError, os.environ.__delitem__, key)
|
2011-11-22 17:20:13 -04:00
|
|
|
else:
|
|
|
|
# "=" is not allowed in a variable name
|
|
|
|
key = 'key='
|
2011-11-22 17:30:19 -04:00
|
|
|
self.assertRaises(OSError, os.environ.__delitem__, key)
|
2011-11-22 17:20:13 -04:00
|
|
|
|
2003-04-25 04:11:48 -03:00
|
|
|
class WalkTests(unittest.TestCase):
|
|
|
|
"""Tests for os.walk()."""
|
|
|
|
|
|
|
|
def test_traversal(self):
|
|
|
|
import os
|
|
|
|
from os.path import join
|
|
|
|
|
|
|
|
# Build:
|
2007-04-25 03:30:05 -03:00
|
|
|
# TESTFN/
|
2007-03-21 06:10:29 -03:00
|
|
|
# TEST1/ a file kid and two directory kids
|
2003-04-25 04:11:48 -03:00
|
|
|
# tmp1
|
|
|
|
# SUB1/ a file kid and a directory kid
|
2007-03-21 06:10:29 -03:00
|
|
|
# tmp2
|
|
|
|
# SUB11/ no kids
|
|
|
|
# SUB2/ a file kid and a dirsymlink kid
|
|
|
|
# tmp3
|
|
|
|
# link/ a symlink to TESTFN.2
|
|
|
|
# TEST2/
|
|
|
|
# tmp4 a lone file
|
|
|
|
walk_path = join(test_support.TESTFN, "TEST1")
|
|
|
|
sub1_path = join(walk_path, "SUB1")
|
2003-04-25 04:11:48 -03:00
|
|
|
sub11_path = join(sub1_path, "SUB11")
|
2007-03-21 06:10:29 -03:00
|
|
|
sub2_path = join(walk_path, "SUB2")
|
|
|
|
tmp1_path = join(walk_path, "tmp1")
|
2003-04-25 04:11:48 -03:00
|
|
|
tmp2_path = join(sub1_path, "tmp2")
|
|
|
|
tmp3_path = join(sub2_path, "tmp3")
|
2007-03-21 06:10:29 -03:00
|
|
|
link_path = join(sub2_path, "link")
|
|
|
|
t2_path = join(test_support.TESTFN, "TEST2")
|
|
|
|
tmp4_path = join(test_support.TESTFN, "TEST2", "tmp4")
|
2003-04-25 04:11:48 -03:00
|
|
|
|
|
|
|
# Create stuff.
|
|
|
|
os.makedirs(sub11_path)
|
|
|
|
os.makedirs(sub2_path)
|
2007-03-21 06:10:29 -03:00
|
|
|
os.makedirs(t2_path)
|
|
|
|
for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path:
|
2003-04-25 04:11:48 -03:00
|
|
|
f = file(path, "w")
|
|
|
|
f.write("I'm " + path + " and proud of it. Blame test_os.\n")
|
|
|
|
f.close()
|
2007-03-21 06:10:29 -03:00
|
|
|
if hasattr(os, "symlink"):
|
|
|
|
os.symlink(os.path.abspath(t2_path), link_path)
|
2007-04-04 15:38:47 -03:00
|
|
|
sub2_tree = (sub2_path, ["link"], ["tmp3"])
|
2007-03-21 06:10:29 -03:00
|
|
|
else:
|
2007-04-04 15:38:47 -03:00
|
|
|
sub2_tree = (sub2_path, [], ["tmp3"])
|
2003-04-25 04:11:48 -03:00
|
|
|
|
|
|
|
# Walk top-down.
|
2007-03-21 06:10:29 -03:00
|
|
|
all = list(os.walk(walk_path))
|
2003-04-25 04:11:48 -03:00
|
|
|
self.assertEqual(len(all), 4)
|
|
|
|
# We can't know which order SUB1 and SUB2 will appear in.
|
|
|
|
# Not flipped: TESTFN, SUB1, SUB11, SUB2
|
|
|
|
# flipped: TESTFN, SUB2, SUB1, SUB11
|
|
|
|
flipped = all[0][1][0] != "SUB1"
|
|
|
|
all[0][1].sort()
|
2007-03-21 06:10:29 -03:00
|
|
|
self.assertEqual(all[0], (walk_path, ["SUB1", "SUB2"], ["tmp1"]))
|
2003-04-25 04:11:48 -03:00
|
|
|
self.assertEqual(all[1 + flipped], (sub1_path, ["SUB11"], ["tmp2"]))
|
|
|
|
self.assertEqual(all[2 + flipped], (sub11_path, [], []))
|
2007-04-04 15:38:47 -03:00
|
|
|
self.assertEqual(all[3 - 2 * flipped], sub2_tree)
|
2003-04-25 04:11:48 -03:00
|
|
|
|
|
|
|
# Prune the search.
|
|
|
|
all = []
|
2007-03-21 06:10:29 -03:00
|
|
|
for root, dirs, files in os.walk(walk_path):
|
2003-04-25 04:11:48 -03:00
|
|
|
all.append((root, dirs, files))
|
|
|
|
# Don't descend into SUB1.
|
|
|
|
if 'SUB1' in dirs:
|
|
|
|
# Note that this also mutates the dirs we appended to all!
|
|
|
|
dirs.remove('SUB1')
|
|
|
|
self.assertEqual(len(all), 2)
|
2007-03-21 06:10:29 -03:00
|
|
|
self.assertEqual(all[0], (walk_path, ["SUB2"], ["tmp1"]))
|
2007-04-04 15:38:47 -03:00
|
|
|
self.assertEqual(all[1], sub2_tree)
|
2003-04-25 04:11:48 -03:00
|
|
|
|
|
|
|
# Walk bottom-up.
|
2007-03-21 06:10:29 -03:00
|
|
|
all = list(os.walk(walk_path, topdown=False))
|
2003-04-25 04:11:48 -03:00
|
|
|
self.assertEqual(len(all), 4)
|
|
|
|
# We can't know which order SUB1 and SUB2 will appear in.
|
|
|
|
# Not flipped: SUB11, SUB1, SUB2, TESTFN
|
|
|
|
# flipped: SUB2, SUB11, SUB1, TESTFN
|
|
|
|
flipped = all[3][1][0] != "SUB1"
|
|
|
|
all[3][1].sort()
|
2007-03-21 06:10:29 -03:00
|
|
|
self.assertEqual(all[3], (walk_path, ["SUB1", "SUB2"], ["tmp1"]))
|
2003-04-25 04:11:48 -03:00
|
|
|
self.assertEqual(all[flipped], (sub11_path, [], []))
|
|
|
|
self.assertEqual(all[flipped + 1], (sub1_path, ["SUB11"], ["tmp2"]))
|
2007-04-04 15:38:47 -03:00
|
|
|
self.assertEqual(all[2 - 2 * flipped], sub2_tree)
|
|
|
|
|
|
|
|
if hasattr(os, "symlink"):
|
|
|
|
# Walk, following symlinks.
|
|
|
|
for root, dirs, files in os.walk(walk_path, followlinks=True):
|
|
|
|
if root == link_path:
|
|
|
|
self.assertEqual(dirs, [])
|
|
|
|
self.assertEqual(files, ["tmp4"])
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.fail("Didn't follow symlink with followlinks=True")
|
2003-04-25 04:11:48 -03:00
|
|
|
|
2007-04-04 15:38:47 -03:00
|
|
|
def tearDown(self):
|
2003-04-25 04:11:48 -03:00
|
|
|
# Tear everything down. This is a decent use for bottom-up on
|
|
|
|
# Windows, which doesn't have a recursive delete command. The
|
|
|
|
# (not so) subtlety is that rmdir will fail unless the dir's
|
|
|
|
# kids are removed first, so bottom up is essential.
|
2003-05-01 14:45:56 -03:00
|
|
|
for root, dirs, files in os.walk(test_support.TESTFN, topdown=False):
|
2003-04-25 04:11:48 -03:00
|
|
|
for name in files:
|
2007-04-04 15:38:47 -03:00
|
|
|
os.remove(os.path.join(root, name))
|
2003-04-25 04:11:48 -03:00
|
|
|
for name in dirs:
|
2007-04-04 15:38:47 -03:00
|
|
|
dirname = os.path.join(root, name)
|
2007-03-21 06:10:29 -03:00
|
|
|
if not os.path.islink(dirname):
|
|
|
|
os.rmdir(dirname)
|
|
|
|
else:
|
|
|
|
os.remove(dirname)
|
2003-05-01 14:45:56 -03:00
|
|
|
os.rmdir(test_support.TESTFN)
|
2003-04-25 04:11:48 -03:00
|
|
|
|
2003-12-23 12:36:11 -04:00
|
|
|
class MakedirTests (unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
os.mkdir(test_support.TESTFN)
|
|
|
|
|
|
|
|
def test_makedir(self):
|
|
|
|
base = test_support.TESTFN
|
|
|
|
path = os.path.join(base, 'dir1', 'dir2', 'dir3')
|
|
|
|
os.makedirs(path) # Should work
|
|
|
|
path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4')
|
|
|
|
os.makedirs(path)
|
|
|
|
|
|
|
|
# Try paths with a '.' in them
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertRaises(OSError, os.makedirs, os.curdir)
|
2003-12-23 12:36:11 -04:00
|
|
|
path = os.path.join(base, 'dir1', 'dir2', 'dir3', 'dir4', 'dir5', os.curdir)
|
|
|
|
os.makedirs(path)
|
|
|
|
path = os.path.join(base, 'dir1', os.curdir, 'dir2', 'dir3', 'dir4',
|
|
|
|
'dir5', 'dir6')
|
|
|
|
os.makedirs(path)
|
|
|
|
|
2004-01-18 16:29:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2003-12-23 12:36:11 -04:00
|
|
|
def tearDown(self):
|
|
|
|
path = os.path.join(test_support.TESTFN, 'dir1', 'dir2', 'dir3',
|
|
|
|
'dir4', 'dir5', 'dir6')
|
|
|
|
# If the tests failed, the bottom-most directory ('../dir6')
|
|
|
|
# may not have been created, so we look for the outermost directory
|
|
|
|
# that exists.
|
|
|
|
while not os.path.exists(path) and path != test_support.TESTFN:
|
|
|
|
path = os.path.dirname(path)
|
|
|
|
|
|
|
|
os.removedirs(path)
|
|
|
|
|
2004-06-08 05:29:33 -03:00
|
|
|
class DevNullTests (unittest.TestCase):
|
|
|
|
def test_devnull(self):
|
|
|
|
f = file(os.devnull, 'w')
|
|
|
|
f.write('hello')
|
|
|
|
f.close()
|
|
|
|
f = file(os.devnull, 'r')
|
2004-06-08 17:34:34 -03:00
|
|
|
self.assertEqual(f.read(), '')
|
2004-06-08 05:29:33 -03:00
|
|
|
f.close()
|
2003-12-23 12:36:11 -04:00
|
|
|
|
2004-08-29 12:46:35 -03:00
|
|
|
class URandomTests (unittest.TestCase):
|
2012-02-20 21:42:21 -04:00
|
|
|
|
|
|
|
def test_urandom_length(self):
|
|
|
|
self.assertEqual(len(os.urandom(0)), 0)
|
|
|
|
self.assertEqual(len(os.urandom(1)), 1)
|
|
|
|
self.assertEqual(len(os.urandom(10)), 10)
|
|
|
|
self.assertEqual(len(os.urandom(100)), 100)
|
|
|
|
self.assertEqual(len(os.urandom(1000)), 1000)
|
|
|
|
|
|
|
|
def test_urandom_value(self):
|
|
|
|
data1 = os.urandom(16)
|
|
|
|
data2 = os.urandom(16)
|
|
|
|
self.assertNotEqual(data1, data2)
|
|
|
|
|
|
|
|
def get_urandom_subprocess(self, count):
|
2012-02-22 17:16:25 -04:00
|
|
|
# We need to use repr() and eval() to avoid line ending conversions
|
|
|
|
# under Windows.
|
2012-02-20 21:42:21 -04:00
|
|
|
code = '\n'.join((
|
|
|
|
'import os, sys',
|
|
|
|
'data = os.urandom(%s)' % count,
|
2012-02-22 17:16:25 -04:00
|
|
|
'sys.stdout.write(repr(data))',
|
2012-02-21 17:02:04 -04:00
|
|
|
'sys.stdout.flush()',
|
|
|
|
'print >> sys.stderr, (len(data), data)'))
|
2012-02-20 21:42:21 -04:00
|
|
|
cmd_line = [sys.executable, '-c', code]
|
|
|
|
p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
|
2012-02-21 17:02:04 -04:00
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2012-02-20 21:42:21 -04:00
|
|
|
out, err = p.communicate()
|
2012-02-21 17:02:04 -04:00
|
|
|
self.assertEqual(p.wait(), 0, (p.wait(), err))
|
2012-02-22 17:16:25 -04:00
|
|
|
out = eval(out)
|
|
|
|
self.assertEqual(len(out), count, err)
|
2012-02-20 21:42:21 -04:00
|
|
|
return out
|
|
|
|
|
|
|
|
def test_urandom_subprocess(self):
|
|
|
|
data1 = self.get_urandom_subprocess(16)
|
|
|
|
data2 = self.get_urandom_subprocess(16)
|
|
|
|
self.assertNotEqual(data1, data2)
|
2004-08-29 12:46:35 -03:00
|
|
|
|
2014-12-26 13:09:00 -04:00
|
|
|
|
|
|
|
HAVE_GETENTROPY = (sysconfig.get_config_var('HAVE_GETENTROPY') == 1)
|
|
|
|
|
|
|
|
@unittest.skipIf(HAVE_GETENTROPY,
|
|
|
|
"getentropy() does not use a file descriptor")
|
|
|
|
class URandomFDTests(unittest.TestCase):
|
2013-08-16 15:44:38 -03:00
|
|
|
@unittest.skipUnless(resource, "test requires the resource module")
|
|
|
|
def test_urandom_failure(self):
|
2013-08-24 15:52:27 -03:00
|
|
|
# Check urandom() failing when it is not able to open /dev/random.
|
|
|
|
# We spawn a new process to make the test more robust (if getrlimit()
|
|
|
|
# failed to restore the file descriptor limit after this, the whole
|
|
|
|
# test suite would crash; this actually happened on the OS X Tiger
|
|
|
|
# buildbot).
|
|
|
|
code = """if 1:
|
|
|
|
import errno
|
|
|
|
import os
|
|
|
|
import resource
|
|
|
|
|
|
|
|
soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
|
|
|
|
resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit))
|
|
|
|
try:
|
2013-08-16 15:44:38 -03:00
|
|
|
os.urandom(16)
|
2013-08-24 15:52:27 -03:00
|
|
|
except OSError as e:
|
|
|
|
assert e.errno == errno.EMFILE, e.errno
|
|
|
|
else:
|
|
|
|
raise AssertionError("OSError not raised")
|
|
|
|
"""
|
|
|
|
assert_python_ok('-c', code)
|
2013-08-16 15:44:38 -03:00
|
|
|
|
2013-08-16 15:56:12 -03:00
|
|
|
|
|
|
|
class ExecvpeTests(unittest.TestCase):
|
|
|
|
|
2010-03-19 11:45:06 -03:00
|
|
|
def test_execvpe_with_bad_arglist(self):
|
|
|
|
self.assertRaises(ValueError, os.execvpe, 'notepad', [], None)
|
|
|
|
|
2013-08-16 15:56:12 -03:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
|
2006-05-04 07:08:42 -03:00
|
|
|
class Win32ErrorTests(unittest.TestCase):
|
|
|
|
def test_rename(self):
|
|
|
|
self.assertRaises(WindowsError, os.rename, test_support.TESTFN, test_support.TESTFN+".bak")
|
|
|
|
|
|
|
|
def test_remove(self):
|
|
|
|
self.assertRaises(WindowsError, os.remove, test_support.TESTFN)
|
|
|
|
|
|
|
|
def test_chdir(self):
|
|
|
|
self.assertRaises(WindowsError, os.chdir, test_support.TESTFN)
|
|
|
|
|
2006-05-06 13:32:54 -03:00
|
|
|
def test_mkdir(self):
|
2009-02-06 06:17:34 -04:00
|
|
|
f = open(test_support.TESTFN, "w")
|
|
|
|
try:
|
|
|
|
self.assertRaises(WindowsError, os.mkdir, test_support.TESTFN)
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
os.unlink(test_support.TESTFN)
|
2006-05-06 13:32:54 -03:00
|
|
|
|
|
|
|
def test_utime(self):
|
|
|
|
self.assertRaises(WindowsError, os.utime, test_support.TESTFN, None)
|
|
|
|
|
|
|
|
def test_chmod(self):
|
2009-02-06 06:17:34 -04:00
|
|
|
self.assertRaises(WindowsError, os.chmod, test_support.TESTFN, 0)
|
2006-05-06 13:32:54 -03:00
|
|
|
|
2009-01-12 14:09:27 -04:00
|
|
|
class TestInvalidFD(unittest.TestCase):
|
2009-01-15 18:40:03 -04:00
|
|
|
singles = ["fchdir", "fdopen", "dup", "fdatasync", "fstat",
|
2009-01-12 14:09:27 -04:00
|
|
|
"fstatvfs", "fsync", "tcgetpgrp", "ttyname"]
|
2009-01-15 18:40:03 -04:00
|
|
|
#singles.append("close")
|
|
|
|
#We omit close because it doesn'r raise an exception on some platforms
|
2009-01-12 14:09:27 -04:00
|
|
|
def get_single(f):
|
|
|
|
def helper(self):
|
2009-01-19 13:37:42 -04:00
|
|
|
if hasattr(os, f):
|
|
|
|
self.check(getattr(os, f))
|
2009-01-12 14:09:27 -04:00
|
|
|
return helper
|
|
|
|
for f in singles:
|
|
|
|
locals()["test_"+f] = get_single(f)
|
|
|
|
|
2009-01-19 13:37:42 -04:00
|
|
|
def check(self, f, *args):
|
2009-01-30 21:42:55 -04:00
|
|
|
try:
|
|
|
|
f(test_support.make_bad_fd(), *args)
|
|
|
|
except OSError as e:
|
|
|
|
self.assertEqual(e.errno, errno.EBADF)
|
|
|
|
else:
|
|
|
|
self.fail("%r didn't raise a OSError with a bad file descriptor"
|
|
|
|
% f)
|
2009-01-19 13:37:42 -04:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'isatty'), 'test needs os.isatty()')
|
2009-01-12 14:09:27 -04:00
|
|
|
def test_isatty(self):
|
2013-11-03 17:15:46 -04:00
|
|
|
self.assertEqual(os.isatty(test_support.make_bad_fd()), False)
|
2009-01-12 14:09:27 -04:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'closerange'), 'test needs os.closerange()')
|
2009-01-12 14:09:27 -04:00
|
|
|
def test_closerange(self):
|
2013-11-03 17:15:46 -04:00
|
|
|
fd = test_support.make_bad_fd()
|
|
|
|
# Make sure none of the descriptors we are about to close are
|
|
|
|
# currently valid (issue 6542).
|
|
|
|
for i in range(10):
|
|
|
|
try: os.fstat(fd+i)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
if i < 2:
|
|
|
|
raise unittest.SkipTest(
|
|
|
|
"Unable to acquire a range of invalid file descriptors")
|
|
|
|
self.assertEqual(os.closerange(fd, fd + i-1), None)
|
2009-01-12 14:09:27 -04:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'dup2'), 'test needs os.dup2()')
|
2009-01-12 14:09:27 -04:00
|
|
|
def test_dup2(self):
|
2013-11-03 17:15:46 -04:00
|
|
|
self.check(os.dup2, 20)
|
2009-01-12 14:09:27 -04:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'fchmod'), 'test needs os.fchmod()')
|
2009-01-12 14:09:27 -04:00
|
|
|
def test_fchmod(self):
|
2013-11-03 17:15:46 -04:00
|
|
|
self.check(os.fchmod, 0)
|
2009-01-12 14:09:27 -04:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'fchown'), 'test needs os.fchown()')
|
2009-01-12 14:09:27 -04:00
|
|
|
def test_fchown(self):
|
2013-11-03 17:15:46 -04:00
|
|
|
self.check(os.fchown, -1, -1)
|
2009-01-12 14:09:27 -04:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'fpathconf'), 'test needs os.fpathconf()')
|
2009-01-12 14:09:27 -04:00
|
|
|
def test_fpathconf(self):
|
2013-11-03 17:15:46 -04:00
|
|
|
self.check(os.fpathconf, "PC_NAME_MAX")
|
2009-01-12 14:09:27 -04:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'ftruncate'), 'test needs os.ftruncate()')
|
2009-01-12 14:09:27 -04:00
|
|
|
def test_ftruncate(self):
|
2013-11-03 17:15:46 -04:00
|
|
|
self.check(os.ftruncate, 0)
|
2009-01-12 14:09:27 -04:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'lseek'), 'test needs os.lseek()')
|
2009-01-12 14:09:27 -04:00
|
|
|
def test_lseek(self):
|
2013-11-03 17:15:46 -04:00
|
|
|
self.check(os.lseek, 0, 0)
|
2009-01-12 14:09:27 -04:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'read'), 'test needs os.read()')
|
2009-01-12 14:09:27 -04:00
|
|
|
def test_read(self):
|
2013-11-03 17:15:46 -04:00
|
|
|
self.check(os.read, 1)
|
2009-01-12 14:09:27 -04:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'tcsetpgrp'), 'test needs os.tcsetpgrp()')
|
2009-01-12 14:09:27 -04:00
|
|
|
def test_tcsetpgrpt(self):
|
2013-11-03 17:15:46 -04:00
|
|
|
self.check(os.tcsetpgrp, 0)
|
2009-01-12 14:09:27 -04:00
|
|
|
|
2013-11-03 17:15:46 -04:00
|
|
|
@unittest.skipUnless(hasattr(os, 'write'), 'test needs os.write()')
|
2009-01-12 14:09:27 -04:00
|
|
|
def test_write(self):
|
2013-11-03 17:15:46 -04:00
|
|
|
self.check(os.write, " ")
|
|
|
|
|
|
|
|
@unittest.skipIf(sys.platform == "win32", "Posix specific tests")
|
|
|
|
class PosixUidGidTests(unittest.TestCase):
|
|
|
|
@unittest.skipUnless(hasattr(os, 'setuid'), 'test needs os.setuid()')
|
|
|
|
def test_setuid(self):
|
|
|
|
if os.getuid() != 0:
|
|
|
|
self.assertRaises(os.error, os.setuid, 0)
|
|
|
|
self.assertRaises(OverflowError, os.setuid, 1<<32)
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(os, 'setgid'), 'test needs os.setgid()')
|
|
|
|
def test_setgid(self):
|
|
|
|
if os.getuid() != 0:
|
|
|
|
self.assertRaises(os.error, os.setgid, 0)
|
|
|
|
self.assertRaises(OverflowError, os.setgid, 1<<32)
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(os, 'seteuid'), 'test needs os.seteuid()')
|
|
|
|
def test_seteuid(self):
|
|
|
|
if os.getuid() != 0:
|
|
|
|
self.assertRaises(os.error, os.seteuid, 0)
|
|
|
|
self.assertRaises(OverflowError, os.seteuid, 1<<32)
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(os, 'setegid'), 'test needs os.setegid()')
|
|
|
|
def test_setegid(self):
|
|
|
|
if os.getuid() != 0:
|
|
|
|
self.assertRaises(os.error, os.setegid, 0)
|
|
|
|
self.assertRaises(OverflowError, os.setegid, 1<<32)
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(os, 'setreuid'), 'test needs os.setreuid()')
|
|
|
|
def test_setreuid(self):
|
|
|
|
if os.getuid() != 0:
|
|
|
|
self.assertRaises(os.error, os.setreuid, 0, 0)
|
|
|
|
self.assertRaises(OverflowError, os.setreuid, 1<<32, 0)
|
|
|
|
self.assertRaises(OverflowError, os.setreuid, 0, 1<<32)
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(os, 'setreuid'), 'test needs os.setreuid()')
|
|
|
|
def test_setreuid_neg1(self):
|
|
|
|
# Needs to accept -1. We run this in a subprocess to avoid
|
|
|
|
# altering the test runner's process state (issue8045).
|
|
|
|
subprocess.check_call([
|
|
|
|
sys.executable, '-c',
|
|
|
|
'import os,sys;os.setreuid(-1,-1);sys.exit(0)'])
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(os, 'setregid'), 'test needs os.setregid()')
|
|
|
|
def test_setregid(self):
|
|
|
|
if os.getuid() != 0:
|
|
|
|
self.assertRaises(os.error, os.setregid, 0, 0)
|
|
|
|
self.assertRaises(OverflowError, os.setregid, 1<<32, 0)
|
|
|
|
self.assertRaises(OverflowError, os.setregid, 0, 1<<32)
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(os, 'setregid'), 'test needs os.setregid()')
|
|
|
|
def test_setregid_neg1(self):
|
|
|
|
# Needs to accept -1. We run this in a subprocess to avoid
|
|
|
|
# altering the test runner's process state (issue8045).
|
|
|
|
subprocess.check_call([
|
|
|
|
sys.executable, '-c',
|
|
|
|
'import os,sys;os.setregid(-1,-1);sys.exit(0)'])
|
|
|
|
|
2009-04-05 20:43:58 -03:00
|
|
|
|
2010-04-02 20:26:06 -03:00
|
|
|
@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
|
|
|
|
class Win32KillTests(unittest.TestCase):
|
2010-04-14 21:40:40 -03:00
|
|
|
def _kill(self, sig):
|
|
|
|
# Start sys.executable as a subprocess and communicate from the
|
|
|
|
# subprocess to the parent that the interpreter is ready. When it
|
|
|
|
# becomes ready, send *sig* via os.kill to the subprocess and check
|
|
|
|
# that the return code is equal to *sig*.
|
|
|
|
import ctypes
|
|
|
|
from ctypes import wintypes
|
|
|
|
import msvcrt
|
|
|
|
|
|
|
|
# Since we can't access the contents of the process' stdout until the
|
|
|
|
# process has exited, use PeekNamedPipe to see what's inside stdout
|
|
|
|
# without waiting. This is done so we can tell that the interpreter
|
|
|
|
# is started and running at a point where it could handle a signal.
|
|
|
|
PeekNamedPipe = ctypes.windll.kernel32.PeekNamedPipe
|
|
|
|
PeekNamedPipe.restype = wintypes.BOOL
|
|
|
|
PeekNamedPipe.argtypes = (wintypes.HANDLE, # Pipe handle
|
|
|
|
ctypes.POINTER(ctypes.c_char), # stdout buf
|
|
|
|
wintypes.DWORD, # Buffer size
|
|
|
|
ctypes.POINTER(wintypes.DWORD), # bytes read
|
|
|
|
ctypes.POINTER(wintypes.DWORD), # bytes avail
|
|
|
|
ctypes.POINTER(wintypes.DWORD)) # bytes left
|
|
|
|
msg = "running"
|
|
|
|
proc = subprocess.Popen([sys.executable, "-c",
|
|
|
|
"import sys;"
|
|
|
|
"sys.stdout.write('{}');"
|
|
|
|
"sys.stdout.flush();"
|
|
|
|
"input()".format(msg)],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
stdin=subprocess.PIPE)
|
2010-11-05 12:31:20 -03:00
|
|
|
self.addCleanup(proc.stdout.close)
|
|
|
|
self.addCleanup(proc.stderr.close)
|
|
|
|
self.addCleanup(proc.stdin.close)
|
2010-04-14 21:40:40 -03:00
|
|
|
|
2010-05-28 12:49:21 -03:00
|
|
|
count, max = 0, 100
|
|
|
|
while count < max and proc.poll() is None:
|
|
|
|
# Create a string buffer to store the result of stdout from the pipe
|
|
|
|
buf = ctypes.create_string_buffer(len(msg))
|
|
|
|
# Obtain the text currently in proc.stdout
|
|
|
|
# Bytes read/avail/left are left as NULL and unused
|
|
|
|
rslt = PeekNamedPipe(msvcrt.get_osfhandle(proc.stdout.fileno()),
|
|
|
|
buf, ctypes.sizeof(buf), None, None, None)
|
|
|
|
self.assertNotEqual(rslt, 0, "PeekNamedPipe failed")
|
|
|
|
if buf.value:
|
|
|
|
self.assertEqual(msg, buf.value)
|
|
|
|
break
|
|
|
|
time.sleep(0.1)
|
|
|
|
count += 1
|
|
|
|
else:
|
|
|
|
self.fail("Did not receive communication from the subprocess")
|
2010-04-14 21:40:40 -03:00
|
|
|
|
2010-04-02 20:26:06 -03:00
|
|
|
os.kill(proc.pid, sig)
|
|
|
|
self.assertEqual(proc.wait(), sig)
|
|
|
|
|
|
|
|
def test_kill_sigterm(self):
|
|
|
|
# SIGTERM doesn't mean anything special, but make sure it works
|
2010-04-14 21:40:40 -03:00
|
|
|
self._kill(signal.SIGTERM)
|
2010-04-02 20:26:06 -03:00
|
|
|
|
|
|
|
def test_kill_int(self):
|
|
|
|
# os.kill on Windows can take an int which gets set as the exit code
|
2010-04-14 21:40:40 -03:00
|
|
|
self._kill(100)
|
2010-04-02 20:26:06 -03:00
|
|
|
|
|
|
|
def _kill_with_event(self, event, name):
|
2010-10-08 06:41:13 -03:00
|
|
|
tagname = "test_os_%s" % uuid.uuid1()
|
|
|
|
m = mmap.mmap(-1, 1, tagname)
|
|
|
|
m[0] = '0'
|
2010-04-02 20:26:06 -03:00
|
|
|
# Run a script which has console control handling enabled.
|
|
|
|
proc = subprocess.Popen([sys.executable,
|
|
|
|
os.path.join(os.path.dirname(__file__),
|
2010-10-08 06:41:13 -03:00
|
|
|
"win_console_handler.py"), tagname],
|
2010-04-02 20:26:06 -03:00
|
|
|
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
|
|
|
|
# Let the interpreter startup before we send signals. See #3137.
|
2010-10-08 06:41:13 -03:00
|
|
|
count, max = 0, 20
|
|
|
|
while count < max and proc.poll() is None:
|
2010-11-05 12:28:19 -03:00
|
|
|
if m[0] == '1':
|
2010-10-08 06:41:13 -03:00
|
|
|
break
|
|
|
|
time.sleep(0.5)
|
|
|
|
count += 1
|
|
|
|
else:
|
|
|
|
self.fail("Subprocess didn't finish initialization")
|
2010-04-02 20:26:06 -03:00
|
|
|
os.kill(proc.pid, event)
|
|
|
|
# proc.send_signal(event) could also be done here.
|
|
|
|
# Allow time for the signal to be passed and the process to exit.
|
2010-04-05 16:04:23 -03:00
|
|
|
time.sleep(0.5)
|
2010-04-02 20:26:06 -03:00
|
|
|
if not proc.poll():
|
|
|
|
# Forcefully kill the process if we weren't able to signal it.
|
|
|
|
os.kill(proc.pid, signal.SIGINT)
|
|
|
|
self.fail("subprocess did not stop on {}".format(name))
|
|
|
|
|
|
|
|
@unittest.skip("subprocesses aren't inheriting CTRL+C property")
|
|
|
|
def test_CTRL_C_EVENT(self):
|
|
|
|
from ctypes import wintypes
|
|
|
|
import ctypes
|
|
|
|
|
|
|
|
# Make a NULL value by creating a pointer with no argument.
|
|
|
|
NULL = ctypes.POINTER(ctypes.c_int)()
|
|
|
|
SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler
|
|
|
|
SetConsoleCtrlHandler.argtypes = (ctypes.POINTER(ctypes.c_int),
|
|
|
|
wintypes.BOOL)
|
|
|
|
SetConsoleCtrlHandler.restype = wintypes.BOOL
|
|
|
|
|
|
|
|
# Calling this with NULL and FALSE causes the calling process to
|
|
|
|
# handle CTRL+C, rather than ignore it. This property is inherited
|
|
|
|
# by subprocesses.
|
|
|
|
SetConsoleCtrlHandler(NULL, 0)
|
|
|
|
|
|
|
|
self._kill_with_event(signal.CTRL_C_EVENT, "CTRL_C_EVENT")
|
|
|
|
|
|
|
|
def test_CTRL_BREAK_EVENT(self):
|
|
|
|
self._kill_with_event(signal.CTRL_BREAK_EVENT, "CTRL_BREAK_EVENT")
|
|
|
|
|
|
|
|
|
2001-09-20 18:33:42 -03:00
|
|
|
def test_main():
|
2003-05-01 14:45:56 -03:00
|
|
|
test_support.run_unittest(
|
2006-07-02 15:44:00 -03:00
|
|
|
FileTests,
|
2003-05-01 14:45:56 -03:00
|
|
|
TemporaryFileTests,
|
|
|
|
StatAttributeTests,
|
|
|
|
EnvironTests,
|
2003-12-23 12:36:11 -04:00
|
|
|
WalkTests,
|
|
|
|
MakedirTests,
|
2004-06-08 05:29:33 -03:00
|
|
|
DevNullTests,
|
2006-05-04 07:08:42 -03:00
|
|
|
URandomTests,
|
2015-03-17 08:34:46 -03:00
|
|
|
URandomFDTests,
|
2013-08-16 15:56:12 -03:00
|
|
|
ExecvpeTests,
|
2009-01-12 14:09:27 -04:00
|
|
|
Win32ErrorTests,
|
2009-04-05 20:43:58 -03:00
|
|
|
TestInvalidFD,
|
2010-04-02 20:26:06 -03:00
|
|
|
PosixUidGidTests,
|
|
|
|
Win32KillTests
|
2003-05-01 14:45:56 -03:00
|
|
|
)
|
2001-09-20 18:33:42 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|