Skip to content

Commit 2ef640c

Browse files
authored
Catch warpx not being initialized in library loader (#5567)
Follows up on, #5412, using @ax3l's [suggestion](#5412 (review)) to catch warpx not being initialized when accessing libwarpx.warpx
1 parent c9168a0 commit 2ef640c

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

Python/pywarpx/_libwarpx.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@ def __getattr__(self, attribute):
4040
# Once loaded, it gets added to the dictionary so this code won't be called again.
4141
self.load_library()
4242
return self.__dict__[attribute]
43+
elif attribute == "warpx":
44+
# A `warpx` attribute has not yet been assigned, so `initialize_warpx` has not been called.
45+
raise AttributeError(
46+
"Trying to access libwarpx.warpx before initialize_warpx has been called!"
47+
)
4348
else:
4449
# For any other attribute, call the built-in routine - this should always
45-
# return an AttributeException.
50+
# return an AttributeError.
4651
return self.__getattribute__(attribute)
4752

4853
def _get_package_root(self):
@@ -143,8 +148,6 @@ def initialize(self, argv=None, mpi_comm=None):
143148
self.libwarpx_so.execute_python_callback("afterinit")
144149
self.libwarpx_so.execute_python_callback("particleloader")
145150

146-
# self.libwarpx_so.warpx_init()
147-
148151
self.initialized = True
149152

150153
def finalize(self, finalize_mpi=1):

Python/pywarpx/particle_containers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def particle_container(self):
3535
self.name
3636
)
3737
except AttributeError as e:
38-
msg = "This is likely caused by attempting to access a ParticleContainerWrapper before initialize_warpx has been called"
38+
msg = "You must initialize WarpX before accessing a ParticleContainerWrapper's particle_container."
3939
raise AttributeError(msg) from e
4040

4141
return self._particle_container
@@ -777,7 +777,7 @@ def particle_buffer(self):
777777
try:
778778
self._particle_buffer = libwarpx.warpx.get_particle_boundary_buffer()
779779
except AttributeError as e:
780-
msg = "This is likely caused by attempting to access a ParticleBoundaryBufferWrapper before initialize_warpx has been called"
780+
msg = "You must initialize WarpX before accessing a ParticleBoundaryBufferWrapper's particle_buffer."
781781
raise AttributeError(msg) from e
782782

783783
return self._particle_buffer

0 commit comments

Comments
 (0)