summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMalfurious <m@lfurio.us>2023-03-11 08:56:26 -0500
committerdusoleil <howcansocksbereal@gmail.com>2023-03-13 19:21:48 -0400
commita412a9a14a73f8041af34f0612c534c80bf9d13d (patch)
tree7e3f0939417b2511da7804274b1aee223d4c0f52
parent1b4e81c5753bb551d9a81e174d851c98d63e0124 (diff)
downloadsploit-a412a9a14a73f8041af34f0612c534c80bf9d13d.tar.gz
sploit-a412a9a14a73f8041af34f0612c534c80bf9d13d.zip
Prefer __repr__ for pretty-printing objects
Define human-readable string formatting for objects in repr, rather than str, as this will enable an interactive interpreter to more conveniently show this data to the user. I believe this especially makes sense in cases where __str__ doesn't perform a semantic type conversion for its class (currently, all affected cases). Scripts can still easily yield this information by using `print(object)`, as print will fallback to repr(object) when there is not an explicitly defined __str__. Furthermore, this patch still maintains backwards compatability (for the time being) of using str(object) to retrieve the information. This is because the default __str__ implementation will defer to __repr__ if provided. This made the Symtbl case of providing both of them especially redundant. Signed-off-by: Malfurious <m@lfurio.us> Signed-off-by: dusoleil <howcansocksbereal@gmail.com>
-rw-r--r--sploit/rev/elf.py5
-rw-r--r--sploit/symtbl.py6
2 files changed, 4 insertions, 7 deletions
diff --git a/sploit/rev/elf.py b/sploit/rev/elf.py
index e099819..a009e97 100644
--- a/sploit/rev/elf.py
+++ b/sploit/rev/elf.py
@@ -8,9 +8,10 @@ class ELF:
self.libs = self.__LIBS__(libs)
self.locals = self.__LOCALS__(self)
- def __str__(self):
+ def __repr__(self):
s = 'ELF: '
s += self.path
+ s += '\n------------'
s += '\nSymbol Table'
s += '\n------------'
s += '\n'
@@ -28,7 +29,7 @@ class ELF:
get = super().__getitem__
if(type(get(lib))==str):self[lib] = ELF(get(lib))
return get(lib)
- def __str__(self):
+ def __repr__(self):
s = ''
for name,lib in self.items():
s += '\n' + str(name) + ' => ' + lib if(type(lib)==str) else str(lib.path)
diff --git a/sploit/symtbl.py b/sploit/symtbl.py
index 05021f7..aa6fc69 100644
--- a/sploit/symtbl.py
+++ b/sploit/symtbl.py
@@ -170,11 +170,7 @@ class SymtblImpl:
return symbol in self.__entries__
def __repr__(self):
- """Return string representation of Symtbl"""
- return str(self)
-
- def __str__(self):
- """Return string representation of Symtbl"""
+ """Return human-readable Symtbl"""
FMT = "\n{:<20} {:<20}"
s = f"{len(self)} symbols @ {hex(self)}"
s += FMT.format("ADDRESS", "SYMBOL")