Comment 14 for bug 1256218

Revision history for this message
Marc Abramowitz (msabramo) wrote : Re: [Bug 1256218] Re: Metaclass exception when accessing headers attribute of a requests response

Yep, it appears to work now!

Check this out:

https://github.com/msabramo/capturemock_examples

Thanks for all your help and I'm glad that I could be of assistance!

Cheers,
Marc

On Fri, Dec 13, 2013 at 12:37 PM, Geoff Bache <email address hidden> wrote:

> I just fixed this. And then I noticed your patches here, which Launchpad
> hadn't mailed me about for some reason...
>
> I think mine is slightly more generic in that it handles multiple base
> classes and lets the existing mechanism find and import everything. But
> I had a rather dense line so I included three of yours to make it more
> readable.
>
> Thanks for your efforts here, hopefully it will work for real now!
>
> --
> You received this bug notification because you are subscribed to the bug
> report.
> https://bugs.launchpad.net/bugs/1256218
>
> Title:
> Metaclass exception when accessing headers attribute of a requests
> response
>
> Status in CaptureMock:
> Fix Committed
>
> Bug description:
> Given the following program:
>
> ```
> (capturemock)marca@marca-mac2:~/capturemock/requests$ cat test.py
> from capturemock import capturemock
>
> @capturemock("requests")
> def do_stuff():
> import requests
>
> resp = requests.get("http://www.google.com/")
> print(resp)
> print(resp.headers)
>
>
> do_stuff()
> ```
>
> ```
> (capturemock)marca@marca-mac2:~/capturemock/requests$ python test.py
> <Response [200]>
> CaseInsensitiveDict({'alternate-protocol': '80:quic',
> 'x-xss-protection': '1; mode=block', 'transfer-encoding': 'chunked',
> 'set-cookie':
> 'PREF=ID=7e28a73323b44303:FF=0:TM=1385711443:LM=1385711443:S=PLAZXhxGUX2EjpFr;
> expires=Sun, 29-Nov-2015 07:50:43 GMT; path=/; domain=.google.com,
> NID=67=ELE4eygYpLp0amsGQzpsg8OjGHrHuf3EVjzChdR9fjkbgRkHqoK1p68w--nDM8r-CdPKNae5llmovNXuNH_vnphKKOfyf7dfXXSsut7mMhebRTpdN_UbljHzjmIaTaqd;
> expires=Sat, 31-May-2014 07:50:43 GMT; path=/; domain=.google.com;
> HttpOnly', 'expires': '-1', 'server': 'gws', 'cache-control': 'private,
> max-age=0', 'date': 'Fri, 29 Nov 2013 07:50:43 GMT', 'p3p': 'CP="This is
> not a P3P policy! See
> http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657for more info."', 'content-type': 'text/html; charset=ISO-8859-1',
> 'x-frame-options': 'SAMEORIGIN'})
> ```
>
> works fine but if setting CAPTUREMOCK_MODE=1, it fails:
>
> ```
> (capturemock)marca@marca-mac2:~/capturemock/requests$
> CAPTUREMOCK_MODE=1 python test.py
> <Response [200]>
> Traceback (most recent call last):
> File "test.py", line 12, in <module>
> do_stuff()
> File "/Users/marca/dev/bzr-repos/capturemock/capturemock/__init__.py",
> line 210, in wrapped_func
> result = func(*funcargs, **funckw)
> File "test.py", line 9, in do_stuff
> print(resp.headers)
> File
> "/Users/marca/dev/bzr-repos/capturemock/capturemock/pythonclient.py", line
> 197, in __getattribute__
> return self.__getattr__(attrname)
> File
> "/Users/marca/dev/bzr-repos/capturemock/capturemock/pythonclient.py", line
> 90, in __getattr__
> self.captureMockTarget)
> File
> "/Users/marca/dev/bzr-repos/capturemock/capturemock/pythontraffic.py", line
> 530, in getAttribute
> return self.getAndRecordRealAttribute(traffic, proxyTarget,
> attrName, proxy, fullAttrName)
> File
> "/Users/marca/dev/bzr-repos/capturemock/capturemock/pythontraffic.py", line
> 574, in getAndRecordRealAttribute
> return self.transformResponse(traffic, realAttr, proxy)
> File
> "/Users/marca/dev/bzr-repos/capturemock/capturemock/pythontraffic.py", line
> 595, in transformResponse
> responseText, transformedResponse =
> traffic.transformResponse(response, proxy)
> File
> "/Users/marca/dev/bzr-repos/capturemock/capturemock/pythontraffic.py", line
> 230, in transformResponse
> transformedResponse = self.transformStructure(wrappedValue,
> self.insertProxy, proxy)
> File
> "/Users/marca/dev/bzr-repos/capturemock/capturemock/pythontraffic.py", line
> 242, in transformStructure
> return transformMethod(result, *args)
> File
> "/Users/marca/dev/bzr-repos/capturemock/capturemock/pythontraffic.py", line
> 252, in insertProxy
> return result.createProxy(proxy)
> File
> "/Users/marca/dev/bzr-repos/capturemock/capturemock/pythontraffic.py", line
> 97, in createProxy
> return proxy.captureMockCreateInstanceProxy(self.name, self.target,
> self.classDesc)
> File
> "/Users/marca/dev/bzr-repos/capturemock/capturemock/pythonclient.py", line
> 94, in captureMockCreateInstanceProxy
> newClass = self.captureMockNameFinder.makeClass(classDesc)
> File
> "/Users/marca/dev/bzr-repos/capturemock/capturemock/pythonclient.py", line
> 36, in makeClass
> return self.defineClass(actualClassName, classDefStr)
> File
> "/Users/marca/dev/bzr-repos/capturemock/capturemock/pythonclient.py", line
> 16, in defineClass
> self.defineClassLocally(classDefStr)
> File
> "/Users/marca/dev/bzr-repos/capturemock/capturemock/pythonclient.py", line
> 49, in defineClassLocally
> exec(classDefStr, self)
> File "<string>", line 1, in <module>
> TypeError: Error when calling the metaclass bases
> metaclass conflict: the metaclass of a derived class must be a
> (non-strict) subclass of the metaclasses of all its bases
> ```
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/capturemock/+bug/1256218/+subscriptions
>