Skip to content

Commit 03d62ae

Browse files
author
jgray-19
committed
Prepare for release:
* Fix bug relating to loadfile * Update documentation a little * Ensure open debug file is closed * Add test on debug files
1 parent 5e1dd42 commit 03d62ae

File tree

5 files changed

+247
-225
lines changed

5 files changed

+247
-225
lines changed

src/pymadng/madp_object.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __init__(
6464
self,
6565
mad_path: str | Path = None,
6666
py_name: str = "py",
67-
debug: int | str | bool = False,
67+
debug: int | str | Path | bool = False,
6868
num_temp_vars: int = 8,
6969
ipython_use_jedi: bool = False,
7070
):
@@ -76,7 +76,7 @@ def __init__(
7676
Args:
7777
mad_path (str): The path to the mad executable, for the default value of None, the one that comes with pymadng package will be used
7878
py_name (str): The name used to interact with the python process from MAD
79-
debug (bool): Sets debug mode on or off
79+
debug (int| str | Path | bool): Sets debug mode on or off. If an int is given, it is expected to be a file descriptor. If a string or Path is given, it is expected to be a file path, and will open this file to write to. If true, it will write to the stdout, and if false no debug information will be written.
8080
num_temp_vars (int): The number of unique temporary variables you intend to use, see :doc:`Managing References <ex-managing-refs>`
8181
ipython_use_jedi (bool): Allow ipython to use jedi in tab completion, will be slower and may result in MAD-NG throwing errors
8282
@@ -222,6 +222,7 @@ def send_tpsa(self, monos: np.ndarray, coefficients: np.ndarray):
222222
AssertionError: The number of monomials and coefficients must be identical.
223223
AssertionError: The monomials must be of type 8-bit unsigned integer
224224
"""
225+
print("Sending TPSA")
225226
self.__process.send_tpsa(monos, coefficients)
226227

227228
def send_cpx_tpsa(self, monos: np.ndarray, coefficients: np.ndarray):
@@ -306,7 +307,8 @@ def loadfile(self, path: str | Path, *vars: str):
306307
else:
307308
# The parent/stem is necessary, otherwise the file will not be found
308309
# This is thanks to the way the require function works in MAD-NG (how it searches for files)
309-
script = f"local __req = require('{path.parent/path.stem}')"
310+
script = f"package.path = '{path.parent}/?.mad;' .. package.path\n"
311+
script += f"local __req = require('{path.stem}')"
310312
for var in vars:
311313
script += f"{var} = __req.{var}\n"
312314
self.__process.send(script)

src/pymadng/madp_pymad.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(
2929
self,
3030
mad_path: str | Path,
3131
py_name: str = "py",
32-
debug: int | str | bool = False,
32+
debug: int | str | Path | bool = False,
3333
) -> None:
3434
self.py_name = py_name
3535

@@ -44,9 +44,10 @@ def __init__(
4444
# Open the pipes for communication to MAD (the stdin of MAD)
4545
self.mad_input_stream = os.fdopen(self.mad_input_pipe, "wb", buffering=0)
4646

47-
if isinstance(debug, str):
48-
debug_file = open(debug, "w")
49-
stdout = debug_file.fileno()
47+
if isinstance(debug, str) or isinstance(debug, Path):
48+
debug = Path(debug)
49+
self.debug_file = open(debug, "w")
50+
stdout = self.debug_file.fileno()
5051
elif isinstance(debug, bool):
5152
stdout = sys.stdout.fileno()
5253
elif isinstance(debug, int):
@@ -213,6 +214,12 @@ def close(self) -> None:
213214
f"Unexpected message received: {close_msg}, MAD-NG may not have completed properly"
214215
)
215216
self.process.terminate() # Terminate the process on the python side
217+
218+
# Close the debug file if it exists
219+
try:
220+
self.debug_file.close()
221+
except AttributeError:
222+
pass
216223

217224
# Close the pipes
218225
if not self.mad_read_stream.closed:

0 commit comments

Comments
 (0)