compat.pxi 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. import sys
  18. def encode_file_path(path):
  19. if isinstance(path, str):
  20. # POSIX systems can handle utf-8. UTF8 is converted to utf16-le in
  21. # libarrow
  22. encoded_path = path.encode('utf-8')
  23. else:
  24. encoded_path = path
  25. # Windows file system requires utf-16le for file names; Arrow C++ libraries
  26. # will convert utf8 to utf16
  27. return encoded_path
  28. if sys.version_info >= (3, 7):
  29. # Starting with Python 3.7, dicts are guaranteed to be insertion-ordered.
  30. ordered_dict = dict
  31. else:
  32. import collections
  33. ordered_dict = collections.OrderedDict
  34. try:
  35. import pickle5 as builtin_pickle
  36. except ImportError:
  37. import pickle as builtin_pickle
  38. try:
  39. import cloudpickle as pickle
  40. except ImportError:
  41. pickle = builtin_pickle
  42. def tobytes(o):
  43. if isinstance(o, str):
  44. return o.encode('utf8')
  45. else:
  46. return o
  47. def frombytes(o, *, safe=False):
  48. if safe:
  49. return o.decode('utf8', errors='replace')
  50. else:
  51. return o.decode('utf8')