From 940f665b54f730ece47fa2c7b85b1f8dc30f62fb Mon Sep 17 00:00:00 2001 From: holger krekel Date: Fri, 7 Sep 2018 18:30:39 +0200 Subject: [PATCH] tox.ini and py35 support, fix linting errors --- python/src/deltachat/__init__.py | 8 +++---- python/src/deltachat/_build.py | 2 +- python/src/deltachat/account.py | 16 +++++++------ python/tests/conftest.py | 4 +--- python/tests/test_lowlevel.py | 7 ++++-- python/tox.ini | 39 ++++++++++++++++++++++++++++++++ 6 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 python/tox.ini diff --git a/python/src/deltachat/__init__.py b/python/src/deltachat/__init__.py index 1e4bbaac..26cc54eb 100644 --- a/python/src/deltachat/__init__.py +++ b/python/src/deltachat/__init__.py @@ -1,6 +1,6 @@ from deltachat import capi from deltachat.capi import ffi -from deltachat.account import Account +from deltachat.account import Account # noqa _DC_CALLBACK_MAP = {} @@ -18,9 +18,9 @@ def py_dc_callback(ctx, evt, data1, data2): # function which provides us signature info of an event call event_sig_types = capi.lib.dc_get_event_signature_types(evt) if data1 and event_sig_types & 1: - data1 = ffi.string(ffi.cast('char*', data1)) + data1 = ffi.string(ffi.cast('char*', data1)).decode("utf8") if data2 and event_sig_types & 2: - data2 = ffi.string(ffi.cast('char*', data2)) + data2 = ffi.string(ffi.cast('char*', data2)).decode("utf8") evt_name = get_dc_event_name(evt) try: ret = callback(ctx, evt_name, data1, data2) @@ -28,7 +28,7 @@ def py_dc_callback(ctx, evt, data1, data2): return ffi.cast('uintptr_t', ret) elif event_sig_types & 8: return ffi.cast('int', ret) - except: + except: # noqa raise ret = 0 return ret diff --git a/python/src/deltachat/_build.py b/python/src/deltachat/_build.py index f7c8a7e3..90993c9e 100644 --- a/python/src/deltachat/_build.py +++ b/python/src/deltachat/_build.py @@ -17,7 +17,7 @@ def read_event_defines(): if len(parts) >= 3: if parts[1].startswith("DC_EVENT"): try: - val = int(parts[2]) + int(parts[2]) except ValueError: continue yield line diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index db90eb56..4d74f513 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -3,10 +3,10 @@ import threading import requests from . import capi import deltachat -from .capi import ffi -def eventprinter((evt_name, data1, data2)): +def eventprinter(evt): + evt_name, data1, data2 = evt t = threading.currentThread() tname = getattr(t, "name", t) print("[" + tname + "]", evt_name, data1, data2) @@ -25,11 +25,10 @@ class EventHandler: return r.content def dc_event_http_get(self, data1, data2): - url = data1.decode("utf-8") - content = self.read_url(url) - s = content.encode("utf-8") + url = data1 + content = self.read_url(url) # we need to return a fresh pointer that the core owns - return capi.lib.dupstring_helper(s) + return capi.lib.dupstring_helper(content) def dc_event_is_offline(self, data1, data2): return 0 # always online @@ -40,6 +39,8 @@ class Account: self.dc_context = ctx = capi.lib.dc_context_new( capi.lib.py_dc_callback, capi.ffi.NULL, capi.ffi.NULL) + if hasattr(db_path, "encode"): + db_path = db_path.encode("utf8") capi.lib.dc_open(ctx, db_path, capi.ffi.NULL) self._logcallback = logcallback or (lambda *args: None) if eventhandler is None: @@ -48,6 +49,8 @@ class Account: def set_config(self, **kwargs): for name, value in kwargs.items(): + name = name.encode("utf8") + value = value.encode("utf8") capi.lib.dc_set_config(self.dc_context, name, value) def start(self): @@ -75,7 +78,6 @@ class Account: return 0 - class IOThreads: def __init__(self, dc_context): self.dc_context = dc_context diff --git a/python/tests/conftest.py b/python/tests/conftest.py index 06a7ce33..9b4929a4 100644 --- a/python/tests/conftest.py +++ b/python/tests/conftest.py @@ -1,11 +1,9 @@ import pytest -import deltachat - def pytest_addoption(parser): parser.addoption("--user", action="store", default=None, - help="user and domain of test account: example user@example.org") + help="user and domain of test account: example user@example.org") parser.addoption("--password", action="store", default=None) diff --git a/python/tests/test_lowlevel.py b/python/tests/test_lowlevel.py index 10f4d29e..6875ccc9 100644 --- a/python/tests/test_lowlevel.py +++ b/python/tests/test_lowlevel.py @@ -2,8 +2,11 @@ from __future__ import print_function import deltachat import re from deltachat import capi -from deltachat.capi import ffi -from queue import Queue +try: + from queue import Queue +except ImportError: + from Queue import Queue + def test_empty_context(): diff --git a/python/tox.ini b/python/tox.ini new file mode 100644 index 00000000..15a4cec9 --- /dev/null +++ b/python/tox.ini @@ -0,0 +1,39 @@ +[tox] +minversion = 2.0 +distshare = {homedir}/.tox/distshare +# make sure to update environment list in travis.yml and appveyor.yml +envlist = + linting + py27 + py35 + +[testenv] +commands = pytest {posargs:tests} +usedevelop = True +deps = pytest + +[testenv:linting] +skipsdist = True +usedevelop = True +basepython = python2.7 +deps = + flake8 + # pygments required by rst-lint + pygments + restructuredtext_lint +commands = + flake8 src/deltachat + flake8 tests/ + # {envpython} scripts/check-rst.py + +[pytest] +minversion = 2.0 +#--pyargs --doctest-modules --ignore=.tox +python_files = tests/test_*.py +norecursedirs = .tox ja .hg cx_freeze_source +xfail_strict=true +filterwarnings = + # produced by path.local + +[flake8] +max-line-length = 120