par_test.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright 2018 Google Inc. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import sys
  16. # This file checks the visible python state against expected values when run
  17. # inside a hermetic par file.
  18. failed = False
  19. def assert_equal(what, a, b):
  20. global failed
  21. if a != b:
  22. print("Expected %s('%s') == '%s'" % (what, a, b))
  23. failed = True
  24. archive = sys.modules["__main__"].__loader__.archive
  25. assert_equal("__name__", __name__, "testpkg.par_test")
  26. fileName = __file__
  27. if fileName.endswith('.pyc'):
  28. fileName = fileName[:-1]
  29. assert_equal("__file__", fileName, os.path.join(archive, "testpkg/par_test.py"))
  30. # Python3 is returning None here for me, and I haven't found any problems caused by this.
  31. if sys.version_info[0] == 2:
  32. assert_equal("__package__", __package__, "testpkg")
  33. else:
  34. assert_equal("__package__", __package__, None)
  35. assert_equal("__loader__.archive", __loader__.archive, archive)
  36. assert_equal("__loader__.prefix", __loader__.prefix, "testpkg/")
  37. if failed:
  38. sys.exit(1)