{"id":198072,"date":"2020-11-06T14:42:47","date_gmt":"2020-11-06T06:42:47","guid":{"rendered":"http:\/\/4563.org\/?p=198072"},"modified":"2020-11-06T14:42:47","modified_gmt":"2020-11-06T06:42:47","slug":"python3-%e4%b8%ad-json-%e7%9a%84%e4%b8%80%e4%b8%aa-bug%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=198072","title":{"rendered":"python3 \u4e2d json \u7684\u4e00\u4e2a bug\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  python3 \u4e2d json \u7684\u4e00\u4e2a bug\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : deplives <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u76ee\u524d\u53d1\u73b0\u8fd9\u6837\u4e00\u4e2a\u95ee\u9898\uff1a<\/p>\n<p>\u81ea\u5b9a\u4e49\u4e00\u4e2a <code>dict<\/code> \u5b50\u7c7b\uff0c\u5e76\u4e14\u5b9e\u73b0\u81ea\u5b9a\u4e49 <code>__bool__<\/code> \u65b9\u6cd5\u540e <code>json.dumps()<\/code> \u4f1a\u4ea7\u751f\u548c\u9884\u671f\u4e0d\u7b26\u7684\u7ed3\u679c<\/p>\n<p>\u8bdd\u4e0d\u591a\u8bf4\uff0c\u4e0a\u4ee3\u7801<\/p>\n<pre><code>import json  class InnerDict(dict):     def __init__(self, **kwargs):         super().__init__(**kwargs)      def __bool__(self):     # \u539f\u59cb\u8fd9\u91cc\u7684\u903b\u8f91\u662f\u53ea\u8981\u5b57\u5178\u4e3a\u7a7a\u624d\u8fd4\u56de False\uff0c\u975e\u7a7a\u90fd\u8fd4\u56de True     # \u4f46\u662f\u5982\u679c\u6539\u6389\u6bd4\u5982\u8fd9\u91cc\u6240\u6709\u90fd\u8fd4\u56de False         return False          dct = InnerDict() dct[\"hello] = \"world\" print(dct) # \u5230\u8fd9\u91cc\u90fd\u6ca1\u6709\u95ee\u9898 json.dumps(dct, ensure_ascii=False, indent=4) # \u4f46\u662f\u8fd9\u91cc\uff0cdumps \u7684\u7ed3\u679c\u5c31\u662f {} <\/code><\/pre>\n<p>\u539f\u56e0\u5206\u6790\uff1a<\/p>\n<p>\u5728 <code>json<\/code> \u5e93 <code>encoder.py<\/code> \u6587\u4ef6\u4e2d <code>_make_iterencode<\/code> \u65b9\u6cd5\u5185\u6709\u4e2a\u95ed\u5305\u7684\u65b9\u6cd5 <code>_iterencode_dict<\/code><\/p>\n<p>\u5728\u8fd9\u65b9\u6cd5\u4e2d\u7b2c\u4e00\u4ef6\u4e8b\u53bb\u5224\u65ad <code>dct<\/code> \u662f\u5426\u4e3a\u7a7a,\u7528\u4e86<\/p>\n<pre><code>if not dct:     yield '{}'     return <\/code><\/pre>\n<p>\u622a\u56fe\u5728\u8fd9\u91cc https:\/\/www.v2ex.com\/i\/5Xjw6YFt.jpeg<\/p>\n<p>\u4f46\u662f\u95ee\u9898\u5c31\u5728\u8fd9\u91cc\uff0c\u8fd9\u91cc\u4f1a\u8c03\u7528 <code>__bool__<\/code> \u5224\u65ad\uff0c\u4f46\u662f <code>__bool__<\/code> \u7684\u903b\u8f91\u5df2\u7ecf\u88ab\u6539\u4e86\uff0c\u6240\u4ee5\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u91cc\u8fd9\u91cc\u4e00\u76f4\u4e3a <code>{}<\/code><\/p>\n<p>\u800c\u4e14\u8ffd\u8e2a\u4e86 <code>_iterencode_dict<\/code> \u65b9\u6cd5\uff0c\u53d1\u73b0\u4e09\u5904\u8c03\u7528\u5904\u5747\u5224\u65ad\u4e86 <code>dct<\/code> \u5bf9\u8c61\u4e3a <code>dict<\/code><\/p>\n<p>\u8fd9\u91cc\u7684 <code>if not dct<\/code> \u4ec5\u4ec5\u505a\u4e86\u5224\u65ad <code>dct<\/code> \u662f\u5426\u4e3a\u7a7a <code>dict<\/code><\/p>\n<p>\u76ee\u524d\u5c31\u662f\u8fd9\u4e48\u4e2a\u60c5\u51b5\uff0c\u4e0d\u77e5\u9053\u8fd9\u7b97\u4e0d\u7b97\u4e00\u4e2a bug<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>16<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4240583\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ruanimal <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u4e0d\u7b97 bug \u5427\u3002\u3002<\/p>\n<p>\u800c\u4e14\u4f60\u8fd9\u4e2a\u7ee7\u627f\u8fdd\u53cd\u4e86\u91cc\u6c0f\u4ee3\u6362\u539f\u5219                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4240584\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : acmore <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c `_iterencode_dict` \u53ea\u7ba1\u6309\u81ea\u5df1\u7684\u903b\u8f91\u53bb\u5224\u65ad\u662f\u5426\u4e3a\u7a7a\uff0c\u800c\u4e0d\u7ba1\u4f60\u600e\u4e48\u5b9e\u73b0 `__bool__`\uff0c\u8fd9\u624d\u662f\u4e00\u4e2a Bug\uff0c\u56e0\u4e3a\u5b9e\u9645\u4e0a\u5bf9 `__bool__` \u7684\u91cd\u8f7d\u662f\u65e0\u6548\u7684\u3002<\/p>\n<p>\u4f60\u6ca1\u6709\u6b63\u786e\u5b9e\u73b0 `__bool__` \u7684\u529f\u80fd\uff0c\u81ea\u7136\u4e5f\u5f97\u4e0d\u5230\u671f\u5f85\u7684\u7ed3\u679c\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4240585\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zmaplex <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u7b97\uff0c\u6211\u8ba4\u4e3a\u4f60\u7834\u574f\u4e86\u8bbe\u8ba1\u4e0a\u7684\u4f20\u9012\u6027\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4240586\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Vinty <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5bf9\u8c61\u4e3a\u7a7a\uff0c\u6240\u4ee5 dump \u4e5f\u662f\u7a7a\u7684\uff0c\u8fd9\u5f88\u7b26\u5408\u903b\u8f91\u554a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4240587\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ofooo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u8981\u5f04 dict \u7684\u5b50\u7c7b\uff0c\u786e\u5b9e\u6709 bug\uff0cpython \u732b\u516c\u4f17\u53f7\u524d\u51e0\u5929\u8fd8\u8bb2\u8fc7\u3002<\/p>\n<p>\u4e0d\u8981\u5f04 python \u5b98\u65b9\u7c7b\u7684\u5b50\u7c7b\u7684\u9b54\u6cd5\u51fd\u6570\uff0c\u4ed6\u7ee7\u627f\u987a\u5e8f\u6709\u4e00\u5b9a\u7684\u4e0d\u4e00\u81f4\u6027                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4240588\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ofooo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u8fc7\u611f\u89c9\u4f60\u8fd9\u4e2a\u662f\u81ea\u4f5c\u81ea\u53d7\u5427\u3002\u3002\u3002\u3002\u4eba\u5bb6\u8fd9\u4e2a\u903b\u8f91\u6ca1\u95ee\u9898\u554a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4240589\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xuboying <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u770b\u4e86\u534a\u5929\u6ca1\u7406\u89e3 op \u89c9\u5f97\u54ea\u91cc\u662f bug \uff1f\u662f\u5bf9\u90a3\u4e2a if \u5224\u65ad\u7684\u6761\u4ef6\u6709\u5f02\u8bae\u4e48\uff1f op \u671f\u671b\u662f\u600e\u6837\u7684\u7ed3\u679c\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4240590\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cz5424 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u4e2a\u95ee\u9898\u7c7b\u4f3c\u4e8e\uff0c\u6211\u4fee\u6539\u4e86\u652f\u4ed8\u5b9d\u5ba2\u670d\u7aef\uff0c\u52a0\u4e86\u4e2a\u4e00\u767e\u4e07\uff0c\u4f46\u662f\u6211\u4e70\u4e0d\u4e86\u4e1c\u897f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4240591\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Wincer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e3b\u610f\u601d\u662f if not dct \u5e94\u8be5\u6539\u6210 if dct is not {}\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4240592\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : SjwNo1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u90a3\u5c31\u518d\u6539 __bool__ \u77e5\u9053\u4f60\u6ee1\u610f\u4e3a\u6b62\u54c8\u54c8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4240593\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mringg <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u60f3\u8bf4\u4eba\u751f\u5df2\u7ecf\u591f\u96be\u4e86\uff0c\u4e0d\u8981\u96be\u4e3a\u81ea\u5df1\u4e86\uff0c\u5c31\u7528\u4e9b\u57fa\u672c\u7684\u7528\u6cd5\u4e0d\u597d\u4e48                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4240594\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : no1xsyzy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             dict \u7684 __bool__ \u542b\u4e49\u5c31\u662f\u8fd9\u4e2a dict \u662f\u5426\u4e3a\u7a7a<br \/>_iterencode_dict \u8bf4\uff1a\u65e2\u7136\u4f60\u8bf4\u5b83\u662f\u7a7a\uff0c\u90a3\u5b83\u5c31\u662f\u7a7a\u54af\uff0c\u4e0d\u53cd\u9a73\uff0c'{}&#8217; \u6765                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4240595\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : timothyqiu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8bf4\u767d\u4e86\u5c31\u662f garbage in, garbage out                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4240596\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cnrting <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u90fd\u7528 python \u4e86\u4f55\u82e6\u518d\u4e3a\u96be\u81ea\u5df1                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4240597\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lithbitren <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ea\u6709\u5728\u7b97\u6cd5\u9898\u91cc\u5199\u5b57\u5178\u6811\u7684\u65f6\u5019\u4f1a\u7ee7\u627f\u5b57\u5178\uff0c\u5176\u4ed6\u60c5\u51b5\u4e0b\u539f\u5219\u4e0a\u90fd\u4e0d\u4f1a\u7ee7\u627f\u8fd9\u4e9b\u57fa\u7840\u6570\u636e\u7ed3\u6784                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4240598\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : krixaar <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u662f\u4e0d\u662f\u53ef\u4ee5\u7c7b\u6bd4\u6210\u540e\u7aef\u5077\u5077\u6539\u4e86\u63a5\u53e3\u8fd4\u56de\u503c\uff0c\u524d\u7aef\u5d29\u4e86\u8bf4\u662f\u524d\u7aef\u6709 bug \uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>python3 \u4e2d json \u7684\u4e00&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/198072"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=198072"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/198072\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=198072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=198072"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=198072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}