forpy_mod.f90 375 KB
Newer Older
1
! Copyright (C) 2017-2020  Elias Rabel
Marco Govoni's avatar
Marco Govoni committed
2
3
!
! This program is free software: you can redistribute it and/or modify
4
5
! it under the terms of the GNU Lesser General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
Marco Govoni's avatar
Marco Govoni committed
6
7
8
! (at your option) any later version.
!
! This program is distributed in the hope that it will be useful,
9
! but WITHOUT ANY WARRANTY; without even the implied warranty of
Marco Govoni's avatar
Marco Govoni committed
10
11
12
13
14
15
16
17
18
19
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
! GNU Lesser General Public License for more details.
!
! You should have received a copy of the GNU Lesser General Public License
! along with this program.  If not, see <http://www.gnu.org/licenses/>.

module forpy_mod
!! author: Elias Rabel
!!
!! Forpy: A library for Fortran-Python interoperability.
20
!!
Marco Govoni's avatar
Marco Govoni committed
21
22
23
24
25
26
27
28
29
30
31
32
!! Forpy allows you to use Python features within Fortran ("Python in Fortran")
!! For example: Python modules, datastructures such as list, dict, tuple
!! Furthermore you can write Python (extension) modules in Fortran ("Fortran in Python")

!  Contact:
!  Let me know, if you find this library useful:
!  Mail: ylikx.0 at gmail dot com
!  Web:  https://github.com/ylikx

!  This project uses the fypp preprocessor (https://github.com/aradi/fypp)
!  to generate the code
!  Do not edit forpy_mod.F90, edit forpy_mod.fypp
33
!
Marco Govoni's avatar
Marco Govoni committed
34
35
36
37
38
39
40
41
42
43
44

#ifdef PYTHON2_32
#define PYTHON2
#endif

use, intrinsic :: iso_c_binding, only: C_CHAR, C_NULL_CHAR, C_INT, C_LONG, C_LONG_LONG, C_NEW_LINE, c_ptr, C_DOUBLE, &
                                       C_DOUBLE_COMPLEX, c_associated, C_NULL_PTR, c_loc, c_f_pointer, &
                                       c_funptr, c_funloc, C_NULL_FUNPTR, C_INTPTR_T

use, intrinsic :: iso_fortran_env, only: int64, int32, real32, real64

45
implicit none
Marco Govoni's avatar
Marco Govoni committed
46
47
48
49

public :: object, type_py, list, dict, tuple, bytes, str, unicode, module_py, &
NoneType, ndarray, Sequence, MutableSequence, ImmutableSequence, Mapping, &
tuple_create, list_create, dict_create, bytes_create, str_create, &
50
unicode_create, NoneType_create, ndarray_create, ndarray_create_nocopy, &
Marco Govoni's avatar
Marco Govoni committed
51
52
ndarray_create_empty, ndarray_create_zeros, ndarray_create_ones, &
import_py, call_py, call_py_noret, assign_py, cast, cast_nonstrict, &
53
PythonMethodTable, PythonModule, forpy_initialize, &
Marco Govoni's avatar
Marco Govoni committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
forpy_finalize, is_long, is_list, is_tuple, is_bytes, is_dict, &
is_float, is_complex, is_bool, is_unicode, is_int, is_str, is_none, &
is_null, is_ndarray, exception_matches, err_clear, err_print, have_exception, &
raise_exception, print_py, get_sys_path, run_string, unsafe_cast_from_c_ptr

! ERROR CODES
integer(kind=C_INT), public, parameter :: NO_NUMPY_ERROR = 2_C_INT
integer(kind=C_INT), public, parameter :: EXCEPTION_ERROR = -1_C_INT

! Flags used for Python extension development
integer(kind=C_INT), public, parameter :: METH_VARARGS = 1_C_INT
integer(kind=C_INT), public, parameter :: METH_KEYWORDS = 2_C_INT
integer(kind=C_INT), public, parameter :: METH_NOARGS = 4_C_INT
integer(kind=C_INT), public, parameter :: METH_O = 8_C_INT

integer, public, parameter :: PY_SSIZE_T_KIND = C_INTPTR_T

PRIVATE

! These global variables shall be set in
! forpy_initialize only and never changed afterwards!
75
integer, private, save :: global_forpy_initialized = 0
Marco Govoni's avatar
Marco Govoni committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
type(c_ptr), private, save :: global_numpy_mod = C_NULL_PTR
type(c_ptr), private, save :: global_numpy_asarray_method = C_NULL_PTR
! the location of the singleton Python Py_NoneStruct method
! initialised in forpy_initialize - if not called, working with None
! is impossible
type(c_ptr), private, save :: global_Py_NoneStruct_ptr = C_NULL_PTR

! Similar for the 2 singleton bools
type(c_ptr), private, save :: global_Py_TrueStruct_ptr = C_NULL_PTR
type(c_ptr), private, save :: global_Py_FalseStruct_ptr = C_NULL_PTR

type(c_ptr), private, save :: global_numpy_ndarray_typeobj = C_NULL_PTR

!pointers to type-objects of fundamental datatypes
!initialised in forpy_initialize
type(c_ptr), private, save :: global_pyfloat_type_ptr = C_NULL_PTR
type(c_ptr), private, save :: global_pycomplex_type_ptr = C_NULL_PTR
type(c_ptr), private, save :: global_pybool_type_ptr = C_NULL_PTR
type(c_ptr), private, save :: global_pyunicode_type_ptr = C_NULL_PTR

type, bind(c) :: Py_buffer
  type(c_ptr) :: buf
  type(c_ptr) :: obj
  integer(kind=PY_SSIZE_T_KIND) :: len
  integer(kind=PY_SSIZE_T_KIND) :: itemsize
  integer(kind=C_INT) :: readonly
  integer(kind=C_INT) :: ndim
  type(c_ptr) :: format
  type(c_ptr) :: shape
  type(c_ptr) :: strides
  type(c_ptr) :: suboffsets
#ifdef PYTHON2
  integer(kind=PY_SSIZE_T_KIND) :: smalltable(2)
#endif
  type(c_ptr) :: internal
end type

type, bind(c) :: PyObject
#ifdef Py_DEBUG
    type(c_ptr) :: ob_next
    type(c_ptr) :: ob_prev
#endif
    integer(kind=PY_SSIZE_T_KIND) :: ob_refcnt
    type(c_ptr) :: ob_type
end type

type, bind(c) :: PyTypeObject
#ifdef Py_DEBUG
    type(c_ptr) :: ob_next
    type(c_ptr) :: ob_prev
#endif
    integer(kind=PY_SSIZE_T_KIND) :: ob_refcnt
    type(c_ptr) :: ob_type
    integer(kind=PY_SSIZE_T_KIND) :: ob_size

    type(c_ptr) :: tp_name ! For printing, in format "<module>.<name>"
    integer(kind=PY_SSIZE_T_KIND) :: tp_basicsize, tp_itemsize ! For allocation

    !Methods to implement standard operations

    type(c_ptr) :: tp_dealloc
    type(c_ptr) :: tp_print
    type(c_ptr) :: tp_getattr
    type(c_ptr) :: tp_setattr
    type(c_ptr) :: tp_compare
    type(c_ptr) :: tp_repr

    !Method suites for standard classes

    type(c_ptr) :: tp_as_number
    type(c_ptr) :: tp_as_sequence
    type(c_ptr) :: tp_as_mapping

    ! More standard operations (here for binary compatibility)

    type(c_ptr) :: tp_hash
    type(c_ptr) :: tp_call
    type(c_ptr) :: tp_str
    type(c_ptr) :: tp_getattro
    type(c_ptr) :: tp_setattro

    ! Functions to access object as input/output buffer
    type(c_ptr) :: tp_as_buffer

    !Flags to define presence of optional/expanded features
    integer(kind=C_LONG) :: tp_flags  ! Python2: long, Python3: unsigned long

    type(c_ptr) :: tp_doc ! Documentation string

    !call function for all accessible objects
    type(c_ptr) :: tp_traverse

    ! delete references to contained objects
    type(c_ptr) :: tp_clear

    ! Assigned meaning in release 2.1
    ! rich comparisons
    type(c_ptr) :: tp_richcompare

    ! weak reference enabler
    integer(kind=PY_SSIZE_T_KIND) :: tp_weaklistoffset

    !Added in release 2.2
    !Iterators
    type(c_ptr) :: tp_iter
    type(c_ptr) :: tp_iternext

    ! Attribute descriptor and subclassing stuff
    type(c_ptr) :: tp_methods
    type(c_ptr) :: tp_members
    type(c_ptr) :: tp_getset
    type(c_ptr) :: tp_base
    type(c_ptr) :: tp_dict
    type(c_ptr) :: tp_descr_get
    type(c_ptr) :: tp_descr_set
    integer(kind=PY_SSIZE_T_KIND) :: tp_dictoffset
    type(c_ptr) :: tp_init
    type(c_ptr) :: tp_alloc
    type(c_ptr) :: tp_new
    type(c_ptr) :: tp_free ! Low-level free-memory routine
    type(c_ptr) :: tp_is_gc ! For PyObject_IS_GC
    type(c_ptr) :: tp_bases
    type(c_ptr) :: tp_mro ! method resolution order
    type(c_ptr) :: tp_cache
    type(c_ptr) :: tp_subclasses
    type(c_ptr) :: tp_weaklist
    type(c_ptr) :: tp_del

    ! Type attribute cache version tag. Added in version 2.6
    integer(kind=C_INT) :: tp_version_tag

    ! additionally Python3 has this field:
    ! destructor tp_finalize;
    ! we should be fine without it, since all we actually need is the offset
    ! of tp_flags and we are not using arrays of PyTypeObjects

end type

type, bind(c) :: Py_complex
  real(kind=C_DOUBLE) :: real_part
  real(kind=C_DOUBLE) :: imag_part
end type

type, bind(c) :: PyMethodDef
  type(c_ptr) :: ml_name
  type(c_funptr) :: ml_meth
  integer(kind=C_INT) :: ml_flags
  type(c_ptr) :: ml_doc
end type

type, bind(c) :: PyModuleDef_Base
  integer(kind=PY_SSIZE_T_KIND) :: ob_refcnt !PyObject_HEAD  (init to 1)
  type(c_ptr) :: ob_type ! from PyObject_HEAD (init to NULL)
229

Marco Govoni's avatar
Marco Govoni committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  type(c_ptr) :: m_init
  integer(kind=PY_SSIZE_T_KIND) :: m_index
  type(c_ptr) :: m_copy
end type

type, bind(c) :: PyModuleDef
  type(PyModuleDef_Base) :: m_base
  type(c_ptr) :: m_name
  type(c_ptr) :: m_doc
  integer(kind=PY_SSIZE_T_KIND) :: m_size
  type(c_ptr) :: m_methods
  type(c_ptr) :: m_slots
  type(c_funptr) :: m_traverse
  type(c_funptr) :: m_clear
  type(c_funptr) :: m_free
end type

interface
  subroutine Py_Initialize() bind(c, name="Py_Initialize")
  end subroutine

251
252
253
254
255
  function Py_IsInitialized() bind(c, name="Py_IsInitialized") result(r)
    import C_INT
    integer(kind=C_INT) :: r
  end function

Marco Govoni's avatar
Marco Govoni committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  subroutine Py_Finalize() bind(c, name="Py_Finalize")
  end subroutine

  function PyImport_ImportModule(a_name) bind(c, name="PyImport_ImportModule") result(m)
    import c_ptr, C_CHAR
    character(kind=C_CHAR), dimension(*) :: a_name
    type(c_ptr) :: m
  end function

  function PyRun_SimpleString(command) bind(c, name="PyRun_SimpleString") result(r)
    import C_INT, C_CHAR
    character(kind=C_CHAR), dimension(*) :: command
    integer(kind=C_INT) :: r
  end function

  function PyList_New(len) bind(c, name="PyList_New") result(r)
    import c_ptr, PY_SSIZE_T_KIND
    integer(kind=PY_SSIZE_T_KIND), value :: len
    type(c_ptr) :: r
  end function

  function PyDict_New() bind(c, name="PyDict_New") result(r)
    import c_ptr
    type(c_ptr) :: r
  end function

  !int PyList_Append(PyObject *list, PyObject *item)
  function PyList_Append(list, item) bind(c, name="PyList_Append") result(r)
    import c_ptr, C_INT
    type(c_ptr), value :: list
    type(c_ptr), value :: item
    integer(kind=C_INT) :: r
  end function
289

Marco Govoni's avatar
Marco Govoni committed
290
291
292
293
294
  function PyList_Sort(list) bind(c, name="PyList_Sort") result(r)
    import c_ptr, C_INT
    type(c_ptr), value :: list
    integer(kind=C_INT) :: r
  end function
295

Marco Govoni's avatar
Marco Govoni committed
296
297
298
299
300
  function PyList_Reverse(list) bind(c, name="PyList_Reverse") result(r)
    import c_ptr, C_INT
    type(c_ptr), value :: list
    integer(kind=C_INT) :: r
  end function
301

Marco Govoni's avatar
Marco Govoni committed
302
303
304
305
306
307
308
309
  !int PyList_Insert(PyObject *list, Py_ssize_t index, PyObject *item)
  function PyList_Insert(list, index, item) bind(c, name="PyList_Insert") result(r)
    import c_ptr, C_INT, PY_SSIZE_T_KIND
    type(c_ptr), value :: list
    integer(kind=PY_SSIZE_T_KIND), value :: index
    type(c_ptr), value :: item
    integer(kind=C_INT) :: r
  end function
310

Marco Govoni's avatar
Marco Govoni committed
311
312
313
314
315
316
  !PyObject* PyLong_FromLongLong(long long ival)
  function PyLong_FromLongLong(ival) bind(c, name="PyLong_FromLongLong") result(r)
    import c_ptr, C_LONG_LONG
    integer(kind=C_LONG_LONG), value :: ival
    type(c_ptr) :: r
  end function
317

Marco Govoni's avatar
Marco Govoni committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
  !PY_LONG_LONG PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)
  function PyLong_AsLongLongAndOverflow(obj, overflow) bind(c, name="PyLong_AsLongLongAndOverflow") result(r)
    import c_ptr, C_LONG_LONG, C_INT
    type(c_ptr), value :: obj
    integer(kind=C_INT) :: overflow
    integer(kind=C_LONG_LONG) :: r
  end function

#ifdef PYTHON2
  !PyObject* PyInt_FromLong(Py_ssize_t ival)
  function PyInt_FromLong(ival) bind(c, name="PyInt_FromLong") result(r)
    import c_ptr, C_LONG
    integer(kind=C_LONG), value :: ival
    type(c_ptr) :: r
  end function
#endif

  !void Py_DecRef(PyObject *o)
  subroutine Py_DecRef(o) bind(c, name="Py_DecRef")
    import c_ptr
    type(c_ptr), value :: o
  end subroutine

  !void Py_IncRef(PyObject *o)
  subroutine Py_IncRef(o) bind(c, name="Py_IncRef")
    import c_ptr
    type(c_ptr), value :: o
  end subroutine

  !PyObject* PyObject_GetItem(PyObject *o, PyObject *key)
  function PyObject_GetItem(o, key) bind(c, name="PyObject_GetItem") result(r)
    import c_ptr
    type(c_ptr), value :: o
    type(c_ptr), value :: key
    type(c_ptr) :: r
  end function

  !int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)
  function PyObject_SetItem(o, key, v) bind(c, name="PyObject_SetItem") result(r)
    import c_ptr, C_INT
    type(c_ptr), value :: o
    type(c_ptr), value :: key
    type(c_ptr), value :: v
    integer(kind=C_INT) :: r
  end function
363

Marco Govoni's avatar
Marco Govoni committed
364
365
366
367
368
369
370
371
372
373
374
375
376
  !Py_ssize_t PyObject_Length(PyObject *o)
  function PyObject_Length(o) bind(c, name="PyObject_Length") result(r)
    import c_ptr, PY_SSIZE_T_KIND
    type(c_ptr), value :: o
    integer(kind=PY_SSIZE_T_KIND) :: r
  end function

  !int PyObject_IsTrue(PyObject *o)
  function PyObject_IsTrue(o) bind(c, name="PyObject_IsTrue") result(r)
    import c_ptr, C_INT
    type(c_ptr), value :: o
    integer(kind=C_INT) :: r
  end function
377

Marco Govoni's avatar
Marco Govoni committed
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
  !PyObject* PyObject_Str(PyObject *o)
  function PyObject_Str(o) bind(c, name="PyObject_Str") result(r)
    import c_ptr
    type(c_ptr), value :: o
    type(c_ptr) :: r
  end function

  !int PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v)
  function PySequence_SetItem(o, i, v) bind(c, name="PySequence_SetItem") result(r)
    import c_ptr, C_INT, PY_SSIZE_T_KIND
    type(c_ptr), value :: o
    integer(kind=PY_SSIZE_T_KIND), value :: i
    type(c_ptr), value :: v
    integer(kind=C_INT) :: r
  end function

  !PyObject* PySequence_GetItem(PyObject *o, Py_ssize_t i)
  function PySequence_GetItem(o, i) bind(c, name="PySequence_GetItem") result(r)
    import c_ptr, C_INT, PY_SSIZE_T_KIND
    type(c_ptr), value :: o
    integer(kind=PY_SSIZE_T_KIND), value :: i
    type(c_ptr) :: r
  end function

  !int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)
  function PyTuple_SetItem(p, pos, o) bind(c, name="PyTuple_SetItem") result(r)
    import c_ptr, C_INT, PY_SSIZE_T_KIND
    type(c_ptr), value :: p
    integer(kind=PY_SSIZE_T_KIND), value :: pos
    type(c_ptr), value :: o
    integer(kind=C_INT) :: r
  end function

  !PyObject* PyTuple_New(Py_ssize_t len)
  function PyTuple_New(len) bind(c, name="PyTuple_New") result(r)
    import c_ptr, PY_SSIZE_T_KIND
    integer(kind=PY_SSIZE_T_KIND), value :: len
    type(c_ptr) :: r
  end function

  !long long PyLong_AsLongLong(PyObject *io)
  function PyLong_AsLongLong(io) bind(c, name="PyLong_AsLongLong") result(r)
    import c_ptr, C_LONG_LONG
    type(c_ptr), value :: io
    integer(kind=C_LONG_LONG) :: r
  end function

  !PyObject* PyFloat_FromDouble(double v)
  function PyFloat_FromDouble(v) bind(c, name="PyFloat_FromDouble") result(r)
    import c_ptr, C_DOUBLE
    real(kind=C_DOUBLE), value :: v
    type(c_ptr) :: r
  end function
431

Marco Govoni's avatar
Marco Govoni committed
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
  !double PyFloat_AsDouble(PyObject *pyfloat)
  function PyFloat_AsDouble(pyfloat) bind(c, name="PyFloat_AsDouble") result(r)
    import c_ptr, C_DOUBLE
    type(c_ptr), value :: pyfloat
    real(kind=C_DOUBLE) :: r
  end function

  function PyComplex_FromDoubles(re, im) bind(c, name="PyComplex_FromDoubles") result(r)
    import c_ptr, C_DOUBLE
    real(kind=C_DOUBLE), value :: re, im
    type(c_ptr) :: r
  end function

  function PyComplex_AsCComplex(obj) bind(c, name="PyComplex_AsCComplex") result(r)
    import c_ptr, Py_complex
    type(c_ptr), value :: obj
    type(Py_complex) :: r
  end function

  function PyErr_Occurred() bind(c, name="PyErr_Occurred") result(r)
    import c_ptr
    type(c_ptr) :: r
  end function

  !void PyErr_Print()
  subroutine PyErr_Print() bind(c, name="PyErr_Print")
  end subroutine

  !void PyErr_Clear()
  subroutine PyErr_Clear() bind(c, name="PyErr_Clear")
  end subroutine

#ifdef PYTHON2
  function PyBytes_FromStringAndSize(v, len) bind(c, name="PyString_FromStringAndSize") result(r)
#else
  function PyBytes_FromStringAndSize(v, len) bind(c, name="PyBytes_FromStringAndSize") result(r)
#endif
    import c_ptr, PY_SSIZE_T_KIND, C_CHAR
    character(kind=C_CHAR), dimension(*), intent(in) :: v
    integer(kind=PY_SSIZE_T_KIND), value :: len
    type(c_ptr) :: r
  end function

#ifdef PYTHON2
  function PyBytes_FromString(v) bind(c, name="PyString_FromString") result(r)
#else
  function PyBytes_FromString(v) bind(c, name="PyBytes_FromString") result(r)
#endif
    import c_ptr, C_CHAR
    character(kind=C_CHAR), dimension(*), intent(in) :: v
    type(c_ptr) :: r
  end function
484

Marco Govoni's avatar
Marco Govoni committed
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
  !char* PyBytes_AsString(PyObject *o)
#ifdef PYTHON2
  function PyBytes_AsString(o) bind(c, name="PyString_AsString") result(r)
#else
  function PyBytes_AsString(o) bind(c, name="PyBytes_AsString") result(r)
#endif
    import c_ptr
    type(c_ptr), value :: o
    type(c_ptr) :: r
  end function

  ! PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name)
  function PyObject_GetAttr(o, attr_name) bind(c, name="PyObject_GetAttr") result(r)
    import c_ptr
    type(c_ptr), value :: o, attr_name
    type(c_ptr) :: r
  end function
502

Marco Govoni's avatar
Marco Govoni committed
503
504
505
506
507
  function PyObject_SetAttr(o, attr_name, v) bind(c, name="PyObject_SetAttr") result(r)
    import c_ptr, C_INT
    type(c_ptr), value :: o, attr_name, v
    integer(kind=C_INT) :: r
  end function
508

Marco Govoni's avatar
Marco Govoni committed
509
510
511
512
513
514
  !int PyObject_DelItem(PyObject *o, PyObject *key)
  function PyObject_DelItem(o, key) bind(c, name="PyObject_DelItem") result(r)
    import c_ptr, C_INT
    type(c_ptr), value :: o, key
    integer(kind=C_INT) :: r
  end function
515

Marco Govoni's avatar
Marco Govoni committed
516
517
518
519
520
521
  !int PySequence_DelItem(PyObject *o, Py_ssize_t i)
  function PySequence_DelItem(o, i) bind(c, name="PySequence_DelItem") result(r)
    import c_ptr, C_INT, PY_SSIZE_T_KIND
    type(c_ptr), value :: o
    integer(kind=PY_SSIZE_T_KIND), value :: i
    integer(kind=C_INT) :: r
522
  end function
Marco Govoni's avatar
Marco Govoni committed
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597

  !PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw)
  function PyObject_Call(callable_object, args, kw) bind(c, name="PyObject_Call") result(r)
    import c_ptr
    type(c_ptr), value :: callable_object, args, kw
    type(c_ptr) :: r
  end function

  !PyObject *PyMemoryView_FromBuffer(Py_buffer *view)
  function PyMemoryView_FromBuffer(view) bind(c, name="PyMemoryView_FromBuffer") result(r)
    import Py_buffer, c_ptr
    type(Py_buffer) :: view
    type(c_ptr) :: r
  end function

  !PyObject *PyMemoryView_FromObject(PyObject *obj)
  function PyMemoryView_FromObject(obj) bind(c, name="PyMemoryView_FromObject") result(r)
    import c_ptr
    type(c_ptr), value :: obj
    type(c_ptr) :: r
  end function

  !int PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)
  function PyObject_GetBuffer(obj, view, flags) bind(c, name="PyObject_GetBuffer") result(r)
    import Py_buffer, c_ptr, C_INT
    type(c_ptr), value :: obj
    type(Py_buffer) :: view
    integer(kind=C_INT), value :: flags
    integer(kind=C_INT) :: r
  end function

  !int PyBuffer_IsContiguous(Py_buffer *view, char fortran)
  function PyBuffer_IsContiguous(view, fortran) bind(c, name="PyBuffer_IsContiguous") result(r)
    import Py_buffer, C_INT, C_CHAR
    type(Py_buffer) :: view
    character(kind=C_CHAR), value :: fortran
    integer(kind=C_INT) :: r
  end function

  !void PyBuffer_Release(Py_buffer *view)
  subroutine PyBuffer_Release(view) bind(c, name="PyBuffer_Release")
    import Py_buffer
    type(Py_buffer) :: view
  end subroutine

  !int PyObject_IsInstance(PyObject *inst, PyObject *cls)
  function PyObject_IsInstance(inst, cls) bind(c, name="PyObject_IsInstance") result(r)
    import C_INT, c_ptr
    type(c_ptr), value :: inst
    type(c_ptr), value :: cls
    integer(kind=C_INT) :: r
  end function

  !int PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
  function PyType_IsSubtype(a, b) bind(c, name="PyType_IsSubtype") result(r)
    import C_INT, c_ptr
    type(c_ptr), value :: a
    type(c_ptr), value :: b
    integer(kind=C_INT) :: r
  end function

  !PyObject* PyBool_FromLong(long v)
  function PyBool_FromLong(v) bind(c, name="PyBool_FromLong") result(r)
    import C_LONG, c_ptr
    integer(kind=C_LONG), value :: v
    type(c_ptr) :: r
  end function

  !PyObject* PyUnicode_DecodeUTF8(const char *s, Py_ssize_t size, const char *errors)
#ifndef PYTHON2
  function PyUnicode_DecodeUTF8(s, size, errors) bind(c, name="PyUnicode_DecodeUTF8") result(r)
#endif

#ifdef PYTHON2
#ifdef PYTHON_NARROW
598
  function PyUnicode_DecodeUTF8(s, size, errors) bind(c, name="PyUnicodeUCS2_DecodeUTF8") result(r)
Marco Govoni's avatar
Marco Govoni committed
599
600
601
602
603
604
605
606
607
608
#else
  function PyUnicode_DecodeUTF8(s, size, errors) bind(c, name="PyUnicodeUCS4_DecodeUTF8") result(r)
#endif
#endif
    import c_ptr, PY_SSIZE_T_KIND, C_CHAR
    character(kind=C_CHAR), dimension(*) :: s
    integer(kind=PY_SSIZE_T_KIND), value :: size
    character(kind=C_CHAR), dimension(*) :: errors
    type(c_ptr) :: r
  end function
609

Marco Govoni's avatar
Marco Govoni committed
610
611
612
613
614
615
616
617
#ifndef PYTHON2
  ! Since Python 3.3 in C-API
  !char* PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *size)
  function PyUnicode_AsUTF8AndSize(unicode, size) bind(c, name="PyUnicode_AsUTF8AndSize") result(r)
    import c_ptr, PY_SSIZE_T_KIND
    type(c_ptr), value :: unicode
    integer(kind=PY_SSIZE_T_KIND) :: size
    type(c_ptr) :: r
618
619
620
621
622
623
624
625
626
627
628
629
630
631
  end function
#endif

  !PyObject* PyUnicode_AsUTF8String(PyObject *unicode)
#ifdef PYTHON2
#ifdef PYTHON2_NARROW
  function PyUnicode_AsUTF8String(unicode) bind(c, name="PyUnicodeUCS2_AsUTF8String") result(r)
#else
  function PyUnicode_AsUTF8String(unicode) bind(c, name="PyUnicodeUCS4_AsUTF8String") result(r)
#endif
    import c_ptr
    type(c_ptr), value :: unicode
    type(c_ptr) :: r
  end function
Marco Govoni's avatar
Marco Govoni committed
632
633
634
635
636
637
#endif

  function PyEval_GetBuiltins() bind(c, name="PyEval_GetBuiltins") result(r)
    import c_ptr
    type(c_ptr) :: r
  end function
638

Marco Govoni's avatar
Marco Govoni committed
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
  !PyObject* PyDict_GetItemString(PyObject *p, const char *key)
  function PyDict_GetItemString(p, key) bind(c, name="PyDict_GetItemString") result(r)
    import c_ptr, C_CHAR
    type(c_ptr), value :: p
    character(kind=C_CHAR), dimension(*) :: key
    type(c_ptr) :: r
  end function

!void PyErr_SetString(PyObject *type, const char *message)
subroutine PyErr_SetString(a_type, message) bind(c, name="PyErr_SetString")
  import c_ptr, C_CHAR
  type(c_ptr), value :: a_type
  character(kind=C_CHAR), dimension(*) :: message
end subroutine

!int PyErr_GivenExceptionMatches(PyObject *given, PyObject *exc)
function PyErr_GivenExceptionMatches(given, exc) bind(c, name="PyErr_GivenExceptionMatches") result(r)
  import c_ptr, C_INT
  type(c_ptr), value :: given, exc
  integer(kind=C_INT) :: r
end function

function PySequence_Tuple(o) bind(c, name="PySequence_Tuple") result(r)
  import c_ptr
  type(c_ptr), value :: o
  type(c_ptr) :: r
end function

function PySequence_List(o) bind(c, name="PySequence_List") result(r)
  import c_ptr
  type(c_ptr), value :: o
  type(c_ptr) :: r
end function

subroutine PyDict_Clear(p) bind(c, name="PyDict_Clear")
  import c_ptr
  type(c_ptr), value :: p
end subroutine

function PyDict_Copy(p) bind(c, name="PyDict_Copy") result(r)
  import c_ptr
  type(c_ptr), value :: p
  type(c_ptr) :: r
end function

!PyObject* PyDict_Items(PyObject *p)
function PyDict_Items(p) bind(c, name="PyDict_Items") result(r)
  import c_ptr
  type(c_ptr), value :: p
  type(c_ptr) :: r
end function

!PyObject* PyDict_Keys(PyObject *p)
function PyDict_Keys(p) bind(c, name="PyDict_Keys") result(r)
  import c_ptr
  type(c_ptr), value :: p
  type(c_ptr) :: r
end function

!PyObject* PyDict_Values(PyObject *p)
function PyDict_Values(p) bind(c, name="PyDict_Values") result(r)
  import c_ptr
  type(c_ptr), value :: p
  type(c_ptr) :: r
end function

!void PyBuffer_FillContiguousStrides(int ndim, Py_ssize_t *shape, Py_ssize_t *strides, Py_ssize_t itemsize, char order)
subroutine PyBuffer_FillContiguousStrides(ndim, shape, strides, itemsize, order) bind(c, name="PyBuffer_FillContiguousStrides")
  import c_ptr, C_INT, C_CHAR, PY_SSIZE_T_KIND
  integer(kind=C_INT), value :: ndim
  type(c_ptr), value :: shape
  type(c_ptr), value :: strides
  integer(kind=PY_SSIZE_T_KIND), value :: itemsize
  character(kind=C_CHAR), value :: order
end subroutine

function PySequence_Contains(o, a_value) bind(c, name="PySequence_Contains") result(r)
  import c_ptr, C_INT
  type(c_ptr), value :: o
718
  type(c_ptr), value :: a_value
Marco Govoni's avatar
Marco Govoni committed
719
720
721
722
723
724
  integer(kind=C_INT) :: r
end function

function PySequence_Index(o, a_value) bind(c, name="PySequence_Index") result(r)
  import c_ptr, PY_SSIZE_T_KIND
  type(c_ptr), value :: o
725
  type(c_ptr), value :: a_value
Marco Govoni's avatar
Marco Govoni committed
726
727
728
729
730
731
  integer(kind=PY_SSIZE_T_KIND) :: r
end function

function PySequence_Count(o, a_value) bind(c, name="PySequence_Count") result(r)
  import c_ptr, PY_SSIZE_T_KIND
  type(c_ptr), value :: o
732
  type(c_ptr), value :: a_value
Marco Govoni's avatar
Marco Govoni committed
733
734
735
736
737
738
  integer(kind=PY_SSIZE_T_KIND) :: r
end function

function PyMapping_HasKey(o, a_value) bind(c, name="PyMapping_HasKey") result(r)
  import c_ptr, C_INT
  type(c_ptr), value :: o
739
  type(c_ptr), value :: a_value
Marco Govoni's avatar
Marco Govoni committed
740
741
742
743
744
745
  integer(kind=C_INT) :: r
end function

function PySequence_Concat(o1, o2) bind(c, name="PySequence_Concat") result(r)
  import c_ptr
  type(c_ptr), value :: o1, o2
746
  type(c_ptr) :: r
Marco Govoni's avatar
Marco Govoni committed
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
end function

!PyObject *PySys_GetObject(const char *name)
function PySys_GetObject(a_name) bind(c, name="PySys_GetObject") result(r)
  import c_ptr, C_CHAR
  character(kind=C_CHAR), dimension(*) :: a_name
  type(c_ptr) :: r
end function

#ifndef PYTHON2
#ifndef Py_DEBUG
function PyModule_Create2(def, module_api_version) bind(c, name="PyModule_Create2") result(r)
#else
function PyModule_Create2(def, module_api_version) bind(c, name="PyModule_Create2TraceRefs") result(r)
#endif
  import c_ptr, C_INT
  type(c_ptr), value :: def
  integer(kind=C_INT), value :: module_api_version
  type(c_ptr) :: r
end function
#endif

#ifdef PYTHON2
!Python 2 only
!PyObject* Py_InitModule4(char *name, PyMethodDef *methods, char *doc, PyObject *self, int apiver)
#ifndef PYTHON2_32
#ifndef Py_DEBUG
function Py_InitModule4(a_name, methods, doc, self, apiver) bind(c, name="Py_InitModule4_64") result(r)
#else
function Py_InitModule4(a_name, methods, doc, self, apiver) bind(c, name="Py_InitModule4TraceRefs_64") result(r)
#endif
#else
#ifndef Py_DEBUG
function Py_InitModule4(a_name, methods, doc, self, apiver) bind(c, name="Py_InitModule4") result(r)
#else
function Py_InitModule4(a_name, methods, doc, self, apiver) bind(c, name="Py_InitModule4TraceRefs") result(r)
#endif
#endif
  import c_ptr, C_CHAR, C_INT
  character(kind=C_CHAR), dimension(*) :: a_name
  type(c_ptr), value :: methods
  character(kind=C_CHAR), dimension(*) :: doc
  type(c_ptr), value :: self
  integer(kind=C_INT), value :: apiver
  type(c_ptr) :: r
end function
#endif

!int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
function PyModule_AddObject(a_module, a_name, a_value) bind(c, name="PyModule_AddObject") result(r)
  import c_ptr, C_CHAR, C_INT
  type(c_ptr), value :: a_module
  character(kind=C_CHAR), dimension(*) :: a_name
  type(c_ptr), value :: a_value
  integer(kind=C_INT) :: r
end function

#ifdef PYTHON2
! Old-style Python2-only buffer protocol API function
!PyObject* PyBuffer_FromReadWriteMemory(void *ptr, Py_ssize_t size)
function PyBuffer_FromReadWriteMemory(ptr, the_size) bind(c, name="PyBuffer_FromReadWriteMemory") result(r)
  import c_ptr, PY_SSIZE_T_KIND
  type(c_ptr), value :: ptr
  integer(kind=PY_SSIZE_T_KIND), value :: the_size
  type(c_ptr) :: r
end function
#endif

function strcmp(s1, s2) bind(c) result(r)
  import c_ptr, C_INT
  type(c_ptr), value :: s1, s2
  integer(kind=C_INT) :: r
end function

end interface

interface box_value
#ifdef PYTHON2
  module procedure box_value_int32
  module procedure box_value_int64
#else
  module procedure box_value_int32_as_long
  module procedure box_value_int64_as_long
#endif
  module procedure box_value_real32
  module procedure box_value_real64
  module procedure box_value_complex_real32
  module procedure box_value_complex_real64
  module procedure box_value_logical
  module procedure box_value_chars
  module procedure box_value_char_1d
end interface

interface unbox_value
  module procedure unbox_value_int32
  module procedure unbox_value_int64
  module procedure unbox_value_real32
  module procedure unbox_value_real64
  module procedure unbox_value_complex_real32
  module procedure unbox_value_complex_real64
  module procedure unbox_value_logical
848
849
850
#ifdef PYTHON2
  module procedure unbox_value_chars_py2
#else
Marco Govoni's avatar
Marco Govoni committed
851
  module procedure unbox_value_chars
852
#endif
Marco Govoni's avatar
Marco Govoni committed
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
  module procedure unbox_value_char_1d
end interface

interface tuple_from_array
  module procedure tuple_from_array_int32
  module procedure tuple_from_array_int64
end interface

!--------- High-level API to Python's datastructures -------------------

!> Type to represent an arbitrary Python object
type object
  private
  type(c_ptr) :: py_object = C_NULL_PTR

  contains
    !> Call to allow for freeing of resources of this object.
    procedure, public :: destroy => object_destroy
    !> Get value of an attribute of this object
    procedure, public :: getattribute => object_getattribute  ! TODO: make generic?
    !> Set value of an attribute of this object
    procedure, public :: setattr => object_setattr
    !> Delete an attribute of this object
    procedure, public :: delattr => object_delattr
    !> Get c_ptr representation of this object. For development of Python extension modules
    procedure, public :: get_c_ptr => object_get_c_ptr
end type

!> Type that represents a "class object". In Python class objects have the type 'type' which we name type_py here.
type, extends(object) :: type_py

end type

!> Abstract type that represents sequence objects. Elements of a sequence can be accessed by an index.
type, abstract, extends(object) :: Sequence
  contains
      procedure, private :: sequence_len_int32
      !> Get the length of the object (number of elements).
      generic, public :: len => sequence_len_int32
      procedure, private :: sequence_count_int32
      generic, public :: count => sequence_count_int32
894

Marco Govoni's avatar
Marco Govoni committed
895
896
897
898
      !index - does not support optional start and stop indices as the Python function does
      procedure, private :: sequence_index_int32
      !> Get the first index of a value.
      generic, public :: index => sequence_index_int32
899

Marco Govoni's avatar
Marco Govoni committed
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
      procedure, private :: sequence_getitem_int32_object
      !> Get item at a certain index
      generic, public :: getitem => sequence_getitem_int32_object
        procedure, private :: sequence_getitem_int32_int32
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int32_int32
        procedure, private :: sequence_getitem_int32_int64
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int32_int64
        procedure, private :: sequence_getitem_int32_real32
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int32_real32
        procedure, private :: sequence_getitem_int32_real64
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int32_real64
        procedure, private :: sequence_getitem_int32_complex_real32
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int32_complex_real32
        procedure, private :: sequence_getitem_int32_complex_real64
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int32_complex_real64
        procedure, private :: sequence_getitem_int32_logical
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int32_logical
        procedure, private :: sequence_getitem_int32_char_1d
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int32_char_1d
        procedure, private :: sequence_getitem_int32_chars
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int32_chars

      procedure, private :: sequence_len_int64
      !> Get the length of the object (number of elements).
      generic, public :: len => sequence_len_int64
      procedure, private :: sequence_count_int64
      generic, public :: count => sequence_count_int64
936

Marco Govoni's avatar
Marco Govoni committed
937
938
939
940
      !index - does not support optional start and stop indices as the Python function does
      procedure, private :: sequence_index_int64
      !> Get the first index of a value.
      generic, public :: index => sequence_index_int64
941

Marco Govoni's avatar
Marco Govoni committed
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
      procedure, private :: sequence_getitem_int64_object
      !> Get item at a certain index
      generic, public :: getitem => sequence_getitem_int64_object
        procedure, private :: sequence_getitem_int64_int32
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int64_int32
        procedure, private :: sequence_getitem_int64_int64
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int64_int64
        procedure, private :: sequence_getitem_int64_real32
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int64_real32
        procedure, private :: sequence_getitem_int64_real64
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int64_real64
        procedure, private :: sequence_getitem_int64_complex_real32
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int64_complex_real32
        procedure, private :: sequence_getitem_int64_complex_real64
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int64_complex_real64
        procedure, private :: sequence_getitem_int64_logical
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int64_logical
        procedure, private :: sequence_getitem_int64_char_1d
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int64_char_1d
        procedure, private :: sequence_getitem_int64_chars
        !> Get item at a certain index
        generic, public :: getitem => sequence_getitem_int64_chars

    !> Checks if a given item is contained in the sequence.
    procedure, public :: contains => sequence_contains
end type

!> Abstract type that represents a sequence, whose items can be changed.
type, abstract, extends(Sequence) :: MutableSequence
  contains
    procedure, private :: mutablesequence_setitem_int32_object
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int32_object
    procedure, private :: mutablesequence_setitem_int32_int32
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int32_int32
    procedure, private :: mutablesequence_setitem_int32_int64
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int32_int64
    procedure, private :: mutablesequence_setitem_int32_real32
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int32_real32
    procedure, private :: mutablesequence_setitem_int32_real64
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int32_real64
    procedure, private :: mutablesequence_setitem_int32_complex_real32
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int32_complex_real32
    procedure, private :: mutablesequence_setitem_int32_complex_real64
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int32_complex_real64
    procedure, private :: mutablesequence_setitem_int32_logical
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int32_logical
    procedure, private :: mutablesequence_setitem_int32_char_1d
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int32_char_1d
    procedure, private :: mutablesequence_setitem_int32_chars
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int32_chars
    procedure, private :: mutablesequence_setitem_int64_object
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int64_object
    procedure, private :: mutablesequence_setitem_int64_int32
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int64_int32
    procedure, private :: mutablesequence_setitem_int64_int64
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int64_int64
    procedure, private :: mutablesequence_setitem_int64_real32
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int64_real32
    procedure, private :: mutablesequence_setitem_int64_real64
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int64_real64
    procedure, private :: mutablesequence_setitem_int64_complex_real32
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int64_complex_real32
    procedure, private :: mutablesequence_setitem_int64_complex_real64
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int64_complex_real64
    procedure, private :: mutablesequence_setitem_int64_logical
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int64_logical
    procedure, private :: mutablesequence_setitem_int64_char_1d
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int64_char_1d
    procedure, private :: mutablesequence_setitem_int64_chars
    !> Set an item at a given index
    generic, public :: setitem => mutablesequence_setitem_int64_chars
end type

!> Abstract type that represents a sequence, whose items can not be changed.
type, abstract, extends(Sequence) :: ImmutableSequence

end type

!> Type that corresponds to a Python list.
type, extends(MutableSequence) :: list
  contains
    procedure, private :: list_append_object
    !> Append an item at the end of a list
    generic, public :: append => list_append_object
1053

Marco Govoni's avatar
Marco Govoni committed
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
    !> Creates a copy of a list
    procedure, public :: copy => list_copy
    !> Sorts the list.
    procedure, public :: sort => list_sort
    !> Reverses a list.
    procedure, public :: reverse => list_reverse
    !> Concatenates another list at the end of a list.
    procedure, public :: add => list_add

    procedure, private :: list_insert_int32
    !> Inserts item at given index.
    generic, public :: insert => list_insert_int32
    procedure, private :: list_delitem_int32
    !> Deletes item at given index from list.
    generic, public :: delitem => list_delitem_int32
    procedure, private :: list_insert_int64
    !> Inserts item at given index.
    generic, public :: insert => list_insert_int64
    procedure, private :: list_delitem_int64
    !> Deletes item at given index from list.
    generic, public :: delitem => list_delitem_int64
1075

Marco Govoni's avatar
Marco Govoni committed
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
    procedure, private :: list_append_int32
    !> Append an item at the end of a list
    generic, public :: append => list_append_int32
    procedure, private :: list_append_int64
    !> Append an item at the end of a list
    generic, public :: append => list_append_int64
    procedure, private :: list_append_real32
    !> Append an item at the end of a list
    generic, public :: append => list_append_real32
    procedure, private :: list_append_real64
    !> Append an item at the end of a list
    generic, public :: append => list_append_real64
    procedure, private :: list_append_complex_real32
    !> Append an item at the end of a list
    generic, public :: append => list_append_complex_real32
    procedure, private :: list_append_complex_real64
    !> Append an item at the end of a list
    generic, public :: append => list_append_complex_real64
    procedure, private :: list_append_logical
    !> Append an item at the end of a list
    generic, public :: append => list_append_logical
    procedure, private :: list_append_char_1d
    !> Append an item at the end of a list
    generic, public :: append => list_append_char_1d
    procedure, private :: list_append_chars
    !> Append an item at the end of a list
    generic, public :: append => list_append_chars

end type

!> Abstract type that represents a datastructure that maps keys to values.
type, abstract, extends(object) :: Mapping
  contains
     procedure, private :: mapping_getitem_object_object
     !> Get value at a given key. KeyError if key does not exist
1111
     generic, public :: getitem => mapping_getitem_object_object
Marco Govoni's avatar
Marco Govoni committed
1112
1113
1114
1115
1116
1117
     procedure, private :: mapping_setitem_object_object
     !> Inserts value at given key. Sets value if key already exists.
     generic, public :: setitem => mapping_setitem_object_object
     procedure, private :: mapping_delitem_object
     !> Delete key-value pair with given key.
     generic, public :: delitem => mapping_delitem_object
1118

Marco Govoni's avatar
Marco Govoni committed
1119
1120
       procedure, private :: mapping_getitem_int32_object
       !> Get value at a given key. KeyError if key does not exist
1121
       generic, public :: getitem => mapping_getitem_int32_object
Marco Govoni's avatar
Marco Govoni committed
1122
1123
1124
1125
1126
1127
1128
1129
       procedure, private :: mapping_setitem_int32_object
       !> Inserts value at given key. Sets value if key already exists.
       generic, public :: setitem => mapping_setitem_int32_object
       !> Delete key-value pair with given key.
       procedure, private :: mapping_delitem_int32
       generic, public :: delitem => mapping_delitem_int32
         procedure, private :: mapping_getitem_int32_int32
         !> Get value at a given key. KeyError if key does not exist
1130
         generic, public :: getitem => mapping_getitem_int32_int32
Marco Govoni's avatar
Marco Govoni committed
1131
1132
1133
1134
1135
         procedure, private :: mapping_setitem_int32_int32
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int32_int32
         procedure, private :: mapping_getitem_int32_int64
         !> Get value at a given key. KeyError if key does not exist
1136
         generic, public :: getitem => mapping_getitem_int32_int64
Marco Govoni's avatar
Marco Govoni committed
1137
1138
1139
1140
1141
         procedure, private :: mapping_setitem_int32_int64
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int32_int64
         procedure, private :: mapping_getitem_int32_real32
         !> Get value at a given key. KeyError if key does not exist
1142
         generic, public :: getitem => mapping_getitem_int32_real32
Marco Govoni's avatar
Marco Govoni committed
1143
1144
1145
1146
1147
         procedure, private :: mapping_setitem_int32_real32
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int32_real32
         procedure, private :: mapping_getitem_int32_real64
         !> Get value at a given key. KeyError if key does not exist
1148
         generic, public :: getitem => mapping_getitem_int32_real64
Marco Govoni's avatar
Marco Govoni committed
1149
1150
1151
1152
1153
         procedure, private :: mapping_setitem_int32_real64
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int32_real64
         procedure, private :: mapping_getitem_int32_complex_real32
         !> Get value at a given key. KeyError if key does not exist
1154
         generic, public :: getitem => mapping_getitem_int32_complex_real32
Marco Govoni's avatar
Marco Govoni committed
1155
1156
1157
1158
1159
         procedure, private :: mapping_setitem_int32_complex_real32
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int32_complex_real32
         procedure, private :: mapping_getitem_int32_complex_real64
         !> Get value at a given key. KeyError if key does not exist
1160
         generic, public :: getitem => mapping_getitem_int32_complex_real64
Marco Govoni's avatar
Marco Govoni committed
1161
1162
1163
1164
1165
         procedure, private :: mapping_setitem_int32_complex_real64
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int32_complex_real64
         procedure, private :: mapping_getitem_int32_logical
         !> Get value at a given key. KeyError if key does not exist
1166
         generic, public :: getitem => mapping_getitem_int32_logical
Marco Govoni's avatar
Marco Govoni committed
1167
1168
1169
1170
1171
         procedure, private :: mapping_setitem_int32_logical
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int32_logical
         procedure, private :: mapping_getitem_int32_char_1d
         !> Get value at a given key. KeyError if key does not exist
1172
         generic, public :: getitem => mapping_getitem_int32_char_1d
Marco Govoni's avatar
Marco Govoni committed
1173
1174
1175
1176
1177
         procedure, private :: mapping_setitem_int32_char_1d
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int32_char_1d
         procedure, private :: mapping_getitem_int32_chars
         !> Get value at a given key. KeyError if key does not exist
1178
         generic, public :: getitem => mapping_getitem_int32_chars
Marco Govoni's avatar
Marco Govoni committed
1179
1180
1181
1182
1183
         procedure, private :: mapping_setitem_int32_chars
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int32_chars
       procedure, private :: mapping_getitem_int64_object
       !> Get value at a given key. KeyError if key does not exist
1184
       generic, public :: getitem => mapping_getitem_int64_object
Marco Govoni's avatar
Marco Govoni committed
1185
1186
1187
1188
1189
1190
1191
1192
       procedure, private :: mapping_setitem_int64_object
       !> Inserts value at given key. Sets value if key already exists.
       generic, public :: setitem => mapping_setitem_int64_object
       !> Delete key-value pair with given key.
       procedure, private :: mapping_delitem_int64
       generic, public :: delitem => mapping_delitem_int64
         procedure, private :: mapping_getitem_int64_int32
         !> Get value at a given key. KeyError if key does not exist
1193
         generic, public :: getitem => mapping_getitem_int64_int32
Marco Govoni's avatar
Marco Govoni committed
1194
1195
1196
1197
1198
         procedure, private :: mapping_setitem_int64_int32
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int64_int32
         procedure, private :: mapping_getitem_int64_int64
         !> Get value at a given key. KeyError if key does not exist
1199
         generic, public :: getitem => mapping_getitem_int64_int64
Marco Govoni's avatar
Marco Govoni committed
1200
1201
1202
1203
1204
         procedure, private :: mapping_setitem_int64_int64
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int64_int64
         procedure, private :: mapping_getitem_int64_real32
         !> Get value at a given key. KeyError if key does not exist
1205
         generic, public :: getitem => mapping_getitem_int64_real32
Marco Govoni's avatar
Marco Govoni committed
1206
1207
1208
1209
1210
         procedure, private :: mapping_setitem_int64_real32
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int64_real32
         procedure, private :: mapping_getitem_int64_real64
         !> Get value at a given key. KeyError if key does not exist
1211
         generic, public :: getitem => mapping_getitem_int64_real64
Marco Govoni's avatar
Marco Govoni committed
1212
1213
1214
1215
1216
         procedure, private :: mapping_setitem_int64_real64
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int64_real64
         procedure, private :: mapping_getitem_int64_complex_real32
         !> Get value at a given key. KeyError if key does not exist
1217
         generic, public :: getitem => mapping_getitem_int64_complex_real32
Marco Govoni's avatar
Marco Govoni committed
1218
1219
1220
1221
1222
         procedure, private :: mapping_setitem_int64_complex_real32
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int64_complex_real32
         procedure, private :: mapping_getitem_int64_complex_real64
         !> Get value at a given key. KeyError if key does not exist
1223
         generic, public :: getitem => mapping_getitem_int64_complex_real64
Marco Govoni's avatar
Marco Govoni committed
1224
1225
1226
1227
1228
         procedure, private :: mapping_setitem_int64_complex_real64
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int64_complex_real64
         procedure, private :: mapping_getitem_int64_logical
         !> Get value at a given key. KeyError if key does not exist
1229
         generic, public :: getitem => mapping_getitem_int64_logical
Marco Govoni's avatar
Marco Govoni committed
1230
1231
1232
1233
1234
         procedure, private :: mapping_setitem_int64_logical
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int64_logical
         procedure, private :: mapping_getitem_int64_char_1d
         !> Get value at a given key. KeyError if key does not exist
1235
         generic, public :: getitem => mapping_getitem_int64_char_1d
Marco Govoni's avatar
Marco Govoni committed
1236
1237
1238
1239
1240
         procedure, private :: mapping_setitem_int64_char_1d
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int64_char_1d
         procedure, private :: mapping_getitem_int64_chars
         !> Get value at a given key. KeyError if key does not exist
1241
         generic, public :: getitem => mapping_getitem_int64_chars
Marco Govoni's avatar
Marco Govoni committed
1242
1243
1244
1245
1246
         procedure, private :: mapping_setitem_int64_chars
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_int64_chars
       procedure, private :: mapping_getitem_chars_object
       !> Get value at a given key. KeyError if key does not exist
1247
       generic, public :: getitem => mapping_getitem_chars_object
Marco Govoni's avatar
Marco Govoni committed
1248
1249
1250
1251
1252
1253
1254
1255
       procedure, private :: mapping_setitem_chars_object
       !> Inserts value at given key. Sets value if key already exists.
       generic, public :: setitem => mapping_setitem_chars_object
       !> Delete key-value pair with given key.
       procedure, private :: mapping_delitem_chars
       generic, public :: delitem => mapping_delitem_chars
         procedure, private :: mapping_getitem_chars_int32
         !> Get value at a given key. KeyError if key does not exist
1256
         generic, public :: getitem => mapping_getitem_chars_int32
Marco Govoni's avatar
Marco Govoni committed
1257
1258
1259
1260
1261
         procedure, private :: mapping_setitem_chars_int32
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_chars_int32
         procedure, private :: mapping_getitem_chars_int64
         !> Get value at a given key. KeyError if key does not exist
1262
         generic, public :: getitem => mapping_getitem_chars_int64
Marco Govoni's avatar
Marco Govoni committed
1263
1264
1265
1266
1267
         procedure, private :: mapping_setitem_chars_int64
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_chars_int64
         procedure, private :: mapping_getitem_chars_real32
         !> Get value at a given key. KeyError if key does not exist
1268
         generic, public :: getitem => mapping_getitem_chars_real32
Marco Govoni's avatar
Marco Govoni committed
1269
1270
1271
1272
1273
         procedure, private :: mapping_setitem_chars_real32
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_chars_real32
         procedure, private :: mapping_getitem_chars_real64
         !> Get value at a given key. KeyError if key does not exist
1274
         generic, public :: getitem => mapping_getitem_chars_real64
Marco Govoni's avatar
Marco Govoni committed
1275
1276
1277
1278
1279
         procedure, private :: mapping_setitem_chars_real64
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_chars_real64
         procedure, private :: mapping_getitem_chars_complex_real32
         !> Get value at a given key. KeyError if key does not exist
1280
         generic, public :: getitem => mapping_getitem_chars_complex_real32
Marco Govoni's avatar
Marco Govoni committed
1281
1282
1283
1284
1285
         procedure, private :: mapping_setitem_chars_complex_real32
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_chars_complex_real32
         procedure, private :: mapping_getitem_chars_complex_real64
         !> Get value at a given key. KeyError if key does not exist
1286
         generic, public :: getitem => mapping_getitem_chars_complex_real64
Marco Govoni's avatar
Marco Govoni committed
1287
1288
1289
1290
1291
         procedure, private :: mapping_setitem_chars_complex_real64
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_chars_complex_real64
         procedure, private :: mapping_getitem_chars_logical
         !> Get value at a given key. KeyError if key does not exist
1292
         generic, public :: getitem => mapping_getitem_chars_logical
Marco Govoni's avatar
Marco Govoni committed
1293
1294
1295
1296
1297
         procedure, private :: mapping_setitem_chars_logical
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_chars_logical
         procedure, private :: mapping_getitem_chars_char_1d
         !> Get value at a given key. KeyError if key does not exist
1298
         generic, public :: getitem => mapping_getitem_chars_char_1d
Marco Govoni's avatar
Marco Govoni committed
1299
1300
1301
1302
1303
         procedure, private :: mapping_setitem_chars_char_1d
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_chars_char_1d
         procedure, private :: mapping_getitem_chars_chars
         !> Get value at a given key. KeyError if key does not exist
1304
         generic, public :: getitem => mapping_getitem_chars_chars
Marco Govoni's avatar
Marco Govoni committed
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
         procedure, private :: mapping_setitem_chars_chars
         !> Inserts value at given key. Sets value if key already exists.
         generic, public :: setitem => mapping_setitem_chars_chars

      procedure, private :: mapping_len_int32
      !> Get number of key-value pairs.
      generic, public :: len => mapping_len_int32
      procedure, private :: mapping_len_int64
      !> Get number of key-value pairs.
      generic, public :: len => mapping_len_int64

    !> Checks if key is contained in datastructure.
    procedure, public :: mapping_contains
1318

Marco Govoni's avatar
Marco Govoni committed
1319
1320
1321
1322
1323
1324
1325
1326
end type

type, extends(Mapping) :: dict
  contains
      !> Removes all key-value pairs from dictionary.
      procedure, public :: clear => dict_clear
      !> Creates a copy of dict
      procedure, public :: copy => dict_copy
1327
      !> Creates a list of a dict's keys
Marco Govoni's avatar
Marco Govoni committed
1328
1329
1330
1331
1332
1333
      procedure, public :: keys => dict_keys
      !> Creates a list of a dict's key-value pairs.
      procedure, public :: items => dict_items
      !> Creates a list of a dict's values
      procedure, public :: values => dict_values

1334
      procedure, private :: dict_get_object_object
Marco Govoni's avatar
Marco Govoni committed
1335
      !> Get value at a given key. If key does not exist, return a default value.
1336
1337
      generic, public :: get => dict_get_object_object
        procedure, private :: dict_get_int32_object
Marco Govoni's avatar
Marco Govoni committed
1338
1339
        !> Get value at a given key. If key does not exist, return a default value.
        generic, public :: get => dict_get_int32_object
1340
        procedure, private :: dict_get_int32_int32
Marco Govoni's avatar
Marco Govoni committed
1341
        !> Get value at a given key. If key does not exist, return a default value.
1342
1343
        generic, public :: get => dict_get_int32_int32
        procedure, private :: dict_get_int32_int64
Marco Govoni's avatar
Marco Govoni committed
1344
        !> Get value at a given key. If key does not exist, return a default value.
1345
1346
        generic, public :: get => dict_get_int32_int64
        procedure, private :: dict_get_int32_real32
Marco Govoni's avatar
Marco Govoni committed
1347
        !> Get value at a given key. If key does not exist, return a default value.
1348
1349
        generic, public :: get => dict_get_int32_real32
        procedure, private :: dict_get_int32_real64
Marco Govoni's avatar
Marco Govoni committed
1350
        !> Get value at a given key. If key does not exist, return a default value.
1351
1352
        generic, public :: get => dict_get_int32_real64
        procedure, private :: dict_get_int32_complex_real32
Marco Govoni's avatar
Marco Govoni committed
1353
        !> Get value at a given key. If key does not exist, return a default value.
1354
1355
        generic, public :: get => dict_get_int32_complex_real32
        procedure, private :: dict_get_int32_complex_real64
Marco Govoni's avatar
Marco Govoni committed
1356
        !> Get value at a given key. If key does not exist, return a default value.
1357
1358
        generic, public :: get => dict_get_int32_complex_real64
        procedure, private :: dict_get_int32_logical
Marco Govoni's avatar
Marco Govoni committed
1359
        !> Get value at a given key. If key does not exist, return a default value.
1360
1361
        generic, public :: get => dict_get_int32_logical
        procedure, private :: dict_get_int32_char_1d
Marco Govoni's avatar
Marco Govoni committed
1362
        !> Get value at a given key. If key does not exist, return a default value.
1363
1364
        generic, public :: get => dict_get_int32_char_1d
        procedure, private :: dict_get_int32_chars
Marco Govoni's avatar
Marco Govoni committed
1365
        !> Get value at a given key. If key does not exist, return a default value.
1366
1367
        generic, public :: get => dict_get_int32_chars
        procedure, private :: dict_get_int64_object
Marco Govoni's avatar
Marco Govoni committed
1368
1369
        !> Get value at a given key. If key does not exist, return a default value.
        generic, public :: get => dict_get_int64_object
1370
        procedure, private :: dict_get_int64_int32
Marco Govoni's avatar
Marco Govoni committed
1371
        !> Get value at a given key. If key does not exist, return a default value.
1372
1373
        generic, public :: get => dict_get_int64_int32
        procedure, private :: dict_get_int64_int64
Marco Govoni's avatar
Marco Govoni committed
1374
        !> Get value at a given key. If key does not exist, return a default value.
1375
1376
        generic, public :: get => dict_get_int64_int64
        procedure, private :: dict_get_int64_real32
Marco Govoni's avatar
Marco Govoni committed
1377
        !> Get value at a given key. If key does not exist, return a default value.
1378
1379
        generic, public :: get => dict_get_int64_real32
        procedure, private :: dict_get_int64_real64
Marco Govoni's avatar
Marco Govoni committed
1380
        !> Get value at a given key. If key does not exist, return a default value.
1381
1382
        generic, public :: get => dict_get_int64_real64
        procedure, private :: dict_get_int64_complex_real32
Marco Govoni's avatar
Marco Govoni committed
1383
        !> Get value at a given key. If key does not exist, return a default value.
1384
1385
        generic, public :: get => dict_get_int64_complex_real32
        procedure, private :: dict_get_int64_complex_real64
Marco Govoni's avatar
Marco Govoni committed
1386
        !> Get value at a given key. If key does not exist, return a default value.
1387
1388
        generic, public :: get => dict_get_int64_complex_real64
        procedure, private :: dict_get_int64_logical
Marco Govoni's avatar
Marco Govoni committed
1389
        !> Get value at a given key. If key does not exist, return a default value.
1390
1391
        generic, public :: get => dict_get_int64_logical
        procedure, private :: dict_get_int64_char_1d
Marco Govoni's avatar
Marco Govoni committed
1392
        !> Get value at a given key. If key does not exist, return a default value.
1393
1394
        generic, public :: get => dict_get_int64_char_1d
        procedure, private :: dict_get_int64_chars
Marco Govoni's avatar
Marco Govoni committed
1395
        !> Get value at a given key. If key does not exist, return a default value.
1396
1397
        generic, public :: get => dict_get_int64_chars
        procedure, private :: dict_get_chars_object
Marco Govoni's avatar
Marco Govoni committed
1398
1399
        !> Get value at a given key. If key does not exist, return a default value.
        generic, public :: get => dict_get_chars_object
1400
        procedure, private :: dict_get_chars_int32
Marco Govoni's avatar
Marco Govoni committed
1401
        !> Get value at a given key. If key does not exist, return a default value.
1402
1403
        generic, public :: get => dict_get_chars_int32
        procedure, private :: dict_get_chars_int64
Marco Govoni's avatar
Marco Govoni committed
1404
        !> Get value at a given key. If key does not exist, return a default value.
1405
1406
        generic, public :: get => dict_get_chars_int64
        procedure, private :: dict_get_chars_real32
Marco Govoni's avatar
Marco Govoni committed
1407
        !> Get value at a given key. If key does not exist, return a default value.
1408
1409
        generic, public :: get => dict_get_chars_real32
        procedure, private :: dict_get_chars_real64
Marco Govoni's avatar
Marco Govoni committed
1410
        !> Get value at a given key. If key does not exist, return a default value.
1411
1412
        generic, public :: get => dict_get_chars_real64
        procedure, private :: dict_get_chars_complex_real32
Marco Govoni's avatar
Marco Govoni committed
1413
        !> Get value at a given key. If key does not exist, return a default value.
1414
1415
        generic, public :: get => dict_get_chars_complex_real32
        procedure, private :: dict_get_chars_complex_real64
Marco Govoni's avatar
Marco Govoni committed
1416
        !> Get value at a given key. If key does not exist, return a default value.
1417
1418
        generic, public :: get => dict_get_chars_complex_real64
        procedure, private :: dict_get_chars_logical
Marco Govoni's avatar
Marco Govoni committed
1419
        !> Get value at a given key. If key does not exist, return a default value.
1420
1421
        generic, public :: get => dict_get_chars_logical
        procedure, private :: dict_get_chars_char_1d
Marco Govoni's avatar
Marco Govoni committed
1422
        !> Get value at a given key. If key does not exist, return a default value.
1423
1424
        generic, public :: get => dict_get_chars_char_1d
        procedure, private :: dict_get_chars_chars
Marco Govoni's avatar
Marco Govoni committed
1425
        !> Get value at a given key. If key does not exist, return a default value.
1426
        generic, public :: get => dict_get_chars_chars
Marco Govoni's avatar
Marco Govoni committed
1427
1428

      !> Get value at a given key. If key does not exist, set value at key to default value and return default.
1429
      procedure, public :: setdefault => dict_setdefault_object_object
Marco Govoni's avatar
Marco Govoni committed
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
end type

!> Type that corresponds to a Python tuple. Create with tuple_create.
type, extends(ImmutableSequence) :: tuple
  contains
    procedure, private :: tuple_setitem_int32_object
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int32_object
    procedure, private :: tuple_setitem_int32_int32
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int32_int32
    procedure, private :: tuple_setitem_int32_int64
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int32_int64
    procedure, private :: tuple_setitem_int32_real32
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int32_real32
    procedure, private :: tuple_setitem_int32_real64
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int32_real64
    procedure, private :: tuple_setitem_int32_complex_real32
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int32_complex_real32
    procedure, private :: tuple_setitem_int32_complex_real64
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int32_complex_real64
    procedure, private :: tuple_setitem_int32_logical
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int32_logical
    procedure, private :: tuple_setitem_int32_char_1d
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int32_char_1d
    procedure, private :: tuple_setitem_int32_chars
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int32_chars
    procedure, private :: tuple_setitem_int64_object
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int64_object
    procedure, private :: tuple_setitem_int64_int32
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int64_int32
    procedure, private :: tuple_setitem_int64_int64
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int64_int64
    procedure, private :: tuple_setitem_int64_real32
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int64_real32
    procedure, private :: tuple_setitem_int64_real64
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int64_real64
    procedure, private :: tuple_setitem_int64_complex_real32
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int64_complex_real32
    procedure, private :: tuple_setitem_int64_complex_real64
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int64_complex_real64
    procedure, private :: tuple_setitem_int64_logical
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int64_logical
    procedure, private :: tuple_setitem_int64_char_1d
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int64_char_1d
    procedure, private :: tuple_setitem_int64_chars
    !> Sets item at given index. One must set all items before use of tuple.
    generic, public :: setitem => tuple_setitem_int64_chars

    !> Concatenates tuples.
    procedure, public :: add => tuple_add
end type

!> Creates a tuple with a given number of items.
interface tuple_create
  module procedure tuple_create_int32
  module procedure tuple_create_int64
  module procedure tuple_create_object
end interface

!> Creates a list. Create as empty list or from other object.
interface list_create
  module procedure list_create_empty
  module procedure list_create_object
end interface

!> Type corresponding to Python 'bytes'.
type, extends(ImmutableSequence) :: bytes

end type

!> Creates a bytes object from Fortran character string or character array.
interface bytes_create
  module procedure bytes_create_chars
1521
  module procedure bytes_create_char_1d
Marco Govoni's avatar
Marco Govoni committed
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
end interface

!> Type corresponding to Python 'str' - Python's string type.
type, extends(ImmutableSequence) :: str

end type

!> Creates a str object from Fortran character string or character array.
interface str_create
  module procedure str_create_chars
1532
1533
  module procedure str_create_char_1d
  module procedure str_create_object
Marco Govoni's avatar
Marco Govoni committed
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
end interface

!> Type corresponding to Python 2 'unicode' or Python 3 'str'.
type, extends(ImmutableSequence) :: unicode

end type

!> Creates a unicode string from Fortran character string or character array.
interface unicode_create
  module procedure unicode_create_chars
1544
  module procedure unicode_create_char_1d
Marco Govoni's avatar
Marco Govoni committed
1545
1546
end interface