parameters: (self)
return_statement: return environment
docstring: Retrieves the environment variables with wich the program is running.
  
          @rtype:  list of tuple(compat.unicode, compat.unicode)
          @return: Environment keys and values as found in the process memory.
  
          @raise WindowsError: On error an exception is raised.
docstring_summary: Retrieves the environment variables with wich the program is running.
docstring_tokens: Retrieves the environment variables with wich the program is running .
function: def get_environment_variables(self):
          """
          Retrieves the environment variables with wich the program is running.
  
          @rtype:  list of tuple(compat.unicode, compat.unicode)
          @return: Environment keys and values as found in the process memory.
  
          @raise WindowsError: On error an exception is raised.
          """
  
          # Note: the first bytes are garbage and must be skipped. Then the first
          # two environment entries are the current drive and directory as key
          # and value pairs, followed by the ExitCode variable (it's what batch
          # files know as "errorlevel"). After that, the real environment vars
          # are there in alphabetical order. In theory that's where it stops,
          # but I've always seen one more "variable" tucked at the end which
          # may be another environment block but in ANSI. I haven't examined it
          # yet, I'm just skipping it because if it's parsed as Unicode it just
          # renders garbage.
  
          # Read the environment block contents.
          data = self.peek( *self.get_environment_block() )
  
          # Put them into a Unicode buffer.
          tmp = ctypes.create_string_buffer(data)
          buffer = ctypes.create_unicode_buffer(len(data))
          ctypes.memmove(buffer, tmp, len(data))
          del tmp
  
          # Skip until the first Unicode null char is found.
          pos = 0
          while buffer[pos] != u'\0':
              pos += 1
          pos += 1
  
          # Loop for each environment variable...
          environment = []
          while buffer[pos] != u'\0':
  
              # Until we find a null char...
              env_name_pos = pos
              env_name = u''
              found_name = False
              while buffer[pos] != u'\0':
  
                  # Get the current char.
                  char = buffer[pos]
  
                  # Is it an equal sign?
                  if char == u'=':
  
                      # Skip leading equal signs.
                      if env_name_pos == pos:
                          env_name_pos += 1
                          pos += 1
                          continue
  
                      # Otherwise we found the separator equal sign.
                      pos += 1
                      found_name = True
                      break
  
                  # Add the char to the variable name.
                  env_name += char
  
                  # Next char.
                  pos += 1
  
              # If the name was not parsed properly, stop.
              if not found_name:
                  break
  
              # Read the variable value until we find a null char.
              env_value = u''
              while buffer[pos] != u'\0':
                  env_value += buffer[pos]
                  pos += 1
  
              # Skip the null char.
              pos += 1
  
              # Add to the list of environment variables found.
              environment.append( (env_name, env_value) )
  
          # Remove the last entry, it's garbage.
          if environment:
              environment.pop()
  
          # Return the environment variables.
          return environment
function_tokens: def get_environment_variables ( self ) : # Note: the first bytes are garbage and must be skipped. Then the first # two environment entries are the current drive and directory as key # and value pairs, followed by the ExitCode variable (it's what batch # files know as "errorlevel"). After that, the real environment vars # are there in alphabetical order. In theory that's where it stops, # but I've always seen one more "variable" tucked at the end which # may be another environment block but in ANSI. I haven't examined it # yet, I'm just skipping it because if it's parsed as Unicode it just # renders garbage. # Read the environment block contents. data = self . peek ( * self . get_environment_block ( ) ) # Put them into a Unicode buffer. tmp = ctypes . create_string_buffer ( data ) buffer = ctypes . create_unicode_buffer ( len ( data ) ) ctypes . memmove ( buffer , tmp , len ( data ) ) del tmp # Skip until the first Unicode null char is found. pos = 0 while buffer [ pos ] != u'\0' : pos += 1 pos += 1 # Loop for each environment variable... environment = [ ] while buffer [ pos ] != u'\0' : # Until we find a null char... env_name_pos = pos env_name = u'' found_name = False while buffer [ pos ] != u'\0' : # Get the current char. char = buffer [ pos ] # Is it an equal sign? if char == u'=' : # Skip leading equal signs. if env_name_pos == pos : env_name_pos += 1 pos += 1 continue # Otherwise we found the separator equal sign. pos += 1 found_name = True break # Add the char to the variable name. env_name += char # Next char. pos += 1 # If the name was not parsed properly, stop. if not found_name : break # Read the variable value until we find a null char. env_value = u'' while buffer [ pos ] != u'\0' : env_value += buffer [ pos ] pos += 1 # Skip the null char. pos += 1 # Add to the list of environment variables found. environment . append ( ( env_name , env_value ) ) # Remove the last entry, it's garbage. if environment : environment . pop ( ) # Return the environment variables. return environment