globPattern_ext.cxx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Filename: globPattern_ext.cxx
  2. // Created by: rdb (17Sep14)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #include "globPattern_ext.h"
  15. #ifdef HAVE_PYTHON
  16. ////////////////////////////////////////////////////////////////////
  17. // Function: Extension<GlobPattern>::match_files
  18. // Access: Published
  19. // Description: This variant on match_files returns a Python list
  20. // of strings.
  21. ////////////////////////////////////////////////////////////////////
  22. PyObject *Extension<GlobPattern>::
  23. match_files(const Filename &cwd) const {
  24. vector_string contents;
  25. _this->match_files(contents, cwd);
  26. PyObject *result = PyList_New(contents.size());
  27. for (size_t i = 0; i < contents.size(); ++i) {
  28. const string &filename = contents[i];
  29. #if PY_MAJOR_VERSION >= 3
  30. // This function expects UTF-8.
  31. PyObject *str = PyUnicode_FromStringAndSize(filename.data(), filename.size());
  32. #else
  33. PyObject *str = PyString_FromStringAndSize(filename.data(), filename.size());
  34. #endif
  35. PyList_SET_ITEM(result, i, str);
  36. }
  37. return result;
  38. }
  39. #endif // HAVE_PYTHON