Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3a90105

Browse files
committed
minor refactoring
1 parent 1204eb0 commit 3a90105

7 files changed

Lines changed: 32 additions & 19 deletions

File tree

lib/core/common.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ def readInput(message, default=None, checkBatch=True):
788788
message += " "
789789

790790
if checkBatch and conf.batch:
791-
if isinstance(default, (list, tuple, set)):
791+
if isListLike(default):
792792
options = ",".join(getUnicode(opt, UNICODE_ENCODING) for opt in default)
793793
elif default:
794794
options = getUnicode(default, UNICODE_ENCODING)
@@ -1888,7 +1888,7 @@ def getUnicode(value, encoding=None, system=False, noneToNull=False):
18881888
if noneToNull and value is None:
18891889
return NULL
18901890

1891-
if isinstance(value, (list, tuple)):
1891+
if isListLike(value):
18921892
value = list(getUnicode(_, encoding, system, noneToNull) for _ in value)
18931893
return value
18941894

@@ -2425,7 +2425,7 @@ def arrayizeValue(value):
24252425
Makes a list out of value if it is not already a list or tuple itself
24262426
"""
24272427

2428-
if not isinstance(value, (list, tuple)):
2428+
if not isListLike(value):
24292429
value = [value]
24302430

24312431
return value
@@ -2435,7 +2435,7 @@ def unArrayizeValue(value):
24352435
Makes a value out of iterable if it is a list or tuple itself
24362436
"""
24372437

2438-
if isinstance(value, (list, tuple)):
2438+
if isListLike(value):
24392439
value = value[0] if len(value) > 0 else None
24402440

24412441
return value
@@ -2446,12 +2446,19 @@ def flattenValue(value):
24462446
"""
24472447

24482448
for i in iter(value):
2449-
if isinstance(i, (list, tuple)):
2449+
if isListLike(i):
24502450
for j in flattenValue(i):
24512451
yield j
24522452
else:
24532453
yield i
24542454

2455+
def isListLike(value):
2456+
"""
2457+
Returns True if the given value is a list-like instance
2458+
"""
2459+
2460+
return isinstance(value, (list, tuple, set, BigArray))
2461+
24552462
def getSortedInjectionTests():
24562463
"""
24572464
Returns prioritized test list by eventually detected DBMS from error
@@ -2784,7 +2791,7 @@ def isNoneValue(value):
27842791

27852792
if isinstance(value, basestring):
27862793
return value in ("None", "")
2787-
elif isinstance(value, (list, tuple)):
2794+
elif isListLike(value):
27882795
return all(isNoneValue(_) for _ in value)
27892796
elif isinstance(value, dict):
27902797
return not any(value)
@@ -3127,7 +3134,7 @@ def applyFunctionRecursively(value, function):
31273134
Applies function recursively through list-like structures
31283135
"""
31293136

3130-
if isinstance(value, (list, tuple, set, BigArray)):
3137+
if isListLike(value):
31313138
retVal = [applyFunctionRecursively(_, function) for _ in value]
31323139
else:
31333140
retVal = function(value)

lib/core/dump.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from lib.core.common import dataToDumpFile
1818
from lib.core.common import dataToStdout
1919
from lib.core.common import getUnicode
20+
from lib.core.common import isListLike
2021
from lib.core.common import normalizeUnicode
2122
from lib.core.common import openFile
2223
from lib.core.common import prioritySortColumns
@@ -72,7 +73,7 @@ def getOutputFile(self):
7273
return self._outputFile
7374

7475
def string(self, header, data, sort=True):
75-
if isinstance(data, (list, tuple, set)):
76+
if isListLike(data):
7677
self.lister(header, data, sort)
7778
elif data:
7879
data = self._formatString(getUnicode(data))
@@ -102,7 +103,7 @@ def lister(self, header, elements, sort=True):
102103
for element in elements:
103104
if isinstance(element, basestring):
104105
self._write("[*] %s" % element)
105-
elif isinstance(element, (list, tuple, set)):
106+
elif isListLike(element):
106107
self._write("[*] " + ", ".join(getUnicode(e) for e in element))
107108

108109
if elements:
@@ -173,7 +174,7 @@ def dbTables(self, dbTables):
173174

174175
for tables in dbTables.values():
175176
for table in tables:
176-
if isinstance(table, (list, tuple, set)):
177+
if table and isListLike(table):
177178
table = table[0]
178179

179180
maxlength = max(maxlength, len(normalizeUnicode(table) or str(table)))
@@ -193,7 +194,7 @@ def dbTables(self, dbTables):
193194
self._write("+%s+" % lines)
194195

195196
for table in tables:
196-
if isinstance(table, (list, tuple, set)):
197+
if table and isListLike(table):
197198
table = table[0]
198199

199200
blank = " " * (maxlength - len(normalizeUnicode(table) or str(table)))

lib/core/option.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from lib.core.common import getConsoleWidth
3636
from lib.core.common import getFileItems
3737
from lib.core.common import getFileType
38+
from lib.core.common import isListLike
3839
from lib.core.common import normalizePath
3940
from lib.core.common import ntToPosixSlashes
4041
from lib.core.common import openFile
@@ -1630,7 +1631,7 @@ def __saveCmdline():
16301631
optionData.sort()
16311632

16321633
for option, value, datatype in optionData:
1633-
if isinstance(datatype, (list, tuple, set)):
1634+
if datatype and isListLike(datatype):
16341635
datatype = datatype[0]
16351636

16361637
if value is None:

lib/request/direct.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from lib.core.common import getUnicode
1818
from lib.core.common import hashDBRetrieve
1919
from lib.core.common import hashDBWrite
20+
from lib.core.common import isListLike
2021
from lib.core.data import conf
2122
from lib.core.data import kb
2223
from lib.core.data import logger
@@ -61,7 +62,7 @@ def direct(query, content=True):
6162
if not output:
6263
return output
6364
elif content:
64-
if output and isinstance(output, (list, tuple)):
65+
if output and isListLike(output):
6566
if len(output[0]) == 1:
6667
if len(output) > 1:
6768
output = map(lambda _: _[0], output)

lib/techniques/error/use.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from lib.core.common import hashDBWrite
2424
from lib.core.common import incrementCounter
2525
from lib.core.common import initTechnique
26+
from lib.core.common import isListLike
2627
from lib.core.common import isNumPosStrValue
2728
from lib.core.common import listToStrValue
2829
from lib.core.common import readInput
@@ -367,7 +368,7 @@ def errorThread():
367368
if not outputs and not abortedFlag:
368369
outputs = __errorFields(expression, expressionFields, expressionFieldsList)
369370

370-
if outputs and isinstance(outputs, list) and len(outputs) == 1 and isinstance(outputs[0], basestring):
371+
if outputs and isListLike(outputs) and len(outputs) == 1 and isinstance(outputs[0], basestring):
371372
outputs = outputs[0]
372373

373374
duration = calculateDeltaSeconds(start)

plugins/generic/enumeration.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from lib.core.common import getLimitRange
2121
from lib.core.common import getUnicode
2222
from lib.core.common import isInferenceAvailable
23+
from lib.core.common import isListLike
2324
from lib.core.common import isNoneValue
2425
from lib.core.common import isNumPosStrValue
2526
from lib.core.common import isTechniqueAvailable
@@ -879,7 +880,7 @@ def getTables(self, bruteForce=None):
879880
if not isNoneValue(value):
880881
value = filter(None, arrayizeValue(value))
881882

882-
if len(value) > 0 and not isinstance(value[0], (list, tuple)):
883+
if len(value) > 0 and not isListLike(value[0]):
883884
value = map(lambda x: (dbs[0], x), value)
884885

885886
for db, table in filterPairValues(value):
@@ -1620,7 +1621,7 @@ def dumpTable(self, foundData=None):
16201621
entries = []
16211622
elif isinstance(entries, basestring):
16221623
entries = [entries]
1623-
elif not isinstance(entries, (list, tuple)):
1624+
elif not isListLike(entries):
16241625
entries = []
16251626

16261627
entriesCount = len(entries)
@@ -1630,7 +1631,7 @@ def dumpTable(self, foundData=None):
16301631
colLen = len(column)
16311632

16321633
if column not in kb.data.dumpedTable:
1633-
kb.data.dumpedTable[column] = {"length": colLen, "values": []}
1634+
kb.data.dumpedTable[column] = {"length": colLen, "values": BigArray()}
16341635

16351636
for entry in entries:
16361637
if entry is None or len(entry) == 0:

plugins/generic/filesystem.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from lib.core.common import dataToOutFile
1616
from lib.core.common import Backend
1717
from lib.core.common import isNumPosStrValue
18+
from lib.core.common import isListLike
1819
from lib.core.common import isTechniqueAvailable
1920
from lib.core.common import randomStr
2021
from lib.core.common import readInput
@@ -213,11 +214,11 @@ def readFile(self, rFile):
213214
self.cleanup(onlyFileTbl=True)
214215

215216
return
216-
elif isinstance(fileContent, (list, tuple, set)):
217+
elif isListLike(fileContent):
217218
newFileContent = ""
218219

219220
for chunk in fileContent:
220-
if isinstance(chunk, (list, tuple, set)):
221+
if isListLike(chunk):
221222
if len(chunk) > 0:
222223
chunk = chunk[0]
223224
else:

0 commit comments

Comments
 (0)