Skip to content

Commit

Permalink
Fix setLogfile method to handle None path (#813)
Browse files Browse the repository at this point in the history
* Fix setLogfile method to handle None path

* Add new test for model setLogFile and fix setLogFile(None) error

* adjust log file handling in Model class

---------

Co-authored-by: Mohammed Ghannam <[email protected]>
  • Loading branch information
liangbug and mmghannam authored Apr 1, 2024
1 parent 1f858fe commit 30148ab
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
### Added
- Added method for adding piecewise linear constraints
- Add SCIP function SCIPgetTreesizeEstimation and wrapper getTreesizeEstimation
- Add recipes sub-package
- New test for model setLogFile
### Fixed
- Fixed model.setLogFile(None) error
- Add recipes sub-package
- Fixed "weakly-referenced object no longer exists" when calling dropEvent in test_customizedbenders
- Fixed incorrect writing/printing when user had a non-default locale
### Changed
Expand Down
7 changes: 5 additions & 2 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -4974,8 +4974,11 @@ cdef class Model:
"""sets the log file name for the currently installed message handler
:param path: name of log file, or None (no log)
"""
c_path = str_conversion(path) if path else None
SCIPsetMessagehdlrLogfile(self._scip, c_path)
if path:
c_path = str_conversion(path)
SCIPsetMessagehdlrLogfile(self._scip, c_path)
else:
SCIPsetMessagehdlrLogfile(self._scip, NULL)

# Parameter Methods

Expand Down
35 changes: 34 additions & 1 deletion tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import os

from pyscipopt import Model, SCIP_STAGE

Expand Down Expand Up @@ -300,6 +301,39 @@ def test_getTreesizeEstimation():
m.optimize()

assert m.getTreesizeEstimation() > 0

def test_setLogFile():
m = Model()
x = m.addVar("x", vtype="I")
y = m.addVar("y", vtype="I")
m.addCons(x + y == 1)
m.setObjective(2*x+y)

log_file_name = "test_setLogFile.log"
m.setLogfile(log_file_name)
assert os.path.exists(log_file_name)

m.optimize()
del m
assert os.path.getsize(log_file_name) > 0
os.remove(log_file_name)

def test_setLogFile_none():
m = Model()
x = m.addVar("x", vtype="I")
y = m.addVar("y", vtype="I")
m.addCons(x + y == 1)
m.setObjective(2*x+y)

log_file_name = "test_setLogfile_none.log"
m.setLogfile(log_file_name)
assert os.path.exists(log_file_name)

m.setLogfile(None)
m.optimize()
del m
assert os.path.getsize(log_file_name) == 0
os.remove(log_file_name)

def test_locale():
import locale
Expand All @@ -315,4 +349,3 @@ def test_locale():

with open("model.cip") as file:
assert "1,1" not in file.read()

0 comments on commit 30148ab

Please sign in to comment.