-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PyLong_FromInt64() and PyLong_AsInt64() #32
Comments
There's nothing particularly controversial about this API, right? Other than the question of whether it should be "real" or macros? I'm quite happy with it being a real API. |
Since Otherwise we can end with two sets of converters with opposite return conventions. It will be very errorprone. |
I don't think that this use case is common enough to not follow the C API Guidelines: For converters, I would prefer to add support for int32_t and uint64_t to Argument Clinic and reuse these functions. |
It's tempting, for sure, but I think we're better to keep converters as the exception. We really want developers to have a single mental model for how to handle return values, and that unfortunately means converters need to be clearly labelled, and not just convenient functions. Adding some form of sized version of |
Thank you for humoring me on the macros thing; I think it's clear we want real functions :)
Yes. We settled on using -1 (or NULL) for errors. Unfortunately some existing functions -- or even families of functions -- don't do that; we have to live with that. Eventually, if we get to the point where |
@serhiy-storchaka has some concerns about calling
IMO it's good that signed and unsigned functions have the same behavior, that both call The GIL issue can be documented. |
@serhiy-storchaka @mdboom: Do you have any additional comment about these APIs, or can I just open a vote? |
I have nothing to add that hasn't already been said. |
My comment was about the existing C API like We need a C API which respects special methods like
On other hand, the lack of |
Right, this is essentially the difference between the abstract and concrete APIs, a difference which has been eroding over the last few years. If we think we can recover it, then we could argue the caller should Alternatively, if we don't expect to shut the door to concrete APIs operating abstractly, then we could document that if I'm +0 on the latter, just out of pragmatism. But it saddens me to lose the clear abstract/concrete separation. (It will make those who want our API to be strongly typed sadder than me, though. I like it being largely abstract, I just think we ought to know which APIs are which.) |
@serhiy-storchaka: It's uneasy for me to understand what do you propose.
So are you ok with PyLong_ToInt64() calling
Do you suggest to not add them and use PyLong_FromInt64() and PyLong_FromUInt64()? That sounds reasonable.
I didn't want to add too many functions at the beginning, so I started with the two most common sizes: 32 and 64 bits. We can add more functions later, once we agree on the design. |
I agree that PyLong_Check(), or even PyLong_CheckExact(), is a good way to check if an argument is an integer. In general, users don't care much if it's exactly an integer or not (if the object type is Python int). For example, I'm used to pass a boolean when an integer is expected (bool inherits from int), even if a boolean is a different concept than an integer (to me at least). |
Right, but the case here is when the user cares a lot that the GIL is not released during their I suspect we could probably get away with The three main questions for me are:
Footnotes
|
Why |
Yes, this was my idea. It was discussed earlier, but I lose previous rounds (perhaps I was not persistent enough). It affects more than the C API, so it should be discussed separately. Where is the best place for this? We need not only the C API Workgroup.
I am OK with calling
Do you have a use case for
Just keep in mind that there is a need for such features. |
Is it possible to have it documented anyway? I've no clue of why calling |
Either the api-evolution repository would be good, if we want to develop a "rule", though you'd have to announce it on a relevant issue to get people's attention. Or I'm sure we can find a bug related to implicit
Convenience, discovery, and pairing with the other API is a totally fine reason. (It may not be justified every time, but it's an okay reason.) We're looking at API development on a long scale (10+ years), and the goal is to have the right people (those who will benefit from it) using the right API in the right way. Making it easy to discover which APIs you should be using (and making it hard to discover the ones you shouldn't be using) is a big part of this, and the final decision/guess on how to do that is the WG's responsibility.
Yes, documentation is cheap, at least for the APIs that already do this and won't be changing. It's worth discussing for any new APIs though. |
As functions such as PyLong_AsLong() return the value directly, but can also return -1 on error and so PyErr_Occurred() should be checked. long value = PyLong_AsLong(obj);
if (value == -1 && PyErr_Occurred()) { /* error */ } vs int64_t value;
if (PyLong_ToInt64(obj, &value) < 0) { /* error */ } |
My initial motivation to propose these new |
Not all The current |
Currently it looks like a legend about Newton and a cat. He made a hole in the door to allow the cat to enter or leave without taking him away from his studies. When she brought in 5 kittens, he made 5 smaller holes for the kittens. All cases with |
I'm not sure we'll ever again see another programming language that won't allow implicitly upcasting integers (like Ada), so yeah, there's probably not a lot gained. If
We should probably figure out what is a defining trait. I have this vague idea that |
The size of |
True, and that matters most for cross-language callers, who probably need to rely on the exact size. So |
@serhiy-storchaka @zooba: Would you feel better if PyLong_ToInt64() and similar functions don't call the |
I think that the new C API should calls If Rust needs I am going to add a full set of converters compatible with |
Regarding naming, I still think we use |
Only very slightly. I'd feel better overall if it's clearly documented (and if those people working on documenting threading guarantees are made aware that int conversions are not thread-safe1). Footnotes
|
Updates:
|
It seems like there is an overall agreement on the proposed API. Let me check with a vote: |
I am sorry, how to vote? |
If you agree, check your checkbox. If you disagree, add a message :-) |
It seems that checking the checkbox does not have permanent effect. In any case I disagree.
I am also not happy with incompatibility with |
One of the admins here needs to add Serhiy (and Mike) as an org member.
What conflict? We don't have any fixed size
I missed the incompatibility. Last I saw you proposed to also add new converters for fixed-width ints? That's great, I'll be happy to see it, but it doesn't change anything about these APIs. |
All
It is not great to have two functions which do the same, but one of them returns 0 on success and other returns 0 on failure. |
This is a valid point. So I guess one purpose of the vote is to decide whether we as a whole think that consistency (with APIs that we consider a bad design) outweighs introducing the preferred pattern. I would be happy either way, but only between these two options.
Converters are the exception here. If we could design them again, they'd be made consistent. But these new functions are not intended to be converters, so they don't have to follow that pattern. We can insist on documenting that while they may look like converters, they're not suitable, but I suspect that pattern is obscure enough that it won't come up. Especially if we were to add format characters to |
Serhiy:
You don't know that; you're of course free to say that you believe it will be confusing, though. I don't believe it will be confusing; it should be sufficient to provide good documentation. |
Sorry for the delay, +1 from me. The
We should, however, stick with the precedent we establish here. |
@zooba prefers to add them: #32 (comment) and #32 (comment) @erlend-aasland also wants to add them: #32 (comment)
Not according to @erlend-aasland: #32 (comment) |
Ping @mdboom for the vote. |
@serhiy-storchaka: Did you see my previous comment about PyLong_FromInt32() and As/To naming? The function can only be added if all C API Working Group members approve the API, but you're against it because of its name. Is the function name a blocker issue for you? Would you mind to elaborate? |
Is the rule so draconic? Then I forced to vote for this feature even if I do not like a part of it. But I do not want to stay on the way of more useful part of this. |
I updated PR python/cpython#120390 to the approved API. |
I closed the decision issue since the vote reached its majority and I implemented the feature. Thanks for the constructive discussion. It was useful to discuss the |
Add 8 new functions to the limited C API to convert C <stdint.h> numbers from/to Python int:
UPDATE 1: I removed PyLong_FromInt32() and PyLong_FromUInt32() functions.
UPDATE 2: I renamed
To
functions toAs
functions.UPDATE 3: I added back PyLong_FromInt32() and PyLong_FromUInt32() functions for 32-bit systems.
As
functions callobj.__index__()
if obj is not a Python int.As
functions converting to unsigned integer raises ValueError if value is negativeAs
functions raise OverflowError if the value is too big (doesn't fit into the C integer range)Compared to
PyLong_AsLong()
,As
function return value is status (success or error, 0 or -1) and the result is set in the value argument (on success). It avoids having to call PyErr_Occurred() to distinguish -1 (success) and -1 (error). See Problem #1 for details.Links:
The text was updated successfully, but these errors were encountered: