Skip to content

Commit 0bc46a5

Browse files
authored
Merge approach to exceptions and subclasses
Candidate approach to exceptions proved on branch. The journey has brought forward a lot of VSJ3 code and useful new work towards subclasses defined in Python. From PR #5.
2 parents 5a0c4b4 + 240b059 commit 0bc46a5

File tree

90 files changed

+24409
-1389
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+24409
-1389
lines changed

docs/src/site/sphinx/plain-java-object-2/_plain-java-object-2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ We elaborate on these aims in the :doc:`introduction`.
2525
introduction
2626
basic-patterns
2727
object-and-pytype
28+
subclasses-in-python
2829
object-and-pytype-java
2930
type-system-init

docs/src/site/sphinx/plain-java-object-2/basic-patterns.rst

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
``Object`` and ``PyType`` Basic Patterns
55
****************************************
66

7-
8-
.. note:: Check for ``PyBaseObject``
9-
10-
11-
127
In this section we set out the plain Java object approach
138
to representing Python objects.
149
We did this already in ``rt3``,
@@ -54,8 +49,8 @@ or possibly a cascade of inter-dependent ``PyType``\s.
5449
We must guard against the possibility that
5550
some other thread may already be doing overlapping work.
5651

57-
Types of Type
58-
-------------
52+
Classes of Type
53+
---------------
5954

6055
Type objects fall into three categories,
6156
which have distinct Java implementations:
@@ -99,10 +94,13 @@ Primary Representation
9994
The *primary* representation class is a (least derived) Java class,
10095
instances of which are acceptable as
10196
the ``self`` argument to methods of the type.
97+
(Notice we use the term *representation* in two ways:
98+
for the Java representation class, and
99+
for the ``Representation`` object.)
102100

103101
In the case of a **simple type**,
104102
all representation classes for the type have a common root class,
105-
which is the primary representation.
103+
which is the primary representation class.
106104
For example, all three classes of type object extend ``PyType``,
107105
the primary representation of ``type``.
108106

@@ -118,12 +116,13 @@ Canonical Base Class
118116
Secondly, we need the idea of a *canonical base* class.
119117
This is the Java class on which the implementation of
120118
a subclass defined in Python will be based.
121-
Specific Java classes may have to be defined
119+
Specific Java classes are needed
122120
as representations of the Python subclasses,
123121
but all will be subclasses in Java of the canonical base.
124122

125123
Instances of subclasses of the canonical base
126124
must be acceptable as a ``self`` argument to methods.
125+
The reason for this is in the definition of instance methods.
127126
When a Python subclass,
128127
seeking a method along the MRO finds it in a type object,
129128
it will call the implementation matching the primary representation.
@@ -149,8 +148,22 @@ as they are mostly ``final``.
149148
As so often, ``object`` is an exception,
150149
where the canonical class is ``java.lang.Object``.
151150

151+
A Java subclass of the canonical base of a type is either
152+
treated as representing the same type
153+
(by being mapped to the same ``Representation``), or
154+
the representation of a Python subclass of the type.
155+
The choice is made when we register the Java class to either
156+
the existing ``Representation`` or
157+
create a new representation for the Python subclass.
158+
When the type in question is a replaceable type,
159+
since equivalent types share the same canonical base,
160+
the Java subclass maps either
161+
to the ``SharedRepresentation`` of the type(s),or
162+
to new ``SharedRepresentation`` defining an equivalence.
163+
152164
.. note::
153-
Not sure about the detail of this.
165+
The detail of this is gradually being confirmed
166+
in the implementation of Python exceptions and sublasses.
154167
The concept is right,
155168
but does the canonical base have properties that make it
156169
always a "designer" class?
@@ -249,7 +262,7 @@ is itself the ``PyType``.
249262

250263
abstract class Representation {
251264
pythonType(o)
252-
javaType()
265+
javaClass()
253266
}
254267

255268
abstract class PyType {
@@ -317,23 +330,24 @@ cites the same ``Representation``.
317330

318331
abstract class Representation {
319332
pythonType(o)
320-
javaType()
333+
javaClass()
321334
}
322335

323336
abstract class PyType {
324337
getDict()
325338
lookup(attr)
326339
}
327340

328-
interface WithType {
341+
interface WithClassAssignment {
329342
getType()
343+
setType(t)
330344
}
331345

332-
T .up.|> WithType
346+
T .up.|> WithClassAssignment
333347
T --> ReplaceableType
334348

335349
SharedRepresentation -up-|> Representation
336-
SharedRepresentation "1" -- "*" ReplaceableType
350+
SharedRepresentation "1" <-- "*" ReplaceableType
337351
338352
' Representation <|-- PyType
339353
PyType --|> Representation
@@ -344,7 +358,7 @@ cites the same ``Representation``.
344358
Instances of a class defined in Python
345359
(by a ``class`` statement),
346360
that have no built-in types in their MRO but ``object``,
347-
will have the Java class ``PyBaseObject`` for ``T``.
361+
will have the Java class ``PyObject`` for ``T``.
348362
In general, ``T`` will be a Java *extension point* subclass of
349363
the representation of the most-derived common ancestor.
350364
(The case of mutiple Java bases needs investigation.)
@@ -354,7 +368,8 @@ and what ``__class__`` assignments are allowed.
354368
We observe that in CPython,
355369
acceptable values for ``__class__``
356370
define an equivalence relation amongst Python classes.
357-
Let :math:`R(A,B)` be the statement ``A.__class__ = B.__class__`` is allowed.
371+
Let :math:`R(A,B)` be the statement that
372+
``A.__class__ = B.__class__`` is allowed.
358373
Then :math:`R(A,A)`,
359374
:math:`R(A,B) ⇒ R(B,A)`,
360375
and :math:`R(A,B) ∧ R(B,C) ⇒ R(A,C)`.
@@ -419,7 +434,7 @@ leading to Python type ``type``.
419434

420435
abstract class Representation {
421436
pythonType(o)
422-
javaType()
437+
javaClass()
423438
}
424439

425440
abstract class PyType {

docs/src/site/sphinx/plain-java-object-2/object-and-pytype-java.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ We now attempt that extra step.
1616
The Challenge of Subclasses
1717
===========================
1818

19-
We ended the last section with a table of
19+
We ended a previous section with a table of
2020
sample Python types and their representations.
2121
In Jython we also need type objects to represent "found" Java types,
2222
that is, where the Java class is not one for which
@@ -47,7 +47,7 @@ For example, the MRO of ``java.util.ArrayList`` in Jython 2.7.4 is:
4747
<type 'java.io.Serializable'>, <type 'java.lang.Object'>,
4848
<type 'object'>)
4949
50-
It surely cannot differ much from this in Jython 3.
50+
The MRO might differ somewhat in Jython 3.
5151

5252
It seems obvious that each Java class we encounter
5353
should have a type object we could import by that name.

0 commit comments

Comments
 (0)