Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 2 -> 3 changes. #1745

Closed
wants to merge 21 commits into from
13 changes: 9 additions & 4 deletions src/bitmessagemain.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@
adjustHalfOpenConnectionsLimit, start_proxyconfig)
from inventory import Inventory
# Network objects and threads
from network import (
BMConnectionPool, Dandelion, AddrThread, AnnounceThread, BMNetworkThread,
InvThread, ReceiveQueueThread, DownloadThread, UploadThread
)
from network.addrthread import AddrThread
from network.connectionpool import BMConnectionPool
from network.dandelion import Dandelion
from network.announcethread import AnnounceThread
from network.networkthread import BMNetworkThread
from network.invthread import InvThread
from network.receivequeuethread import ReceiveQueueThread
from network.downloadthread import DownloadThread
from network.uploadthread import UploadThread
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are binding the code to pathmagic harder instead of preparing its removal ):

network is a package, so you can write from .network import .. ant it will work both in python2.7 and python3 in installed and portable mode.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks it wasn't very clear to me what the best way to fix imports for python 3 would be. I'll give .network a try.

from network.knownnodes import readKnownNodes
from singleinstance import singleinstance
# Synchronous threads
Expand Down
46 changes: 23 additions & 23 deletions src/bmconfigparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
BMConfigParser class definition and default configuration settings
"""

import ConfigParser
import configparser
import os
import shutil
from datetime import datetime
Expand Down Expand Up @@ -43,9 +43,9 @@


@Singleton
class BMConfigParser(ConfigParser.SafeConfigParser):
class BMConfigParser(configparser.ConfigParser):
"""
Singleton class inherited from :class:`ConfigParser.SafeConfigParser`
Singleton class inherited from :class:`configparser.ConfigParser`
with additional methods specific to bitmessage config.
"""
# pylint: disable=too-many-ancestors
Expand All @@ -54,28 +54,28 @@ class BMConfigParser(ConfigParser.SafeConfigParser):

def set(self, section, option, value=None):
if self._optcre is self.OPTCRE or value:
if not isinstance(value, basestring):
if not isinstance(value, str):
raise TypeError("option values must be strings")
if not self.validate(section, option, value):
raise ValueError("Invalid value %s" % value)
return ConfigParser.ConfigParser.set(self, section, option, value)
return configparser.ConfigParser.set(self, section, option, value)

def get(self, section, option, raw=False, variables=None):
def get(self, section, option, raw=False, vars=None, fallback=None):
# pylint: disable=arguments-differ
try:
if section == "bitmessagesettings" and option == "timeformat":
return ConfigParser.ConfigParser.get(
self, section, option, raw, variables)
return configparser.ConfigParser.get(
self, section, option, raw = raw, vars = vars)
try:
return self._temp[section][option]
except KeyError:
pass
return ConfigParser.ConfigParser.get(
self, section, option, True, variables)
except ConfigParser.InterpolationError:
return ConfigParser.ConfigParser.get(
self, section, option, True, variables)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:
return configparser.ConfigParser.get(
self, section, option, raw = True, vars = vars)
except configparser.InterpolationError:
return configparser.ConfigParser.get(
self, section, option, raw = True, vars = vars)
except (configparser.NoSectionError, configparser.NoOptionError) as e:
try:
return BMConfigDefaults[section][option]
except (KeyError, ValueError, AttributeError):
Expand All @@ -92,7 +92,7 @@ def safeGetBoolean(self, section, field):
"""Return value as boolean, False on exceptions"""
try:
return self.getboolean(section, field)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError,
except (configparser.NoSectionError, configparser.NoOptionError,
ValueError, AttributeError):
return False

Expand All @@ -101,23 +101,23 @@ def safeGetInt(self, section, field, default=0):
0 if default missing"""
try:
return self.getint(section, field)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError,
except (configparser.NoSectionError, configparser.NoOptionError,
ValueError, AttributeError):
return default

def safeGet(self, section, option, default=None):
"""Return value as is, default on exceptions, None if default missing"""
try:
return self.get(section, option)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError,
except (configparser.NoSectionError, configparser.NoOptionError,
ValueError, AttributeError):
return default

def items(self, section, raw=False, variables=None):
"""Return section variables as parent,
but override the "raw" argument to always True"""
# pylint: disable=arguments-differ
return ConfigParser.ConfigParser.items(self, section, True, variables)
return configparser.ConfigParser.items(self, section, True, variables)

@staticmethod
def addresses():
Expand All @@ -135,21 +135,21 @@ def _reset(self):
def read(self, filenames):
"""Read config and populate defaults"""
self._reset()
ConfigParser.ConfigParser.read(self, filenames)
configparser.ConfigParser.read(self, filenames)
for section in self.sections():
for option in self.options(section):
try:
if not self.validate(
section, option,
ConfigParser.ConfigParser.get(self, section, option)
configparser.ConfigParser.get(self, section, option)
):
try:
newVal = BMConfigDefaults[section][option]
except KeyError:
continue
ConfigParser.ConfigParser.set(
configparser.ConfigParser.set(
self, section, option, newVal)
except ConfigParser.InterpolationError:
except configparser.InterpolationError:
continue

def save(self):
Expand All @@ -168,7 +168,7 @@ def save(self):
# didn't exist before.
fileNameExisted = False
# write the file
with open(fileName, 'wb') as configfile:
with open(fileName, 'w') as configfile:
self.write(configfile)
# delete the backup
if fileNameExisted:
Expand Down
2 changes: 1 addition & 1 deletion src/class_addressGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from addresses import decodeAddress, encodeAddress, encodeVarint
from bmconfigparser import BMConfigParser
from fallback import RIPEMD160Hash
from network import StoppableThread
from network.threads import StoppableThread
from pyelliptic import arithmetic
from pyelliptic.openssl import OpenSSL

Expand Down
4 changes: 3 additions & 1 deletion src/class_singleCleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
from bmconfigparser import BMConfigParser
from helper_sql import sqlExecute, sqlQuery
from inventory import Inventory
from network import BMConnectionPool, knownnodes, StoppableThread
import network.knownnodes
from network.connectionpool import BMConnectionPool
from network.threads import StoppableThread


#: Equals 4 weeks. You could make this longer if you want
Expand Down
3 changes: 2 additions & 1 deletion src/class_singleWorker.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
from bmconfigparser import BMConfigParser
from helper_sql import sqlExecute, sqlQuery
from inventory import Inventory
from network import knownnodes, StoppableThread
import network.knownnodes
from network.threads import StoppableThread


def sizeof_fmt(num, suffix='h/s'):
Expand Down
4 changes: 2 additions & 2 deletions src/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
just import and log.
"""

import ConfigParser
import configparser
import logging
import logging.config
import os
Expand Down Expand Up @@ -74,7 +74,7 @@ def configureLogging():
False,
'Loaded logger configuration from %s' % logging_config
)
except (OSError, ConfigParser.NoSectionError):
except (OSError, configparser.NoSectionError, KeyError):
if os.path.isfile(logging_config):
fail_msg = \
'Failed to load logger configuration from %s, using default' \
Expand Down
13 changes: 3 additions & 10 deletions src/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

# Only really old versions of Python don't have sys.hexversion. We don't
# support them. The logging module was introduced in Python 2.3
if not hasattr(sys, 'hexversion') or sys.hexversion < 0x20300F0:
if not hasattr(sys, 'hexversion'):
sys.exit(
'Python version: %s\n'
'PyBitmessage requires Python 2.7.4 or greater (but not Python 3)'
'PyBitmessage requires Python 2.7.4 or greater'
% sys.version
)

Expand Down Expand Up @@ -301,7 +301,7 @@ def check_openssl():
' OpenSSL 0.9.8b or later with AES, Elliptic Curves (EC),'
' ECDH, and ECDSA enabled.')
return False
matches = cflags_regex.findall(openssl_cflags)
matches = cflags_regex.findall(openssl_cflags.decode())
if matches:
logger.error(
'This OpenSSL library is missing the following required'
Expand Down Expand Up @@ -410,19 +410,12 @@ def check_dependencies(verbose=False, optional=False):

# Python 2.7.4 is the required minimum.
# (https://bitmessage.org/forum/index.php?topic=4081.0)
# Python 3+ is not supported, but it is still useful to provide
# information about our other requirements.
logger.info('Python version: %s', sys.version)
if sys.hexversion < 0x20704F0:
logger.error(
'PyBitmessage requires Python 2.7.4 or greater'
' (but not Python 3+)')
has_all_dependencies = False
if sys.hexversion >= 0x3000000:
logger.error(
'PyBitmessage does not support Python 3+. Python 2.7.4'
' or greater is required. Python 2.7.18 is recommended.')
sys.exit()

check_functions = [check_ripemd160, check_sqlite, check_openssl]
if optional:
Expand Down
6 changes: 3 additions & 3 deletions src/helper_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
or isn't thread-safe.
"""

import Queue
import queue
import threading

sqlSubmitQueue = Queue.Queue()
sqlSubmitQueue = queue.Queue()
"""the queue for SQL"""
sqlReturnQueue = Queue.Queue()
sqlReturnQueue = queue.Queue()
"""the queue for results"""
sql_lock = threading.Lock()
""" lock to prevent queueing a new request until the previous response
Expand Down
16 changes: 7 additions & 9 deletions src/l10n.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@
if time_format != DEFAULT_TIME_FORMAT:
try:
# Check day names
for i in xrange(7):
unicode(time.strftime(time_format, (0, 0, 0, 0, 0, 0, i, 0, 0)), encoding)
for i in range(7):
time.strftime(time_format, (0, 0, 0, 0, 0, 0, i, 0, 0))
# Check month names
for i in xrange(1, 13):
unicode(time.strftime(time_format, (0, i, 0, 0, 0, 0, 0, 0, 0)), encoding)
for i in range(1, 13):
time.strftime(time_format, (0, i, 0, 0, 0, 0, 0, 0, 0))
# Check AM/PM
unicode(time.strftime(time_format, (0, 0, 0, 11, 0, 0, 0, 0, 0)), encoding)
unicode(time.strftime(time_format, (0, 0, 0, 13, 0, 0, 0, 0, 0)), encoding)
time.strftime(time_format, (0, 0, 0, 11, 0, 0, 0, 0, 0))
time.strftime(time_format, (0, 0, 0, 13, 0, 0, 0, 0, 0))
# Check DST
unicode(time.strftime(time_format, (0, 0, 0, 0, 0, 0, 0, 0, 1)), encoding)
time.strftime(time_format, (0, 0, 0, 0, 0, 0, 0, 0, 1))
except:
logger.exception('Could not decode locale formatted timestamp')
time_format = DEFAULT_TIME_FORMAT
Expand Down Expand Up @@ -110,8 +110,6 @@ def formatTimestamp(timestamp=None, as_unicode=True):
except ValueError:
timestring = time.strftime(time_format)

if as_unicode:
return unicode(timestring, encoding)
return timestring


Expand Down
3 changes: 1 addition & 2 deletions src/messagetypes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
from importlib import import_module
from os import listdir, path
from string import lower

import messagetypes
import paths
Expand All @@ -12,7 +11,7 @@
class MsgBase(object): # pylint: disable=too-few-public-methods
"""Base class for message types"""
def __init__(self):
self.data = {"": lower(type(self).__name__)}
self.data = {"": type(self).__name__.lower()}


def constructObject(data):
Expand Down
6 changes: 3 additions & 3 deletions src/multiqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
Elements are added into a random subqueue, and retrieval rotates
"""

import Queue
import queue
from collections import deque

import helper_random


class MultiQueue(Queue.Queue):
class MultiQueue(queue.Queue):
"""A base queue class"""
# pylint: disable=redefined-builtin,attribute-defined-outside-init
defaultQueueCount = 10
Expand All @@ -19,7 +19,7 @@ def __init__(self, maxsize=0, count=0):
self.queueCount = MultiQueue.defaultQueueCount
else:
self.queueCount = count
Queue.Queue.__init__(self, maxsize)
queue.Queue.__init__(self, maxsize)

# Initialize the queue representation
def _init(self, maxsize):
Expand Down
20 changes: 0 additions & 20 deletions src/network/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +0,0 @@
"""
Network subsystem packages
"""
from addrthread import AddrThread
from announcethread import AnnounceThread
from connectionpool import BMConnectionPool
from dandelion import Dandelion
from downloadthread import DownloadThread
from invthread import InvThread
from networkthread import BMNetworkThread
from receivequeuethread import ReceiveQueueThread
from threads import StoppableThread
from uploadthread import UploadThread


__all__ = [
"BMConnectionPool", "Dandelion",
"AddrThread", "AnnounceThread", "BMNetworkThread", "DownloadThread",
"InvThread", "ReceiveQueueThread", "UploadThread", "StoppableThread"
]
6 changes: 3 additions & 3 deletions src/network/addrthread.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""
Announce addresses as they are received from other hosts
"""
import Queue
import queue

import state
from helper_random import randomshuffle
from network.assemble import assemble_addr
from network.connectionpool import BMConnectionPool
from queues import addrQueue
from threads import StoppableThread
from network.threads import StoppableThread


class AddrThread(StoppableThread):
Expand All @@ -22,7 +22,7 @@ def run(self):
try:
data = addrQueue.get(False)
chunk.append(data)
except Queue.Empty:
except queue.Empty:
break

if chunk:
Expand Down
2 changes: 1 addition & 1 deletion src/network/advanceddispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import network.asyncore_pollchoose as asyncore
import state
from threads import BusyError, nonBlocking
from network.threads import BusyError, nonBlocking


class ProcessingError(Exception):
Expand Down
4 changes: 2 additions & 2 deletions src/network/announcethread.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from bmconfigparser import BMConfigParser
from network.assemble import assemble_addr
from network.connectionpool import BMConnectionPool
from node import Peer
from threads import StoppableThread
from network.node import Peer
from network.threads import StoppableThread


class AnnounceThread(StoppableThread):
Expand Down
Loading