# Regarding whether we should add \`Py\_CurrentArch\` or \`Py\_ArchName\` function

**URL:** <https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191>\
**Category:** C API\
**Created:** [September 27, 2024, 2:32am UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191 "2024-09-27T02:32:51Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruang](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ruang/32/22210_2.png) [@ruang](https://discuss.python.org/u/ruang)\
**Post date:** [September 27, 2024, 2:32am UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/1 "2024-09-27T02:32:51Z")

</div>

When working on task [gh-102536: Added `_architecture` field to `sys.implementation` by rruuaanng · Pull Request #124582 · python/cpython · GitHub](https://github.com/python/cpython/pull/124582), I found that getting the current running architecture through C API is quite troublesome. Even though there’s `uname` in Unix-like systems and `GetNativeSystemInfo` in Win32, not everyone knows these two APIs (maybe I’m not familiar enough with C API, or maybe there’s another way). So, I think it’s necessary to add a public function(or private) in C API to get the hardware architecture we’re currently running on.

Perhaps when implementing other hardware architecture-specific optimizations in the future, an API can be provided to distinguish the current system architecture, And take corresponding actions according to the architecture.

Maybe this code can illustrate my dilemma:

```C
#if !defined(MS_WINDOWS) && defined(HAVE_SYS_UTSNAME_H)
    res = uname(&u);
    if (res < 0)
        goto error;
    value = PyUnicode_FromString(u.machine);
#else
    /* ignore other */
    GetNativeSystemInfo(&s);
    switch (s.wProcessorArchitecture) {
    case PROCESSOR_ARCHITECTURE_AMD64: break;
    case PROCESSOR_ARCHITECTURE_ARM: break;
    case PROCESSOR_ARCHITECTURE_ARM64: break;
    case PROCESSOR_ARCHITECTURE_IA64: break;
    case PROCESSOR_ARCHITECTURE_INTEL: break; 
    case PROCESSOR_ARCHITECTURE_UNKNOWN: break;
    default: break;
    }
#endif /* !MS_WINDOWS */
    if (value == NULL)
        goto error;
    res = PyDict_SetItemString(impl_info, "_architecture", value);
    Py_DECREF(value);
    if (res < 0)
        goto error;

```

Actually, this is how I wanted to express it:

```C
   arch = Py_ArchName();
   value = PyUnicode_FromString(arch);
   res = PyDict_SetItemString(impl_info, "_architecture", value);
   Py_DECREF(value);
   PyMem_RawFree(arch);
   if (res < 0)
       goto error;

```

And in the future, when he branches out into other related areas, he’ll have even more impressive performance.

```C
arch = Py_ArchName() // or Py_CurrentArch()
switch(arch) {
case "win32": break;
case "arm": break;
/* some other*/
}

```

---

<div class="post-metadata">

**Author:** ![ruang](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ruang/32/22210_2.png) [@ruang](https://discuss.python.org/u/ruang)\
**Post date:** [September 27, 2024, 6:43am UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/2 "2024-09-27T06:43:09Z")

</div>

If this proposal is allowed, I will submit my changes (I have already implemented and applied them in my personal compiler, and they are very practical).

---

<div class="post-metadata">

**Author:** ![vstinner](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vstinner/32/15130_2.png) [@vstinner](https://discuss.python.org/u/vstinner)\
**Post date:** [September 27, 2024, 7:26am UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/3 "2024-09-27T07:26:16Z")

</div>

I don’t recall that I ever needed to know the architecture while writing C code. I don’t understand the use case for such function.

(By the way, a string is not convenient in C, I expected an integer or an enum.)

---

<div class="post-metadata">

**Author:** ![ruang](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ruang/32/22210_2.png) [@ruang](https://discuss.python.org/u/ruang)\
**Post date:** [September 27, 2024, 7:28am UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/4 "2024-09-27T07:28:36Z")

</div>

Maybe based on what you mentioned, if allowed, maybe I can initiate this submission.

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [September 27, 2024, 9:49am UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/5 "2024-09-27T09:49:15Z")

</div>

I don’t know that we’ve come up against a major need for checking “does the current OS differ from how CPython was compiled.”\[1\] If there are a few places we already do this kind of check, it might be worth creating a helper function, but I’m not aware of any. Usually we target optimisations for the compile-time architecture, and users get that behaviour wherever they run, so they can choose which build to use.

The proposed `sys.implementation._architecture`\[2\] attribute is also not meant to be the _current_ OS, but it should be the architecture that CPython was _compiled_ for. Right now, there’s no obvious way for Python code to tell whether it’s running on an ARM64, x64 or x86 build of Python (if we’re on Windows ARM64, any of these are possibilities).\[3\]

* * *

1. Which mainly matters on Windows, with its transparent CPU emulation, and possibly on macOS. 

2. Name is still under discussion 

3. The non-obvious way is to check `sys.winver`, but it requires parsing that isn’t specified and won’t be specified, because that’s not the point of this field. It just happens to be the only place where the architecture is embedded at compile time.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [September 27, 2024, 10:43am UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/6 "2024-09-27T10:43:06Z")

</div>

Isn’t is better at runtime to do a feature check?

Is the API you require supported?

- you want to use a new linux kernel system call for example

Does the CPU have the instruction you want to use?

- you want to use AVX512 to optimise an algorithm

---

<div class="post-metadata">

**Author:** ![ruang](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ruang/32/22210_2.png) [@ruang](https://discuss.python.org/u/ruang)\
**Post date:** [September 27, 2024, 11:09am UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/7 "2024-09-27T11:09:22Z")

</div>

But from what I can tell, it seems like the result it returns is just the python version, not the current architecture.

```python
>>> import sys;sys.winver
'3.14'

```

I’m sorry, I don’t understand why sys.winver is parsed.  
So my PR does not include any part about winver, but only obtains the current architecture through win32 api.

---

<div class="post-metadata">

**Author:** ![ruang](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ruang/32/22210_2.png) [@ruang](https://discuss.python.org/u/ruang)\
**Post date:** [September 27, 2024, 11:16am UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/8 "2024-09-27T11:16:48Z")

</div>

Maybe, but this post is just to collect responses from developers. There may be the same developers who may need this API.

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [September 27, 2024, 11:20am UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/9 "2024-09-27T11:20:14Z")

</div>

> [@ruang](#):
>
> But from what I can tell, it seems like the result it returns is just the python version, not the current architecture.

If you are using a 32-bit build, it’ll be `3.14-32`, and on an ARM64 build it’ll be `3.13-arm64`, regardless of what operating system or CPU you’re actually running on. You can try this with a 32-bit build easily enough.

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [September 27, 2024, 11:22am UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/10 "2024-09-27T11:22:48Z")

</div>

> [@ruang](#):
>
> So my PR does not include any part about winver, but only obtains the current architecture through win32 api.

Yes, which we don’t need. The architecture used at compile time is available as preprocessor variables, and we typically code so that we assume the current CPU matches what we compiled for. If it doesn’t match, users may get worse performance, but they could switch to a build that matches. This ensures that we have portable code.

I’m not aware of anywhere currently that we would benefit from detecting that the current CPU is different from what we were compiled for. Hypothetical cases exist, sure, but _not_ adding the API doesn’t prevent us from adding it in the future (or make it more complex in the future).

---

<div class="post-metadata">

**Author:** ![ruang](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ruang/32/22210_2.png) [@ruang](https://discuss.python.org/u/ruang)\
**Post date:** [September 27, 2024, 11:54am UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/11 "2024-09-27T11:54:57Z")

</div>

It seems that it would be better as a macro, because it can be passed as an optional parameter to `config.h`.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [September 27, 2024, 12:17pm UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/12 "2024-09-27T12:17:10Z")

</div>

> [@ruang](#):
>
> Maybe, but this post is just to collect responses from developers. There may be the same developers who may need this API.

Feature checking is a design that is frequently used as your proposed API leads to maintenance issues in code that use it.

You would use answers from your proposed API to imply the availability of features. But history shows this is an error prone method to determine if a feature is available.

---

<div class="post-metadata">

**Author:** ![ruang](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ruang/32/22210_2.png) [@ruang](https://discuss.python.org/u/ruang)\
**Post date:** [September 27, 2024, 12:26pm UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/13 "2024-09-27T12:26:46Z")

</div>

Yes! Here, I’ve gotten confirmation that I should probably try getting the value of `sys.winver` instead of using other method.

---

<div class="post-metadata">

**Author:** ![ruang](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ruang/32/22210_2.png) [@ruang](https://discuss.python.org/u/ruang)\
**Post date:** [September 27, 2024, 12:28pm UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/14 "2024-09-27T12:28:12Z")

</div>

I know what adding this API entails, but it seems like it could provide a temporary solution to a specific issue. (So this post is about whether it’s worth adding the API, not about whether the action is correct).

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [September 27, 2024, 12:33pm UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/15 "2024-09-27T12:33:12Z")

</div>

> [@ruang](#):
>
> So this post is about whether it’s worth adding the API, not about whether the action is correct

I’m confused. are you saying this is a bad idea to add this API but you want it anyway?

---

<div class="post-metadata">

**Author:** ![ruang](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ruang/32/22210_2.png) [@ruang](https://discuss.python.org/u/ruang)\
**Post date:** [September 27, 2024, 12:49pm UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/16 "2024-09-27T12:49:09Z")

</div>

I think it could work for getting the architecture when the program runs, instead of when it’s compiled. But it doesn’t seem to work well with CPython. Hmm, it looks like it’s only good for solving my specific issue and not something I want to put out there publicly.

---

<div class="post-metadata">

**Author:** ![ruang](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ruang/32/22210_2.png) [@ruang](https://discuss.python.org/u/ruang)\
**Post date:** [October 17, 2024, 8:22am UTC](https://discuss.python.org/t/regarding-whether-we-should-add-py-currentarch-or-py-archname-function/65191/17 "2024-10-17T08:22:31Z")

</div>

I’m sorry, this discussion also needs to change to should an `arch` attribute be added to **system.implementation** to describe the architecture of the current Python runtime (it only works on Win)?  
@zooba is the only one who supports this. Maybe I need to listen to more voices.
