Skip to content

Commit

Permalink
Merge pull request #646 from twisted/296.not_done_yet-comparison
Browse files Browse the repository at this point in the history
Avoid bytes/int comparison
  • Loading branch information
exarkun authored Aug 7, 2023
2 parents 7e18f0c + bbda31b commit 9200ef4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
21 changes: 12 additions & 9 deletions src/klein/_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from twisted.web import server
from twisted.web.iweb import IRenderable, IRequest
from twisted.web.resource import IResource, Resource, getChildForRequest
from twisted.web.server import NOT_DONE_YET
from twisted.web.template import renderElement

from ._dihttp import Response
Expand Down Expand Up @@ -304,16 +303,20 @@ def processing_failed(

d.addErrback(processing_failed, self._app._error_handlers)

def write_response(r: object) -> None:
if r is not StandInResource:
if isinstance(r, str):
r = r.encode("utf-8")
def write_response(
r: Union[_StandInResource, str, bytes, int, None]
) -> None:
if r is StandInResource:
return

if (r is not None) and (r != NOT_DONE_YET):
request.write(r)
if isinstance(r, str):
r = r.encode("utf-8")

if not request_finished[0]:
request.finish()
if isinstance(r, bytes):
request.write(r)

if not request_finished[0]:
request.finish()

d.addCallback(write_response)
d.addErrback(log.err, _why="Unhandled Error writing response")
Expand Down
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ setenv =
coverage: COVERAGE_FILE={toxworkdir}/coverage.{envname}
coverage: COVERAGE_PROCESS_START={toxinidir}/.coveragerc

TRIAL_JOBS={env:TRIAL_JOBS:--jobs=2}
TRIAL_ARGS={env:TRIAL_ARGS:}

commands =
# Run trial without coverage
test: trial --random=0 {env:TRIAL_JOBS} --temp-directory="{envlogdir}/trial.d" {posargs:{env:PY_MODULE}}
test: python -b "{envdir}/bin/trial" --random=0 {env:TRIAL_ARGS} --temp-directory="{envlogdir}/trial.d" {posargs:{env:PY_MODULE}}

# Run trial with coverage
# Notes:
Expand All @@ -79,7 +79,7 @@ commands =
# - Use `tox -e coverage_report` to generate a report for all environments.
coverage: python -c 'f=open("{envsitepackagesdir}/zz_coverage.pth", "w"); f.write("import coverage; coverage.process_startup()\n")'
coverage: coverage erase
coverage: coverage run --source="{env:PY_MODULE}" "{envdir}/bin/trial" --random=0 {env:TRIAL_JOBS} --temp-directory="{envlogdir}/trial.d" {posargs:{env:PY_MODULE}}
coverage: python -b -m coverage run --source="{env:PY_MODULE}" "{envdir}/bin/trial" --random=0 {env:TRIAL_ARGS} --temp-directory="{envlogdir}/trial.d" {posargs:{env:PY_MODULE}}
coverage: coverage combine
coverage: coverage xml

Expand Down

0 comments on commit 9200ef4

Please sign in to comment.