<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p>Hello,</p>
<p>A few months ago I published Python wrappers for Yosys
(<a class="moz-txt-link-freetext" href="https://github.com/YosysHQ/yosys">https://github.com/YosysHQ/yosys</a>). These can be used in two ways:</p>
<ol>
<li>Use Yosys in Python code by importing it and using it as a
library</li>
<li>Start the Yosys shell (regular command-line interaction) and
load python code by specifying a source file. In that source
file, one can implement passes by inheriting from the Pass class
and these passes are then available in the Yosys shell.</li>
</ol>
<p>This works well and was already merged into the main Yosys repo.</p>
<p>However, there is an issue I cannot wrap my head around. When
Yosys is installed, I copy the shared library .so file together
with a minimal __init__.py to
/usr/lib/python3.5/site-packages/pyosys. I can then use that by
writing</p>
<p>
<blockquote type="cite">from pyosys import libyosys as ys<br>
</blockquote>
This works great for case 1. I also need that import for case 2,
so that users can inherit from the Pass class and use Yosys data
structures. The import seems to work, but as soon as a command is
issued, an exception is thrown. This exception seems not to
concern the interoperability with Python, but it does not appear
when I copy the .so file to the current working directory and
import it by saying<br>
<blockquote type="cite">import libyosys as ys</blockquote>
In that case, everything is well and the whole thing works. The
Pass defined in Python shows up in the Pass-list, can be executed
and successfully interacts with the data structures created by C++
passes.<br>
</p>
<p>The obvious place to look would be the __init__.py file, since
that is the only thing that is executed when importing from the
path but not executed when importing from the local library,
right? But all I do in the init file is:</p>
<p>
<blockquote type="cite">import os<br>
import sys<br>
sys.setdlopenflags(os.RTLD_NOW | os.RTLD_GLOBAL)<br>
<br>
__all__ = ["libyosys"]</blockquote>
And if I remove the first lines it doesn't work either, so they
cannot (solely) be at fault.</p>
<p>Any other reason why the two ways to import would differ?</p>
<p>The exception thrown is a std::out_of_range exception where an
empty vector is accessed at position 171. I introduced some
debug-output and notice that the vector actually contains 179
elements. Is it somehow possible that Python and C++ see a
different version of the vector? It is filled from C++ before
calling any Python code and there is no place in the code where
anything is removed from the vector, so I don't see why it should
shrink in size.<br>
</p>
<p>There is a Github issue about this where you can find a minimal
example, but you need to build yosys and it is kind of big ->
<a class="moz-txt-link-freetext" href="https://github.com/YosysHQ/yosys/issues/1094">https://github.com/YosysHQ/yosys/issues/1094</a></p>
<p>The Python wrappers are defined in kernel/python_wrappers.cc, but
it is auto-generated from a Python-script during the build, so I
attached it for your convenience.</p>
<p>Any thoughts and ideas are highly welcome! I am completely stuck
and don't know where to start debugging.<br>
</p>
<p>BR</p>
<p>Benedikt<br>
</p>
</body>
</html>