File: ret.py

package info (click to toggle)
cypari2 2.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 908 kB
  • sloc: python: 972; makefile: 34; ansic: 5
file content (86 lines) | stat: -rw-r--r-- 2,358 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Return types for PARI calls
"""

#*****************************************************************************
#       Copyright (C) 2015 Jeroen Demeyer <jdemeyer@cage.ugent.be>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#                  http://www.gnu.org/licenses/
#*****************************************************************************

from __future__ import unicode_literals

class PariReturn(object):
    """
    This class represents the return value of a PARI call.
    """
    def __init__(self):
        self.name = "_ret"

    def __repr__(self):
        return self.ctype()

    def ctype(self):
        """
        Return the C type of the result of the PARI call.
        """
        raise NotImplementedError

    def assign_code(self, value):
        """
        Return code to assign the result of the PARI call in ``value``
        to the variable named ``self.name``.
        """
        s  = "        cdef {ctype} {name} = {value}\n"
        return s.format(ctype=self.ctype(), name=self.name, value=value)

    def return_code(self):
        """
        Return code to return from the Cython wrapper.
        """
        s  = "        clear_stack()\n"
        s += "        return {name}\n"
        return s.format(name=self.name)


class PariReturnGEN(PariReturn):
    def ctype(self):
        return "GEN"
    def return_code(self):
        s = "        return new_gen({name})\n"
        return s.format(name=self.name)

class PariReturnInt(PariReturn):
    def ctype(self):
        return "int"

class PariReturnLong(PariReturn):
    def ctype(self):
        return "long"

class PariReturnULong(PariReturn):
    def ctype(self):
        return "unsigned long"

class PariReturnVoid(PariReturn):
    def ctype(self):
        return "void"
    def assign_code(self, value):
        return "        {value}\n".format(value=value)
    def return_code(self):
        s = "        clear_stack()\n"
        return s


pari_ret_types = {
        '':  PariReturnGEN,
        'm': PariReturnGEN,
        'i': PariReturnInt,
        'l': PariReturnLong,
        'u': PariReturnULong,
        'v': PariReturnVoid,
        }