<div dir="ltr">Hi All,<div><br></div><div>I spent a little bit of time looking at python3/libtbx so have some input on this.<br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Oct 10, 2017 at 6:16 PM, Billy Poon <span dir="ltr">&lt;<a href="mailto:bkpoon@lbl.gov" target="_blank">bkpoon@lbl.gov</a>&gt;</span> wrote:<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>1) Use Python 2 to build Python 2 version of CCTBX (no work)</div></div></blockquote><div><br></div><div>This might not be as simple as &quot;No Work&quot; - cctbx is a few years behind on SCons versions (libtbx.scons --version suggests 2.2.0, from 2012) so there *might* be other issues upgrading the SCons version to 3.0, before trying python3.</div><div><br></div><div>I also feel that SCons-Python3 is something of a red herring - the only thing that non-python3-SCons prevents is an 100% python3-only codebase, and unless the plan is to migrate the entire codebase, including all downstream dependencies (like dials) to python3-only in one massive step (probably impossible), everything would need to be dual 2/3 first, and only then a decision taken on deprecating 2.7 support.</div><div><br></div><div>More usefully, outside of a small core of libtbx code, not much of the buildsystem files are bound to the main project so this shouldn&#39;t be too difficult. In fact, I&#39;ve experimented with converting to CMake, and as one of the approaches I explored, I wrote a SCons-emulator that read and parsed the build *without* any scons/cctbx dependencies. To parse the entire &quot;builder=dials&quot; SCons-tree only required this subset of libtbx: <a href="https://github.com/ndevenish/read_scons/blob/master/tbx2cmake/import_env.py#L202-L235">https://github.com/ndevenish/read_scons/blob/master/tbx2cmake/import_env.py#L202-L235</a></div><div><br></div><div>(Note: my general CMake-work works but isn&#39;t complete/ready/documented for general viewing, and still much resembles a hacky project, but I thought that this was sufficient to decouple the buildsystem is usefully illustrative of how simple the task might be)<br></div><div><br></div><div><br></div><div><br></div><div><br></div><div>Regarding general Python3 conversion, it&#39;s definitely not &quot;Just changing the print statements&quot;. I undertook a study in august to convert libtbx (being the core that *everything* depends on) to dual python2/3 and IIRC got most of the tests working in python3. It&#39;s a couple of months out-of-date, but is probably useful as a benchmark of the effort required. The repository links are:</div><div><br></div><div>    <a href="https://github.com/ndevenish/cctbx_project/tree/py3k-modernize">https://github.com/ndevenish/cctbx_project/tree/py3k-modernize</a><br></div><div>    <a href="https://github.com/ndevenish/cctbx_project/tree/py3k">https://github.com/ndevenish/cctbx_project/tree/py3k</a><br></div><div><br></div><div>Probably best looked at with a graphical viewer to get a top-down view of the history. My approach was to separate manual/automatic changes as follows:</div><div><br></div><div>1. Remove legacy code/modules - e.g. old compatibility. The Optik removal came from this. We don&#39;t want to spend mental effort converting absorbed external libraries from a decade ago (see also e.g. pexpect, subprocess_with_fixes)</div><div>2. Make some manual fixes [Expanded as we go on]</div><div>3. Use futurize and modernize to update idioms ONLY e.g. remove pre-2.7 deprecated ways of working. Each operation was done is a separate commit (so that changes are more visible and I thought people would have less objection than to a massive code-change dump), and each commit ran the test suite for libtbx. Some of the &#39;fixers&#39; in each tool are complementary. If there are any problems with tests or automatic conversion, then fix the problem, put the fix into step 2, then start again. This step should be entirely scriptable. I had 17 commits for separate fixes in this chain.</div><div><br></div><div>This is the where the py3k-modernize branch stops, and should in principle be kept entirely safe to push back onto the python2-only repository. The next steps form the `py3k` branch (not being intended for direct pushing, is a little less organised - some of my changes could definitely be moved to step 2):</div><div><br></div><div>4. Run &#39;modernize&#39; to convert the codebase to as much python2/3 as possible. This introduces the dependency on &#39;six&#39;</div><div>5. Run tests, implement various fixes, repeat. This work was ongoing when I stopped working on the study.</div><div><br></div><div>Various (non-exhaustive) problems found:</div><div>- cStringIO isn&#39;t handled automatically, so these need to be fixed manually ( e.g. <a href="https://github.com/ndevenish/cctbx_project/commit/c793eb58acc37c60360dccbbbdd5205504ec3f1a">https://github.com/ndevenish/cctbx_project/commit/c793eb58acc37c60360dccbbbdd5205504ec3f1a</a> )<br></div><div>- Iterators needed to be fixed in cases where they were missed (next vs __next__)</div><div>- Rounding. Python3 uses &#39;Bankers Rounding&#39; and there are formatting tests where this changes the output. I didn&#39;t know enough about the exact desired result to know the best way to fix this</div><div>- libtbx uses compiler.misc.mangle and I don&#39;t know why - this was always a private interface and was removed in 3.</div><div><div>- Moving print statements to functions - there was several failed tests relating to the old python2-print-soft-spacing behaviour, which was removed. Not too difficult, but definitely causes </div></div><div>- A couple of text/binary mode file issues, which seemed to be simple but may be more complicated than the test cases covered. I&#39;d expect more issues with this in the format readers though.</div><div><br></div><div>I evaluated both the futurize (using future library) and modernize (using the well known six library) tools, both being different approaches to 2to3, but for dual 2/3 codebases. I liked the approach of futurize to attempt to make code look as python3-idiomatic as-possible, but some of the performance implications were slightly opaque: e.g. libtbx makes heavy use of cStringIO (presumably for a good reason), and futurize converted all of these back to using StringIO in the Python2 case, so settled on modernize as I felt two different compatibility libraries would be messy. In either case, using the library means that you can identify exactly everywhere that needs to be removed when moving to python3 only.<br></div><div><br></div><div><br></div><div>My conclusions:</div><div>- Automatic tools are useful for the bulk of changes, but there are still lots of edge cases</div><div>- The complexity means that a phased approach is *absolutely* necessary - starting by converting the core to 2/3 and only moving to 3 once everything downstream is converted.Trying to convert everything at once would likely mean months of feature-freeze.</div><div>- A separate &quot;Remove legacy&quot; cleaning phase might be very useful, though obviously the domain of this could be endless</div><div>- SCons is probably the least important of the conversion worries<br></div><div><br></div><div><br></div><div>Nick</div></div></div></div></div>