Skip to content

Allow testing with python3 #1725

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

Merged
merged 15 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
language: python
cache: pip
dist: bionic
python:
- "2.7"
- "2.7_with_system_site_packages"
- "3.7"
addons:
apt:
packages:
Expand All @@ -11,9 +14,9 @@ addons:
- xvfb
install:
- pip install -r requirements.txt
- ln -s src pybitmessage # tests environment
- python setup.py install
- export PYTHONWARNINGS=all
script:
- python checkdeps.py
- xvfb-run src/bitmessagemain.py -t
- python setup.py test
- python -bm tests
2 changes: 1 addition & 1 deletion checkdeps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python
"""
Check dependencies and give recommendations about how to satisfy them

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage
python_prctl
psutil
pycrypto
14 changes: 9 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,14 @@ def run(self):
['desktop/icons/24x24/pybitmessage.png'])
]

if platform.dist()[0] in ('Debian', 'Ubuntu'):
data_files += [
("etc/apparmor.d/",
['packages/apparmor/pybitmessage'])
]
try:
if platform.dist()[0] in ('Debian', 'Ubuntu'):
data_files += [
("etc/apparmor.d/",
['packages/apparmor/pybitmessage'])
]
except AttributeError:
pass # FIXME: use distro for more recent python

dist = setup(
name='pybitmessage',
Expand All @@ -116,6 +119,7 @@ def run(self):
#keywords='',
install_requires=installRequires,
tests_require=requirements,
test_suite='tests.unittest_discover',
extras_require=EXTRAS_REQUIRE,
classifiers=[
"License :: OSI Approved :: MIT License"
Expand Down
25 changes: 13 additions & 12 deletions src/addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"""
# pylint: disable=redefined-outer-name,inconsistent-return-statements
import hashlib
import logging
from binascii import hexlify, unhexlify
from struct import pack, unpack

from debug import logger

logger = logging.getLogger('default')

ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

Expand All @@ -23,8 +25,7 @@ def encodeBase58(num, alphabet=ALPHABET):
arr = []
base = len(alphabet)
while num:
rem = num % base
num = num // base
num, rem = divmod(num, base)
arr.append(alphabet[rem])
arr.reverse()
return ''.join(arr)
Expand Down Expand Up @@ -148,16 +149,16 @@ def encodeAddress(version, stream, ripe):
'Programming error in encodeAddress: The length of'
' a given ripe hash was not 20.'
)
if ripe[:2] == '\x00\x00':
if ripe[:2] == b'\x00\x00':
ripe = ripe[2:]
elif ripe[:1] == '\x00':
elif ripe[:1] == b'\x00':
ripe = ripe[1:]
elif version == 4:
if len(ripe) != 20:
raise Exception(
'Programming error in encodeAddress: The length of'
' a given ripe hash was not 20.')
ripe = ripe.lstrip('\x00')
ripe = ripe.lstrip(b'\x00')

storedBinaryData = encodeVarint(version) + encodeVarint(stream) + ripe

Expand Down Expand Up @@ -191,8 +192,8 @@ def decodeAddress(address):
status = 'invalidcharacters'
return status, 0, 0, ''
# after converting to hex, the string will be prepended
# with a 0x and appended with a L
hexdata = hex(integer)[2:-1]
# with a 0x and appended with a L in python2
hexdata = hex(integer)[2:].rstrip('L')

if len(hexdata) % 2 != 0:
hexdata = '0' + hexdata
Expand Down Expand Up @@ -242,13 +243,13 @@ def decodeAddress(address):
data[bytesUsedByVersionNumber + bytesUsedByStreamNumber:-4]
if len(embeddedRipeData) == 19:
return status, addressVersionNumber, streamNumber, \
'\x00' + embeddedRipeData
b'\x00' + embeddedRipeData
elif len(embeddedRipeData) == 20:
return status, addressVersionNumber, streamNumber, \
embeddedRipeData
elif len(embeddedRipeData) == 18:
return status, addressVersionNumber, streamNumber, \
'\x00\x00' + embeddedRipeData
b'\x00\x00' + embeddedRipeData
elif len(embeddedRipeData) < 18:
return 'ripetooshort', 0, 0, ''
elif len(embeddedRipeData) > 20:
Expand All @@ -257,15 +258,15 @@ def decodeAddress(address):
elif addressVersionNumber == 4:
embeddedRipeData = \
data[bytesUsedByVersionNumber + bytesUsedByStreamNumber:-4]
if embeddedRipeData[0:1] == '\x00':
if embeddedRipeData[0:1] == b'\x00':
# In order to enforce address non-malleability, encoded
# RIPE data must have NULL bytes removed from the front
return 'encodingproblem', 0, 0, ''
elif len(embeddedRipeData) > 20:
return 'ripetoolong', 0, 0, ''
elif len(embeddedRipeData) < 4:
return 'ripetooshort', 0, 0, ''
x00string = '\x00' * (20 - len(embeddedRipeData))
x00string = b'\x00' * (20 - len(embeddedRipeData))
return status, addressVersionNumber, streamNumber, \
x00string + embeddedRipeData

Expand Down
8 changes: 4 additions & 4 deletions src/bitmessagecurses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ def sendMessage(sender="", recv="", broadcast=None, subject="", body="", reply=F
def loadInbox():
"""Load the list of messages"""
sys.stdout = sys.__stdout__
print "Loading inbox messages..."
print("Loading inbox messages...")
sys.stdout = printlog

where = "toaddress || fromaddress || subject || message"
Expand Down Expand Up @@ -1035,7 +1035,7 @@ def loadInbox():
def loadSent():
"""Load the messages that sent"""
sys.stdout = sys.__stdout__
print "Loading sent messages..."
print("Loading sent messages...")
sys.stdout = printlog

where = "toaddress || fromaddress || subject || message"
Expand Down Expand Up @@ -1121,7 +1121,7 @@ def loadSent():
def loadAddrBook():
"""Load address book"""
sys.stdout = sys.__stdout__
print "Loading address book..."
print("Loading address book...")
sys.stdout = printlog

ret = sqlQuery("SELECT label, address FROM addressbook")
Expand Down Expand Up @@ -1228,7 +1228,7 @@ def run(stdscr):
def doShutdown():
"""Shutting the app down"""
sys.stdout = sys.__stdout__
print "Shutting down..."
print("Shutting down...")
sys.stdout = printlog
shutdown.doCleanShutdown()
sys.stdout = sys.__stdout__
Expand Down
17 changes: 7 additions & 10 deletions src/bitmessagemain.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python2.7
#!/usr/bin/env python
"""
The PyBitmessage startup script
"""
Expand All @@ -12,10 +12,11 @@
import os
import sys

app_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(app_dir)
sys.path.insert(0, app_dir)

try:
import pathmagic
except ImportError:
from pybitmessage import pathmagic
app_dir = pathmagic.setup()

import depends
depends.check_dependencies()
Expand Down Expand Up @@ -377,11 +378,7 @@ def start(self):
test_core_result = test_core.run()
self.stop()
test_core.cleanup()
sys.exit(
'Core tests failed!'
if test_core_result.errors or test_core_result.failures
else 0
)
sys.exit(not test_core_result.wasSuccessful())

@staticmethod
def daemonize():
Expand Down
4 changes: 2 additions & 2 deletions src/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,8 @@ def check_dependencies(verbose=False, optional=False):
if sys.hexversion >= 0x3000000:
logger.error(
'PyBitmessage does not support Python 3+. Python 2.7.4'
' or greater is required.')
has_all_dependencies = False
' or greater is required. Python 2.7.18 is recommended.')
sys.exit()

check_functions = [check_ripemd160, check_sqlite, check_openssl]
if optional:
Expand Down
2 changes: 1 addition & 1 deletion src/network/asyncore_pollchoose.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ def log(self, message):
def log_info(self, message, log_type='info'):
"""Conditionally print a message"""
if log_type not in self.ignore_log_types:
print '%s: %s' % (log_type, message)
print('%s: %s' % (log_type, message))

def handle_read_event(self):
"""Handle a read event"""
Expand Down
10 changes: 5 additions & 5 deletions src/network/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ def __init__(self, host, path="/"): # pylint: disable=redefined-outer-name
self.destination = (host, 80)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect(self.destination)
print "connecting in background to %s:%i" % (self.destination[0], self.destination[1])
print("connecting in background to %s:%i" % self.destination)

def state_init(self):
self.append_write_buf(
"GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n" % (
self.path, self.destination[0]))
print "Sending %ib" % (len(self.write_buf))
print("Sending %ib" % len(self.write_buf))
self.set_state("http_request_sent", 0)
return False

def state_http_request_sent(self):
if self.read_buf:
print "Received %ib" % (len(self.read_buf))
print("Received %ib" % len(self.read_buf))
self.read_buf = b""
if not self.connected:
self.set_state("close", 0)
Expand Down Expand Up @@ -62,13 +62,13 @@ def state_socks_handshake_done(self):
for host in ("bootstrap8080.bitmessage.org", "bootstrap8444.bitmessage.org"):
proxy = Socks5Resolver(host=host)
while asyncore.socket_map:
print "loop %s, len %i" % (proxy.state, len(asyncore.socket_map))
print("loop %s, len %i" % (proxy.state, len(asyncore.socket_map)))
asyncore.loop(timeout=1, count=1)
proxy.resolved()

proxy = Socks4aResolver(host=host)
while asyncore.socket_map:
print "loop %s, len %i" % (proxy.state, len(asyncore.socket_map))
print("loop %s, len %i" % (proxy.state, len(asyncore.socket_map)))
asyncore.loop(timeout=1, count=1)
proxy.resolved()

Expand Down
23 changes: 12 additions & 11 deletions src/openclpow.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
#!/usr/bin/env python2.7
"""
Module for Proof of Work using OpenCL
"""
import logging
import os
from struct import pack

import paths
from bmconfigparser import BMConfigParser
from debug import logger
from state import shutdown

libAvailable = True
try:
import numpy
import pyopencl as cl
libAvailable = True
except ImportError:
libAvailable = False


logger = logging.getLogger('default')

ctx = False
queue = False
program = False
Expand All @@ -19,17 +27,10 @@
vendors = []
hash_dt = None

try:
import pyopencl as cl
import numpy
except ImportError:
libAvailable = False


def initCL():
"""Initlialise OpenCL engine"""
# pylint: disable=global-statement
global ctx, queue, program, hash_dt, libAvailable
global ctx, queue, program, hash_dt # pylint: disable=global-statement
if libAvailable is False:
return
del enabledGpus[:]
Expand Down
10 changes: 10 additions & 0 deletions src/pathmagic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os
import sys


def setup():
"""Add path to this file to sys.path"""
app_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(app_dir)
sys.path.insert(0, app_dir)
return app_dir
15 changes: 8 additions & 7 deletions src/pyelliptic/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def inv(a, n):
lm, hm = 1, 0
low, high = a % n, n
while low > 1:
r = high / low
r = high // low
nm, new = hm - lm * r, high - low * r
lm, low, hm, high = nm, new, lm, low
return lm % n
Expand All @@ -43,8 +43,8 @@ def encode(val, base, minlen=0):
code_string = get_code_string(base)
result = ""
while val > 0:
result = code_string[val % base] + result
val /= base
val, i = divmod(val, base)
result = code_string[i] + result
if len(result) < minlen:
result = code_string[0] * (minlen - len(result)) + result
return result
Expand Down Expand Up @@ -101,10 +101,11 @@ def base10_multiply(a, n):
return G
if n == 1:
return a
if (n % 2) == 0:
return base10_double(base10_multiply(a, n / 2))
if (n % 2) == 1:
return base10_add(base10_double(base10_multiply(a, n / 2)), a)
n, m = divmod(n, 2)
if m == 0:
return base10_double(base10_multiply(a, n))
if m == 1:
return base10_add(base10_double(base10_multiply(a, n)), a)
return None


Expand Down
4 changes: 1 addition & 3 deletions src/pyelliptic/cipher.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Symmetric Encryption
"""
# Copyright (C) 2011 Yann GUIBET <[email protected]>
# See LICENSE for details.

from openssl import OpenSSL
from .openssl import OpenSSL


# pylint: disable=redefined-builtin
Expand Down
Loading