Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unpickle obj attribute error #34

Merged
merged 2 commits into from
Jan 14, 2019

Conversation

junnplus
Copy link
Contributor

https://paste.ubuntu.com/25721297/
raise AttributeError when unpickle object get attribute

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-b4fa9c6bca01> in <module>()
----> 1 getattr(hongbao, 'detail_json')

AttributeError: 'THongbao' object has no attribute 'detail_json'

@codecov
Copy link

codecov bot commented Jan 14, 2019

Codecov Report

Merging #34 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #34   +/-   ##
=======================================
  Coverage   80.31%   80.31%           
=======================================
  Files          35       35           
  Lines        3475     3475           
=======================================
  Hits         2791     2791           
  Misses        684      684
Impacted Files Coverage Δ
thriftpy2/protocol/binary.py 90.27% <100%> (ø) ⬆️
thriftpy2/protocol/compact.py 80.39% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c131270...ba81848. Read the comment docs.

@ethe
Copy link
Member

ethe commented Jan 14, 2019

Please describe your problem in detail and how your patch fixes it. The code you post above is not a regular way to use Thriftpy.

@junnplus
Copy link
Contributor Author

Steps to reproduce

  1. define a thrift file
# foo.thrift
struct TRequest {
    1: optional i32 id,
    2: optional string name,
}

service FooService {
    bool api(1: TRequest request)
}
  1. construct a request and serialize it with pickle, which will be pushed to the MQ
# client
import pickle

request = foo_thrift.Request(
    id=1,
    name='foo'
)

pickled_request = pickle.dumps(request)
message_queue.push(pickled_request)
  1. before the message has been consumed, we announced a new field, and the new thrift struct looks like
struct TRequest {
    1: optional i32 id,
    2: optional string name,
    3: optional string bar,
}
  1. the message consumer receive the message and used as param to call api
pickled_request = message_queue.pop()
request = pickle.loads(pickled_request)
foo_service.api(request)  // errors will occur

now we got a TDecodeException

Conclusion

because the message received from MQ does not have the field bar,
from here we know that each field defined in the thrift specification will be considered as required, which is not backward compatible,
when we serialize the request by new specification, the AttributeError will be raise

This PR provide a workaround to ensure the compatibility

@ethe
Copy link
Member

ethe commented Jan 14, 2019

Thanks.

@ethe ethe merged commit 0e96087 into Thriftpy:master Jan 14, 2019
@junnplus junnplus deleted the unpickle-obj-getattr branch January 16, 2019 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants