-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathPyType.cpp
97 lines (81 loc) · 2.95 KB
/
PyType.cpp
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
87
88
89
90
91
92
93
94
95
96
97
/*
* File: PyType.cpp
* Author: Kent D. Lee
* (c) 2013
* Created on February 12, 2013, 10:58 PM
*
* License:
* Please read the LICENSE file in this distribution for details regarding
* the licensing of this code. This code is freely available for educational
* use. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
*
* Description:
* See the associated header file for a description of the purpose of this
* class. Implementation details are provided here. Read below for
* any specific details.
*
*/
#include "PyObject.h"
#include "PyType.h"
#include "PyException.h"
#include "PyStr.h"
#include <iostream>
#include <sstream>
using namespace std;
PyType::PyType(string typeString, PyTypeId id) : PyCallable() {
this->typeString = typeString;
this->index = id;
}
PyType::~PyType() {
}
string PyType::toString() {
return this->typeString;
}
PyType* PyType::getType() {
return PyTypes[PyTypeType];
}
PyTypeId PyType::typeId() {
return index;
}
string PyType::callName() {
return "type";
}
/* The following method is called when calling one of the magic methods
* on an instance of this type. Only magic methods that are defined
* in the dictionary are allowed. Magic methods are added to the dictionary
* in the initTypes function (in main.cpp) and are added by calling the addFun
* method above.
*/
PyObject* PyType::__str__(vector<PyObject*>* args) {
return new PyStr("<class '" +toString()+"'>");
}
PyObject* PyType::__type__(vector<PyObject*>* args) {
ostringstream msg;
if (args->size() != 0) {
msg << "TypeError: expected 0 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
return PyTypes[PyTypeType];
}
/* Not to be confused with the callMethod method, the __call__ method is called
* by callMethod when the type is called as in int(x) for example. Type conversion is
* handled by calling the __bool__, __int__, __float__, or __str__ method on
* the instance's class. A by-product of this is that __type__ can get called
* on objects within the system. The __type__ magic method was added for this
* implementation to support the implementation of __call__ below. When __type__
* is called on a non-type object it is handled in the PyObject class by
* returning the type of the object. When called on a type, the __type__ function
* returns the instance of the type "type" (seen above). This is exactly as
* the real Python interpreter behaves.
*/
PyObject* PyType::__call__(vector<PyObject*>* args) {
ostringstream msg;
if (args->size() != 1) {
msg << "TypeError: expected 1 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
vector<PyObject*>* emptyArgs = new vector<PyObject*>();
PyObject* arg = (*args)[0];
string funName = "__"+this->toString()+"__";
return arg->callMethod(funName,emptyArgs);
}