source: trunk/py3c.h @ 711

Last change on this file since 711 was 711, checked in by cito, 4 years ago

Flatten the directory structure of the project

Simplified the directory structure by flattening the "module" subdirectory out
to the root directory. That way, the setup.py script can now also access the
top-level docs subdirectory, so it could also install or build the docs.
There was nothing else in the root directory anyway, except the mkdocs and
mktar scripts which could be made unnecessary through setup.py.

Also made the setup script a bit clearer. Removed the note about MinGW for
Windows since the Microsoft compiler for Python 2.7 and Visual Studio Community
are now freely available including 64bit compilers, and produce less problems.

Note that the usual structure would have been to use a "pygresql" package
directory instead of the "module" directory. But since we install PyGreSQL
as two top-level modules "pg" and "pgdb" instead of a package "pygresql",
the flattened structure reflects that much better. For historical reasons
and people don't want to rewrite import statements, we will keep it that way.

File size: 3.8 KB
Line 
1/* Copyright (c) 2015, Red Hat, Inc. and/or its affiliates
2 * Licensed under the MIT license; see py3c.h
3 */
4
5#ifndef _PY3C_COMPAT_H_
6#define _PY3C_COMPAT_H_
7#include <Python.h>
8
9#if PY_MAJOR_VERSION >= 3
10
11/***** Python 3 *****/
12
13#define IS_PY3 1
14
15/* Strings */
16
17#define PyStr_Type PyUnicode_Type
18#define PyStr_Check PyUnicode_Check
19#define PyStr_CheckExact PyUnicode_CheckExact
20#define PyStr_FromString PyUnicode_FromString
21#define PyStr_FromStringAndSize PyUnicode_FromStringAndSize
22#define PyStr_FromFormat PyUnicode_FromFormat
23#define PyStr_FromFormatV PyUnicode_FromFormatV
24#define PyStr_AsString PyUnicode_AsUTF8
25#define PyStr_Concat PyUnicode_Concat
26#define PyStr_Format PyUnicode_Format
27#define PyStr_InternInPlace PyUnicode_InternInPlace
28#define PyStr_InternFromString PyUnicode_InternFromString
29#define PyStr_Decode PyUnicode_Decode
30
31#define PyStr_AsUTF8String PyUnicode_AsUTF8String // returns PyBytes
32#define PyStr_AsUTF8 PyUnicode_AsUTF8
33#define PyStr_AsUTF8AndSize PyUnicode_AsUTF8AndSize
34
35/* Ints */
36
37#define PyInt_Type PyLong_Type
38#define PyInt_Check PyLong_Check
39#define PyInt_CheckExact PyLong_CheckExact
40#define PyInt_FromString PyLong_FromString
41#define PyInt_FromLong PyLong_FromLong
42#define PyInt_FromSsize_t PyLong_FromSsize_t
43#define PyInt_FromSize_t PyLong_FromSize_t
44#define PyInt_AsLong PyLong_AsLong
45#define PyInt_AS_LONG PyLong_AS_LONG
46#define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
47#define PyInt_AsSsize_t PyLong_AsSsize_t
48
49/* Module init */
50
51#define MODULE_INIT_FUNC(name) \
52    PyMODINIT_FUNC PyInit_ ## name(void); \
53    PyMODINIT_FUNC PyInit_ ## name(void)
54
55#else
56
57/***** Python 2 *****/
58
59#define IS_PY3 0
60
61/* Strings */
62
63#define PyStr_Type PyString_Type
64#define PyStr_Check PyString_Check
65#define PyStr_CheckExact PyString_CheckExact
66#define PyStr_FromString PyString_FromString
67#define PyStr_FromStringAndSize PyString_FromStringAndSize
68#define PyStr_FromFormat PyString_FromFormat
69#define PyStr_FromFormatV PyString_FromFormatV
70#define PyStr_AsString PyString_AsString
71#define PyStr_Format PyString_Format
72#define PyStr_InternInPlace PyString_InternInPlace
73#define PyStr_InternFromString PyString_InternFromString
74#define PyStr_Decode PyString_Decode
75
76static inline PyObject *PyStr_Concat(PyObject *left, PyObject *right) {
77    PyObject *str = left;
78    Py_INCREF(left);  // reference to old left will be stolen
79    PyString_Concat(&str, right);
80    if (str) {
81        return str;
82    } else {
83        return NULL;
84    }
85}
86
87#define PyStr_AsUTF8String(str) (Py_INCREF(str), (str))
88#define PyStr_AsUTF8 PyString_AsString
89#define PyStr_AsUTF8AndSize(pystr, sizeptr) \
90    ((*sizeptr=PyString_Size(pystr)), PyString_AsString(pystr))
91
92#define PyBytes_Type PyString_Type
93#define PyBytes_Check PyString_Check
94#define PyBytes_CheckExact PyString_CheckExact
95#define PyBytes_FromString PyString_FromString
96#define PyBytes_FromStringAndSize PyString_FromStringAndSize
97#define PyBytes_FromFormat PyString_FromFormat
98#define PyBytes_FromFormatV PyString_FromFormatV
99#define PyBytes_Size PyString_Size
100#define PyBytes_GET_SIZE PyString_GET_SIZE
101#define PyBytes_AsString PyString_AsString
102#define PyBytes_AS_STRING PyString_AS_STRING
103#define PyBytes_AsStringAndSize PyString_AsStringAndSize
104#define PyBytes_Concat PyString_Concat
105#define PyBytes_ConcatAndDel PyString_ConcatAndDel
106#define _PyBytes_Resize _PyString_Resize
107
108/* Module init */
109
110#define PyModuleDef_HEAD_INIT 0
111
112typedef struct PyModuleDef {
113    int m_base;
114    const char* m_name;
115    const char* m_doc;
116    Py_ssize_t m_size;
117    PyMethodDef *m_methods;
118} PyModuleDef;
119
120#define PyModule_Create(def) \
121    Py_InitModule3((def)->m_name, (def)->m_methods, (def)->m_doc)
122
123#define MODULE_INIT_FUNC(name) \
124    static PyObject *PyInit_ ## name(void); \
125    void init ## name(void); \
126    void init ## name(void) { PyInit_ ## name(); } \
127    static PyObject *PyInit_ ## name(void)
128
129
130#endif
131
132#endif
Note: See TracBrowser for help on using the repository browser.