classification
Title: Add PyComplex_FromString
Type: enhancement Stage: resolved
Components: C API Versions: Python 3.10
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: brandtbucher, mark.dickinson
Priority: normal Keywords: easy (C)

Created on 2021-03-02 19:49 by brandtbucher, last changed 2021-03-03 23:33 by brandtbucher. This issue is now closed.

Messages (3)
msg387958 - (view) Author: Brandt Bucher (brandtbucher) * (Python committer) Date: 2021-03-02 19:49
I recently came across a case where this functionality would be quite useful (parsing complex values from delimited text files). We have PyLong_FromString and PyFloat_FromString, but no PyComplex_FromString (I can't find a reason why it might have been deliberately omitted).

I *think* the best current workaround is to use sscanf to parse out two floats, then feed that to PyComplex_FromDoubles, which is non-trivial.

Do others support this addition? I imagine we would just use something similar to the _Py_string_to_number_with_underscores call at the end of complex_subtype_from_string in Objects/complexobject.c.
msg388015 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-03-03 12:29
I'd imagine "PyComplex_FromString" would be of limited utility in parsing complex data produced by something other than Python, because of Python's quirks with "j" rather than "i". But I guess you're talking about a case where the creation of the text file involved using the repr or str of Python complex values?

But I don't see any reason in principle why it shouldn't/couldn't be added.
msg388065 - (view) Author: Brandt Bucher (brandtbucher) * (Python committer) Date: 2021-03-03 23:33
Hm, I didn't realize until now that PyFloat_FromString parses a Python string, while PyLong_FromString parses a C string (with very different signatures). That's a bit annoying. 

Regardless, I misunderstood the original issue: in this particular case we are converting *Python* strings, so just doing the equivalent of a "complex(s)" call is fine.
History
Date User Action Args
2021-03-03 23:33:17brandtbuchersetstatus: open -> closed
resolution: rejected
messages: + msg388065

stage: resolved
2021-03-03 12:29:43mark.dickinsonsetnosy: + mark.dickinson
messages: + msg388015
2021-03-02 19:49:51brandtbuchercreate