Skip to content

Consider Python shortcuts #218

Closed
Closed
@dilyanpalauzov

Description

diff --git a/pefile.py b/pefile.py
index 2860822..e4f4732 100644
--- a/pefile.py
+++ b/pefile.py
@@ -1144,7 +1144,7 @@ class SectionStructure(Structure):
     def entropy_H(self, data):
         """Calculate the entropy of a chunk of data."""
 
-        if len(data) == 0:
+        if not data:
             return 0.0
 
         occurences = Counter(bytearray(data))
@@ -1357,9 +1357,7 @@ class RelocationData(DataContainer):
             if name == 'type':
                 word = (val << 12) | (word & 0xfff)
             elif name == 'rva':
-                offset = val-self.base_rva
-                if offset < 0:
-                    offset = 0
+                offset = max(val-self.base_rva, 0)
                 word = ( word & 0xf000) | ( offset & 0xfff)
 
             # Store the modified data
@@ -1432,10 +1430,7 @@ def is_valid_dos_filename(s):
         return False
     # Allow path separators as import names can contain directories.
     allowed = allowed_filename + b'\\/'
-    for c in set(s):
-        if c not in allowed:
-            return False
-    return True
+    return all(c in allowed for c in set(s))
 
 
 # Check if a imported name uses the valid accepted characters expected in mangled
@@ -1452,12 +1447,7 @@ else:
         string.digits + b'_?@$()<>')
 
 def is_valid_function_name(s):
-    if s is None or not isinstance(s, (str, bytes, bytearray)):
-        return False
-    for c in set(s):
-        if c not in allowed_function_name:
-            return False
-    return True
+    return s is not None and isinstance(s, (str, bytes, bytearray))) and all(c in allowed_function_name for c in set(s))
 
 

@@ -1742,8 +1732,7 @@ class PE(object):
         self.__structures__ = []
         self.__from_file = None
 
-        if not fast_load:
-            fast_load = globals()['fast_load']
+        fast_load = fast_load or globals()['fast_load']
         try:
             self.__parse__(name, data, fast_load)
         except:
@@ -1804,8 +1793,7 @@ class PE(object):
                 self.__from_file = True
             except IOError as excp:
                 exception_msg = '{0}'.format(excp)
-                if exception_msg:
-                    exception_msg = ': %s' % exception_msg
+                exception_msg = exception_msg and (': %s' % exception_msg)
                 raise Exception('Unable to access file \'{0}\'{1}'.format(fname, exception_msg))
             finally:
                 if fd is not None:
@@ -2313,7 +2301,7 @@ class PE(object):
                 self.__warnings.append(
                     'Invalid section {0}. Contents are null-bytes.'.format(i))
                 break
-            if len(section_data) == 0:
+            if not section_data:
                 self.__warnings.append(
                     'Invalid section {0}. No data in the file (is this corkami\'s virtsectblXP?).'.format(i))
                 break
@@ -3094,7 +3082,7 @@ class PE(object):
             # Check if this entry contains version information
             #
             if level == 0 and res.Id == RESOURCE_TYPE['RT_VERSION']:
-                if len(dir_entries)>0:
+                if dir_entries:
                     last_entry = dir_entries[-1]
 
                 rt_version_struct = None
@@ -5272,7 +5260,7 @@ class PE(object):
         if not isinstance(data, bytes):
             raise TypeError('data should be of type: bytes')
 
-        if offset >= 0 and offset < len(self.__data__):
+        if 0 <= offset < len(self.__data__):
             self.__data__ = ( self.__data__[:offset] + data + self.__data__[offset+len(data):] )
         else:
             return False

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions