returns a date relative to the local timezone instead of UTC, properly
taking daylight savings time into account.
- Optional argument usegmt means that the timezone is written out as
+ Optional argument usegmt means that the timezone is written out as
an ascii string, not numeric one (so "GMT" instead of "+0000"). This
is needed for HTTP, and is only used when localtime==False.
"""
into an absolute file name.
"""
if os.path.isabs(filename) and os.path.exists(filename):
- return filename
+ return filename
f = os.path.join(sys.path[0], filename)
if os.path.exists(f) and self.canonic(f) == self.mainpyfile:
return f
# Start with fresh empty copy of globals and locals and tell the script
# that it's being run as __main__ to avoid scripts being able to access
# the pdb.py namespace.
- globals_ = {"__name__" : "__main__"}
- locals_ = globals_
+ globals_ = {"__name__" : "__main__"}
+ locals_ = globals_
# When bdb sets tracing, a number of call and line events happens
# BEFORE debugger even reaches user's code (and the exact sequence of
pdb._runscript(mainpyfile)
if pdb._user_requested_quit:
break
- print "The program finished and will be restarted"
+ print "The program finished and will be restarted"
except SystemExit:
# In most cases SystemExit does not warrant a post-mortem session.
print "The program exited via sys.exit(). Exit status: ",
# When invoked as main program, invoke the debugger on a script
if __name__=='__main__':
main()
-
# subprocess - Subprocesses with accessible I/O streams
#
-# For more information about this module, see PEP 324.
+# For more information about this module, see PEP 324.
#
# Copyright (c) 2003-2004 by Peter Astrand <astrand@lysator.liu.se>
#
terminate. The optional stdin argument should be a string to be
sent to the child process, or None, if no data should be sent to
the child.
-
+
communicate() returns a tuple (stdout, stderr).
Note: The data read is buffered in memory, so do not use this
Replacing os.spawn*
-------------------
-P_NOWAIT example:
+P_NOWAIT example:
pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
==>
retcode = call(["/bin/mycmd", "myarg"])
-Vector example:
+Vector example:
os.spawnvp(os.P_NOWAIT, path, args)
==>
Popen([path] + args[1:])
-Environment example:
+Environment example:
os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env)
==>
Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})
-Replacing os.popen*
+Replacing os.popen*
-------------------
pipe = os.popen(cmd, mode='r', bufsize)
==>
* the capturestderr argument is replaced with the stderr argument.
* stdin=PIPE and stdout=PIPE must be specified.
* popen2 closes all filedescriptors by default, but you have to specify
- close_fds=True with subprocess.Popen.
+ close_fds=True with subprocess.Popen.
"""
for c in arg:
if c == '\\':
- # Don't know if we need to double yet.
+ # Don't know if we need to double yet.
bs_buf.append(c)
elif c == '"':
- # Double backspaces.
+ # Double backspaces.
result.append('\\' * len(bs_buf)*2)
bs_buf = []
result.append('\\"')
bs_buf = []
result.append(c)
- # Add remaining backspaces, if any.
+ # Add remaining backspaces, if any.
if bs_buf:
result.extend(bs_buf)
if creationflags != 0:
raise ValueError("creationflags is only supported on Windows platforms")
- self.stdin = None
+ self.stdin = None
self.stdout = None
self.stderr = None
self.pid = None
# are file descriptors on both platforms. The parent objects
# are None when not using PIPEs. The child objects are None
# when not redirecting.
-
+
(p2cread, p2cwrite,
c2pread, c2pwrite,
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
self.stderr = os.fdopen(errread, 'rU', bufsize)
else:
self.stderr = os.fdopen(errread, 'rb', bufsize)
-
+
_active.append(self)
"""
if stdin == None and stdout == None and stderr == None:
return (None, None, None, None, None, None)
-
+
p2cread, p2cwrite = None, None
c2pread, c2pwrite = None, None
errread, errwrite = None, None
"for Popen to work with your shell or platform.")
return w9xpopen
-
+
def _execute_child(self, args, executable, preexec_fn, close_fds,
cwd, env, universal_newlines,
startupinfo, creationflags, shell,
if startupinfo == None:
startupinfo = STARTUPINFO()
if not None in (p2cread, c2pwrite, errwrite):
- startupinfo.dwFlags |= STARTF_USESTDHANDLES
+ startupinfo.dwFlags |= STARTF_USESTDHANDLES
startupinfo.hStdInput = p2cread
startupinfo.hStdOutput = c2pwrite
startupinfo.hStdError = errwrite
if errwrite != None:
errwrite.Close()
-
+
def poll(self):
"""Check if child process has terminated. Returns returncode
attribute."""
c2pwrite = stdout
else:
# Assuming file-like object
- c2pwrite = stdout.fileno()
+ c2pwrite = stdout.fileno()
if stderr == None:
pass
os.close(i)
except:
pass
-
-
+
+
def _execute_child(self, args, executable, preexec_fn, close_fds,
cwd, env, universal_newlines,
startupinfo, creationflags, shell,
_active.remove(self)
-
+
def poll(self):
"""Check if child process has terminated. Returns returncode
attribute."""
self.stdin.close()
if self.stdout:
read_set.append(self.stdout)
- stdout = []
+ stdout = []
if self.stderr:
read_set.append(self.stderr)
stderr = []
_demo_windows()
else:
_demo_posix()
-
-
else:
fname = tempfile.mktemp()
return os.open(fname, os.O_RDWR|os.O_CREAT), fname
-
+
#
# Generic tests
#
def test_stdin_filedes(self):
"""stdin is set to open file descriptor"""
- tf = tempfile.TemporaryFile()
+ tf = tempfile.TemporaryFile()
d = tf.fileno()
os.write(d, "pear")
os.lseek(d, 0, 0)
def test_stdout_filedes(self):
"""stdout is set to open file descriptor"""
- tf = tempfile.TemporaryFile()
+ tf = tempfile.TemporaryFile()
d = tf.fileno()
p = subprocess.Popen([sys.executable, "-c",
'import sys; sys.stdout.write("orange")'],
def test_stdout_fileobj(self):
"""stdout is set to open file object"""
- tf = tempfile.TemporaryFile()
+ tf = tempfile.TemporaryFile()
p = subprocess.Popen([sys.executable, "-c",
'import sys; sys.stdout.write("orange")'],
stdout=tf)
def test_stderr_filedes(self):
"""stderr is set to open file descriptor"""
- tf = tempfile.TemporaryFile()
+ tf = tempfile.TemporaryFile()
d = tf.fileno()
p = subprocess.Popen([sys.executable, "-c",
'import sys; sys.stderr.write("strawberry")'],
def test_stderr_fileobj(self):
"""stderr is set to open file object"""
- tf = tempfile.TemporaryFile()
+ tf = tempfile.TemporaryFile()
p = subprocess.Popen([sys.executable, "-c",
'import sys; sys.stderr.write("strawberry")'],
stderr=tf)
def test_communicate_pipe_buf(self):
"""communicate() with writes larger than pipe_buf"""
# This test will probably deadlock rather than fail, if
- # communicate() does not work properly.
+ # communicate() does not work properly.
x, y = os.pipe()
if mswindows:
pipe_buf = 512
os.close(x)
os.close(y)
p = subprocess.Popen([sys.executable, "-c",
- 'import sys,os;'
+ 'import sys,os;'
'sys.stdout.write(sys.stdin.read(47));' \
'sys.stderr.write("xyz"*%d);' \
'sys.stdout.write(sys.stdin.read())' % pipe_buf],
(stdout, stderr) = p.communicate("split")
self.assertEqual(stdout, "bananasplit")
self.assertEqual(stderr, "")
-
+
def test_universal_newlines(self):
"""universal newlines"""
p = subprocess.Popen([sys.executable, "-c",
self.assertEqual(p.wait(), 0)
# Subsequent invocations should just return the returncode
self.assertEqual(p.wait(), 0)
-
+
#
# POSIX tests
#
self.assertNotEqual(e.child_traceback.find("os.chdir"), -1)
else:
self.fail("Expected OSError")
-
+
def test_run_abort(self):
"""returncode handles signal termination"""
p = subprocess.Popen([sys.executable, "-c", "import os; os.abort()"])
'import sys,os;' \
'sys.stdout.write(str(os.dup(0)))'],
stdout=subprocess.PIPE, close_fds=1)
- # When all fds are closed, the next free fd should be 3.
+ # When all fds are closed, the next free fd should be 3.
self.assertEqual(p.stdout.read(), "3")
def test_args_string(self):
rc = subprocess.call(fname)
self.assertEqual(rc, 47)
-
+
#
# Windows tests
#
def test_startupinfo(self):
"""startupinfo argument"""
# We uses hardcoded constants, because we do not want to
- # depend on win32all.
+ # depend on win32all.
STARTF_USESHOWWINDOW = 1
SW_MAXIMIZE = 3
startupinfo = subprocess.STARTUPINFO()
newenv = os.environ.copy()
newenv["FRUIT"] = "physalis"
p = subprocess.Popen(["set"], shell=1,
- stdout=subprocess.PIPE,
+ stdout=subprocess.PIPE,
env=newenv)
self.assertNotEqual(p.stdout.read().find("physalis"), -1)
newenv = os.environ.copy()
newenv["FRUIT"] = "physalis"
p = subprocess.Popen("set", shell=1,
- stdout=subprocess.PIPE,
+ stdout=subprocess.PIPE,
env=newenv)
self.assertNotEqual(p.stdout.read().find("physalis"), -1)
if __name__ == "__main__":
test_main()
-