ansys / pymapdl

Pythonic interface to MAPDL
https://mapdl.docs.pyansys.com
MIT License
424 stars 120 forks source link

result.nodal_stress() keeps .rst file open #177

Closed matpyks95 closed 4 years ago

matpyks95 commented 4 years ago

The .nodal_stress() command causes the original .rst-file to stay open/active in python. This problem does not occur with other .nodal_x commands. The following example will throw the error:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'Test.rst'

import pyansys as pa
import shutil

rst = pa.read_binary('Test.rst')
nnum, stress = rst.nodal_stress(0)
shutil.move('Test.rst','Test2.rst')

I use shutil just as an example here, any file operation that is considered editing/moving/deleting the Test.rst throws the same error. After the error is thrown, it is still not possible to move or edit the file. This is only possible again after killing the python terminal. Is there some way to fix this?

fyi:

Overall the package is great!

akaszynski commented 4 years ago

Looks like I didn't close down the when using the C interface. Thanks for pointing this out; I'll have it fixed in the next release.

akaszynski commented 4 years ago

Well, this was a fun one. Ended up that you have to manually call the destructor in cython. Interestingly, linux doesn't complain about the file being open. See 118ee44af4304639fe4a27183bab1761ce7cf9cd

For anyone who uses ifstream in cython, it appears to be necessary to call the destructor manually with del file, where file is an instance of ifstream.