Re: getChild in python3
Glyph <[email protected]> Tue, 5 Nov 2019 14:27:33 -0800
| Newsgroups | gmane.comp.python.twisted.web |
|---|---|
| Message-ID | <[email protected]> |
--===============6177053369552488847== Content-Type: multipart/alternative; boundary="Apple-Mail=_F0A31A26-48ED-4DB6-B960-C1D47A48F7E6" --Apple-Mail=_F0A31A26-48ED-4DB6-B960-C1D47A48F7E6 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > On Nov 5, 2019, at 12:20 PM, Nathaniel Haggard <[email protected]> = wrote: >=20 > # This code doesn't call getChild in python3, but it does in python2 = (python2 throws AttributeError, but python3 doesn't) with "curl = http://127.0.0.1:8803/bug/a <http://127.0.0.1:8803/bug/a>" and = Twisted=3D=3D19.7.0 with python3.5.2: >=20 > from twisted.application import internet, service, strports > from twisted.web.resource import Resource > from twisted.web.server import Site > from twisted.internet import reactor > from twisted.python import log >=20 >=20 > from twisted.web.resource import Resource >=20 >=20 >=20 > class R(Resource): > allowedMethods=3D('GET',) > def getChild(self, name, request): > pass > =20 >=20 > r =3D Resource() > r.putChild('bug', R()) This isn't technically a bug in Twisted, since you're passing a `str` on = py3 where it expects a `bytes`; note the "type" annotation right at the = end of = https://twistedmatrix.com/documents/19.7.0/api/twisted.web.resource.IResou= rce.html#putChild = <https://twistedmatrix.com/documents/19.7.0/api/twisted.web.resource.IReso= urce.html#putChild> . In other words, you meant `r.putChild(b'bug', R())`. That said, this is certainly not desirable behavior, so maybe we could = fix it? There are probably a couple of gnarly compatibility concerns to = think about, but it would be nice if it worked. -g > application =3D service.Application('web') > site =3D Site(r) > sc =3D service.IServiceCollection(application) > i =3D strports.service("tcp:8803", site) > i.setServiceParent(sc) >=20 > _______________________________________________ > Twisted-web mailing list > [email protected] > https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web --Apple-Mail=_F0A31A26-48ED-4DB6-B960-C1D47A48F7E6 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii <html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; = charset=3Dus-ascii"></head><body style=3D"word-wrap: break-word; = -webkit-nbsp-mode: space; line-break: after-white-space;" class=3D""><div = class=3D""><br class=3D""></div><blockquote type=3D"cite" class=3D"">On = Nov 5, 2019, at 12:20 PM, Nathaniel Haggard <<a = href=3D"mailto:[email protected]" class=3D"">[email protected]</a>> = wrote:<br class=3D""></blockquote><div><blockquote type=3D"cite" = class=3D""><br class=3D"Apple-interchange-newline"><div class=3D""><div = dir=3D"ltr" class=3D""><div class=3D""># This code doesn't call getChild = in python3, but it does in python2 (python2 throws AttributeError, but = python3 doesn't) with "curl <a href=3D"http://127.0.0.1:8803/bug/a" = class=3D"">http://127.0.0.1:8803/bug/a</a>" and Twisted=3D=3D19.7.0 with = python3.5.2:</div><div class=3D""><br class=3D""></div>from = twisted.application import internet, service, strports<br class=3D"">from = twisted.web.resource import Resource<br class=3D"">from = twisted.web.server import Site<br class=3D"">from twisted.internet = import reactor<br class=3D"">from twisted.python import log<br = class=3D""><br class=3D""><br class=3D"">from twisted.web.resource = import Resource<br class=3D""><br class=3D""><br class=3D""><br = class=3D"">class R(Resource):<br class=3D""> = allowedMethods=3D('GET',)<br class=3D""> def getChild(self, = name, request):<br class=3D""> pass<br = class=3D""> <br class=3D""><br class=3D"">r =3D= Resource()<br class=3D"">r.putChild('bug', R())<br = class=3D""></div></div></blockquote><div><br class=3D""></div><div>This = isn't <i class=3D"">technically</i> a bug in Twisted, since = you're passing a `str` on py3 where it expects a `bytes`; note the = "type" annotation right at the end of <a = href=3D"https://twistedmatrix.com/documents/19.7.0/api/twisted.web.resourc= e.IResource.html#putChild" = class=3D"">https://twistedmatrix.com/documents/19.7.0/api/twisted.web.reso= urce.IResource.html#putChild</a> .</div><div><br = class=3D""></div><div>In other words, you meant `r.putChild(b'bug', = R())`.</div><div><div class=3D""><br class=3D""></div><div class=3D"">That= said, this is certainly not desirable behavior, so maybe we could fix = it? There are probably a couple of gnarly compatibility concerns = to think about, but it would be nice if it worked.</div><div = class=3D""><br class=3D""></div><div class=3D"">-g</div></div><br = class=3D""><blockquote type=3D"cite" class=3D""><div class=3D""><div = dir=3D"ltr" class=3D"">application =3D service.Application('web')<br = class=3D"">site =3D Site(r)<br class=3D"">sc =3D = service.IServiceCollection(application)<br class=3D"">i =3D = strports.service("tcp:8803", site)<br class=3D"">i.setServiceParent(sc)<br= class=3D""><div class=3D""><br class=3D""></div></div> _______________________________________________<br class=3D"">Twisted-web = mailing list<br class=3D""><a = href=3D"mailto:[email protected]" = class=3D"">[email protected]</a><br = class=3D"">https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web<= br class=3D""></div></blockquote></div><br class=3D""></body></html>= --Apple-Mail=_F0A31A26-48ED-4DB6-B960-C1D47A48F7E6-- --===============6177053369552488847== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KVHdpc3RlZC13 ZWIgbWFpbGluZyBsaXN0ClR3aXN0ZWQtd2ViQHR3aXN0ZWRtYXRyaXguY29tCmh0dHBzOi8vdHdp c3RlZG1hdHJpeC5jb20vY2dpLWJpbi9tYWlsbWFuL2xpc3RpbmZvL3R3aXN0ZWQtd2ViCg== --===============6177053369552488847==--