<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Nov 2, 2017, at 4:39 AM, Richard Wei <<a href="mailto:rxrwei@gmail.com" class="">rxrwei@gmail.com</a>> wrote:</div><div class=""><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="">Question though: how does it work? Say the first np.array call threw a python exception:</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space;"><div class=""><div class=""><div class=""><font face="Menlo" class="">try Python.do {</font></div><div class=""><font face="Menlo" class=""> let a = np.array([1, 2, 3])</font></div><div class=""><font face="Menlo" class=""> let b = np.array([[2], [4]])</font></div><div class=""><font face="Menlo" class=""> print(a.dot(b)) // matrix mul with incompatible shapes</font></div><div class=""><font face="Menlo" class=""> }</font></div></div></div></div></blockquote></div><div class=""><br class=""></div><div class="">We can definitely make the python glue code notice it, catch it and squirrel it away somewhere, but without compiler hacks we couldn’t make it jump out of the closure. This means that np.array would have to return something, and the calls below it would still execute, or am I missing something?</div></div></div></blockquote><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">We make PythonObjects internally nullable (only in the exception-caught state). The second np.array would just return a null PythonObject. </div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">To be specific, we define three states in the python overlay:</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">- Normal state: PythonObjects are guaranteed to be non-null. Any exception traps.</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">- Exception-catching state: PythonObjects are still guaranteed to be non-null. Any exception triggers the exception-caught state.</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">- Exception-caught state: PythonObjects are nullable — all python expressions return a null PythonObject.</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">The exception-catching state is entered during the execution of Python.do’s body.</div></div></blockquote><br class=""></div><div><br class=""></div><div><div class="">I don’t think that will work well in practice: if you’re intermixing swift and python calls, it would be extremely surprising to see values be computed but produce no value. This seems similar to the ObjC "messaging nil” problem.</div><div class=""><br class=""></div><div class="">That said, I’ve been experimenting with another approach that seems to work well in practice. Due to the language mismatch, a Swift programmer is going to have to decide whether they care about a throwing call or not. As such, I think it makes sense to make this explicit with some kind of syntax - either a postfix operator like ^ or a method like “throwing”. This allows you to write (when we have language sugar) either "a.foo()” or “a.throwing.foo()” if you want to handle the case when foo throws an exception.</div><div class=""><br class=""></div><div class="">This sounds yucky, but actually works pretty well in practice because the “try” warnings (about the absence/excess of try) dovetail really well. Without language support we get something like this:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// import pickle</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">let</span> pickle = <span class="" style="color: rgb(79, 129, 135);">Python</span>.<span class="" style="color: rgb(186, 45, 162);">import</span>(<span class="" style="color: rgb(209, 47, 27);">"pickle"</span>)</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// file = open(filename)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">guard</span> <span class="" style="color: rgb(186, 45, 162);">let</span> file = <span class="" style="color: rgb(186, 45, 162);">try</span>? <span class="" style="color: rgb(79, 129, 135);">Python</span>.<span class="" style="color: rgb(79, 129, 135);">open</span>.<span class="" style="color: rgb(79, 129, 135);">throwing</span>.<span class="" style="color: rgb(49, 89, 93);">call</span>(args: filename) <span class="" style="color: rgb(186, 45, 162);">else</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(62, 30, 129); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>fatalError<span class="" style="color: rgb(0, 0, 0);">(</span><span class="" style="color: rgb(209, 47, 27);">"""</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);"> Didn't find data file at <span class="" style="color: rgb(0, 0, 0);">\</span>(<span class="" style="color: rgb(0, 0, 0);">filename</span>)!</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);"> Update the DataPath at the top of the file.\n</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);"> """<span class="" style="color: rgb(0, 0, 0);">)</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// blob = file.read()</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><div class="" style="margin: 0px; line-height: normal;"> <span class="" style="color: rgb(186, 45, 162);">let</span> blob = file.<span class="" style="color: rgb(49, 89, 93);">get</span>(member: <span class="" style="color: rgb(209, 47, 27);">"read"</span>).<span class="" style="color: rgb(49, 89, 93);">call</span>(args: [])</div><div class=""><br class=""></div></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// a, b, c = pickle.loads(blob)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><div class="" style="margin: 0px; line-height: normal;"><span class="" style="color: rgb(186, 45, 162);"> let</span> (a, <font color="#ba2da2" class="">b</font>, <font color="#ba2da2" class="">c</font>) = pickle.<span class="" style="color: rgb(49, 89, 93);">get</span>(member: <span class="" style="color: rgb(209, 47, 27);">"loads"</span>).<span class="" style="color: rgb(49, 89, 93);">call</span>(args: blob).<span class="" style="color: rgb(49, 89, 93);">get3Tuple</span>()</div><div class=""><br class=""></div></div></div><div class=""><br class=""></div><div class="">When we allow sugaring 'x.get(member: “foo”)’ into ‘x.foo’ and allow sugaring x.call(args: a, b, c) into ‘x(a, b,c)’, we’ll get this code:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// import pickle</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">let</span> pickle = <span class="" style="color: rgb(79, 129, 135);">Python</span>.<span class="" style="color: rgb(186, 45, 162);">import</span>(<span class="" style="color: rgb(209, 47, 27);">"pickle"</span>)</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// file = open(filename)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">guard</span> <span class="" style="color: rgb(186, 45, 162);">let</span> file = <span class="" style="color: rgb(186, 45, 162);">try</span>? <span class="" style="color: rgb(79, 129, 135);">Python</span>.<span class="" style="color: rgb(79, 129, 135);">open</span>.<span class="" style="color: rgb(79, 129, 135);">throwing</span>(filename) <span class="" style="color: rgb(186, 45, 162);">else</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(62, 30, 129); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>fatalError<span class="" style="color: rgb(0, 0, 0);">(</span><span class="" style="color: rgb(209, 47, 27);">"""</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);"> Didn't find data file at <span class="" style="color: rgb(0, 0, 0);">\</span>(<span class="" style="color: rgb(0, 0, 0);">filename</span>)!</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);"> Update the DataPath at the top of the file.\n</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);"> """<span class="" style="color: rgb(0, 0, 0);">)</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// blob = file.read()</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">let</span> blob = file.<span class="" style="color: rgb(209, 47, 27);">read()</span></div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// a, b, c = pickle.loads(blob)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><div class="" style="margin: 0px; line-height: normal;"><span class="" style="color: rgb(186, 45, 162);"> let</span> (a, <font color="#ba2da2" class="">b</font>, <font color="#ba2da2" class="">c</font>) = pickle<span class="" style="color: rgb(209, 47, 27);">loads</span><font color="#31595d" class="">(</font>blob).<span class="" style="color: rgb(49, 89, 93);">get3Tuple</span>()</div><div class=""><br class=""></div></div></div><div class=""><br class=""></div><div class="">Which is pretty nice. We can talk about making tuple destructuring extensible later. :-)</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Here’s the prototype that backs the code above - it is hacky, has known suboptimality, and probably isn’t self contained. I’ll clean this up and write up a proposal for the two bits of sugar above when I have time.</div><div class=""><br class=""></div><div class="">-Chris</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);">/// This represents the result of a failable operation when working with</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);">/// python values.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">enum</span> PythonError : <span class="" style="color: rgb(112, 61, 170);">Error</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>/// This represents a null IUO being passed into a PyRef. This should only</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>/// occur when working with C APIs.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">case</span> nullValue</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>/// This represents an exception thrown out of a Python API. This can occur</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>/// on calls.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">case</span> exception(<span class="" style="color: rgb(186, 45, 162);">_</span> : PyRef)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);">}</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);">/// Reference to a Python value. This is always non-null and always owning of</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);">/// the underlying value.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(186, 45, 162);">public</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">final</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">class</span><span class="" style="color: rgb(0, 0, 0);"> PyRef : </span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);"> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">private</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">var</span><span class="" style="color: rgb(0, 0, 0);"> state : </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">></span></div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">var</span><span class="" style="color: rgb(0, 0, 0);"> borrowedPyObject : </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> state</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">var</span><span class="" style="color: rgb(0, 0, 0);"> ownedPyObject : </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> py_INCREF(state)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">public</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">init</span><span class="" style="color: rgb(0, 0, 0);">(ownedThrowing: </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">>!) </span><span class="" style="color: rgb(186, 45, 162);">throws</span><span class="" style="color: rgb(0, 0, 0);"> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">if</span> <span class="" style="color: rgb(186, 45, 162);">let</span> owned = ownedThrowing {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> state = owned</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> } <span class="" style="color: rgb(186, 45, 162);">else</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">throw</span> PythonError.nullValue</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>public<span class="" style="color: rgb(0, 0, 0);"> </span>convenience</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">init</span><span class="" style="color: rgb(0, 0, 0);">(borrowedThrowing: </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">>!) </span><span class="" style="color: rgb(186, 45, 162);">throws</span><span class="" style="color: rgb(0, 0, 0);"> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">try</span> <span class="" style="color: rgb(186, 45, 162);">self</span>.<span class="" style="color: rgb(186, 45, 162);">init</span>(ownedThrowing: borrowedThrowing)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> py_INCREF(state)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>deinit<span class="" style="color: rgb(0, 0, 0);"> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> py_DECREF(state)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">public</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">convenience</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">init</span><span class="" style="color: rgb(0, 0, 0);">(owned: </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">>!) {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">try</span>! <span class="" style="color: rgb(186, 45, 162);">self</span>.<span class="" style="color: rgb(186, 45, 162);">init</span>(ownedThrowing: owned)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">public</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">convenience</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">init</span><span class="" style="color: rgb(0, 0, 0);">(borrowed: </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">>!) {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">try</span>! <span class="" style="color: rgb(186, 45, 162);">self</span>.<span class="" style="color: rgb(186, 45, 162);">init</span>(borrowedThrowing: borrowed)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">public</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">convenience</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">init</span><span class="" style="color: rgb(0, 0, 0);">?(python: </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">>) {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">self</span>.<span class="" style="color: rgb(186, 45, 162);">init</span>(borrowed: python)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> toPythonObject() -> <span class="" style="color: rgb(112, 61, 170);">PyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>return<span class="" style="color: rgb(0, 0, 0);"> </span>self</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>/// Return a version of this value that throws when an error occurs on its</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>/// next use.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">var</span> throwing : <span class="" style="color: rgb(112, 61, 170);">ThrowingPyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> ThrowingPyRef(<span class="" style="color: rgb(186, 45, 162);">self</span>)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> throwingGet(member: <span class="" style="color: rgb(112, 61, 170);">String</span>) <span class="" style="color: rgb(186, 45, 162);">throws</span> -> <span class="" style="color: rgb(112, 61, 170);">PyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">try</span> PyRef(borrowedThrowing: PyObject_GetAttrString(state, member))</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> get(member: <span class="" style="color: rgb(112, 61, 170);">String</span>) -> <span class="" style="color: rgb(112, 61, 170);">PyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">try</span>! throwingGet(member: member)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> throwingGet(dictMember: <span class="" style="color: rgb(112, 61, 170);">PythonObjectionable</span>) <span class="" style="color: rgb(186, 45, 162);">throws</span> -> <span class="" style="color: rgb(112, 61, 170);">PyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">try</span> PyRef(borrowedThrowing:</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> PyDict_GetItem(state, dictMember.toPythonObject().borrowedPyObject))</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> get(dictMember: <span class="" style="color: rgb(112, 61, 170);">PythonObjectionable</span>) -> <span class="" style="color: rgb(112, 61, 170);">PyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">try</span>! throwingGet(dictMember: dictMember)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>/// Swift subscripts cannot throw yet, so model this as returning an optional</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>/// reference.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">subscript</span>(throwing idx : <span class="" style="color: rgb(112, 61, 170);">PythonObjectionable</span>) -> <span class="" style="color: rgb(112, 61, 170);">PyRef</span>? {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">let</span> item = PyObject_GetItem(<span class="" style="color: rgb(186, 45, 162);">self</span>.state,</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> idx.toPythonObject().borrowedPyObject)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">try</span>? PyRef(borrowedThrowing: item)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">public</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">subscript</span><span class="" style="color: rgb(0, 0, 0);">(idx : </span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">) -> </span>PyRef<span class="" style="color: rgb(0, 0, 0);"> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">self</span>[throwing: idx]!</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> throwingGet(tupleItem: <span class="" style="color: rgb(112, 61, 170);">Int</span>) <span class="" style="color: rgb(186, 45, 162);">throws</span> -> <span class="" style="color: rgb(112, 61, 170);">PyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">try</span> PyRef(borrowedThrowing: PyTuple_GetItem(state, tupleItem))</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> get(tupleItem: <span class="" style="color: rgb(112, 61, 170);">Int</span>) -> <span class="" style="color: rgb(112, 61, 170);">PyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">try</span>! throwingGet(tupleItem: tupleItem)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// Helpers for destructuring tuples</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> get2Tuple() -> (<span class="" style="color: rgb(112, 61, 170);">PyRef</span>, <span class="" style="color: rgb(112, 61, 170);">PyRef</span>) {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> (get(tupleItem: <span class="" style="color: rgb(39, 42, 216);">0</span>), get(tupleItem: <span class="" style="color: rgb(39, 42, 216);">1</span>))</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> get3Tuple() -> (<span class="" style="color: rgb(112, 61, 170);">PyRef</span>, <span class="" style="color: rgb(112, 61, 170);">PyRef</span>, <span class="" style="color: rgb(112, 61, 170);">PyRef</span>) {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> (get(tupleItem: <span class="" style="color: rgb(39, 42, 216);">0</span>), get(tupleItem: <span class="" style="color: rgb(39, 42, 216);">1</span>), get(tupleItem: <span class="" style="color: rgb(39, 42, 216);">2</span>))</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>/// Call self, which must be a Python Callable.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>public</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">func</span> throwingCall(args: [<span class="" style="color: rgb(112, 61, 170);">PythonObjectionable</span>],</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> kwargs: [(<span class="" style="color: rgb(112, 61, 170);">PythonObjectionable</span>,<span class="" style="color: rgb(112, 61, 170);">PythonObjectionable</span>)] = [])</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">throws</span> -> <span class="" style="color: rgb(112, 61, 170);">PyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// Make sure state errors are not around.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> assert(PyErr_Occurred() == </span><span class="" style="color: rgb(186, 45, 162);">nil</span><span class="" style="color: rgb(0, 0, 0);">, </span>"Python threw an error but wasn't handled"<span class="" style="color: rgb(0, 0, 0);">)</span></div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">let</span> kwdict = kwargs.isEmpty ? <span class="" style="color: rgb(186, 45, 162);">nil</span> : pyDict(kwargs)</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// Python calls always return a non-null value when successful. If the</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// Python function produces the equivalent of C "void", it returns the None</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// value. A null result of PyObjectCall happens when there is an error,</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// like 'self' not being a Python callable.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">let</span> result = <span class="" style="color: rgb(186, 45, 162);">try</span> PyRef(ownedThrowing:</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> PyObject_Call(state, pyTuple(args), kwdict))</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// Translate a Python exception into a Swift error if one was thrown.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">if</span> <span class="" style="color: rgb(186, 45, 162);">let</span> exception = PyErr_Occurred() {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> PyErr_Clear()</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">throw</span> PythonError.exception(PyRef(borrowed: exception))</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> result</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>/// Call self, which must be a Python Callable.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>public</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">func</span><span class="" style="color: rgb(0, 0, 0);"> call(args: [</span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">],</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> kwargs: [(</span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">,</span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">)] = []) -> </span>PyRef<span class="" style="color: rgb(0, 0, 0);"> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">try</span>! throwingCall(args: args, kwargs: kwargs)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>/// Call self, which must be a Python Callable.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>public</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">func</span><span class="" style="color: rgb(0, 0, 0);"> call(args: </span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">...,</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> kwargs: [(</span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">,</span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">)] = []) -> </span>PyRef<span class="" style="color: rgb(0, 0, 0);"> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">try</span>! throwingCall(args: args, kwargs: kwargs)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// Run the specified closure on the borrowed function guaranteeing the pointer</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// isn't deallocated while the closure runs.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> borrowedMap<T>(<span class="" style="color: rgb(186, 45, 162);">_</span> fn: (<span class="" style="color: rgb(112, 61, 170);">UnsafeMutablePointer</span><<span class="" style="color: rgb(112, 61, 170);">PyObject</span>>)-><span class="" style="color: rgb(112, 61, 170);">T</span>) -> <span class="" style="color: rgb(112, 61, 170);">T</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> withExtendedLifetime(<span class="" style="color: rgb(186, 45, 162);">self</span>) {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> fn(borrowedPyObject)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);">}</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);">/// Reference to a Python value. This always throws when handed a null object</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);">/// or when a call produces a Python exception.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">struct</span> ThrowingPyRef {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">private</span> <span class="" style="color: rgb(186, 45, 162);">var</span> state : <span class="" style="color: rgb(112, 61, 170);">PyRef</span></div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">init</span>(<span class="" style="color: rgb(186, 45, 162);">_</span> value : <span class="" style="color: rgb(112, 61, 170);">PyRef</span>) {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> state = value</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">public</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">init</span><span class="" style="color: rgb(0, 0, 0);">(owned: </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">>!) </span><span class="" style="color: rgb(186, 45, 162);">throws</span><span class="" style="color: rgb(0, 0, 0);"> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> state = <span class="" style="color: rgb(186, 45, 162);">try</span> PyRef(ownedThrowing: owned)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">public</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">init</span><span class="" style="color: rgb(0, 0, 0);">(borrowed: </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">>!) </span><span class="" style="color: rgb(186, 45, 162);">throws</span><span class="" style="color: rgb(0, 0, 0);"> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> state = <span class="" style="color: rgb(186, 45, 162);">try</span> PyRef(borrowedThrowing: borrowed)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> get(member: <span class="" style="color: rgb(112, 61, 170);">String</span>) <span class="" style="color: rgb(186, 45, 162);">throws</span> -> <span class="" style="color: rgb(112, 61, 170);">PyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">try</span> state.throwingGet(member: member)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> get(dictMember: <span class="" style="color: rgb(112, 61, 170);">PythonObjectionable</span>) <span class="" style="color: rgb(186, 45, 162);">throws</span> -> <span class="" style="color: rgb(112, 61, 170);">PyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">try</span> state.throwingGet(dictMember: dictMember)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>/// Swift subscripts cannot throw yet, so model this as returning an optional</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>/// reference.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">public</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">subscript</span><span class="" style="color: rgb(0, 0, 0);">(idx : </span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">) -> </span>PyRef<span class="" style="color: rgb(0, 0, 0);">? {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> state[throwing: idx]</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> get(tupleItem: <span class="" style="color: rgb(112, 61, 170);">Int</span>) <span class="" style="color: rgb(186, 45, 162);">throws</span> -> <span class="" style="color: rgb(112, 61, 170);">PyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">try</span> state.throwingGet(tupleItem: tupleItem)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> get2Tuple() <span class="" style="color: rgb(186, 45, 162);">throws</span> -> (<span class="" style="color: rgb(112, 61, 170);">PyRef</span>, <span class="" style="color: rgb(112, 61, 170);">PyRef</span>) {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">try</span> (get(tupleItem: <span class="" style="color: rgb(39, 42, 216);">0</span>), get(tupleItem: <span class="" style="color: rgb(39, 42, 216);">1</span>))</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> get3Tuple() <span class="" style="color: rgb(186, 45, 162);">throws</span> -> (<span class="" style="color: rgb(112, 61, 170);">PyRef</span>, <span class="" style="color: rgb(112, 61, 170);">PyRef</span>, <span class="" style="color: rgb(112, 61, 170);">PyRef</span>) {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">try</span> (get(tupleItem: <span class="" style="color: rgb(39, 42, 216);">0</span>), get(tupleItem: <span class="" style="color: rgb(39, 42, 216);">1</span>), get(tupleItem: <span class="" style="color: rgb(39, 42, 216);">2</span>))</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>/// Call self, which must be a Python Callable.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">public</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">func</span><span class="" style="color: rgb(0, 0, 0);"> call(args: [</span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">],</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> kwargs: [(</span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">,</span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">)] = [])</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">throws</span> -> <span class="" style="color: rgb(112, 61, 170);">PyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">try</span> state.throwingCall(args: args, kwargs: kwargs)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>/// Call self, which must be a Python Callable.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">public</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">func</span><span class="" style="color: rgb(0, 0, 0);"> call(args: </span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">...,</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> kwargs: [(</span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">,</span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">)] = [])</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">throws</span> -> <span class="" style="color: rgb(112, 61, 170);">PyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> <span class="" style="color: rgb(186, 45, 162);">try</span> state.throwingCall(args: args, kwargs: kwargs)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><span class="" style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 11px;">}</span></div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(186, 45, 162);">extension</span><span class="" style="color: rgb(0, 0, 0);"> </span>ThrowingPyRef<span class="" style="color: rgb(0, 0, 0);"> : </span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);"> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">public</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">init</span><span class="" style="color: rgb(0, 0, 0);">?(python: </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">>) {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">self</span>.<span class="" style="color: rgb(186, 45, 162);">init</span>(PyRef(python: python)!)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> toPythonObject() -> <span class="" style="color: rgb(112, 61, 170);">PyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> state</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);">}</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(186, 45, 162);">let</span> builtinsObject = PyEval_GetBuiltins()!</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);">public<span class="" style="color: rgb(0, 0, 0);"> </span>enum<span class="" style="color: rgb(0, 0, 0);"> Python {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">static</span> <span class="" style="color: rgb(186, 45, 162);">func</span> `import`(<span class="" style="color: rgb(186, 45, 162);">_</span> name: <span class="" style="color: rgb(112, 61, 170);">String</span>) -> <span class="" style="color: rgb(112, 61, 170);">PyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> PyRef(owned: PyImport_ImportModule(name)!)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">static</span> <span class="" style="color: rgb(186, 45, 162);">var</span> builtins : <span class="" style="color: rgb(112, 61, 170);">PyRef</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> PyRef(borrowed: builtinsObject)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// TODO: Make the Python type itself dynamically callable, so that things like</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// "Python.open" naturally resolve to Python.get(member: "open") and all the</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// builtin functions are therefore available naturally and don't have to be</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(0, 0, 0);"> </span>// enumerated here.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">static</span> <span class="" style="color: rgb(186, 45, 162);">var</span> open : <span class="" style="color: rgb(112, 61, 170);">PyRef</span> { <span class="" style="color: rgb(186, 45, 162);">return</span> builtins[<span class="" style="color: rgb(209, 47, 27);">"open"</span>] }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">static</span> <span class="" style="color: rgb(186, 45, 162);">var</span> repr : <span class="" style="color: rgb(112, 61, 170);">PyRef</span> { <span class="" style="color: rgb(186, 45, 162);">return</span> builtins[<span class="" style="color: rgb(209, 47, 27);">"repr"</span>] }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);">}</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);">/// Make “print(pyref)" print a pretty form of the tensor.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(186, 45, 162);">extension</span><span class="" style="color: rgb(0, 0, 0);"> </span>PyRef<span class="" style="color: rgb(0, 0, 0);"> : </span>CustomStringConvertible<span class="" style="color: rgb(0, 0, 0);"> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">var</span> description: <span class="" style="color: rgb(112, 61, 170);">String</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> String(python: <span class="" style="color: rgb(186, 45, 162);">self</span>.call(member: <span class="" style="color: rgb(209, 47, 27);">"__str__"</span>))!</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);">}</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);">// Make PyRef's show up nicely in the Xcode Playground results sidebar.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);"><span class="" style="color: rgb(186, 45, 162);">extension</span><span class="" style="color: rgb(0, 0, 0);"> </span>PyRef<span class="" style="color: rgb(0, 0, 0);"> : </span>CustomPlaygroundQuickLookable<span class="" style="color: rgb(0, 0, 0);"> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">var</span> customPlaygroundQuickLook: <span class="" style="color: rgb(112, 61, 170);">PlaygroundQuickLook</span> {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> <span class="" style="color: rgb(186, 45, 162);">return</span> .text(description)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);">}</div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">//===----------------------------------------------------------------------===//</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">// Helpers working with PyObjects</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">//===----------------------------------------------------------------------===//</div><div class="" style="margin: 0px; line-height: normal; min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">// Create a Python tuple object with the specified elements.</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> pyTuple(<span class="" style="color: rgb(186, 45, 162);">_</span> vals : [<span class="" style="color: rgb(112, 61, 170);">PythonObjectionable</span>])</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span class="" style="color: rgb(0, 0, 0);"> -> </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(186, 45, 162);">let</span> t = PyTuple_New(vals.count)!</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(186, 45, 162);">for</span> (idx, elt) <span class="" style="color: rgb(186, 45, 162);">in</span> vals.enumerated() {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> PyTuple_SetItem(t, idx, elt.toPythonObject().ownedPyObject)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162);"><span class="" style="color: rgb(0, 0, 0);"> </span>return<span class="" style="color: rgb(0, 0, 0);"> t</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div><div class="" style="margin: 0px; line-height: normal; min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> pyTuple(<span class="" style="color: rgb(186, 45, 162);">_</span> vals : <span class="" style="color: rgb(112, 61, 170);">PythonObjectionable</span>...)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span class="" style="color: rgb(0, 0, 0);"> -> </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(186, 45, 162);">return</span> pyTuple(vals)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div><div class="" style="margin: 0px; line-height: normal; min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> pyList(<span class="" style="color: rgb(186, 45, 162);">_</span> vals : <span class="" style="color: rgb(112, 61, 170);">PythonObjectionable</span>...)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span class="" style="color: rgb(0, 0, 0);"> -> </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(186, 45, 162);">return</span> pyList(vals)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(186, 45, 162);">public</span> <span class="" style="color: rgb(186, 45, 162);">func</span> pyList(<span class="" style="color: rgb(186, 45, 162);">_</span> vals : [<span class="" style="color: rgb(112, 61, 170);">PythonObjectionable</span>])</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span class="" style="color: rgb(0, 0, 0);"> -> </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(186, 45, 162);">let</span> list = PyList_New(vals.count)!</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(186, 45, 162);">for</span> (idx, elt) <span class="" style="color: rgb(186, 45, 162);">in</span> vals.enumerated() {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> PyList_SetItem(list, idx, elt.toPythonObject().ownedPyObject)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(186, 45, 162);">return</span> list</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div><div class="" style="margin: 0px; line-height: normal; min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span class="" style="color: rgb(186, 45, 162);">private</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">func</span><span class="" style="color: rgb(0, 0, 0);"> pyDict(</span><span class="" style="color: rgb(186, 45, 162);">_</span><span class="" style="color: rgb(0, 0, 0);"> elts : [(</span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">,</span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">)])</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span class="" style="color: rgb(0, 0, 0);"> -> </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(186, 45, 162);">let</span> dict = PyDict_New()!</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(186, 45, 162);">for</span> (key, val) <span class="" style="color: rgb(186, 45, 162);">in</span> elts {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> PyDict_SetItem(dict, key.toPythonObject().ownedPyObject,</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> val.toPythonObject().ownedPyObject)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(186, 45, 162);">return</span> dict</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div><div class="" style="margin: 0px; line-height: normal; min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span class="" style="color: rgb(186, 45, 162);">public</span><span class="" style="color: rgb(0, 0, 0);"> </span><span class="" style="color: rgb(186, 45, 162);">func</span><span class="" style="color: rgb(0, 0, 0);"> pySlice(</span><span class="" style="color: rgb(186, 45, 162);">_</span><span class="" style="color: rgb(0, 0, 0);"> start: </span>PythonObjectionable<span class="" style="color: rgb(0, 0, 0);">,</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(186, 45, 162);">_</span> end: <span class="" style="color: rgb(112, 61, 170);">PythonObjectionable</span>,</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(186, 45, 162);">_</span> step : <span class="" style="color: rgb(112, 61, 170);">PythonObjectionable</span>? = <span class="" style="color: rgb(186, 45, 162);">nil</span>)</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span class="" style="color: rgb(0, 0, 0);"> -> </span>UnsafeMutablePointer<span class="" style="color: rgb(0, 0, 0);"><</span>PyObject<span class="" style="color: rgb(0, 0, 0);">> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(186, 45, 162);">let</span> stepv = step.flatMap { $0.toPythonObject().ownedPyObject }</div><div class="" style="margin: 0px; line-height: normal; min-height: 14px;"><br class=""></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> <span class="" style="color: rgb(186, 45, 162);">return</span> PySlice_New(start.toPythonObject().ownedPyObject,</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"> end.toPythonObject().ownedPyObject, stepv)!</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div><div class=""><br class=""></div></div></div></div><br class=""></body></html>