/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos

  • Committer: Teddy Hogeborn
  • Date: 2014-08-09 13:12:55 UTC
  • Revision ID: teddy@recompile.se-20140809131255-lp31j98u2pl0xpe6
mandos: Stop using str() and remove unnecessary unicode() calls.

* mandos (if_nametoindex): Use "bytes" literal instead of str().
  (initlogger): Use a unicode string for log device.
  (AvahiError.__unicode__): Removed.
  (DBusPropertyException.__unicode__): - '' -
  (ClientDBus.Secret_dbus_property): Use bytes() instead of str().
  (IPv6_TCPServer.server_bind): Use .encode() instead of str().
  (string_to_delta): Removed unnecessary unicode() call.
  (main): Use "isinstance(x, bytes)" instead of "type(x) is str", use
          .decode() instead of unicode(), and use .encode() instead of
          str().

Show diffs side-by-side

added added

removed removed

Lines of Context:
104
104
        SIOCGIFINDEX = 0x8933  # From /usr/include/linux/sockios.h
105
105
        with contextlib.closing(socket.socket()) as s:
106
106
            ifreq = fcntl.ioctl(s, SIOCGIFINDEX,
107
 
                                struct.pack(str("16s16x"),
108
 
                                            interface))
109
 
        interface_index = struct.unpack(str("I"),
110
 
                                        ifreq[16:20])[0]
 
107
                                struct.pack(b"16s16x", interface))
 
108
        interface_index = struct.unpack("I", ifreq[16:20])[0]
111
109
        return interface_index
112
110
 
113
111
 
118
116
    syslogger = (logging.handlers.SysLogHandler
119
117
                 (facility =
120
118
                  logging.handlers.SysLogHandler.LOG_DAEMON,
121
 
                  address = str("/dev/log")))
 
119
                  address = "/dev/log"))
122
120
    syslogger.setFormatter(logging.Formatter
123
121
                           ('Mandos [%(process)d]: %(levelname)s:'
124
122
                            ' %(message)s'))
224
222
class AvahiError(Exception):
225
223
    def __init__(self, value, *args, **kwargs):
226
224
        self.value = value
227
 
        super(AvahiError, self).__init__(value, *args, **kwargs)
228
 
    def __unicode__(self):
229
 
        return unicode(repr(self.value))
 
225
        return super(AvahiError, self).__init__(value, *args,
 
226
                                                **kwargs)
230
227
 
231
228
class AvahiServiceError(AvahiError):
232
229
    pass
831
828
class DBusPropertyException(dbus.exceptions.DBusException):
832
829
    """A base class for D-Bus property-related exceptions
833
830
    """
834
 
    def __unicode__(self):
835
 
        return unicode(str(self))
836
 
 
 
831
    pass
837
832
 
838
833
class DBusPropertyAccessException(DBusPropertyException):
839
834
    """A property's access permissions disallows an operation.
1615
1610
    @dbus_service_property(_interface, signature="ay",
1616
1611
                           access="write", byte_arrays=True)
1617
1612
    def Secret_dbus_property(self, value):
1618
 
        self.secret = str(value)
 
1613
        self.secret = bytes(value)
1619
1614
    
1620
1615
    del _interface
1621
1616
 
1960
1955
                try:
1961
1956
                    self.socket.setsockopt(socket.SOL_SOCKET,
1962
1957
                                           SO_BINDTODEVICE,
1963
 
                                           str(self.interface + '\0'))
 
1958
                                           (self.interface + "\0")
 
1959
                                           .encode("utf-8"))
1964
1960
                except socket.error as error:
1965
1961
                    if error.errno == errno.EPERM:
1966
1962
                        logger.error("No permission to bind to"
2226
2222
    timevalue = datetime.timedelta(0)
2227
2223
    for s in interval.split():
2228
2224
        try:
2229
 
            suffix = unicode(s[-1])
 
2225
            suffix = s[-1]
2230
2226
            value = int(s[:-1])
2231
2227
            if suffix == "d":
2232
2228
                delta = datetime.timedelta(value)
2383
2379
    del options
2384
2380
    # Force all strings to be unicode
2385
2381
    for option in server_settings.keys():
2386
 
        if type(server_settings[option]) is str:
2387
 
            server_settings[option] = unicode(server_settings[option])
 
2382
        if isinstance(server_settings[option], bytes):
 
2383
            server_settings[option] = (server_settings[option]
 
2384
                                       .decode("utf-8"))
2388
2385
    # Force all boolean options to be boolean
2389
2386
    for option in ("debug", "use_dbus", "use_ipv6", "restore",
2390
2387
                   "foreground", "zeroconf"):
2533
2530
                                       protocol = protocol, bus = bus)
2534
2531
        if server_settings["interface"]:
2535
2532
            service.interface = (if_nametoindex
2536
 
                                 (str(server_settings["interface"])))
 
2533
                                 (server_settings["interface"]
 
2534
                                  .encode("utf-8")))
2537
2535
    
2538
2536
    global multiprocessing_manager
2539
2537
    multiprocessing_manager = multiprocessing.Manager()
2657
2655
            try:
2658
2656
                with pidfile:
2659
2657
                    pid = os.getpid()
2660
 
                    pidfile.write(str(pid) + "\n".encode("utf-8"))
 
2658
                    pidfile.write("{}\n".format(pid).encode("utf-8"))
2661
2659
            except IOError:
2662
2660
                logger.error("Could not write to file %r with PID %d",
2663
2661
                             pidfilename, pid)