python3.diff 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. From: Till Kamppeter <till.kamppeter@gmail.com>
  2. Date: Fri, 22 Jul 2016 09:33:04 +0200
  3. Subject: Workaround patch for missing Python3 transition of the old
  4. (pre-USB-storage) photo memory card support (pcardext) as this part builds
  5. in Python3 environments but with pointer-related warnings which are fatal
  6. errors for Ubuntu's build servers. The patch silences the warnings but the
  7. memory card support is dropped in Python3 environments. This patch is
  8. supplied by the HPLIP upstream developers and will be replaced by a more
  9. proper solution in the next upstream release of HPLIP (see LP: #1275353)
  10. ---
  11. pcard/pcardext/pcardext.c | 59 +++++++++++++++++++++++++++++++++++++----------
  12. pcard/photocard.py | 2 +-
  13. unload.py | 5 ++++
  14. 3 files changed, 53 insertions(+), 13 deletions(-)
  15. diff --git a/pcard/pcardext/pcardext.c b/pcard/pcardext/pcardext.c
  16. index c1a8273..37d979b 100644
  17. --- a/pcard/pcardext/pcardext.c
  18. +++ b/pcard/pcardext/pcardext.c
  19. @@ -20,7 +20,7 @@ pcardext - Python extension for HP photocard services
  20. Requires:
  21. Python 2.2+
  22. -Author: Don Welch
  23. +Author: Don Welch
  24. \*****************************************************************************/
  25. @@ -38,9 +38,37 @@ typedef int Py_ssize_t;
  26. int verbose=0;
  27. +#if PY_MAJOR_VERSION >= 3
  28. + #define MOD_ERROR_VAL NULL
  29. + #define MOD_SUCCESS_VAL(val) val
  30. + #define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
  31. + #define PyInt_AS_LONG PyLong_AS_LONG
  32. + #define MOD_DEF(ob, name, doc, methods) \
  33. + static struct PyModuleDef moduledef = { \
  34. + PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
  35. + ob = PyModule_Create(&moduledef);
  36. +
  37. +
  38. + #define PY_String_Bytes PyBytes_FromStringAndSize
  39. + #define PY_AsString_Bytes PyBytes_AsStringAndSize
  40. +
  41. +#else
  42. + #define MOD_ERROR_VAL
  43. + #define MOD_SUCCESS_VAL(val)
  44. + #define MOD_INIT(name) void init##name(void)
  45. + #define MOD_DEF(ob, name, doc, methods) \
  46. + ob = Py_InitModule3(name, methods, doc);
  47. +
  48. + #define PY_String_Bytes PyString_FromStringAndSize
  49. + #define PY_AsString_Bytes PyString_AsStringAndSize
  50. +
  51. +#endif
  52. +
  53. PyObject * readsectorFunc = NULL;
  54. PyObject * writesectorFunc = NULL;
  55. +
  56. +
  57. int ReadSector(int sector, int nsector, void *buf, int size)
  58. {
  59. PyObject * result;
  60. @@ -56,9 +84,13 @@ int ReadSector(int sector, int nsector, void *buf, int size)
  61. if( result )
  62. {
  63. Py_ssize_t len = 0;
  64. - PyString_AsStringAndSize( result, &result_str, &len );
  65. +
  66. + //PyString_AsStringAndSize( result, &result_str, &len );
  67. + //PyBytes_AsStringAndSize( result, &result_str, &len );
  68. + PY_AsString_Bytes( result, &result_str, &len );
  69. - if( len < nsector*FAT_HARDSECT )
  70. +
  71. + if( len < nsector*FAT_HARDSECT )
  72. {
  73. goto abort;
  74. }
  75. @@ -205,7 +237,9 @@ PyObject * pcardext_read( PyObject * self, PyObject * args )
  76. if( FatReadFileExt( name, offset, len, buffer ) == len )
  77. {
  78. - return PyString_FromStringAndSize( (char *)buffer, len );
  79. + // return PyString_FromStringAndSize( (char *)buffer, len );
  80. + return PY_String_Bytes( (char *)buffer, len );
  81. + // return PyBytes_FromStringAndSize( (char *)buffer, len );
  82. }
  83. else
  84. {
  85. @@ -233,14 +267,15 @@ static PyMethodDef pcardext_methods[] =
  86. static char pcardext_documentation[] = "Python extension for HP photocard services";
  87. -void initpcardext( void )
  88. -{
  89. - PyObject * mod = Py_InitModule4( "pcardext", pcardext_methods,
  90. - pcardext_documentation, (PyObject*)NULL,
  91. - PYTHON_API_VERSION );
  92. -
  93. - if (mod == NULL)
  94. - return;
  95. +MOD_INIT(pcardext) {
  96. +
  97. + PyObject* mod ;
  98. + MOD_DEF(mod, "pcardext", pcardext_documentation, pcardext_methods);
  99. + if (mod == NULL)
  100. + return MOD_ERROR_VAL;
  101. +
  102. + return MOD_SUCCESS_VAL(mod);
  103. +
  104. }
  105. diff --git a/unload.py b/unload.py
  106. index 3fdd5a3..ce8b069 100755
  107. --- a/unload.py
  108. +++ b/unload.py
  109. @@ -44,6 +44,11 @@ except ImportError:
  110. # Local
  111. from base.g import *
  112. +from base.sixext import PY3
  113. +if PY3:
  114. + log.error("This functionality is not spported in python3 environment.")
  115. + sys.exit(1)
  116. +
  117. from base import device, utils, tui, module
  118. from prnt import cups