Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,15 @@ Contributors (0.10.0):
- Vitaly Magerya (VM)
- Oscar Benjamin (OB)
- Ilia Gradina (IG)
- Uri K.H. (UKH)

Changes (0.10.0):

- [gh-435](https://github.com/flintlib/python-flint/issues/435),
Fix a memory leak in `str()` (and so in `repr()`, which uses it unless
`flint.ctx.pretty` is `False`) of `fmpz_mpoly`, `fmpq_mpoly`,
`fmpz_mod_mpoly` and `nmod_mpoly`: the string returned by FLINT was
copied into a Python `bytes` object but never freed. (UKH)
- [gh-433](https://github.com/flintlib/python-flint/pull/433),
Define the input type aliases in the type stubs (such as `iarb` and
`ifmpz`) after the classes they reference, so that the ty type checker
Expand Down
9 changes: 7 additions & 2 deletions src/flint/types/fmpq_mpoly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ from flint.flint_base.flint_base cimport (
)

from flint.utils.typecheck cimport typecheck
from flint.flintlib.functions.flint cimport flint_free
from flint.utils.flint_exceptions import DomainError, IncompatibleContextError

from flint.types.fmpq cimport fmpq, any_as_fmpq
Expand Down Expand Up @@ -760,9 +761,13 @@ cdef class fmpq_mpoly(flint_mpoly):
return f"{self.ctx}.from_dict({self.to_dict()})"

def str(self):
cdef bytes s = <bytes> fmpq_mpoly_get_str_pretty(self.val, self.ctx.c_names, self.ctx.val)
cdef char * cs = fmpq_mpoly_get_str_pretty(self.val, self.ctx.c_names, self.ctx.val)
try:
s = (<bytes> cs).decode()
finally:
flint_free(cs) # FLINT malloc'ed the string; the bytes cast copies it
# res = s.decode().replace("+", " + ").replace("-", " - ")
res = s.decode()
res = s
if res.startswith("- "):
res = "-" + res[3:]
return res
Expand Down
9 changes: 7 additions & 2 deletions src/flint/types/fmpz_mod_mpoly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ from flint.flint_base.flint_base cimport (
)

from flint.utils.typecheck cimport typecheck
from flint.flintlib.functions.flint cimport flint_free
from flint.utils.flint_exceptions import DomainError, IncompatibleContextError

from flint.types.fmpz cimport any_as_fmpz, fmpz
Expand Down Expand Up @@ -746,8 +747,12 @@ cdef class fmpz_mod_mpoly(flint_mpoly):
return f"{self.ctx}.from_dict({self.to_dict()})"

def str(self):
cdef bytes s = <bytes> fmpz_mod_mpoly_get_str_pretty(self.val, self.ctx.c_names, self.ctx.val)
res = s.decode().replace("+", " + ").replace("-", " - ")
cdef char * cs = fmpz_mod_mpoly_get_str_pretty(self.val, self.ctx.c_names, self.ctx.val)
try:
s = (<bytes> cs).decode()
finally:
flint_free(cs) # FLINT malloc'ed the string; the bytes cast copies it
res = s.replace("+", " + ").replace("-", " - ")
if res.startswith(" - "):
res = "-" + res[3:]
return res
Expand Down
9 changes: 7 additions & 2 deletions src/flint/types/fmpz_mpoly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ from flint.flint_base.flint_base cimport (
)

from flint.utils.typecheck cimport typecheck
from flint.flintlib.functions.flint cimport flint_free
from flint.utils.flint_exceptions import DomainError, IncompatibleContextError

from flint.types.fmpz cimport any_as_fmpz, fmpz
Expand Down Expand Up @@ -734,8 +735,12 @@ cdef class fmpz_mpoly(flint_mpoly):
return f"{self.ctx}.from_dict({self.to_dict()})"

def str(self):
cdef bytes s = <bytes> fmpz_mpoly_get_str_pretty(self.val, self.ctx.c_names, self.ctx.val)
res = s.decode().replace("+", " + ").replace("-", " - ")
cdef char * cs = fmpz_mpoly_get_str_pretty(self.val, self.ctx.c_names, self.ctx.val)
try:
s = (<bytes> cs).decode()
finally:
flint_free(cs) # FLINT malloc'ed the string; the bytes cast copies it
res = s.replace("+", " + ").replace("-", " - ")
if res.startswith(" - "):
res = "-" + res[3:]
return res
Expand Down
9 changes: 7 additions & 2 deletions src/flint/types/nmod_mpoly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ from flint.flint_base.flint_base cimport (
)

from flint.utils.typecheck cimport typecheck
from flint.flintlib.functions.flint cimport flint_free
from flint.utils.flint_exceptions import DomainError, IncompatibleContextError

from flint.types.fmpz cimport fmpz
Expand Down Expand Up @@ -720,8 +721,12 @@ cdef class nmod_mpoly(flint_mpoly):
return f"{self.ctx}.from_dict({self.to_dict()})"

def str(self):
cdef bytes s = <bytes> nmod_mpoly_get_str_pretty(self.val, self.ctx.c_names, self.ctx.val)
res = s.decode().replace("+", " + ").replace("-", " - ")
cdef char * cs = nmod_mpoly_get_str_pretty(self.val, self.ctx.c_names, self.ctx.val)
try:
s = (<bytes> cs).decode()
finally:
flint_free(cs) # FLINT malloc'ed the string; the bytes cast copies it
res = s.replace("+", " + ").replace("-", " - ")
if res.startswith(" - "):
res = "-" + res[3:]
return res
Expand Down
Loading