Commit dc417ccc authored by Berk Onat's avatar Berk Onat

Fixed stability issues on trajectory reader and several major bugs that rise segfault.

parent 31dffe5e
......@@ -111,9 +111,7 @@ PyObject * my_close_file_read(PyObject* molpack) {
molfile_plugin_t* plugin = (molfile_plugin_t*) PyMolfileCapsule_AsVoidPtr(plugincapsule);
void *file_handle = (void*) PyMolfileCapsule_AsVoidPtr(filecapsule);
plugin->close_file_read(file_handle);
Py_XDECREF(filecapsule);
Py_XDECREF(plugin_handle->file_handle);
return (PyObject *)plugin_handle;
Py_RETURN_TRUE;
}
%}
......
......@@ -47,13 +47,13 @@ int initNumpyArray(void){
void del_molfile_plugin_list(PyObject* molcapsule)
{
plugin_list = (molfile_plugin_t**) PyMolfileCapsule_AsVoidPtr(molcapsule);
free(plugin_list);
//free(plugin_list);
Py_XDECREF(molcapsule);
}
void del_molfile_file_handle(PyObject* molcapsule)
{
void *file_handle = (void*) PyMolfileCapsule_AsVoidPtr(molcapsule);
free(file_handle);
//free(file_handle);
Py_XDECREF(molcapsule);
}
#else
......@@ -66,13 +66,13 @@ void initNumpyArray(void){
void del_molfile_plugin_list(void* molcapsule)
{
plugin_list = (molfile_plugin_t**) PyMolfileCapsule_AsVoidPtr((PyObject*)molcapsule);
free(plugin_list);
//free(plugin_list);
Py_XDECREF(molcapsule);
}
void del_molfile_file_handle(void* molcapsule)
{
void *file_handle = PyMolfileCapsule_AsVoidPtr((PyObject*)molcapsule);
free(file_handle);
//free(file_handle);
Py_XDECREF(molcapsule);
}
#endif
......@@ -583,20 +583,20 @@ PyObject* read_fill_next_timestep(PyObject* molpack)
//plugin = plugin_handle->plugin;
} else {
PyErr_Format(PyExc_IOError, "molfile plugin is not active.");
return NULL;
Py_RETURN_NONE;
}
if (plugin_handle->file_handle) {
file_handle = (void*) PyMolfileCapsule_AsVoidPtr(plugin_handle->file_handle);
//file_handle = plugin_handle->file_handle;
} else {
PyErr_Format(PyExc_IOError, "no file handle in molfile plugin handle.");
return NULL;
Py_RETURN_NONE;
}
if (plugin_handle->natoms) {
numatoms = plugin_handle->natoms;
} else {
PyErr_Format(PyExc_IOError, "no assigned number of atoms in molfile plugin handle.");
return NULL;
Py_RETURN_NONE;
}
if (plugin->read_next_timestep) {
PyArrayInterface *inter = NULL;
......
......@@ -91,6 +91,56 @@ def get_extension(file_name):
file_extension_with_dot = os.path.splitext(os.path.basename(file_name))[1]
return file_extension_with_dot.split(".")[-1]
def get_fileExtensions(fname):
if fname is None:
fname = ''
if fname != '':
outList=[]
if '.' in fname:
base, ext = os.path.splitext(fname)
if base.startswith('.'):
base = base[1:]
if ext.startswith('.'):
ext = ext[1:]
if '.' in base:
baseList = get_fileExtensions(base)
else:
baseList = [base]
if '.' in ext:
extList = get_fileExtensions(ext)
else:
extList = [ext]
for bitem in baseList:
outList.append(bitem)
for eitem in extList:
outList.append(eitem)
return outList
else:
return None
def get_zipType(tfile):
basename = os.path.basename(tfile)
exts=get_fileExtensions(basename)
if 'gz' in exts[-1]:
ttype = exts[-2]
ztype = 'gz'
zfile = basename.replace('.gz', '')
elif 'zip' in exts[-1]:
ttype = exts[-2]
ztype = 'zip'
zfile = basename.replace('.zip', '')
else:
ttype = exts[-1]
ztype = None
zfile = tfile
return ttype, zfile, ztype
def get_extType(tfile):
basename = os.path.basename(tfile)
exts=get_fileExtensions(basename)
ttype = exts[-1]
return ttype
def get_plugin_with_ext(file_ext):
""" Search molfile plugins list and returns the plugin info
for the first matching extension.
......@@ -206,11 +256,57 @@ class Topology(object):
#if self.pluginhandle is not None:
# libpymolfile.close_file_read(self.pluginhandle)
class OpenMolTraj(object):
def __init__(self, traj_handle):
self.handle = traj_handle
def __enter__(self):
if self.handle.finOpen is False:
global MOLFILE_PLUGINS
global C_MOLFILE_PLUGINS
numlist = libpymolfile.molfile_init()
self.handle.fplugin = libpymolfile.get_plugin(C_MOLFILE_PLUGINS,
self.handle.plugin[0])
self.handle.pluginhandle = None
if self.handle.silent:
with stdout_redirected():
self.handle.pluginhandle = libpymolfile.open_file_read(
self.handle.fplugin, self.handle.fname,
self.handle.ftype, self.handle.natoms)
self.handle.finOpen = True
else:
self.handle.pluginhandle = libpymolfile.open_file_read(
self.handle.fplugin, self.handle.fname,
self.handle.ftype, self.handle.natoms)
self.handle.finOpen = True
if self.handle.pluginhandle is not None:
return self
def __exit__(self, type, value, traceback):
if self.handle.finEOF:
if self.handle.finOpen:
if self.handle.pluginhandle is not None:
rtn = libpymolfile.close_file_read(self.handle.pluginhandle)
if rtn:
self.handle.pluginhandle = None
del self.handle.pluginhandle
def read_next(self):
rtn = libpymolfile.read_fill_next_timestep(self.handle.pluginhandle)
if rtn is not None:
return rtn
else:
self.handle.finEOF = True
return rtn
class Trajectory(object):
def __init__(self, file_name, file_format, plugin, natoms, silent):
global MOLFILE_PLUGINS
global C_MOLFILE_PLUGINS
self.finEOF = False
self.finOpen = False
self.natoms = None
self.atoms = None
self.plugin = None
......@@ -218,49 +314,26 @@ class Trajectory(object):
self.fname = None
self.ftype = None
self.fname = file_name
self.ftype = file_format
if file_format is None:
self.ftype = get_extType(file_name)
else:
self.ftype = file_format
self.plugin = plugin
self.natoms = 0
self.silent = silent
if natoms is not None:
self.natoms = natoms
numlist = libpymolfile.molfile_init()
self.fplugin = libpymolfile.get_plugin(C_MOLFILE_PLUGINS,
self.plugin[0])
self.pluginhandle = None
if self.natoms > 0:
try:
if self.silent:
with stdout_redirected():
self.pluginhandle = libpymolfile.open_file_read(self.fplugin,
self.fname, self.ftype, self.natoms)
else:
self.pluginhandle = libpymolfile.open_file_read(self.fplugin,
self.fname, self.ftype, self.natoms)
except (IOError, OSError, AttributeError):
pass
#if self.pluginhandle is not None:
# libpymolfile.close_file_read(self.pluginhandle)
def read_next(self):
return libpymolfile.read_fill_next_timestep(self.pluginhandle)
def iter_on_traj(self):
try:
empty = False
self.step = 0
empty = False
self.step = 0
with OpenMolTraj(self) as f:
while not empty:
x = self.read_next()
x = f.read_next()
if x is None:
empty = True
self.finEOF = True
else:
self.step += 1
yield x
except (IOError, AttributeError, OSError):
return
def iread(self):
iter_obj = iter(self.iter_on_traj())
......@@ -273,27 +346,6 @@ class Trajectory(object):
finally:
del iter_obj
# def read_frame(self, index_no):
# numlist = libpymolfile.molfile_init()
# tplugin = libpymolfile.get_plugin(C_MOLFILE_PLUGINS,
# self.plugin[1])
# pluginhandle = libpymolfile.open_file_read(tplugin,
# file_name, file_format, natoms)
# libpymolfile.molfile_finish()
# def read(self, index_list, chunk=None):
# numlist = libpymolfile.molfile_init()
# tplugin = libpymolfile.get_plugin(C_MOLFILE_PLUGINS,
# self.plugin[1])
# pluginhandle = libpymolfile.open_file_read(tplugin,
# file_name, file_format, natoms)
# libpymolfile.molfile_finish()
def __del__(self):
pass
#if self.pluginhandle is not None:
# libpymolfile.close_file_read(self.pluginhandle)
def read_topology(file_name, file_format, plugin, silent):
""" Reads structure, bonds, angles, dihedrals, impropers and
additional informations through molfile_plugin if the
......@@ -402,7 +454,6 @@ class OpenMolfile(object):
def __init__(self, *args, **kwargs):
self.initialize_settings()
#super(OpenMolfile, self).__init__(*args, **kwargs)
global MOLFILE_PLUGINS
global C_MOLFILE_PLUGINS
file_name = None
......@@ -463,7 +514,6 @@ class OpenMolfile(object):
self.kwords["file_format"],
self.smolplugin,
self.kwords["silent"])
#libpymolfile.molfile_finish()
if(self.fplugin[1][2] == 0 or
self.topology is None):
......@@ -537,10 +587,6 @@ class OpenMolfile(object):
"' file format of the file " + self.kwords["file_name"])
def initialize_settings(self):
#global MOLFILE_PLUGINS
#global C_MOLFILE_PLUGINS
#self.plugin_list = MOLFILE_PLUGINS
#self.c_plugin_list = C_MOLFILE_PLUGINS
self.trajectory = None
self.topology = None
self.fplugin = None
......@@ -559,15 +605,6 @@ class OpenMolfile(object):
"nowarnings" : False
}
#if not C_MOLFILE_PLUGINS:
# C_MOLFILE_PLUGINS = libpymolfile.molfile_plugin_list(MAX_NUM_PLUGINS)
#if not MOLFILE_PLUGINS:
# MOLFILE_PLUGINS = plugins()
#if(MOLFILE_PLUGINS and self.plugin_list is None):
# self.plugin_list = MOLFILE_PLUGINS
#if(C_MOLFILE_PLUGINS and self.c_plugin_list is None):
# self.c_plugin_list = C_MOLFILE_PLUGINS
def reset_kwords(self):
self.kwords.update({
"file_name" : None,
......@@ -582,7 +619,5 @@ class OpenMolfile(object):
})
def __del__(self):
#del self.topology
#del self.trajectory
libpymolfile.molfile_finish()
......@@ -98,7 +98,7 @@ def check_tcl_version():
val = None
return val
VERSION = "0.0.1"
VERSION = "0.0.4"
CLASSIFIERS = [
"Development Status :: 1 - Alpha",
"Intended Audience :: Science/Research",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment