py_utils.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "py_utils.h"
  2. #include "py_cast.h"
  3. #include "py_errors.h"
  4. #include "py_gil.h"
  5. #include <util/generic/yexception.h>
  6. #include <util/string/split.h>
  7. #include <regex>
  8. namespace NPython {
  9. TPyObjectPtr PyRepr(TStringBuf asciiStr, bool intern) {
  10. for (auto c : asciiStr) {
  11. Y_ABORT_UNLESS((c&0x80) == 0, "expected ascii");
  12. }
  13. Py_ssize_t size = static_cast<Py_ssize_t>(asciiStr.size());
  14. #if PY_MAJOR_VERSION >= 3
  15. TPyObjectPtr pyStr = PyUnicode_FromStringAndSize(asciiStr.data(), size);
  16. #else
  17. TPyObjectPtr pyStr = PyString_FromStringAndSize(asciiStr.data(), size);
  18. #endif
  19. Y_ABORT_UNLESS(pyStr, "Can't get repr string");
  20. if (!intern) {
  21. return pyStr;
  22. }
  23. PyObject* tmp = pyStr.Release();
  24. #if PY_MAJOR_VERSION >= 3
  25. PyUnicode_InternInPlace(&tmp);
  26. #else
  27. PyString_InternInPlace(&tmp);
  28. #endif
  29. return TPyObjectPtr(tmp);
  30. }
  31. TString PyObjectRepr(PyObject* value) {
  32. static constexpr size_t maxLen = 1000;
  33. static constexpr std::string_view truncSuffix = "(truncated)";
  34. const TPyObjectPtr repr(PyObject_Repr(value));
  35. if (!repr) {
  36. return TString("repr error: ") + GetLastErrorAsString();
  37. }
  38. TString string;
  39. if (!TryPyCast(repr.Get(), string)) {
  40. string = "can't get repr as string";
  41. }
  42. if (string.size() > maxLen) {
  43. string.resize(maxLen - truncSuffix.size());
  44. string += truncSuffix;
  45. }
  46. return string;
  47. }
  48. bool HasEncodingCookie(const TString& source) {
  49. //
  50. // To define a source code encoding, a magic comment must be placed
  51. // into the source files either as first or second line in the file.
  52. //
  53. // See https://www.python.org/dev/peps/pep-0263 for more details.
  54. //
  55. static std::regex encodingRe(
  56. "^[ \\t\\v]*#.*?coding[:=][ \\t]*[-_.a-zA-Z0-9]+.*");
  57. int i = 0;
  58. for (const auto& it: StringSplitter(source).Split('\n')) {
  59. if (i++ == 2) break;
  60. TStringBuf line = it.Token();
  61. if (std::regex_match(line.begin(), line.end(), encodingRe)) {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. void PyCleanup() {
  68. TPyGilLocker lock;
  69. PyErr_Clear();
  70. PySys_SetObject("last_type", Py_None);
  71. PySys_SetObject("last_value", Py_None);
  72. PySys_SetObject("last_traceback", Py_None);
  73. }
  74. } // namspace NPython