Boost C++ Libraries: Ticket #11894: boost::python::file_exec not fully closing file with Python 3.4.3 https://svn.boost.org/trac10/ticket/11894 <p> Using Python 3.4.3 and Boost 1.57.0 on 64-bit Windows 7 with Visual Studio 2013. </p> <p> After boost::python::exec_file is called on a file, that file can't be modified by the program using fopen_s, I think because it is still open. Possibly related to <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8058" title="#8058: Bugs: boost::python::exec_file not closing file descriptor with python 2.7 (closed: fixed)">#8058</a>. </p> <p> In the following example, when <a class="missing wiki">ModifyFile</a> is called before executing the file, it succeeds. After the file has been executed, fopen_s returns 13 (Permission denied) and <a class="missing wiki">ModifyFile</a> returns false. </p> <pre class="wiki">#include "stdafx.h" #include "boost\python.hpp" #include &lt;iostream&gt; #define FILE_NAME "myTest.py" void ModifyFile( std::string newContent ) { FILE* myFile = nullptr; errno_t result = fopen_s( &amp;myFile, FILE_NAME, "wb" ); if ( result == 0 ) { fwrite( newContent.c_str( ), sizeof( byte ), newContent.length( ), myFile ); fclose( myFile ); std::cout &lt;&lt; "Success" &lt;&lt; std::endl; return; } std::cout &lt;&lt; "Failure" &lt;&lt; std::endl; } int main( int argc, char** argv ) { Py_Initialize( ); ModifyFile( "print(\"Hello\")" ); boost::python::api::object mainNamespace = boost::python::import( "__main__" ).attr( "__dict__" ); boost::python::exec_file( FILE_NAME, mainNamespace, mainNamespace ); ModifyFile("print(\"Goodbye\")"); Py_Finalize( ); return 0; } </pre><p> I have tried a similar example using std::fstream to modify the file, and this doesn't seem to have the same problems. </p> <p> (Apologies for not using Github as per the component description - I didn't find more detailed instructions in the guidelines for reporting bugs.) </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11894 Trac 1.4.3 alison.shaw@… Thu, 07 Jan 2016 12:06:54 GMT <link>https://svn.boost.org/trac10/ticket/11894#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11894#comment:1</guid> <description> <p> In the third paragraph, 'returns false' should say 'fails', sorry. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ralf W. Grosse-Kunstleve</dc:creator> <pubDate>Thu, 07 Jan 2016 18:28:22 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/11894#comment:2 https://svn.boost.org/trac10/ticket/11894#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">wontfix</span> </li> </ul> <p> Could you please report this problem on the Boost.Python github project? (Sorry this trac component should be closed but it's been lingering.) </p> Ticket alison.shaw@… Fri, 08 Jan 2016 09:48:43 GMT <link>https://svn.boost.org/trac10/ticket/11894#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11894#comment:3</guid> <description> <p> <a class="ext-link" href="https://github.com/boostorg/python/issues/52"><span class="icon">​</span>https://github.com/boostorg/python/issues/52</a> </p> <p> For the future, it would be helpful (particularly to n00bs like me) for the main boost bugs page (<a href="http://www.boost.org/development/bugs.html">http://www.boost.org/development/bugs.html</a>) to provide instructions for this process. </p> <p> Thanks </p> </description> <category>Ticket</category> </item> </channel> </rss>