@@ -48,6 +48,7 @@ def __init__(self, shared_library_name, verbose=False):
48
48
# Generate a random prefix for tempfile generation
49
49
self .prefix = str (uuid .uuid4 ())
50
50
self .library_path = self .find_shared_library (shared_library_name )
51
+ self .filenames = []
51
52
52
53
def find_shared_library (self , shared_library_name ):
53
54
## This finds only the relative path to the library, unfortunately.
@@ -57,6 +58,7 @@ def find_shared_library(self, shared_library_name):
57
58
return "/usr/lib/libstrategies.so"
58
59
59
60
def load_dll_copy (self ):
61
+ """Load a new copy of the shared library."""
60
62
# Copy the library file to a new location so we can load the copy.
61
63
temp_directory = tempfile .gettempdir ()
62
64
copy_number = len (self .library_copies )
@@ -70,6 +72,7 @@ def load_dll_copy(self):
70
72
if self .verbose :
71
73
print ("Loading {}" .format (new_filename ))
72
74
shutil .copy2 (self .library_path , new_filename )
75
+ self .filenames .append (new_filename )
73
76
shared_library = cdll .LoadLibrary (new_filename )
74
77
self .library_copies .append (shared_library )
75
78
@@ -91,6 +94,8 @@ def next_player_index(self, name):
91
94
raise ValueError ("We shouldn't be here." )
92
95
93
96
def load_library_for_player (self , name ):
97
+ """For a given player return a copy of the shared library for use
98
+ in a Player class, along with an index for later releasing."""
94
99
index = self .next_player_index (name )
95
100
self .player_indices [name ].add (index )
96
101
if self .verbose :
@@ -104,6 +109,12 @@ def release(self, name, index):
104
109
print ("releasing {}" .format (index ))
105
110
self .player_next [name ].add (index )
106
111
112
+ def __del__ (self ):
113
+ """Cleanup temp files on object deletion."""
114
+ for filename in self .filenames :
115
+ if os .path .exists (filename ):
116
+ os .remove (filename )
117
+
107
118
108
119
class Player (axl .Player ):
109
120
@@ -121,13 +132,14 @@ def __init__(self, original_name,
121
132
game: axelrod.Game
122
133
A instance of an axelrod Game
123
134
"""
135
+ super ().__init__ ()
124
136
if not Player .library_manager :
125
137
Player .library_manager = LibraryManager (shared_library_name )
126
- super ().__init__ ()
127
138
self .index , self .shared_library = \
128
139
self .library_manager .load_library_for_player (original_name )
129
140
self .original_name = original_name
130
141
self .original_function = self .original_name
142
+
131
143
is_stochastic = characteristics [self .original_name ]['stochastic' ]
132
144
if is_stochastic is not None :
133
145
self .classifier ['stochastic' ] = is_stochastic
0 commit comments