PyGreSQL 6.2.1__tar.gz → 6.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. {pygresql-6.2.1 → pygresql-6.2.2}/LICENSE.txt +1 -1
  2. {pygresql-6.2.1 → pygresql-6.2.2}/PKG-INFO +1 -1
  3. {pygresql-6.2.1 → pygresql-6.2.2}/PyGreSQL.egg-info/PKG-INFO +1 -1
  4. {pygresql-6.2.1 → pygresql-6.2.2}/docs/about.rst +1 -1
  5. {pygresql-6.2.1 → pygresql-6.2.2}/docs/conf.py +1 -1
  6. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/changelog.rst +6 -0
  7. {pygresql-6.2.1 → pygresql-6.2.2}/docs/copyright.rst +1 -1
  8. {pygresql-6.2.1 → pygresql-6.2.2}/ext/pgconn.c +86 -112
  9. {pygresql-6.2.1 → pygresql-6.2.2}/ext/pginternal.c +70 -1
  10. {pygresql-6.2.1 → pygresql-6.2.2}/ext/pglarge.c +1 -1
  11. {pygresql-6.2.1 → pygresql-6.2.2}/ext/pgmodule.c +14 -3
  12. {pygresql-6.2.1 → pygresql-6.2.2}/ext/pgnotice.c +1 -1
  13. {pygresql-6.2.1 → pygresql-6.2.2}/ext/pgquery.c +2 -5
  14. {pygresql-6.2.1 → pygresql-6.2.2}/ext/pgsource.c +1 -1
  15. {pygresql-6.2.1 → pygresql-6.2.2}/pg/__init__.py +1 -1
  16. {pygresql-6.2.1 → pygresql-6.2.2}/pgdb/__init__.py +1 -1
  17. {pygresql-6.2.1 → pygresql-6.2.2}/pyproject.toml +1 -1
  18. {pygresql-6.2.1 → pygresql-6.2.2}/tests/test_classic_connection.py +4 -8
  19. {pygresql-6.2.1 → pygresql-6.2.2}/MANIFEST.in +0 -0
  20. {pygresql-6.2.1 → pygresql-6.2.2}/PyGreSQL.egg-info/SOURCES.txt +0 -0
  21. {pygresql-6.2.1 → pygresql-6.2.2}/PyGreSQL.egg-info/dependency_links.txt +0 -0
  22. {pygresql-6.2.1 → pygresql-6.2.2}/PyGreSQL.egg-info/not-zip-safe +0 -0
  23. {pygresql-6.2.1 → pygresql-6.2.2}/PyGreSQL.egg-info/top_level.txt +0 -0
  24. {pygresql-6.2.1 → pygresql-6.2.2}/README.rst +0 -0
  25. {pygresql-6.2.1 → pygresql-6.2.2}/docs/Makefile +0 -0
  26. {pygresql-6.2.1 → pygresql-6.2.2}/docs/_static/favicon.ico +0 -0
  27. {pygresql-6.2.1 → pygresql-6.2.2}/docs/_static/pygresql.png +0 -0
  28. {pygresql-6.2.1 → pygresql-6.2.2}/docs/community/homes.rst +0 -0
  29. {pygresql-6.2.1 → pygresql-6.2.2}/docs/community/index.rst +0 -0
  30. {pygresql-6.2.1 → pygresql-6.2.2}/docs/community/issues.rst +0 -0
  31. {pygresql-6.2.1 → pygresql-6.2.2}/docs/community/mailinglist.rst +0 -0
  32. {pygresql-6.2.1 → pygresql-6.2.2}/docs/community/source.rst +0 -0
  33. {pygresql-6.2.1 → pygresql-6.2.2}/docs/community/support.rst +0 -0
  34. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/examples.rst +0 -0
  35. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/general.rst +0 -0
  36. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/index.rst +0 -0
  37. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/install.rst +0 -0
  38. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pg/adaptation.rst +0 -0
  39. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pg/connection.rst +0 -0
  40. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pg/db_types.rst +0 -0
  41. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pg/db_wrapper.rst +0 -0
  42. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pg/index.rst +0 -0
  43. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pg/introduction.rst +0 -0
  44. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pg/large_objects.rst +0 -0
  45. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pg/module.rst +0 -0
  46. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pg/notification.rst +0 -0
  47. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pg/query.rst +0 -0
  48. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pgdb/adaptation.rst +0 -0
  49. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pgdb/connection.rst +0 -0
  50. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pgdb/cursor.rst +0 -0
  51. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pgdb/index.rst +0 -0
  52. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pgdb/introduction.rst +0 -0
  53. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pgdb/module.rst +0 -0
  54. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pgdb/typecache.rst +0 -0
  55. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/pgdb/types.rst +0 -0
  56. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/postgres/advanced.rst +0 -0
  57. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/postgres/basic.rst +0 -0
  58. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/postgres/func.rst +0 -0
  59. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/postgres/index.rst +0 -0
  60. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/postgres/syscat.rst +0 -0
  61. {pygresql-6.2.1 → pygresql-6.2.2}/docs/contents/tutorial.rst +0 -0
  62. {pygresql-6.2.1 → pygresql-6.2.2}/docs/download/download.rst +0 -0
  63. {pygresql-6.2.1 → pygresql-6.2.2}/docs/download/files.rst +0 -0
  64. {pygresql-6.2.1 → pygresql-6.2.2}/docs/download/index.rst +0 -0
  65. {pygresql-6.2.1 → pygresql-6.2.2}/docs/make.bat +0 -0
  66. {pygresql-6.2.1 → pygresql-6.2.2}/docs/requirements.txt +0 -0
  67. {pygresql-6.2.1 → pygresql-6.2.2}/ext/pgtypes.h +0 -0
  68. {pygresql-6.2.1 → pygresql-6.2.2}/pg/_pg.pyi +0 -0
  69. {pygresql-6.2.1 → pygresql-6.2.2}/pg/adapt.py +0 -0
  70. {pygresql-6.2.1 → pygresql-6.2.2}/pg/attrs.py +0 -0
  71. {pygresql-6.2.1 → pygresql-6.2.2}/pg/cast.py +0 -0
  72. {pygresql-6.2.1 → pygresql-6.2.2}/pg/core.py +0 -0
  73. {pygresql-6.2.1 → pygresql-6.2.2}/pg/db.py +0 -0
  74. {pygresql-6.2.1 → pygresql-6.2.2}/pg/error.py +0 -0
  75. {pygresql-6.2.1 → pygresql-6.2.2}/pg/helpers.py +0 -0
  76. {pygresql-6.2.1 → pygresql-6.2.2}/pg/notify.py +0 -0
  77. {pygresql-6.2.1 → pygresql-6.2.2}/pg/py.typed +0 -0
  78. {pygresql-6.2.1 → pygresql-6.2.2}/pg/tz.py +0 -0
  79. {pygresql-6.2.1 → pygresql-6.2.2}/pgdb/adapt.py +0 -0
  80. {pygresql-6.2.1 → pygresql-6.2.2}/pgdb/cast.py +0 -0
  81. {pygresql-6.2.1 → pygresql-6.2.2}/pgdb/connect.py +0 -0
  82. {pygresql-6.2.1 → pygresql-6.2.2}/pgdb/connection.py +0 -0
  83. {pygresql-6.2.1 → pygresql-6.2.2}/pgdb/constants.py +0 -0
  84. {pygresql-6.2.1 → pygresql-6.2.2}/pgdb/cursor.py +0 -0
  85. {pygresql-6.2.1 → pygresql-6.2.2}/pgdb/py.typed +0 -0
  86. {pygresql-6.2.1 → pygresql-6.2.2}/pgdb/typecode.py +0 -0
  87. {pygresql-6.2.1 → pygresql-6.2.2}/setup.cfg +0 -0
  88. {pygresql-6.2.1 → pygresql-6.2.2}/setup.py +0 -0
  89. {pygresql-6.2.1 → pygresql-6.2.2}/tests/__init__.py +0 -0
  90. {pygresql-6.2.1 → pygresql-6.2.2}/tests/config.py +0 -0
  91. {pygresql-6.2.1 → pygresql-6.2.2}/tests/dbapi20.py +0 -0
  92. {pygresql-6.2.1 → pygresql-6.2.2}/tests/test_classic.py +0 -0
  93. {pygresql-6.2.1 → pygresql-6.2.2}/tests/test_classic_attrdict.py +0 -0
  94. {pygresql-6.2.1 → pygresql-6.2.2}/tests/test_classic_dbwrapper.py +0 -0
  95. {pygresql-6.2.1 → pygresql-6.2.2}/tests/test_classic_functions.py +0 -0
  96. {pygresql-6.2.1 → pygresql-6.2.2}/tests/test_classic_largeobj.py +0 -0
  97. {pygresql-6.2.1 → pygresql-6.2.2}/tests/test_classic_notification.py +0 -0
  98. {pygresql-6.2.1 → pygresql-6.2.2}/tests/test_dbapi20.py +0 -0
  99. {pygresql-6.2.1 → pygresql-6.2.2}/tests/test_dbapi20_copy.py +0 -0
  100. {pygresql-6.2.1 → pygresql-6.2.2}/tests/test_tutorial.py +0 -0
  101. {pygresql-6.2.1 → pygresql-6.2.2}/tox.ini +0 -0
@@ -6,7 +6,7 @@ Copyright (c) 1995, Pascal Andre
6
6
 
7
7
  Further modifications copyright (c) 1997-2008 by D'Arcy J.M. Cain
8
8
 
9
- Further modifications copyright (c) 2009-2025 by the PyGreSQL Development Team
9
+ Further modifications copyright (c) 2009-2026 by the PyGreSQL Development Team
10
10
 
11
11
  PyGreSQL is released under the PostgreSQL License, a liberal Open Source
12
12
  license, similar to the BSD or MIT licenses:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyGreSQL
3
- Version: 6.2.1
3
+ Version: 6.2.2
4
4
  Summary: Python PostgreSQL interfaces
5
5
  Home-page: https://pygresql.github.io/
6
6
  Download-URL: https://pygresql.github.io/download/
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyGreSQL
3
- Version: 6.2.1
3
+ Version: 6.2.2
4
4
  Summary: Python PostgreSQL interfaces
5
5
  Home-page: https://pygresql.github.io/
6
6
  Download-URL: https://pygresql.github.io/download/
@@ -8,7 +8,7 @@ powerful PostgreSQL features from Python.
8
8
 
9
9
  | This software is copyright © 1995, Pascal Andre.
10
10
  | Further modifications are copyright © 1997-2008 by D'Arcy J.M. Cain.
11
- | Further modifications are copyright © 2009-2025 by the PyGreSQL team.
11
+ | Further modifications are copyright © 2009-2026 by the PyGreSQL team.
12
12
  | For licensing details, see the full :doc:`copyright`.
13
13
 
14
14
  **PostgreSQL** is a highly scalable, SQL compliant, open source
@@ -8,7 +8,7 @@
8
8
 
9
9
  project = 'PyGreSQL'
10
10
  author = 'The PyGreSQL team'
11
- copyright = '2025, ' + author
11
+ copyright = '2026, ' + author
12
12
 
13
13
  def project_version():
14
14
  with open('../pyproject.toml') as f:
@@ -1,6 +1,12 @@
1
1
  ChangeLog
2
2
  =========
3
3
 
4
+ Version 6.2.2 (2026-01-03)
5
+ --------------------------
6
+ - The `inserttable()` method in the `pg` module can now handle rows of
7
+ arbitrary size (this was previously limited to 64 KB) (#79, thanks
8
+ to Justin Pryzby for this contribution).
9
+
4
10
  Version 6.2.1 (2025-12-27)
5
11
  --------------------------
6
12
  - Made setup cleaner and work with a wider range of setuptools versions (#90).
@@ -10,7 +10,7 @@ Copyright (c) 1995, Pascal Andre
10
10
  Further modifications copyright (c) 1997-2008 by D'Arcy J.M. Cain
11
11
  (darcy@PyGreSQL.org)
12
12
 
13
- Further modifications copyright (c) 2009-2025 by the PyGreSQL team.
13
+ Further modifications copyright (c) 2009-2026 by the PyGreSQL team.
14
14
 
15
15
  Permission to use, copy, modify, and distribute this software and its
16
16
  documentation for any purpose, without fee, and without a written agreement
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * The connection object - this file is part a of the C extension module.
5
5
  *
6
- * Copyright (c) 2025 by the PyGreSQL Development Team
6
+ * Copyright (c) 2026 by the PyGreSQL Development Team
7
7
  *
8
8
  * Please see the LICENSE.TXT file for specific restrictions.
9
9
  */
@@ -532,6 +532,12 @@ conn_describe_prepared(connObject *self, PyObject *args)
532
532
  query_obj->max_row = PQntuples(result);
533
533
  query_obj->num_fields = PQnfields(result);
534
534
  query_obj->col_types = get_col_types(result, query_obj->num_fields);
535
+ if (!query_obj->col_types) {
536
+ PQclear(result);
537
+ Py_DECREF(query_obj);
538
+ Py_DECREF(self);
539
+ return NULL;
540
+ }
535
541
  return (PyObject *)query_obj;
536
542
  }
537
543
  set_error(ProgrammingError, "Cannot describe prepared statement",
@@ -716,9 +722,9 @@ static PyObject *
716
722
  conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
717
723
  {
718
724
  PGresult *result;
719
- char *table, *buffer, *bufpt, *bufmax, *s, *t;
725
+ char *table, *s, *t;
726
+ struct CharBuffer buffer = {0};
720
727
  int freeze = 0, encoding, ret;
721
- size_t bufsiz;
722
728
  PyObject *rows, *iter_row, *item, *columns = NULL;
723
729
  Py_ssize_t i, j, m, n;
724
730
 
@@ -775,18 +781,16 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
775
781
  n = -1; /* number of columns not yet known */
776
782
  }
777
783
 
778
- /* allocate buffer */
779
- if (!(buffer = PyMem_Malloc(MAX_BUFFER_SIZE))) {
784
+ encoding = PQclientEncoding(self->cnx);
785
+
786
+ /* pre-allocate some memory for the query buffer */
787
+ if (!init_char_buffer(&buffer, 4096)) {
780
788
  Py_DECREF(iter_row);
781
789
  return PyErr_NoMemory();
782
790
  }
783
791
 
784
- encoding = PQclientEncoding(self->cnx);
785
-
786
792
  /* starts query */
787
- bufpt = buffer;
788
- bufmax = bufpt + MAX_BUFFER_SIZE;
789
- bufpt += snprintf(bufpt, (size_t)(bufmax - bufpt), "copy ");
793
+ ext_char_buffer_s(&buffer, "copy ");
790
794
 
791
795
  s = table;
792
796
  do {
@@ -794,18 +798,16 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
794
798
  if (!t)
795
799
  t = s + strlen(s);
796
800
  table = PQescapeIdentifier(self->cnx, s, (size_t)(t - s));
797
- if (bufpt < bufmax)
798
- bufpt += snprintf(bufpt, (size_t)(bufmax - bufpt), "%s", table);
801
+ ext_char_buffer_s(&buffer, table);
799
802
  PQfreemem(table);
800
803
  s = t;
801
- if (*s && bufpt < bufmax)
802
- *bufpt++ = *s++;
804
+ if (*s)
805
+ ext_char_buffer_c(&buffer, *s++);
803
806
  } while (*s);
804
807
 
805
808
  if (columns) {
806
809
  /* adds a string like f" ({','.join(columns)})" */
807
- if (bufpt < bufmax)
808
- bufpt += snprintf(bufpt, (size_t)(bufmax - bufpt), " (");
810
+ ext_char_buffer_s(&buffer, " (");
809
811
  for (j = 0; j < n; ++j) {
810
812
  PyObject *obj = PySequence_Fast_GET_ITEM(columns, j);
811
813
  Py_ssize_t slen;
@@ -817,7 +819,7 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
817
819
  else if (PyUnicode_Check(obj)) {
818
820
  obj = get_encoded_string(obj, encoding);
819
821
  if (!obj) {
820
- PyMem_Free(buffer);
822
+ PyMem_Free(buffer.data);
821
823
  Py_DECREF(iter_row);
822
824
  return NULL; /* pass the UnicodeEncodeError */
823
825
  }
@@ -826,37 +828,35 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
826
828
  PyErr_SetString(
827
829
  PyExc_TypeError,
828
830
  "The third argument must contain only strings");
829
- PyMem_Free(buffer);
831
+ PyMem_Free(buffer.data);
830
832
  Py_DECREF(iter_row);
831
833
  return NULL;
832
834
  }
833
835
  PyBytes_AsStringAndSize(obj, &col, &slen);
834
836
  col = PQescapeIdentifier(self->cnx, col, (size_t)slen);
835
837
  Py_DECREF(obj);
836
- if (bufpt < bufmax)
837
- bufpt += snprintf(bufpt, (size_t)(bufmax - bufpt), "%s%s", col,
838
- j == n - 1 ? ")" : ",");
838
+ ext_char_buffer_s(&buffer, col);
839
839
  PQfreemem(col);
840
+ ext_char_buffer_c(&buffer, j == n - 1 ? ')' : ',');
840
841
  }
841
842
  }
842
- if (bufpt < bufmax)
843
- bufpt += snprintf(bufpt, (size_t)(bufmax - bufpt), " from stdin");
844
- if (freeze && bufpt < bufmax) {
845
- bufpt += snprintf(bufpt, (size_t)(bufmax - bufpt), " freeze");
846
- }
843
+ ext_char_buffer_s(&buffer, " from stdin");
844
+ if (freeze)
845
+ ext_char_buffer_s(&buffer, " freeze");
846
+ ext_char_buffer_c(&buffer, '\0');
847
847
 
848
- if (bufpt >= bufmax) {
849
- PyMem_Free(buffer);
848
+ if (buffer.error) {
849
+ PyMem_Free(buffer.data);
850
850
  Py_DECREF(iter_row);
851
851
  return PyErr_NoMemory();
852
852
  }
853
853
 
854
854
  Py_BEGIN_ALLOW_THREADS
855
- result = PQexec(self->cnx, buffer);
855
+ result = PQexec(self->cnx, buffer.data);
856
856
  Py_END_ALLOW_THREADS
857
857
 
858
858
  if (!result || PQresultStatus(result) != PGRES_COPY_IN) {
859
- PyMem_Free(buffer);
859
+ PyMem_Free(buffer.data);
860
860
  Py_DECREF(iter_row);
861
861
  PyErr_SetString(PyExc_ValueError, PQerrorMessage(self->cnx));
862
862
  return NULL;
@@ -871,8 +871,7 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
871
871
 
872
872
  if (!(PyTuple_Check(columns) || PyList_Check(columns))) {
873
873
  PQputCopyEnd(self->cnx, "Invalid arguments");
874
- PyMem_Free(buffer);
875
- Py_DECREF(columns);
874
+ PyMem_Free(buffer.data);
876
875
  Py_DECREF(columns);
877
876
  Py_DECREF(iter_row);
878
877
  PyErr_SetString(
@@ -887,7 +886,7 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
887
886
  }
888
887
  else if (j != n) {
889
888
  PQputCopyEnd(self->cnx, "Invalid arguments");
890
- PyMem_Free(buffer);
889
+ PyMem_Free(buffer.data);
891
890
  Py_DECREF(columns);
892
891
  Py_DECREF(iter_row);
893
892
  PyErr_SetString(
@@ -896,65 +895,54 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
896
895
  return NULL;
897
896
  }
898
897
 
899
- /* builds insert line */
900
- bufpt = buffer;
901
- bufsiz = MAX_BUFFER_SIZE - 1;
898
+ /* empty buffer while keeping allocated memory */
899
+ buffer.size = 0;
900
+
901
+ /* build insert line */
902
902
 
903
903
  for (j = 0; j < n; ++j) {
904
- if (j) {
905
- *bufpt++ = '\t';
906
- --bufsiz;
907
- }
904
+ if (j)
905
+ ext_char_buffer_c(&buffer, '\t');
908
906
 
909
907
  item = PySequence_Fast_GET_ITEM(columns, j);
910
908
 
911
909
  /* convert item to string and append to buffer */
912
910
  if (item == Py_None) {
913
- if (bufsiz > 2) {
914
- *bufpt++ = '\\';
915
- *bufpt++ = 'N';
916
- bufsiz -= 2;
917
- }
918
- else
919
- bufsiz = 0;
911
+ ext_char_buffer_c(&buffer, '\\');
912
+ ext_char_buffer_c(&buffer, 'N');
920
913
  }
921
914
  else if (PyBytes_Check(item)) {
922
915
  const char *t = PyBytes_AsString(item);
923
916
 
924
- while (*t && bufsiz) {
917
+ while (*t) {
925
918
  switch (*t) {
926
919
  case '\\':
927
- *bufpt++ = '\\';
928
- if (--bufsiz)
929
- *bufpt++ = '\\';
920
+ ext_char_buffer_c(&buffer, '\\');
921
+ ext_char_buffer_c(&buffer, '\\');
930
922
  break;
931
923
  case '\t':
932
- *bufpt++ = '\\';
933
- if (--bufsiz)
934
- *bufpt++ = 't';
924
+ ext_char_buffer_c(&buffer, '\\');
925
+ ext_char_buffer_c(&buffer, '\t');
935
926
  break;
936
927
  case '\r':
937
- *bufpt++ = '\\';
938
- if (--bufsiz)
939
- *bufpt++ = 'r';
928
+ ext_char_buffer_c(&buffer, '\\');
929
+ ext_char_buffer_c(&buffer, '\r');
940
930
  break;
941
931
  case '\n':
942
- *bufpt++ = '\\';
943
- if (--bufsiz)
944
- *bufpt++ = 'n';
932
+ ext_char_buffer_c(&buffer, '\\');
933
+ ext_char_buffer_c(&buffer, '\n');
945
934
  break;
946
935
  default:
947
- *bufpt++ = *t;
936
+ ext_char_buffer_c(&buffer, *t);
948
937
  }
949
938
  ++t;
950
- --bufsiz;
951
939
  }
952
940
  }
953
941
  else if (PyUnicode_Check(item)) {
954
942
  PyObject *s = get_encoded_string(item, encoding);
955
943
  if (!s) {
956
944
  PQputCopyEnd(self->cnx, "Encoding error");
957
- PyMem_Free(buffer);
945
+ PyMem_Free(buffer.data);
958
946
  Py_DECREF(item);
959
947
  Py_DECREF(columns);
960
948
  Py_DECREF(iter_row);
@@ -963,33 +951,28 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
963
951
  else {
964
952
  const char *t = PyBytes_AsString(s);
965
953
 
966
- while (*t && bufsiz) {
954
+ while (*t) {
967
955
  switch (*t) {
968
956
  case '\\':
969
- *bufpt++ = '\\';
970
- if (--bufsiz)
971
- *bufpt++ = '\\';
957
+ ext_char_buffer_c(&buffer, '\\');
958
+ ext_char_buffer_c(&buffer, '\\');
972
959
  break;
973
960
  case '\t':
974
- *bufpt++ = '\\';
975
- if (--bufsiz)
976
- *bufpt++ = 't';
961
+ ext_char_buffer_c(&buffer, '\\');
962
+ ext_char_buffer_c(&buffer, '\t');
977
963
  break;
978
964
  case '\r':
979
- *bufpt++ = '\\';
980
- if (--bufsiz)
981
- *bufpt++ = 'r';
965
+ ext_char_buffer_c(&buffer, '\\');
966
+ ext_char_buffer_c(&buffer, '\r');
982
967
  break;
983
968
  case '\n':
984
- *bufpt++ = '\\';
985
- if (--bufsiz)
986
- *bufpt++ = 'n';
969
+ ext_char_buffer_c(&buffer, '\\');
970
+ ext_char_buffer_c(&buffer, '\n');
987
971
  break;
988
972
  default:
989
- *bufpt++ = *t;
973
+ ext_char_buffer_c(&buffer, *t);
990
974
  }
991
975
  ++t;
992
- --bufsiz;
993
976
  }
994
977
  Py_DECREF(s);
995
978
  }
@@ -998,68 +981,59 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
998
981
  PyObject *s = PyObject_Str(item);
999
982
  const char *t = PyUnicode_AsUTF8(s);
1000
983
 
1001
- while (*t && bufsiz) {
1002
- *bufpt++ = *t++;
1003
- --bufsiz;
1004
- }
984
+ ext_char_buffer_s(&buffer, t);
1005
985
  Py_DECREF(s);
1006
986
  }
1007
987
  else {
1008
988
  PyObject *s = PyObject_Repr(item);
1009
989
  const char *t = PyUnicode_AsUTF8(s);
1010
990
 
1011
- while (*t && bufsiz) {
991
+ while (*t) {
1012
992
  switch (*t) {
1013
993
  case '\\':
1014
- *bufpt++ = '\\';
1015
- if (--bufsiz)
1016
- *bufpt++ = '\\';
994
+ ext_char_buffer_c(&buffer, '\\');
995
+ ext_char_buffer_c(&buffer, '\\');
1017
996
  break;
1018
997
  case '\t':
1019
- *bufpt++ = '\\';
1020
- if (--bufsiz)
1021
- *bufpt++ = 't';
998
+ ext_char_buffer_c(&buffer, '\\');
999
+ ext_char_buffer_c(&buffer, '\t');
1022
1000
  break;
1023
1001
  case '\r':
1024
- *bufpt++ = '\\';
1025
- if (--bufsiz)
1026
- *bufpt++ = 'r';
1002
+ ext_char_buffer_c(&buffer, '\\');
1003
+ ext_char_buffer_c(&buffer, '\r');
1027
1004
  break;
1028
1005
  case '\n':
1029
- *bufpt++ = '\\';
1030
- if (--bufsiz)
1031
- *bufpt++ = 'n';
1006
+ ext_char_buffer_c(&buffer, '\\');
1007
+ ext_char_buffer_c(&buffer, '\n');
1032
1008
  break;
1033
1009
  default:
1034
- *bufpt++ = *t;
1010
+ ext_char_buffer_c(&buffer, *t);
1035
1011
  }
1036
1012
  ++t;
1037
- --bufsiz;
1038
1013
  }
1039
1014
  Py_DECREF(s);
1040
1015
  }
1041
-
1042
- if (bufsiz <= 0) {
1043
- PQputCopyEnd(self->cnx, "Memory error");
1044
- PyMem_Free(buffer);
1045
- Py_DECREF(columns);
1046
- Py_DECREF(iter_row);
1047
- return PyErr_NoMemory();
1048
- }
1049
1016
  }
1050
1017
 
1051
1018
  Py_DECREF(columns);
1052
1019
 
1053
- *bufpt++ = '\n';
1020
+ /* terminate line */
1021
+ ext_char_buffer_c(&buffer, '\n');
1022
+ if (buffer.error) {
1023
+ PQputCopyEnd(self->cnx, "Memory error");
1024
+ PyMem_Free(buffer.data);
1025
+ Py_DECREF(iter_row);
1026
+ return PyErr_NoMemory();
1027
+ }
1054
1028
 
1055
- /* sends data */
1056
- ret = PQputCopyData(self->cnx, buffer, (int)(bufpt - buffer));
1029
+ /* send data */
1030
+ ret = PQputCopyData(self->cnx, buffer.data, (int)buffer.size);
1057
1031
  if (ret != 1) {
1058
1032
  char *errormsg = ret == -1 ? PQerrorMessage(self->cnx)
1059
1033
  : "Data cannot be queued";
1060
1034
  PyErr_SetString(PyExc_IOError, errormsg);
1061
1035
  PQputCopyEnd(self->cnx, errormsg);
1062
- PyMem_Free(buffer);
1036
+ PyMem_Free(buffer.data);
1063
1037
  Py_DECREF(iter_row);
1064
1038
  return NULL;
1065
1039
  }
@@ -1067,7 +1041,7 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
1067
1041
 
1068
1042
  Py_DECREF(iter_row);
1069
1043
  if (PyErr_Occurred()) {
1070
- PyMem_Free(buffer);
1044
+ PyMem_Free(buffer.data);
1071
1045
  return NULL; /* pass the iteration error */
1072
1046
  }
1073
1047
 
@@ -1075,11 +1049,11 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
1075
1049
  if (ret != 1) {
1076
1050
  PyErr_SetString(PyExc_IOError, ret == -1 ? PQerrorMessage(self->cnx)
1077
1051
  : "Data cannot be queued");
1078
- PyMem_Free(buffer);
1052
+ PyMem_Free(buffer.data);
1079
1053
  return NULL;
1080
1054
  }
1081
1055
 
1082
- PyMem_Free(buffer);
1056
+ PyMem_Free(buffer.data);
1083
1057
 
1084
1058
  Py_BEGIN_ALLOW_THREADS
1085
1059
  result = PQgetResult(self->cnx);
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Internal functions - this file is part a of the C extension module.
5
5
  *
6
- * Copyright (c) 2025 by the PyGreSQL Development Team
6
+ * Copyright (c) 2026 by the PyGreSQL Development Team
7
7
  *
8
8
  * Please see the LICENSE.TXT file for specific restrictions.
9
9
  */
@@ -1493,3 +1493,72 @@ notice_receiver(void *arg, const PGresult *res)
1493
1493
  }
1494
1494
  PyGILState_Release(gstate);
1495
1495
  }
1496
+
1497
+ /* Pre-allocate some memory for a char buffer and return success status. */
1498
+ static int
1499
+ init_char_buffer(struct CharBuffer *buf, size_t initial_size)
1500
+ {
1501
+ buf->size = 0;
1502
+ buf->data = PyMem_Malloc(initial_size);
1503
+ if (buf->data) {
1504
+ buf->max_size = initial_size;
1505
+ buf->error = 0;
1506
+ }
1507
+ else {
1508
+ buf->max_size = 0;
1509
+ buf->error = 1;
1510
+ }
1511
+ return !buf->error;
1512
+ }
1513
+
1514
+ /* Extend char buffer with given string.
1515
+ Note: We do not assume or guarantee that the buffer is zero-terminated. */
1516
+ static void
1517
+ ext_char_buffer_s(struct CharBuffer *buf, const char *s)
1518
+ {
1519
+ size_t len = strlen(s), need;
1520
+
1521
+ if (!len || buf->error)
1522
+ return;
1523
+
1524
+ if ((need = buf->size + len) >= buf->max_size) {
1525
+ void *tmp;
1526
+
1527
+ if (buf->max_size < 1024 * 1024) {
1528
+ /* allocate powers of two unless it's large */
1529
+ size_t double_size = 2 * buf->max_size;
1530
+ if (double_size >= need) /* overflow check */
1531
+ need = double_size;
1532
+ }
1533
+
1534
+ tmp = PyMem_Realloc(buf->data, need);
1535
+ if (!tmp) {
1536
+ buf->error = 1;
1537
+ return;
1538
+ }
1539
+
1540
+ buf->data = tmp;
1541
+ buf->max_size = need;
1542
+ }
1543
+
1544
+ memcpy(buf->data + buf->size, s, len);
1545
+ buf->size += len;
1546
+ }
1547
+
1548
+ /* Extend char buffer with given character */
1549
+ static void
1550
+ ext_char_buffer_c(struct CharBuffer *buf, char c)
1551
+ {
1552
+ if (buf->error)
1553
+ return;
1554
+
1555
+ if (buf->size >= buf->max_size) { /* buffer is full? */
1556
+ /* slow path dealing with reallocation */
1557
+ char tmp[2] = {c ? c : '\n', '\0'}; /* allow adding a zero-byte */
1558
+ ext_char_buffer_s(buf, tmp);
1559
+ if (!c)
1560
+ buf->data[buf->size - 1] = '\0'; /* fix zero-byte */
1561
+ }
1562
+ else
1563
+ buf->data[buf->size++] = c;
1564
+ }
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Large object support - this file is part a of the C extension module.
5
5
  *
6
- * Copyright (c) 2025 by the PyGreSQL Development Team
6
+ * Copyright (c) 2026 by the PyGreSQL Development Team
7
7
  *
8
8
  * Please see the LICENSE.TXT file for specific restrictions.
9
9
  */
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * This is the main file for the C extension module.
5
5
  *
6
- * Copyright (c) 2025 by the PyGreSQL Development Team
6
+ * Copyright (c) 2026 by the PyGreSQL Development Team
7
7
  *
8
8
  * Please see the LICENSE.TXT file for specific restrictions.
9
9
  */
@@ -53,8 +53,7 @@ static const char *PyPgVersion = TOSTRING(PYGRESQL_VERSION);
53
53
  #define QUERY_MOVENEXT 3
54
54
  #define QUERY_MOVEPREV 4
55
55
 
56
- #define MAX_BUFFER_SIZE 65536 /* maximum transaction size */
57
- #define MAX_ARRAY_DEPTH 16 /* maximum allowed depth of an array */
56
+ #define MAX_ARRAY_DEPTH 16 /* maximum allowed depth of an array */
58
57
 
59
58
  /* MODULE GLOBAL VARIABLES */
60
59
 
@@ -158,6 +157,18 @@ typedef struct {
158
157
  } largeObject;
159
158
  #define is_largeObject(v) (PyType(v) == &largeType)
160
159
 
160
+ /*
161
+ A buffer for character data with routines to handle resizing.
162
+ This is inspired by libpq's PQExpBufferData.
163
+ The buffer can be extended with the extend_char_buffer_s/x() functions.
164
+ */
165
+ struct CharBuffer {
166
+ char *data; /* actual string data */
167
+ size_t size; /* current size of data */
168
+ size_t max_size; /* allocated size */
169
+ int error; /* error flag (invalid data) */
170
+ };
171
+
161
172
  /* Internal functions */
162
173
  #include "pginternal.c"
163
174
 
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * The notice object - this file is part a of the C extension module.
5
5
  *
6
- * Copyright (c) 2025 by the PyGreSQL Development Team
6
+ * Copyright (c) 2026 by the PyGreSQL Development Team
7
7
  *
8
8
  * Please see the LICENSE.TXT file for specific restrictions.
9
9
  */
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * The query object - this file is part a of the C extension module.
5
5
  *
6
- * Copyright (c) 2025 by the PyGreSQL Development Team
6
+ * Copyright (c) 2026 by the PyGreSQL Development Team
7
7
  *
8
8
  * Please see the LICENSE.TXT file for specific restrictions.
9
9
  */
@@ -179,11 +179,8 @@ _get_async_result(queryObject *self, int keep)
179
179
  self->max_row = PQntuples(self->result);
180
180
  self->num_fields = PQnfields(self->result);
181
181
  self->col_types = get_col_types(self->result, self->num_fields);
182
- if (!self->col_types) {
183
- Py_DECREF(self);
184
- Py_DECREF(self);
182
+ if (!self->col_types)
185
183
  return NULL;
186
- }
187
184
  }
188
185
  else if (self->async == 2 && !self->max_row && !self->num_fields &&
189
186
  !self->col_types) {
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * The source object - this file is part a of the C extension module.
5
5
  *
6
- * Copyright (c) 2025 by the PyGreSQL Development Team
6
+ * Copyright (c) 2026 by the PyGreSQL Development Team
7
7
  *
8
8
  * Please see the LICENSE.TXT file for specific restrictions.
9
9
  */
@@ -4,7 +4,7 @@
4
4
  #
5
5
  # This file contains the classic pg module.
6
6
  #
7
- # Copyright (c) 2025 by the PyGreSQL Development Team
7
+ # Copyright (c) 2026 by the PyGreSQL Development Team
8
8
  #
9
9
  # The notification handler is based on pgnotify which is
10
10
  # Copyright (c) 2001 Ng Pheng Siong. All rights reserved.
@@ -4,7 +4,7 @@
4
4
  #
5
5
  # This file contains the DB-API 2 compatible pgdb module.
6
6
  #
7
- # Copyright (c) 2025 by the PyGreSQL Development Team
7
+ # Copyright (c) 2026 by the PyGreSQL Development Team
8
8
  #
9
9
  # Please see the LICENSE.TXT file for specific restrictions.
10
10
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "PyGreSQL"
3
- version = "6.2.1"
3
+ version = "6.2.2"
4
4
  requires-python = ">=3.8"
5
5
  authors = [
6
6
  {name = "D'Arcy J. M. Cain", email = "darcy@pygresql.org"},
@@ -1967,9 +1967,9 @@ class TestInserttable(unittest.TestCase):
1967
1967
  cols = ['very_long_column_name'] * 2000
1968
1968
  # Should raise a value error because the column does not exist
1969
1969
  self.assertRaises(ValueError, self.c.inserttable, 'test', data, cols)
1970
- # double the size, should catch buffer overflow and raise memory error
1970
+ # double the size, should not overflow buffer nor raise memory error
1971
1971
  cols *= 2
1972
- self.assertRaises(MemoryError, self.c.inserttable, 'test', data, cols)
1972
+ self.assertRaises(ValueError, self.c.inserttable, 'test', data, cols)
1973
1973
 
1974
1974
  def test_inserttable_with_out_of_range_data(self):
1975
1975
  # try inserting data out of range for the column type
@@ -2095,16 +2095,12 @@ class TestInserttable(unittest.TestCase):
2095
2095
  self.c.query('select t from test').getresult(), [(s,)] * 3)
2096
2096
 
2097
2097
  def test_insert_table_big_row_size(self):
2098
- # inserting rows with a size of up to 64k bytes should work
2099
- t = '*' * 50000
2098
+ # inserting rows with a size exceeding 64k bytes should work
2099
+ t = '*' * 75000
2100
2100
  data = [(t,)]
2101
2101
  self.c.inserttable('test', data, ['t'])
2102
2102
  self.assertEqual(
2103
2103
  self.c.query('select t from test').getresult(), data)
2104
- # double the size, should catch buffer overflow and raise memory error
2105
- t *= 2
2106
- data = [(t,)]
2107
- self.assertRaises(MemoryError, self.c.inserttable, 'test', data, ['t'])
2108
2104
 
2109
2105
  def test_insert_table_small_int_overflow(self):
2110
2106
  rest_row = self.data[2][1:]
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes