Skip to content
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

Merged
merged 5 commits into from Feb 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions python/google/protobuf/pyext/descriptor.cc
Expand Up @@ -107,10 +107,6 @@ bool _CalledFromGeneratedFile(int stacklevel) {
return false;
}
}
if (frame->f_globals != frame->f_locals) {
// Not at global module scope
return false;
}

if (frame->f_code->co_filename == NULL) {
return false;
Expand All @@ -123,6 +119,10 @@ bool _CalledFromGeneratedFile(int stacklevel) {
PyErr_Clear();
return false;
}
if ((filename_size < 3) || (strcmp(&filename[filename_size - 3], ".py") != 0)) {
Copy link
Member

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.

Copy link
Contributor Author

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.

// Cython's stack does not have .py file name and is not at global module scope.
return true;
}
if (filename_size < 7) {
// filename is too short.
return false;
Expand All @@ -131,6 +131,11 @@ bool _CalledFromGeneratedFile(int stacklevel) {
// Filename is not ending with _pb2.
return false;
}

if (frame->f_globals != frame->f_locals) {
// Not at global module scope
return false;
}
#endif
return true;
}
Expand Down