Skip to content

Commit

Permalink
Merge pull request #65 from nooperpudd/bug/issues-62
Browse files Browse the repository at this point in the history
fix UnicodeDecodeError
  • Loading branch information
nooperpudd authored Sep 28, 2021
2 parents 62fb681 + e505500 commit 8ea7b7c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,37 @@ already install cython package.
## Demo
sample code [samples](samples/)

## issues
https://github.com/nooperpudd/ctpwrapper/issues/62
this is a temporary solution for the UnicodeDecodeError issue.
```
Traceback (most recent call last):
File "ctpwrapper/TraderApi.pyx", line 1402, in ctpwrapper.TraderApi.TraderSpi_OnRspQrySettlementInfo
File "/root/python/futures/trader_main.py", line 149, in OnRspQrySettlementInfo
print(pSettlementInfo.Content)
File "/root/python/futures/.venv/lib/python3.9/site-packages/ctpwrapper/base.py", line 28, in __getattribute__
return value.decode("gbk")
UnicodeDecodeError: 'gbk' codec can't decode byte 0xd2 in position 499: incomplete multibyte sequence
```
```python

error_message = ""

def OnRspQryTradingAccount(self, pTradingAccount, pRspInfo, nRequestID, bIsLast):
print("OnRspQryTradingAccount")
print("nRequestID:", nRequestID)
print("bIsLast:", bIsLast)
print("pRspInfo:", pRspInfo)
print("pTradingAccount:", pTradingAccount)
# need to check is last message from the server.
global error_message
if not bIsLast:
error_message+=pRspInfo.ErrorMsg
else:
error_message+=pRspInfo.ErrorMsg
if isinstance(error_message,bytes):
error_message.decode("gbk")
```

# Documentation
CTP documentation can be found in the [docs](doc/ctp/)
Expand Down
2 changes: 1 addition & 1 deletion ctpwrapper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
along with ctpwrapper. If not, see <http://www.gnu.org/licenses/>.
"""
__version__ = "6.6.1.3"
__version__ = "6.6.1.4"
__date__ = "2021-04-06"

from ctpwrapper.Md import MdApiPy
Expand Down
9 changes: 8 additions & 1 deletion ctpwrapper/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ class Base(ctypes.Structure):
def __getattribute__(self, item):
value = super().__getattribute__(item)
if isinstance(value, bytes):
return value.decode("gbk")
try:
return value.decode("gbk")
except UnicodeDecodeError:
# UnicodeDecodeError: 'gbk' codec
# can't decode byte 0xd2 in position 499:
# incomplete multibyte sequence
# return bytes values
return value
else:
return value

Expand Down

0 comments on commit 8ea7b7c

Please sign in to comment.