You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Argument Clinic: docstring_prototype output causes build failure on Windows #64522
There are a few viable alternatives that I can see.
Give PyDoc_VAR a reasonable default size. I don't like this; it seems fragile.
Remove docstring_prototype as an outputtable entity. I don't like this much either, it should be a reasonable thing to do.
Add a new PyDoc_SIZEDVAR macro, taking name and size. Argument Clinic will know the needed size and can fill it in, but it would be largely useless for manual usage. I think this is my preferred route.
Instead of reusing PyDoc_VAR (or a new PyDoc_SIZEDVAR), just have Clinic output the whole expanded macro, with size; e.g. "static char winsound_PlaySound__doc__[195]". This does have the downside that any future change to docstrings that would have been a simple change to the macro would have to be done in Argument Clinic, with lots of churn in every clinicized file.
Does your proposed solution work properly when docstrings are turned off? Try undefining WITH_DOC_STRINGS. I bet you need to make the size 1 in that case.
Also, the length must be len(f.docstring) + 1, to account for the trailing \0.
Also, error C2133 doesn't look like it's applicable. On line 71 it's not being stored in a structure. Dumb MSVC.
Also PyDoc_SIZEDVAR is a bad name. PyDoc_SIZED_VAR would be an improvement, but I think PyDoc_VAR_WITH_SIZE is the name you want there.
And, I'm still not a +1 on this approach. Can you propose something else?
Finally, I will note that the "buffer" preset would work just fine in this file, if you dumped the buffer just above the PyMethodDef array.
Why PyDoc_VAR prototype is used at all? There are only two occurrences of PyDoc_VAR in current code, and one of them actually can be replaced by PyDoc_STRVAR.
Serhiy: PyDoc_VAR is used in the "two-pass" approach as a forward declaration for docstrings. Imagine if, in winsound.c, sound_methods was defined above the "dump buffer" block. The expansion of WINSOUND_PLAYSOUND_METHODDEF would include a reference to winsound_PlaySound__doc__, which hadn't been defined yet.
winsound.c is simple enough, it doesn't need the two-pass approach. But two-pass would work well for _pickle.c, where there are four or five PyMethodDef structures in the middle of the file. Part of this is just Zachary experimenting with two-pass.
Does your proposed solution work properly when docstrings are turned
off? Try undefining WITH_DOC_STRINGS. I bet you need to make the
size 1 in that case.
For certain values of "properly", yes. It builds with no warnings or errors on Windows, but I don't know how it does on memory usage. It's easy enough to define PyDoc_VAR_WITH_SIZE alongside PyDoc_STR and use '1' instead of 'size' when WITH_DOC_STRINGS is undefined, though.
Also, the length must be len(f.docstring) + 1, to account for the
trailing \0.
I thought so as well, but testing both ways showed no difference. To provoke a difference, I defined length as len(f.docstring) - 1, which threw a compiler warning about an overflow. Adding 1 is probably safer, though.
Also, error C2133 doesn't look like it's applicable. On line 71 it's
not being stored in a structure. Dumb MSVC.
I can't argue that one :)
Also PyDoc_SIZEDVAR is a bad name. PyDoc_SIZED_VAR would be an
improvement, but I think PyDoc_VAR_WITH_SIZE is the name you want
there.
Fair enough.
And, I'm still not a +1 on this approach. Can you propose something
else?
I gave the 4 alternatives I could think of; I'll keep thinking and throw out anything else I come up with.
Finally, I will note that the "buffer" preset would work just fine in
this file, if you dumped the buffer just above the PyMethodDef array.
Indeed; winsound is just a convenient testbed since it's such a small module, but the problem will affect any module that can really benefit from the two-pass output system.
If in C you define
static char a[5] = "abcde"
C suppresses the trailing '\0'. That it continued to work okay was a lucky break--you must not have looked in many docstrings, or you lucked out and they happened to be padded with zeroes.
I had to throw it in a struct to prevent gcc from rearranging the variables. But this demonstrates the problem--when it prints the string, it doesn't stop at the end.
You need to leave static out of the forward definition and then add it in the initialization. The variable is not accessible from other object files - I checked.
"const char[]" and "static const char[]" should work the same, though I haven't tried it.
I thought the bug only happened with the "two-pass" preset. The "two-pass" preset is gone, because I broke it, and nobody was using it anyway, so I removed it.
(I don't even remember what "two-pass" was trying to do. It's possible the same effect could be achieved with the modern "every destination has an arbitrary number of separate numbered buffers you can put text into" technology.)
Activity
zware commentedon Jan 21, 2014
The current two-pass output for the two-pass preset causes compile errors on Windows. A sample:
..\PC\winsound.c(71): error C2133: 'winsound_PlaySound__doc__' : unknown size
Line 71 (clinic-generated docstring_prototype):
Here's a bit more info on the error: http://msdn.microsoft.com/en-us/library/c13wk277%28v=vs.100%29.aspx
There are a few viable alternatives that I can see.
Give PyDoc_VAR a reasonable default size. I don't like this; it seems fragile.
Remove docstring_prototype as an outputtable entity. I don't like this much either, it should be a reasonable thing to do.
Add a new PyDoc_SIZEDVAR macro, taking name and size. Argument Clinic will know the needed size and can fill it in, but it would be largely useless for manual usage. I think this is my preferred route.
Instead of reusing PyDoc_VAR (or a new PyDoc_SIZEDVAR), just have Clinic output the whole expanded macro, with size; e.g. "static char winsound_PlaySound__doc__[195]". This does have the downside that any future change to docstrings that would have been a simple change to the macro would have to be done in Argument Clinic, with lots of churn in every clinicized file.
Here's a patch that implements option 3.
larryhastings commentedon Jan 21, 2014
I don't understand the problem--you didn't give me enough context. Can you attach a file that demonstrates the problem?
zware commentedon Jan 21, 2014
Attached is PC/winsound.c, as converted in bpo-20172, but using the two-pass preset output scheme.
Compiling with this file produces these errors:
..\PC\winsound.c(71): error C2133: 'winsound_PlaySound__doc__' : unknown size [P:\ath\to\cpython\PCbuild\winsound.vcxproj]
..\PC\winsound.c(79): error C2133: 'winsound_Beep__doc__' : unknown size [P:\ath\to\cpython\PCbuild\winsound.vcxproj]
..\PC\winsound.c(87): error C2133: 'winsound_MessageBeep__doc__' : unknown size [P:\ath\to\cpython\PCbuild\winsound.vcxproj]
MSVC doesn't like declaring the unsized array without an asignment.
larryhastings commentedon Jan 21, 2014
Does your proposed solution work properly when docstrings are turned off? Try undefining WITH_DOC_STRINGS. I bet you need to make the size 1 in that case.
Also, the length must be len(f.docstring) + 1, to account for the trailing \0.
Also, error C2133 doesn't look like it's applicable. On line 71 it's not being stored in a structure. Dumb MSVC.
Also PyDoc_SIZEDVAR is a bad name. PyDoc_SIZED_VAR would be an improvement, but I think PyDoc_VAR_WITH_SIZE is the name you want there.
And, I'm still not a +1 on this approach. Can you propose something else?
Finally, I will note that the "buffer" preset would work just fine in this file, if you dumped the buffer just above the PyMethodDef array.
serhiy-storchaka commentedon Jan 21, 2014
Why PyDoc_VAR prototype is used at all? There are only two occurrences of PyDoc_VAR in current code, and one of them actually can be replaced by PyDoc_STRVAR.
larryhastings commentedon Jan 21, 2014
Serhiy: PyDoc_VAR is used in the "two-pass" approach as a forward declaration for docstrings. Imagine if, in winsound.c, sound_methods was defined above the "dump buffer" block. The expansion of WINSOUND_PLAYSOUND_METHODDEF would include a reference to winsound_PlaySound__doc__, which hadn't been defined yet.
winsound.c is simple enough, it doesn't need the two-pass approach. But two-pass would work well for _pickle.c, where there are four or five PyMethodDef structures in the middle of the file. Part of this is just Zachary experimenting with two-pass.
zware commentedon Jan 21, 2014
Larry Hastings wrote:
For certain values of "properly", yes. It builds with no warnings or errors on Windows, but I don't know how it does on memory usage. It's easy enough to define PyDoc_VAR_WITH_SIZE alongside PyDoc_STR and use '1' instead of 'size' when WITH_DOC_STRINGS is undefined, though.
I thought so as well, but testing both ways showed no difference. To provoke a difference, I defined length as len(f.docstring) - 1, which threw a compiler warning about an overflow. Adding 1 is probably safer, though.
I can't argue that one :)
Fair enough.
I gave the 4 alternatives I could think of; I'll keep thinking and throw out anything else I come up with.
Indeed; winsound is just a convenient testbed since it's such a small module, but the problem will affect any module that can really benefit from the two-pass output system.
larryhastings commentedon Jan 21, 2014
If in C you define
static char a[5] = "abcde"
C suppresses the trailing '\0'. That it continued to work okay was a lucky break--you must not have looked in many docstrings, or you lucked out and they happened to be padded with zeroes.
larryhastings commentedon Jan 21, 2014
I had to throw it in a struct to prevent gcc from rearranging the variables. But this demonstrates the problem--when it prints the string, it doesn't stop at the end.
-----
zware commentedon Aug 4, 2014
You're right, of course; I don't know how I got a non-screwball result in prior testing. Here's an updated patch.
I have not come up with any better alternative.
zooba commentedon Feb 25, 2015
Not sure how helpful this is, but the following code compiles fine for me (VC 12.0 and 14.0):
Any reason you can't define the forward definition like this? (It only seems to work with myStr[] and not *myStr, for some reason.)
larryhastings commentedon Feb 25, 2015
Perhaps, now that Guido allows us to redirect into a separate file, we should simply abandon the two-pass approach.
serhiy-storchaka commentedon Feb 25, 2015
You forgot "static" in the declaration Steve.
zware commentedon Feb 25, 2015
That may be for the best.
zware commentedon Feb 25, 2015
(My last comment was aimed at Larry's comment about abandoning two-pass...)
serhiy-storchaka commentedon Feb 25, 2015
What if add "const" in PyDoc_VAR?
zooba commentedon Feb 25, 2015
You need to leave static out of the forward definition and then add it in the initialization. The variable is not accessible from other object files - I checked.
"const char[]" and "static const char[]" should work the same, though I haven't tried it.
larryhastings commentedon Apr 22, 2015
Is this problem gone, now that Serhiy changed everything over to the "file" preset?
serhiy-storchaka commentedon Apr 22, 2015
The "file" preset is default now, but if you will consider Argument Clinic as general tool for use in third-party software, the bug still is here.
larryhastings commentedon Apr 22, 2015
I thought the bug only happened with the "two-pass" preset. The "two-pass" preset is gone, because I broke it, and nobody was using it anyway, so I removed it.
(I don't even remember what "two-pass" was trying to do. It's possible the same effect could be achieved with the modern "every destination has an arbitrary number of separate numbered buffers you can put text into" technology.)
serhiy-storchaka commentedon Apr 22, 2015
Ah, if the "two-pass" preset is gone, then of course the bug is gone too.
zware commentedon Sep 30, 2017
With two-pass gone, this is gone.