Skip to content

Commit cd13116

Browse files
committed
refactor(txt.py): fix exception2text import, improve performance and cleanup
Fix missing traceback import in exception2text() so the fallback path works instead of being dead code. Replace O(n²) string concatenation in filter_mltext() with list comprehension. Simplify nested ternary in extract_str(). Remove unused Python 2 type aliases and outdated comments.
1 parent 0024447 commit cd13116

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
* base.py: remove unused `collections` import
2626
* base.py: strip trailing semicolons in `get_perfdata()` output
2727
* db_sqlite.py: reduce unnecessary dictionary object creation
28+
* txt.py: improve `filter_mltext()` performance (avoid O(n²) string concatenation)
29+
* txt.py: improve readability of `extract_str()` fallback logic
30+
* txt.py: remove unused Python 2 type aliases and outdated comments
2831
* human.py: pre-compute mappings as module constants
2932
* powershell.py: `run_ps()` now always returns a dict
3033
* winrm.py: make `run_cmd()` and `run_ps()` JEA-aware
@@ -34,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3437

3538
* base.py: fix `get_table()` using wrong separator for the second data row when called without a header
3639
* powershell.py: fix outdated shebang line
40+
* txt.py: fix `exception2text()` missing `traceback` import (fallback path was dead code)
3741
* txt.py: fix `sanitize_sensitive_data()` replacing the key name instead of the secret value
3842
* winrm.py: pass parameters correctly in `run_cmd()` when using pypsrp
3943

txt.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@
1919

2020
import codecs
2121
import re
22+
import traceback
2223
try:
2324
codecs.lookup_error('surrogateescape')
2425
HAS_SURROGATEESCAPE = True
2526
except LookupError:
2627
HAS_SURROGATEESCAPE = False
2728
import operator
2829

29-
string_types = str
30-
integer_types = int
31-
class_types = type
3230
text_type = str
3331
binary_type = bytes
3432

@@ -196,7 +194,11 @@ def extract_str(s, from_txt, to_txt, include_fromto=False, be_tolerant=True):
196194
if pos2 != -1:
197195
end = pos2 + len(to_txt) if include_fromto else pos2
198196
return s[start:end]
199-
return s[pos1:] if be_tolerant and include_fromto else s[pos1 + len(from_txt):] if be_tolerant else ''
197+
if not be_tolerant:
198+
return ''
199+
if include_fromto:
200+
return s[pos1:]
201+
return s[pos1 + len(from_txt):]
200202

201203

202204
def filter_mltext(_input, ignore):
@@ -238,11 +240,11 @@ def filter_mltext(_input, ignore):
238240
>>> filter_mltext(s, ['ipsum'])
239241
''
240242
"""
241-
filtered_input = ''
242-
for line in _input.splitlines():
243-
if not any(i_line in line for i_line in ignore):
244-
filtered_input += line + '\n'
245-
return filtered_input
243+
lines = [
244+
line for line in _input.splitlines()
245+
if not any(i_line in line for i_line in ignore)
246+
]
247+
return '\n'.join(lines) + '\n' if lines else ''
246248

247249

248250
def match_regex(regex, string, key=''):
@@ -520,8 +522,7 @@ def to_bytes(obj, encoding='utf-8', errors=None, nonstring='simplerepr'):
520522
elif nonstring == 'passthru':
521523
return obj
522524
elif nonstring == 'empty':
523-
# python2.4 doesn't have b''
524-
return to_bytes('')
525+
return b''
525526
elif nonstring == 'strict':
526527
raise TypeError('obj must be a string type')
527528
else:
@@ -640,4 +641,3 @@ def uniq(string):
640641

641642

642643
to_native = to_text
643-
# PY2: to_native = to_bytes

0 commit comments

Comments
 (0)