| 1 | /***************************************************************************** |
| 2 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | you may not use this file except in compliance with the License. |
| 4 | You may obtain a copy of the License at |
| 5 | |
| 6 | http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | |
| 8 | Unless required by applicable law or agreed to in writing, software |
| 9 | distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | See the License for the specific language governing permissions and |
| 12 | limitations under the License. |
| 13 | |
| 14 | See NOTICE file for details. |
| 15 | *****************************************************************************/ |
| 16 | #ifndef JP_PYTHONTYPES_H_ |
| 17 | #define JP_PYTHONTYPES_H_ |
| 18 | #include <Python.h> |
| 19 | #include <vector> |
| 20 | |
| 21 | /** |
| 22 | * This set of source are mostly sugar with some light weight |
| 23 | * reference management. Each type holds a reference for the duration of its |
| 24 | * scope. If the PyObject must survive the reference container lifespan |
| 25 | * then a keep must be called. keep() should only appear when returning from |
| 26 | * the python module. Any earlier than the return provides an opportunity for |
| 27 | * an exception to be throw which may leak. |
| 28 | * |
| 29 | * For this module to function properly, each call needs to be check against the |
| 30 | * python object model to verify. |
| 31 | * 1) Does a call return an object as a new reference or a borrowed one. |
| 32 | * 2) Does a call set an error that must be checked. |
| 33 | * 3) Does the call steal a reference to an object passed into it. |
| 34 | * |
| 35 | * Calls should always accept a raw PyObject* and return JPPyObject if |
| 36 | * the return can be an new object or PyObject* if the return is a borrowed |
| 37 | * reference. Obviously holding a borrowed reference can create issues as |
| 38 | * nothing prevents a borrowed object from falling out of the list and |
| 39 | * being deleted. |
| 40 | * |
| 41 | */ |
| 42 | |
| 43 | // Note: Python uses a sized size type. Thus we will map it to jlong. |
| 44 | // Note: for conversions we will use jint and jlong types so that we map directly to java. |
| 45 | // Note: Where possible we will use std::string in place of const char* |
| 46 | |
| 47 | #ifndef PyObject_HEAD |
| 48 | struct _object; |
| 49 | typedef _object PyObject; |
| 50 | #endif |
| 51 | |
| 52 | /** Reference to a Python object. |
| 53 | * |
| 54 | * This creates a reference on creation and deletes it on destruction. |
| 55 | * |
| 56 | * Because there is a cost associated with creating wrappers, most |
| 57 | * methods should be static if they don't actually require management. |
| 58 | * |
| 59 | * Methods will return either a bare PyObject* if they hold the |
| 60 | * object like a container, or a wrapper which will control the lifespan |
| 61 | * of the object. |
| 62 | * |
| 63 | * Python has a lot of difference cases for how it returns an object. |
| 64 | * - It can give a new object or set an error. |
| 65 | * - It can return NULL indicating there is no object. |
| 66 | * - It can give a borrowed object or NULL if it doesn't exist. |
| 67 | * - Or we can just have an existing object we want to use. |
| 68 | * |
| 69 | * With all these different methods, we need to have a policy that |
| 70 | * state how we want this object reference to be treated. Each |
| 71 | * policy will produce different actions on the creation of |
| 72 | * a reference wrapper. |
| 73 | * |
| 74 | */ |
| 75 | class JPPyObject |
| 76 | { |
| 77 | /** Create a new reference to a Python object. |
| 78 | * |
| 79 | * @param obj is the python object. |
| 80 | */ |
| 81 | explicit JPPyObject(PyObject* obj); |
| 82 | |
| 83 | public: |
| 84 | |
| 85 | /** |
| 86 | * This policy is used if we need to hold a reference to an existing |
| 87 | * object for some duration. The object may be null. |
| 88 | * |
| 89 | * Increment reference count if not null, and decrement when done. |
| 90 | */ |
| 91 | static JPPyObject use(PyObject* obj); |
| 92 | |
| 93 | /** |
| 94 | * This policy is used when we are given a new reference that we must |
| 95 | * destroy. This will steal a reference. |
| 96 | * |
| 97 | * claim reference, and decremented when done. Clears errors if NULL. |
| 98 | */ |
| 99 | static JPPyObject accept(PyObject* obj); |
| 100 | |
| 101 | /** |
| 102 | * This policy is used when we are given a new reference that we must |
| 103 | * destroy. This will steal a reference. |
| 104 | * |
| 105 | * Assert not null, claim reference, and decremented when done. |
| 106 | * Will throw an exception in the object is null. |
| 107 | */ |
| 108 | static JPPyObject claim(PyObject* obj); |
| 109 | |
| 110 | /** |
| 111 | * This policy is used when we are capturing an object returned from a python |
| 112 | * call that we are responsible for. This will steal a reference. |
| 113 | * |
| 114 | * Check for errors, assert not null, then claim. |
| 115 | * Will throw an exception an error occurs. |
| 116 | */ |
| 117 | static JPPyObject call(PyObject* obj); |
| 118 | |
| 119 | JPPyObject() : m_PyObject(nullptr) |
| 120 | { |
| 121 | } |
| 122 | |
| 123 | JPPyObject(const JPPyObject &self); |
| 124 | |
| 125 | ~JPPyObject(); |
| 126 | |
| 127 | JPPyObject& operator=(const JPPyObject& o); |
| 128 | |
| 129 | /** |
| 130 | * Keep an object by creating a reference. |
| 131 | * |
| 132 | * This should only appear in the return statement in the cpython module. |
| 133 | * The reference must not be null. Keep invalidates this handle from any |
| 134 | * further use as you were supposed to have called return. |
| 135 | * |
| 136 | * @return the pointer to the Python object. |
| 137 | */ |
| 138 | PyObject* keep(); |
| 139 | |
| 140 | /** Used in special case of exception handling. */ |
| 141 | PyObject* keepNull() |
| 142 | { |
| 143 | PyObject *out = m_PyObject; |
| 144 | m_PyObject = nullptr; |
| 145 | return out; |
| 146 | } |
| 147 | |
| 148 | /** Access the object. This should never appear in |
| 149 | * a return statement. |
| 150 | */ |
| 151 | PyObject* get() |
| 152 | { |
| 153 | return m_PyObject; |
| 154 | } |
| 155 | |
| 156 | /** Determine if this python reference is null. |
| 157 | * |
| 158 | * @returns true if null. |
| 159 | */ |
| 160 | bool isNull() const |
| 161 | { |
| 162 | return m_PyObject == nullptr; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Get a reference to Python None. |
| 167 | */ |
| 168 | static JPPyObject getNone(); |
| 169 | |
| 170 | void incref(); |
| 171 | void decref(); |
| 172 | |
| 173 | protected: |
| 174 | PyObject* m_PyObject{nullptr}; |
| 175 | } ; |
| 176 | |
| 177 | /**************************************************************************** |
| 178 | * String |
| 179 | ***************************************************************************/ |
| 180 | |
| 181 | /** Wrapper for the concept of a Python string. |
| 182 | * |
| 183 | * For the purposes of this interface we will hide the differences |
| 184 | * between unicode and string. |
| 185 | */ |
| 186 | class JPPyString : public JPPyObject |
| 187 | { |
| 188 | public: |
| 189 | |
| 190 | |
| 191 | /** Check if the object is a bytes or unicode. |
| 192 | * |
| 193 | * @returns true if the object is bytes or unicode. |
| 194 | */ |
| 195 | static bool check(PyObject* obj); |
| 196 | |
| 197 | /** Create a new string from utf8 encoded string. |
| 198 | * Note: java utf8 is not utf8. |
| 199 | * |
| 200 | * Python2 produced str unless unicode is set to |
| 201 | * true. Python3 will always produce a unicode string. |
| 202 | * |
| 203 | * @param str is the string to convert |
| 204 | */ |
| 205 | static JPPyObject fromStringUTF8(const string& str); |
| 206 | |
| 207 | /** Get a UTF-8 encoded string from Python |
| 208 | */ |
| 209 | static string asStringUTF8(PyObject* obj); |
| 210 | |
| 211 | static JPPyObject fromCharUTF16(jchar c); |
| 212 | static bool checkCharUTF16(PyObject* obj); |
| 213 | static jchar asCharUTF16(PyObject* obj); |
| 214 | |
| 215 | } ; |
| 216 | |
| 217 | /**************************************************************************** |
| 218 | * Container types |
| 219 | ***************************************************************************/ |
| 220 | |
| 221 | /** Wrapper for a Python sequence. |
| 222 | * |
| 223 | * In most cases, we will not use this directly, but rather convert to |
| 224 | * a JPPyObjectVector for easy access. |
| 225 | */ |
| 226 | class JPPySequence |
| 227 | { |
| 228 | JPPyObject m_Sequence; |
| 229 | |
| 230 | explicit JPPySequence(PyObject* obj) |
| 231 | { |
| 232 | m_Sequence = JPPyObject::use(obj); |
| 233 | } |
| 234 | |
| 235 | public: |
| 236 | |
| 237 | /** Needed for named constructor. |
| 238 | */ |
| 239 | JPPySequence(const JPPySequence& seq) = default; |
| 240 | |
| 241 | /** Use an existing Python sequence in C++. |
| 242 | */ |
| 243 | static JPPySequence use(PyObject* obj) |
| 244 | { |
| 245 | return JPPySequence(obj); |
| 246 | } |
| 247 | |
| 248 | JPPyObject operator[](jlong i); |
| 249 | |
| 250 | jlong size(); |
| 251 | |
| 252 | JPPySequence& operator= (const JPPySequence&) = delete; |
| 253 | |
| 254 | } ; |
| 255 | |
| 256 | /** For purposes of efficiency, we should only convert a sequence once per |
| 257 | * method call. This class is to support that operation. |
| 258 | * |
| 259 | * THis object is read only. |
| 260 | */ |
| 261 | class JPPyObjectVector |
| 262 | { |
| 263 | public: |
| 264 | |
| 265 | /** Use an existing sequence members as a vector. |
| 266 | */ |
| 267 | explicit JPPyObjectVector(PyObject* sequence); |
| 268 | |
| 269 | /** Use an existing sequence members as a vector plus the |
| 270 | * object instance. |
| 271 | */ |
| 272 | JPPyObjectVector(PyObject* inst, PyObject* sequence); |
| 273 | |
| 274 | size_t size() const |
| 275 | { |
| 276 | return m_Contents.size(); |
| 277 | } |
| 278 | |
| 279 | PyObject* operator[](Py_ssize_t i) |
| 280 | { |
| 281 | return m_Contents[i].get(); |
| 282 | } |
| 283 | |
| 284 | JPPyObject& getInstance() |
| 285 | { |
| 286 | return m_Instance; |
| 287 | } |
| 288 | |
| 289 | // disallow copying |
| 290 | JPPyObjectVector& operator= (const JPPyObjectVector& ) = delete; |
| 291 | JPPyObjectVector(const JPPyObjectVector& ) = delete; |
| 292 | |
| 293 | private: |
| 294 | JPPyObject m_Instance; |
| 295 | JPPyObject m_Sequence; |
| 296 | vector<JPPyObject> m_Contents; |
| 297 | } ; |
| 298 | |
| 299 | /**************************************************************************** |
| 300 | * Error handling |
| 301 | ***************************************************************************/ |
| 302 | |
| 303 | /** Front end for all Python exception handling. |
| 304 | * |
| 305 | * To issue an error from within the C++ layer use the appropriate |
| 306 | * JP_RAISE_* macro. If within the Python extension module use |
| 307 | * the Python interface. |
| 308 | * |
| 309 | */ |
| 310 | namespace JPPyErr |
| 311 | { |
| 312 | bool fetch(JPPyObject& exceptionClass, JPPyObject& exceptionValue, JPPyObject& exceptionTrace); |
| 313 | void restore(JPPyObject& exceptionClass, JPPyObject& exceptionValue, JPPyObject& exceptionTrace); |
| 314 | } |
| 315 | |
| 316 | /** Memory management for handling a python exception currently in progress. */ |
| 317 | class JPPyErrFrame |
| 318 | { |
| 319 | public: |
| 320 | JPPyObject m_ExceptionClass; |
| 321 | JPPyObject m_ExceptionValue; |
| 322 | JPPyObject m_ExceptionTrace; |
| 323 | bool good; |
| 324 | |
| 325 | JPPyErrFrame(); |
| 326 | ~JPPyErrFrame(); |
| 327 | void clear(); |
| 328 | void normalize(); |
| 329 | } ; |
| 330 | |
| 331 | /** Used to establish a python lock when called from a |
| 332 | * thread external to python such a java. |
| 333 | */ |
| 334 | class JPPyCallAcquire |
| 335 | { |
| 336 | public: |
| 337 | /** Acquire the lock. */ |
| 338 | JPPyCallAcquire(); |
| 339 | /* Release the lock. */ |
| 340 | ~JPPyCallAcquire(); |
| 341 | private: |
| 342 | long m_State; |
| 343 | } ; |
| 344 | |
| 345 | /** Used when leaving python to an external potentially |
| 346 | * blocking call |
| 347 | */ |
| 348 | class JPPyCallRelease |
| 349 | { |
| 350 | public: |
| 351 | /** Release the lock. */ |
| 352 | JPPyCallRelease(); |
| 353 | /** Reacquire the lock. */ |
| 354 | ~JPPyCallRelease(); |
| 355 | private: |
| 356 | PyThreadState* m_State1; |
| 357 | } ; |
| 358 | |
| 359 | class JPPyBuffer |
| 360 | { |
| 361 | public: |
| 362 | /** |
| 363 | * Attempt to create a buffer view. |
| 364 | * |
| 365 | * If this fails then valid will return false and |
| 366 | * PyExc_BufferError will be set. Clear the exception if |
| 367 | * the alternative methods are used. |
| 368 | * |
| 369 | * @param obj |
| 370 | * @param flags |
| 371 | */ |
| 372 | JPPyBuffer(PyObject* obj, int flags); |
| 373 | ~JPPyBuffer(); |
| 374 | |
| 375 | char *getBufferPtr(std::vector<Py_ssize_t>& indices) const; |
| 376 | |
| 377 | Py_buffer& getView() |
| 378 | { |
| 379 | return m_View; |
| 380 | } |
| 381 | |
| 382 | bool valid() const |
| 383 | { |
| 384 | return m_Valid; |
| 385 | } |
| 386 | |
| 387 | private: |
| 388 | Py_buffer m_View{}; |
| 389 | bool m_Valid; |
| 390 | } ; |
| 391 | |
| 392 | #endif |