Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9b7e24a

Browse files
committed
plpython: Code cleanup related to removal of Python 2 support.
Since 19252e8 we reject Python 2 during build configuration. Now that the dust on the buildfarm has settled, remove Python 2 specific code, including the "Python 2/3 porting layer". The code to detect conflicts between plpython using Python 2 and 3 is not removed, in case somebody creates an out-of-tree version adding back support for Python 2. Reviewed-By: Peter Eisentraut <peter@eisentraut.org> Reviewed-By: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/20211031184548.g4sxfe47n2kyi55r@alap3.anarazel.de
1 parent db23464 commit 9b7e24a

16 files changed

+95
-234
lines changed

contrib/hstore_plpython/hstore_plpython.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ extern void _PG_init(void);
1212
/* Linkage to functions in plpython module */
1313
typedef char *(*PLyObject_AsString_t) (PyObject *plrv);
1414
static PLyObject_AsString_t PLyObject_AsString_p;
15-
#if PY_MAJOR_VERSION >= 3
1615
typedef PyObject *(*PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size);
1716
static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p;
18-
#endif
1917

2018
/* Linkage to functions in hstore module */
2119
typedef HStore *(*hstoreUpgrade_t) (Datum orig);
@@ -41,12 +39,10 @@ _PG_init(void)
4139
PLyObject_AsString_p = (PLyObject_AsString_t)
4240
load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyObject_AsString",
4341
true, NULL);
44-
#if PY_MAJOR_VERSION >= 3
4542
AssertVariableIsOfType(&PLyUnicode_FromStringAndSize, PLyUnicode_FromStringAndSize_t);
4643
PLyUnicode_FromStringAndSize_p = (PLyUnicode_FromStringAndSize_t)
4744
load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
4845
true, NULL);
49-
#endif
5046
AssertVariableIsOfType(&hstoreUpgrade, hstoreUpgrade_t);
5147
hstoreUpgrade_p = (hstoreUpgrade_t)
5248
load_external_function("$libdir/hstore", "hstoreUpgrade",
@@ -102,16 +98,16 @@ hstore_to_plpython(PG_FUNCTION_ARGS)
10298
{
10399
PyObject *key;
104100

105-
key = PyString_FromStringAndSize(HSTORE_KEY(entries, base, i),
106-
HSTORE_KEYLEN(entries, i));
101+
key = PLyUnicode_FromStringAndSize(HSTORE_KEY(entries, base, i),
102+
HSTORE_KEYLEN(entries, i));
107103
if (HSTORE_VALISNULL(entries, i))
108104
PyDict_SetItem(dict, key, Py_None);
109105
else
110106
{
111107
PyObject *value;
112108

113-
value = PyString_FromStringAndSize(HSTORE_VAL(entries, base, i),
114-
HSTORE_VALLEN(entries, i));
109+
value = PLyUnicode_FromStringAndSize(HSTORE_VAL(entries, base, i),
110+
HSTORE_VALLEN(entries, i));
115111
PyDict_SetItem(dict, key, value);
116112
Py_XDECREF(value);
117113
}

contrib/jsonb_plpython/jsonb_plpython.c

+11-16
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ static PyObject *PLyObject_FromJsonbContainer(JsonbContainer *jsonb);
2828
static JsonbValue *PLyObject_ToJsonbValue(PyObject *obj,
2929
JsonbParseState **jsonb_state, bool is_elem);
3030

31-
#if PY_MAJOR_VERSION >= 3
3231
typedef PyObject *(*PLyUnicode_FromStringAndSize_t)
3332
(const char *s, Py_ssize_t size);
3433
static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p;
35-
#endif
3634

3735
/*
3836
* Module initialize function: fetch function pointers for cross-module calls.
@@ -45,13 +43,10 @@ _PG_init(void)
4543
PLyObject_AsString_p = (PLyObject_AsString_t)
4644
load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyObject_AsString",
4745
true, NULL);
48-
#if PY_MAJOR_VERSION >= 3
4946
AssertVariableIsOfType(&PLyUnicode_FromStringAndSize, PLyUnicode_FromStringAndSize_t);
5047
PLyUnicode_FromStringAndSize_p = (PLyUnicode_FromStringAndSize_t)
5148
load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
5249
true, NULL);
53-
#endif
54-
5550
AssertVariableIsOfType(&PLy_elog_impl, PLy_elog_impl_t);
5651
PLy_elog_impl_p = (PLy_elog_impl_t)
5752
load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLy_elog_impl",
@@ -65,25 +60,25 @@ _PG_init(void)
6560
#define PLy_elog (PLy_elog_impl_p)
6661

6762
/*
68-
* PLyString_FromJsonbValue
63+
* PLyUnicode_FromJsonbValue
6964
*
7065
* Transform string JsonbValue to Python string.
7166
*/
7267
static PyObject *
73-
PLyString_FromJsonbValue(JsonbValue *jbv)
68+
PLyUnicode_FromJsonbValue(JsonbValue *jbv)
7469
{
7570
Assert(jbv->type == jbvString);
7671

77-
return PyString_FromStringAndSize(jbv->val.string.val, jbv->val.string.len);
72+
return PLyUnicode_FromStringAndSize(jbv->val.string.val, jbv->val.string.len);
7873
}
7974

8075
/*
81-
* PLyString_ToJsonbValue
76+
* PLyUnicode_ToJsonbValue
8277
*
8378
* Transform Python string to JsonbValue.
8479
*/
8580
static void
86-
PLyString_ToJsonbValue(PyObject *obj, JsonbValue *jbvElem)
81+
PLyUnicode_ToJsonbValue(PyObject *obj, JsonbValue *jbvElem)
8782
{
8883
jbvElem->type = jbvString;
8984
jbvElem->val.string.val = PLyObject_AsString(obj);
@@ -118,7 +113,7 @@ PLyObject_FromJsonbValue(JsonbValue *jsonbValue)
118113
}
119114

120115
case jbvString:
121-
return PLyString_FromJsonbValue(jsonbValue);
116+
return PLyUnicode_FromJsonbValue(jsonbValue);
122117

123118
case jbvBool:
124119
if (jsonbValue->val.boolean)
@@ -210,7 +205,7 @@ PLyObject_FromJsonbContainer(JsonbContainer *jsonb)
210205
if (r != WJB_KEY)
211206
continue;
212207

213-
key = PLyString_FromJsonbValue(&v);
208+
key = PLyUnicode_FromJsonbValue(&v);
214209
if (!key)
215210
{
216211
Py_XDECREF(result_v);
@@ -298,7 +293,7 @@ PLyMapping_ToJsonbValue(PyObject *obj, JsonbParseState **jsonb_state)
298293
else
299294
{
300295
/* All others types of keys we serialize to string */
301-
PLyString_ToJsonbValue(key, &jbvKey);
296+
PLyUnicode_ToJsonbValue(key, &jbvKey);
302297
}
303298

304299
(void) pushJsonbValue(jsonb_state, WJB_KEY, &jbvKey);
@@ -415,7 +410,7 @@ PLyObject_ToJsonbValue(PyObject *obj, JsonbParseState **jsonb_state, bool is_ele
415410
{
416411
JsonbValue *out;
417412

418-
if (!(PyString_Check(obj) || PyUnicode_Check(obj)))
413+
if (!PyUnicode_Check(obj))
419414
{
420415
if (PySequence_Check(obj))
421416
return PLySequence_ToJsonbValue(obj, jsonb_state);
@@ -427,8 +422,8 @@ PLyObject_ToJsonbValue(PyObject *obj, JsonbParseState **jsonb_state, bool is_ele
427422

428423
if (obj == Py_None)
429424
out->type = jbvNull;
430-
else if (PyString_Check(obj) || PyUnicode_Check(obj))
431-
PLyString_ToJsonbValue(obj, out);
425+
else if (PyUnicode_Check(obj))
426+
PLyUnicode_ToJsonbValue(obj, out);
432427

433428
/*
434429
* PyNumber_Check() returns true for booleans, so boolean check should

contrib/ltree_plpython/ltree_plpython.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ PG_MODULE_MAGIC;
99
extern void _PG_init(void);
1010

1111
/* Linkage to functions in plpython module */
12-
#if PY_MAJOR_VERSION >= 3
1312
typedef PyObject *(*PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size);
1413
static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p;
15-
#endif
1614

1715

1816
/*
@@ -22,12 +20,10 @@ void
2220
_PG_init(void)
2321
{
2422
/* Asserts verify that typedefs above match original declarations */
25-
#if PY_MAJOR_VERSION >= 3
2623
AssertVariableIsOfType(&PLyUnicode_FromStringAndSize, PLyUnicode_FromStringAndSize_t);
2724
PLyUnicode_FromStringAndSize_p = (PLyUnicode_FromStringAndSize_t)
2825
load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize",
2926
true, NULL);
30-
#endif
3127
}
3228

3329

@@ -54,7 +50,7 @@ ltree_to_plpython(PG_FUNCTION_ARGS)
5450
curlevel = LTREE_FIRST(in);
5551
for (i = 0; i < in->numlevel; i++)
5652
{
57-
PyList_SetItem(list, i, PyString_FromStringAndSize(curlevel->name, curlevel->len));
53+
PyList_SetItem(list, i, PLyUnicode_FromStringAndSize(curlevel->name, curlevel->len));
5854
curlevel = LEVEL_NEXT(curlevel);
5955
}
6056

src/pl/plpython/plpy_cursorobject.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static PyTypeObject PLy_CursorType = {
4040
.tp_name = "PLyCursor",
4141
.tp_basicsize = sizeof(PLyCursorObject),
4242
.tp_dealloc = PLy_cursor_dealloc,
43-
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_ITER,
43+
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
4444
.tp_doc = PLy_cursor_doc,
4545
.tp_iter = PyObject_SelfIter,
4646
.tp_iternext = PLy_cursor_iternext,
@@ -150,7 +150,7 @@ PLy_cursor_plan(PyObject *ob, PyObject *args)
150150

151151
if (args)
152152
{
153-
if (!PySequence_Check(args) || PyString_Check(args) || PyUnicode_Check(args))
153+
if (!PySequence_Check(args) || PyUnicode_Check(args))
154154
{
155155
PLy_exception_set(PyExc_TypeError, "plpy.cursor takes a sequence as its second argument");
156156
return NULL;
@@ -169,7 +169,7 @@ PLy_cursor_plan(PyObject *ob, PyObject *args)
169169

170170
if (!so)
171171
PLy_elog(ERROR, "could not execute plan");
172-
sv = PyString_AsString(so);
172+
sv = PLyUnicode_AsString(so);
173173
PLy_exception_set_plural(PyExc_TypeError,
174174
"Expected sequence of %d argument, got %d: %s",
175175
"Expected sequence of %d arguments, got %d: %s",
@@ -410,7 +410,7 @@ PLy_cursor_fetch(PyObject *self, PyObject *args)
410410
SPI_cursor_fetch(portal, true, count);
411411

412412
Py_DECREF(ret->status);
413-
ret->status = PyInt_FromLong(SPI_OK_FETCH);
413+
ret->status = PyLong_FromLong(SPI_OK_FETCH);
414414

415415
Py_DECREF(ret->nrows);
416416
ret->nrows = PyLong_FromUnsignedLongLong(SPI_processed);

src/pl/plpython/plpy_elog.c

+11-15
Original file line numberDiff line numberDiff line change
@@ -193,24 +193,20 @@ PLy_traceback(PyObject *e, PyObject *v, PyObject *tb,
193193
e_type_o = PyObject_GetAttrString(e, "__name__");
194194
e_module_o = PyObject_GetAttrString(e, "__module__");
195195
if (e_type_o)
196-
e_type_s = PyString_AsString(e_type_o);
196+
e_type_s = PLyUnicode_AsString(e_type_o);
197197
if (e_type_s)
198-
e_module_s = PyString_AsString(e_module_o);
198+
e_module_s = PLyUnicode_AsString(e_module_o);
199199

200200
if (v && ((vob = PyObject_Str(v)) != NULL))
201-
vstr = PyString_AsString(vob);
201+
vstr = PLyUnicode_AsString(vob);
202202
else
203203
vstr = "unknown";
204204

205205
initStringInfo(&xstr);
206206
if (!e_type_s || !e_module_s)
207207
{
208-
if (PyString_Check(e))
209-
/* deprecated string exceptions */
210-
appendStringInfoString(&xstr, PyString_AsString(e));
211-
else
212-
/* shouldn't happen */
213-
appendStringInfoString(&xstr, "unrecognized exception");
208+
/* shouldn't happen */
209+
appendStringInfoString(&xstr, "unrecognized exception");
214210
}
215211
/* mimics behavior of traceback.format_exception_only */
216212
else if (strcmp(e_module_s, "builtins") == 0
@@ -290,11 +286,11 @@ PLy_traceback(PyObject *e, PyObject *v, PyObject *tb,
290286
if (*tb_depth == 1)
291287
fname = "<module>";
292288
else
293-
fname = PyString_AsString(name);
289+
fname = PLyUnicode_AsString(name);
294290

295291
proname = PLy_procedure_name(exec_ctx->curr_proc);
296-
plain_filename = PyString_AsString(filename);
297-
plain_lineno = PyInt_AsLong(lineno);
292+
plain_filename = PLyUnicode_AsString(filename);
293+
plain_lineno = PyLong_AsLong(lineno);
298294

299295
if (proname == NULL)
300296
appendStringInfo(&tbstr, "\n PL/Python anonymous code block, line %ld, in %s",
@@ -365,7 +361,7 @@ PLy_get_sqlerrcode(PyObject *exc, int *sqlerrcode)
365361
if (sqlstate == NULL)
366362
return;
367363

368-
buffer = PyString_AsString(sqlstate);
364+
buffer = PLyUnicode_AsString(sqlstate);
369365
if (strlen(buffer) == 5 &&
370366
strspn(buffer, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") == 5)
371367
{
@@ -573,7 +569,7 @@ get_string_attr(PyObject *obj, char *attrname, char **str)
573569
val = PyObject_GetAttrString(obj, attrname);
574570
if (val != NULL && val != Py_None)
575571
{
576-
*str = pstrdup(PyString_AsString(val));
572+
*str = pstrdup(PLyUnicode_AsString(val));
577573
}
578574
Py_XDECREF(val);
579575
}
@@ -589,7 +585,7 @@ set_string_attr(PyObject *obj, char *attrname, char *str)
589585

590586
if (str != NULL)
591587
{
592-
val = PyString_FromString(str);
588+
val = PLyUnicode_FromString(str);
593589
if (!val)
594590
return false;
595591
}

0 commit comments

Comments
 (0)