PyGreSQL 6.2.1__tar.gz → 6.2.3__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 (103) hide show
  1. {pygresql-6.2.1 → pygresql-6.2.3}/LICENSE.txt +1 -1
  2. {pygresql-6.2.1 → pygresql-6.2.3}/PKG-INFO +1 -1
  3. {pygresql-6.2.1 → pygresql-6.2.3}/PyGreSQL.egg-info/PKG-INFO +1 -1
  4. {pygresql-6.2.1 → pygresql-6.2.3}/PyGreSQL.egg-info/SOURCES.txt +2 -0
  5. {pygresql-6.2.1 → pygresql-6.2.3}/docs/about.rst +1 -1
  6. {pygresql-6.2.1 → pygresql-6.2.3}/docs/conf.py +1 -1
  7. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/changelog.rst +15 -0
  8. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pg/connection.rst +2 -1
  9. {pygresql-6.2.1 → pygresql-6.2.3}/docs/copyright.rst +1 -1
  10. {pygresql-6.2.1 → pygresql-6.2.3}/ext/pgconn.c +126 -117
  11. {pygresql-6.2.1 → pygresql-6.2.3}/ext/pginternal.c +103 -26
  12. pygresql-6.2.3/ext/pginternal.h +91 -0
  13. {pygresql-6.2.1 → pygresql-6.2.3}/ext/pglarge.c +12 -6
  14. {pygresql-6.2.1 → pygresql-6.2.3}/ext/pgmodule.c +35 -128
  15. pygresql-6.2.3/ext/pgmodule.h +145 -0
  16. {pygresql-6.2.1 → pygresql-6.2.3}/ext/pgnotice.c +9 -2
  17. {pygresql-6.2.1 → pygresql-6.2.3}/ext/pgquery.c +7 -6
  18. {pygresql-6.2.1 → pygresql-6.2.3}/ext/pgsource.c +8 -2
  19. {pygresql-6.2.1 → pygresql-6.2.3}/pg/__init__.py +1 -1
  20. {pygresql-6.2.1 → pygresql-6.2.3}/pgdb/__init__.py +1 -1
  21. {pygresql-6.2.1 → pygresql-6.2.3}/pyproject.toml +1 -1
  22. {pygresql-6.2.1 → pygresql-6.2.3}/setup.py +9 -1
  23. {pygresql-6.2.1 → pygresql-6.2.3}/tests/test_classic_connection.py +69 -46
  24. {pygresql-6.2.1 → pygresql-6.2.3}/MANIFEST.in +0 -0
  25. {pygresql-6.2.1 → pygresql-6.2.3}/PyGreSQL.egg-info/dependency_links.txt +0 -0
  26. {pygresql-6.2.1 → pygresql-6.2.3}/PyGreSQL.egg-info/not-zip-safe +0 -0
  27. {pygresql-6.2.1 → pygresql-6.2.3}/PyGreSQL.egg-info/top_level.txt +0 -0
  28. {pygresql-6.2.1 → pygresql-6.2.3}/README.rst +0 -0
  29. {pygresql-6.2.1 → pygresql-6.2.3}/docs/Makefile +0 -0
  30. {pygresql-6.2.1 → pygresql-6.2.3}/docs/_static/favicon.ico +0 -0
  31. {pygresql-6.2.1 → pygresql-6.2.3}/docs/_static/pygresql.png +0 -0
  32. {pygresql-6.2.1 → pygresql-6.2.3}/docs/community/homes.rst +0 -0
  33. {pygresql-6.2.1 → pygresql-6.2.3}/docs/community/index.rst +0 -0
  34. {pygresql-6.2.1 → pygresql-6.2.3}/docs/community/issues.rst +0 -0
  35. {pygresql-6.2.1 → pygresql-6.2.3}/docs/community/mailinglist.rst +0 -0
  36. {pygresql-6.2.1 → pygresql-6.2.3}/docs/community/source.rst +0 -0
  37. {pygresql-6.2.1 → pygresql-6.2.3}/docs/community/support.rst +0 -0
  38. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/examples.rst +0 -0
  39. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/general.rst +0 -0
  40. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/index.rst +0 -0
  41. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/install.rst +0 -0
  42. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pg/adaptation.rst +0 -0
  43. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pg/db_types.rst +0 -0
  44. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pg/db_wrapper.rst +0 -0
  45. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pg/index.rst +0 -0
  46. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pg/introduction.rst +0 -0
  47. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pg/large_objects.rst +0 -0
  48. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pg/module.rst +0 -0
  49. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pg/notification.rst +0 -0
  50. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pg/query.rst +0 -0
  51. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pgdb/adaptation.rst +0 -0
  52. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pgdb/connection.rst +0 -0
  53. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pgdb/cursor.rst +0 -0
  54. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pgdb/index.rst +0 -0
  55. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pgdb/introduction.rst +0 -0
  56. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pgdb/module.rst +0 -0
  57. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pgdb/typecache.rst +0 -0
  58. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/pgdb/types.rst +0 -0
  59. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/postgres/advanced.rst +0 -0
  60. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/postgres/basic.rst +0 -0
  61. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/postgres/func.rst +0 -0
  62. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/postgres/index.rst +0 -0
  63. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/postgres/syscat.rst +0 -0
  64. {pygresql-6.2.1 → pygresql-6.2.3}/docs/contents/tutorial.rst +0 -0
  65. {pygresql-6.2.1 → pygresql-6.2.3}/docs/download/download.rst +0 -0
  66. {pygresql-6.2.1 → pygresql-6.2.3}/docs/download/files.rst +0 -0
  67. {pygresql-6.2.1 → pygresql-6.2.3}/docs/download/index.rst +0 -0
  68. {pygresql-6.2.1 → pygresql-6.2.3}/docs/make.bat +0 -0
  69. {pygresql-6.2.1 → pygresql-6.2.3}/docs/requirements.txt +0 -0
  70. {pygresql-6.2.1 → pygresql-6.2.3}/ext/pgtypes.h +0 -0
  71. {pygresql-6.2.1 → pygresql-6.2.3}/pg/_pg.pyi +0 -0
  72. {pygresql-6.2.1 → pygresql-6.2.3}/pg/adapt.py +0 -0
  73. {pygresql-6.2.1 → pygresql-6.2.3}/pg/attrs.py +0 -0
  74. {pygresql-6.2.1 → pygresql-6.2.3}/pg/cast.py +0 -0
  75. {pygresql-6.2.1 → pygresql-6.2.3}/pg/core.py +0 -0
  76. {pygresql-6.2.1 → pygresql-6.2.3}/pg/db.py +0 -0
  77. {pygresql-6.2.1 → pygresql-6.2.3}/pg/error.py +0 -0
  78. {pygresql-6.2.1 → pygresql-6.2.3}/pg/helpers.py +0 -0
  79. {pygresql-6.2.1 → pygresql-6.2.3}/pg/notify.py +0 -0
  80. {pygresql-6.2.1 → pygresql-6.2.3}/pg/py.typed +0 -0
  81. {pygresql-6.2.1 → pygresql-6.2.3}/pg/tz.py +0 -0
  82. {pygresql-6.2.1 → pygresql-6.2.3}/pgdb/adapt.py +0 -0
  83. {pygresql-6.2.1 → pygresql-6.2.3}/pgdb/cast.py +0 -0
  84. {pygresql-6.2.1 → pygresql-6.2.3}/pgdb/connect.py +0 -0
  85. {pygresql-6.2.1 → pygresql-6.2.3}/pgdb/connection.py +0 -0
  86. {pygresql-6.2.1 → pygresql-6.2.3}/pgdb/constants.py +0 -0
  87. {pygresql-6.2.1 → pygresql-6.2.3}/pgdb/cursor.py +0 -0
  88. {pygresql-6.2.1 → pygresql-6.2.3}/pgdb/py.typed +0 -0
  89. {pygresql-6.2.1 → pygresql-6.2.3}/pgdb/typecode.py +0 -0
  90. {pygresql-6.2.1 → pygresql-6.2.3}/setup.cfg +0 -0
  91. {pygresql-6.2.1 → pygresql-6.2.3}/tests/__init__.py +0 -0
  92. {pygresql-6.2.1 → pygresql-6.2.3}/tests/config.py +0 -0
  93. {pygresql-6.2.1 → pygresql-6.2.3}/tests/dbapi20.py +0 -0
  94. {pygresql-6.2.1 → pygresql-6.2.3}/tests/test_classic.py +0 -0
  95. {pygresql-6.2.1 → pygresql-6.2.3}/tests/test_classic_attrdict.py +0 -0
  96. {pygresql-6.2.1 → pygresql-6.2.3}/tests/test_classic_dbwrapper.py +0 -0
  97. {pygresql-6.2.1 → pygresql-6.2.3}/tests/test_classic_functions.py +0 -0
  98. {pygresql-6.2.1 → pygresql-6.2.3}/tests/test_classic_largeobj.py +0 -0
  99. {pygresql-6.2.1 → pygresql-6.2.3}/tests/test_classic_notification.py +0 -0
  100. {pygresql-6.2.1 → pygresql-6.2.3}/tests/test_dbapi20.py +0 -0
  101. {pygresql-6.2.1 → pygresql-6.2.3}/tests/test_dbapi20_copy.py +0 -0
  102. {pygresql-6.2.1 → pygresql-6.2.3}/tests/test_tutorial.py +0 -0
  103. {pygresql-6.2.1 → pygresql-6.2.3}/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.3
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.3
4
4
  Summary: Python PostgreSQL interfaces
5
5
  Home-page: https://pygresql.github.io/
6
6
  Download-URL: https://pygresql.github.io/download/
@@ -57,8 +57,10 @@ docs/download/files.rst
57
57
  docs/download/index.rst
58
58
  ext/pgconn.c
59
59
  ext/pginternal.c
60
+ ext/pginternal.h
60
61
  ext/pglarge.c
61
62
  ext/pgmodule.c
63
+ ext/pgmodule.h
62
64
  ext/pgnotice.c
63
65
  ext/pgquery.c
64
66
  ext/pgsource.c
@@ -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,21 @@
1
1
  ChangeLog
2
2
  =========
3
3
 
4
+ Version 6.2.3 (2026-01-25)
5
+ --------------------------
6
+ - Minor improvements and fixes (thanks to Justin Pryzby):
7
+
8
+ - The `inserttable()` method in the `pg` module can now directly import
9
+ date, time, datetime and timedelta objects from Python (#93) and was
10
+ slightly improved to avoid memory exhaustion and memory leaks (#92).
11
+ - The C extension has no been properly modularized into separate units (#25).
12
+
13
+ Version 6.2.2 (2026-01-03)
14
+ --------------------------
15
+ - The `inserttable()` method in the `pg` module can now handle rows of
16
+ arbitrary size (this was previously limited to 64 KB) (#79, thanks
17
+ to Justin Pryzby for this contribution).
18
+
4
19
  Version 6.2.1 (2025-12-27)
5
20
  --------------------------
6
21
  - Made setup cleaner and work with a wider range of setuptools versions (#90).
@@ -495,7 +495,8 @@ This method allows to *quickly* insert large blocks of data in a table.
495
495
  Internally, it uses the COPY command of the PostgreSQL database.
496
496
  The method takes an iterable of row values which must be tuples or lists
497
497
  of the same size, containing the values for each inserted row.
498
- These may contain string, integer, long or double (real) values.
498
+ These may contain string, integer, long or double (real) values as well as
499
+ date, time, datetime or timedelta objects.
499
500
  ``columns`` is an optional tuple or list of column names to be passed on
500
501
  to the COPY command.
501
502
  The number of rows affected is returned.
@@ -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,11 +3,18 @@
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
  */
10
10
 
11
+ /* Shared headers */
12
+ #include "pginternal.h"
13
+ #include "pgmodule.h"
14
+
15
+ /* Needs to be after Python.h */
16
+ #include "datetime.h"
17
+
11
18
  /* Deallocate connection object. */
12
19
  static void
13
20
  conn_dealloc(connObject *self)
@@ -27,6 +34,8 @@ static PyObject *
27
34
  conn_getattr(connObject *self, PyObject *nameobj)
28
35
  {
29
36
  const char *name = PyUnicode_AsUTF8(nameobj);
37
+ if (!name)
38
+ return NULL;
30
39
 
31
40
  /*
32
41
  * Although we could check individually, there are only a few
@@ -113,7 +122,7 @@ conn_getattr(connObject *self, PyObject *nameobj)
113
122
  }
114
123
 
115
124
  /* Check connection validity. */
116
- static int
125
+ int
117
126
  _check_cnx_obj(connObject *self)
118
127
  {
119
128
  if (!self || !self->valid || !self->cnx) {
@@ -154,7 +163,7 @@ conn_source(connObject *self, PyObject *noargs)
154
163
 
155
164
  /* For a non-query result, set the appropriate error status,
156
165
  return the appropriate value, and free the result set. */
157
- static PyObject *
166
+ PyObject *
158
167
  _conn_non_query_result(int status, PGresult *result, PGconn *cnx)
159
168
  {
160
169
  switch (status) {
@@ -532,6 +541,12 @@ conn_describe_prepared(connObject *self, PyObject *args)
532
541
  query_obj->max_row = PQntuples(result);
533
542
  query_obj->num_fields = PQnfields(result);
534
543
  query_obj->col_types = get_col_types(result, query_obj->num_fields);
544
+ if (!query_obj->col_types) {
545
+ PQclear(result);
546
+ Py_DECREF(query_obj);
547
+ Py_DECREF(self);
548
+ return NULL;
549
+ }
535
550
  return (PyObject *)query_obj;
536
551
  }
537
552
  set_error(ProgrammingError, "Cannot describe prepared statement",
@@ -716,9 +731,9 @@ static PyObject *
716
731
  conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
717
732
  {
718
733
  PGresult *result;
719
- char *table, *buffer, *bufpt, *bufmax, *s, *t;
734
+ char *table, *s, *t;
735
+ struct CharBuffer buffer = {0};
720
736
  int freeze = 0, encoding, ret;
721
- size_t bufsiz;
722
737
  PyObject *rows, *iter_row, *item, *columns = NULL;
723
738
  Py_ssize_t i, j, m, n;
724
739
 
@@ -775,18 +790,22 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
775
790
  n = -1; /* number of columns not yet known */
776
791
  }
777
792
 
778
- /* allocate buffer */
779
- if (!(buffer = PyMem_Malloc(MAX_BUFFER_SIZE))) {
793
+ encoding = PQclientEncoding(self->cnx);
794
+
795
+ /* import datetime C API (this is not compatible with subinterpreters) */
796
+ PyDateTime_IMPORT;
797
+ if (PyErr_Occurred()) {
798
+ return NULL; /* pass the error */
799
+ }
800
+
801
+ /* pre-allocate some memory for the query buffer */
802
+ if (!init_char_buffer(&buffer, 4096)) {
780
803
  Py_DECREF(iter_row);
781
804
  return PyErr_NoMemory();
782
805
  }
783
806
 
784
- encoding = PQclientEncoding(self->cnx);
785
-
786
807
  /* starts query */
787
- bufpt = buffer;
788
- bufmax = bufpt + MAX_BUFFER_SIZE;
789
- bufpt += snprintf(bufpt, (size_t)(bufmax - bufpt), "copy ");
808
+ ext_char_buffer_s(&buffer, "copy ");
790
809
 
791
810
  s = table;
792
811
  do {
@@ -794,18 +813,16 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
794
813
  if (!t)
795
814
  t = s + strlen(s);
796
815
  table = PQescapeIdentifier(self->cnx, s, (size_t)(t - s));
797
- if (bufpt < bufmax)
798
- bufpt += snprintf(bufpt, (size_t)(bufmax - bufpt), "%s", table);
816
+ ext_char_buffer_s(&buffer, table);
799
817
  PQfreemem(table);
800
818
  s = t;
801
- if (*s && bufpt < bufmax)
802
- *bufpt++ = *s++;
819
+ if (*s)
820
+ ext_char_buffer_c(&buffer, *s++);
803
821
  } while (*s);
804
822
 
805
823
  if (columns) {
806
824
  /* adds a string like f" ({','.join(columns)})" */
807
- if (bufpt < bufmax)
808
- bufpt += snprintf(bufpt, (size_t)(bufmax - bufpt), " (");
825
+ ext_char_buffer_s(&buffer, " (");
809
826
  for (j = 0; j < n; ++j) {
810
827
  PyObject *obj = PySequence_Fast_GET_ITEM(columns, j);
811
828
  Py_ssize_t slen;
@@ -817,7 +834,7 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
817
834
  else if (PyUnicode_Check(obj)) {
818
835
  obj = get_encoded_string(obj, encoding);
819
836
  if (!obj) {
820
- PyMem_Free(buffer);
837
+ PyMem_Free(buffer.data);
821
838
  Py_DECREF(iter_row);
822
839
  return NULL; /* pass the UnicodeEncodeError */
823
840
  }
@@ -826,37 +843,37 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
826
843
  PyErr_SetString(
827
844
  PyExc_TypeError,
828
845
  "The third argument must contain only strings");
829
- PyMem_Free(buffer);
846
+ PyMem_Free(buffer.data);
830
847
  Py_DECREF(iter_row);
831
848
  return NULL;
832
849
  }
833
850
  PyBytes_AsStringAndSize(obj, &col, &slen);
834
851
  col = PQescapeIdentifier(self->cnx, col, (size_t)slen);
835
852
  Py_DECREF(obj);
836
- if (bufpt < bufmax)
837
- bufpt += snprintf(bufpt, (size_t)(bufmax - bufpt), "%s%s", col,
838
- j == n - 1 ? ")" : ",");
853
+ ext_char_buffer_s(&buffer, col);
839
854
  PQfreemem(col);
855
+ ext_char_buffer_c(&buffer, j == n - 1 ? ')' : ',');
840
856
  }
841
857
  }
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
- }
858
+ ext_char_buffer_s(&buffer, " from stdin");
859
+ if (freeze)
860
+ ext_char_buffer_s(&buffer, " freeze");
861
+ ext_char_buffer_c(&buffer, '\0');
847
862
 
848
- if (bufpt >= bufmax) {
849
- PyMem_Free(buffer);
863
+ if (buffer.error) {
864
+ PyMem_Free(buffer.data);
850
865
  Py_DECREF(iter_row);
851
866
  return PyErr_NoMemory();
852
867
  }
853
868
 
854
869
  Py_BEGIN_ALLOW_THREADS
855
- result = PQexec(self->cnx, buffer);
870
+ result = PQexec(self->cnx, buffer.data);
856
871
  Py_END_ALLOW_THREADS
857
872
 
858
873
  if (!result || PQresultStatus(result) != PGRES_COPY_IN) {
859
- PyMem_Free(buffer);
874
+ if (result)
875
+ PQclear(result);
876
+ PyMem_Free(buffer.data);
860
877
  Py_DECREF(iter_row);
861
878
  PyErr_SetString(PyExc_ValueError, PQerrorMessage(self->cnx));
862
879
  return NULL;
@@ -864,6 +881,9 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
864
881
 
865
882
  PQclear(result);
866
883
 
884
+ /* empty buffer while keeping allocated memory */
885
+ buffer.size = 0;
886
+
867
887
  /* feed table */
868
888
  for (i = 0; m < 0 || i < m; ++i) {
869
889
  if (!(columns = PyIter_Next(iter_row)))
@@ -871,8 +891,7 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
871
891
 
872
892
  if (!(PyTuple_Check(columns) || PyList_Check(columns))) {
873
893
  PQputCopyEnd(self->cnx, "Invalid arguments");
874
- PyMem_Free(buffer);
875
- Py_DECREF(columns);
894
+ PyMem_Free(buffer.data);
876
895
  Py_DECREF(columns);
877
896
  Py_DECREF(iter_row);
878
897
  PyErr_SetString(
@@ -887,7 +906,7 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
887
906
  }
888
907
  else if (j != n) {
889
908
  PQputCopyEnd(self->cnx, "Invalid arguments");
890
- PyMem_Free(buffer);
909
+ PyMem_Free(buffer.data);
891
910
  Py_DECREF(columns);
892
911
  Py_DECREF(iter_row);
893
912
  PyErr_SetString(
@@ -896,65 +915,51 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
896
915
  return NULL;
897
916
  }
898
917
 
899
- /* builds insert line */
900
- bufpt = buffer;
901
- bufsiz = MAX_BUFFER_SIZE - 1;
918
+ /* build insert line */
902
919
 
903
920
  for (j = 0; j < n; ++j) {
904
- if (j) {
905
- *bufpt++ = '\t';
906
- --bufsiz;
907
- }
921
+ if (j)
922
+ ext_char_buffer_c(&buffer, '\t');
908
923
 
909
924
  item = PySequence_Fast_GET_ITEM(columns, j);
910
925
 
911
926
  /* convert item to string and append to buffer */
912
927
  if (item == Py_None) {
913
- if (bufsiz > 2) {
914
- *bufpt++ = '\\';
915
- *bufpt++ = 'N';
916
- bufsiz -= 2;
917
- }
918
- else
919
- bufsiz = 0;
928
+ ext_char_buffer_c(&buffer, '\\');
929
+ ext_char_buffer_c(&buffer, 'N');
920
930
  }
921
931
  else if (PyBytes_Check(item)) {
922
932
  const char *t = PyBytes_AsString(item);
923
933
 
924
- while (*t && bufsiz) {
934
+ while (*t) {
925
935
  switch (*t) {
926
936
  case '\\':
927
- *bufpt++ = '\\';
928
- if (--bufsiz)
929
- *bufpt++ = '\\';
937
+ ext_char_buffer_c(&buffer, '\\');
938
+ ext_char_buffer_c(&buffer, '\\');
930
939
  break;
931
940
  case '\t':
932
- *bufpt++ = '\\';
933
- if (--bufsiz)
934
- *bufpt++ = 't';
941
+ ext_char_buffer_c(&buffer, '\\');
942
+ ext_char_buffer_c(&buffer, '\t');
935
943
  break;
936
944
  case '\r':
937
- *bufpt++ = '\\';
938
- if (--bufsiz)
939
- *bufpt++ = 'r';
945
+ ext_char_buffer_c(&buffer, '\\');
946
+ ext_char_buffer_c(&buffer, '\r');
940
947
  break;
941
948
  case '\n':
942
- *bufpt++ = '\\';
943
- if (--bufsiz)
944
- *bufpt++ = 'n';
949
+ ext_char_buffer_c(&buffer, '\\');
950
+ ext_char_buffer_c(&buffer, '\n');
945
951
  break;
946
952
  default:
947
- *bufpt++ = *t;
953
+ ext_char_buffer_c(&buffer, *t);
948
954
  }
949
955
  ++t;
950
- --bufsiz;
951
956
  }
952
957
  }
953
958
  else if (PyUnicode_Check(item)) {
954
959
  PyObject *s = get_encoded_string(item, encoding);
955
960
  if (!s) {
956
961
  PQputCopyEnd(self->cnx, "Encoding error");
957
- PyMem_Free(buffer);
962
+ PyMem_Free(buffer.data);
958
963
  Py_DECREF(item);
959
964
  Py_DECREF(columns);
960
965
  Py_DECREF(iter_row);
@@ -963,111 +968,118 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
963
968
  else {
964
969
  const char *t = PyBytes_AsString(s);
965
970
 
966
- while (*t && bufsiz) {
971
+ while (*t) {
967
972
  switch (*t) {
968
973
  case '\\':
969
- *bufpt++ = '\\';
970
- if (--bufsiz)
971
- *bufpt++ = '\\';
974
+ ext_char_buffer_c(&buffer, '\\');
975
+ ext_char_buffer_c(&buffer, '\\');
972
976
  break;
973
977
  case '\t':
974
- *bufpt++ = '\\';
975
- if (--bufsiz)
976
- *bufpt++ = 't';
978
+ ext_char_buffer_c(&buffer, '\\');
979
+ ext_char_buffer_c(&buffer, '\t');
977
980
  break;
978
981
  case '\r':
979
- *bufpt++ = '\\';
980
- if (--bufsiz)
981
- *bufpt++ = 'r';
982
+ ext_char_buffer_c(&buffer, '\\');
983
+ ext_char_buffer_c(&buffer, '\r');
982
984
  break;
983
985
  case '\n':
984
- *bufpt++ = '\\';
985
- if (--bufsiz)
986
- *bufpt++ = 'n';
986
+ ext_char_buffer_c(&buffer, '\\');
987
+ ext_char_buffer_c(&buffer, '\n');
987
988
  break;
988
989
  default:
989
- *bufpt++ = *t;
990
+ ext_char_buffer_c(&buffer, *t);
990
991
  }
991
992
  ++t;
992
- --bufsiz;
993
993
  }
994
994
  Py_DECREF(s);
995
995
  }
996
996
  }
997
- else if (PyLong_Check(item)) {
997
+ else if (PyLong_Check(item) || PyDate_Check(item) ||
998
+ PyDateTime_Check(item) || PyTime_Check(item) ||
999
+ PyDelta_Check(item)) {
998
1000
  PyObject *s = PyObject_Str(item);
999
1001
  const char *t = PyUnicode_AsUTF8(s);
1000
1002
 
1001
- while (*t && bufsiz) {
1002
- *bufpt++ = *t++;
1003
- --bufsiz;
1004
- }
1003
+ ext_char_buffer_s(&buffer, t);
1005
1004
  Py_DECREF(s);
1006
1005
  }
1007
1006
  else {
1008
1007
  PyObject *s = PyObject_Repr(item);
1009
1008
  const char *t = PyUnicode_AsUTF8(s);
1010
1009
 
1011
- while (*t && bufsiz) {
1010
+ while (*t) {
1012
1011
  switch (*t) {
1013
1012
  case '\\':
1014
- *bufpt++ = '\\';
1015
- if (--bufsiz)
1016
- *bufpt++ = '\\';
1013
+ ext_char_buffer_c(&buffer, '\\');
1014
+ ext_char_buffer_c(&buffer, '\\');
1017
1015
  break;
1018
1016
  case '\t':
1019
- *bufpt++ = '\\';
1020
- if (--bufsiz)
1021
- *bufpt++ = 't';
1017
+ ext_char_buffer_c(&buffer, '\\');
1018
+ ext_char_buffer_c(&buffer, '\t');
1022
1019
  break;
1023
1020
  case '\r':
1024
- *bufpt++ = '\\';
1025
- if (--bufsiz)
1026
- *bufpt++ = 'r';
1021
+ ext_char_buffer_c(&buffer, '\\');
1022
+ ext_char_buffer_c(&buffer, '\r');
1027
1023
  break;
1028
1024
  case '\n':
1029
- *bufpt++ = '\\';
1030
- if (--bufsiz)
1031
- *bufpt++ = 'n';
1025
+ ext_char_buffer_c(&buffer, '\\');
1026
+ ext_char_buffer_c(&buffer, '\n');
1032
1027
  break;
1033
1028
  default:
1034
- *bufpt++ = *t;
1029
+ ext_char_buffer_c(&buffer, *t);
1035
1030
  }
1036
1031
  ++t;
1037
- --bufsiz;
1038
1032
  }
1039
1033
  Py_DECREF(s);
1040
1034
  }
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
1035
  }
1050
1036
 
1051
1037
  Py_DECREF(columns);
1052
1038
 
1053
- *bufpt++ = '\n';
1039
+ /* terminate line */
1040
+ ext_char_buffer_c(&buffer, '\n');
1041
+ if (buffer.error) {
1042
+ PQputCopyEnd(self->cnx, "Memory error");
1043
+ PyMem_Free(buffer.data);
1044
+ Py_DECREF(iter_row);
1045
+ return PyErr_NoMemory();
1046
+ }
1054
1047
 
1055
- /* sends data */
1056
- ret = PQputCopyData(self->cnx, buffer, (int)(bufpt - buffer));
1048
+ if (buffer.size > 128 * 1024) {
1049
+ /* send buffered data after reaching 128KB */
1050
+ ret = PQputCopyData(self->cnx, buffer.data, (int)buffer.size);
1051
+ buffer.size = 0;
1052
+ if (ret != 1) {
1053
+ char *errormsg = ret == -1 ? PQerrorMessage(self->cnx)
1054
+ : "Data cannot be queued";
1055
+ PyErr_SetString(PyExc_IOError, errormsg);
1056
+ PQputCopyEnd(self->cnx, errormsg);
1057
+ PyMem_Free(buffer.data);
1058
+ Py_DECREF(iter_row);
1059
+ return NULL;
1060
+ }
1061
+ }
1062
+ }
1063
+
1064
+ /* flush any remaining data */
1065
+ if (buffer.size) {
1066
+ ret = PQputCopyData(self->cnx, buffer.data, (int)buffer.size);
1067
+ buffer.size = 0;
1057
1068
  if (ret != 1) {
1058
1069
  char *errormsg = ret == -1 ? PQerrorMessage(self->cnx)
1059
1070
  : "Data cannot be queued";
1060
1071
  PyErr_SetString(PyExc_IOError, errormsg);
1061
1072
  PQputCopyEnd(self->cnx, errormsg);
1062
- PyMem_Free(buffer);
1073
+ PyMem_Free(buffer.data);
1063
1074
  Py_DECREF(iter_row);
1064
1075
  return NULL;
1065
1076
  }
1066
1077
  }
1067
1078
 
1079
+ PyMem_Free(buffer.data);
1080
+
1068
1081
  Py_DECREF(iter_row);
1069
1082
  if (PyErr_Occurred()) {
1070
- PyMem_Free(buffer);
1071
1083
  return NULL; /* pass the iteration error */
1072
1084
  }
1073
1085
 
@@ -1075,12 +1087,9 @@ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
1075
1087
  if (ret != 1) {
1076
1088
  PyErr_SetString(PyExc_IOError, ret == -1 ? PQerrorMessage(self->cnx)
1077
1089
  : "Data cannot be queued");
1078
- PyMem_Free(buffer);
1079
1090
  return NULL;
1080
1091
  }
1081
1092
 
1082
- PyMem_Free(buffer);
1083
-
1084
1093
  Py_BEGIN_ALLOW_THREADS
1085
1094
  result = PQgetResult(self->cnx);
1086
1095
  Py_END_ALLOW_THREADS
@@ -1801,7 +1810,7 @@ static struct PyMethodDef conn_methods[] = {
1801
1810
  static char conn__doc__[] = "PostgreSQL connection object";
1802
1811
 
1803
1812
  /* Connection type definition */
1804
- static PyTypeObject connType = {
1813
+ PyTypeObject connType = {
1805
1814
  PyVarObject_HEAD_INIT(NULL, 0) "pg.Connection", /* tp_name */
1806
1815
  sizeof(connObject), /* tp_basicsize */
1807
1816
  0, /* tp_itemsize */