簡體   English   中英

為什么在不同的 python 版本上運行相同的代碼會得到不同的輸出?

[英]Why do I get different output running the same code on different python versions?

所以當我得到一個意想不到的結果時,我一直在學習 ctypes 庫。

我有以下代碼只是為了測試 ctypes 是否正常工作:

from ctypes import *
libc = CDLL("libc.so.6")
message_string = "Hello world!\n"
libc.printf("Testing: %s", message_string)

我用 python2.7 和 python3 運行它,雖然版本不同,但看起來應該有相同的結果。

root@Crushli-pc ~# python3 --version && python --version
Python 3.7.5
Python 2.7.17
root@Crushli-pc ~# python chapter1-printf.py 
Testing: Hello world!
root@Crushli-pc ~# python3 chapter1-printf.py 
T⏎                                                                                                                                                                                                                                         root@Crushli-pc ~# 

知道python2的支持與今年年初結束,這就是為什么我很好奇,為什么作為intented python3不起作用。

我一直在運行代碼的機器:

..............                                     root@Crushli-pc 
            ..,;:ccc,.                             --------------- 
          ......''';lxO.                           OS: Kali GNU/Linux Roll 
.....''''..........,:ld;                           Kernel: 5.3.0-kali2-amd 
           .';;;:::;,,.x,                          Uptime: 1 hour, 21 mins 
      ..'''.            0Xxoc:,.  ...              Packages: 2226 (dpkg) 
  ....                ,ONkc;,;cokOdc',.            Shell: bash 5.0.11 
 .                   OMo           ':ddo.          Resolution: 1080x1920,  
                    dMc               :OO;         DE: Xfce 
                    0M.                 .:o.       WM: Xfwm4 
                    ;Wd                            WM Theme: Kali-Dark 
                     ;XO,                          Theme: Kali-Dark [GTK2/ 
                       ,d0Odlc;,..                 Icons: Flat-Remix-Blue- 
                           ..',;:cdOOd::,.         Terminal: qterminal 
                                    .:d;.':;.      Terminal Font: Fira Cod 
                                       'd,  .'     CPU: Intel i5-3570 (4)  
                                         ;l   ..   GPU: NVIDIA GeForce GTX 
                                          .o       Memory: 1791MiB / 7922M 
                                            c
                                            .'                             
                                             .                             


我會提供任何需要的信息,請詢問。

我的問題是我做錯了什么為什么它現在不能正常工作。

編輯:試過這個 --> Python 2 和 3 之間 ctypes 的差異,得到這樣的代碼:

from ctypes import *                                                                                                                                                                                                                   

msvcrt = CDLL("libc.so.6")                                                                                                                                                                                                             
message_string = str("Hello world!\n").encode('ascii')                                                                                                                                                                                 
msvcrt.printf("Testing: %s", message_string)                                                                                                                                                                                                                                                   

不過,我得到了相同的結果:

root@Crushli-pc ~# cat chapter1-printf.py 
from ctypes import *

msvcrt = CDLL("libc.so.6")
message_string = str("Hello world!\n").encode('ascii')
msvcrt.printf("Testing: %s", message_string)
root@Crushli-pc ~# python3 chapter1-printf.py 
T⏎                                                                                                                                                                                                                                         root@Crushli-pc ~# python chapter1-printf.py 
Testing: Hello world!
root@Crushli-pc ~# 

由於printf需要一個字節字符串,因此您應該在傳遞字符串之前對它們進行編碼:

 libc.printf("Testing with non-ascii chars abc€ éю: %s".encode('utf8'),
             message_string.encode('utf8'))

輸出:

Testing with non-ascii chars abc€ éю: Hello world!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM