Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setLogfile method to handle None path #813

Merged
merged 4 commits into from
Apr 1, 2024
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
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 @@ -259,7 +259,7 @@
if rc == SCIP_OKAY:
pass
elif rc == SCIP_ERROR:
raise Exception('SCIP: unspecified error!')

Check failure on line 262 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / test-coverage (3.11)

SCIP: unspecified error!
elif rc == SCIP_NOMEMORY:
raise MemoryError('SCIP: insufficient memory error!')
elif rc == SCIP_READERROR:
Expand Down Expand Up @@ -4974,8 +4974,11 @@
"""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()

Loading