2
0

msvc.patch 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. diff --git a/python/types.c b/python/types.c
  2. --- a/python/types.c
  3. +++ b/python/types.c
  4. @@ -21,16 +21,66 @@
  5. #if PY_MAJOR_VERSION >= 3
  6. #include <stdio.h>
  7. +#ifdef _WIN32
  8. +#include <io.h>
  9. +#else
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. +#endif
  13. FILE *
  14. libxml_PyFileGet(PyObject *f) {
  15. - int fd, flags;
  16. + int fd;
  17. FILE *res;
  18. const char *mode;
  19. fd = PyObject_AsFileDescriptor(f);
  20. +#ifdef _WIN32
  21. + PyObject *modeObj, *modeBytes;
  22. +
  23. + modeObj = PyObject_GetAttrString(f, "mode");
  24. + if (!modeObj)
  25. + return(NULL);
  26. +
  27. + /* using f.mode to replace posix fcntl(), see
  28. + * https://gist.github.com/novocaine/09d5c00e67fd0aa13cfc */
  29. + if (PyUnicode_Check(modeObj)) {
  30. + modeBytes = PyObject_CallMethod(
  31. + modeObj, "encode", "ascii", "namereplace");
  32. + if (!modeBytes) {
  33. + Py_DECREF(modeObj);
  34. + return(NULL);
  35. + }
  36. + mode = PyBytes_AsString(modeBytes);
  37. + if (!mode) {
  38. + Py_DECREF(modeObj);
  39. + return(NULL);
  40. + }
  41. + Py_DECREF(modeBytes);
  42. + } else if (PyBytes_Check(modeObj)) {
  43. + mode = PyBytes_AsString(modeObj);
  44. + if (!mode) {
  45. + Py_DECREF(modeObj);
  46. + return(NULL);
  47. + }
  48. + } else {
  49. + Py_DECREF(modeObj);
  50. + return(NULL);
  51. + }
  52. +
  53. + Py_DECREF(modeObj);
  54. +
  55. + fd = _dup(fd);
  56. + if (fd == -1)
  57. + return(NULL);
  58. + res = _fdopen(fd, mode);
  59. + if (!res) {
  60. + _close(fd);
  61. + return(NULL);
  62. + }
  63. + return(res);
  64. +#else
  65. + int flags;
  66. /*
  67. * Get the flags on the fd to understand how it was opened
  68. */
  69. @@ -76,6 +126,7 @@ libxml_PyFileGet(PyObject *f) {
  70. return(NULL);
  71. }
  72. return(res);
  73. +#endif
  74. }
  75. void libxml_PyFileRelease(FILE *f) {