1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<meta content="text/html;charset=ISO-8859-1" name="Content-Type"/>
6
<link href="style.css" media="screen" rel="stylesheet" type="text/css"/></head>
9
<div><a href="http://pylib.org"><img alt="py lib" height="57" id="pyimg" src="http://codespeak.net/img/pylib.png" width="77"/></a></div>
12
<div><a class="menu" href="announce/release-1.2.1.html">1.2.1 ANN</a></div></div>
14
<div><a class="menu" href="install.html">INSTALL</a></div></div>
16
<div><a class="menu" href="contact.html">CONTACT</a></div></div>
18
<div><a class="menu" href="changelog.html">CHANGELOG</a></div></div>
20
<div><a class="menu" href="faq.html">FAQ</a></div></div>
24
<div><a class="menu" href="test/index.html">Index</a></div>
25
<div><a class="menu" href="test/quickstart.html">Quickstart</a></div>
26
<div><a class="menu" href="test/features.html">Features</a></div>
27
<div><a class="menu" href="test/plugin/index.html">Plugins</a></div>
28
<div><a class="menu" href="test/funcargs.html">Funcargs</a></div>
29
<div><a class="menu" href="test/customize.html">Customize</a></div>
30
<div><a class="menu" href="test/talks.html">Tutorials</a></div></div></div>
33
<h3>supporting APIs:</h3>
34
<div><a class="menu" href="index.html">Index</a></div>
35
<div><a class="menu" href="path.html">py.path</a></div>
36
<div><a class="menu" href="code.html">py.code</a></div></div></div></div></div>
37
<div id="contentspace">
38
<div class="document" id="miscellaneous-features-of-the-py-lib">
39
<h1 class="title">Miscellaneous features of the py lib</h1>
41
<div class="section" id="mapping-the-standard-python-library-into-py">
42
<h1>Mapping the standard python library into py</h1>
43
<p>The <tt class="docutils literal">py.std</tt> object allows lazy access to
44
standard library modules. For example, to get to the print-exception
45
functionality of the standard library you can write:</p>
46
<pre class="literal-block">
47
py.std.traceback.print_exc()
49
<p>without having to do anything else than the usual <tt class="docutils literal">import py</tt>
50
at the beginning. You can access any other top-level standard
51
library module this way. This means that you will only trigger
52
imports of modules that are actually needed. Note that no attempt
53
is made to import submodules.</p>
55
<div class="section" id="support-for-interaction-with-system-utilities-binaries">
56
<h1>Support for interaction with system utilities/binaries</h1>
57
<p>Currently, the py lib offers two ways to interact with
58
system executables. <tt class="docutils literal">py.process.cmdexec()</tt> invokes
59
the shell in order to execute a string. The other
60
one, <tt class="docutils literal">py.path.local</tt>'s 'sysexec()' method lets you
61
directly execute a binary.</p>
62
<p>Both approaches will raise an exception in case of a return-
63
code other than 0 and otherwise return the stdout-output
64
of the child process.</p>
65
<div class="section" id="the-shell-based-approach">
66
<h2>The shell based approach</h2>
67
<p>You can execute a command via your system shell
68
by doing something like:</p>
69
<pre class="literal-block">
70
out = py.process.cmdexec('ls -v')
72
<p>However, the <tt class="docutils literal">cmdexec</tt> approach has a few shortcomings:</p>
74
<li>it relies on the underlying system shell</li>
75
<li>it neccessitates shell-escaping for expressing arguments</li>
76
<li>it does not easily allow to "fix" the binary you want to run.</li>
77
<li>it only allows to execute executables from the local
81
<div class="section" id="local-paths-have-sysexec">
82
<span id="sysexec"></span><h2>local paths have <tt class="docutils literal">sysexec</tt></h2>
83
<p>In order to synchronously execute an executable file you
84
can use <tt class="docutils literal">sysexec</tt>:</p>
85
<pre class="literal-block">
86
binsvn.sysexec('ls', 'http://codespeak.net/svn')
88
<p>where <tt class="docutils literal">binsvn</tt> is a path that points to the <tt class="docutils literal">svn</tt> commandline
89
binary. Note that this function does not offer any shell-escaping
90
so you have to pass in already separated arguments.</p>
92
<div class="section" id="finding-an-executable-local-path">
93
<h2>finding an executable local path</h2>
94
<p>Finding an executable is quite different on multiple platforms.
95
Currently, the <tt class="docutils literal">PATH</tt> environment variable based search on
96
unix platforms is supported:</p>
97
<pre class="literal-block">
98
py.path.local.sysfind('svn')
100
<p>which returns the first path whose <tt class="docutils literal">basename</tt> matches <tt class="docutils literal">svn</tt>.
101
In principle, <cite>sysfind</cite> deploys platform specific algorithms
102
to perform the search. On Windows, for example, it may look
103
at the registry (XXX).</p>
104
<p>To make the story complete, we allow to pass in a second <tt class="docutils literal">checker</tt>
105
argument that is called for each found executable. For example, if
106
you have multiple binaries available you may want to select the
108
<pre class="literal-block">
110
""" check that the given svn binary has version 1.1. """
111
line = p.execute('--version'').readlines()[0]
112
if line.find('version 1.1'):
114
binsvn = py.path.local.sysfind('svn', checker=mysvn)
118
<div class="section" id="cross-python-version-compatibility-helpers">
119
<h1>Cross-Python Version compatibility helpers</h1>
120
<p>The <tt class="docutils literal">py.builtin</tt> namespace provides a number of helpers that help to write python code compatible across Python interpreters, mainly Python2 and Python3. Type <tt class="docutils literal">help(py.builtin)</tt> on a Python prompt for a the selection of builtins.</p>
124
<script type="text/javascript">
125
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
126
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
128
<script type="text/javascript">
130
var pageTracker = _gat._getTracker("UA-7597274-3");
131
pageTracker._trackPageview();
132
} catch(err) {}</script>
b'\\ No newline at end of file'