简体   繁体   English

Windows x64上可返回的Python 2. * x86(ie32bit)id()函数的最高数目是多少?

[英]What is the highest number Python 2.* x86 (i.e.32bit) id() function on Windows x64 can return?

What is the highest number Python 2.6's x86 id() function can return? Python 2.6的x86 id()函数可以返回的最高位数是多少?

I presume the ceiling must be the amount of memory any 32-bit application can see which must be: 2 32 , ie 4294967296? 我认为上限必须是任何32位应用程序可以看到的内存量,该内存量必须是:2 32 ,即4294967296?

(Which would be fine as I have to fit this value in a C# type for which UInt32 or the CLS-compliant Int64 would suffice, which it the reason for my concern, though irrelevant to the question.) (这很好,因为我必须将此值设置为UInt32或符合CLS的Int64就可以满足的C#类型,这是我关注的原因,尽管与问题无关。)

But what if a I am running on Windows x64 with more than 2GB of memory say 32GB of memory - is it possible for Python's id() function, even though the Python interpreter itself is x86, to return a value higher then 2 32 ??? 但是 ,如果我运行的Windows x64的内存超过2GB,比如说32GB的内存,即使Python解释器本身是x86,Python的id()函数是否仍可能返回比2 32高的值呢? ?

I am guessing it comes down to the Python interpreter's view of the machine - I imagine WoW64 translates 64-bit memory addresses to 32-bit addresses - but I am only guessing - I need to be sure! 我猜想这取决于机器的Python解释器视图-我想WoW64会将64位内存地址转换为32位地址-但是我只是在猜-我需要确定!

From the linked documentation : 链接的文档中

Returns (...) an integer (or long integer) 返回(...)一个整数(或长整数)

In Python 2.x on amd64, a long integer is an integer larger than 64 bits. 在amd64上的Python 2.x中,长整数是大于64位的整数。 In any case, in Python, integers are unbounded. 无论如何,在Python中,整数是无界的。 Therefore, id can return arbitrarily long values . 因此, id可以返回任意长的值

If you must know a definite maximum value, you may assume that the available memory on your platform is an upper boundary for the size of the integer you get. 如果必须知道一个确定的最大值,则可以假定平台上的可用内存是所获取整数大小的上限。 Therefore, I'd specify 2 2 32 for 32 bit, and 2 2 64 for 64 architectures. 因此,我会指定2 2 32 32位,和2 2 64 64个体系结构。 In the case of an x86 python implementation, one can therefore place an upper boundary at 2 2 32 with reasonable confidence. 因此,在使用x86 python的情况下,可以以合理的置信度在2 2 32处设置上限。

cpython (the most popular python implementation) will indeed return a memory addresses ( builtin_id in Python/bltinmodule.c ): cpython(最流行的python实现)确实会返回一个内存地址( Python/bltinmodule.c builtin_id ):

static PyObject * builtin_id(PyObject *self, PyObject *v) {
    return PyLong_FromVoidPtr(v);
}

This will be a 32 bit/64 bit value, but the behavior is an implementation detail , as explicitly stated in the docs. 这将是32位/ 64位值,但是行为是实现细节 ,如docs中明确说明的那样。 By definition, a programmer must not rely on implementation details. 根据定义,程序员不得依赖实现细节。

I strongly doubt that there is a legitimate use case for ever using IDs, much less transferring them to another program. 我强烈怀疑使用ID会存在合法的用例,更不用说将其转移到另一个程序了。 Instead, you should use a custom object table, or just transfer a set. 相反,您应该使用自定义对象表,或仅传输一组。 If you intend to use Python in conjunction with C#, ironpython allows you to do so in the same code. 如果您打算将Python与C#结合使用, ironpython允许您在同一代码中使用。

def func(n):
    max=0L
    for i in range(n):
        x=id(i)
        if x > max : max=x
    return max

for i in range(32):
    x=0L
    try:
        x=func(2**i)
        print '{0: 10}{1: 15}{2: 15}'.format(i,2**i,x)
    except:
        print '{0: 10}{1: 15}{2:>15}'.format(i,2**i,'error')

i think in practical the max value the id() can achieve is 2**32 but in reality it never reaches such high number. 我认为在实用上id()可以达到的最大值是2 ** 32,但实际上它从未达到如此高的数值。 I tried the above code in the 32 bit architecture . 我在32位体系结构中尝试了上面的代码。 please let me know if i made any mistake in understanding your question. 如果我在理解您的问题时犯了任何错误,请告诉我。

4294967296L is the 2^32 and max id() i reached is 1460876200. 4294967296L是2 ^ 32,我达到的最大id()是1460876200。

Your guesses seem reasonable, but I have no idea if they're correct. 您的猜测似乎是合理的,但我不知道它们是否正确。

If you need to rely on behaviour that is an implementation detail (ie isn't specified in the docs), and you need to be sure , then I guess the only thing to do is read the source code for the version you're using and find out what it does. 如果您需要依靠行为来实现细节(即未在文档中指定),并且需要确定 ,那么我想唯一要做的就是阅读所使用版本的源代码。并找出其作用。

Windows will run an application in the environment (32 or 64 bit) that it was compiled for, not the CPU it is running on. Windows将在为其编译的环境(32或64位)中运行应用程序,而不是在其运行的CPU上运行。 Eg, an x64 computer running Windows 7 will run 32-bit python in a 32-bit environment. 例如,运行Windows 7的x64计算机将在32位环境中运行32位python。

So, running python compiled for x86 will always use a 32-bit address space, which means that id() will always return a value that maps to a unique 32-bit pointer. 因此,运行为x86编译的python将始终使用32位地址空间,这意味着id()将始终返回映射到唯一32位指针的值。 (Note that the implementation may do some weird stuff like salting the pointer or using 64-bits or some crazy stuff like that.) (请注意,该实现可能会做一些奇怪的事情,例如给指针加盐或使用64位或类似的疯狂事情。)

Assuming the documentation for id() is correct and CPython (x86) returns the address of the object, the maximum value that can be returned is 2 32 -1 (4294967295). 假设id()的文档正确,并且CPython(x86)返回了对象的地址,则可以返回的最大值为2 32 -1(4294967295)。 This can be demonstrated in a simple C program: 这可以用一个简单的C程序来演示:

#include <stdio.h>

int main(void) {
    void* p = 0;          // specify a pointer at 0
    p = ~p;               // invert all bits
    uintptr_t x = p;      // convert to an integer type fo the same size
    printf("%lu\n", x);
    return 0;
}

This is true regardless of the underlying OS, an application compiled with a 32-bit pointer type can only specify addresses between 0 and 2 32 -1. 无论使用哪种底层OS,都是如此,使用32位指针类型编译的应用程序只能指定0到2 32 -1之间的地址。 Note that the address visible in the application may not correspond to a particular physical memory address, due to the use of virtual memory . 注意,由于使用虚拟内存 ,应用程序中可见的地址可能不对应于特定的物理内存地址。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 构建 Python 扩展失败(“kernel32.lib”无法打开 |“x64”与目标机器类型“x86”冲突) - Building Python extension fails ("kernel32.lib" cannot be opened | "x64" conflicts with target machine type "x86") 我在x86-64 PC上运行32位Windows。 我应该下载32位还是64位Python安装程序? - I'm running 32-bit Windows on an x86-64 PC. Should I download the 32-bit or 64-bit Python installer? Windows x64 vs x86:硬件 vs. 操作系统 vs. 进程 - Windows x64 vs x86: Hardware vs. OS vs. process 如何为x86计算机重新编译x64 blender python脚本? - How to recompile an x64 blender python script for x86 computers? 我安装了 Python 3 x64、Python 2.7 x64 和 Python 2.7 x32。 如何使用 py 启动器切换到 Python 2.7 的 32 位版本? - I have Python 3 x64, Python 2.7 x64, and Python 2.7 x32 installed. How can I use py launcher to switch to the 32 bit version of Python 2.7? 我如何知道要返回哪个实际的dll(x86 v x64)? - How do I tell which actual dll is being returned (x86 v x64)? x64 Python Wheels是否需要x86 Visual C ++库? - Are the x86 Visual C++ libraries needed for x64 Python Wheels? Windows x64上的Python x64位复制文件性能评估/问题 - Python x64 bit on Windows x64 copy file performance evaluation / problem DLL加载失败,同时没有为x86和x64 DLL显示有效的Win32应用 - DLL Load Failed, Not a Valid Win32 App showing for both x86 & x64 DLLs 在 windows x64 位上安装 Opencv-python 时出现问题 - Issue installing Opencv-python on windows x64 bit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM