为什么在不同的python版本上运行相同的代码会得到不同的输出?

2024-05-12 19:58:27 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我一直在学习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的支持在今年年初就结束了,这就是为什么我对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
                                            .'                             
                                             .                             


我会提供任何需要的信息,请询问一下

我的问题是我做错了什么以及为什么现在不能正常工作

编辑: 试过这个-->Differences in ctypes between Python 2 and 3,得到如下代码:

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 ~# 


Tags: pymessagehelloworldstringrootctypestesting
1条回答
网友
1楼 · 发布于 2024-05-12 19:58:27

由于printf需要一个字节字符串,因此在传递字符串之前,您应该对字符串进行编码:

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

输出:

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

相关问题 更多 >