From 2b0de34a57aaa87e84febfa44d1e000f927a5c5b Mon Sep 17 00:00:00 2001 From: kdysput Date: Mon, 9 Sep 2024 14:50:41 +0200 Subject: [PATCH 1/8] Windows compatibility patch --- .github/workflows/test.yml | 25 ++++++++++++++++++++++++- backtracepython/child.py | 10 ++++++---- example/__init__.py | 32 -------------------------------- example/example.py | 34 ++++++++++++++++++++++++++++++++++ pytest.ini | 1 + requirements.txt | 2 ++ setup.py | 3 +-- tests/test_basic_flow.py | 11 +++-------- tox.ini | 2 +- 9 files changed, 72 insertions(+), 48 deletions(-) create mode 100644 example/example.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b800d86..428010c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -26,11 +26,34 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install tox + pip install -r requirements.txt - name: Run Tox run: tox + # On windows we want to test only latest python version. + # Everything else will be tested against Linux + test_on_windows: + runs-on: windows-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: 3.11 + update-environment: false + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Run tests + run: pytest + test_python_2: runs-on: ubuntu-latest diff --git a/backtracepython/child.py b/backtracepython/child.py index 99ba8d9..c377660 100644 --- a/backtracepython/child.py +++ b/backtracepython/child.py @@ -18,13 +18,15 @@ class globs: context_line_count = None def eprint(*args, **kwargs): - print(*args, file=sys.stderr, **kwargs) + print(*args, flush = True, **kwargs) def post_json(full_url, obj): + payload = json.dumps(obj, ignore_nan=True, bigint_as_string = True) if globs.debug_backtrace: - eprint(full_url) - eprint(json.dumps(obj, indent=2, ignore_nan=True)) - payload = json.dumps(obj, ignore_nan=True).encode('utf-8') + data = "Submitting a payload to {},\n {}\n".format(full_url, payload) + eprint(data) + + payload = payload.encode('utf-8') headers = { 'Content-Type': "application/json", 'Content-Length': len(payload), diff --git a/example/__init__.py b/example/__init__.py index 0a26ece..e69de29 100644 --- a/example/__init__.py +++ b/example/__init__.py @@ -1,32 +0,0 @@ -import os - -import backtracepython as backtrace - - -def open_file(name): - open(name).read() - -def main(): - backtrace.initialize( - endpoint= os.getenv('BACKTRACE_SUBMISSION_URL', '"https://submit.backtrace.io/your-universe/token/json"'), - attributes = { - 'application': 'example-app', - 'application.version': '1.0.0', - 'version': '1.0.0' - } - ) - - # send an exception from the try/catch block - try: - open_file("not existing file") - except: - backtrace.send_last_exception() - - # send a message - backtrace.send_report("test message") - - # generate and send unhandled exception - open_file("not existing file") - -if __name__ == "__main__": - main() diff --git a/example/example.py b/example/example.py new file mode 100644 index 0000000..8362f3e --- /dev/null +++ b/example/example.py @@ -0,0 +1,34 @@ +import os + +import backtracepython as backtrace + + +def open_file(name): + open(name).read() + +def main(): + backtrace.initialize( + endpoint= os.getenv('BACKTRACE_SUBMISSION_URL', '"https://submit.backtrace.io/your-universe/token/json"'), + debug_backtrace=True, + attributes = { + 'application': 'example-app', + 'application.version': '1.0.0', + 'version': '1.0.0' + } + + ) + + # send an exception from the try/catch block + try: + open_file("not existing file") + except: + backtrace.send_last_exception() + + # send a message + backtrace.send_report("test message") + + # generate and send unhandled exception + open_file("not existing file") + +if __name__ == "__main__": + main() diff --git a/pytest.ini b/pytest.ini index a635c5c..b51ac33 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,2 +1,3 @@ [pytest] pythonpath = . +faulthandler_timeout = 5 diff --git a/requirements.txt b/requirements.txt index 3d43198..ccf2090 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ six simplejson==3.19.3 +pytest +tox diff --git a/setup.py b/setup.py index 20f87f3..a443601 100644 --- a/setup.py +++ b/setup.py @@ -2,11 +2,10 @@ from setuptools import find_packages, setup -import backtracepython setup( name='backtracepython', - version=backtracepython.version_string, + version='0.3.3', description='Backtrace.io error reporting tool for Python', author='Backtrace.io', author_email='team@backtrace.io', diff --git a/tests/test_basic_flow.py b/tests/test_basic_flow.py index 540f751..81d2eff 100644 --- a/tests/test_basic_flow.py +++ b/tests/test_basic_flow.py @@ -24,7 +24,7 @@ def check_basic_report(obj): assert obj['attributes']['error.message'] == "name 'b' is not defined" source_code_id = obj['threads'][obj['mainThread']]['stack'][0]['sourceCode'] - assert obj['sourceCode'][source_code_id]['path'].endswith("tests/exe/simple_report.py") + assert obj['sourceCode'][source_code_id]['path'].endswith(os.path.join(tests_dir, "exe", "simple_report.py")) assert obj['sourceCode'][source_code_id]['text'].endswith("\na = b\n") assert obj['attributes']['a'] == 1 @@ -40,7 +40,7 @@ def check_multi_file(obj): fault_stack = obj['threads'][obj['mainThread']]['stack'] source_code_id = fault_stack[-1]['sourceCode'] - assert obj['sourceCode'][source_code_id]['path'].endswith("tests/exe/multi_file.py") + assert obj['sourceCode'][source_code_id]['path'].endswith(os.path.join(tests_dir, "exe", "multi_file.py")) lines = obj['sourceCode'][source_code_id]['text'].split("\n") assert lines[fault_stack[-1]['line'] - 1] == 'call_a_file(True)' @@ -79,16 +79,11 @@ def log_message(self, format, *args): host, port = httpd.server_address exe_path = os.path.join(exe_dir, exe_name) - stdio_action = None if debug_backtrace else subprocess.PIPE - child = subprocess.Popen([sys.executable, exe_path, host, str(port)], - stdout=stdio_action, stderr=stdio_action) + child = subprocess.Popen([sys.executable, exe_path, host, str(port)]) httpd.handle_request() check_fn(non_local.json_object) child.wait() - if stdio_action is not None: - child.stdout.close() - child.stderr.close() httpd.server_close() diff --git a/tox.ini b/tox.ini index 4dd7c9f..846eb49 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py27, py37, py38, py39, py310 +envlist = py27, py37, py38, py39, py311 skipsdist = True [testenv] From bcabdcecbe6b5f233c082405ca6987f1945b2e97 Mon Sep 17 00:00:00 2001 From: kdysput Date: Mon, 9 Sep 2024 15:05:15 +0200 Subject: [PATCH 2/8] Bring back tox 310 --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 846eb49..4dd7c9f 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py27, py37, py38, py39, py311 +envlist = py27, py37, py38, py39, py310 skipsdist = True [testenv] From e9bbfb0c6faa78e5de00ee332d9bdd600b2246b3 Mon Sep 17 00:00:00 2001 From: kdysput Date: Mon, 9 Sep 2024 15:08:41 +0200 Subject: [PATCH 3/8] Ignore black on docker container --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 879f6bd..8864e4d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,8 @@ WORKDIR /sdk COPY ./requirements.txt /sdk RUN pip install --upgrade pip \ - && pip install pytest -r requirements.txt + && pip install $(grep -ivE "black" requirements.txt) +# black is not available in python2.7 container COPY . /sdk ENV PYTHONPATH=/sdk From 27a104a6ffb1fde8eab6e09851c677bdf7a54dd8 Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Mon, 9 Sep 2024 18:35:20 +0200 Subject: [PATCH 4/8] Do not use Flush --- backtracepython/child.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracepython/child.py b/backtracepython/child.py index 8e81be7..9f096b4 100644 --- a/backtracepython/child.py +++ b/backtracepython/child.py @@ -20,7 +20,7 @@ class globs: def eprint(*args, **kwargs): - print(*args, flush=True, **kwargs) + print(*args, **kwargs) def post_json(full_url, obj): From 6fcc8af7178f491bc8f9a9a15f633f7750330124 Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Mon, 9 Sep 2024 18:38:46 +0200 Subject: [PATCH 5/8] Pass output to stderr --- backtracepython/child.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracepython/child.py b/backtracepython/child.py index 9f096b4..62e067d 100644 --- a/backtracepython/child.py +++ b/backtracepython/child.py @@ -20,7 +20,7 @@ class globs: def eprint(*args, **kwargs): - print(*args, **kwargs) + print(*args, file=sys.stderr, **kwargs) def post_json(full_url, obj): From 4f3b511dd4e4dae5786059e40a42d7b900a28d4b Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Tue, 10 Sep 2024 13:15:34 +0200 Subject: [PATCH 6/8] EOL requirements --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index d85a085..9c9dee6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,4 @@ six simplejson==3.19.3 pytest tox -black \ No newline at end of file +black From 51f386a9ebd91528313c3a71536710070bc94696 Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Tue, 10 Sep 2024 13:33:05 +0200 Subject: [PATCH 7/8] Conditional requirements --- Dockerfile | 3 +-- requirements.txt | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8864e4d..6140ece 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,8 +4,7 @@ WORKDIR /sdk COPY ./requirements.txt /sdk RUN pip install --upgrade pip \ - && pip install $(grep -ivE "black" requirements.txt) -# black is not available in python2.7 container + && pip install -r requirements.txt COPY . /sdk ENV PYTHONPATH=/sdk diff --git a/requirements.txt b/requirements.txt index 9c9dee6..afdc7fd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,4 @@ six simplejson==3.19.3 pytest tox -black +black; python_version < '3.0' From 4f9ce7c7e1134bc72735089e7510f2f3c5484e2d Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Tue, 10 Sep 2024 13:37:50 +0200 Subject: [PATCH 8/8] Use > --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index afdc7fd..6f4311d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,4 @@ six simplejson==3.19.3 pytest tox -black; python_version < '3.0' +black; python_version > '3.0'