Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#11894 closed Bugs (wontfix)

boost::python::file_exec not fully closing file with Python 3.4.3

Reported by: alison.shaw@… Owned by: Ralf W. Grosse-Kunstleve
Milestone: To Be Determined Component: python USE GITHUB
Version: Boost 1.57.0 Severity: Problem
Keywords: Cc:

Description

Using Python 3.4.3 and Boost 1.57.0 on 64-bit Windows 7 with Visual Studio 2013.

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 #8058.

In the following example, when ModifyFile is called before executing the file, it succeeds. After the file has been executed, fopen_s returns 13 (Permission denied) and ModifyFile returns false.

#include "stdafx.h"
#include "boost\python.hpp"
#include <iostream>

#define FILE_NAME         "myTest.py"

void ModifyFile( std::string newContent )
{
  FILE* myFile = nullptr;
  errno_t result = fopen_s( &myFile, FILE_NAME, "wb" );
  if ( result == 0 )
  {
    fwrite( newContent.c_str( ), sizeof( byte ), newContent.length( ), myFile );
    fclose( myFile );
    std::cout << "Success" << std::endl;
    return;
  }

  std::cout << "Failure" << 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;
}

I have tried a similar example using std::fstream to modify the file, and this doesn't seem to have the same problems.

(Apologies for not using Github as per the component description - I didn't find more detailed instructions in the guidelines for reporting bugs.)

Change History (3)

comment:1 by alison.shaw@…, 7 years ago

In the third paragraph, 'returns false' should say 'fails', sorry.

comment:2 by Ralf W. Grosse-Kunstleve, 7 years ago

Resolution: wontfix
Status: newclosed

Could you please report this problem on the Boost.Python github project? (Sorry this trac component should be closed but it's been lingering.)

comment:3 by alison.shaw@…, 7 years ago

https://github.com/boostorg/python/issues/52

For the future, it would be helpful (particularly to n00bs like me) for the main boost bugs page (http://www.boost.org/development/bugs.html) to provide instructions for this process.

Thanks

Note: See TracTickets for help on using tickets.