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
Only check filenames when end with .py in _CalledFromGeneratedFile() #4262
Conversation
…for _CalledFromGeneratedFile()
Many thanks for creating this pull request. I have just tested it and unfortunately it doesn't fix the problem; I still get the error "Descriptors should not be created directly...". The problem is this piece of code earlier in that function:
Unfortunately, when I was debugging this before it hadn't occured to me to print this out in the stack frame. Here is a typical stack with everything being tested for, with Cython (first is the topmost level of the stack):
It seems to me that this check serves two purposes, so here are similar stacks from those two situations but WITHOUT Cython (where the check stands a chance of working): (1) The check finds where a
(2) The check stops people manually creating
So we can NOT change the check to So here are my suggestions:
Would that be reasonable? Thanks again for your efforts on this and really sorry my suggestions aren't in the form of a pull request. |
arthur-tacca@, Thanks for your investigate. But I am little confusing with what situation makes the Cython check fail. The first stack example with Cython is checking myprot_pb2.py (stacklevel = 0, global) which should be passing. Are you able to give a stack example when Cython check fail? Stack frame for '_CalledFromGeneratedFile, stacklevel = 1' with Cython would help |
Ugh, I'm really sorry. The text in my comment was correct, but that first stack trace was wrong – I must have pasted in the wrong one. That stack trace is where NOTHING is Cythoned! There is a top-level script Here are stack traces from that should work but fail. They come from a program run with a single import of a
|
Thanks for the updated stack traces. I've changed the PR, can you help to test and review? |
I notice that instead of removing the global check, you have kept it in, but moved after the check that the filename ends in I have tested your most recent commit (7b44b6d) with a Cythoned |
Hi haberman@, |
@@ -123,6 +119,10 @@ bool _CalledFromGeneratedFile(int stacklevel) { | |||
PyErr_Clear(); | |||
return false; | |||
} | |||
if ((filename_size < 3) || (strcmp(&filename[filename_size - 3], ".py") != 0)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use C++ string manipulation instead? strcmp()
scares me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original code is using strcmp. I also searched the code base, strcmp has been used several times. If we want to use string manipulation instead, I'd prefer to in another change.
LGTM. This check is only to prevent surprises, so that descriptors appear immutable. |
…rotocolbuffers#4262) * Cython's stack does not have .py file name. Only check filenames when end with .py for _CalledFromGeneratedFile()
Cython is not using .py file. Only check filenames when end with .py in _CalledFromGeneratedFile()
Fixes #2896