FinitePointGroup constructor fixes

Former-commit-id: f5f28af66e333b7378fbd5580dc07a165b43ced5
This commit is contained in:
Marek Nečada 2019-03-02 20:16:14 +00:00
parent 65605c4680
commit 786a4f9906
1 changed files with 7 additions and 3 deletions

View File

@ -1112,11 +1112,13 @@ cdef char *make_c_string(pythonstring):
''' '''
bytestring = pythonstring.encode('UTF-8') bytestring = pythonstring.encode('UTF-8')
cdef Py_ssize_t n = len(bytestring) cdef Py_ssize_t n = len(bytestring)
cdef Py_ssize_t i
cdef char *s cdef char *s
s = <char *>malloc(n+1) s = <char *>malloc(n+1)
if not s: if not s:
raise MemoryError raise MemoryError
s[:] = bytestring #s[:n] = bytestring # This segfaults; why?
for i in range(n): s[i] = bytestring[i]
s[n] = <char>0 s[n] = <char>0
return s return s
@ -1138,7 +1140,8 @@ cdef class FinitePointGroup:
cdef int order = len(permlist) cdef int order = len(permlist)
permindices = {perm: i for i, perm in enumerate(permlist)} # 'invert' permlist permindices = {perm: i for i, perm in enumerate(permlist)} # 'invert' permlist
identity = info.permgroup.identity identity = info.permgroup.identity
self.G = <qpms_finite_group_t *>malloc(sizeof(qpms_finite_group_t)) # We use calloc to avoid calling free to unitialized pointers
self.G = <qpms_finite_group_t *>calloc(1,sizeof(qpms_finite_group_t))
if not self.G: raise MemoryError if not self.G: raise MemoryError
self.G[0].name = make_c_string(info.name) self.G[0].name = make_c_string(info.name)
self.G[0].order = order self.G[0].order = order
@ -1156,7 +1159,7 @@ cdef class FinitePointGroup:
self.G[0].gens = <qpms_gmi_t *>malloc(sizeof(qpms_gmi_t) * self.G[0].ngens) self.G[0].gens = <qpms_gmi_t *>malloc(sizeof(qpms_gmi_t) * self.G[0].ngens)
if not self.G[0].gens: raise MemoryError if not self.G[0].gens: raise MemoryError
for i in range(self.G[0].ngens): for i in range(self.G[0].ngens):
self.G[0].gens[i] = permindices[info.groupgens[i]] self.G[0].gens[i] = permindices[info.permgroupgens[i]]
self.G[0].permrep = <char **>calloc(order, sizeof(char *)) self.G[0].permrep = <char **>calloc(order, sizeof(char *))
if not self.G[0].permrep: raise MemoryError if not self.G[0].permrep: raise MemoryError
for i in range(order): for i in range(order):
@ -1187,6 +1190,7 @@ cdef class FinitePointGroup:
for row in range(dim): for row in range(dim):
for col in range(dim): for col in range(dim):
self.G[0].irreps[iri].m[i*dim*dim + row*dim + col] = irrep[permlist[i]][row,col] self.G[0].irreps[iri].m[i*dim*dim + row*dim + col] = irrep[permlist[i]][row,col]
self.G[0].elemlabels = <char **> 0 # Elem labels not yet implemented
self.owns_data = True self.owns_data = True
def __dealloc__(self): def __dealloc__(self):